mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-15 09:08:04 +00:00
Class optimization
This commit is contained in:
+22
-10
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
namespace QuanTAlib;
|
||||
|
||||
/// <summary>
|
||||
@@ -31,13 +31,15 @@ namespace QuanTAlib;
|
||||
/// Note: Often used in cases where target values follow exponential growth
|
||||
/// </remarks>
|
||||
|
||||
public class Msle : AbstractBase
|
||||
[SkipLocalsInit]
|
||||
public sealed class Msle : AbstractBase
|
||||
{
|
||||
private readonly CircularBuffer _actualBuffer;
|
||||
private readonly CircularBuffer _predictedBuffer;
|
||||
|
||||
/// <param name="period">The number of points over which to calculate the MSLE.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown when period is less than 1.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Msle(int period)
|
||||
{
|
||||
if (period < 1)
|
||||
@@ -53,12 +55,14 @@ public class Msle : AbstractBase
|
||||
|
||||
/// <param name="source">The data source object that publishes updates.</param>
|
||||
/// <param name="period">The number of points over which to calculate the MSLE.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Msle(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();
|
||||
@@ -66,6 +70,7 @@ public class Msle : AbstractBase
|
||||
_predictedBuffer.Clear();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
@@ -75,6 +80,16 @@ public class Msle : AbstractBase
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
private static double CalculateSquaredLogError(double actual, double predicted)
|
||||
{
|
||||
double logActual = Math.Log(actual + 1);
|
||||
double logPredicted = Math.Log(predicted + 1);
|
||||
double error = logActual - logPredicted;
|
||||
return error * error;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(Input.IsNew);
|
||||
@@ -89,19 +104,16 @@ public class Msle : AbstractBase
|
||||
double msle = 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 sumSquaredLogError = 0;
|
||||
for (int i = 0; i < _actualBuffer.Count; i++)
|
||||
for (int i = 0; i < actualValues.Length; i++)
|
||||
{
|
||||
double logActual = Math.Log(actualValues[i] + 1);
|
||||
double logPredicted = Math.Log(predictedValues[i] + 1);
|
||||
double error = logActual - logPredicted;
|
||||
sumSquaredLogError += error * error;
|
||||
sumSquaredLogError += CalculateSquaredLogError(actualValues[i], predictedValues[i]);
|
||||
}
|
||||
|
||||
msle = sumSquaredLogError / _actualBuffer.Count;
|
||||
msle = sumSquaredLogError / actualValues.Length;
|
||||
}
|
||||
|
||||
IsHot = _index >= WarmupPeriod;
|
||||
|
||||
Reference in New Issue
Block a user