mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-19 19:18:05 +00:00
style patterns
This commit is contained in:
+89
-15
@@ -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];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user