mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-21 20:18:05 +00:00
SIMD Refactor: Merge simd-dev into dev (#55)
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com> Co-authored-by: aider (openrouter/anthropic/claude-sonnet-4) <aider@aider.chat> Co-authored-by: Warp <agent@warp.dev>
This commit is contained in:
co-authored by
Claude Opus 4.5
aider
Warp
parent
5bcdf8d614
commit
86fe32a682
@@ -0,0 +1,474 @@
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class PseudoHuberTests
|
||||
{
|
||||
private const double Epsilon = 1e-10;
|
||||
private const int DefaultPeriod = 14;
|
||||
|
||||
#region Constructor Tests
|
||||
|
||||
[Fact]
|
||||
public void Constructor_ValidatesInput()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => new PseudoHuber(0));
|
||||
Assert.Throws<ArgumentException>(() => new PseudoHuber(-1));
|
||||
Assert.Throws<ArgumentException>(() => new PseudoHuber(10, 0));
|
||||
Assert.Throws<ArgumentException>(() => new PseudoHuber(10, -1));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_ValidPeriod_Succeeds()
|
||||
{
|
||||
var pseudoHuber = new PseudoHuber(10);
|
||||
Assert.NotNull(pseudoHuber);
|
||||
Assert.Equal(10, pseudoHuber.WarmupPeriod);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_ValidDelta_Succeeds()
|
||||
{
|
||||
var pseudoHuber = new PseudoHuber(10, 0.5);
|
||||
Assert.NotNull(pseudoHuber);
|
||||
Assert.Equal(0.5, pseudoHuber.Delta);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Property Tests
|
||||
|
||||
[Fact]
|
||||
public void Properties_Accessible()
|
||||
{
|
||||
var pseudoHuber = new PseudoHuber(DefaultPeriod, 1.5);
|
||||
|
||||
Assert.Equal(0, pseudoHuber.Last.Value);
|
||||
Assert.False(pseudoHuber.IsHot);
|
||||
Assert.Contains("PseudoHuber", pseudoHuber.Name, StringComparison.Ordinal);
|
||||
Assert.Equal(1.5, pseudoHuber.Delta);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsHot_BecomesTrueWhenBufferFull()
|
||||
{
|
||||
var pseudoHuber = new PseudoHuber(5);
|
||||
|
||||
Assert.False(pseudoHuber.IsHot);
|
||||
|
||||
for (int i = 1; i <= 4; i++)
|
||||
{
|
||||
pseudoHuber.Update(100 + i, 100.0);
|
||||
Assert.False(pseudoHuber.IsHot);
|
||||
}
|
||||
|
||||
pseudoHuber.Update(105, 100.0);
|
||||
Assert.True(pseudoHuber.IsHot);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Calculation Tests
|
||||
|
||||
[Fact]
|
||||
public void Calculate_PerfectPredictions_ReturnsZero()
|
||||
{
|
||||
var pseudoHuber = new PseudoHuber(5);
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
double value = 100 + i;
|
||||
pseudoHuber.Update(value, value);
|
||||
}
|
||||
|
||||
Assert.Equal(0.0, pseudoHuber.Last.Value, Epsilon);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_SmallErrors_ApproximatesL2()
|
||||
{
|
||||
// For small errors, Pseudo-Huber ≈ 0.5 * error²
|
||||
var pseudoHuber = new PseudoHuber(1, delta: 10.0);
|
||||
const double error = 0.1; // Small relative to delta
|
||||
pseudoHuber.Update(100.0 + error, 100.0);
|
||||
|
||||
// Pseudo-Huber = δ² * (√(1 + (x/δ)²) - 1)
|
||||
// For small x/δ: √(1 + ε) ≈ 1 + ε/2, so loss ≈ δ² * (x/δ)²/2 = x²/2
|
||||
double expectedApprox = error * error / 2.0;
|
||||
double ratio = pseudoHuber.Last.Value / expectedApprox;
|
||||
|
||||
// Should be close to 1.0 for small errors
|
||||
Assert.InRange(ratio, 0.99, 1.01);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_LargeErrors_ApproximatesL1()
|
||||
{
|
||||
// For large errors, Pseudo-Huber ≈ δ * |error| - δ²/2
|
||||
var pseudoHuber = new PseudoHuber(1, delta: 1.0);
|
||||
double error = 100.0; // Large relative to delta
|
||||
pseudoHuber.Update(100.0 + error, 100.0);
|
||||
|
||||
// For large x: √(1 + (x/δ)²) ≈ |x/δ|
|
||||
// So loss ≈ δ² * (|x/δ| - 1) = δ|x| - δ²
|
||||
double expectedApprox = Math.Abs(error) - 1.0;
|
||||
double ratio = pseudoHuber.Last.Value / expectedApprox;
|
||||
|
||||
// Should be close to 1.0 for large errors
|
||||
Assert.InRange(ratio, 0.99, 1.01);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_SmoothTransition()
|
||||
{
|
||||
// Pseudo-Huber should be smooth across all error magnitudes
|
||||
var pseudoHuber = new PseudoHuber(1, delta: 1.0);
|
||||
double[] errors = { 0.01, 0.1, 0.5, 1.0, 2.0, 5.0, 10.0 };
|
||||
double[] losses = new double[errors.Length];
|
||||
|
||||
for (int i = 0; i < errors.Length; i++)
|
||||
{
|
||||
pseudoHuber.Reset();
|
||||
pseudoHuber.Update(100.0 + errors[i], 100.0);
|
||||
losses[i] = pseudoHuber.Last.Value;
|
||||
}
|
||||
|
||||
// Losses should be monotonically increasing
|
||||
for (int i = 1; i < losses.Length; i++)
|
||||
{
|
||||
Assert.True(losses[i] > losses[i - 1],
|
||||
$"Loss should increase: {losses[i - 1]} -> {losses[i]}");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_Symmetry()
|
||||
{
|
||||
// Pseudo-Huber should be symmetric: loss(e) = loss(-e)
|
||||
var pseudoHuber1 = new PseudoHuber(1);
|
||||
var pseudoHuber2 = new PseudoHuber(1);
|
||||
|
||||
double error = 5.0;
|
||||
pseudoHuber1.Update(100.0 + error, 100.0); // Positive error
|
||||
pseudoHuber2.Update(100.0 - error, 100.0); // Negative error
|
||||
|
||||
Assert.Equal(pseudoHuber1.Last.Value, pseudoHuber2.Last.Value, Epsilon);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_DeltaEffectOnTransition()
|
||||
{
|
||||
// Larger delta means smoother transition, smaller delta means sharper
|
||||
var smallDelta = new PseudoHuber(1, delta: 0.5);
|
||||
var largeDelta = new PseudoHuber(1, delta: 2.0);
|
||||
|
||||
double error = 1.0; // Fixed error
|
||||
smallDelta.Update(100.0 + error, 100.0);
|
||||
largeDelta.Update(100.0 + error, 100.0);
|
||||
|
||||
// With large delta, the loss is more quadratic (smaller)
|
||||
// With small delta, the loss is more linear (larger relative to quadratic)
|
||||
// The raw loss values depend on the formula
|
||||
Assert.True(double.IsFinite(smallDelta.Last.Value));
|
||||
Assert.True(double.IsFinite(largeDelta.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_ComparedToHuber()
|
||||
{
|
||||
// Pseudo-Huber should produce similar (but not identical) results to Huber
|
||||
var huber = new Huber(1, delta: 1.0);
|
||||
var pseudoHuber = new PseudoHuber(1, delta: 1.0);
|
||||
|
||||
// Test at various error magnitudes
|
||||
double[] errors = { 0.5, 1.0, 2.0 };
|
||||
|
||||
foreach (var error in errors)
|
||||
{
|
||||
huber.Reset();
|
||||
pseudoHuber.Reset();
|
||||
|
||||
huber.Update(100.0 + error, 100.0);
|
||||
pseudoHuber.Update(100.0 + error, 100.0);
|
||||
|
||||
// They should be in the same ballpark
|
||||
double ratio = pseudoHuber.Last.Value / huber.Last.Value;
|
||||
Assert.InRange(ratio, 0.5, 2.0); // Within factor of 2
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_AlwaysNonNegative()
|
||||
{
|
||||
var pseudoHuber = new PseudoHuber(DefaultPeriod);
|
||||
var gbm = new GBM();
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
var bar = gbm.Next();
|
||||
pseudoHuber.Update(bar.Close, bar.Close + (i % 2 == 0 ? 1 : -1) * (i + 1));
|
||||
Assert.True(pseudoHuber.Last.Value >= 0, "Pseudo-Huber loss should always be non-negative");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region State Management Tests
|
||||
|
||||
[Fact]
|
||||
public void Calculate_IsNew_False_UpdatesValue()
|
||||
{
|
||||
var pseudoHuber = new PseudoHuber(5);
|
||||
|
||||
pseudoHuber.Update(100.0, 99.0);
|
||||
pseudoHuber.Update(101.0, 99.0, isNew: true);
|
||||
double beforeUpdate = pseudoHuber.Last.Value;
|
||||
|
||||
pseudoHuber.Update(105.0, 99.0, isNew: false);
|
||||
double afterUpdate = pseudoHuber.Last.Value;
|
||||
|
||||
Assert.NotEqual(beforeUpdate, afterUpdate);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IterativeCorrections_RestoreToOriginalState()
|
||||
{
|
||||
var pseudoHuber = new PseudoHuber(5);
|
||||
var gbm = new GBM();
|
||||
|
||||
// Feed 10 new values
|
||||
double tenthActual = 0, tenthPredicted = 0;
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
var bar = gbm.Next(isNew: true);
|
||||
tenthActual = bar.Close;
|
||||
tenthPredicted = bar.Close * 0.99;
|
||||
pseudoHuber.Update(tenthActual, tenthPredicted, isNew: true);
|
||||
}
|
||||
|
||||
// Remember state after 10 values
|
||||
double stateAfterTen = pseudoHuber.Last.Value;
|
||||
|
||||
// Generate 9 corrections with isNew=false (different values)
|
||||
for (int i = 0; i < 9; i++)
|
||||
{
|
||||
var bar = gbm.Next(isNew: false);
|
||||
pseudoHuber.Update(bar.Close, bar.Close * 1.01, isNew: false);
|
||||
}
|
||||
|
||||
// Feed the remembered 10th input again with isNew=false
|
||||
var finalResult = pseudoHuber.Update(tenthActual, tenthPredicted, isNew: false);
|
||||
|
||||
// State should match the original state after 10 values
|
||||
Assert.Equal(stateAfterTen, finalResult.Value, Epsilon);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reset_ClearsState()
|
||||
{
|
||||
var pseudoHuber = new PseudoHuber(DefaultPeriod);
|
||||
|
||||
pseudoHuber.Update(100.0, 99.0);
|
||||
pseudoHuber.Update(101.0, 99.0);
|
||||
double valueBefore = pseudoHuber.Last.Value;
|
||||
|
||||
pseudoHuber.Reset();
|
||||
|
||||
Assert.Equal(0, pseudoHuber.Last.Value);
|
||||
Assert.False(pseudoHuber.IsHot);
|
||||
|
||||
pseudoHuber.Update(50.0, 49.0);
|
||||
Assert.NotEqual(0, pseudoHuber.Last.Value);
|
||||
Assert.NotEqual(valueBefore, pseudoHuber.Last.Value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Robustness Tests
|
||||
|
||||
[Fact]
|
||||
public void NaN_Input_UsesLastValidValue()
|
||||
{
|
||||
var pseudoHuber = new PseudoHuber(5);
|
||||
|
||||
pseudoHuber.Update(100.0, 99.0);
|
||||
pseudoHuber.Update(101.0, 99.5);
|
||||
|
||||
var resultAfterNaN = pseudoHuber.Update(double.NaN, 100.0);
|
||||
Assert.True(double.IsFinite(resultAfterNaN.Value));
|
||||
|
||||
var resultAfterNaN2 = pseudoHuber.Update(102.0, double.NaN);
|
||||
Assert.True(double.IsFinite(resultAfterNaN2.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Infinity_Input_UsesLastValidValue()
|
||||
{
|
||||
var pseudoHuber = new PseudoHuber(5);
|
||||
|
||||
pseudoHuber.Update(100.0, 99.0);
|
||||
pseudoHuber.Update(101.0, 99.5);
|
||||
|
||||
var resultAfterPosInf = pseudoHuber.Update(double.PositiveInfinity, 100.0);
|
||||
Assert.True(double.IsFinite(resultAfterPosInf.Value));
|
||||
|
||||
var resultAfterNegInf = pseudoHuber.Update(102.0, double.NegativeInfinity);
|
||||
Assert.True(double.IsFinite(resultAfterNegInf.Value));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Batch/Span Tests
|
||||
|
||||
[Fact]
|
||||
public void BatchCalc_MatchesIterativeCalc()
|
||||
{
|
||||
var gbm = new GBM();
|
||||
var actualSeries = new TSeries();
|
||||
var predictedSeries = new TSeries();
|
||||
|
||||
const int count = 100;
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
var bar = gbm.Next();
|
||||
actualSeries.Add(bar.Time, bar.Close);
|
||||
predictedSeries.Add(bar.Time, bar.Close * (1.0 + (i % 2 == 0 ? 0.01 : -0.01)));
|
||||
}
|
||||
|
||||
// Calculate iteratively
|
||||
var iterative = new PseudoHuber(DefaultPeriod);
|
||||
var iterativeResults = new List<double>();
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
iterativeResults.Add(iterative.Update(actualSeries[i].Value, predictedSeries[i].Value).Value);
|
||||
}
|
||||
|
||||
// Calculate batch
|
||||
var batchResults = PseudoHuber.Calculate(actualSeries, predictedSeries, DefaultPeriod);
|
||||
|
||||
// Compare
|
||||
Assert.Equal(iterativeResults.Count, batchResults.Count);
|
||||
for (int i = 0; i < batchResults.Count; i++)
|
||||
{
|
||||
Assert.Equal(batchResults[i].Value, iterativeResults[i], Epsilon);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SpanBatch_ValidatesInput()
|
||||
{
|
||||
double[] actual = [1, 2, 3, 4, 5];
|
||||
double[] predicted = [1.1, 2.1, 3.1, 4.1, 5.1];
|
||||
double[] output = new double[5];
|
||||
double[] wrongSizeOutput = new double[3];
|
||||
|
||||
Assert.Throws<ArgumentException>(() =>
|
||||
PseudoHuber.Batch(actual.AsSpan(), predicted.AsSpan(), wrongSizeOutput.AsSpan(), DefaultPeriod));
|
||||
|
||||
Assert.Throws<ArgumentException>(() =>
|
||||
PseudoHuber.Batch(actual.AsSpan(), predicted.AsSpan(), output.AsSpan(), 0));
|
||||
|
||||
Assert.Throws<ArgumentException>(() =>
|
||||
PseudoHuber.Batch(actual.AsSpan(), predicted.AsSpan(), output.AsSpan(), DefaultPeriod, 0));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SpanBatch_MatchesTSeriesBatch()
|
||||
{
|
||||
var gbm = new GBM();
|
||||
var actualSeries = new TSeries();
|
||||
var predictedSeries = new TSeries();
|
||||
double[] actualData = new double[100];
|
||||
double[] predictedData = new double[100];
|
||||
double[] output = new double[100];
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
var bar = gbm.Next();
|
||||
actualData[i] = bar.Close;
|
||||
predictedData[i] = bar.Close * 0.99;
|
||||
actualSeries.Add(bar.Time, actualData[i]);
|
||||
predictedSeries.Add(bar.Time, predictedData[i]);
|
||||
}
|
||||
|
||||
var tseriesResult = PseudoHuber.Calculate(actualSeries, predictedSeries, DefaultPeriod);
|
||||
PseudoHuber.Batch(actualData.AsSpan(), predictedData.AsSpan(), output.AsSpan(), DefaultPeriod);
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
Assert.Equal(tseriesResult[i].Value, output[i], Epsilon);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SpanBatch_HandlesNaN()
|
||||
{
|
||||
double[] actual = [100, 110, double.NaN, 120, 130];
|
||||
double[] predicted = [99, 109, 115, double.NaN, 129];
|
||||
double[] output = new double[5];
|
||||
|
||||
PseudoHuber.Batch(actual.AsSpan(), predicted.AsSpan(), output.AsSpan(), 3);
|
||||
|
||||
foreach (var val in output)
|
||||
{
|
||||
Assert.True(double.IsFinite(val), $"Expected finite value but got {val}");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Error Handling Tests
|
||||
|
||||
[Fact]
|
||||
public void Update_ThrowsOnSingleInput()
|
||||
{
|
||||
var pseudoHuber = new PseudoHuber(DefaultPeriod);
|
||||
var input = new TValue(DateTime.UtcNow, 100.0);
|
||||
|
||||
Assert.Throws<NotSupportedException>(() => pseudoHuber.Update(input));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Prime_ThrowsNotSupported()
|
||||
{
|
||||
var pseudoHuber = new PseudoHuber(DefaultPeriod);
|
||||
double[] data = [1, 2, 3, 4, 5];
|
||||
|
||||
Assert.Throws<NotSupportedException>(() => pseudoHuber.Prime(data.AsSpan()));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_MismatchedSeriesLengths_Throws()
|
||||
{
|
||||
var actual = new TSeries();
|
||||
var predicted = new TSeries();
|
||||
|
||||
actual.Add(DateTime.UtcNow, 100);
|
||||
actual.Add(DateTime.UtcNow, 101);
|
||||
predicted.Add(DateTime.UtcNow, 99);
|
||||
|
||||
Assert.Throws<ArgumentException>(() => PseudoHuber.Calculate(actual, predicted, 5));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Resync Tests
|
||||
|
||||
[Fact]
|
||||
public void Resync_PreventsFloatingPointDrift()
|
||||
{
|
||||
var pseudoHuber = new PseudoHuber(10);
|
||||
var gbm = new GBM();
|
||||
|
||||
// Feed many values to trigger resync
|
||||
for (int i = 0; i < 2500; i++)
|
||||
{
|
||||
var bar = gbm.Next();
|
||||
pseudoHuber.Update(bar.Close, bar.Close * 0.99);
|
||||
}
|
||||
|
||||
// Should still produce valid results after many iterations
|
||||
Assert.True(double.IsFinite(pseudoHuber.Last.Value));
|
||||
Assert.True(pseudoHuber.Last.Value >= 0);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace QuanTAlib;
|
||||
|
||||
/// <summary>
|
||||
/// PseudoHuber: Pseudo-Huber Loss (Charbonnier Loss)
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The Pseudo-Huber loss is a smooth approximation to the Huber loss function.
|
||||
/// Unlike Huber loss which has a piecewise definition, Pseudo-Huber is smooth
|
||||
/// and differentiable everywhere, making it ideal for gradient-based optimization.
|
||||
///
|
||||
/// Formula:
|
||||
/// PseudoHuber = δ² * (√(1 + (error/δ)²) - 1)
|
||||
///
|
||||
/// Key properties:
|
||||
/// - Smooth and continuously differentiable everywhere
|
||||
/// - Approximates L2 (squared error) for small errors
|
||||
/// - Approximates L1 (absolute error) for large errors
|
||||
/// - δ (delta) controls the transition point
|
||||
/// - More computationally efficient than Huber's conditional logic
|
||||
/// - Also known as Charbonnier loss in image processing
|
||||
/// </remarks>
|
||||
[SkipLocalsInit]
|
||||
public sealed class PseudoHuber : BiInputIndicatorBase
|
||||
{
|
||||
private readonly double _deltaSquared;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the delta parameter (transition scale).
|
||||
/// </summary>
|
||||
public double Delta { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Creates a Pseudo-Huber Loss indicator.
|
||||
/// </summary>
|
||||
/// <param name="period">Number of values to average (must be > 0)</param>
|
||||
/// <param name="delta">Scale parameter controlling transition smoothness (must be > 0). Default 1.0</param>
|
||||
public PseudoHuber(int period, double delta = 1.0)
|
||||
: base(period, $"PseudoHuber({period},{delta:F3})")
|
||||
{
|
||||
if (delta <= 0)
|
||||
throw new ArgumentException("Delta must be positive", nameof(delta));
|
||||
|
||||
Delta = delta;
|
||||
_deltaSquared = delta * delta;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Computes Pseudo-Huber loss: δ² * (√(1 + (error/δ)²) - 1)
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected override double ComputeError(double actual, double predicted)
|
||||
{
|
||||
double diff = actual - predicted;
|
||||
double ratio = diff / Delta;
|
||||
double sqrtTerm = Math.Sqrt(1.0 + ratio * ratio);
|
||||
return Math.FusedMultiplyAdd(_deltaSquared, sqrtTerm, -_deltaSquared);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates Pseudo-Huber Loss for two time series.
|
||||
/// </summary>
|
||||
public static TSeries Calculate(TSeries actual, TSeries predicted, int period, double delta = 1.0)
|
||||
{
|
||||
if (actual.Count != predicted.Count)
|
||||
throw new ArgumentException("Actual and predicted series must have the same length", nameof(predicted));
|
||||
|
||||
int len = actual.Count;
|
||||
var t = new List<long>(len);
|
||||
var v = new List<double>(len);
|
||||
CollectionsMarshal.SetCount(t, len);
|
||||
CollectionsMarshal.SetCount(v, len);
|
||||
|
||||
var tSpan = CollectionsMarshal.AsSpan(t);
|
||||
var vSpan = CollectionsMarshal.AsSpan(v);
|
||||
|
||||
Batch(actual.Values, predicted.Values, vSpan, period, delta);
|
||||
actual.Times.CopyTo(tSpan);
|
||||
|
||||
return new TSeries(t, v);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Batch computation of Pseudo-Huber Loss using shared error helpers.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Batch(ReadOnlySpan<double> actual, ReadOnlySpan<double> predicted, Span<double> output, int period, double delta = 1.0)
|
||||
{
|
||||
if (actual.Length != predicted.Length || actual.Length != output.Length)
|
||||
throw new ArgumentException("All spans must have the same length", nameof(output));
|
||||
if (period <= 0)
|
||||
throw new ArgumentException("Period must be greater than 0", nameof(period));
|
||||
if (delta <= 0)
|
||||
throw new ArgumentException("Delta must be positive", nameof(delta));
|
||||
|
||||
int len = actual.Length;
|
||||
if (len == 0) return;
|
||||
|
||||
// Pre-compute Pseudo-Huber errors using shared helper
|
||||
const int StackAllocThreshold = 256;
|
||||
Span<double> errors = len <= StackAllocThreshold
|
||||
? stackalloc double[len]
|
||||
: new double[len];
|
||||
|
||||
ErrorHelpers.ComputePseudoHuberErrors(actual, predicted, errors, delta);
|
||||
|
||||
// Apply rolling mean
|
||||
ErrorHelpers.ApplyRollingMean(errors, output, period);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
# Pseudo-Huber: Smooth Huber Approximation
|
||||
|
||||
> "All the robustness of Huber, none of the discontinuities."
|
||||
|
||||
Pseudo-Huber Loss (also called Charbonnier Loss) is a smooth approximation to the Huber loss function. Unlike Huber which has a piecewise definition with a kink at δ, Pseudo-Huber is continuously differentiable everywhere, making it ideal for gradient-based optimization.
|
||||
|
||||
## Historical Context
|
||||
|
||||
The Pseudo-Huber function emerged from the optimization and machine learning communities as a way to get Huber-like robustness while maintaining smooth gradients. It's also known as Charbonnier loss in image processing, where it's used for edge-preserving smoothing and optical flow estimation.
|
||||
|
||||
## Architecture & Physics
|
||||
|
||||
Pseudo-Huber uses the formula δ²(√(1 + (x/δ)²) - 1), which smoothly interpolates between quadratic behavior for small errors and linear behavior for large errors. The transition is gradual rather than abrupt, with no discontinuity in derivatives.
|
||||
|
||||
### Properties
|
||||
|
||||
* **Smooth everywhere**: Infinitely differentiable (unlike Huber's kink)
|
||||
* **Non-negative**: Always ≥ 0, with 0 for perfect prediction
|
||||
* **Robust**: Large errors grow linearly, not quadratically
|
||||
* **Tunable**: δ (delta) controls the L2-to-L1 transition point
|
||||
|
||||
## Mathematical Foundation
|
||||
|
||||
### 1. Pseudo-Huber Function
|
||||
|
||||
For each error, compute:
|
||||
|
||||
$$L_\delta(e) = \delta^2 \left(\sqrt{1 + \left(\frac{e}{\delta}\right)^2} - 1\right)$$
|
||||
|
||||
Where:
|
||||
* $e = y - \hat{y}$ = prediction error
|
||||
* $\delta$ = tuning parameter (transition width)
|
||||
|
||||
### 2. Asymptotic Behavior
|
||||
|
||||
For small errors (|e| << δ):
|
||||
|
||||
$$L_\delta(e) \approx \frac{e^2}{2}$$
|
||||
|
||||
For large errors (|e| >> δ):
|
||||
|
||||
$$L_\delta(e) \approx \delta|e| - \delta^2$$
|
||||
|
||||
### 3. Gradient (Derivative)
|
||||
|
||||
$$\frac{dL}{de} = \frac{e}{\sqrt{1 + (e/\delta)^2}}$$
|
||||
|
||||
This approaches:
|
||||
* e for small errors (like L2)
|
||||
* δ·sign(e) for large errors (like L1)
|
||||
|
||||
### 4. Running Update (O(1))
|
||||
|
||||
QuanTAlib uses a ring buffer with running sum for O(1) updates:
|
||||
|
||||
$$S_{new} = S_{old} - L_{oldest} + L_{newest}$$
|
||||
|
||||
$$PseudoHuber = \frac{S_{new}}{n}$$
|
||||
|
||||
## Implementation Details
|
||||
|
||||
### Usage Patterns
|
||||
|
||||
```csharp
|
||||
// Streaming mode - with custom delta
|
||||
var pseudoHuber = new PseudoHuber(period: 20, delta: 1.0);
|
||||
var result = pseudoHuber.Update(actualValue, predictedValue);
|
||||
|
||||
// Batch mode - calculate for entire series
|
||||
var results = PseudoHuber.Calculate(actualSeries, predictedSeries, period: 20, delta: 1.0);
|
||||
|
||||
// Span mode - zero-allocation for high performance
|
||||
PseudoHuber.Batch(actualSpan, predictedSpan, outputSpan, period: 20, delta: 1.0);
|
||||
```
|
||||
|
||||
### Parameters
|
||||
|
||||
| Parameter | Type | Default | Description |
|
||||
| :--- | :--- | :--- | :--- |
|
||||
| **period** | int | - | Lookback window for averaging (must be > 0) |
|
||||
| **delta** | double | 1.0 | Transition parameter (must be > 0) |
|
||||
|
||||
### Properties
|
||||
|
||||
| Property | Type | Description |
|
||||
| :--- | :--- | :--- |
|
||||
| **Last** | TValue | Most recent Pseudo-Huber value |
|
||||
| **IsHot** | bool | True when buffer is full |
|
||||
| **Delta** | double | Current delta parameter |
|
||||
| **Name** | string | Indicator name (e.g., "PseudoHuber(20,1.000)") |
|
||||
| **WarmupPeriod** | int | Number of periods before valid output |
|
||||
|
||||
## Performance Profile
|
||||
|
||||
| Metric | Score | Notes |
|
||||
| :--- | :--- | :--- |
|
||||
| **Throughput** | ~15 ns/bar | O(1) update, sqrt computation |
|
||||
| **Allocations** | 0 | Uses pre-allocated ring buffer |
|
||||
| **Complexity** | O(1) | Constant time per update |
|
||||
| **Accuracy** | 10/10 | Exact calculation |
|
||||
| **Timeliness** | 9/10 | No lag beyond the period |
|
||||
| **Smoothness** | 10/10 | Infinitely differentiable |
|
||||
|
||||
## Comparison with Huber
|
||||
|
||||
| Aspect | Huber | Pseudo-Huber |
|
||||
| :--- | :--- | :--- |
|
||||
| **Small errors** | e²/2 | ≈ e²/2 |
|
||||
| **Large errors** | δ\|e\| - δ²/2 | ≈ δ\|e\| - δ² |
|
||||
| **At e = δ** | Kink (C¹) | Smooth (C^∞) |
|
||||
| **Gradient** | Discontinuous 2nd derivative | Continuous all derivatives |
|
||||
| **Computation** | Conditional logic | Single formula |
|
||||
| **Optimization** | Can cause issues | Smooth convergence |
|
||||
|
||||
### Numerical Comparison
|
||||
|
||||
| Error (e) | Huber (δ=1) | Pseudo-Huber (δ=1) |
|
||||
| :--- | :--- | :--- |
|
||||
| **0.0** | 0.000 | 0.000 |
|
||||
| **0.5** | 0.125 | 0.118 |
|
||||
| **1.0** | 0.500 | 0.414 |
|
||||
| **2.0** | 1.500 | 1.236 |
|
||||
| **10.0** | 9.500 | 9.049 |
|
||||
|
||||
Pseudo-Huber produces slightly smaller values but follows the same qualitative behavior.
|
||||
|
||||
## Choosing δ
|
||||
|
||||
| δ Value | Behavior | Use Case |
|
||||
| :--- | :--- | :--- |
|
||||
| **0.1** | Quickly linear | Aggressive outlier handling |
|
||||
| **1.0** | Balanced | Standard choice |
|
||||
| **10.0** | Mostly quadratic | Near-MSE behavior |
|
||||
| **100.0** | Almost pure L2 | When outliers are rare |
|
||||
|
||||
## Common Use Cases
|
||||
|
||||
1. **Neural Network Training**: Smooth loss for gradient descent
|
||||
2. **Computer Vision**: Optical flow, stereo matching
|
||||
3. **Robust Regression**: When smoothness matters for optimization
|
||||
4. **Image Processing**: Edge-preserving filtering (Charbonnier)
|
||||
|
||||
## Edge Cases
|
||||
|
||||
* **Perfect Predictions**: Returns exactly 0
|
||||
* **NaN Handling**: Uses last valid value substitution
|
||||
* **Single Input**: Not supported (requires two series)
|
||||
* **δ = 0**: Invalid (division by zero)
|
||||
* **Large Errors**: Numerically stable (no overflow)
|
||||
|
||||
## Related Indicators
|
||||
|
||||
* [Huber](../huber/Huber.md) - Huber Loss (piecewise, with kink)
|
||||
* [LogCosh](../logcosh/LogCosh.md) - Log-Cosh Loss (different smooth approximation)
|
||||
* [MAE](../mae/Mae.md) - Mean Absolute Error (pure L1)
|
||||
* [MSE](../mse/Mse.md) - Mean Squared Error (pure L2)
|
||||
Reference in New Issue
Block a user