mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-16 17:48:05 +00:00
Class optimization
This commit is contained in:
+56
-30
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
namespace QuanTAlib;
|
||||
|
||||
/// <summary>
|
||||
@@ -44,22 +43,26 @@ namespace QuanTAlib;
|
||||
/// Note: Requires minimum of 3 data points for calculation
|
||||
/// </remarks>
|
||||
|
||||
public class Skew : AbstractBase
|
||||
[SkipLocalsInit]
|
||||
public sealed class Skew : AbstractBase
|
||||
{
|
||||
private readonly int Period;
|
||||
private readonly CircularBuffer _buffer;
|
||||
private const double Epsilon = 1e-10;
|
||||
private const int MinimumPoints = 3;
|
||||
|
||||
/// <param name="period">The number of points to consider for skewness calculation.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown when period is less than 3.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Skew(int period)
|
||||
{
|
||||
if (period < 3)
|
||||
if (period < MinimumPoints)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(period),
|
||||
"Period must be greater than or equal to 3 for skewness calculation.");
|
||||
}
|
||||
Period = period;
|
||||
WarmupPeriod = 3;
|
||||
WarmupPeriod = MinimumPoints;
|
||||
_buffer = new CircularBuffer(period);
|
||||
Name = $"Skew(period={period})";
|
||||
Init();
|
||||
@@ -67,18 +70,21 @@ public class Skew : AbstractBase
|
||||
|
||||
/// <param name="source">The data source object that publishes updates.</param>
|
||||
/// <param name="period">The number of points to consider for skewness calculation.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Skew(object source, int period) : this(period)
|
||||
{
|
||||
var pubEvent = source.GetType().GetEvent("Pub");
|
||||
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override void Init()
|
||||
{
|
||||
base.Init();
|
||||
_buffer.Clear();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
@@ -88,38 +94,58 @@ public class Skew : AbstractBase
|
||||
}
|
||||
}
|
||||
|
||||
[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 m3, double m2) CalculateMoments(ReadOnlySpan<double> values, double mean)
|
||||
{
|
||||
double sumCubedDeviations = 0;
|
||||
double sumSquaredDeviations = 0;
|
||||
|
||||
for (int i = 0; i < values.Length; i++)
|
||||
{
|
||||
double deviation = values[i] - mean;
|
||||
double squared = deviation * deviation;
|
||||
sumSquaredDeviations += squared;
|
||||
sumCubedDeviations += squared * deviation;
|
||||
}
|
||||
|
||||
double n = values.Length;
|
||||
return (sumCubedDeviations / n, sumSquaredDeviations / n);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
private static double CalculateSkewness(double m3, double m2, int n)
|
||||
{
|
||||
double s3 = Math.Pow(m2, 1.5);
|
||||
if (s3 < Epsilon)
|
||||
return 0;
|
||||
|
||||
return (Math.Sqrt(n * (n - 1)) / (n - 2)) * (m3 / s3);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(Input.IsNew);
|
||||
_buffer.Add(Input.Value, Input.IsNew);
|
||||
|
||||
double skew = 0;
|
||||
if (_buffer.Count >= 3) // Need at least 3 points for skewness
|
||||
if (_buffer.Count >= MinimumPoints) // Need at least 3 points for skewness
|
||||
{
|
||||
var values = _buffer.GetSpan().ToArray();
|
||||
double mean = values.Average();
|
||||
double n = values.Length;
|
||||
|
||||
// Calculate third and second moments
|
||||
double sumCubedDeviations = 0;
|
||||
double sumSquaredDeviations = 0;
|
||||
|
||||
foreach (var value in values)
|
||||
{
|
||||
double deviation = value - mean;
|
||||
sumCubedDeviations += Math.Pow(deviation, 3);
|
||||
sumSquaredDeviations += Math.Pow(deviation, 2);
|
||||
}
|
||||
|
||||
// Fisher-Pearson standardized moment coefficient
|
||||
double m3 = sumCubedDeviations / n;
|
||||
double m2 = sumSquaredDeviations / n;
|
||||
double s3 = Math.Pow(m2, 1.5);
|
||||
|
||||
if (s3 != 0) // Avoid division by zero
|
||||
{
|
||||
skew = (Math.Sqrt(n * (n - 1)) / (n - 2)) * (m3 / s3);
|
||||
}
|
||||
ReadOnlySpan<double> values = _buffer.GetSpan();
|
||||
double mean = CalculateMean(values);
|
||||
var (m3, m2) = CalculateMoments(values, mean);
|
||||
skew = CalculateSkewness(m3, m2, values.Length);
|
||||
}
|
||||
|
||||
IsHot = _buffer.Count >= Period;
|
||||
|
||||
Reference in New Issue
Block a user