forked from danpele/LLM-Risk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgarch_var_calculator.py
204 lines (177 loc) · 10.1 KB
/
garch_var_calculator.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import numpy as np
import pandas as pd
from scipy.optimize import minimize
from scipy.stats import norm, t
from scipy.special import gamma
class GARCHVaRCalculator:
def __init__(self, returns, alpha, window, nu=None):
self.returns = returns
self.alpha = np.ndarray([alpha]) if isinstance(alpha, float) or isinstance(alpha, np.ndarray) else np.array(alpha)
self.window = window
self.nu = nu
def GARCH_VaR(self):
def garch_log_likelihood(params, returns):
omega, garch_alpha, garch_beta = params
n = len(returns)
var = np.zeros(n)
var[0] = np.var(returns) + 1e-6
for t in range(1, n):
var[t] = omega + garch_alpha * returns.iloc[t-1]**2 + garch_beta * var[t-1]
if var[t] < 1e-6 or np.isnan(var[t]) or np.isinf(var[t]):
var[t] = 1e-6
log_likelihood = -0.5 * np.sum(np.log(2 * np.pi) + np.log(var) + returns**2 / var)
return -log_likelihood
VaR_series, ES_series = {}, {}
garch_volatility = np.zeros(len(self.returns))
for i in range(self.window, len(self.returns)):
window_returns = self.returns[i-self.window:i]
current_index = self.returns.index[i]
initial_params = [0.1, 0.1, 0.8]
bounds = [(1e-6, None), (0, 1), (0, 1)]
result = minimize(garch_log_likelihood, initial_params, args=(window_returns,), bounds=bounds, method='L-BFGS-B')
if result.success:
omega, garch_alpha, garch_beta = result.x
var_forecast = omega + garch_alpha * window_returns.iloc[-1]**2 + garch_beta * np.var(window_returns)
if var_forecast < 1e-6:
var_forecast = 1e-6
forecast_var = np.sqrt(var_forecast)
garch_volatility[i] = forecast_var
z_score = norm.ppf(1 - self.alpha)
z_pdf = norm.pdf(z_score)
VaR_series[current_index] = -z_score * forecast_var
ES_series[current_index] = forecast_var / self.alpha * z_pdf
return pd.concat([
pd.Series(garch_volatility[self.window:], name="volatility", index=self.returns.index[self.window:]),
pd.DataFrame.from_dict(VaR_series, orient="index", columns=[f"VaR_{alpha_:.4f}" for alpha_ in self.alpha]),
pd.DataFrame.from_dict(ES_series, orient="index", columns=[f"ES_{alpha_:.4f}" for alpha_ in self.alpha])
], axis=1)
def GARCH_t_VaR(self):
def garch_log_likelihood(params, returns, nu):
omega, garch_alpha, garch_beta = params
n = len(returns)
var = np.zeros(n)
var[0] = np.var(returns) + 1e-6
for t in range(1, n):
var[t] = omega + garch_alpha * returns.iloc[t-1]**2 + garch_beta * var[t-1]
if var[t] < 1e-6 or np.isnan(var[t]) or np.isinf(var[t]):
var[t] = 1e-6
log_likelihood_constant_term = np.log(gamma((nu + 1) / 2)) - np.log(gamma(nu / 2)) - 0.5 * np.log((nu - 2) * np.pi)
log_likelihood_variable_term = - 0.5 * np.log(var) - (nu + 1) / 2 * np.log(1 + (returns ** 2) / ((nu - 2) * var))
log_likelihood = np.sum(log_likelihood_constant_term + log_likelihood_variable_term)
return log_likelihood
VaR_series, ES_series = {}, {}
garch_volatility = np.zeros(len(self.returns))
for i in range(self.window, len(self.returns)):
window_returns = self.returns[i-self.window:i]
current_index = self.returns.index[i]
initial_params = [0.1, 0.1, 0.8]
bounds = [(1e-6, None), (0, 1), (0, 1)]
result = minimize(garch_log_likelihood, initial_params, args=(window_returns, self.nu), bounds=bounds, method='L-BFGS-B')
if result.success:
omega, garch_alpha, garch_beta = result.x
var_forecast = omega + garch_alpha * window_returns.iloc[-1]**2 + garch_beta * np.var(window_returns)
if var_forecast < 1e-6:
var_forecast = 1e-6
forecast_var = np.sqrt(var_forecast)
garch_volatility[i] = forecast_var
t_score = t.ppf(1 - self.alpha, df=self.nu)
t_ES = t.pdf(t_score, df=self.nu) / self.alpha
t_ES *= (self.nu + t_score ** 2) / (self.nu - 1)
VaR_series[current_index] = -t_score * forecast_var
ES_series[current_index] = forecast_var * t_ES
return pd.concat([
pd.Series(garch_volatility[self.window:], name="volatility", index=self.returns.index[self.window:]),
pd.DataFrame.from_dict(VaR_series, orient="index", columns=[f"VaR_{alpha_:.4f}" for alpha_ in self.alpha]),
pd.DataFrame.from_dict(ES_series, orient="index", columns=[f"ES_{alpha_:.4f}" for alpha_ in self.alpha])
], axis=1)
def GAS_GARCH_VaR(self):
def score_function(returns, var):
return (returns**2 / var - 1)
def gas_log_likelihood(params, returns):
omega, garch_alpha, garch_beta = params
n = len(returns)
var = np.zeros(n)
var[0] = np.var(returns) + 1e-6 # Initialize with sample variance
score = np.zeros(n)
for t in range(1, n):
score[t-1] = score_function(returns.iloc[t-1], var[t-1])
var[t] = omega + garch_alpha * score[t-1] * var[t-1] + garch_beta * var[t-1]
if var[t] < 1e-6 or np.isnan(var[t]) or np.isinf(var[t]):
var[t] = 1e-6
log_likelihood = -0.5 * np.sum(np.log(2 * np.pi) + np.log(var) + returns**2 / var)
return -log_likelihood
VaR_series, ES_series = {}, {}
garch_volatility = np.zeros(len(self.returns))
for i in range(self.window, len(self.returns)):
window_returns = self.returns[i-self.window:i]
current_index = self.returns.index[i]
initial_params = [0.01, 0.01, 0.9]
bounds = [(1e-6, None), (0, 1), (0, 1)]
result = minimize(gas_log_likelihood, initial_params, args=(window_returns,), bounds=bounds, method='L-BFGS-B')
if result.success:
omega, garch_alpha, garch_beta = result.x
last_return = window_returns.iloc[-1]
last_var = np.var(window_returns)
last_score = score_function(last_return, last_var)
var_forecast = omega + garch_alpha * last_score * last_var + garch_beta * last_var
if var_forecast < 1e-6:
var_forecast = 1e-6
forecast_var = np.sqrt(var_forecast)
garch_volatility[i] = forecast_var
z_score = norm.ppf(1 - self.alpha)
z_pdf = norm.pdf(z_score)
VaR_series[current_index] = -z_score * forecast_var
ES_series[current_index] = forecast_var / self.alpha * z_pdf
return pd.concat([
pd.Series(garch_volatility[self.window:], name="volatility", index=self.returns.index[self.window:]),
pd.DataFrame.from_dict(VaR_series, orient="index", columns=[f"VaR_{alpha_:.4f}" for alpha_ in self.alpha]),
pd.DataFrame.from_dict(ES_series, orient="index", columns=[f"ES_{alpha_:.4f}" for alpha_ in self.alpha])
], axis=1)
def GAS_GARCH_t_VaR(self):
def score_function(returns, var, nu):
term1 = 1 + (returns**2 / ((nu - 2) * var))
term2 = (returns**2 * (nu + 1) / (var * (nu - 2)) - var)
scaling_term = (nu + 3) / nu
return scaling_term * term1**(-1) * term2
def gas_log_likelihood(params, returns, nu):
omega, garch_alpha, garch_beta = params
n = len(returns)
var = np.zeros(n)
var[0] = np.var(returns) + 1e-6 # Initialize with sample variance
score = np.zeros(n)
for t in range(1, n):
score[t-1] = score_function(returns.iloc[t-1], var[t-1], nu)
var[t] = omega + garch_alpha * score[t-1] * var[t-1] + garch_beta * var[t-1]
if var[t] < 1e-6 or np.isnan(var[t]) or np.isinf(var[t]):
var[t] = 1e-6
log_likelihood = -0.5 * np.sum(np.log(var) + (nu + 1) * np.log(1 + returns**2 / (nu * var)))
return -log_likelihood
VaR_series, ES_series = {}, {}
garch_volatility = np.zeros(len(self.returns))
for i in range(self.window, len(self.returns)):
window_returns = self.returns[i-self.window:i]
current_index = self.returns.index[i]
initial_params = [0.01, 0.01, 0.9]
bounds = [(1e-6, None), (0, 1), (0, 1)]
options = {'maxiter': 100}
result = minimize(gas_log_likelihood, initial_params, args=(window_returns, self.nu), bounds=bounds, method='L-BFGS-B', options=options)
if result.success:
omega, garch_alpha, garch_beta = result.x
last_return = window_returns.iloc[-1]
last_var = np.var(window_returns)
last_score = score_function(last_return, last_var, self.nu)
var_forecast = omega + garch_alpha * last_score * last_var + garch_beta * last_var
if var_forecast < 1e-6:
var_forecast = 1e-6
forecast_var = np.sqrt(var_forecast)
garch_volatility[i] = forecast_var
t_score = t.ppf(1 - self.alpha, df=self.nu)
t_ES = t.pdf(t_score, df=self.nu) / self.alpha
t_ES *= (self.nu + t_score ** 2) / (self.nu - 1)
VaR_series[current_index] = -t_score * forecast_var
ES_series[current_index] = forecast_var * t_ES
return pd.concat([
pd.Series(garch_volatility[self.window:], name="volatility", index=self.returns.index[self.window:]),
pd.DataFrame.from_dict(VaR_series, orient="index", columns=[f"VaR_{alpha_:.4f}" for alpha_ in self.alpha]),
pd.DataFrame.from_dict(ES_series, orient="index", columns=[f"ES_{alpha_:.4f}" for alpha_ in self.alpha])
], axis=1)