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:
Miha Kralj
2026-01-18 19:02:03 -08:00
committed by GitHub
co-authored by Claude Opus 4.5 aider Warp
parent 5bcdf8d614
commit 86fe32a682
1750 changed files with 198235 additions and 80539 deletions
+388
View File
@@ -0,0 +1,388 @@
namespace QuanTAlib.Tests;
public class MsleTests
{
private const double Precision = 1e-10;
[Fact]
public void Constructor_ValidatesInput()
{
Assert.Throws<ArgumentException>(() => new Msle(0));
Assert.Throws<ArgumentException>(() => new Msle(-1));
var msle = new Msle(10);
Assert.NotNull(msle);
}
[Fact]
public void Calc_ReturnsValue()
{
var msle = new Msle(10);
var result = msle.Update(100.0, 90.0);
Assert.True(double.IsFinite(result.Value));
Assert.Equal(result.Value, msle.Last.Value);
}
[Fact]
public void ZeroError_ReturnsZero()
{
var msle = new Msle(5);
for (int i = 0; i < 5; i++)
{
msle.Update(100.0, 100.0);
}
Assert.Equal(0.0, msle.Last.Value, Precision);
}
[Fact]
public void KnownValues_CalculatesCorrectly()
{
var msle = new Msle(1);
// MSLE = (log(1 + actual) - log(1 + predicted))²
// actual=99, predicted=49 -> log(100) - log(50) = ln(100) - ln(50) = ln(2)
// MSLE = ln(2)² ≈ 0.480453
var result = msle.Update(99.0, 49.0);
double expected = Math.Pow(Math.Log(100.0) - Math.Log(50.0), 2);
Assert.Equal(expected, result.Value, Precision);
}
[Fact]
public void Period1_ReturnsCurrentError()
{
var msle = new Msle(1);
// actual=9, predicted=4 -> log(10) - log(5) = ln(2)
var r1 = msle.Update(9.0, 4.0);
double expected1 = Math.Pow(Math.Log(10.0) - Math.Log(5.0), 2);
Assert.Equal(expected1, r1.Value, Precision);
// Perfect prediction
var r2 = msle.Update(100.0, 100.0);
Assert.Equal(0.0, r2.Value, Precision);
}
[Fact]
public void AsymmetricPenalty_UnderPredictionPenalizedMore()
{
var msle1 = new Msle(1);
var msle2 = new Msle(1);
// Under-prediction: actual=100, predicted=50
// log(101) - log(51) ≈ 0.683
var underPred = msle1.Update(100.0, 50.0);
// Over-prediction: actual=50, predicted=100
// log(51) - log(101) ≈ -0.683
var overPred = msle2.Update(50.0, 100.0);
// Squared errors are equal for MSLE (unlike MAPE)
// But the raw log errors show asymmetry
Assert.Equal(underPred.Value, overPred.Value, Precision);
}
[Fact]
public void ZeroValues_HandledCorrectly()
{
var msle = new Msle(1);
// actual=0, predicted=0 -> log(1) - log(1) = 0
var bothZero = msle.Update(0.0, 0.0);
Assert.Equal(0.0, bothZero.Value, Precision);
// actual=0, predicted=9 -> log(1) - log(10) = -ln(10)
var actualZero = msle.Update(0.0, 9.0);
double expectedActualZero = Math.Pow(Math.Log(1.0) - Math.Log(10.0), 2);
Assert.Equal(expectedActualZero, actualZero.Value, Precision);
// actual=9, predicted=0 -> log(10) - log(1) = ln(10)
var predZero = msle.Update(9.0, 0.0);
double expectedPredZero = Math.Pow(Math.Log(10.0) - Math.Log(1.0), 2);
Assert.Equal(expectedPredZero, predZero.Value, Precision);
}
[Fact]
public void LargeScale_CompressesErrors()
{
var msle = new Msle(1);
var mse = new Mse(1);
// Large values: actual=1000000, predicted=500000
var msleResult = msle.Update(1000000.0, 500000.0);
var mseResult = mse.Update(1000000.0, 500000.0);
// MSE = (500000)² = 2.5e11
// MSLE = (log(1000001) - log(500001))² ≈ 0.48 (much smaller)
Assert.True(msleResult.Value < 1.0);
Assert.True(mseResult.Value > 1e10);
}
[Fact]
public void NaN_Input_UsesLastValidValue()
{
var msle = new Msle(5);
msle.Update(100.0, 90.0);
msle.Update(100.0, 95.0);
var resultAfterNaN = msle.Update(double.NaN, 90.0);
Assert.True(double.IsFinite(resultAfterNaN.Value));
}
[Fact]
public void Infinity_Input_UsesLastValidValue()
{
var msle = new Msle(5);
msle.Update(100.0, 90.0);
var resultAfterPosInf = msle.Update(double.PositiveInfinity, 90.0);
Assert.True(double.IsFinite(resultAfterPosInf.Value));
var resultAfterNegInf = msle.Update(100.0, double.NegativeInfinity);
Assert.True(double.IsFinite(resultAfterNegInf.Value));
}
[Fact]
public void NegativeValues_TreatedAsInvalid()
{
var msle = new Msle(5);
msle.Update(100.0, 90.0);
// Negative values should use last valid value
var resultAfterNeg = msle.Update(-50.0, 90.0);
Assert.True(double.IsFinite(resultAfterNeg.Value));
}
[Fact]
public void IsHot_BecomesTrueWhenBufferFull()
{
var msle = new Msle(5);
Assert.False(msle.IsHot);
for (int i = 1; i <= 4; i++)
{
msle.Update(100.0, 90.0 + i);
Assert.False(msle.IsHot);
}
msle.Update(100.0, 95.0);
Assert.True(msle.IsHot);
}
[Fact]
public void Reset_ClearsState()
{
var msle = new Msle(10);
msle.Update(100.0, 90.0);
msle.Update(100.0, 95.0);
msle.Reset();
Assert.Equal(0, msle.Last.Value);
Assert.False(msle.IsHot);
}
[Fact]
public void IsNew_False_UpdatesCurrentBar()
{
var msle = new Msle(5);
msle.Update(100.0, 90.0);
double valueBefore = msle.Last.Value;
msle.Update(100.0, 95.0, isNew: false);
double valueAfter = msle.Last.Value;
Assert.NotEqual(valueBefore, valueAfter);
}
[Fact]
public void IterativeCorrections_RestoreToOriginalState()
{
var msle = new Msle(5);
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1);
for (int i = 0; i < 10; i++)
{
var bar = gbm.Next(isNew: true);
msle.Update(bar.Close, bar.Close * 0.95, isNew: true);
}
double stateAfterTen = msle.Last.Value;
var lastBar = gbm.Next(isNew: false);
double lastActual = lastBar.Close;
double lastPredicted = lastBar.Close * 0.95;
for (int i = 0; i < 5; i++)
{
var bar = gbm.Next(isNew: false);
msle.Update(bar.Close, bar.Close * 0.9, isNew: false);
}
msle.Update(lastActual, lastPredicted, isNew: false);
Assert.Equal(stateAfterTen, msle.Last.Value, 1e-6);
}
[Fact]
public void BatchCalc_MatchesIterativeCalc()
{
var msleIterative = new Msle(10);
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1, seed: 42);
var actualSeries = new TSeries();
var predictedSeries = new TSeries();
for (int i = 0; i < 100; i++)
{
var bar = gbm.Next(isNew: true);
actualSeries.Add(bar.Time, bar.Close);
predictedSeries.Add(bar.Time, bar.Close * 0.95);
}
var iterativeResults = new List<double>();
for (int i = 0; i < actualSeries.Count; i++)
{
iterativeResults.Add(msleIterative.Update(actualSeries[i], predictedSeries[i]).Value);
}
var batchResults = Msle.Calculate(actualSeries, predictedSeries, 10);
Assert.Equal(iterativeResults.Count, batchResults.Count);
for (int i = 0; i < iterativeResults.Count; i++)
{
Assert.Equal(iterativeResults[i], batchResults[i].Value, Precision);
}
}
[Fact]
public void SpanBatch_ValidatesInput()
{
double[] actual = [100, 100, 100];
double[] predicted = [90, 95, 100];
double[] output = new double[3];
double[] wrongSizeOutput = new double[2];
Assert.Throws<ArgumentException>(() =>
Msle.Batch(actual.AsSpan(), predicted.AsSpan(), wrongSizeOutput.AsSpan(), 3));
Assert.Throws<ArgumentException>(() =>
Msle.Batch(actual.AsSpan(), predicted.AsSpan(), output.AsSpan(), 0));
}
[Fact]
public void SpanBatch_MatchesTSeriesBatch()
{
var gbm = new GBM(startPrice: 100.0, mu: 0.02, sigma: 0.1, seed: 42);
var actualSeries = new TSeries();
var predictedSeries = new TSeries();
double[] actualArr = new double[100];
double[] predictedArr = new double[100];
double[] output = new double[100];
for (int i = 0; i < 100; i++)
{
var bar = gbm.Next(isNew: true);
actualArr[i] = bar.Close;
predictedArr[i] = bar.Close * 0.95;
actualSeries.Add(bar.Time, bar.Close);
predictedSeries.Add(bar.Time, bar.Close * 0.95);
}
var tseriesResult = Msle.Calculate(actualSeries, predictedSeries, 10);
Msle.Batch(actualArr.AsSpan(), predictedArr.AsSpan(), output.AsSpan(), 10);
for (int i = 0; i < 100; i++)
{
Assert.Equal(tseriesResult[i].Value, output[i], Precision);
}
}
[Fact]
public void SpanBatch_HandlesNaN()
{
double[] actual = [100, 100, double.NaN, 100, 100];
double[] predicted = [90, 95, 92, double.NaN, 95];
double[] output = new double[5];
Msle.Batch(actual.AsSpan(), predicted.AsSpan(), output.AsSpan(), 3);
foreach (var val in output)
{
Assert.True(double.IsFinite(val), $"Expected finite value but got {val}");
}
}
[Fact]
public void Calculate_MismatchedLengths_ThrowsException()
{
var actual = new TSeries();
var predicted = new TSeries();
actual.Add(DateTime.UtcNow.Ticks, 100);
actual.Add(DateTime.UtcNow.Ticks + 1, 100);
predicted.Add(DateTime.UtcNow.Ticks, 90);
Assert.Throws<ArgumentException>(() => Msle.Calculate(actual, predicted, 5));
}
[Fact]
public void Name_IsSetCorrectly()
{
var msle = new Msle(14);
Assert.Equal("Msle(14)", msle.Name);
}
[Fact]
public void WarmupPeriod_IsSetCorrectly()
{
var msle = new Msle(20);
Assert.Equal(20, msle.WarmupPeriod);
}
[Fact]
public void SlidingWindow_Works()
{
var msle = new Msle(3);
// actual=0, predicted=0 -> MSLE = 0
msle.Update(0.0, 0.0);
Assert.Equal(0.0, msle.Last.Value, Precision);
// actual=e-1≈1.718, predicted=0 -> log(e) - log(1) = 1 -> MSLE = 1
msle.Update(Math.E - 1, 0.0);
// Average: (0 + 1) / 2 = 0.5
Assert.Equal(0.5, msle.Last.Value, Precision);
// actual=0, predicted=0 -> MSLE = 0
msle.Update(0.0, 0.0);
// Average: (0 + 1 + 0) / 3 = 1/3
Assert.Equal(1.0 / 3.0, msle.Last.Value, Precision);
}
[Fact]
public void MultiplicativeRelationship_ConsistentError()
{
// MSLE is consistent for multiplicative relationships
var msle1 = new Msle(1);
var msle2 = new Msle(1);
var mse1 = new Mse(1);
var mse2 = new Mse(1);
// actual=10, predicted=5 (ratio 2:1)
var smallMsle = msle1.Update(10.0, 5.0);
var smallMse = mse1.Update(10.0, 5.0);
// actual=1000, predicted=500 (ratio 2:1)
var largeMsle = msle2.Update(1000.0, 500.0);
var largeMse = mse2.Update(1000.0, 500.0);
// MSLE should be more consistent for same ratios than MSE
// log(11) - log(6) ≈ 0.606 vs log(1001) - log(501) ≈ 0.692
// The +1 offset causes some difference for small values
double msleDiff = Math.Abs(smallMsle.Value - largeMsle.Value);
double mseRatio = largeMse.Value / smallMse.Value;
// MSLE difference should be much smaller than the MSE ratio
// MSE: 25 vs 250000 (ratio of 10000)
// MSLE difference is only about 0.12 (squared log errors)
Assert.True(msleDiff < 0.2, $"MSLE difference was {msleDiff}");
Assert.True(mseRatio > 1000, $"MSE ratio was {mseRatio}");
}
}
+99
View File
@@ -0,0 +1,99 @@
using System.Runtime.CompilerServices;
namespace QuanTAlib;
/// <summary>
/// MSLE: Mean Squared Logarithmic Error
/// </summary>
/// <remarks>
/// MSLE measures the ratio between actual and predicted values using logarithms,
/// penalizing under-predictions more than over-predictions of the same magnitude.
/// Useful when targets span several orders of magnitude.
///
/// Formula:
/// MSLE = (1/n) * Σ(log(1 + actual) - log(1 + predicted))²
///
/// Key properties:
/// - Robust to outliers (logarithmic compression)
/// - Penalizes under-predictions more heavily
/// - Requires non-negative values (uses 1 + x to handle zeros)
/// - Scale-independent for multiplicative relationships
/// </remarks>
[SkipLocalsInit]
public sealed class Msle : BiInputIndicatorBase
{
/// <summary>
/// Creates MSLE with specified period.
/// </summary>
/// <param name="period">Number of values to average (must be > 0)</param>
public Msle(int period) : base(period, $"Msle({period})") { }
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected override double ComputeError(double actual, double predicted)
{
// Ensure non-negative (MSLE requires non-negative values)
double act = actual < 0 ? 0 : actual;
double pred = predicted < 0 ? 0 : predicted;
// MSLE formula: (log(1 + actual) - log(1 + predicted))²
double logActual = Math.Log(1.0 + act);
double logPredicted = Math.Log(1.0 + pred);
double logError = logActual - logPredicted;
return logError * logError;
}
/// <summary>
/// Calculates MSLE for entire series.
/// </summary>
public static TSeries Calculate(TSeries actual, TSeries predicted, int period)
=> CalculateImpl(actual, predicted, period, Batch);
/// <summary>
/// Batch calculation using log squared error computation with rolling mean.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Batch(ReadOnlySpan<double> actual, ReadOnlySpan<double> predicted, Span<double> output, int period)
{
ValidateBatchInputs(actual, predicted, output, period);
int len = actual.Length;
if (len == 0) return;
const int StackAllocThreshold = 256;
Span<double> errors = len <= StackAllocThreshold
? stackalloc double[len]
: new double[len];
ComputeLogSquaredErrors(actual, predicted, errors);
ErrorHelpers.ApplyRollingMean(errors, output, period);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void ComputeLogSquaredErrors(ReadOnlySpan<double> actual, ReadOnlySpan<double> predicted, Span<double> output)
{
int len = actual.Length;
double lastValidActual = 0, lastValidPredicted = 0;
// Find first valid non-negative values
for (int i = 0; i < len; i++)
if (double.IsFinite(actual[i]) && actual[i] >= 0) { lastValidActual = actual[i]; break; }
for (int i = 0; i < len; i++)
if (double.IsFinite(predicted[i]) && predicted[i] >= 0) { lastValidPredicted = predicted[i]; break; }
for (int i = 0; i < len; i++)
{
double act = actual[i];
double pred = predicted[i];
// Handle NaN/Infinity and negative values
if (double.IsFinite(act) && act >= 0) lastValidActual = act; else act = lastValidActual;
if (double.IsFinite(pred) && pred >= 0) lastValidPredicted = pred; else pred = lastValidPredicted;
double logActual = Math.Log(1.0 + act);
double logPredicted = Math.Log(1.0 + pred);
double logError = logActual - logPredicted;
output[i] = logError * logError;
}
}
}
+170
View File
@@ -0,0 +1,170 @@
# MSLE: Mean Squared Logarithmic Error
> "When your data spans orders of magnitude, MSLE keeps outliers from hijacking your loss function."
Mean Squared Logarithmic Error transforms both actual and predicted values through logarithms before computing squared error. This compression makes MSLE robust to outliers and particularly suited for data with exponential growth patterns or wide dynamic ranges.
## Architecture & Physics
MSLE computes the squared difference in log space:
$$\text{MSLE} = \frac{1}{n} \sum_{i=1}^{n} \left(\log(1 + \text{actual}_i) - \log(1 + \text{predicted}_i)\right)^2$$
The `1 + x` transformation ensures defined behavior at zero and prevents negative arguments to the logarithm.
### Logarithmic Compression
For large values, logarithms compress the scale dramatically:
| Actual | Predicted | Absolute Error | MSE | MSLE |
| :--- | :--- | :--- | :--- | :--- |
| 100 | 50 | 50 | 2,500 | 0.48 |
| 10,000 | 5,000 | 5,000 | 25,000,000 | 0.48 |
| 1,000,000 | 500,000 | 500,000 | 2.5×10¹¹ | 0.48 |
Same ratio (2:1) produces nearly identical MSLE regardless of scale.
## Mathematical Foundation
### 1. Log Transform
$$\tilde{x} = \log(1 + x)$$
### 2. Squared Log Error
$$e_i = \left(\log(1 + \text{actual}_i) - \log(1 + \text{predicted}_i)\right)^2$$
This can be rewritten using the quotient rule:
$$e_i = \left(\log\frac{1 + \text{actual}_i}{1 + \text{predicted}_i}\right)^2$$
### 3. Rolling Average
$$\text{MSLE}_t = \frac{1}{n} \sum_{i=t-n+1}^{t} e_i$$
## Performance Profile
| Metric | Score | Notes |
| :--- | :--- | :--- |
| **Throughput** | 25 ns/bar | O(1) via running sum |
| **Allocations** | 0 | Zero-allocation hot path |
| **Complexity** | O(1) | Constant per update |
| **Outlier Robustness** | 9/10 | Log compression |
| **Scale Independence** | 10/10 | Ratio-based comparison |
| **Zero Handling** | 10/10 | Uses 1+x transform |
| **Interpretability** | 5/10 | Log-scale units |
## Usage
```csharp
// Streaming mode - ideal for growth metrics
var msle = new Msle(20);
// Price prediction with wide range
msle.Update(actual: 1000.0, predicted: 950.0);
msle.Update(actual: 100000.0, predicted: 95000.0); // Same 5% error, similar MSLE
double logError = msle.Last.Value;
// Batch mode - historical analysis
var actual = new TSeries { 100, 1000, 10000, 100000 };
var predicted = new TSeries { 95, 950, 9500, 95000 };
var results = Msle.Calculate(actual, predicted, period: 3);
// Span mode - zero-allocation bulk processing
Span<double> output = stackalloc double[1000];
Msle.Batch(actualSpan, predictedSpan, output, period: 20);
```
## Interpretation Guide
| MSLE Value | Interpretation | Approximate Ratio Error |
| :--- | :--- | :--- |
| **0** | Perfect prediction | 1:1 |
| **0.01** | Excellent | ~10% ratio error |
| **0.1** | Good | ~30% ratio error |
| **0.5** | Moderate | ~70% ratio error |
| **1.0** | Poor | ~170% ratio error |
| **2.0** | Very poor | ~300% ratio error |
To convert MSLE to approximate percentage error:
$$\text{Ratio Error} \approx e^{\sqrt{\text{MSLE}}} - 1$$
## Use Cases
### 1. Growth Metrics
Revenue, user counts, and other metrics with exponential growth:
```csharp
// Day 1: Revenue $1,000, predicted $900
// Day 100: Revenue $1,000,000, predicted $900,000
// Both have same 10% error, MSLE treats them equally
```
### 2. Price Prediction
Stock prices, real estate, and other values spanning decades:
```csharp
// 1990: AAPL $0.30, predicted $0.27 (10% error)
// 2024: AAPL $180, predicted $162 (10% error)
// MSE would be dominated by 2024; MSLE balances both
```
### 3. Population/Count Data
Any count that varies by orders of magnitude:
```csharp
// City A: Population 10,000, predicted 9,000
// City B: Population 10,000,000, predicted 9,000,000
// MSLE weights these equally
```
## Comparison with Related Metrics
| Metric | Best For | Limitation |
| :--- | :--- | :--- |
| **MSE** | Uniform scale data | Outlier sensitive |
| **MSLE** | Wide dynamic range | Requires non-negative |
| **MAPE** | Percentage comparison | Undefined at zero |
| **Huber** | Mixed outliers | Requires delta tuning |
## Common Pitfalls
### 1. Negative Values
MSLE requires non-negative inputs. The implementation clamps negative values to 0:
```csharp
// negative actual or predicted → uses last valid value or 0
```
For data with negative values, consider MSE or ME instead.
### 2. Asymmetry
While MSLE squares the log error (making it sign-independent), the logarithm itself is asymmetric around ratios. Predicting 2x the actual has different log error than predicting 0.5x:
```csharp
// actual=100, predicted=200: log(101/201) ≈ -0.69
// actual=100, predicted=50: log(101/51) ≈ 0.68
// After squaring: ~0.48 vs ~0.46 (slightly different)
```
### 3. Near-Zero Sensitivity
Near zero, small absolute differences create large MSLE:
```csharp
// actual=0, predicted=1: log(1/2) = -0.69 → MSLE = 0.48
// actual=0, predicted=9: log(1/10) = -2.30 → MSLE = 5.30
```
## See Also
* [RMSLE](../rmsle/Rmsle.md) - Root of MSLE for interpretable units
* [MSE](../mse/Mse.md) - Linear-scale squared error
* [MAPE](../mape/Mape.md) - Percentage-based comparison