Files
QuanTAlib/Source/Statistics/ENTROPY_Series.cs
T

44 lines
1.4 KiB
C#
Raw Normal View History

2022-04-19 22:34:42 -07:00
namespace QuanTAlib;
using System;
2022-11-17 07:53:45 -08:00
using System.Linq;
2022-04-19 14:35:10 -07:00
2022-04-19 22:34:42 -07:00
/* <summary>
ENTP: Entropy
Introduced by Claude Shannon in 1948, entropy measures the unpredictability
of the data, or equivalently, of its average information.
2022-04-19 14:35:10 -07:00
Calculation:
P = close / Σ(close)
ENTP = Σ(-P * Log(P) / Log(base))
Sources:
https://en.wikipedia.org/wiki/Entropy_(information_theory)
https://math.stackexchange.com/questions/3428693/how-to-calculate-entropy-from-a-set-of-correlated-samples
2022-04-19 22:34:42 -07:00
</summary> */
2022-11-17 07:53:45 -08:00
public class ENTROPY_Series : Single_TSeries_Indicator
2022-04-19 14:35:10 -07:00
{
2022-11-17 07:53:45 -08:00
public ENTROPY_Series(TSeries source, int period, double logbase = 2.0, bool useNaN = false) : base(source, period, useNaN)
2022-04-19 14:35:10 -07:00
{
this._logbase = logbase;
if (base._data.Count > 0) { base.Add(base._data); }
}
private readonly double _logbase;
2022-04-19 14:35:10 -07:00
private readonly System.Collections.Generic.List<double> _buffer = new();
private readonly System.Collections.Generic.List<double> _buff2 = new();
public override void Add((System.DateTime t, double v) TValue, bool update)
{
2022-11-17 07:53:45 -08:00
Add_Replace_Trim(_buffer, TValue.v, _p, update);
double _sum = _buffer.Sum();
2022-04-19 14:35:10 -07:00
double _pp = this._buffer[this._buffer.Count - 1] / _sum;
double _ppp = -_pp * Math.Log(_pp) / Math.Log(this._logbase);
2022-11-17 07:53:45 -08:00
Add_Replace_Trim(_buff2, _ppp, _p, update);
double _entp = _buff2.Sum();
2022-04-19 14:35:10 -07:00
2022-11-17 07:53:45 -08:00
base.Add((TValue.t, _entp), update, _NaN);
2022-04-19 14:35:10 -07:00
}
}