Files
QuanTAlib/lib/errors/Mae.cs
T

110 lines
3.6 KiB
C#
Raw Normal View History

2024-10-27 16:11:08 -07:00
using System.Runtime.CompilerServices;
2024-10-11 18:02:09 -07:00
namespace QuanTAlib;
2024-10-27 09:38:53 -07:00
/// <summary>
/// MAE: Mean Absolute Error
/// A straightforward error metric that measures the average magnitude of errors
/// between predicted and actual values, without considering their direction.
/// MAE treats all individual differences equally in the average.
/// </summary>
/// <remarks>
/// The MAE calculation process:
/// 1. Calculates absolute difference between each actual and predicted value
/// 2. Sums all absolute differences
/// 3. Divides by the number of observations
///
/// Key characteristics:
/// - Linear scale (all differences weighted equally)
/// - Robust to outliers compared to MSE
/// - Easy to interpret (same units as data)
/// - Constant gradient for optimization
/// - Less sensitive to large errors than MSE
///
/// Formula:
/// MAE = (1/n) * Σ|actual - predicted|
///
/// Sources:
/// https://en.wikipedia.org/wiki/Mean_absolute_error
/// https://www.statisticshowto.com/absolute-error/
/// </remarks>
2024-10-27 16:11:08 -07:00
[SkipLocalsInit]
public sealed class Mae : AbstractBase
2024-10-11 18:02:09 -07:00
{
private readonly CircularBuffer _actualBuffer;
private readonly CircularBuffer _predictedBuffer;
2024-10-27 09:38:53 -07:00
/// <param name="period">The number of points over which to calculate the MAE.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when period is less than 1.</exception>
2024-10-27 16:11:08 -07:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-10-11 18:02:09 -07:00
public Mae(int period)
{
if (period < 1)
{
throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 1.");
}
WarmupPeriod = period;
_actualBuffer = new CircularBuffer(period);
_predictedBuffer = new CircularBuffer(period);
Name = $"Mae(period={period})";
Init();
}
2024-10-27 09:38:53 -07:00
/// <param name="source">The data source object that publishes updates.</param>
/// <param name="period">The number of points over which to calculate the MAE.</param>
2024-10-27 16:11:08 -07:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-10-11 18:02:09 -07:00
public Mae(object source, int period) : this(period)
{
var pubEvent = source.GetType().GetEvent("Pub");
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
}
2024-10-27 16:11:08 -07:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-10-11 18:02:09 -07:00
public override void Init()
{
base.Init();
_actualBuffer.Clear();
_predictedBuffer.Clear();
}
2024-10-27 16:11:08 -07:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-10-11 18:02:09 -07:00
protected override void ManageState(bool isNew)
{
if (isNew)
{
_lastValidValue = Input.Value;
_index++;
}
}
2024-10-27 16:11:08 -07:00
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
2024-10-11 18:02:09 -07:00
protected override double Calculation()
{
ManageState(Input.IsNew);
double actual = Input.Value;
_actualBuffer.Add(actual, Input.IsNew);
2024-10-27 09:38:53 -07:00
// If no predicted value provided, use mean of actual values
2024-10-11 18:02:09 -07:00
double predicted = double.IsNaN(Input2.Value) ? _actualBuffer.Average() : Input2.Value;
_predictedBuffer.Add(predicted, Input.IsNew);
double mae = 0;
if (_actualBuffer.Count > 0)
{
2024-10-27 16:11:08 -07:00
ReadOnlySpan<double> actualValues = _actualBuffer.GetSpan();
ReadOnlySpan<double> predictedValues = _predictedBuffer.GetSpan();
2024-10-11 18:02:09 -07:00
2024-10-13 11:19:27 -07:00
double sumAbsoluteError = 0;
2024-10-27 16:11:08 -07:00
for (int i = 0; i < actualValues.Length; i++)
2024-10-11 18:02:09 -07:00
{
2024-10-13 11:19:27 -07:00
sumAbsoluteError += Math.Abs(actualValues[i] - predictedValues[i]);
2024-10-11 18:02:09 -07:00
}
2024-10-27 16:11:08 -07:00
mae = sumAbsoluteError / actualValues.Length;
2024-10-11 18:02:09 -07:00
}
IsHot = _index >= WarmupPeriod;
return mae;
}
}