-
Notifications
You must be signed in to change notification settings - Fork 0
/
MetropolisHastings.py
167 lines (138 loc) · 6.32 KB
/
MetropolisHastings.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
import numpy as np
import pandas as pd
import os
import logging
from abc import ABC, abstractmethod
import statsmodels.api as sm
from statsmodels.base.model import GenericLikelihoodModel
from scipy import stats
import matplotlib.pyplot as plt
logging.basicConfig(level=logging.DEBUG)
class MetropolisHastings(ABC):
def __init__(self, burnIn=1000):
self.logger = logging.getLogger(self.__class__.__name__)
self.burnIn = burnIn
@abstractmethod
def sampleFromProposalDensity(self, state0):
raise NotImplementedError("Base class needs to implement")
@abstractmethod
def proposalDensity(self, state0, state1):
raise NotImplementedError("Base class needs to implement")
@abstractmethod
def targetProb(self, state, params):
raise NotImplementedError("Base class needs to implement")
def sample(self, N, initial, params, burnIn=None):
if burnIn is None:
burnIn = self.burnIn
samples = np.zeros(N, dtype=np.float64)
state0 = initial
i = 0
while i < burnIn + N:
state = self.sampleFromProposalDensity(state0)
fac1 = self.proposalDensity(state, state0) / self.proposalDensity(state0, state)
fac2 = self.targetProb(state, params) / self.targetProb(state0, params)
acceptanceProb = min(fac1 * fac2, 1)
u = np.random.random(1)
if u <= acceptanceProb:
state0 = state
if i >= burnIn:
samples[i - burnIn] = state0
i += 1
return samples
class Garch11Model(GenericLikelihoodModel):
def __init__(self, endog, exog):
super().__init__(endog=endog, exog=exog)
self.endog = endog
self.exog = sm.add_constant(exog, has_constant="add")
assert self.exog.shape[1] == 3
self.parameters = np.random.random(3)
def loglikeobs(self, params):
pred = np.einsum("ij,j->i", self.exog, params)
return np.sum(stats.norm.logpdf(pred, self.endog, 1))
def fit(self, **kwargs):
return super().fit(self.parameters, method="bfgs")
def predict(self, exog):
exog = sm.add_constant(exog, has_constant="add")
return np.einsum("ij,j->i", exog, self.parameters)
class SP500ReturnPosterior(MetropolisHastings):
PRICE_COL = "Close"
PERIOD = 5
def __init__(self, dirname, security, trainTestRatio=0.9):
super().__init__()
self.logger = logging.getLogger(self.__class__.__name__)
self.dirname = dirname
self.df = pd.read_csv(os.path.join(dirname, f"{security}.csv"), parse_dates=["Date"])
self.trainTestRatio = trainTestRatio
self.ntraining = int(self.df.shape[0] * trainTestRatio)
self.garchModel = None
self.calculateEndogExogVars()
self.volatForProb = None
def calculateEndogExogVars(self):
price = self.df.loc[:, self.PRICE_COL].values
returns = price[1:] / price[0:-1] - 1
self.df.loc[:, "returns"] = 0
self.df.loc[1:, "returns"] = returns
self.df.loc[:, "returns_square"] = self.df.loc[:, "returns"] ** 2
self.df.loc[:, "volat"] = 0
self.df.loc[:, "lagged_volat"] = 0
sumsq = np.sum(returns[0:self.PERIOD] ** 2)
for i in range(self.PERIOD, self.df.shape[0]-1, 1):
self.df.loc[i, "volat"] = sumsq / self.PERIOD
self.df.loc[i+1, "lagged_volat"] = self.df.loc[i, "volat"]
sumsq += returns[i] * returns[i] - returns[i - self.PERIOD] * returns[i - self.PERIOD]
def fitGarch(self):
endog = self.df.loc[self.PERIOD+1:self.ntraining, "volat"].values
exog = self.df.loc[self.PERIOD+1:self.ntraining, ["returns_square", "lagged_volat"]].values
self.garchModel = Garch11Model(endog=endog, exog=exog)
res = self.garchModel.fit()
self.logger.info(res.summary())
self.garchModel.parameters = res.params
self.volatForProb = res.params[0] / (1 - res.params[2])
def fitMHSampler(self):
state = self.df.loc[self.ntraining, "returns"]
mu = np.mean(self.df.loc[self.ntraining - self.PERIOD:self.ntraining, "returns"].values)
volat = self.df.loc[self.ntraining, "volat"]
params = (mu, volat)
self.sample(1, state, params)
def fit(self):
self.fitGarch()
self.fitMHSampler()
def sampleFromProposalDensity(self, state0):
return np.random.normal(size=1, loc=state0, scale=self.volatForProb)
def proposalDensity(self, state0, state1):
return stats.norm.pdf(state0 - state1, 0, 1)
def targetProb(self, state, params):
mu, volat = params
nu = 2 * volat / (1 - volat)
return (1 + (state - mu)**2/nu) ** (-(nu+1)/2) * stats.norm.pdf(state, mu, 1)
def test(self):
exog = self.df.loc[self.ntraining:, ["returns_square", "lagged_volat"]].values
actual = self.df.loc[self.ntraining:, "volat"].values
predictedVol = self.garchModel.predict(exog)
sampledVol = np.zeros(self.df.shape[0]-1-self.ntraining, dtype=np.float64)
x = self.df.loc[self.ntraining:, "Date"].values
for i in range(self.ntraining, self.df.shape[0]-1, 1):
vol = predictedVol[i-self.ntraining]
self.volatForProb = self.df.loc[i, "lagged_volat"]
mu = np.mean(self.df.loc[i-self.PERIOD:i, "returns"].values)
initial = self.df.loc[i, "returns"]
params = (mu, vol)
returns = self.sample(20, initial, params, burnIn=0)
sampledVol[i-self.ntraining] = np.std(returns)
plt.figure(figsize=(10, 10))
plt.plot(x[0:-1], sampledVol, label="Sampled")
plt.plot(x[0:-1], predictedVol[0:-1], label="GARCH(1,1)")
plt.plot(x[0:-1], actual[0:-1], label="Empirical")
plt.grid()
plt.legend()
plt.xlabel("Date")
plt.ylabel("Daily Volatility")
plt.savefig(os.path.join(self.dirname, "mcmc_variance.jpeg"),
dpi=500)
plt.show()
if __name__ == "__main__":
dirname = r"C:\prog\cygwin\home\samit_000\latex\book_stats\code\data"
posterior = SP500ReturnPosterior(dirname, "SPY")
np.random.seed(32)
posterior.fit()
posterior.test()