mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-16 09:38:05 +00:00
COVAR
semver fix VAR test fix new: COVAR, ZSCORE, CORR, LINREG versioning refactoring
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
/* <summary>
|
||||
BIAS: Rate of change between the source and a moving average.
|
||||
@@ -23,17 +24,11 @@ public class BIAS_Series : Single_TSeries_Indicator
|
||||
|
||||
public override void Add((System.DateTime t, double v) TValue, bool update)
|
||||
{
|
||||
if (update) { this._buffer[this._buffer.Count - 1] = TValue.v; }
|
||||
else { this._buffer.Add(TValue.v); }
|
||||
if (this._buffer.Count > this._p && this._p != 0) { this._buffer.RemoveAt(0); }
|
||||
Add_Replace_Trim(_buffer, TValue.v, _p, update);
|
||||
|
||||
double _sma = 0;
|
||||
for (int i = 0; i < this._buffer.Count; i++) { _sma += this._buffer[i]; }
|
||||
_sma /= this._buffer.Count;
|
||||
double _bias = (this._buffer[this._buffer.Count - 1] / ((_sma != 0) ? _sma : 1)) - 1;
|
||||
double _sma = _buffer.Average();
|
||||
double _bias = (_buffer[_buffer.Count - 1] / ((_sma != 0) ? _sma : 1)) - 1;
|
||||
|
||||
var result = (TValue.t, (this.Count < this._p - 1 && this._NaN) ? double.NaN : _bias);
|
||||
|
||||
base.Add(result, update);
|
||||
base.Add((TValue.t, _bias), update, _NaN);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
/* <summary>
|
||||
CORR: Pearson's Correlation Coefficient
|
||||
@@ -14,57 +16,37 @@ Sources:
|
||||
</summary> */
|
||||
|
||||
public class CORR_Series : Pair_TSeries_Indicator
|
||||
{
|
||||
public CORR_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<double> _x = new();
|
||||
private readonly System.Collections.Generic.List<double> _xx = new();
|
||||
private readonly System.Collections.Generic.List<double> _y = new();
|
||||
private readonly System.Collections.Generic.List<double> _yy = new();
|
||||
private readonly System.Collections.Generic.List<double> _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 _div = (_sumxx - _sumx * _sumx / _p) * (_sumyy - _sumy * _sumy / _p);
|
||||
double _cor = (_div != 0) ? (_sumxy - _sumx * _sumy / _p) / Math.Sqrt(_div) : 0.0;
|
||||
|
||||
var result = (TValue1.t, (this.Count < this._p - 1 && this._NaN) ? double.NaN : _cor);
|
||||
if (update) { base[base.Count - 1] = result; } else { base.Add(result); }
|
||||
{
|
||||
public CORR_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<double> _x = new();
|
||||
private readonly System.Collections.Generic.List<double> _xx = new();
|
||||
private readonly System.Collections.Generic.List<double> _y = new();
|
||||
private readonly System.Collections.Generic.List<double> _yy = new();
|
||||
private readonly System.Collections.Generic.List<double> _xy = new();
|
||||
|
||||
public override void Add((System.DateTime t, double v) TValue1, (System.DateTime t, double v) TValue2, bool update)
|
||||
{
|
||||
Add_Replace_Trim(_x, TValue1.v, _p, update);
|
||||
Add_Replace_Trim(_xx, TValue1.v * TValue1.v, _p, update);
|
||||
Add_Replace_Trim(_y, TValue2.v, _p, update);
|
||||
Add_Replace_Trim(_yy, TValue2.v * TValue2.v, _p, update);
|
||||
Add_Replace_Trim(_xy, TValue1.v * TValue2.v, _p, update);
|
||||
|
||||
double _sumx = _x.Sum();
|
||||
double _sumxx = _xx.Sum();
|
||||
double _sumy = _y.Sum();
|
||||
double _sumyy = _yy.Sum();
|
||||
double _sumxy = _xy.Sum();
|
||||
|
||||
double _covar = (_sumxx - _sumx * _sumx / _p) * (_sumyy - _sumy * _sumy / _p);
|
||||
double _cor = (_covar != 0) ? (_sumxy - _sumx * _sumy / _p) / Math.Sqrt(_covar) : 0.0;
|
||||
|
||||
var result = (TValue1.t, (this.Count < this._p - 1 && this._NaN) ? double.NaN : _cor);
|
||||
if (update) { base[base.Count - 1] = result; } else { base.Add(result); }
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
/* <summary>
|
||||
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
|
||||
|
||||
</summary> */
|
||||
|
||||
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<double> _x = new();
|
||||
private readonly System.Collections.Generic.List<double> _y = new();
|
||||
private readonly System.Collections.Generic.List<double> _xy = new();
|
||||
|
||||
public override void Add((System.DateTime t, double v) TValue1, (System.DateTime t, double v) TValue2, bool update)
|
||||
{
|
||||
Add_Replace_Trim(_x, TValue1.v, _p, update);
|
||||
Add_Replace_Trim(_y, TValue2.v, _p, update);
|
||||
Add_Replace_Trim(_xy, TValue1.v * TValue2.v, _p, update);
|
||||
|
||||
double _avgx = _x.Average();
|
||||
double _avgy = _y.Average();
|
||||
double _avgxy = _xy.Average();
|
||||
double _covar = _avgxy - (_avgx * _avgy);
|
||||
|
||||
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); }
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
/* <summary>
|
||||
ENTP: Entropy
|
||||
@@ -16,9 +17,9 @@ Sources:
|
||||
|
||||
</summary> */
|
||||
|
||||
public class ENTP_Series : Single_TSeries_Indicator
|
||||
public class ENTROPY_Series : Single_TSeries_Indicator
|
||||
{
|
||||
public ENTP_Series(TSeries source, int period, double logbase = 2.0, bool useNaN = false) : base(source, period, useNaN)
|
||||
public ENTROPY_Series(TSeries source, int period, double logbase = 2.0, bool useNaN = false) : base(source, period, useNaN)
|
||||
{
|
||||
this._logbase = logbase;
|
||||
if (base._data.Count > 0) { base.Add(base._data); }
|
||||
@@ -29,24 +30,15 @@ public class ENTP_Series : Single_TSeries_Indicator
|
||||
|
||||
public override void Add((System.DateTime t, double v) TValue, bool update)
|
||||
{
|
||||
if (update) { this._buffer[this._buffer.Count - 1] = TValue.v; }
|
||||
else { this._buffer.Add(TValue.v); }
|
||||
if (this._buffer.Count > this._p && this._p != 0) { this._buffer.RemoveAt(0); }
|
||||
|
||||
double _sum = 0;
|
||||
for (int i = 0; i < this._buffer.Count; i++) { _sum += this._buffer[i]; }
|
||||
|
||||
Add_Replace_Trim(_buffer, TValue.v, _p, update);
|
||||
double _sum = _buffer.Sum();
|
||||
|
||||
double _pp = this._buffer[this._buffer.Count - 1] / _sum;
|
||||
double _ppp = -_pp * Math.Log(_pp) / Math.Log(this._logbase);
|
||||
|
||||
if (update) { this._buff2[this._buff2.Count - 1] = _ppp; }
|
||||
else { this._buff2.Add(_ppp); }
|
||||
if (this._buff2.Count > this._p && this._p != 0) { this._buff2.RemoveAt(0); }
|
||||
Add_Replace_Trim(_buff2, _ppp, _p, update);
|
||||
double _entp = _buff2.Sum();
|
||||
|
||||
double _entp = 0;
|
||||
for (int i = 0; i < this._buff2.Count; i++) { _entp += this._buff2[i]; }
|
||||
|
||||
var result = (TValue.t, (this.Count < this._p - 1 && this._NaN) ? double.NaN : _entp);
|
||||
base.Add(result, update);
|
||||
base.Add((TValue.t, _entp), update, _NaN);
|
||||
}
|
||||
}
|
||||
@@ -1,62 +1,57 @@
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
|
||||
/* <summary>
|
||||
KURT: Kurtosis of population
|
||||
Kurtosis characterizes the relative peakedness or flatness of a distribution
|
||||
compared with the normal distribution. Positive kurtosis indicates a relatively
|
||||
peaked distribution. Negative kurtosis indicates a relatively flat distribution.
|
||||
|
||||
The normal curve is called Mesokurtic curve. If the curve of a distribution is
|
||||
more outlier prone (or heavier-tailed) than a normal or mesokurtic curve then
|
||||
it is referred to as a Leptokurtic curve. If a curve is less outlier prone (or
|
||||
lighter-tailed) than a normal curve, it is called as a platykurtic curve.
|
||||
|
||||
Calculation:
|
||||
sum4 = Σ(close-SMA)^4
|
||||
sum2 = (Σ(close-SMA)^2)^2
|
||||
KURT = length * (sum4/sum2)
|
||||
|
||||
Sources:
|
||||
https://en.wikipedia.org/wiki/Kurtosis
|
||||
https://stats.oarc.ucla.edu/other/mult-pkg/faq/general/faq-whats-with-the-different-formulas-for-kurtosis/
|
||||
|
||||
</summary> */
|
||||
|
||||
public class KURT_Series : Single_TSeries_Indicator
|
||||
{
|
||||
public KURT_Series(TSeries source, int period, double logbase = 2.0, bool useNaN = false) : base(source, period, useNaN)
|
||||
{
|
||||
this._logbase = logbase;
|
||||
if (base._data.Count > 0) { base.Add(base._data); }
|
||||
}
|
||||
protected double _logbase;
|
||||
private readonly System.Collections.Generic.List<double> _buffer = new();
|
||||
|
||||
public override void Add((System.DateTime t, double v) TValue, bool update)
|
||||
{
|
||||
if (update) { this._buffer[this._buffer.Count - 1] = TValue.v; }
|
||||
else { this._buffer.Add(TValue.v); }
|
||||
if (this._buffer.Count > this._p && this._p != 0) { this._buffer.RemoveAt(0); }
|
||||
|
||||
double _n = this._buffer.Count;
|
||||
|
||||
double _avg = 0;
|
||||
for (int i = 0; i < this._buffer.Count; i++) { _avg += this._buffer[i]; }
|
||||
_avg /= _n;
|
||||
|
||||
double _s2 = 0;
|
||||
double _s4 = 0;
|
||||
for (int i = 0; i < this._buffer.Count; i++)
|
||||
{
|
||||
_s2 += (this._buffer[i] - _avg) * (this._buffer[i] - _avg);
|
||||
_s4 += (this._buffer[i] - _avg) * (this._buffer[i] - _avg) * (this._buffer[i] - _avg) * (this._buffer[i] - _avg);
|
||||
}
|
||||
|
||||
double _Vx = _s2 / (_n - 1);
|
||||
double _kurt = (_n > 3) ? ((((_n * (_n + 1)) / (((_n - 1) * (_n - 2)) * (_n - 3))) * (_s4 / (_Vx * _Vx))) - (3 * (((_n - 1) * (_n - 1)) / ((_n - 2) * (_n - 3))))) : Double.NaN;
|
||||
|
||||
var result = (TValue.t, (this.Count < this._p - 1 && this._NaN) ? Double.NaN : _kurt);
|
||||
base.Add(result, update);
|
||||
}
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
/* <summary>
|
||||
KURT: Kurtosis of population
|
||||
Kurtosis characterizes the relative peakedness or flatness of a distribution
|
||||
compared with the normal distribution. Positive kurtosis indicates a relatively
|
||||
peaked distribution. Negative kurtosis indicates a relatively flat distribution.
|
||||
|
||||
The normal curve is called Mesokurtic curve. If the curve of a distribution is
|
||||
more outlier prone (or heavier-tailed) than a normal or mesokurtic curve then
|
||||
it is referred to as a Leptokurtic curve. If a curve is less outlier prone (or
|
||||
lighter-tailed) than a normal curve, it is called as a platykurtic curve.
|
||||
|
||||
Calculation:
|
||||
sum4 = Σ(close-SMA)^4
|
||||
sum2 = (Σ(close-SMA)^2)^2
|
||||
KURT = length * (sum4/sum2)
|
||||
|
||||
Sources:
|
||||
https://en.wikipedia.org/wiki/Kurtosis
|
||||
https://stats.oarc.ucla.edu/other/mult-pkg/faq/general/faq-whats-with-the-different-formulas-for-kurtosis/
|
||||
|
||||
</summary> */
|
||||
|
||||
public class KURTOSIS_Series : Single_TSeries_Indicator
|
||||
{
|
||||
public KURTOSIS_Series(TSeries source, int period, double logbase = 2.0, bool useNaN = false) : base(source, period, useNaN)
|
||||
{
|
||||
this._logbase = logbase;
|
||||
if (base._data.Count > 0) { base.Add(base._data); }
|
||||
}
|
||||
protected double _logbase;
|
||||
private readonly System.Collections.Generic.List<double> _buffer = new();
|
||||
|
||||
public override void Add((System.DateTime t, double v) TValue, bool update)
|
||||
{
|
||||
Add_Replace_Trim(_buffer, TValue.v, _p, update);
|
||||
double _n = this._buffer.Count;
|
||||
double _avg = _buffer.Average();
|
||||
|
||||
double _s2 = 0;
|
||||
double _s4 = 0;
|
||||
for (int i = 0; i < this._buffer.Count; i++)
|
||||
{
|
||||
_s2 += (_buffer[i] - _avg) * (_buffer[i] - _avg);
|
||||
_s4 += (_buffer[i] - _avg) * (_buffer[i] - _avg) * (_buffer[i] - _avg) * (_buffer[i] - _avg);
|
||||
}
|
||||
|
||||
double _Vx = _s2 / (_n - 1);
|
||||
double _kurt = (_n > 3) ? ((((_n * (_n + 1)) / (((_n - 1) * (_n - 2)) * (_n - 3))) * (_s4 / (_Vx * _Vx))) - (3 * (((_n - 1) * (_n - 1)) / ((_n - 2) * (_n - 3))))) : Double.NaN;
|
||||
|
||||
var result = (TValue.t, (this.Count < this._p - 1 && this._NaN) ? Double.NaN : _kurt);
|
||||
base.Add(result, update);
|
||||
}
|
||||
}
|
||||
@@ -34,9 +34,7 @@ public class LINREG_Series : Single_TSeries_Indicator
|
||||
|
||||
public override void Add((System.DateTime t, double v) TValue, bool update)
|
||||
{
|
||||
if (update) { this._buffer[this._buffer.Count - 1] = TValue.v; }
|
||||
else { this._buffer.Add(TValue.v); }
|
||||
if (this._buffer.Count > this._p) { this._buffer.RemoveAt(0); }
|
||||
Add_Replace_Trim(_buffer, TValue.v, _p, update);
|
||||
|
||||
int _len = this._buffer.Count;
|
||||
|
||||
@@ -79,7 +77,7 @@ public class LINREG_Series : Single_TSeries_Indicator
|
||||
double _RSquared = arrr * arrr;
|
||||
|
||||
var ret = (TValue.t, this.Count < this._p - 1 && this._NaN ? double.NaN : _slope);
|
||||
base.Add(ret, update);
|
||||
base.Add(ret, update, _NaN);
|
||||
|
||||
ret = (TValue.t, this.Count < this._p - 1 && this._NaN ? double.NaN : _intercept);
|
||||
Intercept.Add(ret, update);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
/* <summary>
|
||||
MAD: Mean Absolute Deviation
|
||||
@@ -24,19 +25,14 @@ public class MAD_Series : Single_TSeries_Indicator
|
||||
|
||||
public override void Add((System.DateTime t, double v) TValue, bool update)
|
||||
{
|
||||
if (update) { this._buffer[this._buffer.Count - 1] = TValue.v; }
|
||||
else { _buffer.Add(TValue.v); }
|
||||
if (_buffer.Count > this._p && this._p != 0) { _buffer.RemoveAt(0); }
|
||||
Add_Replace_Trim(_buffer, TValue.v, _p, update);
|
||||
|
||||
double _sma = 0;
|
||||
for (int i = 0; i < _buffer.Count; i++) { _sma += _buffer[i]; }
|
||||
_sma /= this._buffer.Count;
|
||||
double _sma = _buffer.Average();
|
||||
|
||||
double _mad = 0;
|
||||
for (int i = 0; i < _buffer.Count; i++) { _mad += Math.Abs(_buffer[i] - _sma); }
|
||||
_mad /= this._buffer.Count;
|
||||
|
||||
var result = (TValue.t, (this.Count < this._p - 1 && this._NaN) ? double.NaN : _mad);
|
||||
base.Add(result, update);
|
||||
base.Add((TValue.t, _mad), update, _NaN);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
/* <summary>
|
||||
MAPE: Mean Absolute Percentage Error
|
||||
@@ -27,19 +28,15 @@ public class MAPE_Series : Single_TSeries_Indicator
|
||||
|
||||
public override void Add((System.DateTime t, double v) TValue, bool update)
|
||||
{
|
||||
if (update) { _buffer[_buffer.Count - 1] = TValue.v; }
|
||||
else { _buffer.Add(TValue.v); }
|
||||
if (_buffer.Count > this._p && this._p != 0) { _buffer.RemoveAt(0); }
|
||||
|
||||
double _sma = 0;
|
||||
for (int i = 0; i < _buffer.Count; i++) { _sma += _buffer[i]; }
|
||||
_sma /= this._buffer.Count;
|
||||
Add_Replace_Trim(_buffer, TValue.v, _p, update);
|
||||
double _sma = _buffer.Average();
|
||||
|
||||
double _mape = 0;
|
||||
for (int i = 0; i < _buffer.Count; i++) { _mape += (_buffer[i] != 0) ? Math.Abs(_buffer[i] - _sma) / Math.Abs(_buffer[i]) : double.PositiveInfinity; }
|
||||
_mape /= this._buffer.Count;
|
||||
for (int i = 0; i < _buffer.Count; i++) {
|
||||
_mape += (_buffer[i] != 0) ? Math.Abs(_buffer[i] - _sma) / Math.Abs(_buffer[i]) : double.PositiveInfinity;
|
||||
}
|
||||
_mape /= (_buffer.Count>0) ? _buffer.Count : 1;
|
||||
|
||||
var result = (TValue.t, (this.Count < this._p - 1 && this._NaN) ? double.NaN : _mape);
|
||||
base.Add(result, update);
|
||||
base.Add((TValue.t, _mape), update, _NaN);
|
||||
}
|
||||
}
|
||||
@@ -1,47 +1,44 @@
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
|
||||
/* <summary>
|
||||
MED - Median value
|
||||
Median of numbers is the middlemost value of the given set of numbers.
|
||||
It separates the higher half and the lower half of a given data sample.
|
||||
At least half of the observations are smaller than or equal to median
|
||||
and at least half of the observations are greater than or equal to the median.
|
||||
|
||||
If the number of values is odd, the middlemost observation of the sorted
|
||||
list is the median of the given data. If the number of values is even,
|
||||
median is the average of (n/2)th and [(n/2) + 1]th values of the sorted list.
|
||||
|
||||
If period = 0 => period is max
|
||||
|
||||
Sources:
|
||||
https://corporatefinanceinstitute.com/resources/knowledge/other/median/
|
||||
https://en.wikipedia.org/wiki/Median
|
||||
|
||||
</summary> */
|
||||
|
||||
public class MED_Series : Single_TSeries_Indicator
|
||||
{
|
||||
public MED_Series(TSeries source, int period, bool useNaN = false) : base(source, period, useNaN)
|
||||
{
|
||||
if (base._data.Count > 0) { base.Add(base._data); }
|
||||
}
|
||||
private readonly System.Collections.Generic.List<double> _buffer = new();
|
||||
|
||||
public override void Add((System.DateTime t, double v) TValue, bool update)
|
||||
{
|
||||
if (update) { this._buffer[this._buffer.Count - 1] = TValue.v; }
|
||||
else { this._buffer.Add(TValue.v); }
|
||||
if (this._buffer.Count > this._p && this._p != 0) { this._buffer.RemoveAt(0); }
|
||||
|
||||
System.Collections.Generic.List<double> _s = new(this._buffer);
|
||||
_s.Sort();
|
||||
int _p1 = _s.Count / 2;
|
||||
int _p2 = Math.Max(0, (_s.Count / 2) - 1);
|
||||
double _med = (_s.Count % 2 != 0) ? _s[_p1] : (_s[_p1] + _s[_p2]) / 2;
|
||||
|
||||
var result = (TValue.t, (this.Count < this._p - 1 && this._NaN) ? double.NaN : _med);
|
||||
|
||||
base.Add(result, update);
|
||||
}
|
||||
}
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
using static System.Net.Mime.MediaTypeNames;
|
||||
|
||||
/* <summary>
|
||||
MED - Median value
|
||||
Median of numbers is the middlemost value of the given set of numbers.
|
||||
It separates the higher half and the lower half of a given data sample.
|
||||
At least half of the observations are smaller than or equal to median
|
||||
and at least half of the observations are greater than or equal to the median.
|
||||
|
||||
If the number of values is odd, the middlemost observation of the sorted
|
||||
list is the median of the given data. If the number of values is even,
|
||||
median is the average of (n/2)th and [(n/2) + 1]th values of the sorted list.
|
||||
|
||||
If period = 0 => period is max
|
||||
|
||||
Sources:
|
||||
https://corporatefinanceinstitute.com/resources/knowledge/other/median/
|
||||
https://en.wikipedia.org/wiki/Median
|
||||
|
||||
</summary> */
|
||||
|
||||
public class MEDIAN_Series : Single_TSeries_Indicator
|
||||
{
|
||||
public MEDIAN_Series(TSeries source, int period, bool useNaN = false) : base(source, period, useNaN)
|
||||
{
|
||||
if (base._data.Count > 0) { base.Add(base._data); }
|
||||
}
|
||||
private readonly System.Collections.Generic.List<double> _buffer = new();
|
||||
|
||||
public override void Add((System.DateTime t, double v) TValue, bool update)
|
||||
{
|
||||
Add_Replace_Trim(_buffer, TValue.v, _p, update);
|
||||
|
||||
System.Collections.Generic.List<double> _s = new(this._buffer);
|
||||
_s.Sort();
|
||||
int _p1 = _s.Count / 2;
|
||||
int _p2 = Math.Max(0, (_s.Count / 2) - 1);
|
||||
double _med = (_s.Count % 2 != 0) ? _s[_p1] : (_s[_p1] + _s[_p2]) / 2;
|
||||
|
||||
base.Add((TValue.t, _med), update, _NaN);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
/* <summary>
|
||||
MSE: Mean Square Error
|
||||
@@ -20,19 +21,13 @@ public class MSE_Series : Single_TSeries_Indicator
|
||||
|
||||
public override void Add((System.DateTime t, double v) TValue, bool update)
|
||||
{
|
||||
if (update) { _buffer[_buffer.Count - 1] = TValue.v; }
|
||||
else { _buffer.Add(TValue.v); }
|
||||
if (_buffer.Count > this._p && this._p != 0) { _buffer.RemoveAt(0); }
|
||||
|
||||
double _sma = 0;
|
||||
for (int i = 0; i < _buffer.Count; i++) { _sma += _buffer[i]; }
|
||||
_sma /= this._buffer.Count;
|
||||
Add_Replace_Trim(_buffer, TValue.v, _p, update);
|
||||
double _sma = _buffer.Average();
|
||||
|
||||
double _mse = 0;
|
||||
for (int i = 0; i < _buffer.Count; i++) { _mse += (_buffer[i] - _sma) * (_buffer[i] - _sma); }
|
||||
_mse /= this._buffer.Count;
|
||||
|
||||
var result = (TValue.t, (this.Count < this._p - 1 && this._NaN) ? double.NaN : _mse);
|
||||
base.Add(result, update);
|
||||
base.Add((TValue.t, _mse), update, _NaN);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
/* <summary>
|
||||
SDEV: Population Standard Deviation
|
||||
@@ -25,20 +26,14 @@ public class SDEV_Series : Single_TSeries_Indicator
|
||||
|
||||
public override void Add((System.DateTime t, double v) TValue, bool update)
|
||||
{
|
||||
if (update) { _buffer[_buffer.Count - 1] = TValue.v; }
|
||||
else { _buffer.Add(TValue.v); }
|
||||
if (_buffer.Count > this._p && this._p != 0) { _buffer.RemoveAt(0); }
|
||||
|
||||
double _sma = 0;
|
||||
for (int i = 0; i < _buffer.Count; i++) { _sma += _buffer[i]; }
|
||||
_sma /= this._buffer.Count;
|
||||
Add_Replace_Trim(_buffer, TValue.v, _p, update);
|
||||
double _sma = _buffer.Average();
|
||||
|
||||
double _pvar = 0;
|
||||
for (int i = 0; i < _buffer.Count; i++) { _pvar += (_buffer[i] - _sma) * (_buffer[i] - _sma); }
|
||||
_pvar /= this._buffer.Count;
|
||||
double _psdev = Math.Sqrt(_pvar);
|
||||
|
||||
var result = (TValue.t, (this.Count < this._p - 1 && this._NaN) ? double.NaN : _psdev);
|
||||
base.Add(result, update);
|
||||
base.Add((TValue.t, _psdev), update, _NaN);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
/* <summary>
|
||||
SMAPE: Symmetric Mean Absolute Percentage Error
|
||||
@@ -20,19 +21,13 @@ public class SMAPE_Series : Single_TSeries_Indicator
|
||||
|
||||
public override void Add((System.DateTime t, double v) TValue, bool update)
|
||||
{
|
||||
if (update) { _buffer[_buffer.Count - 1] = TValue.v; }
|
||||
else { _buffer.Add(TValue.v); }
|
||||
if (_buffer.Count > this._p && this._p != 0) { _buffer.RemoveAt(0); }
|
||||
|
||||
double _sma = 0;
|
||||
for (int i = 0; i < _buffer.Count; i++) { _sma += _buffer[i]; }
|
||||
_sma /= this._buffer.Count;
|
||||
Add_Replace_Trim(_buffer, TValue.v, _p, update);
|
||||
double _sma = _buffer.Average();
|
||||
|
||||
double _smape = 0;
|
||||
for (int i = 0; i < _buffer.Count; i++) { _smape += Math.Abs(_buffer[i] - _sma) / (Math.Abs(_buffer[i]) + Math.Abs(_sma)); }
|
||||
_smape /= this._buffer.Count;
|
||||
|
||||
var result = (TValue.t, (this.Count < this._p - 1 && this._NaN) ? double.NaN : _smape);
|
||||
base.Add(result, update);
|
||||
base.Add((TValue.t, _smape), update, _NaN);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
/* <summary>
|
||||
SSDEV: (Corrected) Sample Standard Deviation
|
||||
@@ -25,20 +26,14 @@ public class SSDEV_Series : Single_TSeries_Indicator
|
||||
|
||||
public override void Add((System.DateTime t, double v) TValue, bool update)
|
||||
{
|
||||
if (update) { this._buffer[this._buffer.Count - 1] = TValue.v; }
|
||||
else { this._buffer.Add(TValue.v); }
|
||||
if (this._buffer.Count > this._p && this._p != 0) { this._buffer.RemoveAt(0); }
|
||||
|
||||
double _sma = 0;
|
||||
for (int i = 0; i < this._buffer.Count; i++) { _sma += this._buffer[i]; }
|
||||
_sma /= this._buffer.Count;
|
||||
Add_Replace_Trim(_buffer, TValue.v, _p, update);
|
||||
double _sma = _buffer.Average();
|
||||
|
||||
double _svar = 0;
|
||||
for (int i = 0; i < this._buffer.Count; i++) { _svar += (this._buffer[i] - _sma) * (this._buffer[i] - _sma); }
|
||||
_svar /= (this._buffer.Count > 1) ? this._buffer.Count - 1 : 1; // Bessel's correction
|
||||
for (int i = 0; i < this._buffer.Count; i++) { _svar += (_buffer[i] - _sma) * (_buffer[i] - _sma); }
|
||||
_svar /= (_buffer.Count > 1) ? _buffer.Count - 1 : 1; // Bessel's correction
|
||||
double _ssdev = Math.Sqrt(_svar);
|
||||
|
||||
var result = (TValue.t, (this.Count < this._p - 1 && this._NaN) ? double.NaN : _ssdev);
|
||||
base.Add(result, update);
|
||||
base.Add((TValue.t, _ssdev), update, _NaN);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
/* <summary>
|
||||
SVAR: Sample Variance
|
||||
@@ -25,19 +26,13 @@ public class SVAR_Series : Single_TSeries_Indicator
|
||||
|
||||
public override void Add((System.DateTime t, double v) TValue, bool update)
|
||||
{
|
||||
if (update) { this._buffer[this._buffer.Count - 1] = TValue.v; }
|
||||
else { this._buffer.Add(TValue.v); }
|
||||
if (this._buffer.Count > this._p && this._p != 0) { this._buffer.RemoveAt(0); }
|
||||
|
||||
double _sma = 0;
|
||||
for (int i = 0; i < this._buffer.Count; i++) { _sma += this._buffer[i]; }
|
||||
_sma /= this._buffer.Count;
|
||||
Add_Replace_Trim(_buffer, TValue.v, _p, update);
|
||||
double _sma = _buffer.Average();
|
||||
|
||||
double _svar = 0;
|
||||
for (int i = 0; i < this._buffer.Count; i++) { _svar += (this._buffer[i] - _sma) * (this._buffer[i] - _sma); }
|
||||
_svar /= (this._buffer.Count > 1) ? this._buffer.Count - 1 : 1; // Bessel's correction
|
||||
|
||||
var result = (TValue.t, (this.Count < this._p - 1 && this._NaN) ? double.NaN : _svar);
|
||||
base.Add(result, update);
|
||||
base.Add((TValue.t, _svar), update, _NaN);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
/* <summary>
|
||||
VAR: Population Variance
|
||||
@@ -25,19 +26,13 @@ public class VAR_Series : Single_TSeries_Indicator
|
||||
|
||||
public override void Add((System.DateTime t, double v) TValue, bool update)
|
||||
{
|
||||
if (update) { _buffer[_buffer.Count - 1] = TValue.v; }
|
||||
else { _buffer.Add(TValue.v); }
|
||||
if (_buffer.Count > this._p && this._p != 0) { _buffer.RemoveAt(0); }
|
||||
|
||||
double _sma = 0;
|
||||
for (int i = 0; i < _buffer.Count; i++) { _sma += _buffer[i]; }
|
||||
_sma /= this._buffer.Count;
|
||||
Add_Replace_Trim(_buffer, TValue.v, _p, update);
|
||||
double _sma = _buffer.Average();
|
||||
|
||||
double _pvar = 0;
|
||||
for (int i = 0; i < _buffer.Count; i++) { _pvar += (_buffer[i] - _sma) * (_buffer[i] - _sma); }
|
||||
_pvar /= this._buffer.Count;
|
||||
|
||||
var result = (TValue.t, (this.Count < this._p - 1 && this._NaN) ? double.NaN : _pvar);
|
||||
base.Add(result, update);
|
||||
base.Add((TValue.t, _pvar), update, _NaN);
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,12 @@
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
/* <summary>
|
||||
WMAPE: Weighted Mean Absolute Percentage Error
|
||||
Measures the size of the error in percentage terms
|
||||
Measures the size of the error in percentage terms. Improves problems with MAPE
|
||||
when there are zero or close-to-zero values because there would be a division by zero
|
||||
or values of MAPE tending to infinity.
|
||||
|
||||
Sources:
|
||||
https://en.wikipedia.org/wiki/WMAPE
|
||||
@@ -20,13 +23,8 @@ public class WMAPE_Series : Single_TSeries_Indicator
|
||||
|
||||
public override void Add((System.DateTime t, double v) TValue, bool update)
|
||||
{
|
||||
if (update) { _buffer[_buffer.Count - 1] = TValue.v; }
|
||||
else { _buffer.Add(TValue.v); }
|
||||
if (_buffer.Count > this._p && this._p != 0) { _buffer.RemoveAt(0); }
|
||||
|
||||
double _sma = 0;
|
||||
for (int i = 0; i < _buffer.Count; i++) { _sma += _buffer[i]; }
|
||||
_sma /= this._buffer.Count;
|
||||
Add_Replace_Trim(_buffer, TValue.v, _p, update);
|
||||
double _sma = _buffer.Average();
|
||||
|
||||
double _div = 0;
|
||||
double _wmape = 0;
|
||||
@@ -35,9 +33,8 @@ public class WMAPE_Series : Single_TSeries_Indicator
|
||||
_wmape += Math.Abs(_buffer[i] - _sma);
|
||||
_div += Math.Abs(_buffer[i]);
|
||||
}
|
||||
_wmape /= _div;
|
||||
_wmape = (_div!=0) ? _wmape/_div : double.PositiveInfinity;
|
||||
|
||||
var result = (TValue.t, (this.Count < this._p - 1 && this._NaN) ? double.NaN : _wmape);
|
||||
base.Add(result, update);
|
||||
base.Add((TValue.t, _wmape), update, _NaN);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
namespace QuanTAlib;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
/* <summary>
|
||||
ZSCORE: number of standard deviations from SMA
|
||||
@@ -31,13 +32,8 @@ public class ZSCORE_Series : Single_TSeries_Indicator
|
||||
|
||||
public override void Add((System.DateTime t, double v) TValue, bool update)
|
||||
{
|
||||
if (update) { _buffer[_buffer.Count - 1] = TValue.v; }
|
||||
else { _buffer.Add(TValue.v); }
|
||||
if (_buffer.Count > this._p && this._p != 0) { _buffer.RemoveAt(0); }
|
||||
|
||||
double _sma = 0;
|
||||
for (int i = 0; i < _buffer.Count; i++) { _sma += _buffer[i]; }
|
||||
_sma /= this._buffer.Count;
|
||||
Add_Replace_Trim(_buffer, TValue.v, _p, update);
|
||||
double _sma = _buffer.Average();
|
||||
|
||||
double _pvar = 0;
|
||||
for (int i = 0; i < _buffer.Count; i++) { _pvar += (_buffer[i] - _sma) * (_buffer[i] - _sma); }
|
||||
@@ -45,7 +41,6 @@ public class ZSCORE_Series : Single_TSeries_Indicator
|
||||
double _psdev = Math.Sqrt(_pvar);
|
||||
double _zscore = (_psdev == 0) ? double.NaN : (TValue.v - _sma) / _psdev;
|
||||
|
||||
var result = (TValue.t, (this.Count < this._p - 1 && this._NaN) ? double.NaN : _zscore);
|
||||
base.Add(result, update);
|
||||
base.Add((TValue.t, _zscore), update, _NaN);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user