forked from cmf-team/importance-sampling-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
41 lines (28 loc) · 1.05 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from arch import arch_model
from scipy.stats import norm
import numpy as np
class RiskMetrics:
def __init__(self, alpha):
self.alpha = alpha
self.lambd = 0.94
self.window_size = 74
def forecast(self, feat):
sigma = 0
for r in feat[:-1]:
sigma = self.lambd*sigma + (1-self.lambd)*r**2
return norm.ppf(1 - self.alpha, scale=sigma**0.5)
class HistoricalSimulation:
def __init__(self, alpha, window_size):
self.alpha = alpha
self.window_size = window_size
def forecast(self, feat):
return np.quantile(feat[-self.window_size:], q = 1-self.alpha)
class GARCH11:
def __init__(self, alpha, window_size):
self.alpha = alpha
self.window_size = window_size
def forecast(self, feat):
model = arch_model(feat[-self.window_size:], p=1, q=1, rescale=False)
res = model.fit(disp='off')
sigma2 = res.forecast(horizon=1, reindex=False).variance.values[0, 0]
return norm.ppf(1 - self.alpha, scale=sigma2**0.5)