mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-22 20:48:04 +00:00
adding missing validations
This commit is contained in:
@@ -0,0 +1,193 @@
|
||||
using TradingPlatform.BusinessLayer;
|
||||
using QuanTAlib;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
|
||||
public sealed class PolyfitIndicatorTests
|
||||
{
|
||||
// ── 1. Constructor defaults ───────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void Constructor_DefaultValues()
|
||||
{
|
||||
var ind = new PolyfitIndicator();
|
||||
Assert.Equal(20, ind.Period);
|
||||
Assert.Equal(2, ind.Degree);
|
||||
Assert.True(ind.ShowColdValues);
|
||||
Assert.Equal("Polyfit - Polynomial Fitting", ind.Name);
|
||||
Assert.False(ind.SeparateWindow);
|
||||
Assert.True(ind.OnBackGround);
|
||||
Assert.Equal(SourceType.Close, ind.Source);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_ShortName_IncludesPeriodDegree()
|
||||
{
|
||||
var ind = new PolyfitIndicator { Period = 10, Degree = 3 };
|
||||
Assert.Equal("Polyfit 10,3", ind.ShortName);
|
||||
}
|
||||
|
||||
// ── 2. MinHistoryDepths ───────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void MinHistoryDepths_IsZero()
|
||||
{
|
||||
Assert.Equal(0, PolyfitIndicator.MinHistoryDepths);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MinHistoryDepths_InterfaceImplementation()
|
||||
{
|
||||
IWatchlistIndicator ind = new PolyfitIndicator();
|
||||
Assert.Equal(0, ind.MinHistoryDepths);
|
||||
}
|
||||
|
||||
// ── 3. Initialize creates internal indicator and line series ──────────────
|
||||
|
||||
[Fact]
|
||||
public void Initialize_CreatesLineSeries()
|
||||
{
|
||||
var ind = new PolyfitIndicator { Period = 10 };
|
||||
ind.Initialize();
|
||||
|
||||
Assert.Single(ind.LinesSeries);
|
||||
Assert.Equal("Polyfit", ind.LinesSeries[0].Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Initialize_CustomPeriodDegree()
|
||||
{
|
||||
var ind = new PolyfitIndicator { Period = 8, Degree = 3 };
|
||||
ind.Initialize();
|
||||
Assert.Equal("Polyfit 8,3", ind.ShortName);
|
||||
}
|
||||
|
||||
// ── 4. ProcessUpdate — historical data ────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void ProcessUpdate_HistoricalBars_ProducesFiniteValues()
|
||||
{
|
||||
var ind = new PolyfitIndicator { Period = 5, Degree = 2 };
|
||||
ind.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
ind.HistoricalData.AddBar(now.AddMinutes(i), 100 + i, 110 + i, 90 + i, 105 + i);
|
||||
var args = new UpdateArgs(UpdateReason.HistoricalBar);
|
||||
ind.ProcessUpdate(args);
|
||||
}
|
||||
|
||||
double val = ind.LinesSeries[0].GetValue(0);
|
||||
Assert.True(double.IsFinite(val));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ProcessUpdate_NewBar_UpdatesValue()
|
||||
{
|
||||
var ind = new PolyfitIndicator { Period = 5, Degree = 2 };
|
||||
ind.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
// Fill warmup with historical bars
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
ind.HistoricalData.AddBar(now.AddMinutes(i), 100 + i, 110 + i, 90 + i, 105 + i);
|
||||
ind.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
}
|
||||
|
||||
double val1 = ind.LinesSeries[0].GetValue(0);
|
||||
|
||||
// Add one more new bar
|
||||
ind.HistoricalData.AddBar(now.AddMinutes(5), 110, 120, 100, 115);
|
||||
ind.ProcessUpdate(new UpdateArgs(UpdateReason.NewBar));
|
||||
double val2 = ind.LinesSeries[0].GetValue(0);
|
||||
|
||||
Assert.True(double.IsFinite(val1));
|
||||
Assert.True(double.IsFinite(val2));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ProcessUpdate_SameBarUpdate_ProducesFiniteValue()
|
||||
{
|
||||
var ind = new PolyfitIndicator { Period = 5, Degree = 2 };
|
||||
ind.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
ind.HistoricalData.AddBar(now.AddMinutes(i), 100 + i, 110 + i, 90 + i, 105 + i);
|
||||
ind.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
}
|
||||
|
||||
// Non-new bar update (bar correction)
|
||||
ind.HistoricalData.AddBar(now.AddMinutes(4), 108, 118, 98, 112);
|
||||
ind.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
double val = ind.LinesSeries[0].GetValue(0);
|
||||
|
||||
Assert.True(double.IsFinite(val));
|
||||
}
|
||||
|
||||
// ── 5. Different source types ─────────────────────────────────────────────
|
||||
|
||||
[Theory]
|
||||
[InlineData(SourceType.Close)]
|
||||
[InlineData(SourceType.Open)]
|
||||
[InlineData(SourceType.High)]
|
||||
[InlineData(SourceType.Low)]
|
||||
[InlineData(SourceType.HL2)]
|
||||
public void DifferentSourceTypes_ProducesFiniteValues(SourceType sourceType)
|
||||
{
|
||||
var ind = new PolyfitIndicator { Period = 5, Degree = 2, Source = sourceType };
|
||||
ind.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
ind.HistoricalData.AddBar(now.AddMinutes(i), 100 + i, 110 + i, 90 + i, 105 + i);
|
||||
ind.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
}
|
||||
|
||||
double val = ind.LinesSeries[0].GetValue(0);
|
||||
Assert.True(double.IsFinite(val));
|
||||
}
|
||||
|
||||
// ── 6. Different degree variants ─────────────────────────────────────────
|
||||
|
||||
[Theory]
|
||||
[InlineData(1)]
|
||||
[InlineData(2)]
|
||||
[InlineData(3)]
|
||||
public void DifferentDegrees_ProducesFiniteValues(int degree)
|
||||
{
|
||||
var ind = new PolyfitIndicator { Period = 10, Degree = degree };
|
||||
ind.Initialize();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
ind.HistoricalData.AddBar(now.AddMinutes(i), 100 + i, 110 + i, 90 + i, 105 + i);
|
||||
ind.ProcessUpdate(new UpdateArgs(UpdateReason.HistoricalBar));
|
||||
}
|
||||
|
||||
double val = ind.LinesSeries[0].GetValue(0);
|
||||
Assert.True(double.IsFinite(val));
|
||||
Assert.True(val > 0, "Expected positive overlay value");
|
||||
}
|
||||
|
||||
// ── 7. SeparateWindow and SourceCodeLink ──────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public void SeparateWindow_IsFalse_Overlay()
|
||||
{
|
||||
var ind = new PolyfitIndicator();
|
||||
Assert.False(ind.SeparateWindow);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SourceCodeLink_ContainsPolyfit()
|
||||
{
|
||||
var ind = new PolyfitIndicator();
|
||||
Assert.Contains("Polyfit", ind.SourceCodeLink, StringComparison.Ordinal);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
using System.Drawing;
|
||||
using System.Runtime.CompilerServices;
|
||||
using TradingPlatform.BusinessLayer;
|
||||
|
||||
namespace QuanTAlib;
|
||||
|
||||
[SkipLocalsInit]
|
||||
public sealed class PolyfitIndicator : Indicator, IWatchlistIndicator
|
||||
{
|
||||
[InputParameter("Period", sortIndex: 1, 2, 2000, 1, 0)]
|
||||
public int Period { get; set; } = 20;
|
||||
|
||||
[InputParameter("Degree", sortIndex: 2, 1, 6, 1, 0)]
|
||||
public int Degree { get; set; } = 2;
|
||||
|
||||
[IndicatorExtensions.DataSourceInput]
|
||||
public SourceType Source { get; set; } = SourceType.Close;
|
||||
|
||||
[InputParameter("Show cold values", sortIndex: 21)]
|
||||
public bool ShowColdValues { get; set; } = true;
|
||||
|
||||
private Polyfit _polyfit = null!;
|
||||
private readonly LineSeries _series;
|
||||
private Func<IHistoryItem, double> _priceSelector = null!;
|
||||
|
||||
public static int MinHistoryDepths => 0;
|
||||
int IWatchlistIndicator.MinHistoryDepths => MinHistoryDepths;
|
||||
|
||||
public override string ShortName => $"Polyfit {Period},{Degree}";
|
||||
public override string SourceCodeLink => "https://github.com/mihakralj/QuanTAlib/blob/main/lib/statistics/polyfit/Polyfit.Quantower.cs";
|
||||
|
||||
public PolyfitIndicator()
|
||||
{
|
||||
OnBackGround = true;
|
||||
SeparateWindow = false;
|
||||
Name = "Polyfit - Polynomial Fitting";
|
||||
Description = "Rolling polynomial regression of configurable degree; returns fitted value at current bar";
|
||||
|
||||
_series = new LineSeries(name: "Polyfit", color: IndicatorExtensions.Statistics, width: 2, style: LineStyle.Solid);
|
||||
AddLineSeries(_series);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected override void OnInit()
|
||||
{
|
||||
_polyfit = new Polyfit(Period, Degree);
|
||||
_priceSelector = Source.GetPriceSelector();
|
||||
base.OnInit();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected override void OnUpdate(UpdateArgs args)
|
||||
{
|
||||
var item = this.HistoricalData[this.Count - 1, SeekOriginHistory.Begin];
|
||||
double value = _priceSelector(item);
|
||||
var time = this.HistoricalData.Time();
|
||||
|
||||
var input = new TValue(time, value);
|
||||
TValue result = _polyfit.Update(input, args.IsNewBar());
|
||||
|
||||
_series.SetValue(result.Value, _polyfit.IsHot, ShowColdValues);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,460 @@
|
||||
using System.Buffers;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace QuanTAlib;
|
||||
|
||||
/// <summary>
|
||||
/// Polyfit: Polynomial Fit (Regression) Moving Average
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Fits a degree-m polynomial y = a0 + a1*t + ... + am*t^m to the most recent
|
||||
/// N bars via least squares normal equations, where t is normalized to [0,1]
|
||||
/// (t=0 oldest bar, t=1 newest bar). Returns the fitted value at t=1.
|
||||
///
|
||||
/// Calculation: Accumulate (2m+1) power sums + (m+1) cross-products in O(N*m),
|
||||
/// solve (m+1)×(m+1) normal equations via Gaussian elimination with partial
|
||||
/// pivoting in O(m³). Degree is clamped to period-1. Min period = degree+1.
|
||||
///
|
||||
/// With degree=1 the result is identical to LSMA (linear regression endpoint).
|
||||
/// </remarks>
|
||||
/// <seealso href="Polyfit.md">Detailed documentation</seealso>
|
||||
[SkipLocalsInit]
|
||||
public sealed class Polyfit : AbstractBase
|
||||
{
|
||||
private readonly int _period;
|
||||
private readonly int _degree;
|
||||
private readonly RingBuffer _buffer;
|
||||
private readonly TValuePublishedHandler _handler;
|
||||
private ITValuePublisher? _source;
|
||||
private int _disposed;
|
||||
|
||||
[StructLayout(LayoutKind.Auto)]
|
||||
private record struct State(double LastVal, double LastValidValue);
|
||||
private State _state;
|
||||
private State _p_state;
|
||||
|
||||
private bool _isNew;
|
||||
|
||||
public int Degree => _degree;
|
||||
public override bool IsHot => _buffer.IsFull;
|
||||
public bool IsNew => _isNew;
|
||||
|
||||
/// <summary>
|
||||
/// Creates Polyfit with specified period and polynomial degree.
|
||||
/// </summary>
|
||||
/// <param name="period">Lookback window size (must be >= 2)</param>
|
||||
/// <param name="degree">Polynomial degree 1–6 (clamped to period-1)</param>
|
||||
public Polyfit(int period, int degree = 2)
|
||||
{
|
||||
if (period < 2)
|
||||
{
|
||||
throw new ArgumentException("Period must be at least 2", nameof(period));
|
||||
}
|
||||
if (degree < 1)
|
||||
{
|
||||
throw new ArgumentException("Degree must be at least 1", nameof(degree));
|
||||
}
|
||||
|
||||
_period = period;
|
||||
_degree = Math.Min(degree, period - 1);
|
||||
_buffer = new RingBuffer(period);
|
||||
Name = $"Polyfit({period},{_degree})";
|
||||
WarmupPeriod = period;
|
||||
_handler = Handle;
|
||||
_state.LastValidValue = double.NaN;
|
||||
}
|
||||
|
||||
public Polyfit(ITValuePublisher source, int period, int degree = 2) : this(period, degree)
|
||||
{
|
||||
_source = source ?? throw new ArgumentNullException(nameof(source));
|
||||
_source.Pub += _handler;
|
||||
}
|
||||
|
||||
private void Handle(object? sender, in TValueEventArgs e) => Update(e.Value, e.IsNew);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private double GetValidValue(double input)
|
||||
{
|
||||
if (double.IsFinite(input))
|
||||
{
|
||||
_state.LastValidValue = input;
|
||||
return input;
|
||||
}
|
||||
return _state.LastValidValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Solves the (m+1)×(m+1) normal equation system for polynomial regression of degree m.
|
||||
/// t-convention: data[0]=oldest (t=0/(n-1)), data[n-1]=newest (t=1).
|
||||
/// Returns the fitted value at t=1.0 (newest bar).
|
||||
/// </summary>
|
||||
/// <param name="data">Values oldest-first (data[0] = oldest, data[n-1] = newest)</param>
|
||||
/// <param name="count">Number of valid values in data</param>
|
||||
/// <param name="degree">Polynomial degree</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static double SolvePoly(ReadOnlySpan<double> data, int count, int degree)
|
||||
{
|
||||
int m = degree;
|
||||
int sz = m + 1;
|
||||
|
||||
// Power sums and cross products accumulate with normalized t ∈ [0, 1].
|
||||
// Max degree=6 → sz=7, matrix=7*8=56 doubles + powSums=13 + crossSums=7 — all stackalloc safe.
|
||||
Span<double> powSums = stackalloc double[2 * m + 1];
|
||||
Span<double> crossSums = stackalloc double[sz];
|
||||
Span<double> aug = stackalloc double[sz * (sz + 1)]; // augmented matrix row-major
|
||||
|
||||
powSums.Clear();
|
||||
crossSums.Clear();
|
||||
aug.Clear();
|
||||
|
||||
double tScale = count > 1 ? 1.0 / (count - 1) : 0.0;
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
double v = data[i];
|
||||
double t = i * tScale; // t=0 for oldest (i=0), t=1 for newest (i=count-1)
|
||||
double tk = 1.0;
|
||||
for (int k = 0; k <= 2 * m; k++)
|
||||
{
|
||||
powSums[k] += tk;
|
||||
tk *= t;
|
||||
}
|
||||
tk = 1.0;
|
||||
for (int k = 0; k <= m; k++)
|
||||
{
|
||||
crossSums[k] = Math.FusedMultiplyAdd(tk, v, crossSums[k]);
|
||||
tk *= t;
|
||||
}
|
||||
}
|
||||
|
||||
// Build augmented matrix: G[row,col] = powSums[row+col], rhs[row] = crossSums[row]
|
||||
int stride = sz + 1;
|
||||
for (int row = 0; row < sz; row++)
|
||||
{
|
||||
for (int col = 0; col < sz; col++)
|
||||
{
|
||||
aug[row * stride + col] = powSums[row + col];
|
||||
}
|
||||
aug[row * stride + sz] = crossSums[row];
|
||||
}
|
||||
|
||||
// Gaussian elimination with partial pivoting
|
||||
for (int col = 0; col < sz; col++)
|
||||
{
|
||||
int pivotRow = col;
|
||||
double pivotMax = Math.Abs(aug[col * stride + col]);
|
||||
for (int row = col + 1; row < sz; row++)
|
||||
{
|
||||
double absVal = Math.Abs(aug[row * stride + col]);
|
||||
if (absVal > pivotMax)
|
||||
{
|
||||
pivotMax = absVal;
|
||||
pivotRow = row;
|
||||
}
|
||||
}
|
||||
|
||||
if (pivotMax < 1e-12)
|
||||
{
|
||||
return double.NaN; // Singular — caller substitutes raw price
|
||||
}
|
||||
|
||||
if (pivotRow != col)
|
||||
{
|
||||
int colOff = col * stride;
|
||||
int pivOff = pivotRow * stride;
|
||||
for (int k = col; k <= sz; k++)
|
||||
{
|
||||
(aug[colOff + k], aug[pivOff + k]) = (aug[pivOff + k], aug[colOff + k]);
|
||||
}
|
||||
}
|
||||
|
||||
double diag = aug[col * stride + col];
|
||||
for (int row = col + 1; row < sz; row++)
|
||||
{
|
||||
double factor = aug[row * stride + col] / diag;
|
||||
for (int k = col; k <= sz; k++)
|
||||
{
|
||||
aug[row * stride + k] = Math.FusedMultiplyAdd(-factor, aug[col * stride + k], aug[row * stride + k]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Back-substitution → coefficients a[0..m]
|
||||
Span<double> a = stackalloc double[sz];
|
||||
for (int row = sz - 1; row >= 0; row--)
|
||||
{
|
||||
double val = aug[row * stride + sz];
|
||||
for (int k = row + 1; k < sz; k++)
|
||||
{
|
||||
val = Math.FusedMultiplyAdd(-aug[row * stride + k], a[k], val);
|
||||
}
|
||||
a[row] = val / aug[row * stride + row];
|
||||
}
|
||||
|
||||
// Evaluate polynomial at t=1: P(1) = a0 + a1 + a2 + ... + am
|
||||
double result = 0.0;
|
||||
for (int k = 0; k < sz; k++)
|
||||
{
|
||||
result += a[k];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Public entry point for the validation tests: accepts oldest-first data,
|
||||
/// returns the polynomial fit evaluated at t=1 (the newest bar endpoint).
|
||||
/// </summary>
|
||||
public static double ComputePolyfit(ReadOnlySpan<double> data, int degree)
|
||||
{
|
||||
if (data.Length < 1)
|
||||
{
|
||||
return double.NaN;
|
||||
}
|
||||
int m = Math.Min(degree, data.Length - 1);
|
||||
if (m < 1)
|
||||
{
|
||||
return data[^1];
|
||||
}
|
||||
return SolvePoly(data, data.Length, m);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override TValue Update(TValue input, bool isNew = true)
|
||||
{
|
||||
_isNew = isNew;
|
||||
if (isNew)
|
||||
{
|
||||
_p_state = _state;
|
||||
double val = GetValidValue(input.Value);
|
||||
_buffer.Add(val);
|
||||
_state.LastVal = val;
|
||||
}
|
||||
else
|
||||
{
|
||||
_state.LastValidValue = _p_state.LastValidValue;
|
||||
double val = GetValidValue(input.Value);
|
||||
_buffer.UpdateNewest(val);
|
||||
_state.LastVal = val;
|
||||
}
|
||||
|
||||
double result;
|
||||
int count = _buffer.Count;
|
||||
int minPoints = _degree + 1;
|
||||
if (count < minPoints)
|
||||
{
|
||||
result = _buffer.Newest;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Get buffer in chronological oldest-first order for SolvePoly
|
||||
const int StackAllocThreshold = 256;
|
||||
double[]? rented = count > StackAllocThreshold ? ArrayPool<double>.Shared.Rent(count) : null;
|
||||
Span<double> data = rented != null
|
||||
? rented.AsSpan(0, count)
|
||||
: stackalloc double[count];
|
||||
|
||||
try
|
||||
{
|
||||
// RingBuffer.GetSpan() returns oldest-first — matches SolvePoly t=0..1 convention
|
||||
_buffer.GetSpan().CopyTo(data);
|
||||
|
||||
double solved = SolvePoly(data, count, _degree);
|
||||
result = double.IsFinite(solved) ? solved : _buffer.Newest;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (rented != null)
|
||||
{
|
||||
ArrayPool<double>.Shared.Return(rented);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Last = new TValue(input.Time, result);
|
||||
PubEvent(Last, isNew);
|
||||
return Last;
|
||||
}
|
||||
|
||||
public override TSeries Update(TSeries source)
|
||||
{
|
||||
if (source.Count == 0)
|
||||
{
|
||||
return new TSeries([], []);
|
||||
}
|
||||
|
||||
int len = source.Count;
|
||||
var t = new List<long>(len);
|
||||
var v = new List<double>(len);
|
||||
CollectionsMarshal.SetCount(t, len);
|
||||
CollectionsMarshal.SetCount(v, len);
|
||||
|
||||
var tSpan = CollectionsMarshal.AsSpan(t);
|
||||
var vSpan = CollectionsMarshal.AsSpan(v);
|
||||
|
||||
double initialLastValid = _state.LastValidValue;
|
||||
Batch(source.Values, vSpan, _period, _degree, initialLastValid);
|
||||
source.Times.CopyTo(tSpan);
|
||||
|
||||
// Restore streaming state by replaying last 'period' bars
|
||||
int windowSize = Math.Min(len, _period);
|
||||
int startIndex = len - windowSize;
|
||||
|
||||
Reset();
|
||||
|
||||
if (startIndex > 0)
|
||||
{
|
||||
for (int i = startIndex - 1; i >= 0; i--)
|
||||
{
|
||||
if (double.IsFinite(source.Values[i]))
|
||||
{
|
||||
_state.LastValidValue = source.Values[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_state.LastValidValue = initialLastValid;
|
||||
}
|
||||
|
||||
for (int i = startIndex; i < len; i++)
|
||||
{
|
||||
double val = GetValidValue(source.Values[i]);
|
||||
_buffer.Add(val);
|
||||
_state.LastVal = val;
|
||||
}
|
||||
_p_state = _state;
|
||||
|
||||
Last = new TValue(tSpan[len - 1], vSpan[len - 1]);
|
||||
return new TSeries(t, v);
|
||||
}
|
||||
|
||||
public override void Prime(ReadOnlySpan<double> source, TimeSpan? step = null)
|
||||
{
|
||||
foreach (var value in source)
|
||||
{
|
||||
Update(new TValue(DateTime.MinValue, value));
|
||||
}
|
||||
}
|
||||
|
||||
public static TSeries Batch(TSeries source, int period, int degree = 2)
|
||||
{
|
||||
var pf = new Polyfit(period, degree);
|
||||
return pf.Update(source);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates Polyfit in-place, writing results to pre-allocated output span.
|
||||
/// Zero-allocation method for maximum performance. Data oldest-first.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Batch(ReadOnlySpan<double> source, Span<double> output, int period, int degree = 2, double initialLastValid = double.NaN)
|
||||
{
|
||||
if (source.Length != output.Length)
|
||||
{
|
||||
throw new ArgumentException("Source and output must have the same length", nameof(output));
|
||||
}
|
||||
if (period < 2)
|
||||
{
|
||||
throw new ArgumentException("Period must be at least 2", nameof(period));
|
||||
}
|
||||
if (degree < 1)
|
||||
{
|
||||
throw new ArgumentException("Degree must be at least 1", nameof(degree));
|
||||
}
|
||||
|
||||
int m = Math.Min(degree, period - 1);
|
||||
int len = source.Length;
|
||||
if (len == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
const int StackAllocThreshold = 256;
|
||||
|
||||
double[]? rentedClean = len > StackAllocThreshold ? ArrayPool<double>.Shared.Rent(len) : null;
|
||||
Span<double> clean = rentedClean != null
|
||||
? rentedClean.AsSpan(0, len)
|
||||
: stackalloc double[len];
|
||||
|
||||
double[]? rentedData = period > StackAllocThreshold ? ArrayPool<double>.Shared.Rent(period) : null;
|
||||
Span<double> dataBuffer = rentedData != null
|
||||
? rentedData.AsSpan(0, period)
|
||||
: stackalloc double[period];
|
||||
|
||||
try
|
||||
{
|
||||
// Build NaN-corrected array (oldest-first matches source order)
|
||||
double lastValid = initialLastValid;
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
double val = source[i];
|
||||
if (double.IsFinite(val))
|
||||
{
|
||||
lastValid = val;
|
||||
clean[i] = val;
|
||||
}
|
||||
else
|
||||
{
|
||||
clean[i] = double.IsFinite(lastValid) ? lastValid : 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
int minPoints = m + 1;
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
int n = Math.Min(i + 1, period);
|
||||
if (n < minPoints)
|
||||
{
|
||||
output[i] = clean[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
// Window is clean[i-n+1..i] already oldest-first
|
||||
Span<double> data = dataBuffer[..n];
|
||||
clean.Slice(i - n + 1, n).CopyTo(data);
|
||||
|
||||
double solved = SolvePoly(data, n, m);
|
||||
output[i] = double.IsFinite(solved) ? solved : clean[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (rentedClean != null)
|
||||
{
|
||||
ArrayPool<double>.Shared.Return(rentedClean);
|
||||
}
|
||||
if (rentedData != null)
|
||||
{
|
||||
ArrayPool<double>.Shared.Return(rentedData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static (TSeries Results, Polyfit Indicator) Calculate(TSeries source, int period, int degree = 2)
|
||||
{
|
||||
var indicator = new Polyfit(period, degree);
|
||||
TSeries results = indicator.Update(source);
|
||||
return (results, indicator);
|
||||
}
|
||||
|
||||
public override void Reset()
|
||||
{
|
||||
_buffer.Clear();
|
||||
_state = default;
|
||||
_state.LastValidValue = double.NaN;
|
||||
_p_state = default;
|
||||
Last = default;
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (Interlocked.CompareExchange(ref _disposed, 1, 0) == 0 && _source != null)
|
||||
{
|
||||
_source.Pub -= _handler;
|
||||
_source = null;
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user