Files
QuanTAlib/lib/statistics/Kurtosis.cs
T

111 lines
3.3 KiB
C#
Raw Normal View History

2024-09-22 17:31:24 -07:00
namespace QuanTAlib;
2024-10-05 15:20:13 -07:00
/// <summary>
/// Calculates excess kurtosis using the Sheskin Algorithm.
/// Measures the "tailedness" of the probability distribution of a real-valued random variable.
/// </summary>
2024-09-22 17:31:24 -07:00
public class Kurtosis : AbstractBase
{
2024-09-30 15:53:48 -07:00
private readonly int Period;
2024-09-23 22:08:40 -07:00
private readonly CircularBuffer _buffer;
2024-09-22 17:31:24 -07:00
2024-10-05 15:20:13 -07:00
/// <summary>
/// Initializes a new instance of the Kurtosis class.
/// </summary>
/// <param name="period">The number of data points to consider for calculation.</param>
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown when the period is less than 4.
/// </exception>
2024-10-06 14:44:43 -07:00
public Kurtosis(int period)
2024-09-22 17:31:24 -07:00
{
if (period < 4)
{
2024-10-05 15:20:13 -07:00
throw new ArgumentOutOfRangeException(nameof(period),
"Period must be greater than or equal to 4 for kurtosis calculation.");
2024-09-22 17:31:24 -07:00
}
Period = period;
WarmupPeriod = Period - 1;
_buffer = new CircularBuffer(period);
Name = $"Kurtosis(period={period})";
Init();
}
2024-10-05 15:20:13 -07:00
/// <summary>
/// Initializes a new instance of the Kurtosis class with a data source.
/// </summary>
/// <param name="source">The source object that publishes data.</param>
/// <param name="period">The number of data points to consider.</param>
2024-09-22 17:31:24 -07:00
public Kurtosis(object source, int period) : this(period)
{
var pubEvent = source.GetType().GetEvent("Pub");
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
}
2024-10-05 15:20:13 -07:00
/// <summary>
/// Resets the Kurtosis indicator to its initial state.
/// </summary>
2024-09-22 17:31:24 -07:00
public override void Init()
{
base.Init();
_buffer.Clear();
}
2024-10-05 15:20:13 -07:00
/// <summary>
/// Manages the state of the indicator.
/// </summary>
/// <param name="isNew">Indicates if the current data point is new.</param>
2024-09-22 17:31:24 -07:00
protected override void ManageState(bool isNew)
{
if (isNew)
{
_lastValidValue = Input.Value;
_index++;
}
}
2024-10-05 15:20:13 -07:00
/// <summary>
/// Performs the kurtosis calculation.
/// </summary>
/// <returns>
/// The calculated excess kurtosis. Positive for heavy-tailed distributions,
/// negative for light-tailed distributions.
/// </returns>
/// <remarks>
/// Uses the Sheskin Algorithm for kurtosis calculation.
/// Requires at least 4 data points for a valid calculation.
/// </remarks>
2024-09-22 17:31:24 -07:00
protected override double Calculation()
{
ManageState(Input.IsNew);
_buffer.Add(Input.Value, Input.IsNew);
double kurtosis = 0;
if (_buffer.Count > 3)
{
var values = _buffer.GetSpan().ToArray();
double mean = values.Average();
double n = values.Length;
double s2 = 0;
double s4 = 0;
for (int i = 0; i < values.Length; i++)
{
double diff = values[i] - mean;
s2 += diff * diff;
s4 += diff * diff * diff * diff;
}
double variance = s2 / (n - 1);
2024-10-05 15:20:13 -07:00
// Sheskin Algorithm
2024-09-22 17:31:24 -07:00
kurtosis = (n * (n + 1) * s4) / (variance * variance * (n - 3) * (n - 1) * (n - 2))
- (3 * (n - 1) * (n - 1) / ((n - 2) * (n - 3)));
}
IsHot = _buffer.Count >= Period;
return kurtosis;
}
}