Files
QuanTAlib/lib/averages/Gma.cs
T

114 lines
3.8 KiB
C#
Raw Normal View History

2024-10-27 16:11:08 -07:00
using System.Runtime.CompilerServices;
2024-09-22 17:31:24 -07:00
namespace QuanTAlib;
2024-10-27 09:38:53 -07:00
/// <summary>
/// GMA: Gaussian Moving Average
/// A moving average that uses weights based on the Gaussian (normal) distribution curve.
/// This creates a smooth, bell-shaped weighting scheme that gives maximum weight to the
/// center of the period and gradually decreasing weights towards the edges.
/// </summary>
/// <remarks>
/// The GMA calculation process:
/// 1. Creates a Gaussian distribution of weights centered on the period
/// 2. Normalizes the weights to sum to 1
/// 3. Applies the weights through convolution
///
/// Key characteristics:
/// - Smooth, symmetric weight distribution
/// - Natural bell curve weighting
/// - Reduces noise while preserving signal characteristics
/// - Less sensitive to outliers than simple moving averages
/// - Implemented using efficient convolution operations
///
/// Implementation:
/// Based on Gaussian distribution principles from statistics
/// </remarks>
2024-09-22 17:31:24 -07:00
public class Gma : AbstractBase
{
private readonly Convolution _convolution;
2024-10-27 09:38:53 -07:00
/// <param name="period">The number of data points used in the GMA calculation.</param>
/// <exception cref="ArgumentException">Thrown when period is less than 1.</exception>
2024-09-22 17:31:24 -07:00
public Gma(int period)
{
if (period < 1)
{
2024-10-27 16:11:08 -07:00
throw new System.ArgumentException("Period must be greater than or equal to 1.", nameof(period));
2024-09-22 17:31:24 -07:00
}
2024-11-03 09:27:12 -08:00
double[] _kernel = GenerateKernel(period);
2024-10-27 16:11:08 -07:00
_convolution = new Convolution(_kernel);
2024-09-22 17:31:24 -07:00
Name = "Gma";
WarmupPeriod = period;
Init();
}
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 data points used in the GMA calculation.</param>
2024-09-22 17:31:24 -07:00
public Gma(object source, int period) : this(period)
{
var pubEvent = source.GetType().GetEvent("Pub");
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
}
2024-10-27 09:38:53 -07:00
/// <summary>
/// Generates the Gaussian-based convolution kernel for the GMA calculation.
/// </summary>
/// <param name="period">The period for which to generate the kernel.</param>
/// <param name="sigma">The standard deviation parameter controlling the spread of the Gaussian curve. Default is 1.0.</param>
/// <returns>An array of normalized Gaussian-based weights for the convolution operation.</returns>
2024-10-27 16:11:08 -07:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-09-22 17:31:24 -07:00
public static double[] GenerateKernel(int period, double sigma = 1.0)
{
double[] kernel = new double[period];
double weightSum = 0;
int center = period / 2;
2024-10-27 16:11:08 -07:00
double centerRecip = 1.0 / center;
double sigmaSquared2 = 2.0 * sigma * sigma;
2024-09-22 17:31:24 -07:00
2024-10-27 16:11:08 -07:00
// Calculate weights and sum in one pass
2024-09-22 17:31:24 -07:00
for (int i = 0; i < period; i++)
{
2024-10-27 16:11:08 -07:00
double x = (i - center) * centerRecip;
kernel[i] = System.Math.Exp(-(x * x) / sigmaSquared2);
2024-09-22 17:31:24 -07:00
weightSum += kernel[i];
}
2024-10-27 16:11:08 -07:00
// Normalize using multiplication instead of division
double invWeightSum = 1.0 / weightSum;
2024-09-22 17:31:24 -07:00
for (int i = 0; i < period; i++)
{
2024-10-27 16:11:08 -07:00
kernel[i] *= invWeightSum;
2024-09-22 17:31:24 -07:00
}
return kernel;
}
2024-10-27 16:11:08 -07:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-09-22 17:31:24 -07:00
private new void Init()
{
base.Init();
_convolution.Init();
}
2024-10-27 16:11:08 -07:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-09-22 17:31:24 -07:00
protected override void ManageState(bool isNew)
{
if (isNew)
{
_lastValidValue = Input.Value;
_index++;
}
}
protected override double Calculation()
{
ManageState(Input.IsNew);
// Use Convolution for calculation
2024-10-27 16:11:08 -07:00
var convolutionResult = _convolution.Calc(Input);
2024-09-22 17:31:24 -07:00
IsHot = _index >= WarmupPeriod;
2024-10-27 16:11:08 -07:00
return convolutionResult.Value;
2024-09-22 17:31:24 -07:00
}
2024-10-27 09:38:53 -07:00
}