mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-20 03:28:05 +00:00
Class optimization
This commit is contained in:
+27
-11
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
namespace QuanTAlib;
|
||||
|
||||
/// <summary>
|
||||
@@ -31,6 +30,7 @@ public class Pwma : AbstractBase
|
||||
{
|
||||
private readonly int _period;
|
||||
private readonly Convolution _convolution;
|
||||
private readonly double[] _kernel;
|
||||
|
||||
/// <param name="period">The number of data points used in the PWMA calculation.</param>
|
||||
/// <exception cref="ArgumentException">Thrown when period is less than 1.</exception>
|
||||
@@ -38,10 +38,11 @@ public class Pwma : AbstractBase
|
||||
{
|
||||
if (period < 1)
|
||||
{
|
||||
throw new ArgumentException("Period must be greater than or equal to 1.", nameof(period));
|
||||
throw new System.ArgumentException("Period must be greater than or equal to 1.", nameof(period));
|
||||
}
|
||||
_period = period;
|
||||
_convolution = new Convolution(GenerateKernel(_period));
|
||||
_kernel = GenerateKernel(_period);
|
||||
_convolution = new Convolution(_kernel);
|
||||
Name = "Pwma";
|
||||
WarmupPeriod = period;
|
||||
Init();
|
||||
@@ -55,12 +56,14 @@ public class Pwma : AbstractBase
|
||||
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private new void Init()
|
||||
{
|
||||
base.Init();
|
||||
_convolution.Init();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
@@ -70,24 +73,33 @@ public class Pwma : AbstractBase
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static double CalculateKernelSum(double[] kernel, int length)
|
||||
{
|
||||
double sum = 0;
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
sum += kernel[i];
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(Input.IsNew);
|
||||
|
||||
// Use Convolution for calculation
|
||||
TValue convolutionResult = _convolution.Calc(Input);
|
||||
|
||||
var convolutionResult = _convolution.Calc(Input);
|
||||
double result = convolutionResult.Value;
|
||||
|
||||
// Adjust for partial periods during warmup
|
||||
if (_index < _period)
|
||||
{
|
||||
double[] partialKernel = GenerateKernel(_index);
|
||||
result /= partialKernel.Sum();
|
||||
result *= CalculateKernelSum(_kernel, _period) / CalculateKernelSum(partialKernel, _index);
|
||||
}
|
||||
|
||||
IsHot = _index >= WarmupPeriod;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -96,11 +108,13 @@ public class Pwma : AbstractBase
|
||||
/// </summary>
|
||||
/// <param name="period">The period for which to generate the kernel.</param>
|
||||
/// <returns>An array of normalized Pascal's triangle-based weights for the convolution operation.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static double[] GenerateKernel(int period)
|
||||
{
|
||||
double[] kernel = new double[period];
|
||||
kernel[0] = 1;
|
||||
|
||||
// Generate Pascal's triangle coefficients
|
||||
for (int i = 1; i < period; i++)
|
||||
{
|
||||
for (int j = i; j > 0; j--)
|
||||
@@ -109,11 +123,13 @@ public class Pwma : AbstractBase
|
||||
}
|
||||
}
|
||||
|
||||
// Normalize the kernel
|
||||
double weightSum = kernel.Sum();
|
||||
// Calculate sum and normalize in one pass
|
||||
double weightSum = CalculateKernelSum(kernel, period);
|
||||
double invWeightSum = 1.0 / weightSum;
|
||||
|
||||
for (int i = 0; i < period; i++)
|
||||
{
|
||||
kernel[i] /= weightSum;
|
||||
kernel[i] *= invWeightSum;
|
||||
}
|
||||
|
||||
return kernel;
|
||||
|
||||
Reference in New Issue
Block a user