-
Notifications
You must be signed in to change notification settings - Fork 0
/
msm.py
321 lines (233 loc) · 7.14 KB
/
msm.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import numpy as np
import pandas as pd
import scipy.optimize as opt
from statsmodels import regression
import statsmodels.formula.api as sm
from numba import jit, njit, prange, float64, int64
def glo_min(kbar, data, niter, temperature, stepsize):
"""2-step basin-hopping method combines global stepping algorithm
with local minimization at each step.
"""
"""step 1: local minimizations
"""
theta, theta_LLs, theta_out, ierr, numfunc = loc_min(kbar, data)
"""step 2: global minimum search uses basin-hopping
(scipy.optimize.basinhopping)
"""
# objective function
f = g_LLb_h
# x0 = initial guess, being theta, from Step 1.
# Presents as: [b, m0, gamma_kbar, sigma]
x0 = theta
# basinhopping arguments
niter = niter
T = temperature
stepsize = stepsize
args = (kbar, data)
# bounds
bounds = ((1.001,50),(1,1.99),(1e-3,0.999999),(1e-4,5))
# minimizer_kwargs
minimizer_kwargs = dict(method = "L-BFGS-B", bounds = bounds, args = args)
res = opt.basinhopping(
func = f, x0 = x0, niter = niter, T = T, stepsize = stepsize,
minimizer_kwargs = minimizer_kwargs)
parameters, LL, niter, output = res.x,res.fun,res.nit,res.message
return(parameters, LL, niter, output)
def loc_min(kbar, data):
"""step 1: local minimization
parameter estimation uses bounded optimization (scipy.optimize.fminbound)
"""
# set up
b = np.array([1.5, 5, 15, 30])
lb = len(b)
gamma_kbar = np.array([0.1, 0.5, 0.9, 0.95])
lg = len(gamma_kbar)
sigma = np.std(data)
# templates
theta_out = np.zeros(((lb*lg),3))
theta_LLs = np.zeros((lb*lg))
# objective function
f = g_LL
# bounds
m0_l = 1.2
m0_u = 1.8
# Optimizaton stops when change in x between iterations is less than xtol
xtol = 1e-05
# display: 0, no message; 1, non-convergence; 2, convergence;
# 3, iteration results.
disp = 1
idx = 0
for i in range(lb):
for j in range(lg):
# args
theta_in = [b[i], gamma_kbar[j], sigma]
args = (kbar, data, theta_in)
xopt, fval, ierr, numfunc = opt.fminbound(
func = f, x1 = m0_l, x2 = m0_u, xtol = xtol,
args = args, full_output = True, disp = disp)
m0, LL = xopt, fval
theta_out[idx,:] = b[i], m0, gamma_kbar[j]
theta_LLs[idx] = LL
idx +=1
idx = np.argsort(theta_LLs)
theta_LLs = np.sort(theta_LLs)
theta = theta_out[idx[0],:].tolist()+[sigma]
theta_out = theta_out[idx,:]
return(theta, theta_LLs, theta_out, ierr, numfunc)
def g_LL(m0, kbar, data, theta_in):
"""return LL, the vector of log likelihoods
"""
# set up
b = theta_in[0]
gamma_kbar = theta_in[1]
sigma = theta_in[2]
kbar2 = 2**kbar
T = len(data)
pa = (2*np.pi)**(-0.5)
# gammas and transition probabilities
A = g_t(kbar, b, gamma_kbar)
# switching probabilities
g_m = s_p(kbar, m0)
# volatility model
s = sigma*g_m
# returns
w_t = data
w_t = pa*np.exp(-0.5*((w_t/s)**2))/s
w_t = w_t + 1e-16
# log likelihood using numba
LL = _LL(kbar2, T, A, g_m, w_t)
return(LL)
@jit(nopython=True)
def _LL(kbar2, T, A, g_m, w_t):
"""speed up Bayesian recursion with numba
"""
LLs = np.zeros(T)
pi_mat = np.zeros((T+1,kbar2))
pi_mat[0,:] = (1/kbar2)*np.ones(kbar2)
for t in range(T):
piA = np.dot(pi_mat[t,:],A)
C = (w_t[t,:]*piA)
ft = np.sum(C)
if abs(ft-0) <= 1e-05:
pi_mat[t+1,1] = 1
else:
pi_mat[t+1,:] = C/ft
# vector of log likelihoods
LLs[t] = np.log(np.dot(w_t[t,:],piA))
LL = -np.sum(LLs)
return(LL)
def g_pi_t(m0, kbar, data, theta_in):
"""return pi_t, the current distribution of states
"""
# set up
b = theta_in[0]
gamma_kbar = theta_in[1]
sigma = theta_in[2]
kbar2 = 2**kbar
T = len(data)
pa = (2*np.pi)**(-0.5)
pi_mat = np.zeros((T+1,kbar2))
pi_mat[0,:] = (1/kbar2)*np.ones(kbar2)
# gammas and transition probabilities
A = g_t(kbar, b, gamma_kbar)
# switching probabilities
g_m = s_p(kbar, m0)
# volatility model
s = sigma*g_m
# returns
w_t = data
w_t = pa*np.exp(-0.5*((w_t/s)**2))/s
w_t = w_t + 1e-16
# compute pi_t with numba acceleration
pi_t = _t(kbar2, T, A, g_m, w_t)
return(pi_t)
@jit(nopython=True)
def _t(kbar2, T, A, g_m, w_t):
pi_mat = np.zeros((T+1,kbar2))
pi_mat[0,:] = (1/kbar2)*np.ones(kbar2)
for t in range(T):
piA = np.dot(pi_mat[t,:],A)
C = (w_t[t,:]*piA)
ft = np.sum(C)
if abs(ft-0) <= 1e-05:
pi_mat[t+1,1] = 1
else:
pi_mat[t+1,:] = C/ft
pi_t = pi_mat[-1,:]
return(pi_t)
class memoize(dict):
"""use memoize decorator to speed up compute of the
transition probability matrix A
"""
def __init__(self, func):
self.func = func
def __call__(self, *args):
return self[args]
def __missing__(self, key):
result = self[key] = self.func(*key)
return result
@memoize
def g_t(kbar, b, gamma_kbar):
"""return A, the transition probability matrix
"""
# compute gammas
gamma = np.zeros((kbar,1))
gamma[0,0] = 1-(1-gamma_kbar)**(1/(b**(kbar-1)))
for i in range(1,kbar):
gamma[i,0] = 1-(1-gamma[0,0])**(b**(i))
gamma = gamma*0.5
gamma = np.c_[gamma,gamma]
gamma[:,0] = 1 - gamma[:,0]
# transition probabilities
kbar2 = 2**kbar
prob = np.ones(kbar2)
for i in range(kbar2):
for m in range(kbar):
tmp = np.unpackbits(
np.arange(i,i+1,dtype = np.uint16).view(np.uint8))
tmp = np.append(tmp[8:],tmp[:8])
prob[i] =prob[i] * gamma[kbar-m-1,tmp[-(m+1)]]
A = np.fromfunction(
lambda i,j: prob[np.bitwise_xor(i,j)],(kbar2,kbar2),dtype = np.uint16)
return(A)
def j_b(x, num_bits):
"""vectorize first part of computing transition probability matrix A
"""
xshape = list(x.shape)
x = x.reshape([-1, 1])
to_and = 2**np.arange(num_bits).reshape([1, num_bits])
return (x & to_and).astype(bool).astype(int).reshape(xshape + [num_bits])
@jit(nopython=True)
def s_p(kbar, m0):
"""speed up computation of switching probabilities with Numba
"""
# switching probabilities
m1 = 2-m0
kbar2 = 2**kbar
g_m = np.zeros(kbar2)
g_m1 = np.arange(kbar2)
for i in range(kbar2):
g = 1
for j in range(kbar):
if np.bitwise_and(g_m1[i],(2**j))!=0:
g = g*m1
else:
g = g*m0
g_m[i] = g
return(np.sqrt(g_m))
def g_LLb_h(theta, kbar, data):
"""bridge global minimization to local minimization
"""
theta_in = unpack(theta)
m0 = theta[1]
LL = g_LL(m0, kbar, data, theta_in)
return(LL)
def unpack(theta):
"""unpack theta, package theta_in
"""
b = theta[0]
m0 = theta[1]
gamma_kbar = theta[2]
sigma = theta[3]
theta_in = [b, gamma_kbar, sigma]
return(theta_in)