mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-25 13:58:04 +00:00
docs: remove C# Implementation Considerations sections, clean up temp scripts, reorganize test files
- Remove 'C# Implementation Considerations' sections from 34 indicator .md files - Delete 29 temp PowerShell scripts (_fix_mojibake.ps1, _hex_scan.ps1, etc.) - Move test files into tests/ subdirectories for consistent project structure - Add trader-focused bullet points to indicator documentation
This commit is contained in:
@@ -0,0 +1,633 @@
|
||||
using Xunit;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class LognormdistTests
|
||||
{
|
||||
private const double Tolerance = 1e-10;
|
||||
|
||||
// ─── A) Constructor validation ────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Constructor_DefaultParameters_SetsProperties()
|
||||
{
|
||||
var indicator = new Lognormdist();
|
||||
Assert.Equal("Lognormdist(0.00,1.00,14)", indicator.Name);
|
||||
Assert.Equal(14, indicator.WarmupPeriod);
|
||||
Assert.False(indicator.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_CustomParameters_SetsName()
|
||||
{
|
||||
var indicator = new Lognormdist(mu: -1.0, sigma: 0.5, period: 20);
|
||||
Assert.Equal("Lognormdist(-1.00,0.50,20)", indicator.Name);
|
||||
Assert.Equal(20, indicator.WarmupPeriod);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_ZeroSigma_ThrowsArgumentException()
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentException>(() => new Lognormdist(sigma: 0.0));
|
||||
Assert.Equal("sigma", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_NegativeSigma_ThrowsArgumentException()
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentException>(() => new Lognormdist(sigma: -1.0));
|
||||
Assert.Equal("sigma", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_PeriodOne_ThrowsArgumentException()
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentException>(() => new Lognormdist(period: 1));
|
||||
Assert.Equal("period", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_ZeroPeriod_ThrowsArgumentException()
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentException>(() => new Lognormdist(period: 0));
|
||||
Assert.Equal("period", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_NegativePeriod_ThrowsArgumentException()
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentException>(() => new Lognormdist(period: -1));
|
||||
Assert.Equal("period", ex.ParamName);
|
||||
}
|
||||
|
||||
// ─── B) Basic calculation ─────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Update_ReturnsValidTValue()
|
||||
{
|
||||
var indicator = new Lognormdist(period: 5);
|
||||
var time = DateTime.UtcNow;
|
||||
var input = new TValue(time, 100.0);
|
||||
var result = indicator.Update(input);
|
||||
Assert.Equal(input.Time, result.Time);
|
||||
Assert.True(double.IsFinite(result.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_OutputInRange()
|
||||
{
|
||||
var indicator = new Lognormdist(period: 5);
|
||||
var time = DateTime.UtcNow;
|
||||
double[] prices = { 100.0, 102.0, 98.0, 105.0, 103.0 };
|
||||
|
||||
foreach (var p in prices)
|
||||
{
|
||||
indicator.Update(new TValue(time, p));
|
||||
time = time.AddMinutes(1);
|
||||
}
|
||||
|
||||
Assert.True(indicator.Last.Value >= 0.0, "Output must be >= 0");
|
||||
Assert.True(indicator.Last.Value <= 1.0, "Output must be <= 1");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Last_IsAccessible_AfterUpdate()
|
||||
{
|
||||
var indicator = new Lognormdist(period: 3);
|
||||
var time = DateTime.UtcNow;
|
||||
indicator.Update(new TValue(time, 50.0));
|
||||
Assert.NotEqual(default, indicator.Last);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsHot_Property_ReflectsWarmup()
|
||||
{
|
||||
var indicator = new Lognormdist(period: 5);
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
indicator.Update(new TValue(time.AddMinutes(i), 100.0 + i));
|
||||
Assert.False(indicator.IsHot);
|
||||
}
|
||||
|
||||
indicator.Update(new TValue(time.AddMinutes(4), 104.0));
|
||||
Assert.True(indicator.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Name_IsAccessible()
|
||||
{
|
||||
var indicator = new Lognormdist(mu: 0.0, sigma: 1.0, period: 14);
|
||||
Assert.Equal("Lognormdist(0.00,1.00,14)", indicator.Name);
|
||||
}
|
||||
|
||||
// ─── C) State + bar correction ────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Update_IsNewTrue_AdvancesState()
|
||||
{
|
||||
var indicator = new Lognormdist(period: 5);
|
||||
var time = DateTime.UtcNow;
|
||||
double[] prices = { 100.0, 102.0, 98.0, 105.0, 103.0 };
|
||||
|
||||
foreach (var p in prices)
|
||||
{
|
||||
indicator.Update(new TValue(time, p));
|
||||
time = time.AddMinutes(1);
|
||||
}
|
||||
|
||||
double first = indicator.Last.Value;
|
||||
indicator.Update(new TValue(time, 110.0), true);
|
||||
double second = indicator.Last.Value;
|
||||
|
||||
Assert.NotEqual(first, second, Tolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_IsNewFalse_RewritesLastBar()
|
||||
{
|
||||
var indicator = new Lognormdist(period: 5);
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
double[] prices = { 100.0, 102.0, 98.0, 105.0, 103.0 };
|
||||
foreach (var p in prices)
|
||||
{
|
||||
indicator.Update(new TValue(time, p));
|
||||
time = time.AddMinutes(1);
|
||||
}
|
||||
|
||||
// New bar with value A
|
||||
indicator.Update(new TValue(time, 110.0), true);
|
||||
double valueA = indicator.Last.Value;
|
||||
|
||||
// Correct same bar with very different value B
|
||||
indicator.Update(new TValue(time, 90.0), false);
|
||||
double valueB = indicator.Last.Value;
|
||||
|
||||
Assert.NotEqual(valueA, valueB, Tolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_IterativeCorrection_RestoresState()
|
||||
{
|
||||
var time = DateTime.UtcNow;
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 82001);
|
||||
var bars = gbm.Fetch(20, time.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
// Streaming without corrections
|
||||
var straight = new Lognormdist(period: 5);
|
||||
for (int i = 0; i < bars.Close.Count; i++)
|
||||
{
|
||||
straight.Update(bars.Close[i]);
|
||||
}
|
||||
|
||||
double finalStraight = straight.Last.Value;
|
||||
|
||||
// With corrections (wrong → corrected)
|
||||
var corrected = new Lognormdist(period: 5);
|
||||
for (int i = 0; i < bars.Close.Count; i++)
|
||||
{
|
||||
corrected.Update(new TValue(bars.Close[i].Time, 999.0), true);
|
||||
corrected.Update(bars.Close[i], false);
|
||||
}
|
||||
|
||||
Assert.Equal(finalStraight, corrected.Last.Value, Tolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reset_ClearsState()
|
||||
{
|
||||
var indicator = new Lognormdist(period: 5);
|
||||
var time = DateTime.UtcNow;
|
||||
double[] prices = { 100.0, 102.0, 98.0, 105.0, 103.0 };
|
||||
|
||||
foreach (var p in prices)
|
||||
{
|
||||
indicator.Update(new TValue(time, p));
|
||||
time = time.AddMinutes(1);
|
||||
}
|
||||
|
||||
Assert.True(indicator.IsHot);
|
||||
|
||||
indicator.Reset();
|
||||
|
||||
Assert.False(indicator.IsHot);
|
||||
Assert.Equal(default, indicator.Last);
|
||||
}
|
||||
|
||||
// ─── D) Warmup / convergence ──────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void IsHot_FlipsAtPeriod()
|
||||
{
|
||||
int period = 10;
|
||||
var indicator = new Lognormdist(period: period);
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
for (int i = 0; i < period - 1; i++)
|
||||
{
|
||||
indicator.Update(new TValue(time.AddMinutes(i), 100.0 + i));
|
||||
Assert.False(indicator.IsHot, $"Should not be hot at bar {i + 1}");
|
||||
}
|
||||
|
||||
indicator.Update(new TValue(time.AddMinutes(period - 1), 100.0 + period));
|
||||
Assert.True(indicator.IsHot, "Should be hot after period bars");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WarmupPeriod_EqualsConstructorPeriod()
|
||||
{
|
||||
var indicator = new Lognormdist(period: 25);
|
||||
Assert.Equal(25, indicator.WarmupPeriod);
|
||||
}
|
||||
|
||||
// ─── E) Robustness ────────────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Update_NaN_UsesLastValidValue()
|
||||
{
|
||||
var indicator = new Lognormdist(period: 5);
|
||||
var time = DateTime.UtcNow;
|
||||
double[] prices = { 100.0, 102.0, 98.0, 105.0, 103.0 };
|
||||
|
||||
foreach (var p in prices)
|
||||
{
|
||||
indicator.Update(new TValue(time, p));
|
||||
time = time.AddMinutes(1);
|
||||
}
|
||||
|
||||
double before = indicator.Last.Value;
|
||||
|
||||
indicator.Update(new TValue(time, double.NaN));
|
||||
Assert.Equal(before, indicator.Last.Value, Tolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_PositiveInfinity_UsesLastValidValue()
|
||||
{
|
||||
var indicator = new Lognormdist(period: 5);
|
||||
var time = DateTime.UtcNow;
|
||||
double[] prices = { 100.0, 102.0, 98.0, 105.0, 103.0 };
|
||||
|
||||
foreach (var p in prices)
|
||||
{
|
||||
indicator.Update(new TValue(time, p));
|
||||
time = time.AddMinutes(1);
|
||||
}
|
||||
|
||||
double before = indicator.Last.Value;
|
||||
indicator.Update(new TValue(time, double.PositiveInfinity));
|
||||
Assert.Equal(before, indicator.Last.Value, Tolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_NegativeInfinity_UsesLastValidValue()
|
||||
{
|
||||
var indicator = new Lognormdist(period: 5);
|
||||
var time = DateTime.UtcNow;
|
||||
double[] prices = { 100.0, 102.0, 98.0, 105.0, 103.0 };
|
||||
|
||||
foreach (var p in prices)
|
||||
{
|
||||
indicator.Update(new TValue(time, p));
|
||||
time = time.AddMinutes(1);
|
||||
}
|
||||
|
||||
double before = indicator.Last.Value;
|
||||
indicator.Update(new TValue(time, double.NegativeInfinity));
|
||||
Assert.Equal(before, indicator.Last.Value, Tolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_BatchNaN_Stable()
|
||||
{
|
||||
var indicator = new Lognormdist(period: 5);
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
double[] prices = { 100.0, double.NaN, 102.0, double.NaN, 98.0, 105.0, 103.0 };
|
||||
foreach (var p in prices)
|
||||
{
|
||||
var result = indicator.Update(new TValue(time, p));
|
||||
Assert.True(double.IsFinite(result.Value), "Output must always be finite");
|
||||
time = time.AddMinutes(1);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_FlatValues_OutputIsFinite()
|
||||
{
|
||||
// When all values identical, range=0 → x=0.5 → finite CDF output
|
||||
var indicator = new Lognormdist(mu: 0.0, sigma: 1.0, period: 5);
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
var result = indicator.Update(new TValue(time.AddMinutes(i), 100.0));
|
||||
Assert.True(double.IsFinite(result.Value));
|
||||
}
|
||||
}
|
||||
|
||||
// ─── F) Consistency: batch == streaming == span == eventing ──────────────
|
||||
|
||||
[Fact]
|
||||
public void AllModes_ConsistencyCheck()
|
||||
{
|
||||
int count = 100;
|
||||
int period = 20;
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 82002);
|
||||
var bars = gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
var source = bars.Close;
|
||||
|
||||
// Streaming
|
||||
var streaming = new Lognormdist(period: period);
|
||||
for (int i = 0; i < source.Count; i++)
|
||||
{
|
||||
streaming.Update(source[i]);
|
||||
}
|
||||
|
||||
// Batch (TSeries)
|
||||
var batch = Lognormdist.Batch(source, period: period);
|
||||
|
||||
// Span
|
||||
var rawValues = new double[source.Count];
|
||||
for (int i = 0; i < source.Count; i++)
|
||||
{
|
||||
rawValues[i] = source[i].Value;
|
||||
}
|
||||
|
||||
var spanOutput = new double[source.Count];
|
||||
Lognormdist.Batch(rawValues, spanOutput, period: period);
|
||||
|
||||
// Eventing
|
||||
var eventResults = new List<double>();
|
||||
var eventSource = new TSeries();
|
||||
var eventIndicator = new Lognormdist(eventSource, period: period);
|
||||
eventIndicator.Pub += (object? s, in TValueEventArgs e) => eventResults.Add(e.Value.Value);
|
||||
|
||||
for (int i = 0; i < source.Count; i++)
|
||||
{
|
||||
eventSource.Add(source[i], true);
|
||||
}
|
||||
|
||||
// Verify last value matches across all modes
|
||||
double streamingLast = streaming.Last.Value;
|
||||
double batchLast = batch[source.Count - 1].Value;
|
||||
double spanLast = spanOutput[source.Count - 1];
|
||||
double eventLast = eventResults[^1];
|
||||
|
||||
Assert.Equal(streamingLast, batchLast, Tolerance);
|
||||
Assert.Equal(streamingLast, spanLast, Tolerance);
|
||||
Assert.Equal(streamingLast, eventLast, Tolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Streaming_VsBatch_AllValues_Match()
|
||||
{
|
||||
int count = 80;
|
||||
int period = 15;
|
||||
var gbm = new GBM(startPrice: 50, mu: 0.0, sigma: 0.3, seed: 82003);
|
||||
var bars = gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
var source = bars.Close;
|
||||
|
||||
var streaming = new Lognormdist(period: period);
|
||||
var streamingVals = new double[count];
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
streaming.Update(source[i]);
|
||||
streamingVals[i] = streaming.Last.Value;
|
||||
}
|
||||
|
||||
var batch = Lognormdist.Batch(source, period: period);
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
Assert.Equal(streamingVals[i], batch[i].Value, Tolerance);
|
||||
}
|
||||
}
|
||||
|
||||
// ─── G) Span API tests ────────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_EmptySource_ThrowsArgumentException()
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentException>(() =>
|
||||
Lognormdist.Batch([], Array.Empty<double>()));
|
||||
Assert.Equal("source", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_OutputTooShort_ThrowsArgumentException()
|
||||
{
|
||||
double[] src = { 1.0, 2.0, 3.0 };
|
||||
double[] dst = new double[2];
|
||||
var ex = Assert.Throws<ArgumentException>(() =>
|
||||
Lognormdist.Batch(src, dst));
|
||||
Assert.Equal("output", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_InvalidSigma_ThrowsArgumentException()
|
||||
{
|
||||
double[] src = { 1.0, 2.0, 3.0 };
|
||||
double[] dst = new double[3];
|
||||
var ex = Assert.Throws<ArgumentException>(() =>
|
||||
Lognormdist.Batch(src, dst, sigma: 0.0));
|
||||
Assert.Equal("sigma", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_NegativeSigma_ThrowsArgumentException()
|
||||
{
|
||||
double[] src = { 1.0, 2.0, 3.0 };
|
||||
double[] dst = new double[3];
|
||||
var ex = Assert.Throws<ArgumentException>(() =>
|
||||
Lognormdist.Batch(src, dst, sigma: -1.0));
|
||||
Assert.Equal("sigma", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_InvalidPeriod_ThrowsArgumentException()
|
||||
{
|
||||
double[] src = { 1.0, 2.0, 3.0 };
|
||||
double[] dst = new double[3];
|
||||
var ex = Assert.Throws<ArgumentException>(() =>
|
||||
Lognormdist.Batch(src, dst, period: 1));
|
||||
Assert.Equal("period", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_OutputInRange()
|
||||
{
|
||||
int count = 100;
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 82004);
|
||||
var bars = gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
double[] src = new double[count];
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
src[i] = bars.Close[i].Value;
|
||||
}
|
||||
|
||||
double[] dst = new double[count];
|
||||
Lognormdist.Batch(src, dst, period: 20);
|
||||
|
||||
foreach (double v in dst)
|
||||
{
|
||||
Assert.True(v >= 0.0 && v <= 1.0, $"Output {v} out of [0,1] range");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_HandlesNaN()
|
||||
{
|
||||
double[] src = { 100.0, double.NaN, 102.0, 98.0, 105.0, 103.0 };
|
||||
double[] dst = new double[src.Length];
|
||||
Lognormdist.Batch(src, dst, period: 4);
|
||||
|
||||
foreach (double v in dst)
|
||||
{
|
||||
Assert.True(double.IsFinite(v), "Span output should always be finite");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_NoStackOverflow_LargeData()
|
||||
{
|
||||
int count = 5000;
|
||||
double[] src = new double[count];
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
src[i] = 100.0 + Math.Sin(i * 0.1) * 10.0;
|
||||
}
|
||||
|
||||
double[] dst = new double[count];
|
||||
Lognormdist.Batch(src, dst, period: 300);
|
||||
|
||||
foreach (double v in dst)
|
||||
{
|
||||
Assert.True(double.IsFinite(v));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_MatchesStreaming()
|
||||
{
|
||||
int count = 60;
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.25, seed: 82005);
|
||||
var bars = gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
double[] src = new double[count];
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
src[i] = bars.Close[i].Value;
|
||||
}
|
||||
|
||||
double[] spanOut = new double[count];
|
||||
Lognormdist.Batch(src, spanOut, period: 14);
|
||||
|
||||
var streaming = new Lognormdist(period: 14);
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
streaming.Update(bars.Close[i]);
|
||||
Assert.Equal(streaming.Last.Value, spanOut[i], Tolerance);
|
||||
}
|
||||
}
|
||||
|
||||
// ─── H) Chainability ──────────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Pub_EventFires()
|
||||
{
|
||||
var indicator = new Lognormdist(period: 3);
|
||||
int count = 0;
|
||||
indicator.Pub += (object? sender, in TValueEventArgs args) => count++;
|
||||
|
||||
var time = DateTime.UtcNow;
|
||||
indicator.Update(new TValue(time, 100.0));
|
||||
indicator.Update(new TValue(time.AddMinutes(1), 102.0));
|
||||
indicator.Update(new TValue(time.AddMinutes(2), 98.0));
|
||||
|
||||
Assert.Equal(3, count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Chaining_Constructor_Works()
|
||||
{
|
||||
int period = 5;
|
||||
var source = new TSeries();
|
||||
var indicator = new Lognormdist(source, period: period);
|
||||
|
||||
var time = DateTime.UtcNow;
|
||||
double[] prices = { 100.0, 102.0, 98.0, 105.0, 103.0 };
|
||||
|
||||
foreach (var p in prices)
|
||||
{
|
||||
source.Add(new TValue(time, p), true);
|
||||
time = time.AddMinutes(1);
|
||||
}
|
||||
|
||||
Assert.True(indicator.IsHot);
|
||||
Assert.True(indicator.Last.Value >= 0.0 && indicator.Last.Value <= 1.0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Pub_EventValue_MatchesLast()
|
||||
{
|
||||
var indicator = new Lognormdist(period: 5);
|
||||
TValue? lastEvent = null;
|
||||
indicator.Pub += (object? s, in TValueEventArgs e) => lastEvent = e.Value;
|
||||
|
||||
var time = DateTime.UtcNow;
|
||||
double[] prices = { 100.0, 102.0, 98.0, 105.0, 103.0 };
|
||||
|
||||
foreach (var p in prices)
|
||||
{
|
||||
indicator.Update(new TValue(time, p));
|
||||
time = time.AddMinutes(1);
|
||||
}
|
||||
|
||||
Assert.NotNull(lastEvent);
|
||||
Assert.Equal(indicator.Last.Value, lastEvent.Value.Value, Tolerance);
|
||||
}
|
||||
|
||||
// ─── Additional: Parameter effects and Calculate ──────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void DifferentSigma_ProduceDifferentResults()
|
||||
{
|
||||
int count = 60;
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 82006);
|
||||
var bars = gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
var ind1 = new Lognormdist(mu: 0.0, sigma: 0.5, period: 20);
|
||||
var ind2 = new Lognormdist(mu: 0.0, sigma: 1.0, period: 20);
|
||||
var ind3 = new Lognormdist(mu: 0.0, sigma: 3.0, period: 20);
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
ind1.Update(bars.Close[i]);
|
||||
ind2.Update(bars.Close[i]);
|
||||
ind3.Update(bars.Close[i]);
|
||||
}
|
||||
|
||||
Assert.InRange(ind1.Last.Value, 0.0, 1.0);
|
||||
Assert.InRange(ind2.Last.Value, 0.0, 1.0);
|
||||
Assert.InRange(ind3.Last.Value, 0.0, 1.0);
|
||||
Assert.NotEqual(ind1.Last.Value, ind3.Last.Value, 1e-4);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_StaticMethod_ReturnsTuple()
|
||||
{
|
||||
int count = 50;
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 82007);
|
||||
var bars = gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
var (results, instance) = Lognormdist.Calculate(bars.Close, period: 20);
|
||||
|
||||
Assert.Equal(count, results.Count);
|
||||
Assert.True(instance.IsHot);
|
||||
Assert.Equal(results[^1].Value, instance.Last.Value, Tolerance);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,363 @@
|
||||
using Xunit;
|
||||
using MathNet.Numerics.Distributions;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// LognormdistValidationTests — validates against known mathematical properties
|
||||
/// of the Log-Normal Distribution CDF and against MathNet.Numerics LogNormal.
|
||||
/// Known-value tests call Lognormdist.StaticCdf / LogNormalCdf directly (bypassing windowing).
|
||||
/// Tolerance 1e-6 for the 5-term A&S 7.1.26 approximation (max error ~1.5e-7;
|
||||
/// using 1e-6 to give headroom). MathNet cross-validation uses 1e-6.
|
||||
/// </summary>
|
||||
public class LognormdistValidationTests
|
||||
{
|
||||
private const double ApproxTolerance = 1e-6; // A&S 7.1.26 five-term max error ~1.5e-7
|
||||
private const double LooseTolerance = 1e-4;
|
||||
|
||||
// ─── Boundary: x <= 0 → CDF = 0 ──────────────────────────────────────────
|
||||
|
||||
[Theory]
|
||||
[InlineData(0.0, 0.0, 1.0)]
|
||||
[InlineData(-1.0, 0.0, 1.0)]
|
||||
[InlineData(-5.0, 0.0, 1.0)]
|
||||
[InlineData(0.0, -1.0, 0.5)]
|
||||
public void StaticCdf_NonPositiveX_IsZero(double x, double mu, double sigma)
|
||||
{
|
||||
double cdf = Lognormdist.StaticCdf(x, mu, sigma);
|
||||
Assert.Equal(0.0, cdf, 1e-10);
|
||||
}
|
||||
|
||||
// ─── CDF always in [0, 1] ─────────────────────────────────────────────────
|
||||
|
||||
[Theory]
|
||||
[InlineData(0.001, 0.0, 1.0)]
|
||||
[InlineData(0.5, 0.0, 1.0)]
|
||||
[InlineData(1.0, 0.0, 1.0)]
|
||||
[InlineData(10.0, 0.0, 1.0)]
|
||||
[InlineData(1000.0, 0.0, 1.0)]
|
||||
[InlineData(0.1, -1.0, 0.5)]
|
||||
[InlineData(2.0, 1.0, 2.0)]
|
||||
public void StaticCdf_OutputBounded_ZeroToOne(double x, double mu, double sigma)
|
||||
{
|
||||
double cdf = Lognormdist.StaticCdf(x, mu, sigma);
|
||||
Assert.True(cdf >= 0.0 && cdf <= 1.0,
|
||||
$"CDF({x},{mu},{sigma})={cdf} out of [0,1]");
|
||||
}
|
||||
|
||||
// ─── Median: F(exp(μ)) = 0.5 ─────────────────────────────────────────────
|
||||
|
||||
[Theory]
|
||||
[InlineData(0.0, 1.0)]
|
||||
[InlineData(1.0, 1.0)]
|
||||
[InlineData(-2.0, 0.5)]
|
||||
[InlineData(0.0, 2.0)]
|
||||
[InlineData(3.0, 0.25)]
|
||||
public void StaticCdf_AtMedian_IsHalf(double mu, double sigma)
|
||||
{
|
||||
// Median of LogNormal(μ, σ²) = exp(μ)
|
||||
double median = Math.Exp(mu);
|
||||
double cdf = Lognormdist.StaticCdf(median, mu, sigma);
|
||||
Assert.Equal(0.5, cdf, ApproxTolerance);
|
||||
}
|
||||
|
||||
// ─── Standard LogNormal(0,1) known percentiles ────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void StaticCdf_LogNormal01_At1_IsHalf()
|
||||
{
|
||||
// ln(1)=0=μ, so z=0 → Φ(0)=0.5
|
||||
double cdf = Lognormdist.StaticCdf(1.0, 0.0, 1.0);
|
||||
Assert.Equal(0.5, cdf, ApproxTolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StaticCdf_LogNormal01_AtExpPlusSigma_Is0841()
|
||||
{
|
||||
// F(exp(μ+σ)) = F(exp(1)) = Φ(1) ≈ 0.8413
|
||||
double x = Math.Exp(1.0); // exp(μ+σ) with μ=0, σ=1
|
||||
double cdf = Lognormdist.StaticCdf(x, 0.0, 1.0);
|
||||
Assert.Equal(0.8413, cdf, 3);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StaticCdf_LogNormal01_AtExpMinusSigma_Is0159()
|
||||
{
|
||||
// F(exp(μ-σ)) = F(exp(-1)) = Φ(-1) ≈ 0.1587
|
||||
double x = Math.Exp(-1.0); // exp(μ-σ) with μ=0, σ=1
|
||||
double cdf = Lognormdist.StaticCdf(x, 0.0, 1.0);
|
||||
Assert.Equal(0.1587, cdf, 3);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StaticCdf_LogNormal01_AtExpPlus2Sigma_Is0977()
|
||||
{
|
||||
// F(exp(μ+2σ)) = Φ(2) ≈ 0.9772
|
||||
double x = Math.Exp(2.0);
|
||||
double cdf = Lognormdist.StaticCdf(x, 0.0, 1.0);
|
||||
Assert.Equal(0.9772, cdf, 3);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StaticCdf_LogNormal01_AtExpMinus2Sigma_Is0023()
|
||||
{
|
||||
// F(exp(-2)) = Φ(-2) ≈ 0.0228
|
||||
double x = Math.Exp(-2.0);
|
||||
double cdf = Lognormdist.StaticCdf(x, 0.0, 1.0);
|
||||
Assert.Equal(0.0228, cdf, 3);
|
||||
}
|
||||
|
||||
// ─── Monotonicity for x > 0 ───────────────────────────────────────────────
|
||||
|
||||
[Theory]
|
||||
[InlineData(0.0, 1.0)]
|
||||
[InlineData(1.0, 0.5)]
|
||||
[InlineData(-1.0, 2.0)]
|
||||
public void StaticCdf_MonotonicIncreasing_ForPositiveX(double mu, double sigma)
|
||||
{
|
||||
double prev = -1.0;
|
||||
|
||||
for (int i = -20; i <= 20; i++)
|
||||
{
|
||||
double x = Math.Exp(i * 0.25); // x in (exp(-5), exp(5)) — always positive
|
||||
double cdf = Lognormdist.StaticCdf(x, mu, sigma);
|
||||
Assert.True(cdf >= prev - LooseTolerance,
|
||||
$"CDF not monotonic at x={x} (μ={mu}, σ={sigma}): got {cdf}, prev={prev}");
|
||||
prev = cdf;
|
||||
}
|
||||
}
|
||||
|
||||
// ─── MathNet.Numerics cross-validation ────────────────────────────────────
|
||||
|
||||
[Theory]
|
||||
[InlineData(1.0, 0.0, 1.0)]
|
||||
[InlineData(2.0, 0.0, 1.0)]
|
||||
[InlineData(0.5, 0.0, 1.0)]
|
||||
[InlineData(0.1, 0.0, 1.0)]
|
||||
[InlineData(10.0, 0.0, 1.0)]
|
||||
[InlineData(1.0, 1.0, 1.0)]
|
||||
[InlineData(0.5, 0.0, 2.0)]
|
||||
[InlineData(3.0, 2.0, 0.5)]
|
||||
[InlineData(0.1, -1.0, 0.5)]
|
||||
[InlineData(1.0, 0.0, 0.25)]
|
||||
public void StaticCdf_VsMathNet_KnownValues(double x, double mu, double sigma)
|
||||
{
|
||||
var dist = new LogNormal(mu, sigma);
|
||||
double expected = dist.CumulativeDistribution(x);
|
||||
double actual = Lognormdist.StaticCdf(x, mu, sigma);
|
||||
Assert.Equal(expected, actual, ApproxTolerance);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(1.0, 0.0, 1.0)]
|
||||
[InlineData(2.718, 0.0, 1.0)]
|
||||
[InlineData(0.368, 0.0, 1.0)]
|
||||
[InlineData(1.0, 1.0, 2.0)]
|
||||
[InlineData(5.0, 1.0, 0.5)]
|
||||
public void LogNormalCdf_VsMathNet_KnownValues(double x, double mu, double sigma)
|
||||
{
|
||||
var dist = new LogNormal(mu, sigma);
|
||||
double expected = dist.CumulativeDistribution(x);
|
||||
double actual = Lognormdist.LogNormalCdf(x, mu, sigma);
|
||||
Assert.Equal(expected, actual, ApproxTolerance);
|
||||
}
|
||||
|
||||
// ─── Multiple points all match MathNet ───────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void StaticCdf_MultiplePoints_AllMatchMathNet()
|
||||
{
|
||||
double mu = 0.0, sigma = 1.0;
|
||||
var dist = new LogNormal(mu, sigma);
|
||||
|
||||
double[] testX = { 0.01, 0.1, 0.25, 0.5, 1.0, 2.0, 5.0, 10.0, 50.0, 100.0 };
|
||||
|
||||
foreach (double x in testX)
|
||||
{
|
||||
double expected = dist.CumulativeDistribution(x);
|
||||
double actual = Lognormdist.StaticCdf(x, mu, sigma);
|
||||
Assert.Equal(expected, actual, ApproxTolerance);
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Output bounded [0,1] with streaming indicator ───────────────────────
|
||||
|
||||
[Fact]
|
||||
public void LognormdistCdf_OutputBounded_Zero_To_One()
|
||||
{
|
||||
int count = 200;
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.3, seed: 85001);
|
||||
var bars = gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
var indicator = new Lognormdist(mu: 0.0, sigma: 1.0, period: 20);
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
indicator.Update(bars.Close[i]);
|
||||
double v = indicator.Last.Value;
|
||||
Assert.True(v >= 0.0 && v <= 1.0, $"Output {v} at bar {i} out of [0,1]");
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Flat range → finite output ──────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void LognormdistCdf_FlatRange_IsFinite()
|
||||
{
|
||||
var ind = new Lognormdist(mu: 0.0, sigma: 1.0, period: 10);
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
ind.Update(new TValue(time.AddSeconds(i), 100.0));
|
||||
}
|
||||
|
||||
Assert.True(double.IsFinite(ind.Last.Value));
|
||||
Assert.True(ind.Last.Value >= 0.0 && ind.Last.Value <= 1.0);
|
||||
}
|
||||
|
||||
// ─── NormalCdf internal correctness ──────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void NormalCdf_AtZero_IsHalf()
|
||||
{
|
||||
double v = Lognormdist.NormalCdf(0.0);
|
||||
Assert.Equal(0.5, v, ApproxTolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NormalCdf_AtLargePositive_ApproachesOne()
|
||||
{
|
||||
double v = Lognormdist.NormalCdf(10.0);
|
||||
Assert.True(v > 0.9999, $"Φ(10) should approach 1, got {v}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NormalCdf_AtLargeNegative_ApproachesZero()
|
||||
{
|
||||
double v = Lognormdist.NormalCdf(-10.0);
|
||||
Assert.True(v < 1e-4, $"Φ(-10) should approach 0, got {v}");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NormalCdf_IsSymmetric()
|
||||
{
|
||||
// Φ(z) + Φ(-z) = 1
|
||||
double[] testZ = { 0.5, 1.0, 1.5, 2.0, 3.0 };
|
||||
foreach (double z in testZ)
|
||||
{
|
||||
double pos = Lognormdist.NormalCdf(z);
|
||||
double neg = Lognormdist.NormalCdf(-z);
|
||||
Assert.Equal(1.0, pos + neg, ApproxTolerance);
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Parameter combos all within [0,1] ────────────────────────────────────
|
||||
|
||||
[Theory]
|
||||
[InlineData(0.0, 1.0, 5)]
|
||||
[InlineData(0.0, 1.0, 14)]
|
||||
[InlineData(-1.0, 0.5, 10)]
|
||||
[InlineData(0.0, 2.0, 20)]
|
||||
[InlineData(1.0, 1.0, 30)]
|
||||
public void LognormdistCdf_ParameterCombos_OutputBounded(double mu, double sigma, int period)
|
||||
{
|
||||
int count = period + 50;
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 85002 + (int)(sigma * 100));
|
||||
var bars = gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
var indicator = new Lognormdist(mu, sigma, period);
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
indicator.Update(bars.Close[i]);
|
||||
double v = indicator.Last.Value;
|
||||
Assert.True(v >= 0.0 && v <= 1.0,
|
||||
$"Out of [0,1] at bar {i}: {v} (μ={mu}, σ={sigma}, period={period})");
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Large dataset stable ─────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void LognormdistCdf_LargeDataset_Stable()
|
||||
{
|
||||
int count = 2000;
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 85003);
|
||||
var bars = gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
var indicator = new Lognormdist(mu: 0.0, sigma: 1.0, period: 50);
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
indicator.Update(bars.Close[i]);
|
||||
double v = indicator.Last.Value;
|
||||
Assert.True(double.IsFinite(v) && v >= 0.0 && v <= 1.0,
|
||||
$"Invalid output {v} at bar {i}");
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Span batch vs TSeries consistency ────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_MatchesTSeries()
|
||||
{
|
||||
int count = 150;
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.25, seed: 85004);
|
||||
var bars = gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
double[] rawValues = new double[count];
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
rawValues[i] = bars.Close[i].Value;
|
||||
}
|
||||
|
||||
var tseriesResult = Lognormdist.Batch(bars.Close, period: 30);
|
||||
double[] spanResult = new double[count];
|
||||
Lognormdist.Batch(rawValues, spanResult, period: 30);
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
Assert.Equal(tseriesResult[i].Value, spanResult[i], 1e-10);
|
||||
}
|
||||
}
|
||||
|
||||
// ─── High-period streaming convergence ────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void LognormdistCdf_HighPeriod_StillConverges()
|
||||
{
|
||||
int period = 200;
|
||||
var indicator = new Lognormdist(mu: 0.0, sigma: 1.0, period: period);
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.3, seed: 85005);
|
||||
var bars = gbm.Fetch(period + 50, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
for (int i = 0; i < bars.Close.Count; i++)
|
||||
{
|
||||
indicator.Update(bars.Close[i]);
|
||||
Assert.True(double.IsFinite(indicator.Last.Value),
|
||||
$"Non-finite output at bar {i}");
|
||||
}
|
||||
}
|
||||
|
||||
// ─── MathNet parameter sweep ──────────────────────────────────────────────
|
||||
|
||||
[Theory]
|
||||
[InlineData(0.0, 0.5)]
|
||||
[InlineData(0.0, 1.0)]
|
||||
[InlineData(0.0, 2.0)]
|
||||
[InlineData(1.0, 1.0)]
|
||||
[InlineData(-1.0, 0.5)]
|
||||
public void StaticCdf_SweepX_VsMathNet(double mu, double sigma)
|
||||
{
|
||||
var dist = new LogNormal(mu, sigma);
|
||||
double[] xs = { 0.01, 0.05, 0.1, 0.25, 0.5, 1.0, 2.0, 5.0, 10.0, 20.0 };
|
||||
|
||||
foreach (double x in xs)
|
||||
{
|
||||
double expected = dist.CumulativeDistribution(x);
|
||||
double actual = Lognormdist.StaticCdf(x, mu, sigma);
|
||||
Assert.Equal(expected, actual, ApproxTolerance);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user