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
+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);
}
}
}