garch 1.2.0 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/build/index.cjs +5 -2
- package/build/index.mjs +5 -3
- package/package.json +48 -48
- package/types.d.ts +11 -3
package/README.md
CHANGED
|
@@ -556,13 +556,13 @@ where RV_t is Parkinson per-candle realized variance and sigma_t^2 is the model'
|
|
|
556
556
|
|
|
557
557
|
## Tests
|
|
558
558
|
|
|
559
|
-
**
|
|
559
|
+
**932 tests** across **22 test files**. All passing.
|
|
560
560
|
|
|
561
561
|
| Category | Files | Tests | What's covered |
|
|
562
562
|
|----------|-------|-------|----------------|
|
|
563
563
|
| Mathematical formulas | `math.test.ts` | 45 | GARCH/EGARCH variance recursion, log-likelihood, forecast formulas, AIC/BIC, QLIKE, Yang-Zhang, Garman-Klass, Ljung-Box, chi-squared |
|
|
564
564
|
| Math coverage | `math-coverage.test.ts` | 79 | Parkinson formula verification, rv↔returns alignment, H=L fallback, Parkinson-based forecast, candle validation, reliable flag cascade, backtest validity, numerical precision, cross-model consistency, Realized GARCH/EGARCH/GJR-GARCH Candle[] vs number[], perCandleParkinson shared function |
|
|
565
|
-
| Full pipeline coverage | `plan-coverage.test.ts` |
|
|
565
|
+
| Full pipeline coverage | `plan-coverage.test.ts` | 82 | End-to-end: fit, forecast, predict, predictRange, backtest, model selection |
|
|
566
566
|
| GARCH unit | `garch.test.ts` | 10 | Parameter estimation, variance series, forecast convergence, candle vs price input |
|
|
567
567
|
| EGARCH unit | `egarch.test.ts` | 11 | Leverage detection, asymmetric volatility, model comparison |
|
|
568
568
|
| GJR-GARCH unit | `gjr-garch.test.ts` | 86 | Variance recursion (r² and Parkinson), indicator function I(r<0), forecast formula (one-step + multi-step), constraint barriers, computed fields, AIC/BIC numParams=5, estimation properties (perturbation, determinism), numerical stability, degenerate params, Realized path (Candle[] vs number[], flat candles, bad OHLC), options forwarding, immutability, instance isolation, cross-model consistency, scale invariance, property-based fuzz, predict/predictRange/backtest integration |
|
package/build/index.cjs
CHANGED
|
@@ -1780,8 +1780,9 @@ function checkReliable(fit) {
|
|
|
1780
1780
|
* @param confidence — two-sided probability in (0,1). Default ≈0.6827 (±1σ).
|
|
1781
1781
|
* Common values: 0.90 → z=1.645, 0.95 → z=1.96, 0.99 → z=2.576.
|
|
1782
1782
|
*/
|
|
1783
|
-
function predict(candles, interval, currentPrice
|
|
1783
|
+
function predict(candles, interval, currentPrice, confidence = 0.6827) {
|
|
1784
1784
|
assertMinCandles(candles, interval);
|
|
1785
|
+
currentPrice = currentPrice || candles[candles.length - 1].close;
|
|
1785
1786
|
const z = probit(confidence);
|
|
1786
1787
|
const fit = fitModel(candles, INTERVALS_PER_YEAR[interval], 1);
|
|
1787
1788
|
const sigma = fit.forecast.volatility[0];
|
|
@@ -1804,10 +1805,11 @@ function predict(candles, interval, currentPrice = candles[candles.length - 1].c
|
|
|
1804
1805
|
* Uses log-normal price bands: P·exp(±z·σ), where z = probit(confidence).
|
|
1805
1806
|
* @param confidence — two-sided probability in (0,1). Default ≈0.6827 (±1σ).
|
|
1806
1807
|
*/
|
|
1807
|
-
function predictRange(candles, interval, steps, currentPrice
|
|
1808
|
+
function predictRange(candles, interval, steps, currentPrice, confidence = 0.6827) {
|
|
1808
1809
|
assertMinCandles(candles, interval);
|
|
1809
1810
|
const z = probit(confidence);
|
|
1810
1811
|
const fit = fitModel(candles, INTERVALS_PER_YEAR[interval], steps);
|
|
1812
|
+
currentPrice = currentPrice || candles[candles.length - 1].close;
|
|
1811
1813
|
const cumulativeVariance = fit.forecast.variance.reduce((sum, v) => sum + v, 0);
|
|
1812
1814
|
const sigma = Math.sqrt(cumulativeVariance);
|
|
1813
1815
|
const upperPrice = currentPrice * Math.exp(z * sigma);
|
|
@@ -1879,6 +1881,7 @@ exports.nelderMeadMultiStart = nelderMeadMultiStart;
|
|
|
1879
1881
|
exports.perCandleParkinson = perCandleParkinson;
|
|
1880
1882
|
exports.predict = predict;
|
|
1881
1883
|
exports.predictRange = predictRange;
|
|
1884
|
+
exports.probit = probit;
|
|
1882
1885
|
exports.profileStudentTDf = profileStudentTDf;
|
|
1883
1886
|
exports.qlike = qlike;
|
|
1884
1887
|
exports.sampleVariance = sampleVariance;
|
package/build/index.mjs
CHANGED
|
@@ -1778,8 +1778,9 @@ function checkReliable(fit) {
|
|
|
1778
1778
|
* @param confidence — two-sided probability in (0,1). Default ≈0.6827 (±1σ).
|
|
1779
1779
|
* Common values: 0.90 → z=1.645, 0.95 → z=1.96, 0.99 → z=2.576.
|
|
1780
1780
|
*/
|
|
1781
|
-
function predict(candles, interval, currentPrice
|
|
1781
|
+
function predict(candles, interval, currentPrice, confidence = 0.6827) {
|
|
1782
1782
|
assertMinCandles(candles, interval);
|
|
1783
|
+
currentPrice = currentPrice || candles[candles.length - 1].close;
|
|
1783
1784
|
const z = probit(confidence);
|
|
1784
1785
|
const fit = fitModel(candles, INTERVALS_PER_YEAR[interval], 1);
|
|
1785
1786
|
const sigma = fit.forecast.volatility[0];
|
|
@@ -1802,10 +1803,11 @@ function predict(candles, interval, currentPrice = candles[candles.length - 1].c
|
|
|
1802
1803
|
* Uses log-normal price bands: P·exp(±z·σ), where z = probit(confidence).
|
|
1803
1804
|
* @param confidence — two-sided probability in (0,1). Default ≈0.6827 (±1σ).
|
|
1804
1805
|
*/
|
|
1805
|
-
function predictRange(candles, interval, steps, currentPrice
|
|
1806
|
+
function predictRange(candles, interval, steps, currentPrice, confidence = 0.6827) {
|
|
1806
1807
|
assertMinCandles(candles, interval);
|
|
1807
1808
|
const z = probit(confidence);
|
|
1808
1809
|
const fit = fitModel(candles, INTERVALS_PER_YEAR[interval], steps);
|
|
1810
|
+
currentPrice = currentPrice || candles[candles.length - 1].close;
|
|
1809
1811
|
const cumulativeVariance = fit.forecast.variance.reduce((sum, v) => sum + v, 0);
|
|
1810
1812
|
const sigma = Math.sqrt(cumulativeVariance);
|
|
1811
1813
|
const upperPrice = currentPrice * Math.exp(z * sigma);
|
|
@@ -1853,4 +1855,4 @@ function backtest(candles, interval, confidence = 0.6827, requiredPercent = 68)
|
|
|
1853
1855
|
return (hits / total) * 100 >= requiredPercent;
|
|
1854
1856
|
}
|
|
1855
1857
|
|
|
1856
|
-
export { EXPECTED_ABS_NORMAL, Egarch, Garch, GjrGarch, HarRv, NoVaS, backtest, calculateReturns, calculateReturnsFromPrices, calibrateEgarch, calibrateGarch, calibrateGjrGarch, calibrateHarRv, calibrateNoVaS, checkLeverageEffect, expectedAbsStudentT, garmanKlassVariance, ljungBox, logGamma, nelderMead, nelderMeadMultiStart, perCandleParkinson, predict, predictRange, profileStudentTDf, qlike, sampleVariance, sampleVarianceWithMean, studentTNegLL, yangZhangVariance };
|
|
1858
|
+
export { EXPECTED_ABS_NORMAL, Egarch, Garch, GjrGarch, HarRv, NoVaS, backtest, calculateReturns, calculateReturnsFromPrices, calibrateEgarch, calibrateGarch, calibrateGjrGarch, calibrateHarRv, calibrateNoVaS, checkLeverageEffect, expectedAbsStudentT, garmanKlassVariance, ljungBox, logGamma, nelderMead, nelderMeadMultiStart, perCandleParkinson, predict, predictRange, probit, profileStudentTDf, qlike, sampleVariance, sampleVarianceWithMean, studentTNegLL, yangZhangVariance };
|
package/package.json
CHANGED
|
@@ -1,48 +1,48 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "garch",
|
|
3
|
-
"version": "1.2.
|
|
4
|
-
"description": "GARCH and EGARCH volatility models for TypeScript",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"main": "./build/index.cjs",
|
|
7
|
-
"module": "./build/index.mjs",
|
|
8
|
-
"types": "./types.d.ts",
|
|
9
|
-
"exports": {
|
|
10
|
-
".": {
|
|
11
|
-
"types": "./types.d.ts",
|
|
12
|
-
"import": "./build/index.mjs",
|
|
13
|
-
"require": "./build/index.cjs"
|
|
14
|
-
}
|
|
15
|
-
},
|
|
16
|
-
"files": [
|
|
17
|
-
"build",
|
|
18
|
-
"types.d.ts",
|
|
19
|
-
"README.md"
|
|
20
|
-
],
|
|
21
|
-
"scripts": {
|
|
22
|
-
"build": "rollup -c",
|
|
23
|
-
"test": "vitest run",
|
|
24
|
-
"test:watch": "vitest",
|
|
25
|
-
"prepublishOnly": "npm run build"
|
|
26
|
-
},
|
|
27
|
-
"keywords": [
|
|
28
|
-
"garch",
|
|
29
|
-
"egarch",
|
|
30
|
-
"volatility",
|
|
31
|
-
"finance",
|
|
32
|
-
"econometrics",
|
|
33
|
-
"time-series"
|
|
34
|
-
],
|
|
35
|
-
"author": "",
|
|
36
|
-
"license": "MIT",
|
|
37
|
-
"devDependencies": {
|
|
38
|
-
"@rollup/plugin-typescript": "^12.3.0",
|
|
39
|
-
"@types/node": "^20.10.0",
|
|
40
|
-
"@vitest/coverage-v8": "^1.6.1",
|
|
41
|
-
"rollup": "^4.57.1",
|
|
42
|
-
"rollup-plugin-dts": "^6.3.0",
|
|
43
|
-
"rollup-plugin-peer-deps-external": "^2.2.4",
|
|
44
|
-
"tslib": "^2.8.1",
|
|
45
|
-
"typescript": "^5.3.0",
|
|
46
|
-
"vitest": "^1.0.0"
|
|
47
|
-
}
|
|
48
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "garch",
|
|
3
|
+
"version": "1.2.2",
|
|
4
|
+
"description": "GARCH and EGARCH volatility models for TypeScript",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./build/index.cjs",
|
|
7
|
+
"module": "./build/index.mjs",
|
|
8
|
+
"types": "./types.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./types.d.ts",
|
|
12
|
+
"import": "./build/index.mjs",
|
|
13
|
+
"require": "./build/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"build",
|
|
18
|
+
"types.d.ts",
|
|
19
|
+
"README.md"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "rollup -c",
|
|
23
|
+
"test": "vitest run",
|
|
24
|
+
"test:watch": "vitest",
|
|
25
|
+
"prepublishOnly": "npm run build"
|
|
26
|
+
},
|
|
27
|
+
"keywords": [
|
|
28
|
+
"garch",
|
|
29
|
+
"egarch",
|
|
30
|
+
"volatility",
|
|
31
|
+
"finance",
|
|
32
|
+
"econometrics",
|
|
33
|
+
"time-series"
|
|
34
|
+
],
|
|
35
|
+
"author": "",
|
|
36
|
+
"license": "MIT",
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@rollup/plugin-typescript": "^12.3.0",
|
|
39
|
+
"@types/node": "^20.10.0",
|
|
40
|
+
"@vitest/coverage-v8": "^1.6.1",
|
|
41
|
+
"rollup": "^4.57.1",
|
|
42
|
+
"rollup-plugin-dts": "^6.3.0",
|
|
43
|
+
"rollup-plugin-peer-deps-external": "^2.2.4",
|
|
44
|
+
"tslib": "^2.8.1",
|
|
45
|
+
"typescript": "^5.3.0",
|
|
46
|
+
"vitest": "^1.0.0"
|
|
47
|
+
}
|
|
48
|
+
}
|
package/types.d.ts
CHANGED
|
@@ -487,6 +487,14 @@ declare function profileStudentTDf(returns: number[], varianceSeries: number[]):
|
|
|
487
487
|
* of how the model was calibrated (MLE, OLS, D², etc.).
|
|
488
488
|
*/
|
|
489
489
|
declare function qlike(varianceSeries: number[], rv: number[]): number;
|
|
490
|
+
/**
|
|
491
|
+
* Inverse standard normal CDF (probit function).
|
|
492
|
+
* Converts a two-sided confidence level (e.g. 0.95) to the corresponding
|
|
493
|
+
* z-score (e.g. 1.96).
|
|
494
|
+
*
|
|
495
|
+
* Uses Acklam's rational approximation (max relative error < 1.15e-9).
|
|
496
|
+
*/
|
|
497
|
+
declare function probit(confidence: number): number;
|
|
490
498
|
|
|
491
499
|
type CandleInterval = '1m' | '3m' | '5m' | '15m' | '30m' | '1h' | '2h' | '4h' | '6h' | '8h';
|
|
492
500
|
interface PredictionResult {
|
|
@@ -506,7 +514,7 @@ interface PredictionResult {
|
|
|
506
514
|
* @param confidence — two-sided probability in (0,1). Default ≈0.6827 (±1σ).
|
|
507
515
|
* Common values: 0.90 → z=1.645, 0.95 → z=1.96, 0.99 → z=2.576.
|
|
508
516
|
*/
|
|
509
|
-
declare function predict(candles: Candle[], interval: CandleInterval, currentPrice?: number, confidence?: number): PredictionResult;
|
|
517
|
+
declare function predict(candles: Candle[], interval: CandleInterval, currentPrice?: number | null, confidence?: number): PredictionResult;
|
|
510
518
|
/**
|
|
511
519
|
* Forecast expected price range over multiple candles.
|
|
512
520
|
*
|
|
@@ -514,7 +522,7 @@ declare function predict(candles: Candle[], interval: CandleInterval, currentPri
|
|
|
514
522
|
* Uses log-normal price bands: P·exp(±z·σ), where z = probit(confidence).
|
|
515
523
|
* @param confidence — two-sided probability in (0,1). Default ≈0.6827 (±1σ).
|
|
516
524
|
*/
|
|
517
|
-
declare function predictRange(candles: Candle[], interval: CandleInterval, steps: number, currentPrice?: number, confidence?: number): PredictionResult;
|
|
525
|
+
declare function predictRange(candles: Candle[], interval: CandleInterval, steps: number, currentPrice?: number | null, confidence?: number): PredictionResult;
|
|
518
526
|
/**
|
|
519
527
|
* Walk-forward backtest of predict.
|
|
520
528
|
*
|
|
@@ -541,5 +549,5 @@ declare function nelderMeadMultiStart(fn: (x: number[]) => number, x0: number[],
|
|
|
541
549
|
restarts?: number;
|
|
542
550
|
}): OptimizerResult;
|
|
543
551
|
|
|
544
|
-
export { EXPECTED_ABS_NORMAL, Egarch, Garch, GjrGarch, HarRv, NoVaS, backtest, calculateReturns, calculateReturnsFromPrices, calibrateEgarch, calibrateGarch, calibrateGjrGarch, calibrateHarRv, calibrateNoVaS, checkLeverageEffect, expectedAbsStudentT, garmanKlassVariance, ljungBox, logGamma, nelderMead, nelderMeadMultiStart, perCandleParkinson, predict, predictRange, profileStudentTDf, qlike, sampleVariance, sampleVarianceWithMean, studentTNegLL, yangZhangVariance };
|
|
552
|
+
export { EXPECTED_ABS_NORMAL, Egarch, Garch, GjrGarch, HarRv, NoVaS, backtest, calculateReturns, calculateReturnsFromPrices, calibrateEgarch, calibrateGarch, calibrateGjrGarch, calibrateHarRv, calibrateNoVaS, checkLeverageEffect, expectedAbsStudentT, garmanKlassVariance, ljungBox, logGamma, nelderMead, nelderMeadMultiStart, perCandleParkinson, predict, predictRange, probit, profileStudentTDf, qlike, sampleVariance, sampleVarianceWithMean, studentTNegLL, yangZhangVariance };
|
|
545
553
|
export type { CalibrationResult, Candle, CandleInterval, EgarchOptions, EgarchParams, GarchOptions, GarchParams, GjrGarchOptions, GjrGarchParams, HarRvOptions, HarRvParams, LeverageStats, NoVaSOptions, NoVaSParams, OptimizerResult, PredictionResult, VolatilityForecast };
|