namespace QuanTAlib; /// /// Represents a Huber Loss calculator that combines the best properties of L2 squared loss for normal data /// and L1 absolute loss for outliers. /// /// /// The Huberloss class calculates the Huber Loss using circular buffers /// to efficiently manage the actual and predicted data points within the specified period. /// public class Huberloss : AbstractBase { private readonly CircularBuffer _actualBuffer; private readonly CircularBuffer _predictedBuffer; private readonly double _delta; /// /// Initializes a new instance of the Huberloss class with the specified period and delta. /// /// The period over which to calculate the Huber Loss. /// The threshold at which to switch from squared to linear loss. /// /// Thrown when period is less than 1 or delta is less than or equal to 0. /// public Huberloss(int period, double delta = 1.0) { if (period < 1) { throw new ArgumentOutOfRangeException(nameof(period), "Period must be greater than or equal to 1."); } if (delta <= 0) { throw new ArgumentOutOfRangeException(nameof(delta), "Delta must be greater than 0."); } WarmupPeriod = period; _actualBuffer = new CircularBuffer(period); _predictedBuffer = new CircularBuffer(period); _delta = delta; Name = $"Huberloss(period={period}, delta={delta})"; Init(); } /// /// Initializes a new instance of the Mape class with the specified source and period. /// /// The source object to subscribe to for value updates. /// The period over which to calculate the Mean Absolute Percentage Error. public Huberloss(object source, int period) : this(period) { var pubEvent = source.GetType().GetEvent("Pub"); pubEvent?.AddEventHandler(source, new ValueSignal(Sub)); } /// /// Initializes the Huberloss instance by clearing the buffers. /// public override void Init() { base.Init(); _actualBuffer.Clear(); _predictedBuffer.Clear(); } /// /// Manages the state of the Huberloss instance based on whether new values are being processed. /// /// Indicates whether the current inputs are new values. protected override void ManageState(bool isNew) { if (isNew) { _lastValidValue = Input.Value; _index++; } } /// /// Performs the Huber Loss calculation for the current period. /// /// /// The calculated Huber Loss value for the current period. /// /// /// This method calculates the Huber Loss using the formula: /// L(a, p) = 0.5 * (a - p)^2 for |a - p| <= delta /// L(a, p) = delta * |a - p| - 0.5 * delta^2 for |a - p| > delta /// where a is the actual value, p is the predicted value, and delta is the threshold. /// protected override double Calculation() { ManageState(Input.IsNew); double actual = Input.Value; _actualBuffer.Add(actual, Input.IsNew); double predicted = double.IsNaN(Input2.Value) ? _actualBuffer.Average() : Input2.Value; _predictedBuffer.Add(predicted, Input.IsNew); double huberLoss = 0; if (_actualBuffer.Count > 0) { var actualValues = _actualBuffer.GetSpan().ToArray(); var predictedValues = _predictedBuffer.GetSpan().ToArray(); double sumLoss = 0; for (int i = 0; i < _actualBuffer.Count; i++) { double error = Math.Abs(actualValues[i] - predictedValues[i]); if (error <= _delta) { sumLoss += 0.5 * error * error; } else { sumLoss += _delta * error - 0.5 * _delta * _delta; } } huberLoss = sumLoss / _actualBuffer.Count; } IsHot = _index >= WarmupPeriod; return huberLoss; } /// /// Calculates the Huber Loss for the given actual and predicted values. /// /// The actual value. /// The predicted value. /// The calculated Huber Loss. public double Calc(double actual, double predicted) { Input = new TValue(DateTime.Now, actual); Input2 = new TValue(DateTime.Now, predicted); return Calculation(); } }