From 3fe639e491416eb1cd8299899b9911c3e1abc883 Mon Sep 17 00:00:00 2001 From: Miha Kralj Date: Tue, 15 Nov 2022 22:02:02 -0800 Subject: [PATCH] COVAR semver fix --- GitVersion.yml | 1 - Source/Statistics/COVAR_Series.cs | 67 ++++++++++++++++++++++++++++++ Tests/Validations/Skender_Stock.cs | 9 ++++ docs/readme.md | 2 +- 4 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 Source/Statistics/COVAR_Series.cs diff --git a/GitVersion.yml b/GitVersion.yml index deb60508..a2c78995 100644 --- a/GitVersion.yml +++ b/GitVersion.yml @@ -1,4 +1,3 @@ -next-version: 0.1.19 minor-version-bump-message: \+semver:\s?(feature|new) branches: main: diff --git a/Source/Statistics/COVAR_Series.cs b/Source/Statistics/COVAR_Series.cs new file mode 100644 index 00000000..06c3463d --- /dev/null +++ b/Source/Statistics/COVAR_Series.cs @@ -0,0 +1,67 @@ +namespace QuanTAlib; +using System; + +/* +COVAR: Covariance + Covariance is defined as the expected value (or mean) of the product + of their deviations from their individual expected values. + +Sources: + https://en.wikipedia.org/wiki/Covariance + + */ + +public class COVAR_Series : Pair_TSeries_Indicator +{ + public COVAR_Series(TSeries d1, TSeries d2, int period, bool useNaN = false) : base(d1, d2, period, useNaN) + { + if (base._d1.Count > 0 && base._d2.Count > 0) { for (int i = 0; i < base._d1.Count; i++) { this.Add(base._d1[i], base._d2[i], false); } } + } + + private readonly System.Collections.Generic.List _x = new(); + private readonly System.Collections.Generic.List _xx = new(); + private readonly System.Collections.Generic.List _y = new(); + private readonly System.Collections.Generic.List _yy = new(); + private readonly System.Collections.Generic.List _xy = new(); + + public override void Add((System.DateTime t, double v) TValue1, (System.DateTime t, double v) TValue2, bool update) + { + if (update) + { + _x[_x.Count - 1] = TValue1.v; + _xx[_xx.Count - 1] = TValue1.v * TValue1.v; + _y[_y.Count - 1] = TValue2.v; + _y[_yy.Count - 1] = TValue2.v * TValue2.v; + _xy[_xy.Count - 1] = TValue1.v * TValue2.v; + } + else + { + _x.Add(TValue1.v); + _xx.Add(TValue1.v * TValue1.v); + _y.Add(TValue2.v); + _yy.Add(TValue2.v * TValue2.v); + _xy.Add(TValue1.v * TValue2.v); + } + if (_x.Count > this._p) { _x.RemoveAt(0); } + if (_xx.Count > this._p) { _xx.RemoveAt(0); } + if (_y.Count > this._p) { _y.RemoveAt(0); } + if (_yy.Count > this._p) { _yy.RemoveAt(0); } + if (_xy.Count > this._p) { _xy.RemoveAt(0); } + + double _sumx = 0; + for (int i = 0; i < _x.Count; i++) { _sumx += _x[i]; } + double _sumxx = 0; + for (int i = 0; i < _xx.Count; i++) { _sumxx += _xx[i]; } + double _sumy = 0; + for (int i = 0; i < _y.Count; i++) { _sumy += _y[i]; } + double _sumyy = 0; + for (int i = 0; i < _yy.Count; i++) { _sumyy += _yy[i]; } + double _sumxy = 0; + for (int i = 0; i < _xy.Count; i++) { _sumxy += _xy[i]; } + + double _covar = (_sumxy / _p) - ((_sumx / _p) * (_sumy / _p)); + + var result = (TValue1.t, (this.Count < this._p - 1 && this._NaN) ? double.NaN : _covar); + if (update) { base[base.Count - 1] = result; } else { base.Add(result); } + } +} \ No newline at end of file diff --git a/Tests/Validations/Skender_Stock.cs b/Tests/Validations/Skender_Stock.cs index c2976b00..fcc27368 100644 --- a/Tests/Validations/Skender_Stock.cs +++ b/Tests/Validations/Skender_Stock.cs @@ -98,6 +98,15 @@ public class Skender_Stock Assert.Equal(Math.Round((double)SK.Last().Mape!, 6), Math.Round(QL.Last().v, 6)); } + [Fact] + public void COVAR() + { + COVAR_Series QL = new(bars.High, bars.Low, period, false); + var SK = quotes.Use(CandlePart.High).GetCorrelation(quotes.Use(CandlePart.Low), period); + + Assert.Equal(Math.Round((double)SK.Last().Covariance!, 6), Math.Round(QL.Last().v, 6)); + } + [Fact] public void CORR() { diff --git a/docs/readme.md b/docs/readme.md index eff2c21c..53c77b6c 100644 --- a/docs/readme.md +++ b/docs/readme.md @@ -56,7 +56,7 @@ See [Getting Started](https://github.com/mihakralj/QuanTAlib/blob/main/Docs/gett | **STATISTICS & NUMERICAL ANALYSIS** | **QuanTAlib** | **TA-LIB** | **Skender** | **Pandas TA** | | ⭐ BIAS - Bias | `BIAS_Series` ||| bias | | ⭐ CORR - Pearson's Correlation Coefficient | `CORR_Series` | CORREL | GetCorrelation || -| ⛔ COVAR - Covariance ||| GetCorrelation || +| ⭐ COVAR - Covariance | `COVAR_Series` || GetCorrelation || | ⭐ ENTP - Entropy | `ENTP_Series` ||| entropy | | ⭐ KURT - Kurtosis | `KURT_Series` ||| kurtosis | | ⭐ LINREG - Linear Regression | `LINREG_Series` || GetSlope ||