mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-21 03: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,672 @@
|
||||
using Xunit;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class DwtTests
|
||||
{
|
||||
private const double Tolerance = 1e-10;
|
||||
|
||||
// ─── A) Constructor validation ────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Constructor_DefaultParameters_SetsProperties()
|
||||
{
|
||||
var indicator = new Dwt();
|
||||
Assert.Equal("Dwt(4,0)", indicator.Name);
|
||||
Assert.False(indicator.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_CustomParameters_SetsName()
|
||||
{
|
||||
var indicator = new Dwt(levels: 3, output: 1);
|
||||
Assert.Equal("Dwt(3,1)", indicator.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_ZeroLevel_ThrowsArgumentException()
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentException>(() => new Dwt(levels: 0));
|
||||
Assert.Equal("levels", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_NegativeLevel_ThrowsArgumentException()
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentException>(() => new Dwt(levels: -1));
|
||||
Assert.Equal("levels", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_LevelAboveMax_ThrowsArgumentException()
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentException>(() => new Dwt(levels: 9));
|
||||
Assert.Equal("levels", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_OutputNegative_ThrowsArgumentException()
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentException>(() => new Dwt(levels: 4, output: -1));
|
||||
Assert.Equal("output", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_OutputAboveLevels_ThrowsArgumentException()
|
||||
{
|
||||
// levels=3, output=4 is invalid
|
||||
var ex = Assert.Throws<ArgumentException>(() => new Dwt(levels: 3, output: 4));
|
||||
Assert.Equal("output", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_OutputEqualToLevels_IsValid()
|
||||
{
|
||||
// output == levels is valid (detail at deepest level)
|
||||
var indicator = new Dwt(levels: 3, output: 3);
|
||||
Assert.NotNull(indicator);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WarmupPeriod_IsPowerOfTwo()
|
||||
{
|
||||
// WarmupPeriod = 2^levels
|
||||
Assert.Equal(2, new Dwt(levels: 1).WarmupPeriod);
|
||||
Assert.Equal(4, new Dwt(levels: 2).WarmupPeriod);
|
||||
Assert.Equal(8, new Dwt(levels: 3).WarmupPeriod);
|
||||
Assert.Equal(16, new Dwt(levels: 4).WarmupPeriod);
|
||||
Assert.Equal(32, new Dwt(levels: 5).WarmupPeriod);
|
||||
Assert.Equal(64, new Dwt(levels: 6).WarmupPeriod);
|
||||
Assert.Equal(128, new Dwt(levels: 7).WarmupPeriod);
|
||||
Assert.Equal(256, new Dwt(levels: 8).WarmupPeriod);
|
||||
}
|
||||
|
||||
// ─── B) Basic calculation ─────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Update_ReturnsValidTValue()
|
||||
{
|
||||
var indicator = new Dwt(levels: 2);
|
||||
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_ApproximationOutput_IsFinite()
|
||||
{
|
||||
var indicator = new Dwt(levels: 2, output: 0);
|
||||
var time = DateTime.UtcNow;
|
||||
int warmup = indicator.WarmupPeriod;
|
||||
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 80001);
|
||||
var bars = gbm.Fetch(warmup + 10, time.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
for (int i = 0; i < bars.Close.Count; i++)
|
||||
{
|
||||
var result = indicator.Update(bars.Close[i]);
|
||||
Assert.True(double.IsFinite(result.Value),
|
||||
$"DWT approximation must be finite at bar {i}, got {result.Value}");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_DetailOutput_IsFinite()
|
||||
{
|
||||
var indicator = new Dwt(levels: 3, output: 1); // detail at level 1
|
||||
var time = DateTime.UtcNow;
|
||||
int warmup = indicator.WarmupPeriod;
|
||||
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 80002);
|
||||
var bars = gbm.Fetch(warmup + 10, time.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
for (int i = 0; i < bars.Close.Count; i++)
|
||||
{
|
||||
var result = indicator.Update(bars.Close[i]);
|
||||
Assert.True(double.IsFinite(result.Value),
|
||||
$"DWT detail must be finite at bar {i}, got {result.Value}");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Last_IsAccessible_AfterUpdate()
|
||||
{
|
||||
var indicator = new Dwt(levels: 2);
|
||||
var time = DateTime.UtcNow;
|
||||
indicator.Update(new TValue(time, 50.0));
|
||||
Assert.NotEqual(default, indicator.Last);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Name_Accessible_AndContainsDwt()
|
||||
{
|
||||
var indicator = new Dwt(levels: 4, output: 0);
|
||||
Assert.NotNull(indicator.Name);
|
||||
Assert.Contains("Dwt", indicator.Name, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
// ─── C) State + bar correction ────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Update_IsNewTrue_AdvancesState()
|
||||
{
|
||||
var indicator = new Dwt(levels: 2);
|
||||
var time = DateTime.UtcNow;
|
||||
int warmup = indicator.WarmupPeriod;
|
||||
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 80003);
|
||||
var bars = gbm.Fetch(warmup + 5, time.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
for (int i = 0; i < warmup; i++)
|
||||
{
|
||||
indicator.Update(bars.Close[i]);
|
||||
}
|
||||
|
||||
double before = indicator.Last.Value;
|
||||
indicator.Update(new TValue(time.AddMinutes(warmup), 9999.0), true);
|
||||
double after = indicator.Last.Value;
|
||||
|
||||
Assert.True(double.IsFinite(after));
|
||||
Assert.NotEqual(before, after, 1.0); // extreme value should change result
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_IsNewFalse_RewritesLastBar()
|
||||
{
|
||||
var indicator = new Dwt(levels: 2);
|
||||
var time = DateTime.UtcNow;
|
||||
int warmup = indicator.WarmupPeriod;
|
||||
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 80004);
|
||||
var bars = gbm.Fetch(warmup + 2, time.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
for (int i = 0; i < warmup; i++)
|
||||
{
|
||||
indicator.Update(bars.Close[i]);
|
||||
}
|
||||
|
||||
// New bar with extreme value A
|
||||
indicator.Update(new TValue(time.AddMinutes(warmup), 9999.0), true);
|
||||
double valueA = indicator.Last.Value;
|
||||
|
||||
// Correct same bar with very different value B
|
||||
indicator.Update(new TValue(time.AddMinutes(warmup), 0.001), false);
|
||||
double valueB = indicator.Last.Value;
|
||||
|
||||
Assert.NotEqual(valueA, valueB, 1e-6);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_IterativeCorrection_RestoresState()
|
||||
{
|
||||
var time = DateTime.UtcNow;
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 80005);
|
||||
int count = 30;
|
||||
var bars = gbm.Fetch(count, time.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
// Streaming without corrections
|
||||
var straight = new Dwt(levels: 2);
|
||||
for (int i = 0; i < bars.Close.Count; i++)
|
||||
{
|
||||
straight.Update(bars.Close[i]);
|
||||
}
|
||||
|
||||
double finalStraight = straight.Last.Value;
|
||||
|
||||
// With corrections (wrong → corrected to same value)
|
||||
var corrected = new Dwt(levels: 2);
|
||||
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 Dwt(levels: 2);
|
||||
var time = DateTime.UtcNow;
|
||||
int warmup = indicator.WarmupPeriod;
|
||||
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 80006);
|
||||
var bars = gbm.Fetch(warmup, time.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
for (int i = 0; i < bars.Close.Count; i++)
|
||||
{
|
||||
indicator.Update(bars.Close[i]);
|
||||
}
|
||||
|
||||
Assert.True(indicator.IsHot);
|
||||
|
||||
indicator.Reset();
|
||||
|
||||
Assert.False(indicator.IsHot);
|
||||
Assert.Equal(default, indicator.Last);
|
||||
}
|
||||
|
||||
// ─── D) Warmup / convergence ──────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void IsHot_FlipsAtBufferSize()
|
||||
{
|
||||
// levels=2: bufferSize=4
|
||||
var indicator = new Dwt(levels: 2);
|
||||
var time = DateTime.UtcNow;
|
||||
int warmup = indicator.WarmupPeriod; // 4
|
||||
|
||||
for (int i = 0; i < warmup - 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(warmup - 1), 100.0 + warmup));
|
||||
Assert.True(indicator.IsHot, "Should be hot after warmup bars");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WarmupPeriod_LevelsDependent()
|
||||
{
|
||||
Assert.Equal(4, new Dwt(levels: 2).WarmupPeriod);
|
||||
Assert.Equal(16, new Dwt(levels: 4).WarmupPeriod);
|
||||
Assert.Equal(64, new Dwt(levels: 6).WarmupPeriod);
|
||||
}
|
||||
|
||||
// ─── E) Robustness ────────────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Update_NaN_UsesLastValidValue()
|
||||
{
|
||||
var indicator = new Dwt(levels: 2);
|
||||
var time = DateTime.UtcNow;
|
||||
int warmup = indicator.WarmupPeriod;
|
||||
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 80007);
|
||||
var bars = gbm.Fetch(warmup, time.Ticks, TimeSpan.FromMinutes(1));
|
||||
for (int i = 0; i < warmup; i++)
|
||||
{
|
||||
indicator.Update(bars.Close[i]);
|
||||
}
|
||||
|
||||
double before = indicator.Last.Value;
|
||||
indicator.Update(new TValue(time.AddMinutes(warmup), double.NaN));
|
||||
Assert.Equal(before, indicator.Last.Value, Tolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_PositiveInfinity_UsesLastValidValue()
|
||||
{
|
||||
var indicator = new Dwt(levels: 2);
|
||||
var time = DateTime.UtcNow;
|
||||
int warmup = indicator.WarmupPeriod;
|
||||
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 80008);
|
||||
var bars = gbm.Fetch(warmup, time.Ticks, TimeSpan.FromMinutes(1));
|
||||
for (int i = 0; i < warmup; i++)
|
||||
{
|
||||
indicator.Update(bars.Close[i]);
|
||||
}
|
||||
|
||||
double before = indicator.Last.Value;
|
||||
indicator.Update(new TValue(time.AddMinutes(warmup), double.PositiveInfinity));
|
||||
Assert.Equal(before, indicator.Last.Value, Tolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_NegativeInfinity_UsesLastValidValue()
|
||||
{
|
||||
var indicator = new Dwt(levels: 2);
|
||||
var time = DateTime.UtcNow;
|
||||
int warmup = indicator.WarmupPeriod;
|
||||
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 80009);
|
||||
var bars = gbm.Fetch(warmup, time.Ticks, TimeSpan.FromMinutes(1));
|
||||
for (int i = 0; i < warmup; i++)
|
||||
{
|
||||
indicator.Update(bars.Close[i]);
|
||||
}
|
||||
|
||||
double before = indicator.Last.Value;
|
||||
indicator.Update(new TValue(time.AddMinutes(warmup), double.NegativeInfinity));
|
||||
Assert.Equal(before, indicator.Last.Value, Tolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Update_BatchNaN_AlwaysFinite()
|
||||
{
|
||||
var indicator = new Dwt(levels: 1); // warmup = 2
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
double[] prices = { 100.0, double.NaN, 102.0, double.NaN, 98.0, 105.0, 103.0, 99.0 };
|
||||
for (int i = 0; i < prices.Length; i++)
|
||||
{
|
||||
var result = indicator.Update(new TValue(time.AddMinutes(i), prices[i]));
|
||||
Assert.True(double.IsFinite(result.Value),
|
||||
$"Output must be finite at {i}, got {result.Value}");
|
||||
}
|
||||
}
|
||||
|
||||
// ─── F) Consistency: batch == streaming == span == eventing ──────────────
|
||||
|
||||
[Fact]
|
||||
public void AllModes_ConsistencyCheck()
|
||||
{
|
||||
int levels = 2;
|
||||
int count = 50;
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 80010);
|
||||
var bars = gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
var source = bars.Close;
|
||||
|
||||
// Streaming
|
||||
var streaming = new Dwt(levels);
|
||||
for (int i = 0; i < source.Count; i++)
|
||||
{
|
||||
streaming.Update(source[i]);
|
||||
}
|
||||
|
||||
// Batch (TSeries)
|
||||
var batch = Dwt.Batch(source, levels);
|
||||
|
||||
// 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];
|
||||
Dwt.Batch(rawValues, spanOutput, levels);
|
||||
|
||||
// Eventing
|
||||
var eventResults = new List<double>();
|
||||
var eventSource = new TSeries();
|
||||
var eventIndicator = new Dwt(eventSource, levels);
|
||||
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);
|
||||
}
|
||||
|
||||
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 = 50;
|
||||
int levels = 2;
|
||||
var gbm = new GBM(startPrice: 50, mu: 0.0, sigma: 0.3, seed: 80011);
|
||||
var bars = gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
var source = bars.Close;
|
||||
|
||||
var streaming = new Dwt(levels);
|
||||
var streamingVals = new double[count];
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
streaming.Update(source[i]);
|
||||
streamingVals[i] = streaming.Last.Value;
|
||||
}
|
||||
|
||||
var batch = Dwt.Batch(source, levels);
|
||||
|
||||
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>(() =>
|
||||
Dwt.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>(() =>
|
||||
Dwt.Batch(src, dst));
|
||||
Assert.Equal("output", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_InvalidLevels_ThrowsArgumentException()
|
||||
{
|
||||
double[] src = { 1.0, 2.0, 3.0 };
|
||||
double[] dst = new double[3];
|
||||
var ex = Assert.Throws<ArgumentException>(() =>
|
||||
Dwt.Batch(src, dst, levels: 0));
|
||||
Assert.Equal("levels", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_InvalidOutput_ThrowsArgumentException()
|
||||
{
|
||||
double[] src = { 1.0, 2.0, 3.0 };
|
||||
double[] dst = new double[3];
|
||||
var ex = Assert.Throws<ArgumentException>(() =>
|
||||
Dwt.Batch(src, dst, levels: 2, outputComponent: 5));
|
||||
Assert.Equal("outputComponent", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_OutputIsFinite()
|
||||
{
|
||||
int count = 100;
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 80012);
|
||||
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];
|
||||
Dwt.Batch(src, dst, levels: 3);
|
||||
|
||||
foreach (double v in dst)
|
||||
{
|
||||
Assert.True(double.IsFinite(v), $"DWT output {v} must be finite");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_HandlesNaN()
|
||||
{
|
||||
// levels=1: bufferSize=2
|
||||
double[] src = new double[20];
|
||||
for (int i = 0; i < src.Length; i++)
|
||||
{
|
||||
src[i] = 100.0 + i;
|
||||
}
|
||||
|
||||
src[3] = double.NaN;
|
||||
double[] dst = new double[src.Length];
|
||||
Dwt.Batch(src, dst, levels: 1);
|
||||
|
||||
foreach (double v in dst)
|
||||
{
|
||||
Assert.True(double.IsFinite(v), $"Span output should be finite, got {v}");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_NoStackOverflow_Level8()
|
||||
{
|
||||
// levels=8: bufferSize=256 — exactly at StackallocThreshold boundary
|
||||
int count = 500;
|
||||
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];
|
||||
Dwt.Batch(src, dst, levels: 8);
|
||||
|
||||
foreach (double v in dst)
|
||||
{
|
||||
Assert.True(double.IsFinite(v));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_MatchesStreaming()
|
||||
{
|
||||
int count = 60;
|
||||
int levels = 2;
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.25, seed: 80013);
|
||||
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];
|
||||
Dwt.Batch(src, spanOut, levels: levels);
|
||||
|
||||
var streaming = new Dwt(levels);
|
||||
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 Dwt(levels: 2);
|
||||
int count = 0;
|
||||
indicator.Pub += (object? sender, in TValueEventArgs args) => count++;
|
||||
|
||||
var time = DateTime.UtcNow;
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
indicator.Update(new TValue(time.AddMinutes(i), 100.0 + i));
|
||||
}
|
||||
|
||||
Assert.Equal(5, count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Chaining_Constructor_Works()
|
||||
{
|
||||
int levels = 2;
|
||||
var source = new TSeries();
|
||||
var indicator = new Dwt(source, levels);
|
||||
int warmup = indicator.WarmupPeriod;
|
||||
|
||||
var time = DateTime.UtcNow;
|
||||
for (int i = 0; i < warmup; i++)
|
||||
{
|
||||
source.Add(new TValue(time.AddMinutes(i), 100.0 + i), true);
|
||||
}
|
||||
|
||||
Assert.True(indicator.IsHot);
|
||||
Assert.True(double.IsFinite(indicator.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Pub_EventValue_MatchesLast()
|
||||
{
|
||||
var indicator = new Dwt(levels: 2);
|
||||
TValue? lastEvent = null;
|
||||
indicator.Pub += (object? s, in TValueEventArgs e) => lastEvent = e.Value;
|
||||
|
||||
var time = DateTime.UtcNow;
|
||||
int warmup = indicator.WarmupPeriod;
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 80014);
|
||||
var bars = gbm.Fetch(warmup + 2, time.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
for (int i = 0; i < bars.Close.Count; i++)
|
||||
{
|
||||
indicator.Update(bars.Close[i]);
|
||||
}
|
||||
|
||||
Assert.NotNull(lastEvent);
|
||||
Assert.Equal(indicator.Last.Value, lastEvent.Value.Value, Tolerance);
|
||||
}
|
||||
|
||||
// ─── Additional: static Calculate method ─────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Calculate_StaticMethod_ReturnsTuple()
|
||||
{
|
||||
int count = 50;
|
||||
int levels = 2;
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 80015);
|
||||
var bars = gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
var (results, instance) = Dwt.Calculate(bars.Close, levels);
|
||||
|
||||
Assert.Equal(count, results.Count);
|
||||
Assert.Equal(results[^1].Value, instance.Last.Value, Tolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AllLevels_Approximation_IsFinite()
|
||||
{
|
||||
int count = 300;
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 80016);
|
||||
var bars = gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
for (int level = 1; level <= 8; level++)
|
||||
{
|
||||
var ind = new Dwt(levels: level, output: 0);
|
||||
for (int i = 0; i < bars.Close.Count; i++)
|
||||
{
|
||||
var result = ind.Update(bars.Close[i]);
|
||||
Assert.True(double.IsFinite(result.Value),
|
||||
$"Level {level} approximation must be finite at bar {i}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AllDetailLevels_AreFinite()
|
||||
{
|
||||
int count = 300;
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 80017);
|
||||
var bars = gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
// Test detail output at each level
|
||||
for (int maxLevels = 1; maxLevels <= 5; maxLevels++)
|
||||
{
|
||||
for (int detail = 1; detail <= maxLevels; detail++)
|
||||
{
|
||||
var ind = new Dwt(levels: maxLevels, output: detail);
|
||||
for (int i = 0; i < bars.Close.Count; i++)
|
||||
{
|
||||
var result = ind.Update(bars.Close[i]);
|
||||
Assert.True(double.IsFinite(result.Value),
|
||||
$"Detail level {detail}/{maxLevels} must be finite at bar {i}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,371 @@
|
||||
using Xunit;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Validation tests for Dwt using known mathematical properties of the
|
||||
/// à trous Haar wavelet decomposition. No external library reference —
|
||||
/// validates against first-principles mathematical invariants.
|
||||
/// </summary>
|
||||
public class DwtValidationTests
|
||||
{
|
||||
private const double Tolerance = 1e-10;
|
||||
private const double CoarseTolerance = 1e-6;
|
||||
|
||||
// ─── Property 1: Constant signal → approximation = constant, detail ≈ 0 ──
|
||||
|
||||
[Fact]
|
||||
public void HaarDwt_ConstantSignal_ApproximationEqualsConstant()
|
||||
{
|
||||
// À trous Haar: avg of identical samples = the sample itself
|
||||
const double constantValue = 42.0;
|
||||
var indicator = new Dwt(levels: 4, output: 0); // approximation
|
||||
var time = DateTime.UtcNow;
|
||||
int warmup = indicator.WarmupPeriod;
|
||||
|
||||
for (int i = 0; i < warmup + 10; i++)
|
||||
{
|
||||
indicator.Update(new TValue(time.AddMinutes(i), constantValue));
|
||||
}
|
||||
|
||||
// After warmup, approximation of constant signal = constant
|
||||
Assert.Equal(constantValue, indicator.Last.Value, CoarseTolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HaarDwt_ConstantSignal_DetailEqualsZero()
|
||||
{
|
||||
// Detail = c[j-1] - c[j]; for constant input, both levels equal constant → detail = 0
|
||||
const double constantValue = 100.0;
|
||||
var time = DateTime.UtcNow;
|
||||
|
||||
for (int level = 1; level <= 5; level++)
|
||||
{
|
||||
var indicator = new Dwt(levels: level, output: level); // detail at deepest level
|
||||
int warmup = indicator.WarmupPeriod;
|
||||
|
||||
for (int i = 0; i < warmup + 5; i++)
|
||||
{
|
||||
indicator.Update(new TValue(time.AddMinutes(i), constantValue));
|
||||
}
|
||||
|
||||
Assert.Equal(0.0, indicator.Last.Value, CoarseTolerance);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HaarDwt_ConstantSignal_AllDetailLevelsZero()
|
||||
{
|
||||
// Every detail level of a constant signal should be zero
|
||||
const double constantValue = 50.0;
|
||||
var time = DateTime.UtcNow;
|
||||
int maxLevels = 4;
|
||||
int warmup = 1 << maxLevels; // 16
|
||||
|
||||
for (int detail = 1; detail <= maxLevels; detail++)
|
||||
{
|
||||
var indicator = new Dwt(levels: maxLevels, output: detail);
|
||||
for (int i = 0; i < warmup + 5; i++)
|
||||
{
|
||||
indicator.Update(new TValue(time.AddMinutes(i), constantValue));
|
||||
}
|
||||
|
||||
Assert.Equal(0.0, indicator.Last.Value, CoarseTolerance);
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Property 2: Zero input → zero output ────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void HaarDwt_ZeroInput_ZeroApproximation()
|
||||
{
|
||||
var indicator = new Dwt(levels: 3, output: 0);
|
||||
var time = DateTime.UtcNow;
|
||||
int warmup = indicator.WarmupPeriod;
|
||||
|
||||
for (int i = 0; i < warmup + 5; i++)
|
||||
{
|
||||
indicator.Update(new TValue(time.AddMinutes(i), 0.0));
|
||||
}
|
||||
|
||||
Assert.Equal(0.0, indicator.Last.Value, Tolerance);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HaarDwt_ZeroInput_ZeroDetail()
|
||||
{
|
||||
var indicator = new Dwt(levels: 3, output: 1);
|
||||
var time = DateTime.UtcNow;
|
||||
int warmup = indicator.WarmupPeriod;
|
||||
|
||||
for (int i = 0; i < warmup + 5; i++)
|
||||
{
|
||||
indicator.Update(new TValue(time.AddMinutes(i), 0.0));
|
||||
}
|
||||
|
||||
Assert.Equal(0.0, indicator.Last.Value, Tolerance);
|
||||
}
|
||||
|
||||
// ─── Property 3: Perfect reconstruction ──────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void PerfectReconstruction_ApproxPlusSumOfDetails_EqualsInput()
|
||||
{
|
||||
// x[n] = c[L][n] + sum(d[j][n], j=1..L)
|
||||
// All components computed at the same time = same input, so their sum = input.
|
||||
int levels = 3;
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 90001);
|
||||
int count = 50;
|
||||
var bars = gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
// Run all components simultaneously on same data
|
||||
var approxInd = new Dwt(levels, output: 0);
|
||||
var detail1Ind = new Dwt(levels, output: 1);
|
||||
var detail2Ind = new Dwt(levels, output: 2);
|
||||
var detail3Ind = new Dwt(levels, output: 3);
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
approxInd.Update(bars.Close[i]);
|
||||
detail1Ind.Update(bars.Close[i]);
|
||||
detail2Ind.Update(bars.Close[i]);
|
||||
detail3Ind.Update(bars.Close[i]);
|
||||
}
|
||||
|
||||
// Only check after full warmup
|
||||
double reconstructed = approxInd.Last.Value
|
||||
+ detail1Ind.Last.Value
|
||||
+ detail2Ind.Last.Value
|
||||
+ detail3Ind.Last.Value;
|
||||
|
||||
double original = bars.Close[^1].Value;
|
||||
Assert.Equal(original, reconstructed, 1e-8);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PerfectReconstruction_Level2_HoldsForMultipleBars()
|
||||
{
|
||||
int levels = 2;
|
||||
int warmup = 1 << levels; // 4
|
||||
int count = 30;
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.0, sigma: 0.15, seed: 90002);
|
||||
var bars = gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
var approxInd = new Dwt(levels, output: 0);
|
||||
var detail1Ind = new Dwt(levels, output: 1);
|
||||
var detail2Ind = new Dwt(levels, output: 2);
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
approxInd.Update(bars.Close[i]);
|
||||
detail1Ind.Update(bars.Close[i]);
|
||||
detail2Ind.Update(bars.Close[i]);
|
||||
|
||||
if (i >= warmup - 1)
|
||||
{
|
||||
double reconstructed = approxInd.Last.Value
|
||||
+ detail1Ind.Last.Value
|
||||
+ detail2Ind.Last.Value;
|
||||
double original = bars.Close[i].Value;
|
||||
Assert.Equal(original, reconstructed, 1e-8);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Property 4: Approximation smooths variance ───────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Approximation_HasLowerVariance_ThanInput()
|
||||
{
|
||||
// By design, Haar averaging reduces high-frequency variance.
|
||||
int levels = 3;
|
||||
int count = 200;
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.0, sigma: 0.3, seed: 90003);
|
||||
var bars = gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
int warmup = 1 << levels;
|
||||
|
||||
var approxInd = new Dwt(levels, output: 0);
|
||||
var approxVals = new List<double>();
|
||||
var inputVals = new List<double>();
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
approxInd.Update(bars.Close[i]);
|
||||
if (i >= warmup)
|
||||
{
|
||||
approxVals.Add(approxInd.Last.Value);
|
||||
inputVals.Add(bars.Close[i].Value);
|
||||
}
|
||||
}
|
||||
|
||||
double inputVar = Variance(inputVals);
|
||||
double approxVar = Variance(approxVals);
|
||||
|
||||
Assert.True(approxVar <= inputVar,
|
||||
$"Approximation variance {approxVar:F6} should be <= input variance {inputVar:F6}");
|
||||
}
|
||||
|
||||
// ─── Property 5: Linearity of the transform ───────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void DwtApproximation_IsLinear_ScaledInputScalesOutput()
|
||||
{
|
||||
// DWT is a linear operator: DWT(k*x) = k*DWT(x)
|
||||
const double scale = 2.5;
|
||||
int levels = 2;
|
||||
int count = 20;
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.0, sigma: 0.1, seed: 90004);
|
||||
var bars = gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
|
||||
var ind1 = new Dwt(levels, output: 0);
|
||||
var ind2 = new Dwt(levels, output: 0);
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
ind1.Update(bars.Close[i]);
|
||||
ind2.Update(new TValue(bars.Close[i].Time, bars.Close[i].Value * scale));
|
||||
}
|
||||
|
||||
// ind2.Last ≈ scale * ind1.Last
|
||||
Assert.Equal(ind1.Last.Value * scale, ind2.Last.Value, 1e-8);
|
||||
}
|
||||
|
||||
// ─── Property 6: Span API perfect-reconstruction ─────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_PerfectReconstruction_Level2()
|
||||
{
|
||||
int levels = 2;
|
||||
int count = 40;
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.0, sigma: 0.2, seed: 90005);
|
||||
var bars = gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
int warmup = 1 << levels;
|
||||
|
||||
double[] src = new double[count];
|
||||
for (int i = 0; i < count; i++) { src[i] = bars.Close[i].Value; }
|
||||
|
||||
double[] approx = new double[count];
|
||||
double[] d1 = new double[count];
|
||||
double[] d2 = new double[count];
|
||||
|
||||
Dwt.Batch(src, approx, levels, 0);
|
||||
Dwt.Batch(src, d1, levels, 1);
|
||||
Dwt.Batch(src, d2, levels, 2);
|
||||
|
||||
for (int i = warmup - 1; i < count; i++)
|
||||
{
|
||||
double reconstructed = approx[i] + d1[i] + d2[i];
|
||||
Assert.Equal(src[i], reconstructed, 1e-8);
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Property 7: Detail level 1 captures 2-bar differences ───────────────
|
||||
|
||||
[Fact]
|
||||
public void Detail1_CapturesHighFrequency_LargerThanDetail2()
|
||||
{
|
||||
// For GBM noise: detail level 1 (2-bar scale) has larger variance than detail level 2 (4-bar scale)
|
||||
// because lower-frequency details progressively smooth
|
||||
int levels = 3;
|
||||
int count = 200;
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.0, sigma: 0.3, seed: 90006);
|
||||
var bars = gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
||||
int warmup = 1 << levels;
|
||||
|
||||
var d1Ind = new Dwt(levels, output: 1);
|
||||
var d2Ind = new Dwt(levels, output: 2);
|
||||
var d1Vals = new List<double>();
|
||||
var d2Vals = new List<double>();
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
d1Ind.Update(bars.Close[i]);
|
||||
d2Ind.Update(bars.Close[i]);
|
||||
if (i >= warmup)
|
||||
{
|
||||
d1Vals.Add(d1Ind.Last.Value);
|
||||
d2Vals.Add(d2Ind.Last.Value);
|
||||
}
|
||||
}
|
||||
|
||||
double d1Var = Variance(d1Vals);
|
||||
double d2Var = Variance(d2Vals);
|
||||
|
||||
// d1 captures finer-scale variation → should have higher energy than d2
|
||||
Assert.True(d1Var >= d2Var * 0.5,
|
||||
$"Detail 1 variance {d1Var:F6} should be >= 50% of detail 2 variance {d2Var:F6}");
|
||||
}
|
||||
|
||||
// ─── Property 8: Span vs streaming consistency across all levels ──────────
|
||||
|
||||
[Fact]
|
||||
public void Batch_Span_MatchesStreaming_AllLevels()
|
||||
{
|
||||
int count = 100;
|
||||
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 90007);
|
||||
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; }
|
||||
|
||||
for (int levels = 1; levels <= 5; levels++)
|
||||
{
|
||||
double[] spanOut = new double[count];
|
||||
Dwt.Batch(src, spanOut, levels, 0);
|
||||
|
||||
var streaming = new Dwt(levels, 0);
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
streaming.Update(bars.Close[i]);
|
||||
Assert.Equal(streaming.Last.Value, spanOut[i], Tolerance);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Dwt_Correction_Recomputes()
|
||||
{
|
||||
var ind = new Dwt(levels: 4);
|
||||
var t0 = DateTime.MinValue;
|
||||
|
||||
// Build state well past warmup (WarmupPeriod = 2^4 = 16)
|
||||
for (int i = 0; i < 50; i++)
|
||||
{
|
||||
ind.Update(new TValue(t0.AddSeconds(i), 100.0 + (i * 0.5)));
|
||||
}
|
||||
|
||||
// Anchor bar
|
||||
var anchorTime = t0.AddSeconds(50);
|
||||
const double anchorPrice = 125.0;
|
||||
ind.Update(new TValue(anchorTime, anchorPrice), isNew: true);
|
||||
double anchorResult = ind.Last.Value;
|
||||
|
||||
// Correction with dramatically different value — DWT uses anchor at lag 0
|
||||
ind.Update(new TValue(anchorTime, anchorPrice * 10), isNew: false);
|
||||
Assert.NotEqual(anchorResult, ind.Last.Value);
|
||||
|
||||
// Correction back to original — must exactly restore
|
||||
ind.Update(new TValue(anchorTime, anchorPrice), isNew: false);
|
||||
Assert.Equal(anchorResult, ind.Last.Value, 1e-9);
|
||||
}
|
||||
|
||||
// ─── Helper ──────────────────────────────────────────────────────────────
|
||||
|
||||
private static double Variance(List<double> vals)
|
||||
{
|
||||
if (vals.Count < 2) { return 0.0; }
|
||||
|
||||
double mean = 0.0;
|
||||
for (int i = 0; i < vals.Count; i++) { mean += vals[i]; }
|
||||
|
||||
mean /= vals.Count;
|
||||
double ss = 0.0;
|
||||
for (int i = 0; i < vals.Count; i++)
|
||||
{
|
||||
double d = vals[i] - mean;
|
||||
ss = Math.FusedMultiplyAdd(d, d, ss);
|
||||
}
|
||||
|
||||
return ss / (vals.Count - 1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user