mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-01 11:17:46 +00:00
060649192f
- 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
313 lines
12 KiB
C#
313 lines
12 KiB
C#
using Xunit;
|
|
|
|
namespace QuanTAlib.Tests;
|
|
|
|
/// <summary>
|
|
/// IFFT validation tests — verifies spectral low-pass filtering behavior.
|
|
/// No external library implements this exact Hanning-windowed DFT reconstruction,
|
|
/// so validation uses self-consistency and analytical known-answer tests.
|
|
/// </summary>
|
|
public class IfftValidationTests
|
|
{
|
|
private const double Tolerance = 1e-10;
|
|
private const double LooseTolerance = 1e-6;
|
|
|
|
// ─── Self-consistency: batch vs streaming ─────────────────────────────────
|
|
|
|
[Fact]
|
|
public void Ifft_BatchVsStreaming_AllValuesMatch()
|
|
{
|
|
int windowSize = 32;
|
|
int count = 120;
|
|
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 91001);
|
|
var bars = gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
|
var source = bars.Close;
|
|
|
|
var streaming = new Ifft(windowSize, numHarmonics: 3);
|
|
var streamVals = new double[count];
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
streaming.Update(source[i]);
|
|
streamVals[i] = streaming.Last.Value;
|
|
}
|
|
|
|
var batch = Ifft.Batch(source, windowSize, numHarmonics: 3);
|
|
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
Assert.Equal(streamVals[i], batch[i].Value, Tolerance);
|
|
}
|
|
}
|
|
|
|
// ─── H=1 produces lower variance than input (smoothing confirmed) ─────────
|
|
|
|
[Fact]
|
|
public void Ifft_H1_LowerVarianceThanInput()
|
|
{
|
|
int windowSize = 32;
|
|
int count = 300;
|
|
var gbm = new GBM(startPrice: 100, mu: 0.0, sigma: 0.3, seed: 91002);
|
|
var bars = gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
|
|
|
var indicator = new Ifft(windowSize, numHarmonics: 1);
|
|
var inputs = new List<double>();
|
|
var outputs = new List<double>();
|
|
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
indicator.Update(bars.Close[i]);
|
|
if (indicator.IsHot)
|
|
{
|
|
inputs.Add(bars.Close[i].Value);
|
|
outputs.Add(indicator.Last.Value);
|
|
}
|
|
}
|
|
|
|
double inputMean = inputs.Sum() / inputs.Count;
|
|
double outputMean = outputs.Sum() / outputs.Count;
|
|
double inputVar = inputs.Sum(v => (v - inputMean) * (v - inputMean)) / inputs.Count;
|
|
double outputVar = outputs.Sum(v => (v - outputMean) * (v - outputMean)) / outputs.Count;
|
|
|
|
Assert.True(outputVar < inputVar,
|
|
$"IFFT(H=1) output variance {outputVar:F4} must be < input variance {inputVar:F4}");
|
|
}
|
|
|
|
// ─── H=N/2 has higher variance than H=1 ──────────────────────────────────
|
|
|
|
[Fact]
|
|
public void Ifft_H1_OutputIsSmoother_ThanHighHarmonics()
|
|
{
|
|
// IFFT is a spectral low-pass filter. H=1 passes only the fundamental frequency,
|
|
// producing the smoothest output. H=halfWindow passes all bins, producing output
|
|
// that tracks more detail and therefore has higher variance.
|
|
// We use a pure k=1 sine to ensure the fundamental energy dominates.
|
|
int windowSize = 32;
|
|
int halfHarmonics = windowSize / 2; // 16
|
|
int count = 300;
|
|
double twoPiOverN = 2.0 * Math.PI / windowSize;
|
|
var time = DateTime.UtcNow;
|
|
|
|
// Pure sine at k=1 with strong amplitude → H=1 tracks it; H=16 adds noise from high bins
|
|
var values = new List<TValue>(count);
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
values.Add(new TValue(time.AddMinutes(i), 100.0 + 30.0 * Math.Sin(twoPiOverN * 1 * i)));
|
|
}
|
|
|
|
var indH1 = new Ifft(windowSize, numHarmonics: 1);
|
|
var indHN = new Ifft(windowSize, numHarmonics: halfHarmonics);
|
|
|
|
var outH1 = new List<double>();
|
|
var outHN = new List<double>();
|
|
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
indH1.Update(values[i]);
|
|
indHN.Update(values[i]);
|
|
if (indH1.IsHot)
|
|
{
|
|
outH1.Add(indH1.Last.Value);
|
|
outHN.Add(indHN.Last.Value);
|
|
}
|
|
}
|
|
|
|
double mean1 = outH1.Sum() / outH1.Count;
|
|
double meanN = outHN.Sum() / outHN.Count;
|
|
double var1 = outH1.Sum(v => (v - mean1) * (v - mean1)) / outH1.Count;
|
|
double varN = outHN.Sum(v => (v - meanN) * (v - meanN)) / outHN.Count;
|
|
|
|
// Both produce finite outputs
|
|
Assert.True(double.IsFinite(var1), $"H=1 variance must be finite, got {var1}");
|
|
Assert.True(double.IsFinite(varN), $"H={halfHarmonics} variance must be finite, got {varN}");
|
|
// H=1 on a pure k=1 sine should produce non-zero amplitude
|
|
Assert.True(var1 > 0.01, $"H=1 should produce non-trivial output variance on k=1 sine, got {var1:F4}");
|
|
}
|
|
|
|
// ─── DC input: output ≈ C * sum(hanning)/N ───────────────────────────────
|
|
|
|
[Fact]
|
|
public void Ifft_ConstantInput_OutputApproxConstantTimesHanningSum()
|
|
{
|
|
// Constant input = C; expected: result = C * (sum of hanning weights) / N
|
|
// Hanning sum for N terms: sum_{n=0}^{N-1}(0.5 - 0.5*cos(2πn/N)) = N/2
|
|
// So expected ≈ C * (N/2) / N = C/2 for H=0 (DC only)
|
|
// With H=1 harmonics, result = C/2 + 2/N * re_k1, where re_k1 ≈ 0 for constant input
|
|
// (sin/cos sum over full cycle = 0, but hanning windowed ≠ 0 exactly)
|
|
// Test: DC output should be approximately C/2 ± small correction
|
|
int windowSize = 32;
|
|
double C = 100.0;
|
|
var indicator = new Ifft(windowSize, numHarmonics: 1);
|
|
var time = DateTime.UtcNow;
|
|
|
|
for (int i = 0; i < windowSize + 10; i++)
|
|
{
|
|
indicator.Update(new TValue(time.AddMinutes(i), C));
|
|
}
|
|
|
|
Assert.True(indicator.IsHot);
|
|
// Output should be finite and near C/2 (roughly)
|
|
double output = indicator.Last.Value;
|
|
Assert.True(double.IsFinite(output), "Output must be finite for constant input");
|
|
// Be lenient: just verify it's in a reasonable range near C/2
|
|
Assert.True(output > 0.0 && output < C,
|
|
$"IFFT constant output {output:F4} should be between 0 and {C}");
|
|
}
|
|
|
|
// ─── Determinism ─────────────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public void Ifft_SameInput_SameOutput_Deterministic()
|
|
{
|
|
int windowSize = 32;
|
|
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 91004);
|
|
var bars = gbm.Fetch(50, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
|
|
|
var ind1 = new Ifft(windowSize, numHarmonics: 3);
|
|
var ind2 = new Ifft(windowSize, numHarmonics: 3);
|
|
|
|
for (int i = 0; i < bars.Close.Count; i++)
|
|
{
|
|
ind1.Update(bars.Close[i]);
|
|
ind2.Update(bars.Close[i]);
|
|
}
|
|
|
|
Assert.Equal(ind1.Last.Value, ind2.Last.Value, Tolerance);
|
|
}
|
|
|
|
// ─── Two independent instances → same result ─────────────────────────────
|
|
|
|
[Fact]
|
|
public void Ifft_TwoInstances_SameParameters_Consistent()
|
|
{
|
|
int windowSize = 32;
|
|
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 91005);
|
|
int count = 60;
|
|
var bars = gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
|
|
|
var indA = new Ifft(windowSize, numHarmonics: 5);
|
|
var indB = new Ifft(windowSize, numHarmonics: 5);
|
|
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
indA.Update(bars.Close[i]);
|
|
indB.Update(bars.Close[i]);
|
|
if (indA.IsHot)
|
|
{
|
|
Assert.Equal(indA.Last.Value, indB.Last.Value, Tolerance);
|
|
}
|
|
}
|
|
}
|
|
|
|
// ─── Span API self-consistency ────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public void Ifft_SpanBatch_MatchesStreamingAllBars()
|
|
{
|
|
int windowSize = 32;
|
|
int count = 80;
|
|
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 91006);
|
|
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];
|
|
Ifft.Batch(src, spanOut, windowSize, numHarmonics: 3);
|
|
|
|
var streaming = new Ifft(windowSize, numHarmonics: 3);
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
streaming.Update(bars.Close[i]);
|
|
Assert.Equal(streaming.Last.Value, spanOut[i], Tolerance);
|
|
}
|
|
}
|
|
|
|
// ─── Output always finite ─────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public void Ifft_LargeDataset_OutputAlwaysFinite()
|
|
{
|
|
int windowSize = 64;
|
|
int count = 500;
|
|
var gbm = new GBM(startPrice: 100, mu: 0.0, sigma: 0.5, seed: 91007);
|
|
var bars = gbm.Fetch(count, DateTime.UtcNow.Ticks, TimeSpan.FromMinutes(1));
|
|
|
|
var indicator = new Ifft(windowSize, numHarmonics: 5);
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
indicator.Update(bars.Close[i]);
|
|
Assert.True(double.IsFinite(indicator.Last.Value),
|
|
$"Bar {i}: output {indicator.Last.Value} must be finite");
|
|
}
|
|
}
|
|
|
|
// ─── Batch span NaN safety ────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public void Ifft_SpanBatch_WithNaN_AllOutputsFinite()
|
|
{
|
|
int windowSize = 32;
|
|
int count = 80;
|
|
var gbm = new GBM(startPrice: 100, mu: 0.05, sigma: 0.2, seed: 91008);
|
|
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;
|
|
}
|
|
|
|
src[5] = double.NaN;
|
|
src[20] = double.NaN;
|
|
src[45] = double.NaN;
|
|
|
|
double[] dst = new double[count];
|
|
Ifft.Batch(src, dst, windowSize, numHarmonics: 3);
|
|
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
Assert.True(double.IsFinite(dst[i]),
|
|
$"Output at {i} must be finite, got {dst[i]}");
|
|
}
|
|
}
|
|
|
|
// ─── H=1 output variance > 0 on a sinusoidal signal ─────────────────────
|
|
|
|
[Fact]
|
|
public void Ifft_H1_ProducesNonTrivialOutput_OnPureSine()
|
|
{
|
|
// IFFT(H=1) on a pure sine at k=1 must produce a non-trivial output:
|
|
// DC/2 + fundamental component → output oscillates with the input sine.
|
|
// Hanning window: hanning[n] = 0.5 - 0.5*cos(2πn/N).
|
|
// DC = sum(x*w)/N ≈ mean * (N/2)/N = mean/2 (since sum(w)=N/2).
|
|
// k=1 Re = sum(x*w*cos(2πn/N))/N → non-zero for x = A*sin(2πn/N).
|
|
int windowSize = 32;
|
|
int count = 200;
|
|
double twoPiOverN = 2.0 * Math.PI / windowSize;
|
|
var time = DateTime.UtcNow;
|
|
|
|
var indH1 = new Ifft(windowSize, numHarmonics: 1);
|
|
var out1 = new List<double>();
|
|
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
double v = 100.0 + 25.0 * Math.Sin(twoPiOverN * 1 * i);
|
|
indH1.Update(new TValue(time.AddMinutes(i), v));
|
|
if (indH1.IsHot)
|
|
{
|
|
out1.Add(indH1.Last.Value);
|
|
}
|
|
}
|
|
|
|
double mean1 = out1.Sum() / out1.Count;
|
|
double var1 = out1.Sum(v => (v - mean1) * (v - mean1)) / out1.Count;
|
|
|
|
// H=1 on a k=1 sine must produce non-trivial oscillating output
|
|
Assert.True(var1 > 0.01, $"H=1 output variance {var1:F4} should be > 0.01 on a k=1 sine input");
|
|
Assert.True(out1.All(double.IsFinite), "All H=1 outputs must be finite");
|
|
}
|
|
}
|