mirror of
https://github.com/mihakralj/QuanTAlib.git
synced 2026-08-26 06:18:05 +00:00
validation and profiles
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
using OoplesFinance.StockIndicators;
|
||||
using OoplesFinance.StockIndicators.Models;
|
||||
using Tulip;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace QuanTAlib.Tests;
|
||||
@@ -186,4 +189,108 @@ public sealed class TsfValidationTests : IDisposable
|
||||
Assert.Equal(expectedLast, tsf.Last.Value, 1e-6);
|
||||
_output.WriteLine("TSF bar correction consistency verified");
|
||||
}
|
||||
|
||||
// ── Tulip Cross-Validation ─────────────────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// Validates TSF against Tulip <c>tsf</c> (Time Series Forecast).
|
||||
/// Tulip formula: linear regression value projected one period forward —
|
||||
/// identical to QuanTAlib TSF = slope*(n-1+1) + intercept = Lsma(offset=1).
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Tsf_Matches_Tulip_Batch()
|
||||
{
|
||||
const int period = 14;
|
||||
double[] data = _testData.RawData.ToArray();
|
||||
|
||||
var qResult = global::QuanTAlib.Tsf.Batch(_testData.Data, period);
|
||||
|
||||
var tulipIndicator = Tulip.Indicators.tsf;
|
||||
double[][] inputs = { data };
|
||||
double[] options = { period };
|
||||
int lookback = tulipIndicator.Start(options);
|
||||
double[][] outputs = { new double[data.Length - lookback] };
|
||||
tulipIndicator.Run(inputs, options, outputs);
|
||||
double[] tResult = outputs[0];
|
||||
|
||||
ValidationHelper.VerifyData(qResult, tResult, lookback, tolerance: 1e-9);
|
||||
_output.WriteLine("TSF Batch validated against Tulip tsf");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Tsf_Matches_Tulip_Streaming()
|
||||
{
|
||||
const int period = 20;
|
||||
double[] data = _testData.RawData.ToArray();
|
||||
|
||||
var tsf = new global::QuanTAlib.Tsf(period);
|
||||
var qResults = new List<double>();
|
||||
foreach (var item in _testData.Data)
|
||||
{
|
||||
qResults.Add(tsf.Update(item).Value);
|
||||
}
|
||||
|
||||
var tulipIndicator = Tulip.Indicators.tsf;
|
||||
double[][] inputs = { data };
|
||||
double[] options = { period };
|
||||
int lookback = tulipIndicator.Start(options);
|
||||
double[][] outputs = { new double[data.Length - lookback] };
|
||||
tulipIndicator.Run(inputs, options, outputs);
|
||||
double[] tResult = outputs[0];
|
||||
|
||||
// Tolerance relaxed to 1e-8: floating-point accumulation over ~5000 bars produces
|
||||
// up to ~4e-9 drift between streaming (incremental) and batch (single-pass) paths.
|
||||
ValidationHelper.VerifyData(qResults, tResult, lookback, tolerance: 1e-8);
|
||||
_output.WriteLine("TSF Streaming validated against Tulip tsf");
|
||||
}
|
||||
|
||||
// ── Cross-library: OoplesFinance ────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// Structural validation against Ooples <c>CalculateTimeSeriesForecast</c>.
|
||||
/// Ooples TSF uses the same linear-regression-forecast-one-bar-ahead definition.
|
||||
/// Numeric equality is not asserted: Ooples default period is 500 (batch-oriented),
|
||||
/// so at period=14 results may differ due to seeding strategy.
|
||||
/// Both must produce finite output after warmup on the same close series.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Tsf_MatchesOoples_Structural()
|
||||
{
|
||||
const int period = 14;
|
||||
|
||||
var ooplesData = _testData.SkenderQuotes.Select(q => new TickerData
|
||||
{
|
||||
Date = q.Date,
|
||||
Open = (double)q.Open,
|
||||
High = (double)q.High,
|
||||
Low = (double)q.Low,
|
||||
Close = (double)q.Close,
|
||||
Volume = (double)q.Volume
|
||||
}).ToList();
|
||||
|
||||
var stockData = new StockData(ooplesData);
|
||||
var oResult = stockData.CalculateTimeSeriesForecast(length: period);
|
||||
var oValues = oResult.OutputValues.Values.First();
|
||||
|
||||
var tsf = new Tsf(period);
|
||||
var qValues = new System.Collections.Generic.List<double>();
|
||||
foreach (var item in _testData.Data)
|
||||
{
|
||||
qValues.Add(tsf.Update(item).Value);
|
||||
}
|
||||
|
||||
Assert.True(oValues.Count > 0, "Ooples TSF must produce output");
|
||||
|
||||
int finiteCount = 0;
|
||||
for (int i = period; i < Math.Min(oValues.Count, qValues.Count); i++)
|
||||
{
|
||||
if (double.IsFinite(oValues[i]) && double.IsFinite(qValues[i]))
|
||||
{
|
||||
finiteCount++;
|
||||
}
|
||||
}
|
||||
|
||||
Assert.True(finiteCount > 100, $"Expected >100 finite TSF pairs, got {finiteCount}");
|
||||
_output.WriteLine($"TSF Ooples structural: {finiteCount} finite pairs verified.");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user