garch 1.0.0
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/LICENSE +21 -0
- package/README.md +207 -0
- package/build/index.cjs +528 -0
- package/build/index.mjs +516 -0
- package/package.json +47 -0
- package/types.d.ts +197 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Petr Tripolsky
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="./assets/logo.png" height="115px" alt="garch" />
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<p align="center">
|
|
6
|
+
<strong>Missing GARCH/EGARCH forecast for NodeJS</strong><br>
|
|
7
|
+
GARCH and EGARCH volatility models for TypeScript. Zero dependencies.
|
|
8
|
+
</p>
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install garch-ts
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
### GARCH(1,1)
|
|
19
|
+
|
|
20
|
+
```typescript
|
|
21
|
+
import { calibrateGarch, Garch } from 'garch';
|
|
22
|
+
|
|
23
|
+
// From price array
|
|
24
|
+
const prices = [100, 101, 99, 102, 98, ...];
|
|
25
|
+
const result = calibrateGarch(prices, { periodsPerYear: 252 });
|
|
26
|
+
|
|
27
|
+
console.log(result.params);
|
|
28
|
+
// {
|
|
29
|
+
// omega: 0.000012,
|
|
30
|
+
// alpha: 0.08,
|
|
31
|
+
// beta: 0.89,
|
|
32
|
+
// persistence: 0.97,
|
|
33
|
+
// unconditionalVariance: 0.0004,
|
|
34
|
+
// annualizedVol: 31.7
|
|
35
|
+
// }
|
|
36
|
+
|
|
37
|
+
// Or use the class for more control
|
|
38
|
+
const model = new Garch(prices, { periodsPerYear: 252 });
|
|
39
|
+
const fit = model.fit();
|
|
40
|
+
|
|
41
|
+
// Get variance series
|
|
42
|
+
const variance = model.getVarianceSeries(fit.params);
|
|
43
|
+
|
|
44
|
+
// Forecast 10 periods ahead
|
|
45
|
+
const forecast = model.forecast(fit.params, 10);
|
|
46
|
+
console.log(forecast.annualized); // [32.1, 31.9, 31.8, ...]
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### EGARCH(1,1)
|
|
50
|
+
|
|
51
|
+
EGARCH captures asymmetric volatility (leverage effect):
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
import { calibrateEgarch, Egarch, checkLeverageEffect } from 'garch';
|
|
55
|
+
|
|
56
|
+
// Check if EGARCH is warranted
|
|
57
|
+
const returns = calculateReturnsFromPrices(prices);
|
|
58
|
+
const leverage = checkLeverageEffect(returns);
|
|
59
|
+
console.log(leverage);
|
|
60
|
+
// { negativeVol: 0.021, positiveVol: 0.015, ratio: 1.4, recommendation: 'egarch' }
|
|
61
|
+
|
|
62
|
+
// Fit EGARCH
|
|
63
|
+
const result = calibrateEgarch(prices, { periodsPerYear: 365 }); // crypto = 365
|
|
64
|
+
|
|
65
|
+
console.log(result.params);
|
|
66
|
+
// {
|
|
67
|
+
// omega: -0.12,
|
|
68
|
+
// alpha: 0.15,
|
|
69
|
+
// gamma: -0.08, // negative = leverage effect
|
|
70
|
+
// beta: 0.95,
|
|
71
|
+
// persistence: 0.95,
|
|
72
|
+
// annualizedVol: 45.2,
|
|
73
|
+
// leverageEffect: -0.08
|
|
74
|
+
// }
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### From OHLCV Candles
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
import { Candle, calibrateGarch } from 'garch';
|
|
81
|
+
|
|
82
|
+
const candles: Candle[] = [
|
|
83
|
+
{ open: 100, high: 102, low: 99, close: 101, volume: 1000 },
|
|
84
|
+
{ open: 101, high: 103, low: 100, close: 99, volume: 1200 },
|
|
85
|
+
// ...
|
|
86
|
+
];
|
|
87
|
+
|
|
88
|
+
const result = calibrateGarch(candles);
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Model Selection
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
import { calibrateGarch, calibrateEgarch } from 'garch';
|
|
95
|
+
|
|
96
|
+
const garch = calibrateGarch(prices);
|
|
97
|
+
const egarch = calibrateEgarch(prices);
|
|
98
|
+
|
|
99
|
+
// Compare using AIC (lower is better)
|
|
100
|
+
if (egarch.diagnostics.aic < garch.diagnostics.aic) {
|
|
101
|
+
console.log('EGARCH fits better');
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## API
|
|
106
|
+
|
|
107
|
+
### `calibrateGarch(data, options?)`
|
|
108
|
+
|
|
109
|
+
Calibrate GARCH(1,1) model.
|
|
110
|
+
|
|
111
|
+
**Parameters:**
|
|
112
|
+
- `data`: `Candle[]` or `number[]` (prices)
|
|
113
|
+
- `options.periodsPerYear`: Annualization factor (default: 252)
|
|
114
|
+
- `options.maxIter`: Maximum optimizer iterations (default: 1000)
|
|
115
|
+
- `options.tol`: Convergence tolerance (default: 1e-8)
|
|
116
|
+
|
|
117
|
+
**Returns:** `CalibrationResult<GarchParams>`
|
|
118
|
+
|
|
119
|
+
### `calibrateEgarch(data, options?)`
|
|
120
|
+
|
|
121
|
+
Calibrate EGARCH(1,1) model.
|
|
122
|
+
|
|
123
|
+
**Parameters:** Same as `calibrateGarch`
|
|
124
|
+
|
|
125
|
+
**Returns:** `CalibrationResult<EgarchParams>`
|
|
126
|
+
|
|
127
|
+
### `checkLeverageEffect(returns)`
|
|
128
|
+
|
|
129
|
+
Check for asymmetric volatility.
|
|
130
|
+
|
|
131
|
+
**Returns:** `{ negativeVol, positiveVol, ratio, recommendation }`
|
|
132
|
+
|
|
133
|
+
### Classes
|
|
134
|
+
|
|
135
|
+
`Garch` and `Egarch` classes provide:
|
|
136
|
+
- `.fit(options?)` - Calibrate parameters
|
|
137
|
+
- `.getVarianceSeries(params)` - Compute conditional variance
|
|
138
|
+
- `.forecast(params, steps)` - Multi-step variance forecast
|
|
139
|
+
- `.getReturns()` - Get computed returns
|
|
140
|
+
|
|
141
|
+
## Timeframes
|
|
142
|
+
|
|
143
|
+
The library works with any candle timeframe. The only thing that changes is the `periodsPerYear` option, which controls annualization of volatility.
|
|
144
|
+
|
|
145
|
+
| Timeframe | `periodsPerYear` | Notes |
|
|
146
|
+
|-----------|-----------------|-------|
|
|
147
|
+
| **1d** | `252` (default) | Trading days per year |
|
|
148
|
+
| **4h** | `1512` | 252 × 6 |
|
|
149
|
+
| **1h** | `6048` (crypto) / `1638` (stocks) | Crypto trades 24/7, stocks ~6.5h/day |
|
|
150
|
+
| **15m** | `24192` (crypto) / `6552` (stocks) | 96 or 26 bars per day × 252 |
|
|
151
|
+
| **1m** | `362880` (crypto) / `393120` (stocks) | 1440 or 390 bars per day × 252 |
|
|
152
|
+
|
|
153
|
+
```typescript
|
|
154
|
+
// Daily candles (default)
|
|
155
|
+
calibrateGarch(prices);
|
|
156
|
+
|
|
157
|
+
// 4-hour candles
|
|
158
|
+
calibrateGarch(prices, { periodsPerYear: 1512 });
|
|
159
|
+
|
|
160
|
+
// 15-minute candles (crypto, 24/7 market)
|
|
161
|
+
calibrateGarch(prices, { periodsPerYear: 24192 });
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
**Minimum data:** 50 candles are required for stable parameter estimation.
|
|
165
|
+
|
|
166
|
+
**Recommended timeframes:** 1d and 4h are the most reliable for GARCH models. Lower timeframes (15m, 1m) contain more microstructure noise which can degrade calibration quality — use larger datasets to compensate.
|
|
167
|
+
|
|
168
|
+
## Model Details
|
|
169
|
+
|
|
170
|
+
### GARCH(1,1)
|
|
171
|
+
|
|
172
|
+
```
|
|
173
|
+
σ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
- `ω` (omega) > 0: constant term
|
|
177
|
+
- `α` (alpha) ≥ 0: reaction to shocks
|
|
178
|
+
- `β` (beta) ≥ 0: persistence
|
|
179
|
+
- Stationarity: α + β < 1
|
|
180
|
+
|
|
181
|
+
### EGARCH(1,1)
|
|
182
|
+
|
|
183
|
+
```
|
|
184
|
+
ln(σ²ₜ) = ω + α·(|zₜ₋₁| - E[|z|]) + γ·zₜ₋₁ + β·ln(σ²ₜ₋₁)
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
- `γ` (gamma) < 0: leverage effect (negative returns increase vol more)
|
|
188
|
+
- No positivity constraints needed (models log-variance)
|
|
189
|
+
- `|β|` < 1 for stationarity
|
|
190
|
+
|
|
191
|
+
### Model Selection
|
|
192
|
+
|
|
193
|
+
```typescript
|
|
194
|
+
import { calibrateGarch, calibrateEgarch } from 'garch';
|
|
195
|
+
|
|
196
|
+
const garch = calibrateGarch(prices);
|
|
197
|
+
const egarch = calibrateEgarch(prices);
|
|
198
|
+
|
|
199
|
+
// Compare using AIC (lower is better)
|
|
200
|
+
if (egarch.diagnostics.aic < garch.diagnostics.aic) {
|
|
201
|
+
console.log('EGARCH fits better — leverage effect is significant');
|
|
202
|
+
}
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
## License
|
|
206
|
+
|
|
207
|
+
MIT
|