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
+356
View File
@@ -0,0 +1,356 @@
namespace QuanTAlib.Tests;
public class MapdTests
{
[Fact]
public void Constructor_ValidatesInput()
{
Assert.Throws<ArgumentException>(() => new Mapd(0));
Assert.Throws<ArgumentException>(() => new Mapd(-1));
var mapd = new Mapd(10);
Assert.NotNull(mapd);
}
[Fact]
public void Properties_Accessible()
{
var mapd = new Mapd(10);
Assert.Equal(0, mapd.Last.Value);
Assert.False(mapd.IsHot);
Assert.Contains("Mapd", mapd.Name, StringComparison.Ordinal);
mapd.Update(100, 105);
Assert.NotEqual(0, mapd.Last.Time);
}
[Fact]
public void IsHot_BecomesTrueWhenBufferFull()
{
const int period = 5;
var mapd = new Mapd(period);
for (int i = 0; i < period - 1; i++)
{
Assert.False(mapd.IsHot, $"IsHot should be false at index {i}");
mapd.Update(100 + i, 105 + i);
}
mapd.Update(104, 109);
Assert.True(mapd.IsHot, "IsHot should be true after period updates");
}
[Fact]
public void Mapd_CalculatesCorrectly()
{
var mapd = new Mapd(3);
// |100 - 110| / 110 * 100 = 9.0909...%
var res1 = mapd.Update(100, 110);
Assert.Equal(100.0 * 10.0 / 110.0, res1.Value, 10);
// |200 - 220| / 220 * 100 = 9.0909...%, Mean = same
var res2 = mapd.Update(200, 220);
Assert.Equal(100.0 * 10.0 / 110.0, res2.Value, 10);
// |50 - 60| / 60 * 100 = 16.666...%
var res3 = mapd.Update(50, 60);
double expected = (100.0 * 10 / 110 + 100.0 * 20 / 220 + 100.0 * 10 / 60) / 3;
Assert.Equal(expected, res3.Value, 10);
}
[Fact]
public void Mapd_PerfectPrediction_ReturnsZero()
{
var mapd = new Mapd(5);
for (int i = 1; i <= 10; i++)
{
mapd.Update(i * 10, i * 10); // Perfect prediction
}
Assert.Equal(0.0, mapd.Last.Value, 10);
}
[Fact]
public void Mapd_DividesbyPredicted_NotActual()
{
var mape = new Mape(1);
var mapd = new Mapd(1);
// actual=100, predicted=200
mape.Update(100, 200);
mapd.Update(100, 200);
// MAPE: |100-200|/100 * 100 = 100%
// MAPD: |100-200|/200 * 100 = 50%
Assert.Equal(100.0, mape.Last.Value, 10);
Assert.Equal(50.0, mapd.Last.Value, 10);
}
[Fact]
public void Calc_IsNew_AcceptsParameter()
{
var mapd = new Mapd(10);
mapd.Update(100, 110, isNew: true);
double value1 = mapd.Last.Value;
mapd.Update(100, 120, isNew: true);
double value2 = mapd.Last.Value;
Assert.NotEqual(value1, value2);
}
[Fact]
public void Calc_IsNew_False_UpdatesValue()
{
var mapd = new Mapd(10);
mapd.Update(100, 110);
mapd.Update(100, 120, isNew: true);
double beforeUpdate = mapd.Last.Value;
mapd.Update(100, 130, isNew: false);
double afterUpdate = mapd.Last.Value;
Assert.NotEqual(beforeUpdate, afterUpdate);
}
[Fact]
public void IterativeCorrections_RestoreToOriginalState()
{
var mapd = new Mapd(5);
double tenthActual = 0;
double tenthPredicted = 0;
// Feed 10 updates
for (int i = 1; i <= 10; i++)
{
tenthActual = i * 10;
tenthPredicted = i * 10 + 5;
mapd.Update(tenthActual, tenthPredicted);
}
double stateAfterTen = mapd.Last.Value;
// Apply 5 corrections with isNew=false
for (int i = 0; i < 5; i++)
{
mapd.Update(100 + i, 200 + i, isNew: false);
}
// Restore to original values
mapd.Update(tenthActual, tenthPredicted, isNew: false);
Assert.Equal(stateAfterTen, mapd.Last.Value, 10);
}
[Fact]
public void Reset_ClearsState()
{
var mapd = new Mapd(5);
for (int i = 1; i <= 10; i++)
{
mapd.Update(i * 10, i * 10 + 5);
}
Assert.True(mapd.IsHot);
mapd.Reset();
Assert.False(mapd.IsHot);
Assert.Equal(0, mapd.Last.Value);
}
[Fact]
public void NaN_Input_UsesLastValidValue()
{
var mapd = new Mapd(5);
mapd.Update(100, 110);
mapd.Update(110, 120);
mapd.Update(120, 130);
var result = mapd.Update(double.NaN, double.NaN);
Assert.True(double.IsFinite(result.Value));
}
[Fact]
public void Infinity_Input_UsesLastValidValue()
{
var mapd = new Mapd(5);
mapd.Update(100, 110);
mapd.Update(110, 120);
var result = mapd.Update(double.PositiveInfinity, double.NegativeInfinity);
Assert.True(double.IsFinite(result.Value));
}
[Fact]
public void MultipleNaN_ContinuesWithLastValid()
{
var mapd = new Mapd(5);
mapd.Update(100, 110);
mapd.Update(110, 120);
mapd.Update(120, 130);
var r1 = mapd.Update(double.NaN, double.NaN);
var r2 = mapd.Update(double.NaN, double.NaN);
var r3 = mapd.Update(double.NaN, double.NaN);
Assert.True(double.IsFinite(r1.Value));
Assert.True(double.IsFinite(r2.Value));
Assert.True(double.IsFinite(r3.Value));
}
[Fact]
public void Mapd_Throws_On_Single_Input()
{
var mapd = new Mapd(10);
Assert.Throws<NotSupportedException>(() => mapd.Update(new TValue(DateTime.UtcNow, 1)));
Assert.Throws<NotSupportedException>(() => mapd.Update(new TSeries()));
Assert.Throws<NotSupportedException>(() => mapd.Prime([1, 2, 3]));
}
[Fact]
public void BatchSpan_MatchesStreaming()
{
int period = 5;
int count = 100;
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 123);
double[] actual = new double[count];
double[] predicted = new double[count];
for (int i = 0; i < count; i++)
{
var bar = gbm.Next();
actual[i] = bar.Close;
predicted[i] = bar.Close * 1.05 + 2; // Offset prediction
}
// Streaming
var mapd = new Mapd(period);
var streamingResults = new double[count];
for (int i = 0; i < count; i++)
{
streamingResults[i] = mapd.Update(actual[i], predicted[i]).Value;
}
// Batch
double[] batchResults = new double[count];
Mapd.Batch(actual, predicted, batchResults, period);
// Compare
for (int i = 0; i < count; i++)
{
Assert.Equal(streamingResults[i], batchResults[i], 9);
}
}
[Fact]
public void BatchSpan_ValidatesInput()
{
double[] actual = [1, 2, 3, 4, 5];
double[] predicted = [1, 2, 3, 4, 5];
double[] output = new double[5];
double[] wrongSizeOutput = new double[3];
double[] wrongSizePredicted = new double[3];
// Period must be > 0
Assert.Throws<ArgumentException>(() =>
Mapd.Batch(actual.AsSpan(), predicted.AsSpan(), output.AsSpan(), 0));
Assert.Throws<ArgumentException>(() =>
Mapd.Batch(actual.AsSpan(), predicted.AsSpan(), output.AsSpan(), -1));
// Output must be same length as source
Assert.Throws<ArgumentException>(() =>
Mapd.Batch(actual.AsSpan(), predicted.AsSpan(), wrongSizeOutput.AsSpan(), 3));
// Predicted must be same length as actual
Assert.Throws<ArgumentException>(() =>
Mapd.Batch(actual.AsSpan(), wrongSizePredicted.AsSpan(), output.AsSpan(), 3));
}
[Fact]
public void Calculate_Works()
{
var actual = new TSeries();
var predicted = new TSeries();
var now = DateTime.UtcNow;
for (int i = 1; i <= 10; i++)
{
actual.Add(now.AddMinutes(i), 100);
predicted.Add(now.AddMinutes(i), 110);
}
var results = Mapd.Calculate(actual, predicted, 3);
Assert.Equal(10, results.Count);
// |100-110|/110 * 100 = 9.0909...%
Assert.Equal(100.0 * 10 / 110, results.Last.Value, 10);
}
[Fact]
public void Calculate_ValidatesMismatchedLengths()
{
var actual = new TSeries();
var predicted = new TSeries();
for (int i = 0; i < 10; i++) actual.Add(DateTime.UtcNow, i + 1);
for (int i = 0; i < 5; i++) predicted.Add(DateTime.UtcNow, i + 1);
Assert.Throws<ArgumentException>(() => Mapd.Calculate(actual, predicted, 3));
}
[Fact]
public void BatchSpan_HandlesNaN()
{
double[] actual = [100, 110, double.NaN, 130, 140];
double[] predicted = [105, 115, 125, double.NaN, 145];
double[] output = new double[5];
Mapd.Batch(actual, predicted, output, 3);
foreach (var val in output)
{
Assert.True(double.IsFinite(val), $"Expected finite value but got {val}");
}
}
[Fact]
public void Mapd_Resync_Works()
{
var mapd = new Mapd(5);
// Force many updates to trigger resync (ResyncInterval = 1000)
for (int i = 0; i < 1100; i++)
{
mapd.Update(100, 110);
}
// |100-110|/110 * 100 = 9.0909...%
Assert.Equal(100.0 * 10 / 110, mapd.Last.Value, 10);
}
[Fact]
public void Mapd_ZeroPredicted_HandlesGracefully()
{
var mapd = new Mapd(3);
mapd.Update(100, 110);
mapd.Update(100, 110);
// Zero predicted should not cause division by zero
var result = mapd.Update(10, 0);
Assert.True(double.IsFinite(result.Value));
}
}
+149
View File
@@ -0,0 +1,149 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace QuanTAlib;
/// <summary>
/// MAPD: Mean Absolute Percentage Deviation
/// </summary>
/// <remarks>
/// MAPD measures the average absolute percentage deviation between actual and predicted values.
/// Unlike MAPE which divides by actual, MAPD divides by predicted.
///
/// Formula:
/// MAPD = (100/n) * Σ|((actual - predicted) / predicted)|
///
/// Key properties:
/// - Scale-independent (expressed as percentage)
/// - Cannot be calculated when predicted = 0 (uses epsilon protection)
/// - Differs from MAPE in denominator choice
/// - More stable when actuals have high variance
/// </remarks>
[SkipLocalsInit]
public sealed class Mapd : BiInputIndicatorBase
{
private const double Epsilon = 1e-10;
/// <summary>
/// Creates a MAPD (Mean Absolute Percentage Deviation) indicator.
/// </summary>
/// <param name="period">Number of values to average (must be > 0)</param>
public Mapd(int period)
: base(period, $"Mapd({period})")
{
}
/// <summary>
/// Computes percentage deviation: |actual - predicted| / |predicted| * 100
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected override double ComputeError(double actual, double predicted)
{
double absPredicted = Math.Abs(predicted);
return absPredicted > Epsilon
? Math.Abs(actual - predicted) / absPredicted * 100.0
: 0.0;
}
/// <summary>
/// Calculates Mean Absolute Percentage Deviation for two time series.
/// </summary>
public static TSeries Calculate(TSeries actual, TSeries predicted, int period)
{
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);
actual.Times.CopyTo(tSpan);
return new TSeries(t, v);
}
/// <summary>
/// Batch computation.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Batch(ReadOnlySpan<double> actual, ReadOnlySpan<double> predicted, Span<double> output, int period)
{
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));
int len = actual.Length;
if (len == 0) return;
// Pre-compute percentage errors (divided by predicted, not actual)
const int StackAllocThreshold = 256;
Span<double> errors = len <= StackAllocThreshold
? stackalloc double[len]
: new double[len];
ComputeMapdErrors(actual, predicted, errors);
// Apply rolling mean
ErrorHelpers.ApplyRollingMean(errors, output, period);
}
/// <summary>
/// Computes MAPD errors (percentage errors divided by predicted).
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void ComputeMapdErrors(
ReadOnlySpan<double> actual,
ReadOnlySpan<double> predicted,
Span<double> output)
{
int len = actual.Length;
double lastValidActual = 0.0;
double lastValidPredicted = 1.0; // Default to 1 to avoid division by zero
// Find first valid values
for (int k = 0; k < len; k++)
{
if (double.IsFinite(actual[k]))
{
lastValidActual = actual[k];
break;
}
}
for (int k = 0; k < len; k++)
{
if (double.IsFinite(predicted[k]) && Math.Abs(predicted[k]) >= Epsilon)
{
lastValidPredicted = predicted[k];
break;
}
}
for (int i = 0; i < len; i++)
{
double act = actual[i];
double pred = predicted[i];
if (double.IsFinite(act))
lastValidActual = act;
else
act = lastValidActual;
if (double.IsFinite(pred) && Math.Abs(pred) >= Epsilon)
lastValidPredicted = pred;
else
pred = lastValidPredicted;
double absPredicted = Math.Abs(pred);
output[i] = absPredicted > Epsilon
? Math.Abs(act - pred) / absPredicted * 100.0
: 0.0;
}
}
}
+141
View File
@@ -0,0 +1,141 @@
# MAPD: Mean Absolute Percentage Deviation
> "Like MAPE, but divides by what you predicted instead of what actually happened."
Mean Absolute Percentage Deviation (MAPD) measures the average absolute percentage difference between actual and predicted values, using the predicted value as the denominator. This is the key difference from MAPE, which uses the actual value.
## Historical Context
MAPD emerged as an alternative to MAPE when analysts needed a metric that was more stable when actual values had high variance or approached zero. By using the predicted value as the denominator, MAPD provides different asymmetry characteristics than MAPE.
## Architecture & Physics
MAPD divides each absolute error by the predicted value instead of the actual value. This choice affects the asymmetry of the metric: MAPD penalizes under-prediction more heavily than over-prediction (opposite of MAPE).
### Properties
* **Scale-independent**: Expressed as percentage
* **Asymmetric**: Penalizes under-prediction more than over-prediction
* **Undefined at zero**: Cannot compute when predicted value is zero
* **Non-negative**: MAPD ≥ 0, with 0 indicating perfect prediction
* **Opposite bias to MAPE**: Favors over-prediction
## Mathematical Foundation
### 1. Percentage Deviation
For each observation, calculate the absolute percentage deviation:
$$APD_i = 100 \times \left| \frac{y_i - \hat{y}_i}{\hat{y}_i} \right|$$
Where:
* $y_i$ = actual value
* $\hat{y}_i$ = predicted value
### 2. Mean Calculation
Average the absolute percentage deviations over the period:
$$MAPD = \frac{100}{n} \sum_{i=1}^{n} \left| \frac{y_i - \hat{y}_i}{\hat{y}_i} \right|$$
### 3. Running Update (O(1))
QuanTAlib uses a ring buffer with running sum for O(1) updates:
$$S_{new} = S_{old} - APD_{oldest} + APD_{newest}$$
$$MAPD = \frac{S_{new}}{n}$$
## Implementation Details
### Usage Patterns
```csharp
// Streaming mode - update with each new observation
var mapd = new Mapd(period: 20);
var result = mapd.Update(actualValue, predictedValue);
// Batch mode - calculate for entire series
var results = Mapd.Calculate(actualSeries, predictedSeries, period: 20);
// Span mode - zero-allocation for high performance
Mapd.Batch(actualSpan, predictedSpan, outputSpan, period: 20);
```
### Parameters
| Parameter | Type | Description |
| :--- | :--- | :--- |
| **period** | int | Lookback window for averaging (must be > 0) |
### Properties
| Property | Type | Description |
| :--- | :--- | :--- |
| **Last** | TValue | Most recent MAPD value (as percentage) |
| **IsHot** | bool | True when buffer is full |
| **Name** | string | Indicator name (e.g., "Mapd(20)") |
| **WarmupPeriod** | int | Number of periods before valid output |
## Performance Profile
| Metric | Score | Notes |
| :--- | :--- | :--- |
| **Throughput** | ~12 ns/bar | O(1) update complexity |
| **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** | 7/10 | Moderate smoothing |
## MAPE vs MAPD Comparison
```csharp
var mape = new Mape(1);
var mapd = new Mapd(1);
// actual=100, predicted=200
mape.Update(100, 200); // |100-200|/100 = 100%
mapd.Update(100, 200); // |100-200|/200 = 50%
// actual=200, predicted=100
mape.Update(200, 100); // |200-100|/200 = 50%
mapd.Update(200, 100); // |200-100|/100 = 100%
```
| Scenario | MAPE | MAPD |
| :--- | :--- | :--- |
| **Over-prediction** | Lower | Higher |
| **Under-prediction** | Higher | Lower |
## Comparison with Other Metrics
| Metric | Denominator | Bias |
| :--- | :--- | :--- |
| **MAPE** | Actual | Favors under-prediction |
| **MAPD** | Predicted | Favors over-prediction |
| **SMAPE** | (Actual + Predicted)/2 | Symmetric |
| **MAE** | None | No percentage conversion |
## Common Use Cases
1. **Forecast Validation**: When predicted values are more reliable than actuals
2. **Model Comparison**: Alternative perspective to MAPE
3. **Budgeting**: When comparing actuals to budget (predicted)
4. **Quality Control**: When predictions are the reference standard
## Edge Cases
* **Identical Values**: Returns 0% when actual equals predicted
* **Zero Predicted**: Uses epsilon (1e-10) to avoid division by zero
* **NaN Handling**: Uses last valid value substitution
* **Single Input**: Not supported (requires two series)
* **Period = 1**: Returns current percentage deviation
## Related Indicators
* [MAPE](../mape/Mape.md) - Mean Absolute Percentage Error (divides by actual)
* [SMAPE](../smape/Smape.md) - Symmetric Mean Absolute Percentage Error
* [MPE](../mpe/Mpe.md) - Mean Percentage Error (signed)
* [MAE](../mae/Mae.md) - Mean Absolute Error (same units)