mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-24 13:38:05 +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,504 @@
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public class PolyfitTests
|
||||
{
|
||||
// ── A) Constructor validation ─────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Constructor_DefaultParams_SetsName()
|
||||
{
|
||||
var p = new Polyfit(20);
|
||||
Assert.Equal("Polyfit(20,2)", p.Name);
|
||||
Assert.Equal(20, p.WarmupPeriod);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_ExplicitDegree_SetsName()
|
||||
{
|
||||
var p = new Polyfit(10, 3);
|
||||
Assert.Equal("Polyfit(10,3)", p.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_PeriodLessThan2_Throws()
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentException>(() => new Polyfit(1));
|
||||
Assert.Equal("period", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_PeriodZero_Throws()
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentException>(() => new Polyfit(0));
|
||||
Assert.Equal("period", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_DegreeZero_Throws()
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentException>(() => new Polyfit(10, 0));
|
||||
Assert.Equal("degree", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_DegreeClampedToPeriodMinus1()
|
||||
{
|
||||
// degree=10 with period=5 → clamped to 4
|
||||
var p = new Polyfit(5, 10);
|
||||
Assert.Equal("Polyfit(5,4)", p.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_ChainingSubscribes()
|
||||
{
|
||||
var src = new Sma(3);
|
||||
var p = new Polyfit(src, 5, 2);
|
||||
Assert.Equal("Polyfit(5,2)", p.Name);
|
||||
}
|
||||
|
||||
// ── B) Basic calculation ──────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void BasicCalc_ReturnsFiniteAfterWarmup()
|
||||
{
|
||||
var p = new Polyfit(5, 2);
|
||||
var gbm = new GBM(100, 0.05, 0.2, seed: 1);
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
var bar = gbm.Next();
|
||||
p.Update(new TValue(bar.Time, bar.Close));
|
||||
}
|
||||
Assert.True(p.IsHot);
|
||||
Assert.True(double.IsFinite(p.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BasicCalc_LinearInput_Degree1_MatchesLinearTrend()
|
||||
{
|
||||
// For perfectly linear data y=i with period=5, degree=1,
|
||||
// the linear fit should reproduce the last value y=4 (value at i=4).
|
||||
var p = new Polyfit(5, 1);
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
p.Update(new TValue(DateTime.UtcNow.AddSeconds(i), (double)i));
|
||||
}
|
||||
// Linear regression: slope=1, passes through points 0..4
|
||||
// P(1.0 normalized) = y at x=1.0 = 4.0
|
||||
Assert.Equal(4.0, p.Last.Value, 1e-9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BasicCalc_ConstantInput_ReturnsConstant()
|
||||
{
|
||||
var p = new Polyfit(5, 2);
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
p.Update(new TValue(DateTime.UtcNow.AddSeconds(i), 42.0));
|
||||
}
|
||||
Assert.Equal(42.0, p.Last.Value, 1e-9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BasicCalc_NotHotBeforeWarmup()
|
||||
{
|
||||
var p = new Polyfit(5, 2);
|
||||
Assert.False(p.IsHot);
|
||||
p.Update(new TValue(DateTime.UtcNow, 10.0));
|
||||
Assert.False(p.IsHot);
|
||||
}
|
||||
|
||||
// ── C) State + bar correction (isNew) ────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void IsNewTrue_AdvancesBuffer()
|
||||
{
|
||||
var p = new Polyfit(5, 2);
|
||||
var gbm = new GBM(100, 0.05, 0.2, seed: 2);
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
var bar = gbm.Next();
|
||||
p.Update(new TValue(bar.Time, bar.Close));
|
||||
}
|
||||
double v1 = p.Last.Value;
|
||||
|
||||
// Adding a new bar with extreme value changes the result
|
||||
p.Update(new TValue(DateTime.UtcNow.AddSeconds(5), 200.0));
|
||||
double v2 = p.Last.Value;
|
||||
Assert.NotEqual(v1, v2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsNewFalse_CorrectsBars_RestoresExactly()
|
||||
{
|
||||
var p = new Polyfit(5, 2);
|
||||
double[] vals = [10.0, 20.0, 30.0, 40.0, 50.0];
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
p.Update(new TValue(DateTime.UtcNow.AddSeconds(i), vals[i]));
|
||||
}
|
||||
double original = p.Last.Value;
|
||||
|
||||
// Overwrite current bar with different value
|
||||
p.Update(new TValue(DateTime.UtcNow.AddSeconds(4), 9999.0), isNew: false);
|
||||
Assert.NotEqual(original, p.Last.Value);
|
||||
|
||||
// Restore — must exactly match original
|
||||
p.Update(new TValue(DateTime.UtcNow.AddSeconds(4), vals[4]), isNew: false);
|
||||
Assert.Equal(original, p.Last.Value, 1e-9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IterativeCorrections_FinalMatchesOriginal()
|
||||
{
|
||||
var p = new Polyfit(5, 2);
|
||||
double[] vals = [10, 20, 30, 40, 50];
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
p.Update(new TValue(DateTime.UtcNow.AddSeconds(i), vals[i]));
|
||||
}
|
||||
double original = p.Last.Value;
|
||||
|
||||
for (int iter = 0; iter < 5; iter++)
|
||||
{
|
||||
p.Update(new TValue(DateTime.UtcNow.AddSeconds(4), 999.0), isNew: false);
|
||||
p.Update(new TValue(DateTime.UtcNow.AddSeconds(4), 50.0), isNew: false);
|
||||
}
|
||||
Assert.Equal(original, p.Last.Value, 1e-9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Reset_ClearsAllState()
|
||||
{
|
||||
var p = new Polyfit(5, 2);
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
p.Update(new TValue(DateTime.UtcNow.AddSeconds(i), (double)(i + 1) * 10));
|
||||
}
|
||||
Assert.True(p.IsHot);
|
||||
|
||||
p.Reset();
|
||||
Assert.False(p.IsHot);
|
||||
Assert.Equal(default, p.Last);
|
||||
}
|
||||
|
||||
// ── D) Warmup / convergence ───────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void IsHot_FlipsAtPeriod()
|
||||
{
|
||||
var p = new Polyfit(4, 2);
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
p.Update(new TValue(DateTime.UtcNow.AddSeconds(i), 10.0));
|
||||
Assert.False(p.IsHot);
|
||||
}
|
||||
p.Update(new TValue(DateTime.UtcNow.AddSeconds(3), 10.0));
|
||||
Assert.True(p.IsHot);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WarmupPeriod_MatchesConstructorPeriod()
|
||||
{
|
||||
var p = new Polyfit(12, 3);
|
||||
Assert.Equal(12, p.WarmupPeriod);
|
||||
}
|
||||
|
||||
// ── E) Robustness: NaN / Infinity ─────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void NaN_SubstitutesLastValid()
|
||||
{
|
||||
var p = new Polyfit(5, 2);
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
p.Update(new TValue(DateTime.UtcNow.AddSeconds(i), 10.0 + i));
|
||||
}
|
||||
p.Update(new TValue(DateTime.UtcNow.AddSeconds(4), double.NaN));
|
||||
Assert.True(double.IsFinite(p.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Infinity_SubstitutesLastValid()
|
||||
{
|
||||
var p = new Polyfit(5, 2);
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
p.Update(new TValue(DateTime.UtcNow.AddSeconds(i), 10.0));
|
||||
}
|
||||
p.Update(new TValue(DateTime.UtcNow.AddSeconds(4), double.PositiveInfinity));
|
||||
Assert.True(double.IsFinite(p.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BatchNaN_Safe()
|
||||
{
|
||||
double[] src = [10, 20, double.NaN, 30, 40, double.NaN, 50];
|
||||
double[] dst = new double[src.Length];
|
||||
Polyfit.Batch(src, dst, period: 5, degree: 2);
|
||||
// All outputs should be finite (NaN substituted by last valid)
|
||||
for (int i = 0; i < src.Length; i++)
|
||||
{
|
||||
Assert.True(double.IsFinite(dst[i]) || dst[i] == 0);
|
||||
}
|
||||
}
|
||||
|
||||
// ── F) Consistency: batch == streaming == span == eventing ───────────────
|
||||
|
||||
[Fact]
|
||||
public void AllModes_Consistent()
|
||||
{
|
||||
int period = 7;
|
||||
int degree = 2;
|
||||
int dataLen = 40;
|
||||
var gbm = new GBM(100, 0.05, 0.2, seed: 99);
|
||||
var series = new TSeries();
|
||||
for (int i = 0; i < dataLen; i++)
|
||||
{
|
||||
var bar = gbm.Next();
|
||||
series.Add(new TValue(bar.Time, bar.Close));
|
||||
}
|
||||
|
||||
// 1. Batch (TSeries)
|
||||
var batchResult = Polyfit.Batch(series, period, degree);
|
||||
|
||||
// 2. Streaming (separate GBM reset to same seed)
|
||||
var streaming = new Polyfit(period, degree);
|
||||
for (int i = 0; i < dataLen; i++)
|
||||
{
|
||||
streaming.Update(series[i]);
|
||||
}
|
||||
|
||||
// 3. Span
|
||||
double[] spanOut = new double[dataLen];
|
||||
Polyfit.Batch(series.Values, spanOut.AsSpan(), period, degree);
|
||||
|
||||
// Compare batch vs span for all hot values
|
||||
for (int i = period - 1; i < dataLen; i++)
|
||||
{
|
||||
Assert.Equal(batchResult[i].Value, spanOut[i], 1e-9);
|
||||
}
|
||||
|
||||
// Final value: streaming == batch
|
||||
Assert.Equal(batchResult[dataLen - 1].Value, streaming.Last.Value, 1e-9);
|
||||
}
|
||||
|
||||
// ── G) Span API ───────────────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void SpanAPI_WrongLength_Throws()
|
||||
{
|
||||
double[] src = [1, 2, 3, 4, 5];
|
||||
double[] dst = new double[4];
|
||||
var ex = Assert.Throws<ArgumentException>(() =>
|
||||
Polyfit.Batch(src.AsSpan(), dst.AsSpan(), period: 3, degree: 2));
|
||||
Assert.Equal("output", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SpanAPI_PeriodLessThan2_Throws()
|
||||
{
|
||||
double[] src = [1, 2, 3];
|
||||
double[] dst = new double[3];
|
||||
var ex = Assert.Throws<ArgumentException>(() =>
|
||||
Polyfit.Batch(src.AsSpan(), dst.AsSpan(), period: 1, degree: 2));
|
||||
Assert.Equal("period", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SpanAPI_DegreeLessThan1_Throws()
|
||||
{
|
||||
double[] src = [1, 2, 3];
|
||||
double[] dst = new double[3];
|
||||
var ex = Assert.Throws<ArgumentException>(() =>
|
||||
Polyfit.Batch(src.AsSpan(), dst.AsSpan(), period: 3, degree: 0));
|
||||
Assert.Equal("degree", ex.ParamName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SpanAPI_LargeData_NoStackOverflow()
|
||||
{
|
||||
int n = 2000;
|
||||
double[] src = new double[n];
|
||||
var gbm = new GBM(100, 0.05, 0.2, seed: 7);
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
src[i] = gbm.Next().Close;
|
||||
}
|
||||
double[] dst = new double[n];
|
||||
// period=300 > StackallocThreshold(256) → uses ArrayPool path
|
||||
Polyfit.Batch(src.AsSpan(), dst.AsSpan(), period: 300, degree: 2);
|
||||
Assert.True(double.IsFinite(dst[n - 1]));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SpanAPI_MatchesTSeries()
|
||||
{
|
||||
int period = 6;
|
||||
int degree = 2;
|
||||
var gbm = new GBM(100, 0.05, 0.2, seed: 55);
|
||||
var series = new TSeries();
|
||||
for (int i = 0; i < 30; i++)
|
||||
{
|
||||
var bar = gbm.Next();
|
||||
series.Add(new TValue(bar.Time, bar.Close));
|
||||
}
|
||||
|
||||
var batchResult = Polyfit.Batch(series, period, degree);
|
||||
double[] spanOut = new double[30];
|
||||
Polyfit.Batch(series.Values, spanOut.AsSpan(), period, degree);
|
||||
|
||||
for (int i = period - 1; i < 30; i++)
|
||||
{
|
||||
Assert.Equal(batchResult[i].Value, spanOut[i], 1e-9);
|
||||
}
|
||||
}
|
||||
|
||||
// ── H) Chainability ───────────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void EventFires_OnUpdate()
|
||||
{
|
||||
var p = new Polyfit(3, 1);
|
||||
int eventCount = 0;
|
||||
p.Pub += (_, in args) => eventCount++;
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
p.Update(new TValue(DateTime.UtcNow.AddSeconds(i), 10.0));
|
||||
}
|
||||
Assert.Equal(3, eventCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Chaining_WorksCorrectly()
|
||||
{
|
||||
var sma = new Sma(3);
|
||||
var poly = new Polyfit(sma, 5, 2);
|
||||
Assert.False(poly.IsHot);
|
||||
for (int i = 0; i < 7; i++)
|
||||
{
|
||||
sma.Update(new TValue(DateTime.UtcNow.AddSeconds(i), 10.0 + i));
|
||||
}
|
||||
Assert.True(poly.IsHot);
|
||||
}
|
||||
|
||||
// ── I) Degree=1 matches LSMA / linear regression ─────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Degree1_MatchesLinearRegression()
|
||||
{
|
||||
int period = 5;
|
||||
var poly = new Polyfit(period, 1);
|
||||
var lsma = new Lsma(period);
|
||||
|
||||
var gbm = new GBM(100, 0.05, 0.2, seed: 42);
|
||||
for (int i = 0; i < 30; i++)
|
||||
{
|
||||
var bar = gbm.Next();
|
||||
var tv = new TValue(bar.Time, bar.Close);
|
||||
poly.Update(tv);
|
||||
lsma.Update(tv);
|
||||
}
|
||||
// Degree=1 polynomial fit == linear regression endpoint
|
||||
Assert.Equal(lsma.Last.Value, poly.Last.Value, 1e-6);
|
||||
}
|
||||
|
||||
// ── J) Quadratic captures curvature ──────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Degree2_QuadraticData_MatchesExact()
|
||||
{
|
||||
// Data: y_i = (i/(n-1))^2 for i=0..n-1, n=5
|
||||
// Quadratic fit should be exact → P(1.0) = 1.0^2 = 1.0
|
||||
var p = new Polyfit(5, 2);
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
double xi = i / 4.0;
|
||||
p.Update(new TValue(DateTime.UtcNow.AddSeconds(i), xi * xi));
|
||||
}
|
||||
Assert.Equal(1.0, p.Last.Value, 1e-9);
|
||||
}
|
||||
|
||||
// ── K) Prime() – stateful priming ─────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Prime_SetsState()
|
||||
{
|
||||
var p = new Polyfit(5, 2);
|
||||
double[] primeData = [10.0, 20.0, 30.0, 40.0, 50.0];
|
||||
p.Prime(primeData);
|
||||
Assert.True(p.IsHot);
|
||||
Assert.True(double.IsFinite(p.Last.Value));
|
||||
}
|
||||
|
||||
// ── L) Calculate static method ────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Calculate_StaticMethod_ReturnsBoth()
|
||||
{
|
||||
var gbm = new GBM(100, 0.05, 0.2, seed: 7);
|
||||
var series = new TSeries();
|
||||
for (int i = 0; i < 25; i++)
|
||||
{
|
||||
var bar = gbm.Next();
|
||||
series.Add(new TValue(bar.Time, bar.Close));
|
||||
}
|
||||
var (results, indicator) = Polyfit.Calculate(series, period: 10, degree: 2);
|
||||
Assert.NotNull(results);
|
||||
Assert.NotNull(indicator);
|
||||
Assert.Equal(25, results.Count);
|
||||
Assert.True(indicator.IsHot);
|
||||
}
|
||||
|
||||
// ── M) Various degrees ────────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Degree3_Cubic_ReturnsFinite()
|
||||
{
|
||||
var p = new Polyfit(10, 3);
|
||||
var gbm = new GBM(100, 0.05, 0.2, seed: 101);
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
p.Update(new TValue(DateTime.UtcNow.AddSeconds(i), gbm.Next().Close));
|
||||
}
|
||||
Assert.True(p.IsHot);
|
||||
Assert.True(double.IsFinite(p.Last.Value));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Degree6_MaxDegree_ReturnsFinite()
|
||||
{
|
||||
var p = new Polyfit(10, 6);
|
||||
var gbm = new GBM(100, 0.05, 0.2, seed: 202);
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
p.Update(new TValue(DateTime.UtcNow.AddSeconds(i), gbm.Next().Close));
|
||||
}
|
||||
Assert.True(p.IsHot);
|
||||
Assert.True(double.IsFinite(p.Last.Value));
|
||||
}
|
||||
|
||||
// ── N) Update(TSeries) round-trip ────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void UpdateTSeries_MatchesBatch()
|
||||
{
|
||||
int period = 8;
|
||||
int degree = 2;
|
||||
var gbm = new GBM(100, 0.05, 0.2, seed: 77);
|
||||
var series = new TSeries();
|
||||
for (int i = 0; i < 30; i++)
|
||||
{
|
||||
var bar = gbm.Next();
|
||||
series.Add(new TValue(bar.Time, bar.Close));
|
||||
}
|
||||
|
||||
var p = new Polyfit(period, degree);
|
||||
var result = p.Update(series);
|
||||
var batchResult = Polyfit.Batch(series, period, degree);
|
||||
|
||||
for (int i = 0; i < 30; i++)
|
||||
{
|
||||
Assert.Equal(batchResult[i].Value, result[i].Value, 1e-9);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,277 @@
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Validation tests for Polyfit against manual OLS computations and mathematical identities.
|
||||
/// No external library (Skender/TA-Lib/Tulip/Ooples) implements polynomial regression of
|
||||
/// variable degree, so validation is against closed-form solutions and known identities.
|
||||
/// </summary>
|
||||
public class PolyfitValidationTests
|
||||
{
|
||||
// ── 1. Streaming vs Batch vs Span consistency ─────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Streaming_Batch_Span_Consistent()
|
||||
{
|
||||
int period = 10;
|
||||
int degree = 2;
|
||||
int dataLen = 50;
|
||||
var gbm = new GBM(100, 0.05, 0.2, seed: 42);
|
||||
var series = new TSeries();
|
||||
for (int i = 0; i < dataLen; i++)
|
||||
{
|
||||
var bar = gbm.Next();
|
||||
series.Add(new TValue(bar.Time, bar.Close));
|
||||
}
|
||||
|
||||
// Streaming
|
||||
var streaming = new Polyfit(period, degree);
|
||||
double[] streamVals = new double[dataLen];
|
||||
for (int i = 0; i < dataLen; i++)
|
||||
{
|
||||
streaming.Update(series[i]);
|
||||
streamVals[i] = streaming.Last.Value;
|
||||
}
|
||||
|
||||
// Batch TSeries
|
||||
var batchResult = Polyfit.Batch(series, period, degree);
|
||||
|
||||
// Span
|
||||
double[] spanOut = new double[dataLen];
|
||||
Polyfit.Batch(series.Values, spanOut.AsSpan(), period, degree);
|
||||
|
||||
// All modes must agree at every hot position
|
||||
for (int i = period - 1; i < dataLen; i++)
|
||||
{
|
||||
Assert.Equal(streamVals[i], batchResult[i].Value, 1e-9);
|
||||
Assert.Equal(streamVals[i], spanOut[i], 1e-9);
|
||||
}
|
||||
}
|
||||
|
||||
// ── 2. Known values: degree=1 matches closed-form linear regression ────────
|
||||
|
||||
[Fact]
|
||||
public void Degree1_KnownValues_MatchOlsLinearRegression()
|
||||
{
|
||||
// For y = [1,2,3,4,5] with x_norm = [0, 0.25, 0.5, 0.75, 1.0]:
|
||||
// Linear fit: b1=(n*Σxy-Σx*Σy)/(n*Σx²-Σx²), b0=Ȳ-b1*x̄
|
||||
// P(1.0) for y=1..5 → value at the endpoint = 5 (perfect linear fit)
|
||||
var p = new Polyfit(5, 1);
|
||||
for (int i = 1; i <= 5; i++)
|
||||
{
|
||||
p.Update(new TValue(DateTime.UtcNow.AddSeconds(i), (double)i));
|
||||
}
|
||||
Assert.Equal(5.0, p.Last.Value, 1e-9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Degree1_ReverseLinear_MatchesEndpoint()
|
||||
{
|
||||
// y = 5,4,3,2,1 → P(1.0) = 1.0 (last value)
|
||||
var p = new Polyfit(5, 1);
|
||||
for (int i = 5; i >= 1; i--)
|
||||
{
|
||||
p.Update(new TValue(DateTime.UtcNow.AddSeconds(5 - i), (double)i));
|
||||
}
|
||||
Assert.Equal(1.0, p.Last.Value, 1e-9);
|
||||
}
|
||||
|
||||
// ── 3. Degree=2 exact quadratic recovery ──────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Degree2_ExactQuadratic_RecoverCoefficients()
|
||||
{
|
||||
// y = 3 + 2*x + x^2 with x_norm in [0,1] over 5 points
|
||||
// P(1) = 3 + 2 + 1 = 6
|
||||
int n = 5;
|
||||
var p = new Polyfit(n, 2);
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
double x = i / (double)(n - 1);
|
||||
double y = Math.FusedMultiplyAdd(x, x, Math.FusedMultiplyAdd(2.0, x, 3.0));
|
||||
p.Update(new TValue(DateTime.UtcNow.AddSeconds(i), y));
|
||||
}
|
||||
Assert.Equal(6.0, p.Last.Value, 1e-9);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Degree2_PureQuadratic_RecoverEndpoint()
|
||||
{
|
||||
// y = x^2, n=11, x in [0,1] step 0.1 → P(1.0) = 1.0
|
||||
int n = 11;
|
||||
var p = new Polyfit(n, 2);
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
double x = i / (double)(n - 1);
|
||||
p.Update(new TValue(DateTime.UtcNow.AddSeconds(i), x * x));
|
||||
}
|
||||
Assert.Equal(1.0, p.Last.Value, 1e-9);
|
||||
}
|
||||
|
||||
// ── 4. Degree=3 exact cubic recovery ──────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Degree3_ExactCubic_RecoverEndpoint()
|
||||
{
|
||||
// y = x^3 with x_norm in [0,1], n=10 → P(1.0) = 1.0
|
||||
int n = 10;
|
||||
var p = new Polyfit(n, 3);
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
double x = i / (double)(n - 1);
|
||||
p.Update(new TValue(DateTime.UtcNow.AddSeconds(i), x * x * x));
|
||||
}
|
||||
Assert.Equal(1.0, p.Last.Value, 1e-9);
|
||||
}
|
||||
|
||||
// ── 5. Constant data trivially correct for all degrees ────────────────────
|
||||
|
||||
[Theory]
|
||||
[InlineData(1)]
|
||||
[InlineData(2)]
|
||||
[InlineData(3)]
|
||||
[InlineData(4)]
|
||||
public void ConstantData_AllDegrees_ReturnsConstant(int degree)
|
||||
{
|
||||
var p = new Polyfit(10, degree);
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
p.Update(new TValue(DateTime.UtcNow.AddSeconds(i), 100.0));
|
||||
}
|
||||
Assert.Equal(100.0, p.Last.Value, 1e-9);
|
||||
}
|
||||
|
||||
// ── 6. Degree=1 matches Lsma (offset=0) exactly ───────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Degree1_MatchesLsma_MultiBar()
|
||||
{
|
||||
int period = 10;
|
||||
var poly = new Polyfit(period, 1);
|
||||
var lsma = new Lsma(period);
|
||||
|
||||
var gbm = new GBM(100, 0.05, 0.2, seed: 123);
|
||||
for (int i = 0; i < 50; i++)
|
||||
{
|
||||
var bar = gbm.Next();
|
||||
var tv = new TValue(bar.Time, bar.Close);
|
||||
poly.Update(tv);
|
||||
lsma.Update(tv);
|
||||
|
||||
if (poly.IsHot)
|
||||
{
|
||||
// Polyfit(degree=1) == LSMA(offset=0): both are the lin-reg endpoint
|
||||
Assert.Equal(lsma.Last.Value, poly.Last.Value, 1e-6);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── 7. Higher degree fits better for polynomial data ──────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Degree2_FitsBetterThanDegree1_ForQuadraticSignal()
|
||||
{
|
||||
// Quadratic signal: degree=2 should recover the endpoint more accurately
|
||||
int n = 20;
|
||||
var series = new TSeries();
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
double x = i / (double)(n - 1);
|
||||
double y = x * x;
|
||||
series.Add(new TValue(DateTime.UtcNow.AddSeconds(i), y));
|
||||
}
|
||||
|
||||
var poly1 = new Polyfit(n, 1);
|
||||
var poly2 = new Polyfit(n, 2);
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
poly1.Update(series[i]);
|
||||
poly2.Update(series[i]);
|
||||
}
|
||||
|
||||
// Degree=2 should exactly reproduce y=1.0 for pure quadratic
|
||||
Assert.Equal(1.0, poly2.Last.Value, 1e-9);
|
||||
|
||||
// Degree=1 approximates but can't exactly match a quadratic
|
||||
double err1 = Math.Abs(poly1.Last.Value - 1.0);
|
||||
double err2 = Math.Abs(poly2.Last.Value - 1.0);
|
||||
Assert.True(err2 <= err1 + 1e-12);
|
||||
}
|
||||
|
||||
// ── 8. Rolling window correctness ─────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void RollingWindow_StreamingMatchesBatchAtEachBar()
|
||||
{
|
||||
int period = 6;
|
||||
int degree = 2;
|
||||
var gbm = new GBM(100, 0.05, 0.2, seed: 321);
|
||||
double[] allData = new double[25];
|
||||
DateTime[] allTimes = new DateTime[25];
|
||||
for (int i = 0; i < 25; i++)
|
||||
{
|
||||
var bar = gbm.Next();
|
||||
allData[i] = bar.Close;
|
||||
allTimes[i] = DateTime.UtcNow.AddSeconds(i);
|
||||
}
|
||||
|
||||
var streaming = new Polyfit(period, degree);
|
||||
for (int i = 0; i < 25; i++)
|
||||
{
|
||||
streaming.Update(new TValue(allTimes[i], allData[i]));
|
||||
|
||||
// At each bar, manually compute polyfit over the window ending at bar i
|
||||
int windowStart = Math.Max(0, i - period + 1);
|
||||
int windowLen = i - windowStart + 1;
|
||||
double[] window = allData[windowStart..(i + 1)];
|
||||
|
||||
double manualResult = Polyfit.ComputePolyfit(window, Math.Min(degree, windowLen - 1));
|
||||
Assert.Equal(manualResult, streaming.Last.Value, 1e-9);
|
||||
}
|
||||
}
|
||||
|
||||
// ── 9. Multiple periods with GBM data ─────────────────────────────────────
|
||||
|
||||
[Theory]
|
||||
[InlineData(5, 1)]
|
||||
[InlineData(10, 2)]
|
||||
[InlineData(20, 3)]
|
||||
[InlineData(14, 2)]
|
||||
public void GBMData_AllFinite(int period, int degree)
|
||||
{
|
||||
var gbm = new GBM(100, 0.05, 0.2, seed: period * 10 + degree);
|
||||
var p = new Polyfit(period, degree);
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
var bar = gbm.Next();
|
||||
p.Update(new TValue(bar.Time, bar.Close));
|
||||
if (p.IsHot)
|
||||
{
|
||||
Assert.True(double.IsFinite(p.Last.Value),
|
||||
$"Got non-finite at i={i}: {p.Last.Value}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── 10. Batch TSeries vs streaming at last value ───────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void BatchFinalValue_MatchesStreamingFinalValue()
|
||||
{
|
||||
int period = 8;
|
||||
int degree = 2;
|
||||
var gbm = new GBM(100, 0.05, 0.2, seed: 999);
|
||||
var series = new TSeries();
|
||||
var streaming = new Polyfit(period, degree);
|
||||
|
||||
for (int i = 0; i < 40; i++)
|
||||
{
|
||||
var bar = gbm.Next();
|
||||
var tv = new TValue(bar.Time, bar.Close);
|
||||
series.Add(tv);
|
||||
streaming.Update(tv);
|
||||
}
|
||||
|
||||
var batchResult = Polyfit.Batch(series, period, degree);
|
||||
Assert.Equal(batchResult[39].Value, streaming.Last.Value, 1e-9);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user