mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-16 17:48:05 +00:00
corrections
This commit is contained in:
+59
-69
@@ -1,45 +1,40 @@
|
||||
using System;
|
||||
|
||||
namespace QuanTAlib;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a Mean Absolute Scaled Error calculator that measures the ratio of the mean absolute error
|
||||
/// of the forecast values to the mean absolute error of the naive forecast.
|
||||
/// Represents the Mean Absolute Scaled Error (MASE) calculation.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The Mase class calculates the Mean Absolute Scaled Error using circular buffers
|
||||
/// to efficiently manage the data points within the specified period.
|
||||
/// </remarks>
|
||||
public class Mase : AbstractBase
|
||||
{
|
||||
private readonly CircularBuffer _actualBuffer;
|
||||
private readonly CircularBuffer _forecastBuffer;
|
||||
private readonly int _period;
|
||||
private readonly CircularBuffer _predictedBuffer;
|
||||
private readonly CircularBuffer _naiveBuffer;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the Mase class with the specified period.
|
||||
/// Initializes a new instance of the Mase class.
|
||||
/// </summary>
|
||||
/// <param name="period">The period over which to calculate the Mean Absolute Scaled Error.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">
|
||||
/// Thrown when period is less than 3.
|
||||
/// </exception>
|
||||
/// <param name="period">The period for MASE calculation.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown when period is less than 1.</exception>
|
||||
public Mase(int period)
|
||||
{
|
||||
if (period < 3)
|
||||
if (period < 1)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 3.");
|
||||
throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 1.");
|
||||
}
|
||||
_period = period;
|
||||
WarmupPeriod = period;
|
||||
_actualBuffer = new CircularBuffer(period);
|
||||
_forecastBuffer = new CircularBuffer(period);
|
||||
_predictedBuffer = new CircularBuffer(period);
|
||||
_naiveBuffer = new CircularBuffer(period);
|
||||
Name = $"Mase(period={period})";
|
||||
Init();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the Mase class with the specified source and period.
|
||||
/// Initializes a new instance of the Mase class with a source object.
|
||||
/// </summary>
|
||||
/// <param name="source">The source object to subscribe to for value updates.</param>
|
||||
/// <param name="period">The period over which to calculate the Mean Absolute Scaled Error.</param>
|
||||
/// <param name="source">The source object for event subscription.</param>
|
||||
/// <param name="period">The period for MASE calculation.</param>
|
||||
public Mase(object source, int period) : this(period)
|
||||
{
|
||||
var pubEvent = source.GetType().GetEvent("Pub");
|
||||
@@ -47,19 +42,20 @@ public class Mase : AbstractBase
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the Mase instance by clearing the buffers.
|
||||
/// Initializes the Mase instance.
|
||||
/// </summary>
|
||||
public override void Init()
|
||||
{
|
||||
base.Init();
|
||||
_actualBuffer.Clear();
|
||||
_forecastBuffer.Clear();
|
||||
_predictedBuffer.Clear();
|
||||
_naiveBuffer.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Manages the state of the Mase instance based on whether new values are being processed.
|
||||
/// Manages the state of the Mase instance.
|
||||
/// </summary>
|
||||
/// <param name="isNew">Indicates whether the current inputs are new values.</param>
|
||||
/// <param name="isNew">Indicates if the input is new.</param>
|
||||
protected override void ManageState(bool isNew)
|
||||
{
|
||||
if (isNew)
|
||||
@@ -70,17 +66,9 @@ public class Mase : AbstractBase
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Performs the Mean Absolute Scaled Error calculation for the current period.
|
||||
/// Performs the MASE calculation.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// The calculated Mean Absolute Scaled Error value for the current period.
|
||||
/// </returns>
|
||||
/// <remarks>
|
||||
/// This method calculates the Mean Absolute Scaled Error using the formula:
|
||||
/// MASE = mean(|actual - forecast|) / mean(|actual[t] - actual[t-1]|)
|
||||
/// where actual is each actual value and forecast is each forecast value.
|
||||
/// If there are fewer than 3 values in the buffers, the method returns 0.
|
||||
/// </remarks>
|
||||
/// <returns>The calculated MASE value.</returns>
|
||||
protected override double Calculation()
|
||||
{
|
||||
ManageState(Input.IsNew);
|
||||
@@ -88,49 +76,51 @@ public class Mase : AbstractBase
|
||||
double actual = Input.Value;
|
||||
_actualBuffer.Add(actual, Input.IsNew);
|
||||
|
||||
double forecast = double.IsNaN(Input2.Value) ? _actualBuffer.Average() : Input2.Value;
|
||||
_forecastBuffer.Add(forecast, Input.IsNew);
|
||||
double predicted = double.IsNaN(Input2.Value) ? _actualBuffer.Average() : Input2.Value;
|
||||
_predictedBuffer.Add(predicted, Input.IsNew);
|
||||
|
||||
double mase = 0;
|
||||
if (_actualBuffer.Count >= 3)
|
||||
if (_actualBuffer.Count > 1)
|
||||
{
|
||||
var actualValues = _actualBuffer.GetSpan().ToArray();
|
||||
var forecastValues = _forecastBuffer.GetSpan().ToArray();
|
||||
|
||||
double sumAbsoluteError = 0;
|
||||
double sumAbsoluteNaiveError = 0;
|
||||
|
||||
int count = Math.Min(_actualBuffer.Count, _period);
|
||||
|
||||
for (int i = 1; i < count; i++)
|
||||
{
|
||||
sumAbsoluteError += Math.Abs(actualValues[i] - forecastValues[i]);
|
||||
sumAbsoluteNaiveError += Math.Abs(actualValues[i] - actualValues[i - 1]);
|
||||
}
|
||||
|
||||
double meanAbsoluteError = sumAbsoluteError / (count - 1);
|
||||
double meanAbsoluteNaiveError = sumAbsoluteNaiveError / (count - 1);
|
||||
|
||||
if (meanAbsoluteNaiveError != 0)
|
||||
{
|
||||
mase = meanAbsoluteError / meanAbsoluteNaiveError;
|
||||
}
|
||||
_naiveBuffer.Add(_actualBuffer.GetSpan()[^2], Input.IsNew);
|
||||
}
|
||||
|
||||
double mase = CalculateMase();
|
||||
|
||||
IsHot = _index >= WarmupPeriod;
|
||||
return mase;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the Mean Absolute Scaled Error for the given actual and forecast values.
|
||||
/// </summary>
|
||||
/// <param name="actual">The actual value.</param>
|
||||
/// <param name="forecast">The forecast value.</param>
|
||||
/// <returns>The calculated Mean Absolute Scaled Error.</returns>
|
||||
public double Calc(double actual, double forecast)
|
||||
private double CalculateMase()
|
||||
{
|
||||
Input = new TValue(DateTime.Now, actual);
|
||||
Input2 = new TValue(DateTime.Now, forecast);
|
||||
return Calculation();
|
||||
if (_actualBuffer.Count <= 1) return 0;
|
||||
|
||||
ReadOnlySpan<double> actualValues = _actualBuffer.GetSpan();
|
||||
ReadOnlySpan<double> predictedValues = _predictedBuffer.GetSpan();
|
||||
ReadOnlySpan<double> naiveValues = _naiveBuffer.GetSpan();
|
||||
|
||||
double sumAbsoluteError = CalculateSumAbsoluteError(actualValues, predictedValues);
|
||||
double _naiveForecastError = CalculateNaiveForecastError(actualValues, naiveValues);
|
||||
|
||||
return _naiveForecastError != 0 ? (sumAbsoluteError / _actualBuffer.Count) / _naiveForecastError : double.PositiveInfinity;
|
||||
}
|
||||
|
||||
private static double CalculateSumAbsoluteError(ReadOnlySpan<double> actualValues, ReadOnlySpan<double> predictedValues)
|
||||
{
|
||||
double sum = 0;
|
||||
for (int i = 0; i < actualValues.Length; i++)
|
||||
{
|
||||
sum += Math.Abs(actualValues[i] - predictedValues[i]);
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
private static double CalculateNaiveForecastError(ReadOnlySpan<double> actualValues, ReadOnlySpan<double> naiveValues)
|
||||
{
|
||||
double sum = 0;
|
||||
for (int i = 1; i < actualValues.Length; i++)
|
||||
{
|
||||
sum += Math.Abs(actualValues[i] - naiveValues[i - 1]);
|
||||
}
|
||||
return sum / (actualValues.Length - 1);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user