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/types.d.ts
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
interface Candle {
|
|
2
|
+
open: number;
|
|
3
|
+
high: number;
|
|
4
|
+
low: number;
|
|
5
|
+
close: number;
|
|
6
|
+
volume: number;
|
|
7
|
+
timestamp?: number;
|
|
8
|
+
}
|
|
9
|
+
interface GarchParams {
|
|
10
|
+
omega: number;
|
|
11
|
+
alpha: number;
|
|
12
|
+
beta: number;
|
|
13
|
+
persistence: number;
|
|
14
|
+
unconditionalVariance: number;
|
|
15
|
+
annualizedVol: number;
|
|
16
|
+
}
|
|
17
|
+
interface EgarchParams {
|
|
18
|
+
omega: number;
|
|
19
|
+
alpha: number;
|
|
20
|
+
gamma: number;
|
|
21
|
+
beta: number;
|
|
22
|
+
persistence: number;
|
|
23
|
+
unconditionalVariance: number;
|
|
24
|
+
annualizedVol: number;
|
|
25
|
+
leverageEffect: number;
|
|
26
|
+
}
|
|
27
|
+
interface CalibrationResult<T> {
|
|
28
|
+
params: T;
|
|
29
|
+
diagnostics: {
|
|
30
|
+
logLikelihood: number;
|
|
31
|
+
aic: number;
|
|
32
|
+
bic: number;
|
|
33
|
+
iterations: number;
|
|
34
|
+
converged: boolean;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
interface VolatilityForecast {
|
|
38
|
+
variance: number[];
|
|
39
|
+
volatility: number[];
|
|
40
|
+
annualized: number[];
|
|
41
|
+
}
|
|
42
|
+
interface LeverageStats {
|
|
43
|
+
negativeVol: number;
|
|
44
|
+
positiveVol: number;
|
|
45
|
+
ratio: number;
|
|
46
|
+
recommendation: 'garch' | 'egarch';
|
|
47
|
+
}
|
|
48
|
+
interface OptimizerResult {
|
|
49
|
+
x: number[];
|
|
50
|
+
fx: number;
|
|
51
|
+
iterations: number;
|
|
52
|
+
converged: boolean;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
interface GarchOptions {
|
|
56
|
+
periodsPerYear?: number;
|
|
57
|
+
maxIter?: number;
|
|
58
|
+
tol?: number;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* GARCH(1,1) model
|
|
62
|
+
*
|
|
63
|
+
* σ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁
|
|
64
|
+
*
|
|
65
|
+
* where:
|
|
66
|
+
* - ω (omega) > 0: constant term
|
|
67
|
+
* - α (alpha) ≥ 0: ARCH parameter (reaction to shocks)
|
|
68
|
+
* - β (beta) ≥ 0: GARCH parameter (persistence)
|
|
69
|
+
* - α + β < 1: stationarity condition
|
|
70
|
+
*/
|
|
71
|
+
declare class Garch {
|
|
72
|
+
private returns;
|
|
73
|
+
private periodsPerYear;
|
|
74
|
+
private initialVariance;
|
|
75
|
+
constructor(data: Candle[] | number[], options?: GarchOptions);
|
|
76
|
+
/**
|
|
77
|
+
* Calibrate GARCH(1,1) parameters using Maximum Likelihood Estimation
|
|
78
|
+
*/
|
|
79
|
+
fit(options?: {
|
|
80
|
+
maxIter?: number;
|
|
81
|
+
tol?: number;
|
|
82
|
+
}): CalibrationResult<GarchParams>;
|
|
83
|
+
/**
|
|
84
|
+
* Calculate conditional variance series given parameters
|
|
85
|
+
*/
|
|
86
|
+
getVarianceSeries(params: GarchParams): number[];
|
|
87
|
+
/**
|
|
88
|
+
* Forecast variance forward
|
|
89
|
+
*/
|
|
90
|
+
forecast(params: GarchParams, steps?: number): VolatilityForecast;
|
|
91
|
+
/**
|
|
92
|
+
* Get the return series
|
|
93
|
+
*/
|
|
94
|
+
getReturns(): number[];
|
|
95
|
+
/**
|
|
96
|
+
* Get initial variance estimate
|
|
97
|
+
*/
|
|
98
|
+
getInitialVariance(): number;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Convenience function to calibrate GARCH(1,1) from candles
|
|
102
|
+
*/
|
|
103
|
+
declare function calibrateGarch(data: Candle[] | number[], options?: GarchOptions): CalibrationResult<GarchParams>;
|
|
104
|
+
|
|
105
|
+
interface EgarchOptions {
|
|
106
|
+
periodsPerYear?: number;
|
|
107
|
+
maxIter?: number;
|
|
108
|
+
tol?: number;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* EGARCH(1,1) model (Nelson, 1991)
|
|
112
|
+
*
|
|
113
|
+
* ln(σ²ₜ) = ω + α·(|zₜ₋₁| - E[|z|]) + γ·zₜ₋₁ + β·ln(σ²ₜ₋₁)
|
|
114
|
+
*
|
|
115
|
+
* where:
|
|
116
|
+
* - zₜ = εₜ/σₜ (standardized residual)
|
|
117
|
+
* - ω (omega): constant term
|
|
118
|
+
* - α (alpha): magnitude effect
|
|
119
|
+
* - γ (gamma): leverage effect (typically negative)
|
|
120
|
+
* - β (beta): persistence
|
|
121
|
+
* - E[|z|] = √(2/π) for standard normal
|
|
122
|
+
*/
|
|
123
|
+
declare class Egarch {
|
|
124
|
+
private returns;
|
|
125
|
+
private periodsPerYear;
|
|
126
|
+
private initialVariance;
|
|
127
|
+
constructor(data: Candle[] | number[], options?: EgarchOptions);
|
|
128
|
+
/**
|
|
129
|
+
* Calibrate EGARCH(1,1) parameters using Maximum Likelihood Estimation
|
|
130
|
+
*/
|
|
131
|
+
fit(options?: {
|
|
132
|
+
maxIter?: number;
|
|
133
|
+
tol?: number;
|
|
134
|
+
}): CalibrationResult<EgarchParams>;
|
|
135
|
+
/**
|
|
136
|
+
* Calculate conditional variance series given parameters
|
|
137
|
+
*/
|
|
138
|
+
getVarianceSeries(params: EgarchParams): number[];
|
|
139
|
+
/**
|
|
140
|
+
* Forecast variance forward
|
|
141
|
+
*
|
|
142
|
+
* Note: EGARCH forecasts are more complex because they depend on
|
|
143
|
+
* the path of shocks. This provides an approximation assuming
|
|
144
|
+
* expected values of future shocks.
|
|
145
|
+
*/
|
|
146
|
+
forecast(params: EgarchParams, steps?: number): VolatilityForecast;
|
|
147
|
+
/**
|
|
148
|
+
* Get the return series
|
|
149
|
+
*/
|
|
150
|
+
getReturns(): number[];
|
|
151
|
+
/**
|
|
152
|
+
* Get initial variance estimate
|
|
153
|
+
*/
|
|
154
|
+
getInitialVariance(): number;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Convenience function to calibrate EGARCH(1,1) from candles
|
|
158
|
+
*/
|
|
159
|
+
declare function calibrateEgarch(data: Candle[] | number[], options?: EgarchOptions): CalibrationResult<EgarchParams>;
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Calculate log returns from candles
|
|
163
|
+
*/
|
|
164
|
+
declare function calculateReturns(candles: Candle[]): number[];
|
|
165
|
+
/**
|
|
166
|
+
* Calculate log returns from price array
|
|
167
|
+
*/
|
|
168
|
+
declare function calculateReturnsFromPrices(prices: number[]): number[];
|
|
169
|
+
/**
|
|
170
|
+
* Calculate sample variance (mean-zero assumption)
|
|
171
|
+
*/
|
|
172
|
+
declare function sampleVariance(returns: number[]): number;
|
|
173
|
+
/**
|
|
174
|
+
* Calculate sample variance with mean adjustment
|
|
175
|
+
*/
|
|
176
|
+
declare function sampleVarianceWithMean(returns: number[]): number;
|
|
177
|
+
/**
|
|
178
|
+
* Check for leverage effect (asymmetry in volatility)
|
|
179
|
+
*/
|
|
180
|
+
declare function checkLeverageEffect(returns: number[]): LeverageStats;
|
|
181
|
+
/**
|
|
182
|
+
* Expected value of |Z| where Z ~ N(0,1)
|
|
183
|
+
* E[|Z|] = sqrt(2/π)
|
|
184
|
+
*/
|
|
185
|
+
declare const EXPECTED_ABS_NORMAL: number;
|
|
186
|
+
|
|
187
|
+
declare function nelderMead(fn: (x: number[]) => number, x0: number[], options?: {
|
|
188
|
+
maxIter?: number;
|
|
189
|
+
tol?: number;
|
|
190
|
+
alpha?: number;
|
|
191
|
+
gamma?: number;
|
|
192
|
+
rho?: number;
|
|
193
|
+
sigma?: number;
|
|
194
|
+
}): OptimizerResult;
|
|
195
|
+
|
|
196
|
+
export { EXPECTED_ABS_NORMAL, Egarch, Garch, calculateReturns, calculateReturnsFromPrices, calibrateEgarch, calibrateGarch, checkLeverageEffect, nelderMead, sampleVariance, sampleVarianceWithMean };
|
|
197
|
+
export type { CalibrationResult, Candle, EgarchOptions, EgarchParams, GarchOptions, GarchParams, LeverageStats, OptimizerResult, VolatilityForecast };
|