2024-10-27 16:11:08 -07:00
|
|
|
|
using System.Runtime.CompilerServices;
|
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>
|
2024-10-27 09:38:53 -07:00
|
|
|
|
/// STDDEV: Standard Deviation Volatility Measure
|
|
|
|
|
|
/// A statistical measure that quantifies the amount of variation or dispersion
|
|
|
|
|
|
/// in a dataset. Standard deviation is widely used in finance as a measure of
|
|
|
|
|
|
/// volatility and risk assessment.
|
2024-10-05 15:20:13 -07:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>
|
2024-10-27 09:38:53 -07:00
|
|
|
|
/// The StdDev calculation process:
|
|
|
|
|
|
/// 1. Calculates mean of the data
|
|
|
|
|
|
/// 2. Computes squared deviations from mean
|
|
|
|
|
|
/// 3. Averages squared deviations
|
|
|
|
|
|
/// 4. Takes square root of average
|
2024-10-11 18:02:09 -07:00
|
|
|
|
///
|
2024-10-27 09:38:53 -07:00
|
|
|
|
/// Key characteristics:
|
|
|
|
|
|
/// - Measures data dispersion
|
|
|
|
|
|
/// - Same units as input data
|
|
|
|
|
|
/// - Sensitive to outliers
|
|
|
|
|
|
/// - Population or sample versions
|
|
|
|
|
|
/// - Key volatility indicator
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Formula:
|
|
|
|
|
|
/// Population: σ = √(Σ(x - μ)² / N)
|
|
|
|
|
|
/// Sample: s = √(Σ(x - x̄)² / (n-1))
|
|
|
|
|
|
/// where:
|
|
|
|
|
|
/// x = values
|
|
|
|
|
|
/// μ, x̄ = mean
|
|
|
|
|
|
/// N, n = count
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Market Applications:
|
|
|
|
|
|
/// - Volatility measurement
|
|
|
|
|
|
/// - Risk assessment
|
|
|
|
|
|
/// - Bollinger Bands
|
|
|
|
|
|
/// - Option pricing
|
|
|
|
|
|
/// - Portfolio management
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Sources:
|
|
|
|
|
|
/// https://en.wikipedia.org/wiki/Standard_deviation
|
|
|
|
|
|
/// "Options, Futures, and Other Derivatives" - John C. Hull
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Note: Foundation for many volatility-based indicators
|
2024-10-05 15:20:13 -07:00
|
|
|
|
/// </remarks>
|
2024-10-27 16:11:08 -07:00
|
|
|
|
[SkipLocalsInit]
|
|
|
|
|
|
public sealed class Stddev : AbstractBase
|
2024-10-06 06:59:26 +00:00
|
|
|
|
{
|
2024-10-05 15:20:13 -07:00
|
|
|
|
private readonly bool IsPopulation;
|
|
|
|
|
|
private readonly CircularBuffer _buffer;
|
2024-10-27 16:11:08 -07:00
|
|
|
|
private const double Epsilon = 1e-10;
|
|
|
|
|
|
private const int MinimumPoints = 2;
|
2024-09-22 17:31:24 -07:00
|
|
|
|
|
2024-10-27 09:38:53 -07:00
|
|
|
|
/// <param name="period">The number of points to consider for standard deviation calculation.</param>
|
|
|
|
|
|
/// <param name="isPopulation">True for population stddev, false for sample stddev (default).</param>
|
|
|
|
|
|
/// <exception cref="ArgumentOutOfRangeException">Thrown when period is less than 2.</exception>
|
2024-10-27 16:11:08 -07:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-10-06 14:44:43 -07:00
|
|
|
|
public Stddev(int period, bool isPopulation = false)
|
2024-10-06 06:59:26 +00:00
|
|
|
|
{
|
2024-10-27 16:11:08 -07:00
|
|
|
|
if (period < MinimumPoints)
|
2024-10-06 06:59:26 +00:00
|
|
|
|
{
|
2024-10-27 09:38:53 -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
|
|
|
|
IsPopulation = isPopulation;
|
|
|
|
|
|
WarmupPeriod = 0;
|
|
|
|
|
|
_buffer = new CircularBuffer(period);
|
|
|
|
|
|
Name = $"Stddev(period={period}, population={isPopulation})";
|
|
|
|
|
|
Init();
|
|
|
|
|
|
}
|
2024-09-22 17:31:24 -07:00
|
|
|
|
|
2024-10-27 09:38:53 -07:00
|
|
|
|
/// <param name="source">The data source object that publishes updates.</param>
|
|
|
|
|
|
/// <param name="period">The number of points to consider for standard deviation calculation.</param>
|
|
|
|
|
|
/// <param name="isPopulation">True for population stddev, false for sample stddev (default).</param>
|
2024-10-27 16:11:08 -07:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-10-06 06:59:26 +00:00
|
|
|
|
public Stddev(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-27 16:11:08 -07:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
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-27 16:11:08 -07:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
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-27 16:11:08 -07:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
|
|
|
|
|
private static double CalculateMean(ReadOnlySpan<double> values)
|
|
|
|
|
|
{
|
|
|
|
|
|
double sum = 0;
|
|
|
|
|
|
for (int i = 0; i < values.Length; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
sum += values[i];
|
|
|
|
|
|
}
|
|
|
|
|
|
return sum / values.Length;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
|
|
|
|
|
private static double CalculateSumSquaredDeviations(ReadOnlySpan<double> values, double mean)
|
|
|
|
|
|
{
|
|
|
|
|
|
double sum = 0;
|
|
|
|
|
|
for (int i = 0; i < values.Length; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
double diff = values[i] - mean;
|
|
|
|
|
|
sum += diff * diff;
|
|
|
|
|
|
}
|
|
|
|
|
|
return sum;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
2024-10-06 06:59:26 +00:00
|
|
|
|
protected override double Calculation()
|
|
|
|
|
|
{
|
2024-10-05 15:20:13 -07:00
|
|
|
|
ManageState(Input.IsNew);
|
|
|
|
|
|
_buffer.Add(Input.Value, Input.IsNew);
|
2024-09-22 17:31:24 -07:00
|
|
|
|
|
2024-10-05 15:20:13 -07:00
|
|
|
|
double stddev = 0;
|
2024-10-06 06:59:26 +00:00
|
|
|
|
if (_buffer.Count > 1)
|
|
|
|
|
|
{
|
2024-10-27 16:11:08 -07:00
|
|
|
|
ReadOnlySpan<double> values = _buffer.GetSpan();
|
|
|
|
|
|
double mean = CalculateMean(values);
|
|
|
|
|
|
double sumOfSquaredDifferences = CalculateSumSquaredDeviations(values, mean);
|
2024-09-22 17:31:24 -07:00
|
|
|
|
|
2024-10-27 09:38:53 -07:00
|
|
|
|
// Use appropriate divisor based on population/sample calculation
|
2024-10-05 15:20:13 -07:00
|
|
|
|
double divisor = IsPopulation ? _buffer.Count : _buffer.Count - 1;
|
|
|
|
|
|
double variance = sumOfSquaredDifferences / divisor;
|
|
|
|
|
|
stddev = Math.Sqrt(variance);
|
2024-09-22 17:31:24 -07:00
|
|
|
|
}
|
2024-10-05 15:20:13 -07:00
|
|
|
|
|
|
|
|
|
|
IsHot = true; // StdDev calc is valid from bar 1
|
|
|
|
|
|
return stddev;
|
2024-09-22 17:31:24 -07:00
|
|
|
|
}
|
2024-10-05 15:20:13 -07:00
|
|
|
|
}
|