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
+359
View File
@@ -0,0 +1,359 @@
namespace QuanTAlib.Tests;
public class MaeTests
{
[Fact]
public void Constructor_ValidatesInput()
{
Assert.Throws<ArgumentException>(() => new Mae(0));
Assert.Throws<ArgumentException>(() => new Mae(-1));
var mae = new Mae(10);
Assert.NotNull(mae);
}
[Fact]
public void Properties_Accessible()
{
var mae = new Mae(10);
Assert.Equal(0, mae.Last.Value);
Assert.False(mae.IsHot);
Assert.Contains("Mae", mae.Name, StringComparison.Ordinal);
mae.Update(100, 105);
Assert.NotEqual(0, mae.Last.Time);
}
[Fact]
public void IsHot_BecomesTrueWhenBufferFull()
{
const int period = 5;
var mae = new Mae(period);
for (int i = 0; i < period - 1; i++)
{
Assert.False(mae.IsHot, $"IsHot should be false at index {i}");
mae.Update(i * 10, i * 10 + 5);
}
mae.Update((period - 1) * 10, (period - 1) * 10 + 5);
Assert.True(mae.IsHot, "IsHot should be true after period updates");
}
[Fact]
public void Mae_CalculatesCorrectly()
{
var mae = new Mae(3);
// |10 - 15| = 5
var res1 = mae.Update(10, 15);
Assert.Equal(5.0, res1.Value, 10);
// |20 - 30| = 10, Mean = (5 + 10) / 2 = 7.5
var res2 = mae.Update(20, 30);
Assert.Equal(7.5, res2.Value, 10);
// |30 - 25| = 5, Mean = (5 + 10 + 5) / 3 = 6.666...
var res3 = mae.Update(30, 25);
Assert.Equal(20.0 / 3.0, res3.Value, 10);
// |40 - 35| = 5, Window slides: (10 + 5 + 5) / 3 = 6.666...
var res4 = mae.Update(40, 35);
Assert.Equal(20.0 / 3.0, res4.Value, 10);
}
[Fact]
public void Mae_PerfectPrediction_ReturnsZero()
{
var mae = new Mae(5);
for (int i = 0; i < 10; i++)
{
mae.Update(i * 10, i * 10); // Perfect prediction
}
Assert.Equal(0.0, mae.Last.Value, 10);
}
[Fact]
public void Mae_ConstantError_ReturnsConstant()
{
var mae = new Mae(5);
for (int i = 0; i < 10; i++)
{
mae.Update(100, 110); // Constant error of 10
}
Assert.Equal(10.0, mae.Last.Value, 10);
}
[Fact]
public void Mae_NegativeError_TakesAbsoluteValue()
{
var mae = new Mae(3);
// Error = |15 - 10| = 5 (predicted > actual)
mae.Update(10, 15);
// Error = |20 - 30| = 10 (predicted > actual)
mae.Update(20, 30);
// Error = |50 - 25| = 25 (predicted < actual)
mae.Update(50, 25);
// Mean = (5 + 10 + 25) / 3 = 40 / 3
Assert.Equal(40.0 / 3.0, mae.Last.Value, 10);
}
[Fact]
public void Calc_IsNew_AcceptsParameter()
{
var mae = new Mae(10);
mae.Update(100, 110, isNew: true);
double value1 = mae.Last.Value;
mae.Update(100, 120, isNew: true);
double value2 = mae.Last.Value;
Assert.NotEqual(value1, value2);
}
[Fact]
public void Calc_IsNew_False_UpdatesValue()
{
var mae = new Mae(10);
mae.Update(100, 110);
mae.Update(100, 120, isNew: true);
double beforeUpdate = mae.Last.Value;
mae.Update(100, 130, isNew: false);
double afterUpdate = mae.Last.Value;
Assert.NotEqual(beforeUpdate, afterUpdate);
}
[Fact]
public void IterativeCorrections_RestoreToOriginalState()
{
var mae = new Mae(5);
double tenthActual = 0;
double tenthPredicted = 0;
// Feed 10 updates
for (int i = 0; i < 10; i++)
{
tenthActual = i * 10;
tenthPredicted = i * 10 + 5;
mae.Update(tenthActual, tenthPredicted);
}
double stateAfterTen = mae.Last.Value;
// Apply 5 corrections with isNew=false
for (int i = 0; i < 5; i++)
{
mae.Update(100 + i, 200 + i, isNew: false);
}
// Restore to original values
mae.Update(tenthActual, tenthPredicted, isNew: false);
Assert.Equal(stateAfterTen, mae.Last.Value, 10);
}
[Fact]
public void Reset_ClearsState()
{
var mae = new Mae(5);
for (int i = 0; i < 10; i++)
{
mae.Update(i * 10, i * 10 + 5);
}
Assert.True(mae.IsHot);
mae.Reset();
Assert.False(mae.IsHot);
Assert.Equal(0, mae.Last.Value);
}
[Fact]
public void NaN_Input_UsesLastValidValue()
{
var mae = new Mae(5);
mae.Update(100, 110);
mae.Update(110, 120);
mae.Update(120, 130);
var result = mae.Update(double.NaN, double.NaN);
Assert.True(double.IsFinite(result.Value));
}
[Fact]
public void Infinity_Input_UsesLastValidValue()
{
var mae = new Mae(5);
mae.Update(100, 110);
mae.Update(110, 120);
var result = mae.Update(double.PositiveInfinity, double.NegativeInfinity);
Assert.True(double.IsFinite(result.Value));
}
[Fact]
public void MultipleNaN_ContinuesWithLastValid()
{
var mae = new Mae(5);
mae.Update(100, 110);
mae.Update(110, 120);
mae.Update(120, 130);
var r1 = mae.Update(double.NaN, double.NaN);
var r2 = mae.Update(double.NaN, double.NaN);
var r3 = mae.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 Mae_Throws_On_Single_Input()
{
var mae = new Mae(10);
Assert.Throws<NotSupportedException>(() => mae.Update(new TValue(DateTime.UtcNow, 1)));
Assert.Throws<NotSupportedException>(() => mae.Update(new TSeries()));
Assert.Throws<NotSupportedException>(() => mae.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 mae = new Mae(period);
var streamingResults = new double[count];
for (int i = 0; i < count; i++)
{
streamingResults[i] = mae.Update(actual[i], predicted[i]).Value;
}
// Batch
double[] batchResults = new double[count];
Mae.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>(() =>
Mae.Batch(actual.AsSpan(), predicted.AsSpan(), output.AsSpan(), 0));
Assert.Throws<ArgumentException>(() =>
Mae.Batch(actual.AsSpan(), predicted.AsSpan(), output.AsSpan(), -1));
// Output must be same length as source
Assert.Throws<ArgumentException>(() =>
Mae.Batch(actual.AsSpan(), predicted.AsSpan(), wrongSizeOutput.AsSpan(), 3));
// Predicted must be same length as actual
Assert.Throws<ArgumentException>(() =>
Mae.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 = 0; i < 10; i++)
{
actual.Add(now.AddMinutes(i), i * 10);
predicted.Add(now.AddMinutes(i), i * 10 + 5);
}
var results = Mae.Calculate(actual, predicted, 3);
Assert.Equal(10, results.Count);
// All errors are 5, so MAE should be 5
Assert.Equal(5.0, 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);
for (int i = 0; i < 5; i++) predicted.Add(DateTime.UtcNow, i);
Assert.Throws<ArgumentException>(() => Mae.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];
Mae.Batch(actual, predicted, output, 3);
foreach (var val in output)
{
Assert.True(double.IsFinite(val), $"Expected finite value but got {val}");
}
}
[Fact]
public void Mae_Resync_Works()
{
var mae = new Mae(5);
// Force many updates to trigger resync (ResyncInterval = 1000)
for (int i = 0; i < 1100; i++)
{
mae.Update(i, i + 10); // Constant error of 10
}
// After resync, result should still be correct
Assert.Equal(10.0, mae.Last.Value, 10);
}
}
+74
View File
@@ -0,0 +1,74 @@
using MathNet.Numerics;
using QuanTAlib.Tests;
namespace QuanTAlib.Validation;
public sealed class MaeValidationTests : IDisposable
{
private readonly ValidationTestData _data = new();
public void Dispose() => _data.Dispose();
[Fact]
public void Mae_Matches_MathNet()
{
int[] periods = { 5, 10, 20, 50, 100 };
var quotes = _data.SkenderQuotes.ToList();
double[] actual = quotes.Select(q => (double)q.Close).ToArray();
double[] predicted = quotes.Select(q => (double)q.Open).ToArray();
foreach (int period in periods)
{
var mae = new Mae(period);
for (int i = 0; i < actual.Length; i++)
{
var val = mae.Update(
new TValue(quotes[i].Date, actual[i]),
new TValue(quotes[i].Date, predicted[i]));
// Validate last 100 bars
if (i >= actual.Length - 100 && i >= period - 1)
{
var windowActual = actual[(i - period + 1)..(i + 1)];
var windowPredicted = predicted[(i - period + 1)..(i + 1)];
double expected = Distance.MAE(windowActual, windowPredicted);
Assert.Equal(expected, val.Value, 1e-9);
}
}
}
}
[Fact]
public void Mae_Batch_Matches_MathNet()
{
int[] periods = { 5, 10, 20, 50, 100 };
var quotes = _data.SkenderQuotes.ToList();
double[] actual = quotes.Select(q => (double)q.Close).ToArray();
double[] predicted = quotes.Select(q => (double)q.Open).ToArray();
foreach (int period in periods)
{
double[] output = new double[actual.Length];
Mae.Batch(actual, predicted, output, period);
// Validate last 100 bars
for (int i = actual.Length - 100; i < actual.Length; i++)
{
if (i >= period - 1)
{
var windowActual = actual[(i - period + 1)..(i + 1)];
var windowPredicted = predicted[(i - period + 1)..(i + 1)];
double expected = Distance.MAE(windowActual, windowPredicted);
Assert.Equal(expected, output[i], 1e-9);
}
}
}
}
}
+89
View File
@@ -0,0 +1,89 @@
using System.Buffers;
using System.Runtime.CompilerServices;
namespace QuanTAlib;
/// <summary>
/// MAE: Mean Absolute Error
/// </summary>
/// <remarks>
/// MAE measures the average magnitude of errors between paired observations,
/// without considering their direction. It is the mean of the absolute differences
/// between actual and predicted values.
///
/// Formula:
/// MAE = (1/n) * Σ|actual - predicted|
///
/// Uses a RingBuffer for O(1) streaming updates with running sum.
///
/// Key properties:
/// - Always non-negative (MAE ≥ 0)
/// - Same units as the original data
/// - Less sensitive to outliers than MSE/RMSE
/// - MAE = 0 indicates perfect prediction
/// </remarks>
[SkipLocalsInit]
public sealed class Mae : BiInputIndicatorBase
{
/// <summary>
/// Creates MAE with specified period.
/// </summary>
/// <param name="period">Number of values to average (must be > 0)</param>
public Mae(int period) : base(period, $"Mae({period})") { }
/// <summary>
/// Computes absolute error: |actual - predicted|
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected override double ComputeError(double actual, double predicted)
=> Math.Abs(actual - predicted);
/// <summary>
/// Calculates MAE for the entire series pair.
/// </summary>
/// <param name="actual">Actual values series</param>
/// <param name="predicted">Predicted values series</param>
/// <param name="period">MAE period</param>
/// <returns>MAE series</returns>
public static TSeries Calculate(TSeries actual, TSeries predicted, int period)
=> CalculateImpl(actual, predicted, period, Batch);
/// <summary>
/// Calculates MAE in-place using pre-allocated spans.
/// Uses SIMD acceleration when available.
/// </summary>
/// <param name="actual">Actual values</param>
/// <param name="predicted">Predicted values</param>
/// <param name="output">Output span (must be same length as inputs)</param>
/// <param name="period">MAE period (must be > 0)</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Batch(ReadOnlySpan<double> actual, ReadOnlySpan<double> predicted, Span<double> output, int period)
{
ValidateBatchInputs(actual, predicted, output, period);
if (actual.Length == 0) return;
// Allocate temporary buffer for absolute errors
const int StackAllocThreshold = 256;
int len = actual.Length;
if (len <= StackAllocThreshold)
{
Span<double> absErrors = stackalloc double[len];
ErrorHelpers.ComputeAbsoluteErrors(actual, predicted, absErrors);
ErrorHelpers.ApplyRollingMean(absErrors, output, period);
}
else
{
double[] rented = ArrayPool<double>.Shared.Rent(len);
try
{
Span<double> absErrors = rented.AsSpan(0, len);
ErrorHelpers.ComputeAbsoluteErrors(actual, predicted, absErrors);
ErrorHelpers.ApplyRollingMean(absErrors, output, period);
}
finally
{
ArrayPool<double>.Shared.Return(rented);
}
}
}
}
+125
View File
@@ -0,0 +1,125 @@
# MAE: Mean Absolute Error
> "When you need to know how wrong you are on average, without the drama of squared errors."
Mean Absolute Error (MAE) measures the average magnitude of errors in a set of predictions, without considering their direction. It represents the average of the absolute differences between actual and predicted values.
## Historical Context
MAE is one of the oldest and most intuitive error metrics in statistics. Its simplicity and interpretability have made it a staple in regression analysis, forecasting, and model evaluation since the early days of statistical analysis.
## Architecture & Physics
MAE treats all errors equally, making it more robust to outliers compared to squared-error metrics like MSE. The absolute value operation removes directionality, focusing purely on error magnitude.
### Properties
* **Non-negative**: MAE ≥ 0, with 0 indicating perfect prediction
* **Same units**: Unlike MSE, MAE is in the same units as the original data
* **Linear sensitivity**: Each unit of error contributes equally to the final metric
* **Robust**: Less sensitive to outliers than squared-error metrics
## Mathematical Foundation
### 1. Absolute Error
For each observation, calculate the absolute difference between actual and predicted values:
$$e_i = |y_i - \hat{y}_i|$$
Where:
* $y_i$ = actual value
* $\hat{y}_i$ = predicted value
### 2. Mean Calculation
Average the absolute errors over the period:
$$MAE = \frac{1}{n} \sum_{i=1}^{n} |y_i - \hat{y}_i|$$
### 3. Running Update (O(1))
QuanTAlib uses a ring buffer with running sum for O(1) updates:
$$S_{new} = S_{old} - e_{oldest} + e_{newest}$$
$$MAE = \frac{S_{new}}{n}$$
## Implementation Details
### Usage Patterns
```csharp
// Streaming mode - update with each new observation
var mae = new Mae(period: 20);
var result = mae.Update(actualValue, predictedValue);
// Batch mode - calculate for entire series
var results = Mae.Calculate(actualSeries, predictedSeries, period: 20);
// Span mode - zero-allocation for high performance
Mae.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 MAE value |
| **IsHot** | bool | True when buffer is full |
| **Name** | string | Indicator name (e.g., "Mae(20)") |
| **WarmupPeriod** | int | Number of periods before valid output |
## Performance Profile
| Metric | Score | Notes |
| :--- | :--- | :--- |
| **Throughput** | ~10 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 |
## Interpretation
| MAE Range | Interpretation |
| :--- | :--- |
| **0** | Perfect prediction |
| **Low** | Predictions are close to actual values |
| **High** | Large average prediction error |
## Comparison with Other Metrics
| Metric | Outlier Sensitivity | Units | Interpretation |
| :--- | :--- | :--- | :--- |
| **MAE** | Low | Same as data | Average absolute error |
| **MSE** | High | Squared units | Penalizes large errors more |
| **RMSE** | High | Same as data | MSE in original units |
| **MAPE** | Varies | Percentage | Relative error |
## Common Use Cases
1. **Forecast Evaluation**: Measure prediction accuracy over time
2. **Model Comparison**: Compare different prediction models
3. **Trading Strategy**: Track signal accuracy
4. **Risk Assessment**: Monitor prediction reliability
## Edge Cases
* **Identical Values**: Returns 0 when actual equals predicted
* **NaN Handling**: Uses last valid value substitution
* **Single Input**: Not supported (requires two series)
* **Period = 1**: Returns current absolute error
## Related Indicators
* [MSE](../mse/Mse.md) - Mean Squared Error
* [RMSE](../rmse/Rmse.md) - Root Mean Squared Error
* [MAPE](../mape/Mape.md) - Mean Absolute Percentage Error