mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-25 13:58:04 +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,78 @@
|
||||
using System.Buffers;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace QuanTAlib;
|
||||
|
||||
/// <summary>
|
||||
/// ME: Mean Error (also known as Mean Bias Error)
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// ME measures the average error between actual and predicted values,
|
||||
/// preserving the sign to indicate systematic bias in predictions.
|
||||
///
|
||||
/// Formula:
|
||||
/// ME = (1/n) * Σ(actual - predicted)
|
||||
///
|
||||
/// Key properties:
|
||||
/// - Can be positive or negative
|
||||
/// - Positive ME indicates under-prediction (actual > predicted)
|
||||
/// - Negative ME indicates over-prediction (actual < predicted)
|
||||
/// - ME = 0 indicates no systematic bias (but not necessarily accurate predictions)
|
||||
/// - Errors can cancel out, hiding large individual errors
|
||||
/// </remarks>
|
||||
[SkipLocalsInit]
|
||||
public sealed class Me : BiInputIndicatorBase
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates ME with specified period.
|
||||
/// </summary>
|
||||
/// <param name="period">Number of values to average (must be > 0)</param>
|
||||
public Me(int period) : base(period, $"Me({period})") { }
|
||||
|
||||
/// <inheritdoc/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected override double ComputeError(double actual, double predicted)
|
||||
{
|
||||
// ME preserves sign: actual - predicted
|
||||
return actual - predicted;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates ME for entire series.
|
||||
/// </summary>
|
||||
public static TSeries Calculate(TSeries actual, TSeries predicted, int period)
|
||||
=> CalculateImpl(actual, predicted, period, Batch);
|
||||
|
||||
/// <summary>
|
||||
/// Batch calculation using signed error computation with rolling mean.
|
||||
/// </summary>
|
||||
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;
|
||||
if (len <= StackAllocThreshold)
|
||||
{
|
||||
Span<double> errors = stackalloc double[len];
|
||||
ErrorHelpers.ComputeSignedErrors(actual, predicted, errors);
|
||||
ErrorHelpers.ApplyRollingMean(errors, output, period);
|
||||
}
|
||||
else
|
||||
{
|
||||
double[] rented = ArrayPool<double>.Shared.Rent(len);
|
||||
try
|
||||
{
|
||||
Span<double> errors = rented.AsSpan(0, len);
|
||||
ErrorHelpers.ComputeSignedErrors(actual, predicted, errors);
|
||||
ErrorHelpers.ApplyRollingMean(errors, output, period);
|
||||
}
|
||||
finally
|
||||
{
|
||||
ArrayPool<double>.Shared.Return(rented);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user