style patterns

This commit is contained in:
Miha Kralj
2026-01-25 16:01:45 -08:00
parent 2836f253c4
commit e59665c8f0
399 changed files with 6892 additions and 1323 deletions
+10 -3
View File
@@ -345,8 +345,15 @@ public class HuberTests
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);
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>(() => Huber.Calculate(actual, predicted, 3));
}
@@ -403,4 +410,4 @@ public class HuberTests
// With delta=5: linear region -> 5*10 - 12.5 = 37.5
Assert.NotEqual(huber1.Last.Value, huber2.Last.Value);
}
}
}
+16 -1
View File
@@ -40,7 +40,9 @@ public sealed class Huber : BiInputIndicatorBase
: base(period, $"Huber({period},{delta:F3})")
{
if (delta <= 0)
{
throw new ArgumentException("Delta must be greater than 0", nameof(delta));
}
Delta = delta;
_negHalfDeltaSquared = -0.5 * delta * delta;
@@ -68,7 +70,9 @@ public sealed class Huber : BiInputIndicatorBase
public static TSeries Calculate(TSeries actual, TSeries predicted, int period, double delta = 1.345)
{
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);
@@ -92,14 +96,25 @@ public sealed class Huber : BiInputIndicatorBase
public static void Batch(ReadOnlySpan<double> actual, ReadOnlySpan<double> predicted, Span<double> output, int period, double delta = 1.345)
{
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 greater than 0", nameof(delta));
}
int len = actual.Length;
if (len == 0) return;
if (len == 0)
{
return;
}
// Pre-compute Huber errors using shared helper
const int StackAllocThreshold = 256;
+8 -2
View File
@@ -48,7 +48,10 @@ public sealed class LogCosh : BiInputIndicatorBase
double absX = Math.Abs(x);
// For large values, use asymptotic approximation to avoid overflow
if (absX > 20.0)
{
return absX - 0.6931471805599453; // log(2)
}
return Math.Log(Math.Cosh(x));
}
@@ -67,7 +70,10 @@ public sealed class LogCosh : BiInputIndicatorBase
ValidateBatchInputs(actual, predicted, output, period);
int len = actual.Length;
if (len == 0) return;
if (len == 0)
{
return;
}
const int StackAllocThreshold = 256;
if (len <= StackAllocThreshold)
@@ -91,4 +97,4 @@ public sealed class LogCosh : BiInputIndicatorBase
}
}
}
}
}
+24 -2
View File
@@ -52,7 +52,9 @@ public sealed class Maape : BiInputIndicatorBase
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);
@@ -76,12 +78,20 @@ public sealed class Maape : BiInputIndicatorBase
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;
if (len == 0)
{
return;
}
// Pre-compute arctangent errors
const int StackAllocThreshold = 256;
@@ -100,7 +110,9 @@ public sealed class Maape : BiInputIndicatorBase
finally
{
if (rented != null)
{
ArrayPool<double>.Shared.Return(rented);
}
}
}
@@ -133,7 +145,9 @@ public sealed class Maape : BiInputIndicatorBase
foundPredicted = true;
}
if (foundActual && foundPredicted)
{
break;
}
}
for (int i = 0; i < len; i++)
@@ -142,18 +156,26 @@ public sealed class Maape : BiInputIndicatorBase
double pred = predicted[i];
if (double.IsFinite(act))
{
lastValidActual = act;
}
else
{
act = lastValidActual;
}
if (double.IsFinite(pred))
{
lastValidPredicted = pred;
}
else
{
pred = lastValidPredicted;
}
double absActual = Math.Abs(act);
double absError = Math.Abs(act - pred);
output[i] = absActual > Epsilon ? Math.Atan(absError / absActual) : Math.PI / 2.0;
}
}
}
}
+10 -3
View File
@@ -321,8 +321,15 @@ public class MaeTests
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);
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));
}
@@ -356,4 +363,4 @@ public class MaeTests
// After resync, result should still be correct
Assert.Equal(10.0, mae.Last.Value, 10);
}
}
}
+5 -2
View File
@@ -60,7 +60,10 @@ public sealed class Mae : BiInputIndicatorBase
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;
if (actual.Length == 0)
{
return;
}
// Allocate temporary buffer for absolute errors
const int StackAllocThreshold = 256;
@@ -86,4 +89,4 @@ public sealed class Mae : BiInputIndicatorBase
}
}
}
}
}
+10 -3
View File
@@ -305,8 +305,15 @@ public class MapdTests
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);
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));
}
@@ -353,4 +360,4 @@ public class MapdTests
var result = mapd.Update(10, 0);
Assert.True(double.IsFinite(result.Value));
}
}
}
+19 -1
View File
@@ -51,7 +51,9 @@ public sealed class Mapd : BiInputIndicatorBase
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);
@@ -75,12 +77,20 @@ public sealed class Mapd : BiInputIndicatorBase
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;
if (len == 0)
{
return;
}
// Pre-compute percentage errors (divided by predicted, not actual)
const int StackAllocThreshold = 256;
@@ -131,14 +141,22 @@ public sealed class Mapd : BiInputIndicatorBase
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
+10 -3
View File
@@ -320,8 +320,15 @@ public class MapeTests
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);
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>(() => Mape.Calculate(actual, predicted, 3));
}
@@ -386,4 +393,4 @@ public class MapeTests
// Over-prediction should have higher MAPE due to smaller denominator
Assert.True(overPrediction.Value > underPrediction.Value);
}
}
}
+5 -2
View File
@@ -52,7 +52,10 @@ public sealed class Mape : BiInputIndicatorBase
ValidateBatchInputs(actual, predicted, output, period);
int len = actual.Length;
if (len == 0) return;
if (len == 0)
{
return;
}
const int StackAllocThreshold = 256;
Span<double> percentErrors = len <= StackAllocThreshold
@@ -62,4 +65,4 @@ public sealed class Mape : BiInputIndicatorBase
ErrorHelpers.ComputePercentageErrors(actual, predicted, percentErrors, Epsilon);
ErrorHelpers.ApplyRollingMean(percentErrors, output, period);
}
}
}
+75 -9
View File
@@ -44,7 +44,9 @@ public sealed class Mase : AbstractBase
public Mase(int period)
{
if (period <= 0)
{
throw new ArgumentException("Period must be greater than 0", nameof(period));
}
_errorBuffer = new RingBuffer(period);
_scaleBuffer = new RingBuffer(period);
@@ -63,14 +65,22 @@ public sealed class Mase : AbstractBase
double predictedVal = predicted.Value;
if (!double.IsFinite(actualVal))
{
actualVal = double.IsFinite(_state.LastValidActual) ? _state.LastValidActual : 0.0;
}
else
{
_state.LastValidActual = actualVal;
}
if (!double.IsFinite(predictedVal))
{
predictedVal = double.IsFinite(_state.LastValidPredicted) ? _state.LastValidPredicted : 0.0;
}
else
{
_state.LastValidPredicted = predictedVal;
}
double absError = Math.Abs(actualVal - predictedVal);
double naiveDiff = double.IsFinite(_state.PrevActual) ? Math.Abs(actualVal - _state.PrevActual) : 0.0;
@@ -124,9 +134,14 @@ public sealed class Mase : AbstractBase
// TickCount is 1-based (incremented after adding), so use >= period+1 for post-warmup
double scale;
if (_state.TickCount > period)
{
scale = _state.ScaleSum / period;
}
else
{
scale = count > 1 ? _state.ScaleSum / (count - 1) : 1.0;
}
double result = scale > 1e-10 ? mae / scale : mae;
Last = new TValue(actual.Time, result);
@@ -167,7 +182,9 @@ public sealed class Mase : AbstractBase
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);
@@ -188,12 +205,20 @@ public sealed class Mase : AbstractBase
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;
if (len == 0)
{
return;
}
const int StackAllocThreshold = 256;
Span<double> errorBuffer = period <= StackAllocThreshold
@@ -211,11 +236,19 @@ public sealed class Mase : AbstractBase
for (int k = 0; k < len; k++)
{
if (double.IsFinite(actual[k])) { lastValidActual = actual[k]; break; }
if (double.IsFinite(actual[k]))
{
lastValidActual = actual[k];
break;
}
}
for (int k = 0; k < len; k++)
{
if (double.IsFinite(predicted[k])) { lastValidPredicted = predicted[k]; break; }
if (double.IsFinite(predicted[k]))
{
lastValidPredicted = predicted[k];
break;
}
}
int bufferIndex = 0;
@@ -227,8 +260,23 @@ public sealed class Mase : AbstractBase
double act = actual[i];
double pred = predicted[i];
if (double.IsFinite(act)) lastValidActual = act; else act = lastValidActual;
if (double.IsFinite(pred)) lastValidPredicted = pred; else pred = lastValidPredicted;
if (double.IsFinite(act))
{
lastValidActual = act;
}
else
{
act = lastValidActual;
}
if (double.IsFinite(pred))
{
lastValidPredicted = pred;
}
else
{
pred = lastValidPredicted;
}
double absError = Math.Abs(act - pred);
double naiveDiff = double.IsFinite(prevActual) ? Math.Abs(act - prevActual) : 0.0;
@@ -251,8 +299,23 @@ public sealed class Mase : AbstractBase
double act = actual[i];
double pred = predicted[i];
if (double.IsFinite(act)) lastValidActual = act; else act = lastValidActual;
if (double.IsFinite(pred)) lastValidPredicted = pred; else pred = lastValidPredicted;
if (double.IsFinite(act))
{
lastValidActual = act;
}
else
{
act = lastValidActual;
}
if (double.IsFinite(pred))
{
lastValidPredicted = pred;
}
else
{
pred = lastValidPredicted;
}
double absError = Math.Abs(act - pred);
double naiveDiff = Math.Abs(act - prevActual);
@@ -263,7 +326,10 @@ public sealed class Mase : AbstractBase
scaleBuffer[bufferIndex] = naiveDiff;
bufferIndex++;
if (bufferIndex >= period) bufferIndex = 0;
if (bufferIndex >= period)
{
bufferIndex = 0;
}
double mae = errorSum / period;
double scale = scaleSum / period;
@@ -286,4 +352,4 @@ public sealed class Mase : AbstractBase
}
}
}
}
}
+89 -15
View File
@@ -35,7 +35,9 @@ public sealed class Mdae : AbstractBase
public Mdae(int period)
{
if (period <= 0)
{
throw new ArgumentException("Period must be greater than 0", nameof(period));
}
_buffer = new RingBuffer(period);
_sortBuffer = new double[period];
@@ -62,14 +64,22 @@ public sealed class Mdae : AbstractBase
}
if (!double.IsFinite(actualVal))
{
actualVal = double.IsFinite(_state.LastValidActual) ? _state.LastValidActual : 0.0;
}
else
{
_state.LastValidActual = actualVal;
}
if (!double.IsFinite(predictedVal))
{
predictedVal = double.IsFinite(_state.LastValidPredicted) ? _state.LastValidPredicted : 0.0;
}
else
{
_state.LastValidPredicted = predictedVal;
}
double absError = Math.Abs(actualVal - predictedVal);
@@ -124,7 +134,10 @@ public sealed class Mdae : AbstractBase
private double CalculateMedian()
{
int count = _buffer.Count;
if (count == 0) return 0.0;
if (count == 0)
{
return 0.0;
}
// Copy buffer contents to sort buffer using GetSequencedSpans to handle wraparound
_buffer.GetSequencedSpans(out var first, out var second);
@@ -151,7 +164,9 @@ public sealed class Mdae : AbstractBase
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);
@@ -172,12 +187,20 @@ public sealed class Mdae : AbstractBase
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;
if (len == 0)
{
return;
}
// Use stackalloc for small periods, heap for larger
scoped Span<double> buffer;
@@ -199,11 +222,19 @@ public sealed class Mdae : AbstractBase
for (int k = 0; k < len; k++)
{
if (double.IsFinite(actual[k])) { lastValidActual = actual[k]; break; }
if (double.IsFinite(actual[k]))
{
lastValidActual = actual[k];
break;
}
}
for (int k = 0; k < len; k++)
{
if (double.IsFinite(predicted[k])) { lastValidPredicted = predicted[k]; break; }
if (double.IsFinite(predicted[k]))
{
lastValidPredicted = predicted[k];
break;
}
}
int bufferIndex = 0;
@@ -214,16 +245,38 @@ public sealed class Mdae : AbstractBase
double act = actual[i];
double pred = predicted[i];
if (double.IsFinite(act)) lastValidActual = act; else act = lastValidActual;
if (double.IsFinite(pred)) lastValidPredicted = pred; else pred = lastValidPredicted;
if (double.IsFinite(act))
{
lastValidActual = act;
}
else
{
act = lastValidActual;
}
if (double.IsFinite(pred))
{
lastValidPredicted = pred;
}
else
{
pred = lastValidPredicted;
}
double absError = Math.Abs(act - pred);
// Add to circular buffer
buffer[bufferIndex] = absError;
bufferIndex++;
if (bufferIndex >= period) bufferIndex = 0;
if (bufferCount < period) bufferCount++;
if (bufferIndex >= period)
{
bufferIndex = 0;
}
if (bufferCount < period)
{
bufferCount++;
}
// Copy and use QuickSelect for median
buffer.Slice(0, bufferCount).CopyTo(sortBuffer);
@@ -277,9 +330,20 @@ public sealed class Mdae : AbstractBase
// Median-of-three pivot selection for better pivot choice
int mid = left + (right - left) / 2;
if (span[mid] < span[left]) (span[left], span[mid]) = (span[mid], span[left]);
if (span[right] < span[left]) (span[left], span[right]) = (span[right], span[left]);
if (span[right] < span[mid]) (span[mid], span[right]) = (span[right], span[mid]);
if (span[mid] < span[left])
{
(span[left], span[mid]) = (span[mid], span[left]);
}
if (span[right] < span[left])
{
(span[left], span[right]) = (span[right], span[left]);
}
if (span[right] < span[mid])
{
(span[mid], span[right]) = (span[right], span[mid]);
}
// Use median as pivot, move to right-1 position
double pivot = span[mid];
@@ -297,11 +361,21 @@ public sealed class Mdae : AbstractBase
}
(span[storeIndex], span[right - 1]) = (span[right - 1], span[storeIndex]);
if (k == storeIndex) return span[storeIndex];
if (k < storeIndex) right = storeIndex - 1;
else left = storeIndex + 1;
if (k == storeIndex)
{
return span[storeIndex];
}
if (k < storeIndex)
{
right = storeIndex - 1;
}
else
{
left = storeIndex + 1;
}
}
return span[left];
}
}
}
+57 -8
View File
@@ -33,7 +33,9 @@ public sealed class Mdape : AbstractBase
public Mdape(int period)
{
if (period <= 0)
{
throw new ArgumentException("Period must be greater than 0", nameof(period));
}
_buffer = new RingBuffer(period);
_sortBuffer = new double[period];
@@ -73,14 +75,22 @@ public sealed class Mdape : AbstractBase
private TValue UpdateCore(DateTime time, double actualVal, double predictedVal, bool isNew)
{
if (!double.IsFinite(actualVal))
{
actualVal = double.IsFinite(_state.LastValidActual) ? _state.LastValidActual : 1.0;
}
else
{
_state.LastValidActual = actualVal;
}
if (!double.IsFinite(predictedVal))
{
predictedVal = double.IsFinite(_state.LastValidPredicted) ? _state.LastValidPredicted : 0.0;
}
else
{
_state.LastValidPredicted = predictedVal;
}
// Calculate absolute percentage error
double absActual = Math.Abs(actualVal);
@@ -124,7 +134,10 @@ public sealed class Mdape : AbstractBase
private double CalculateMedian()
{
int count = _buffer.Count;
if (count == 0) return 0.0;
if (count == 0)
{
return 0.0;
}
// Copy buffer contents to sort buffer using GetSequencedSpans to handle wraparound
_buffer.GetSequencedSpans(out var first, out var second);
@@ -151,7 +164,9 @@ public sealed class Mdape : AbstractBase
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);
@@ -172,12 +187,20 @@ public sealed class Mdape : AbstractBase
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;
if (len == 0)
{
return;
}
// Use dual-heap sliding median for O(log n) updates instead of O(n log n) sort per element
var slidingMedian = new SlidingMedianHeap(period);
@@ -187,11 +210,19 @@ public sealed class Mdape : AbstractBase
for (int k = 0; k < len; k++)
{
if (double.IsFinite(actual[k]) && Math.Abs(actual[k]) >= 1e-10) { lastValidActual = actual[k]; break; }
if (double.IsFinite(actual[k]) && Math.Abs(actual[k]) >= 1e-10)
{
lastValidActual = actual[k];
break;
}
}
for (int k = 0; k < len; k++)
{
if (double.IsFinite(predicted[k])) { lastValidPredicted = predicted[k]; break; }
if (double.IsFinite(predicted[k]))
{
lastValidPredicted = predicted[k];
break;
}
}
for (int i = 0; i < len; i++)
@@ -199,8 +230,23 @@ public sealed class Mdape : AbstractBase
double act = actual[i];
double pred = predicted[i];
if (double.IsFinite(act) && Math.Abs(act) >= 1e-10) lastValidActual = act; else act = lastValidActual;
if (double.IsFinite(pred)) lastValidPredicted = pred; else pred = lastValidPredicted;
if (double.IsFinite(act) && Math.Abs(act) >= 1e-10)
{
lastValidActual = act;
}
else
{
act = lastValidActual;
}
if (double.IsFinite(pred))
{
lastValidPredicted = pred;
}
else
{
pred = lastValidPredicted;
}
double absActual = Math.Abs(act);
double absError = Math.Abs(act - pred);
@@ -255,7 +301,10 @@ public sealed class Mdape : AbstractBase
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double GetMedian()
{
if (_lowerCount == 0 && _upperCount == 0) return 0.0;
if (_lowerCount == 0 && _upperCount == 0)
{
return 0.0;
}
if (_lowerCount > _upperCount)
{
@@ -353,4 +402,4 @@ public sealed class Mdape : AbstractBase
}
}
}
}
}
+10 -3
View File
@@ -347,8 +347,15 @@ public class MeTests
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);
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>(() => Me.Calculate(actual, predicted, 3));
}
@@ -382,4 +389,4 @@ public class MeTests
// After resync, result should still be correct
Assert.Equal(10.0, me.Last.Value, 10);
}
}
}
+5 -2
View File
@@ -51,7 +51,10 @@ public sealed class Me : BiInputIndicatorBase
ValidateBatchInputs(actual, predicted, output, period);
int len = actual.Length;
if (len == 0) return;
if (len == 0)
{
return;
}
const int StackAllocThreshold = 256;
if (len <= StackAllocThreshold)
@@ -75,4 +78,4 @@ public sealed class Me : BiInputIndicatorBase
}
}
}
}
}
+37 -6
View File
@@ -64,7 +64,10 @@ public sealed class Mpe : BiInputIndicatorBase
ValidateBatchInputs(actual, predicted, output, period);
int len = actual.Length;
if (len == 0) return;
if (len == 0)
{
return;
}
const int StackAllocThreshold = 256;
Span<double> errors = len <= StackAllocThreshold
@@ -82,17 +85,45 @@ public sealed class Mpe : BiInputIndicatorBase
double lastValidActual = 1.0, lastValidPredicted = 0;
for (int i = 0; i < len; i++)
if (double.IsFinite(actual[i]) && Math.Abs(actual[i]) >= Epsilon) { lastValidActual = actual[i]; break; }
{
if (double.IsFinite(actual[i]) && Math.Abs(actual[i]) >= Epsilon)
{
lastValidActual = actual[i];
break;
}
}
for (int i = 0; i < len; i++)
if (double.IsFinite(predicted[i])) { lastValidPredicted = predicted[i]; break; }
{
if (double.IsFinite(predicted[i]))
{
lastValidPredicted = predicted[i];
break;
}
}
for (int i = 0; i < len; i++)
{
double act = actual[i];
double pred = predicted[i];
if (double.IsFinite(act) && Math.Abs(act) >= Epsilon) lastValidActual = act; else act = lastValidActual;
if (double.IsFinite(pred)) lastValidPredicted = pred; else pred = lastValidPredicted;
if (double.IsFinite(act) && Math.Abs(act) >= Epsilon)
{
lastValidActual = act;
}
else
{
act = lastValidActual;
}
if (double.IsFinite(pred))
{
lastValidPredicted = pred;
}
else
{
pred = lastValidPredicted;
}
// Use signed epsilon to preserve the original sign when actual is near zero
double divisor;
@@ -108,4 +139,4 @@ public sealed class Mpe : BiInputIndicatorBase
output[i] = 100.0 * (act - pred) / divisor;
}
}
}
}
+10 -3
View File
@@ -296,8 +296,15 @@ public class MraeTests
var actual = new TSeries();
var predicted = new TSeries();
for (int i = 1; i <= 10; i++) actual.Add(DateTime.UtcNow, i * 10);
for (int i = 1; i <= 5; i++) predicted.Add(DateTime.UtcNow, i * 10);
for (int i = 1; i <= 10; i++)
{
actual.Add(DateTime.UtcNow, i * 10);
}
for (int i = 1; i <= 5; i++)
{
predicted.Add(DateTime.UtcNow, i * 10);
}
Assert.Throws<ArgumentException>(() => Mrae.Calculate(actual, predicted, 3));
}
@@ -330,4 +337,4 @@ public class MraeTests
Assert.Equal(0.1, mrae.Last.Value, 10);
}
}
}
+20 -2
View File
@@ -52,7 +52,9 @@ public sealed class Mrae : BiInputIndicatorBase
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);
@@ -75,12 +77,20 @@ public sealed class Mrae : BiInputIndicatorBase
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;
if (len == 0)
{
return;
}
// Pre-compute relative errors (same as percentage errors but without *100)
const int StackAllocThreshold = 256;
@@ -130,14 +140,22 @@ public sealed class Mrae : BiInputIndicatorBase
double pred = predicted[i];
if (double.IsFinite(act) && Math.Abs(act) >= Epsilon)
{
lastValidActual = act;
}
else
{
act = lastValidActual;
}
if (double.IsFinite(pred))
{
lastValidPredicted = pred;
}
else
{
pred = lastValidPredicted;
}
double absActual = Math.Abs(act);
output[i] = absActual > Epsilon
@@ -145,4 +163,4 @@ public sealed class Mrae : BiInputIndicatorBase
: 0.0;
}
}
}
}
+1 -1
View File
@@ -308,4 +308,4 @@ public class MseTests
Assert.True(double.IsFinite(val), $"Expected finite value but got {val}");
}
}
}
}
+4 -1
View File
@@ -61,7 +61,10 @@ public sealed class Mse : BiInputIndicatorBase
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;
if (actual.Length == 0)
{
return;
}
// Allocate temporary buffer for squared errors
const int StackAllocThreshold = 256;
+36 -5
View File
@@ -58,7 +58,10 @@ public sealed class Msle : BiInputIndicatorBase
ValidateBatchInputs(actual, predicted, output, period);
int len = actual.Length;
if (len == 0) return;
if (len == 0)
{
return;
}
const int StackAllocThreshold = 256;
Span<double> errors = len <= StackAllocThreshold
@@ -77,9 +80,22 @@ public sealed class Msle : BiInputIndicatorBase
// 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; }
{
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; }
{
if (double.IsFinite(predicted[i]) && predicted[i] >= 0)
{
lastValidPredicted = predicted[i];
break;
}
}
for (int i = 0; i < len; i++)
{
@@ -87,8 +103,23 @@ public sealed class Msle : BiInputIndicatorBase
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;
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);
+17 -2
View File
@@ -41,7 +41,9 @@ public sealed class PseudoHuber : BiInputIndicatorBase
: base(period, $"PseudoHuber({period},{delta:F3})")
{
if (delta <= 0)
{
throw new ArgumentException("Delta must be positive", nameof(delta));
}
Delta = delta;
_deltaSquared = delta * delta;
@@ -65,7 +67,9 @@ public sealed class PseudoHuber : BiInputIndicatorBase
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);
@@ -89,14 +93,25 @@ public sealed class PseudoHuber : BiInputIndicatorBase
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;
if (len == 0)
{
return;
}
// Pre-compute Pseudo-Huber errors using shared helper
const int StackAllocThreshold = 256;
@@ -109,4 +124,4 @@ public sealed class PseudoHuber : BiInputIndicatorBase
// Apply rolling mean
ErrorHelpers.ApplyRollingMean(errors, output, period);
}
}
}
+68 -9
View File
@@ -38,7 +38,9 @@ public sealed class QuantileLoss : BiInputIndicatorBase
: base(period, $"QuantileLoss({period},{quantile:F2})")
{
if (quantile <= 0.0 || quantile >= 1.0)
{
throw new ArgumentException("Quantile must be between 0 and 1 (exclusive)", nameof(quantile));
}
Quantile = quantile;
}
@@ -61,7 +63,9 @@ public sealed class QuantileLoss : BiInputIndicatorBase
public static TSeries Calculate(TSeries actual, TSeries predicted, int period, double quantile = 0.5)
{
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);
@@ -82,14 +86,25 @@ public sealed class QuantileLoss : BiInputIndicatorBase
public static void Batch(ReadOnlySpan<double> actual, ReadOnlySpan<double> predicted, Span<double> output, int period, double quantile = 0.5)
{
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 (quantile <= 0.0 || quantile >= 1.0)
{
throw new ArgumentException("Quantile must be between 0 and 1 (exclusive)", nameof(quantile));
}
int len = actual.Length;
if (len == 0) return;
if (len == 0)
{
return;
}
const int StackAllocThreshold = 256;
Span<double> lossBuffer = period <= StackAllocThreshold
@@ -102,11 +117,19 @@ public sealed class QuantileLoss : BiInputIndicatorBase
for (int k = 0; k < len; k++)
{
if (double.IsFinite(actual[k])) { lastValidActual = actual[k]; break; }
if (double.IsFinite(actual[k]))
{
lastValidActual = actual[k];
break;
}
}
for (int k = 0; k < len; k++)
{
if (double.IsFinite(predicted[k])) { lastValidPredicted = predicted[k]; break; }
if (double.IsFinite(predicted[k]))
{
lastValidPredicted = predicted[k];
break;
}
}
int bufferIndex = 0;
@@ -118,8 +141,23 @@ public sealed class QuantileLoss : BiInputIndicatorBase
double act = actual[i];
double pred = predicted[i];
if (double.IsFinite(act)) lastValidActual = act; else act = lastValidActual;
if (double.IsFinite(pred)) lastValidPredicted = pred; else pred = lastValidPredicted;
if (double.IsFinite(act))
{
lastValidActual = act;
}
else
{
act = lastValidActual;
}
if (double.IsFinite(pred))
{
lastValidPredicted = pred;
}
else
{
pred = lastValidPredicted;
}
double diff = act - pred;
double loss = diff >= 0 ? quantile * diff : (quantile - 1.0) * diff;
@@ -136,8 +174,23 @@ public sealed class QuantileLoss : BiInputIndicatorBase
double act = actual[i];
double pred = predicted[i];
if (double.IsFinite(act)) lastValidActual = act; else act = lastValidActual;
if (double.IsFinite(pred)) lastValidPredicted = pred; else pred = lastValidPredicted;
if (double.IsFinite(act))
{
lastValidActual = act;
}
else
{
act = lastValidActual;
}
if (double.IsFinite(pred))
{
lastValidPredicted = pred;
}
else
{
pred = lastValidPredicted;
}
double diff = act - pred;
double loss = diff >= 0 ? quantile * diff : (quantile - 1.0) * diff;
@@ -146,7 +199,10 @@ public sealed class QuantileLoss : BiInputIndicatorBase
lossBuffer[bufferIndex] = loss;
bufferIndex++;
if (bufferIndex >= period) bufferIndex = 0;
if (bufferIndex >= period)
{
bufferIndex = 0;
}
output[i] = lossSum / period;
@@ -156,9 +212,12 @@ public sealed class QuantileLoss : BiInputIndicatorBase
tickCount = 0;
double recalcSum = 0;
for (int k = 0; k < period; k++)
{
recalcSum += lossBuffer[k];
}
lossSum = recalcSum;
}
}
}
}
}
+69 -8
View File
@@ -43,7 +43,9 @@ public sealed class Rae : AbstractBase
public Rae(int period)
{
if (period <= 0)
{
throw new ArgumentException("Period must be greater than 0", nameof(period));
}
_actualBuffer = new RingBuffer(period);
_absErrorBuffer = new RingBuffer(period);
@@ -61,14 +63,22 @@ public sealed class Rae : AbstractBase
double predictedVal = predicted.Value;
if (!double.IsFinite(actualVal))
{
actualVal = double.IsFinite(_state.LastValidActual) ? _state.LastValidActual : 0.0;
}
else
{
_state.LastValidActual = actualVal;
}
if (!double.IsFinite(predictedVal))
{
predictedVal = double.IsFinite(_state.LastValidPredicted) ? _state.LastValidPredicted : 0.0;
}
else
{
_state.LastValidPredicted = predictedVal;
}
if (isNew)
{
@@ -163,7 +173,9 @@ public sealed class Rae : AbstractBase
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);
@@ -184,12 +196,20 @@ public sealed class Rae : AbstractBase
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;
if (len == 0)
{
return;
}
const int StackAllocThreshold = 256;
Span<double> actualBuffer = period <= StackAllocThreshold
@@ -210,11 +230,19 @@ public sealed class Rae : AbstractBase
for (int k = 0; k < len; k++)
{
if (double.IsFinite(actual[k])) { lastValidActual = actual[k]; break; }
if (double.IsFinite(actual[k]))
{
lastValidActual = actual[k];
break;
}
}
for (int k = 0; k < len; k++)
{
if (double.IsFinite(predicted[k])) { lastValidPredicted = predicted[k]; break; }
if (double.IsFinite(predicted[k]))
{
lastValidPredicted = predicted[k];
break;
}
}
int bufferIndex = 0;
@@ -226,8 +254,23 @@ public sealed class Rae : AbstractBase
double act = actual[i];
double pred = predicted[i];
if (double.IsFinite(act)) lastValidActual = act; else act = lastValidActual;
if (double.IsFinite(pred)) lastValidPredicted = pred; else pred = lastValidPredicted;
if (double.IsFinite(act))
{
lastValidActual = act;
}
else
{
act = lastValidActual;
}
if (double.IsFinite(pred))
{
lastValidPredicted = pred;
}
else
{
pred = lastValidPredicted;
}
actualSum += act;
actualBuffer[i] = act;
@@ -250,8 +293,23 @@ public sealed class Rae : AbstractBase
double act = actual[i];
double pred = predicted[i];
if (double.IsFinite(act)) lastValidActual = act; else act = lastValidActual;
if (double.IsFinite(pred)) lastValidPredicted = pred; else pred = lastValidPredicted;
if (double.IsFinite(act))
{
lastValidActual = act;
}
else
{
act = lastValidActual;
}
if (double.IsFinite(pred))
{
lastValidPredicted = pred;
}
else
{
pred = lastValidPredicted;
}
actualSum = actualSum - actualBuffer[bufferIndex] + act;
actualBuffer[bufferIndex] = act;
@@ -266,7 +324,10 @@ public sealed class Rae : AbstractBase
absBaselineBuffer[bufferIndex] = absBaseline;
bufferIndex++;
if (bufferIndex >= period) bufferIndex = 0;
if (bufferIndex >= period)
{
bufferIndex = 0;
}
output[i] = absBaselineSum > 1e-10 ? absErrorSum / absBaselineSum : 1.0;
+1 -1
View File
@@ -247,4 +247,4 @@ public class RmseTests
// All errors are 5, MSE = 25, RMSE = 5
Assert.Equal(5.0, results.Last.Value, 10);
}
}
}
+4 -1
View File
@@ -57,7 +57,10 @@ public sealed class Rmse : BiInputIndicatorBase
ValidateBatchInputs(actual, predicted, output, period);
int len = actual.Length;
if (len == 0) return;
if (len == 0)
{
return;
}
const int StackAllocThreshold = 256;
Span<double> sqErrors = len <= StackAllocThreshold
+36 -5
View File
@@ -61,7 +61,10 @@ public sealed class Rmsle : BiInputIndicatorBase
ValidateBatchInputs(actual, predicted, output, period);
int len = actual.Length;
if (len == 0) return;
if (len == 0)
{
return;
}
const int StackAllocThreshold = 256;
Span<double> errors = len <= StackAllocThreshold
@@ -80,9 +83,22 @@ public sealed class Rmsle : BiInputIndicatorBase
// 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; }
{
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; }
{
if (double.IsFinite(predicted[i]) && predicted[i] >= 0)
{
lastValidPredicted = predicted[i];
break;
}
}
for (int i = 0; i < len; i++)
{
@@ -90,8 +106,23 @@ public sealed class Rmsle : BiInputIndicatorBase
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;
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);
+70 -9
View File
@@ -43,7 +43,9 @@ public sealed class Rse : AbstractBase
public Rse(int period)
{
if (period <= 0)
{
throw new ArgumentException("Period must be greater than 0", nameof(period));
}
_actualBuffer = new RingBuffer(period);
_sqErrorBuffer = new RingBuffer(period);
@@ -67,14 +69,22 @@ public sealed class Rse : AbstractBase
}
if (!double.IsFinite(actualVal))
{
actualVal = double.IsFinite(_state.LastValidActual) ? _state.LastValidActual : 0.0;
}
else
{
_state.LastValidActual = actualVal;
}
if (!double.IsFinite(predictedVal))
{
predictedVal = double.IsFinite(_state.LastValidPredicted) ? _state.LastValidPredicted : 0.0;
}
else
{
_state.LastValidPredicted = predictedVal;
}
if (isNew)
{
@@ -177,7 +187,9 @@ public sealed class Rse : AbstractBase
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);
@@ -198,12 +210,20 @@ public sealed class Rse : AbstractBase
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;
if (len == 0)
{
return;
}
const int StackAllocThreshold = 256;
Span<double> actualBuffer = period <= StackAllocThreshold
@@ -224,11 +244,19 @@ public sealed class Rse : AbstractBase
for (int k = 0; k < len; k++)
{
if (double.IsFinite(actual[k])) { lastValidActual = actual[k]; break; }
if (double.IsFinite(actual[k]))
{
lastValidActual = actual[k];
break;
}
}
for (int k = 0; k < len; k++)
{
if (double.IsFinite(predicted[k])) { lastValidPredicted = predicted[k]; break; }
if (double.IsFinite(predicted[k]))
{
lastValidPredicted = predicted[k];
break;
}
}
int bufferIndex = 0;
@@ -240,8 +268,23 @@ public sealed class Rse : AbstractBase
double act = actual[i];
double pred = predicted[i];
if (double.IsFinite(act)) lastValidActual = act; else act = lastValidActual;
if (double.IsFinite(pred)) lastValidPredicted = pred; else pred = lastValidPredicted;
if (double.IsFinite(act))
{
lastValidActual = act;
}
else
{
act = lastValidActual;
}
if (double.IsFinite(pred))
{
lastValidPredicted = pred;
}
else
{
pred = lastValidPredicted;
}
actualSum += act;
actualBuffer[i] = act;
@@ -266,8 +309,23 @@ public sealed class Rse : AbstractBase
double act = actual[i];
double pred = predicted[i];
if (double.IsFinite(act)) lastValidActual = act; else act = lastValidActual;
if (double.IsFinite(pred)) lastValidPredicted = pred; else pred = lastValidPredicted;
if (double.IsFinite(act))
{
lastValidActual = act;
}
else
{
act = lastValidActual;
}
if (double.IsFinite(pred))
{
lastValidPredicted = pred;
}
else
{
pred = lastValidPredicted;
}
actualSum = actualSum - actualBuffer[bufferIndex] + act;
actualBuffer[bufferIndex] = act;
@@ -284,7 +342,10 @@ public sealed class Rse : AbstractBase
sqBaselineBuffer[bufferIndex] = sqBaseline;
bufferIndex++;
if (bufferIndex >= period) bufferIndex = 0;
if (bufferIndex >= period)
{
bufferIndex = 0;
}
output[i] = sqBaselineSum > 1e-10 ? sqErrorSum / sqBaselineSum : 1.0;
@@ -305,4 +366,4 @@ public sealed class Rse : AbstractBase
}
}
}
}
}
+69 -8
View File
@@ -44,7 +44,9 @@ public sealed class Rsquared : AbstractBase
public Rsquared(int period)
{
if (period <= 0)
{
throw new ArgumentException("Period must be greater than 0", nameof(period));
}
_actualBuffer = new RingBuffer(period);
_sqResidualBuffer = new RingBuffer(period);
@@ -62,14 +64,22 @@ public sealed class Rsquared : AbstractBase
double predictedVal = predicted.Value;
if (!double.IsFinite(actualVal))
{
actualVal = double.IsFinite(_state.LastValidActual) ? _state.LastValidActual : 0.0;
}
else
{
_state.LastValidActual = actualVal;
}
if (!double.IsFinite(predictedVal))
{
predictedVal = double.IsFinite(_state.LastValidPredicted) ? _state.LastValidPredicted : 0.0;
}
else
{
_state.LastValidPredicted = predictedVal;
}
if (isNew)
{
@@ -171,7 +181,9 @@ public sealed class Rsquared : AbstractBase
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);
@@ -192,12 +204,20 @@ public sealed class Rsquared : AbstractBase
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;
if (len == 0)
{
return;
}
const int StackAllocThreshold = 256;
Span<double> actualBuffer = period <= StackAllocThreshold
@@ -218,11 +238,19 @@ public sealed class Rsquared : AbstractBase
for (int k = 0; k < len; k++)
{
if (double.IsFinite(actual[k])) { lastValidActual = actual[k]; break; }
if (double.IsFinite(actual[k]))
{
lastValidActual = actual[k];
break;
}
}
for (int k = 0; k < len; k++)
{
if (double.IsFinite(predicted[k])) { lastValidPredicted = predicted[k]; break; }
if (double.IsFinite(predicted[k]))
{
lastValidPredicted = predicted[k];
break;
}
}
int bufferIndex = 0;
@@ -234,8 +262,23 @@ public sealed class Rsquared : AbstractBase
double act = actual[i];
double pred = predicted[i];
if (double.IsFinite(act)) lastValidActual = act; else act = lastValidActual;
if (double.IsFinite(pred)) lastValidPredicted = pred; else pred = lastValidPredicted;
if (double.IsFinite(act))
{
lastValidActual = act;
}
else
{
act = lastValidActual;
}
if (double.IsFinite(pred))
{
lastValidPredicted = pred;
}
else
{
pred = lastValidPredicted;
}
actualSum += act;
actualBuffer[i] = act;
@@ -260,8 +303,23 @@ public sealed class Rsquared : AbstractBase
double act = actual[i];
double pred = predicted[i];
if (double.IsFinite(act)) lastValidActual = act; else act = lastValidActual;
if (double.IsFinite(pred)) lastValidPredicted = pred; else pred = lastValidPredicted;
if (double.IsFinite(act))
{
lastValidActual = act;
}
else
{
act = lastValidActual;
}
if (double.IsFinite(pred))
{
lastValidPredicted = pred;
}
else
{
pred = lastValidPredicted;
}
actualSum = actualSum - actualBuffer[bufferIndex] + act;
actualBuffer[bufferIndex] = act;
@@ -278,7 +336,10 @@ public sealed class Rsquared : AbstractBase
sqTotalBuffer[bufferIndex] = sqTotal;
bufferIndex++;
if (bufferIndex >= period) bufferIndex = 0;
if (bufferIndex >= period)
{
bufferIndex = 0;
}
output[i] = sqTotalSum > 1e-10 ? 1.0 - (sqResidualSum / sqTotalSum) : 1.0;
+36 -5
View File
@@ -55,7 +55,10 @@ public sealed class Smape : BiInputIndicatorBase
ValidateBatchInputs(actual, predicted, output, period);
int len = actual.Length;
if (len == 0) return;
if (len == 0)
{
return;
}
const int StackAllocThreshold = 256;
Span<double> symErrors = len <= StackAllocThreshold
@@ -74,17 +77,45 @@ public sealed class Smape : BiInputIndicatorBase
double lastValidActual = 0, lastValidPredicted = 0;
for (int i = 0; i < len; i++)
if (double.IsFinite(actual[i])) { lastValidActual = actual[i]; break; }
{
if (double.IsFinite(actual[i]))
{
lastValidActual = actual[i];
break;
}
}
for (int i = 0; i < len; i++)
if (double.IsFinite(predicted[i])) { lastValidPredicted = predicted[i]; break; }
{
if (double.IsFinite(predicted[i]))
{
lastValidPredicted = predicted[i];
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)) lastValidPredicted = pred; else pred = lastValidPredicted;
if (double.IsFinite(act))
{
lastValidActual = act;
}
else
{
act = lastValidActual;
}
if (double.IsFinite(pred))
{
lastValidPredicted = pred;
}
else
{
pred = lastValidPredicted;
}
double absDiff = Math.Abs(act - pred);
double sumAbs = Math.Abs(act) + Math.Abs(pred);
+70 -9
View File
@@ -39,7 +39,9 @@ public sealed class TheilU : AbstractBase
public TheilU(int period)
{
if (period <= 0)
{
throw new ArgumentException("Period must be greater than 0", nameof(period));
}
_sqErrorBuffer = new RingBuffer(period);
_sqActualBuffer = new RingBuffer(period);
@@ -80,14 +82,22 @@ public sealed class TheilU : AbstractBase
private TValue UpdateCore(DateTime time, double actualVal, double predictedVal, bool isNew)
{
if (!double.IsFinite(actualVal))
{
actualVal = double.IsFinite(_state.LastValidActual) ? _state.LastValidActual : 0.0;
}
else
{
_state.LastValidActual = actualVal;
}
if (!double.IsFinite(predictedVal))
{
predictedVal = double.IsFinite(_state.LastValidPredicted) ? _state.LastValidPredicted : 0.0;
}
else
{
_state.LastValidPredicted = predictedVal;
}
double error = predictedVal - actualVal;
double sqError = error * error;
@@ -163,7 +173,9 @@ public sealed class TheilU : AbstractBase
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);
@@ -184,12 +196,20 @@ public sealed class TheilU : AbstractBase
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;
if (len == 0)
{
return;
}
const int StackAllocThreshold = 256;
Span<double> sqErrorBuffer = period <= StackAllocThreshold
@@ -210,11 +230,19 @@ public sealed class TheilU : AbstractBase
for (int k = 0; k < len; k++)
{
if (double.IsFinite(actual[k])) { lastValidActual = actual[k]; break; }
if (double.IsFinite(actual[k]))
{
lastValidActual = actual[k];
break;
}
}
for (int k = 0; k < len; k++)
{
if (double.IsFinite(predicted[k])) { lastValidPredicted = predicted[k]; break; }
if (double.IsFinite(predicted[k]))
{
lastValidPredicted = predicted[k];
break;
}
}
int bufferIndex = 0;
@@ -226,8 +254,23 @@ public sealed class TheilU : AbstractBase
double act = actual[i];
double pred = predicted[i];
if (double.IsFinite(act)) lastValidActual = act; else act = lastValidActual;
if (double.IsFinite(pred)) lastValidPredicted = pred; else pred = lastValidPredicted;
if (double.IsFinite(act))
{
lastValidActual = act;
}
else
{
act = lastValidActual;
}
if (double.IsFinite(pred))
{
lastValidPredicted = pred;
}
else
{
pred = lastValidPredicted;
}
double error = pred - act;
double sqError = error * error;
@@ -251,8 +294,23 @@ public sealed class TheilU : AbstractBase
double act = actual[i];
double pred = predicted[i];
if (double.IsFinite(act)) lastValidActual = act; else act = lastValidActual;
if (double.IsFinite(pred)) lastValidPredicted = pred; else pred = lastValidPredicted;
if (double.IsFinite(act))
{
lastValidActual = act;
}
else
{
act = lastValidActual;
}
if (double.IsFinite(pred))
{
lastValidPredicted = pred;
}
else
{
pred = lastValidPredicted;
}
double error = pred - act;
double sqError = error * error;
@@ -269,7 +327,10 @@ public sealed class TheilU : AbstractBase
sqPredBuffer[bufferIndex] = sqPred;
bufferIndex++;
if (bufferIndex >= period) bufferIndex = 0;
if (bufferIndex >= period)
{
bufferIndex = 0;
}
double denom = Math.Sqrt(sqActualSum + sqPredSum);
output[i] = denom > 1e-10 ? Math.Sqrt(sqErrorSum) / denom : 0.0;
@@ -291,4 +352,4 @@ public sealed class TheilU : AbstractBase
}
}
}
}
}
+19 -2
View File
@@ -35,7 +35,9 @@ public sealed class TukeyBiweight : BiInputIndicatorBase
: base(period, $"TukeyBiweight({period},{c:F3})")
{
if (c <= 0)
{
throw new ArgumentException("Threshold c must be positive", nameof(c));
}
C = c;
_cSquaredOver6 = (c * c) / 6.0;
@@ -53,7 +55,9 @@ public sealed class TukeyBiweight : BiInputIndicatorBase
double absError = Math.Abs(error);
if (absError > C)
{
return _cSquaredOver6;
}
double ratio = error / C;
double ratioSq = ratio * ratio;
@@ -65,7 +69,9 @@ public sealed class TukeyBiweight : BiInputIndicatorBase
public static TSeries Calculate(TSeries actual, TSeries predicted, int period, double c = DefaultC)
{
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);
@@ -86,14 +92,25 @@ public sealed class TukeyBiweight : BiInputIndicatorBase
public static void Batch(ReadOnlySpan<double> actual, ReadOnlySpan<double> predicted, Span<double> output, int period, double c = DefaultC)
{
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 (c <= 0)
{
throw new ArgumentException("Threshold c must be positive", nameof(c));
}
int len = actual.Length;
if (len == 0) return;
if (len == 0)
{
return;
}
// Rent buffer for intermediate Tukey biweight errors
double[] rented = ArrayPool<double>.Shared.Rent(len);
@@ -112,4 +129,4 @@ public sealed class TukeyBiweight : BiInputIndicatorBase
ArrayPool<double>.Shared.Return(rented, clearArray: false);
}
}
}
}
+77 -11
View File
@@ -38,7 +38,9 @@ public sealed class Wmape : AbstractBase
public Wmape(int period)
{
if (period <= 0)
{
throw new ArgumentException("Period must be greater than 0", nameof(period));
}
_absErrorBuffer = new RingBuffer(period);
_absActualBuffer = new RingBuffer(period);
@@ -65,14 +67,22 @@ public sealed class Wmape : AbstractBase
}
if (!double.IsFinite(actualVal))
{
actualVal = double.IsFinite(_state.LastValidActual) ? _state.LastValidActual : 0.0;
}
else
{
_state.LastValidActual = actualVal;
}
if (!double.IsFinite(predictedVal))
{
predictedVal = double.IsFinite(_state.LastValidPredicted) ? _state.LastValidPredicted : 0.0;
}
else
{
_state.LastValidPredicted = predictedVal;
}
double absError = Math.Abs(actualVal - predictedVal);
double absActual = Math.Abs(actualVal);
@@ -148,7 +158,9 @@ public sealed class Wmape : AbstractBase
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);
@@ -169,12 +181,20 @@ public sealed class Wmape : AbstractBase
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;
if (len == 0)
{
return;
}
// Use stackalloc for small periods, ArrayPool for larger
scoped Span<double> absErrorBuffer;
@@ -202,14 +222,22 @@ public sealed class Wmape : AbstractBase
double lastValidActual = 0;
double lastValidPredicted = 0;
for (int k = 0; k < len; k++)
for (int k = 0; k < len; k++)
{
if (double.IsFinite(actual[k]))
{
if (double.IsFinite(actual[k])) { lastValidActual = actual[k]; break; }
lastValidActual = actual[k];
break;
}
for (int k = 0; k < len; k++)
}
for (int k = 0; k < len; k++)
{
if (double.IsFinite(predicted[k]))
{
if (double.IsFinite(predicted[k])) { lastValidPredicted = predicted[k]; break; }
lastValidPredicted = predicted[k];
break;
}
}
int bufferIndex = 0;
int i = 0;
@@ -220,8 +248,23 @@ public sealed class Wmape : AbstractBase
double act = actual[i];
double pred = predicted[i];
if (double.IsFinite(act)) lastValidActual = act; else act = lastValidActual;
if (double.IsFinite(pred)) lastValidPredicted = pred; else pred = lastValidPredicted;
if (double.IsFinite(act))
{
lastValidActual = act;
}
else
{
act = lastValidActual;
}
if (double.IsFinite(pred))
{
lastValidPredicted = pred;
}
else
{
pred = lastValidPredicted;
}
double absError = Math.Abs(act - pred);
double absActual = Math.Abs(act);
@@ -240,8 +283,23 @@ public sealed class Wmape : AbstractBase
double act = actual[i];
double pred = predicted[i];
if (double.IsFinite(act)) lastValidActual = act; else act = lastValidActual;
if (double.IsFinite(pred)) lastValidPredicted = pred; else pred = lastValidPredicted;
if (double.IsFinite(act))
{
lastValidActual = act;
}
else
{
act = lastValidActual;
}
if (double.IsFinite(pred))
{
lastValidPredicted = pred;
}
else
{
pred = lastValidPredicted;
}
double absError = Math.Abs(act - pred);
double absActual = Math.Abs(act);
@@ -252,7 +310,10 @@ public sealed class Wmape : AbstractBase
absActualBuffer[bufferIndex] = absActual;
bufferIndex++;
if (bufferIndex >= period) bufferIndex = 0;
if (bufferIndex >= period)
{
bufferIndex = 0;
}
output[i] = absActualSum > 1e-10 ? (absErrorSum / absActualSum) * 100.0 : 0.0;
@@ -274,9 +335,14 @@ public sealed class Wmape : AbstractBase
finally
{
if (rentedError != null)
{
ArrayPool<double>.Shared.Return(rentedError);
}
if (rentedActual != null)
{
ArrayPool<double>.Shared.Return(rentedActual);
}
}
}
}
}
+9 -3
View File
@@ -360,7 +360,10 @@ public class WrmseTests
for (int i = 0; i < 10; i++)
{
actual.Add(now.AddMinutes(i), i * 10);
if (i < 5) predicted.Add(now.AddMinutes(i), i * 10 + 5);
if (i < 5)
{
predicted.Add(now.AddMinutes(i), i * 10 + 5);
}
}
Assert.Throws<ArgumentException>(() => Wrmse.Calculate(actual, predicted, 3));
@@ -378,7 +381,10 @@ public class WrmseTests
{
actual.Add(now.AddMinutes(i), i * 10);
predicted.Add(now.AddMinutes(i), i * 10 + 5);
if (i < 5) weights.Add(now.AddMinutes(i), 1.0);
if (i < 5)
{
weights.Add(now.AddMinutes(i), 1.0);
}
}
Assert.Throws<ArgumentException>(() => Wrmse.Calculate(actual, predicted, weights, 3));
@@ -411,4 +417,4 @@ public class WrmseTests
Assert.Equal(rmseResults[i], wrmseResults[i], 9);
}
}
}
}
+37 -3
View File
@@ -50,7 +50,9 @@ public sealed class Wrmse : AbstractBase
public Wrmse(int period)
{
if (period <= 0)
{
throw new ArgumentException("Period must be greater than 0", nameof(period));
}
_weightedErrorBuffer = new RingBuffer(period);
_weightBuffer = new RingBuffer(period);
@@ -86,19 +88,31 @@ public sealed class Wrmse : AbstractBase
// Sanitize inputs
if (!double.IsFinite(actualVal))
{
actualVal = double.IsFinite(_state.LastValidActual) ? _state.LastValidActual : 0.0;
}
else
{
_state.LastValidActual = actualVal;
}
if (!double.IsFinite(predictedVal))
{
predictedVal = double.IsFinite(_state.LastValidPredicted) ? _state.LastValidPredicted : 0.0;
}
else
{
_state.LastValidPredicted = predictedVal;
}
if (!double.IsFinite(weight) || weight < 0)
{
weight = _state.LastValidWeight;
}
else
{
_state.LastValidWeight = weight;
}
// Compute weighted squared error
double diff = actualVal - predictedVal;
@@ -210,7 +224,9 @@ public sealed class Wrmse : AbstractBase
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);
@@ -233,7 +249,9 @@ public sealed class Wrmse : AbstractBase
public static TSeries Calculate(TSeries actual, TSeries predicted, TSeries weights, int period)
{
if (actual.Count != predicted.Count || actual.Count != weights.Count)
{
throw new ArgumentException("All series must have the same length", nameof(weights));
}
int len = actual.Count;
var t = new List<long>(len);
@@ -258,12 +276,20 @@ public sealed class Wrmse : AbstractBase
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;
if (len == 0)
{
return;
}
// With uniform weights, WRMSE = RMSE
const int StackAllocThreshold = 256;
@@ -283,12 +309,20 @@ public sealed class Wrmse : AbstractBase
public static void Batch(ReadOnlySpan<double> actual, ReadOnlySpan<double> predicted, ReadOnlySpan<double> weights, Span<double> output, int period)
{
if (actual.Length != predicted.Length || actual.Length != weights.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;
if (len == 0)
{
return;
}
const int StackAllocThreshold = 256;
Span<double> weightedErrors = len <= StackAllocThreshold
@@ -298,4 +332,4 @@ public sealed class Wrmse : AbstractBase
ErrorHelpers.ComputeWeightedErrors(actual, predicted, weights, weightedErrors);
ErrorHelpers.ApplyRollingWeightedMeanSqrt(weightedErrors, weights, output, period);
}
}
}