2024-09-22 17:31:24 -07:00
|
|
|
|
namespace QuanTAlib;
|
|
|
|
|
|
|
2024-10-05 15:20:13 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Represents a Z-score calculator that measures how many standard deviations
|
|
|
|
|
|
/// an element is from the mean of a set of values.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// The Zscore class calculates the Z-score (also known as standard score) for
|
|
|
|
|
|
/// the most recent value in a given period. It uses a circular buffer to
|
|
|
|
|
|
/// efficiently manage the data points within the specified period.
|
|
|
|
|
|
/// </remarks>
|
2024-10-06 06:59:26 +00:00
|
|
|
|
public class Zscore : 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 Zscore class with the specified period.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="period">The period over which to calculate the Z-score.</param>
|
|
|
|
|
|
/// <exception cref="ArgumentOutOfRangeException">
|
|
|
|
|
|
/// Thrown when period is less than 2.
|
|
|
|
|
|
/// </exception>
|
2024-10-06 14:44:43 -07:00
|
|
|
|
public Zscore(int period)
|
2024-10-06 06:59:26 +00:00
|
|
|
|
{
|
|
|
|
|
|
if (period < 2)
|
|
|
|
|
|
{
|
2024-09-22 17:31:24 -07:00
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 2 for Z-score calculation.");
|
|
|
|
|
|
}
|
|
|
|
|
|
Period = period;
|
|
|
|
|
|
WarmupPeriod = 2;
|
|
|
|
|
|
_buffer = new CircularBuffer(period);
|
|
|
|
|
|
Name = $"ZScore(period={period})";
|
|
|
|
|
|
Init();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-05 15:20:13 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initializes a new instance of the Zscore class with the specified source and period.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="source">The source object to subscribe to for value updates.</param>
|
|
|
|
|
|
/// <param name="period">The period over which to calculate the Z-score.</param>
|
2024-10-06 06:59:26 +00:00
|
|
|
|
public Zscore(object source, int period) : this(period)
|
|
|
|
|
|
{
|
2024-09-22 17:31:24 -07:00
|
|
|
|
var pubEvent = source.GetType().GetEvent("Pub");
|
|
|
|
|
|
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-05 15:20:13 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initializes the Zscore instance by clearing the buffer.
|
|
|
|
|
|
/// </summary>
|
2024-10-06 06:59:26 +00:00
|
|
|
|
public override void Init()
|
|
|
|
|
|
{
|
2024-09-22 17:31:24 -07:00
|
|
|
|
base.Init();
|
|
|
|
|
|
_buffer.Clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-05 15:20:13 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Manages the state of the Zscore instance based on whether a new value is being processed.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="isNew">Indicates whether the current input is a new value.</param>
|
2024-10-06 06:59:26 +00:00
|
|
|
|
protected override void ManageState(bool isNew)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (isNew)
|
|
|
|
|
|
{
|
2024-09-22 17:31:24 -07:00
|
|
|
|
_lastValidValue = Input.Value;
|
|
|
|
|
|
_index++;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-05 15:20:13 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Performs the Z-score calculation for the current period.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>
|
|
|
|
|
|
/// The calculated Z-score value for the most recent input in the current period.
|
|
|
|
|
|
/// </returns>
|
|
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// This method calculates the Z-score using the formula:
|
|
|
|
|
|
/// Z = (x - μ) / σ
|
|
|
|
|
|
/// where x is the input value, μ is the mean of the period, and σ is the sample standard deviation.
|
|
|
|
|
|
/// If there are fewer than 2 data points or if the standard deviation is 0, the method returns 0.
|
|
|
|
|
|
/// </remarks>
|
2024-10-06 06:59:26 +00:00
|
|
|
|
protected override double Calculation()
|
|
|
|
|
|
{
|
2024-09-22 17:31:24 -07:00
|
|
|
|
ManageState(Input.IsNew);
|
|
|
|
|
|
|
|
|
|
|
|
_buffer.Add(Input.Value, Input.IsNew);
|
|
|
|
|
|
|
|
|
|
|
|
double zScore = 0;
|
2024-10-06 06:59:26 +00:00
|
|
|
|
if (_buffer.Count >= 2)
|
|
|
|
|
|
{ // We need at least 2 data points for Z-score
|
2024-09-22 17:31:24 -07:00
|
|
|
|
var values = _buffer.GetSpan().ToArray();
|
|
|
|
|
|
double mean = values.Average();
|
|
|
|
|
|
double n = values.Length;
|
|
|
|
|
|
|
|
|
|
|
|
double sumSquaredDeviations = values.Sum(x => Math.Pow(x - mean, 2));
|
|
|
|
|
|
double standardDeviation = Math.Sqrt(sumSquaredDeviations / (n - 1)); // Sample standard deviation
|
|
|
|
|
|
|
2024-10-06 06:59:26 +00:00
|
|
|
|
if (standardDeviation != 0)
|
|
|
|
|
|
{ // Avoid division by zero
|
2024-09-22 17:31:24 -07:00
|
|
|
|
zScore = (Input.Value - mean) / standardDeviation;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
IsHot = _buffer.Count >= Period;
|
|
|
|
|
|
return zScore;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|