mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-15 09:08:04 +00:00
Class optimization
This commit is contained in:
+19
-7
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
namespace QuanTAlib;
|
||||
|
||||
/// <summary>
|
||||
@@ -30,13 +30,15 @@ namespace QuanTAlib;
|
||||
/// Note: Also known as Mean Bias Error (MBE) or Mean Signed Difference (MSD)
|
||||
/// </remarks>
|
||||
|
||||
public class Me : AbstractBase
|
||||
[SkipLocalsInit]
|
||||
public sealed class Me : AbstractBase
|
||||
{
|
||||
private readonly CircularBuffer _actualBuffer;
|
||||
private readonly CircularBuffer _predictedBuffer;
|
||||
|
||||
/// <param name="period">The number of points over which to calculate the ME.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown when period is less than 1.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Me(int period)
|
||||
{
|
||||
if (period < 1)
|
||||
@@ -52,12 +54,14 @@ public class Me : AbstractBase
|
||||
|
||||
/// <param name="source">The data source object that publishes updates.</param>
|
||||
/// <param name="period">The number of points over which to calculate the ME.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Me(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();
|
||||
@@ -65,6 +69,7 @@ public class Me : AbstractBase
|
||||
_predictedBuffer.Clear();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
@@ -74,6 +79,13 @@ public class Me : AbstractBase
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
private static double CalculateError(double actual, double predicted)
|
||||
{
|
||||
return actual - predicted;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(Input.IsNew);
|
||||
@@ -88,16 +100,16 @@ public class Me : AbstractBase
|
||||
double me = 0;
|
||||
if (_actualBuffer.Count > 0)
|
||||
{
|
||||
var actualValues = _actualBuffer.GetSpan().ToArray();
|
||||
var predictedValues = _predictedBuffer.GetSpan().ToArray();
|
||||
ReadOnlySpan<double> actualValues = _actualBuffer.GetSpan();
|
||||
ReadOnlySpan<double> predictedValues = _predictedBuffer.GetSpan();
|
||||
|
||||
double sumError = 0;
|
||||
for (int i = 0; i < _actualBuffer.Count; i++)
|
||||
for (int i = 0; i < actualValues.Length; i++)
|
||||
{
|
||||
sumError += actualValues[i] - predictedValues[i];
|
||||
sumError += CalculateError(actualValues[i], predictedValues[i]);
|
||||
}
|
||||
|
||||
me = sumError / _actualBuffer.Count;
|
||||
me = sumError / actualValues.Length;
|
||||
}
|
||||
|
||||
IsHot = _index >= WarmupPeriod;
|
||||
|
||||
Reference in New Issue
Block a user