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
@@ -162,4 +162,4 @@ public class SigmoidIndicatorTests
now = now.AddMinutes(1);
}
}
}
}
+4 -1
View File
@@ -46,7 +46,10 @@ public class SigmoidIndicator : Indicator, IWatchlistIndicator
protected override void OnUpdate(UpdateArgs args)
{
if (_sigmoid == null || _selector == null) return;
if (_sigmoid == null || _selector == null)
{
return;
}
var item = HistoricalData[0, SeekOriginHistory.End];
double value = _selector(item);
+13 -1
View File
@@ -219,7 +219,9 @@ public class SigmoidTests
var series = new TSeries();
for (int i = 0; i < 100; i++)
{
series.Add(new TValue(DateTime.UtcNow.AddSeconds(i), i - 50), isNew: true);
}
var result = sigmoid.Update(series);
@@ -232,7 +234,9 @@ public class SigmoidTests
var series = new TSeries();
for (int i = 0; i < 100; i++)
{
series.Add(new TValue(DateTime.UtcNow.AddSeconds(i), i - 50), isNew: true);
}
var result = Sigmoid.Calculate(series);
@@ -274,7 +278,9 @@ public class SigmoidTests
double[] source = new double[100];
var rng = new Random(42);
for (int i = 0; i < source.Length; i++)
{
source[i] = rng.NextDouble() * 200 - 100;
}
double[] spanOutput = new double[source.Length];
Sigmoid.Calculate(source.AsSpan(), spanOutput.AsSpan());
@@ -282,10 +288,14 @@ public class SigmoidTests
var sigmoid = new Sigmoid();
double[] streamOutput = new double[source.Length];
for (int i = 0; i < source.Length; i++)
{
streamOutput[i] = sigmoid.Update(new TValue(DateTime.UtcNow.AddSeconds(i), source[i]), true).Value;
}
for (int i = 0; i < source.Length; i++)
{
Assert.Equal(streamOutput[i], spanOutput[i], Epsilon);
}
}
[Fact]
@@ -315,7 +325,9 @@ public class SigmoidTests
sigmoid.Pub += (_, in _) => eventCount++;
for (int i = 0; i < 10; i++)
{
source.Add(new TValue(DateTime.UtcNow.AddSeconds(i), i), isNew: true);
}
Assert.Equal(10, eventCount);
}
@@ -351,4 +363,4 @@ public class SigmoidTests
Assert.Equal(0.5, result0.Value, Epsilon);
Assert.Equal(0.5, result100.Value, Epsilon);
}
}
}
@@ -222,7 +222,9 @@ public class SigmoidValidationTests
double[] source = new double[500];
var rng = new Random(42);
for (int i = 0; i < source.Length; i++)
{
source[i] = rng.NextDouble() * 200 - 50; // Range [-50, 150]
}
// Span calculation
double[] spanOutput = new double[source.Length];
@@ -236,4 +238,4 @@ public class SigmoidValidationTests
Assert.Equal(spanOutput[i], result.Value, Epsilon);
}
}
}
}
+24 -2
View File
@@ -38,7 +38,9 @@ public sealed class Sigmoid : AbstractBase
public Sigmoid(double k = 1.0, double x0 = 0.0)
{
if (k <= 0)
{
throw new ArgumentException("Steepness (k) must be positive", nameof(k));
}
_k = k;
_x0 = x0;
@@ -65,8 +67,16 @@ public sealed class Sigmoid : AbstractBase
{
double exponent = -k * (x - x0);
// Guard against overflow: exp(>709) overflows, exp(<-709) underflows to 0
if (exponent > 700) return 0.0; // exp(-700) ≈ 0
if (exponent < -700) return 1.0; // 1/(1+0) = 1
if (exponent > 700)
{
return 0.0; // exp(-700) ≈ 0
}
if (exponent < -700)
{
return 1.0; // 1/(1+0) = 1
}
return 1.0 / (1.0 + Math.Exp(exponent));
}
@@ -74,9 +84,13 @@ public sealed class Sigmoid : AbstractBase
public override TValue Update(TValue input, bool isNew = true)
{
if (isNew)
{
_p_state = _state;
}
else
{
_state = _p_state;
}
double value = input.Value;
double result;
@@ -134,11 +148,19 @@ public sealed class Sigmoid : AbstractBase
public static void Calculate(ReadOnlySpan<double> source, Span<double> output, double k = 1.0, double x0 = 0.0)
{
if (source.Length == 0)
{
throw new ArgumentException("Source cannot be empty", nameof(source));
}
if (output.Length < source.Length)
{
throw new ArgumentException("Output length must be >= source length", nameof(output));
}
if (k <= 0)
{
throw new ArgumentException("Steepness (k) must be positive", nameof(k));
}
double lastValid = 0.5; // Sigmoid(x0) = 0.5
int i = 0;