Files
QuanTAlib/lib/statistics/Variance.cs
T

118 lines
4.0 KiB
C#
Raw Normal View History

2024-10-05 15:20:13 -07:00
namespace QuanTAlib;
2024-09-22 17:31:24 -07:00
2024-10-05 15:20:13 -07:00
/// <summary>
/// Represents a variance calculator that measures the spread of a set of numbers
/// from their average value.
/// </summary>
/// <remarks>
/// The Variance class calculates either the population variance or the sample
/// variance based on the isPopulation parameter. 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 Variance : AbstractBase
{
2024-10-05 15:20:13 -07:00
private readonly int Period;
private readonly bool IsPopulation;
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 Variance class with the specified period and
/// population flag.
/// </summary>
/// <param name="period">The period over which to calculate the variance.</param>
/// <param name="isPopulation">
/// A flag indicating whether to calculate population (true) or sample (false) variance.
/// </param>
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown when period is less than 2.
/// </exception>
2024-10-08 10:47:21 -07:00
<<<<<<< HEAD
2024-10-06 06:59:26 +00:00
public Variance(int period, bool isPopulation = false) : base()
2024-10-08 10:47:21 -07:00
=======
2024-10-06 14:44:43 -07:00
public Variance(int period, bool isPopulation = false)
2024-10-08 10:47:21 -07:00
>>>>>>> dev
2024-10-06 06:59:26 +00:00
{
if (period < 2)
{
2024-10-05 15:20:13 -07:00
throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 2.");
2024-09-22 17:31:24 -07:00
}
2024-10-05 15:20:13 -07:00
Period = period;
IsPopulation = isPopulation;
WarmupPeriod = 0;
_buffer = new CircularBuffer(period);
Name = $"Variance(period={period}, population={isPopulation})";
Init();
}
2024-09-22 17:31:24 -07:00
2024-10-05 15:20:13 -07:00
/// <summary>
/// Initializes a new instance of the Variance class with the specified source, period,
/// and population flag.
/// </summary>
/// <param name="source">The source object to subscribe to for value updates.</param>
/// <param name="period">The period over which to calculate the variance.</param>
/// <param name="isPopulation">
/// A flag indicating whether to calculate population (true) or sample (false) variance.
/// </param>
2024-10-06 06:59:26 +00:00
public Variance(object source, int period, bool isPopulation = false) : this(period, isPopulation)
{
2024-10-05 15:20:13 -07:00
var pubEvent = source.GetType().GetEvent("Pub");
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
}
2024-09-22 17:31:24 -07:00
2024-10-05 15:20:13 -07:00
/// <summary>
/// Initializes the Variance instance by clearing the buffer.
/// </summary>
2024-10-06 06:59:26 +00:00
public override void Init()
{
2024-10-05 15:20:13 -07:00
base.Init();
_buffer.Clear();
}
2024-09-22 17:31:24 -07:00
2024-10-05 15:20:13 -07:00
/// <summary>
/// Manages the state of the Variance 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-10-05 15:20:13 -07:00
_lastValidValue = Input.Value;
_index++;
2024-09-22 17:31:24 -07:00
}
2024-10-05 15:20:13 -07:00
}
2024-09-22 17:31:24 -07:00
2024-10-05 15:20:13 -07:00
/// <summary>
/// Performs the variance calculation for the current period.
/// </summary>
/// <returns>
/// The calculated variance value for the current period.
/// </returns>
/// <remarks>
/// This method calculates the variance using the formula:
/// sum((x - mean)^2) / n for population, or
/// sum((x - mean)^2) / (n - 1) for sample,
/// where x is each value, mean is the average of all values, and n is the number of values.
/// If there's only one value in the buffer, the method returns 0.
/// </remarks>
2024-10-06 06:59:26 +00:00
protected override double Calculation()
{
2024-10-05 15:20:13 -07:00
ManageState(Input.IsNew);
2024-09-22 17:31:24 -07:00
2024-10-05 15:20:13 -07:00
_buffer.Add(Input.Value, Input.IsNew);
2024-09-22 17:31:24 -07:00
2024-10-05 15:20:13 -07:00
double variance = 0;
2024-10-06 06:59:26 +00:00
if (_buffer.Count > 1)
{
2024-10-05 15:20:13 -07:00
var values = _buffer.GetSpan().ToArray();
double mean = values.Average();
double sumOfSquaredDifferences = values.Sum(x => Math.Pow(x - mean, 2));
2024-09-22 17:31:24 -07:00
2024-10-05 15:20:13 -07:00
double divisor = IsPopulation ? _buffer.Count : _buffer.Count - 1;
variance = sumOfSquaredDifferences / divisor;
2024-09-22 17:31:24 -07:00
}
2024-10-05 15:20:13 -07:00
IsHot = true;
return variance;
2024-09-22 17:31:24 -07:00
}
2024-10-05 15:20:13 -07:00
}