validation and profiles

This commit is contained in:
Miha Kralj
2026-02-26 22:02:52 -08:00
parent 9ab37c1200
commit 8a1ba95173
317 changed files with 18704 additions and 622 deletions
@@ -1,4 +1,5 @@
using Skender.Stock.Indicators;
using TALib;
namespace QuanTAlib.Tests;
@@ -86,4 +87,85 @@ public sealed class BetaValidationTests : IDisposable
}
}
}
[Fact]
public void Validate_Against_Talib()
{
// TALib Beta takes two price series (e.g. stock vs market returns via price series).
// TALib.Functions.Beta(stockPrices, marketPrices, range, output, outRange, period)
// Internally computes beta from price returns within each rolling window.
//
// Note: TALib Beta uses a different return calculation (price[i]/price[i-1] - 1)
// and a different beta formula (covariance/variance from returns) than Skender.
// QuanTAlib Beta matches Skender (covariance of returns / variance of market returns).
// Direct numeric equality with TALib is not expected; we verify structural properties.
var marketQuotes = _data.Data;
// Build correlated asset prices
var noiseGbm = new GBM(startPrice: 100, mu: 0, sigma: 0.2, seed: 999);
double assetPrice = 100;
const double targetBeta = 1.2;
var assetPrices = new double[marketQuotes.Count];
var marketPrices = new double[marketQuotes.Count];
assetPrices[0] = assetPrice;
marketPrices[0] = marketQuotes[0].Value;
for (int i = 1; i < marketQuotes.Count; i++)
{
double mktReturn = (marketQuotes[i].Value - marketQuotes[i - 1].Value) / marketQuotes[i - 1].Value;
var noiseBar = noiseGbm.Next();
double noise = (noiseBar.Close - noiseBar.Open) / noiseBar.Open;
double astReturn = targetBeta * mktReturn + noise * 0.1;
assetPrice *= (1 + astReturn);
assetPrices[i] = assetPrice;
marketPrices[i] = marketQuotes[i].Value;
}
const int period = 20;
// TALib Beta
double[] taOut = new double[marketPrices.Length];
var retCode = Functions.Beta<double>(
assetPrices.AsSpan(), marketPrices.AsSpan(),
0..^0, taOut, out var outRange, period);
Assert.Equal(Core.RetCode.Success, retCode);
(int offset, int length) = outRange.GetOffsetAndLength(taOut.Length);
// Verify TALib produces finite values
Assert.True(length > 0, "TALib Beta produced no output");
for (int j = 0; j < length; j++)
{
Assert.True(double.IsFinite(taOut[j]),
$"TALib Beta[{j}] = {taOut[j]} is not finite");
}
// QuanTAlib Beta
var beta = new Beta(period);
var qlBetaArr = new double[marketQuotes.Count];
for (int i = 0; i < marketQuotes.Count; i++)
{
qlBetaArr[i] = beta.Update(assetPrices[i], marketPrices[i]).Value;
}
// Both should produce finite values after warmup
for (int i = period + 5; i < marketQuotes.Count; i++)
{
Assert.True(double.IsFinite(qlBetaArr[i]), $"QuanTAlib Beta[{i}] is not finite");
}
// Sign agreement: positively correlated asset → >60% positive betas from both
int taPositive = 0;
int qlPositive = 0;
for (int j = 0; j < length; j++)
{
int qi = j + offset;
if (taOut[j] > 0) { taPositive++; }
if (qlBetaArr[qi] > 0) { qlPositive++; }
}
Assert.True(taPositive > length * 0.6, $"TALib Beta positive rate {taPositive}/{length} < 60%");
Assert.True(qlPositive > length * 0.6, $"QuanTAlib Beta positive rate {qlPositive}/{length} < 60%");
}
}
+15
View File
@@ -41,6 +41,21 @@ This formula is mathematically equivalent to the covariance/variance definition
## Performance Profile
### Operation Count (Streaming Mode)
Beta uses running sums of returns (Welford-style) for O(1) covariance/variance update.
| Operation | Count | Cost (cycles) | Subtotal |
| :--- | :---: | :---: | :---: |
| Ring buffer add/evict (2 inputs) | 2 | 3 cy | ~6 cy |
| Compute asset + market returns | 2 | 3 cy | ~6 cy |
| Update 4 running sums (Ra, Rm, Ra*Rm, Rm^2) | 4 | 2 cy | ~8 cy |
| Compute covariance / variance | 2 | 5 cy | ~10 cy |
| NaN guard (zero variance) | 1 | 2 cy | ~2 cy |
| **Total** | **O(1)** | — | **~32 cy** |
O(1) per update. Dual-input constraint prevents SIMD batch optimization; sequential return computation enforces ordering.
| Metric | Score | Notes |
| :--- | :--- | :--- |
| **Throughput** | 15 ns/bar | Single-pass O(1) calculation. |