corrections

This commit is contained in:
Miha Kralj
2024-10-13 17:18:31 -07:00
parent 2236f5f483
commit bbefc72d73
65 changed files with 669 additions and 1087 deletions
+9 -59
View File
@@ -1,27 +1,11 @@
namespace QuanTAlib;
/// <summary>
/// Represents a Huber Loss calculator that combines the best properties of L2 squared loss for normal data
/// and L1 absolute loss for outliers.
/// </summary>
/// <remarks>
/// The Huberloss class calculates the Huber Loss using circular buffers
/// to efficiently manage the actual and predicted data points within the specified period.
/// </remarks>
public class Huberloss : AbstractBase
{
private readonly CircularBuffer _actualBuffer;
private readonly CircularBuffer _predictedBuffer;
private readonly double _delta;
/// <summary>
/// Initializes a new instance of the Huberloss class with the specified period and delta.
/// </summary>
/// <param name="period">The period over which to calculate the Huber Loss.</param>
/// <param name="delta">The threshold at which to switch from squared to linear loss.</param>
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown when period is less than 1 or delta is less than or equal to 0.
/// </exception>
public Huberloss(int period, double delta = 1.0)
{
if (period < 1)
@@ -40,20 +24,12 @@ public class Huberloss : AbstractBase
Init();
}
/// <summary>
/// Initializes a new instance of the Mape class with the specified source and period.
/// </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 Percentage Error.</param>
public Huberloss(object source, int period) : this(period)
public Huberloss(object source, int period, double delta = 1.0) : this(period, delta)
{
var pubEvent = source.GetType().GetEvent("Pub");
pubEvent?.AddEventHandler(source, new ValueSignal(Sub));
}
/// <summary>
/// Initializes the Huberloss instance by clearing the buffers.
/// </summary>
public override void Init()
{
base.Init();
@@ -61,10 +37,6 @@ public class Huberloss : AbstractBase
_predictedBuffer.Clear();
}
/// <summary>
/// Manages the state of the Huberloss instance based on whether new values are being processed.
/// </summary>
/// <param name="isNew">Indicates whether the current inputs are new values.</param>
protected override void ManageState(bool isNew)
{
if (isNew)
@@ -74,18 +46,6 @@ public class Huberloss : AbstractBase
}
}
/// <summary>
/// Performs the Huber Loss calculation for the current period.
/// </summary>
/// <returns>
/// The calculated Huber Loss value for the current period.
/// </returns>
/// <remarks>
/// 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.
/// </remarks>
protected override double Calculation()
{
ManageState(Input.IsNew);
@@ -96,7 +56,7 @@ public class Huberloss : AbstractBase
double predicted = double.IsNaN(Input2.Value) ? _actualBuffer.Average() : Input2.Value;
_predictedBuffer.Add(predicted, Input.IsNew);
double huberLoss = 0;
double huberloss = 0;
if (_actualBuffer.Count > 0)
{
var actualValues = _actualBuffer.GetSpan().ToArray();
@@ -105,34 +65,24 @@ public class Huberloss : AbstractBase
double sumLoss = 0;
for (int i = 0; i < _actualBuffer.Count; i++)
{
double error = Math.Abs(actualValues[i] - predictedValues[i]);
if (error <= _delta)
double error = actualValues[i] - predictedValues[i];
double absError = Math.Abs(error);
if (absError <= _delta)
{
sumLoss += 0.5 * error * error;
}
else
{
sumLoss += _delta * error - 0.5 * _delta * _delta;
sumLoss += _delta * (absError - 0.5 * _delta);
}
}
huberLoss = sumLoss / _actualBuffer.Count;
huberloss = sumLoss / _actualBuffer.Count;
}
IsHot = _index >= WarmupPeriod;
return huberLoss;
return huberloss;
}
/// <summary>
/// Calculates the Huber Loss for the given actual and predicted values.
/// </summary>
/// <param name="actual">The actual value.</param>
/// <param name="predicted">The predicted value.</param>
/// <returns>The calculated Huber Loss.</returns>
public double Calc(double actual, double predicted)
{
Input = new TValue(DateTime.Now, actual);
Input2 = new TValue(DateTime.Now, predicted);
return Calculation();
}
}