feat(tests): enhance tests with GBM for noise generation and improve tolerance for MAMA validation

feat(trends): implement IDisposable in Bessel and Conv classes to manage event subscriptions
fix(trends): add validation for period and parameters in Kama and MGDI calculations
fix(trends): clamp logarithmic calculations in JMA to avoid -Infinity
This commit is contained in:
Miha Kralj
2025-12-25 20:18:14 -08:00
parent df598c810d
commit ac8b2dbb3f
20 changed files with 281 additions and 187 deletions
+8 -2
View File
@@ -40,14 +40,20 @@ public class BetaValidationTests : IDisposable
var assetQuotes = new List<TBar>();
double assetPrice = 100;
double targetBeta = 1.5;
var rnd = new Random(123);
// Use GBM for noise generation (sigma=0.2 gives ~0.0006 per step noise which matches original random noise level)
var noiseGbm = new GBM(startPrice: 100, mu: 0, sigma: 0.2, seed: 777);
assetQuotes.Add(new TBar(marketQuotes[0].Time, assetPrice, assetPrice, assetPrice, assetPrice, 1000));
for (int i = 1; i < marketQuotes.Count; i++)
{
double marketReturn = (marketQuotes[i].Value - marketQuotes[i-1].Value) / marketQuotes[i-1].Value;
double noise = (rnd.NextDouble() - 0.5) * 0.002; // Small noise
// Get noise from GBM return
var noiseBar = noiseGbm.Next();
double noise = (noiseBar.Close - noiseBar.Open) / noiseBar.Open;
double assetReturn = targetBeta * marketReturn + noise;
assetPrice *= (1 + assetReturn);
@@ -12,13 +12,14 @@ public class CovarianceSimdTests
// Arrange
int count = 1000; // > 256 to trigger SIMD
int period = 20;
var r = new Random(42);
var gbmX = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 42);
var gbmY = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 123);
var dataX = new double[count];
var dataY = new double[count];
for (int i = 0; i < count; i++)
{
dataX[i] = r.NextDouble() * 100;
dataY[i] = r.NextDouble() * 100;
dataX[i] = gbmX.Next().Close;
dataY[i] = gbmY.Next().Close;
}
var sourceX = new TSeries();
@@ -11,14 +11,15 @@ public class CovarianceValidationTests
// Arrange
int period = 10;
var cov = new Covariance(period, isPopulation: false);
var r = new Random(123);
var gbmX = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 123);
var gbmY = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 456);
double[] x = new double[100];
double[] y = new double[100];
for (int i = 0; i < 100; i++)
{
x[i] = r.NextDouble() * 100;
y[i] = r.NextDouble() * 100;
x[i] = gbmX.Next().Close;
y[i] = gbmY.Next().Close;
cov.Update(x[i], y[i]);
if (i >= period - 1)
@@ -52,14 +53,15 @@ public class CovarianceValidationTests
// Arrange
int period = 10;
var cov = new Covariance(period, isPopulation: true);
var r = new Random(456);
var gbmX = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 456);
var gbmY = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 789);
double[] x = new double[100];
double[] y = new double[100];
for (int i = 0; i < 100; i++)
{
x[i] = r.NextDouble() * 100;
y[i] = r.NextDouble() * 100;
x[i] = gbmX.Next().Close;
y[i] = gbmY.Next().Close;
cov.Update(x[i], y[i]);
if (i >= period - 1)
+9 -6
View File
@@ -78,10 +78,11 @@ public class StdDevTests
int period = 10;
int count = 1000;
var data = new double[count];
var random = new Random(123);
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 123);
for (int i = 0; i < count; i++)
{
data[i] = random.NextDouble() * 100;
data[i] = gbm.Next().Close;
}
// Iterative
@@ -100,7 +101,7 @@ public class StdDevTests
// Compare
for (int i = 0; i < count; i++)
{
Assert.Equal(iterativeResults[i], batchResults[i], precision: 7);
Assert.Equal(iterativeResults[i], batchResults[i], precision: 6);
}
}
@@ -110,10 +111,12 @@ public class StdDevTests
int period = 10;
int count = 1000;
var data = new TSeries();
var random = new Random(123);
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 123);
for (int i = 0; i < count; i++)
{
data.Add(new TValue(DateTime.UtcNow, random.NextDouble() * 100));
var bar = gbm.Next();
data.Add(new TValue(bar.Time, bar.Close));
}
// Iterative
@@ -132,7 +135,7 @@ public class StdDevTests
// Compare
for (int i = 0; i < count; i++)
{
Assert.Equal(iterativeResults[i], batchSeries[i].Value, precision: 7);
Assert.Equal(iterativeResults[i], batchSeries[i].Value, precision: 6);
}
}
}
+3 -2
View File
@@ -96,10 +96,11 @@ public class VarianceTests
int period = 10;
int count = 1000;
var data = new double[count];
var random = new Random(123);
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 123);
for (int i = 0; i < count; i++)
{
data[i] = random.NextDouble() * 100;
data[i] = gbm.Next().Close;
}
// Iterative