2022-04-19 23:29:04 -07:00
|
|
|
namespace QuanTAlib;
|
2022-04-19 22:34:42 -07:00
|
|
|
using System;
|
2022-11-17 07:53:45 -08:00
|
|
|
using System.Linq;
|
2022-04-19 14:35:10 -07:00
|
|
|
|
2022-04-19 22:34:42 -07:00
|
|
|
/* <summary>
|
|
|
|
|
WMAPE: Weighted Mean Absolute Percentage Error
|
2022-11-17 07:53:45 -08:00
|
|
|
Measures the size of the error in percentage terms. Improves problems with MAPE
|
|
|
|
|
when there are zero or close-to-zero values because there would be a division by zero
|
|
|
|
|
or values of MAPE tending to infinity.
|
2022-04-19 14:35:10 -07:00
|
|
|
|
|
|
|
|
Sources:
|
|
|
|
|
https://en.wikipedia.org/wiki/WMAPE
|
|
|
|
|
|
2022-04-19 22:34:42 -07:00
|
|
|
</summary> */
|
2022-04-19 14:35:10 -07:00
|
|
|
|
|
|
|
|
public class WMAPE_Series : Single_TSeries_Indicator
|
|
|
|
|
{
|
|
|
|
|
public WMAPE_Series(TSeries source, int period, bool useNaN = false) : base(source, period, useNaN)
|
|
|
|
|
{
|
|
|
|
|
if (base._data.Count > 0) { base.Add(base._data); }
|
|
|
|
|
}
|
|
|
|
|
private readonly System.Collections.Generic.List<double> _buffer = new();
|
|
|
|
|
|
2022-04-19 23:29:04 -07:00
|
|
|
public override void Add((System.DateTime t, double v) TValue, bool update)
|
2022-04-19 14:35:10 -07:00
|
|
|
{
|
2022-11-17 07:53:45 -08:00
|
|
|
Add_Replace_Trim(_buffer, TValue.v, _p, update);
|
|
|
|
|
double _sma = _buffer.Average();
|
2022-04-19 14:35:10 -07:00
|
|
|
|
|
|
|
|
double _div = 0;
|
|
|
|
|
double _wmape = 0;
|
|
|
|
|
for (int i = 0; i < _buffer.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
_wmape += Math.Abs(_buffer[i] - _sma);
|
|
|
|
|
_div += Math.Abs(_buffer[i]);
|
|
|
|
|
}
|
2022-11-17 07:53:45 -08:00
|
|
|
_wmape = (_div!=0) ? _wmape/_div : double.PositiveInfinity;
|
2022-04-19 14:35:10 -07:00
|
|
|
|
2022-11-17 07:53:45 -08:00
|
|
|
base.Add((TValue.t, _wmape), update, _NaN);
|
2022-04-19 14:35:10 -07:00
|
|
|
}
|
|
|
|
|
}
|