-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathChapter7.py
More file actions
212 lines (150 loc) · 4.88 KB
/
Chapter7.py
File metadata and controls
212 lines (150 loc) · 4.88 KB
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
205
206
207
208
209
210
211
212
import numpy as np
import pandas as pd
from scipy import stats, optimize
import seaborn as sns
import statsmodels.api as sm
from arch.univariate import ZeroMean, StudentsT
from archEx.NGARCH import NGARCH11
scale = 100
# Exercise 1
data = pd.read_csv(
'data/Chapter7_Data.csv', parse_dates=True, index_col='date')
returns = data.apply(np.log) - data.apply(np.log).shift()
returns.dropna(inplace=True)
returns *= scale
returns.plot()
# Exercise 2
print(returns.cov())
print(returns.corr())
sns.regplot(
x='sp500', y='tnote10',
data=returns)
# Exercise 3
returns['portfolio'] = (returns['sp500'] + returns['tnote10']) / 2
std_sp = stats.norm.fit(returns['sp500'], floc=0)[1]
std_tn = stats.norm.fit(returns['tnote10'], floc=0)[1]
std_p = stats.norm.fit(returns['portfolio'], floc=0)[1]
VaRsp = -stats.norm.ppf(0.01, 0, scale=std_sp)
VaRtn = -stats.norm.ppf(q=0.01, scale=std_tn)
VaRp = -stats.norm.ppf(q=0.01, scale=std_p)
print(VaRp < (VaRsp + VaRtn) / 2)
# Exercise 4
omega = 1.5E-6 * scale ** 2
alpha = 0.05
beta = 0.8
theta = 1.25
tsm = ZeroMean(returns['sp500'])
ngarch = NGARCH11(
np.array([omega, alpha, beta, theta]))
tsm.volatility = ngarch
tsm.distribution = StudentsT()
sp500_rst = tsm.fit()
print(sp500_rst)
sp500_rst.plot(annualize='D')
sns.distplot(sp500_rst.std_resid, fit=stats.t)
print(
ngarch.is_valid(
sp500_rst.params['alpha'],
sp500_rst.params['beta'],
sp500_rst.params['theta']))
sm.graphics.qqplot(
sp500_rst.std_resid, line='45')
returns['std_sp500'] = sp500_rst.std_resid
omega = 5E-6 * scale ** 2
alpha = 0.03
beta = 0.97
theta = 0.0
tsm = ZeroMean(returns['tnote10'])
ngarch = NGARCH11(
np.array([omega, alpha, beta, theta]))
tsm.volatility = ngarch
tsm.distribution = StudentsT()
tnote10_rst = tsm.fit()
print(tnote10_rst)
tnote10_rst.plot(annualize='D')
sns.distplot(tnote10_rst.std_resid, fit=stats.t)
print(
ngarch.is_valid(
tnote10_rst.params['alpha'],
tnote10_rst.params['beta'],
tnote10_rst.params['theta']))
sm.graphics.qqplot(
tnote10_rst.std_resid, line='45')
returns['std_tnote10'] = tnote10_rst.std_resid
sns.regplot(
x='std_sp500', y='std_tnote10',
data=returns)
print(returns[['std_sp500', 'std_tnote10']].cov())
print(returns[['std_sp500', 'std_tnote10']].corr())
# Exercise 5
def ExponentialSmootherLogLikelihood(data: pd.DataFrame):
z = np.asmatrix(data.values.T)
def LogLikelihood(x: np.ndarray) -> float:
# start value of Q
Q = np.asmatrix(np.corrcoef(z))
row_len, col_len = z.shape
log_likelihood = 0.0
lmd = x[0]
for i in range(col_len):
diag = np.asmatrix(np.diag(np.diag(Q)))
sqrt_diag_inv = np.sqrt(diag.I)
R = np.asmatrix(
np.dot(np.dot(sqrt_diag_inv, Q), sqrt_diag_inv))
core = np.log(np.linalg.det(R)) + np.dot(np.dot(z[:, i].T, R.I), z[:, i])
# core is matrix
log_likelihood += -0.5 * core[0, 0]
# update Q
Q = (1 - lmd) * np.dot(z[:, i], z[:, i].T) + lmd * Q
return log_likelihood
return LogLikelihood
logLikelihood = ExponentialSmootherLogLikelihood(
returns[['std_sp500', 'std_tnote10']])
x = np.arange(0.9, 0.99, 0.005)
y = [logLikelihood(x[i:(i + 1)]) for i in range(len(x))]
logLikelihoodTab = pd.DataFrame(
{'x': x, 'y': y})
sns.lineplot(
x='x', y='y', data=logLikelihoodTab)
func = lambda x: -logLikelihood(x)
x0 = np.array([0.94])
res = optimize.minimize(
func, x0, method='SLSQP', bounds=[(0.9, 0.99)])
print(res)
# Exercise 6
def MeanRevertingLogLikelihood(data: pd.DataFrame):
z = np.asmatrix(data.values.T)
def LogLikelihood(x: np.ndarray) -> float:
# start value of Q and long-run correlation
Q = np.asmatrix(np.corrcoef(z))
ER = np.asmatrix(np.corrcoef(z))
row_len, col_len = z.shape
log_likelihood = 0.0
alpha = x[0]
beta = x[1]
for i in range(col_len):
diag = np.asmatrix(np.diag(np.diag(Q)))
sqrt_diag_inv = np.sqrt(diag.I)
R = np.asmatrix(
np.dot(np.dot(sqrt_diag_inv, Q), sqrt_diag_inv))
core = np.log(np.linalg.det(R)) + np.dot(np.dot(z[:, i].T, R.I), z[:, i])
# core is matrix
log_likelihood += -0.5 * core[0, 0]
# update Q
Q = (1 - alpha - beta) * ER + \
alpha * np.dot(z[:, i], z[:, i].T) + \
beta * Q
return log_likelihood
return LogLikelihood
logLikelihood = MeanRevertingLogLikelihood(
returns[['std_sp500', 'std_tnote10']])
func = lambda x: -logLikelihood(x)
x0 = np.array([0.05, 0.9])
cons = optimize.LinearConstraint(
A=np.array([[1.0, 0.0], [0.0, 1.0], [1.0, 1.0]]),
lb=np.zeros(3),
ub=np.ones(3))
res = optimize.minimize(
func, x0, method='SLSQP',
bounds=[(0.0, 0.2), (0.8, 1.0)],
constraints=cons)
print(res)