Files
QuanTAlib/lib/errors/Mape.cs
T
2024-10-27 09:38:53 -07:00

110 lines
3.5 KiB
C#

using System;
namespace QuanTAlib;
/// <summary>
/// MAPE: Mean Absolute Percentage Error
/// A percentage-based error metric that measures the average absolute percentage
/// difference between predicted and actual values. MAPE expresses accuracy as a
/// percentage, making it scale-independent and easy to interpret.
/// </summary>
/// <remarks>
/// The MAPE calculation process:
/// 1. Calculates absolute percentage error for each point
/// 2. Sums all absolute percentage errors
/// 3. Divides by the number of observations
///
/// Key characteristics:
/// - Scale-independent (percentage-based)
/// - Easy to interpret (0-100% range)
/// - Useful for comparing different scales
/// - Cannot handle zero actual values
/// - Asymmetric (treats over/under predictions differently)
///
/// Formula:
/// MAPE = (1/n) * Σ|((actual - predicted) / actual)| * 100%
///
/// Sources:
/// https://en.wikipedia.org/wiki/Mean_absolute_percentage_error
/// https://www.statisticshowto.com/mean-absolute-percentage-error-mape/
///
/// Note: Also known as MAPD (Mean Absolute Percentage Deviation) in some contexts
/// </remarks>
public class Mape : AbstractBase
{
private readonly CircularBuffer _actualBuffer;
private readonly CircularBuffer _predictedBuffer;
/// <param name="period">The number of points over which to calculate the MAPE.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when period is less than 1.</exception>
public Mape(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 = $"Mape(period={period})";
Init();
}
/// <param name="source">The data source object that publishes updates.</param>
/// <param name="period">The number of points over which to calculate the MAPE.</param>
public Mape(object source, int period) : this(period)
{
var pubEvent = source.GetType().GetEvent("Pub");
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
}
public override void Init()
{
base.Init();
_actualBuffer.Clear();
_predictedBuffer.Clear();
}
protected override void ManageState(bool isNew)
{
if (isNew)
{
_lastValidValue = Input.Value;
_index++;
}
}
protected override double Calculation()
{
ManageState(Input.IsNew);
double actual = Input.Value;
_actualBuffer.Add(actual, Input.IsNew);
// If no predicted value provided, use mean of actual values
double predicted = double.IsNaN(Input2.Value) ? _actualBuffer.Average() : Input2.Value;
_predictedBuffer.Add(predicted, Input.IsNew);
double mape = 0;
if (_actualBuffer.Count > 0)
{
var actualValues = _actualBuffer.GetSpan().ToArray();
var predictedValues = _predictedBuffer.GetSpan().ToArray();
double sumAbsolutePercentageError = 0;
for (int i = 0; i < _actualBuffer.Count; i++)
{
if (actualValues[i] != 0)
{
sumAbsolutePercentageError += Math.Abs((actualValues[i] - predictedValues[i]) / actualValues[i]);
}
}
mape = sumAbsolutePercentageError / _actualBuffer.Count;
}
IsHot = _index >= WarmupPeriod;
return mape;
}
}