-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcore.py
349 lines (232 loc) · 11.8 KB
/
core.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
from Multivariate_Markov_Switching_Model.tools import *
from Multivariate_Markov_Switching_Model.tools import _2dim
import statsmodels.api as sm
from Multivariate_Markov_Switching_Model.summaries import *
class Markov_Multivarite_Regression(object):
def __init__(self,y,x,z,k_regimes,variance_regimes,covariance_type,apriori,**kwargs):
self.original_y = y
self.original_x = x
self.original_z = z
self.mean_variance = False
if z is None and (len(np.unique(x)) == 1 and x.flatten()[0] == 1):
self.mean_variance = True
self.covariance_type = covariance_type
self.variance_regimes = variance_regimes
self.apriori = apriori
self.k_regimes = k_regimes
self.has_delta = True
self.x,self.y,self.z = Data(self.original_y,self.original_x,self.original_z,self.mean_variance)()
if len(np.unique(self.z)) == 1 and self.z.flatten()[0] == 0:
self.has_delta = False
self.nobs,self.neqs_y = self.y.shape
self.neqs_x = self.x.shape[1]
self.neqs_z = self.z.shape[1]
self.indices,self.cov_obs = slicing_parameter(self.k_regimes,self.neqs_y,self.neqs_z,
self.neqs_x,self.covariance_type,self.variance_regimes)
self.start_params = None
if not isinstance(self.apriori,(pd.Series,np.ndarray)):
self.apriori = clustering(self.y,self.k_regimes)
self.kwargs = kwargs
self.results = {'status': -1}
self.parameters = {"nobs":self.nobs,"neqs_y":self.neqs_y,"neqs_x":self.neqs_x,
"neqs_z":self.neqs_z,"k_regimes":self.k_regimes,
"mean_variance":self.mean_variance,"covariance_type":covariance_type,"variance_regimes":variance_regimes,
"indices":self.indices}
self.current_params = None
def fit(self):
param = start_params(y=self.y,x=self.x,z=self.z,k_regimes=self.k_regimes,covariance_type=self.covariance_type,
variance_regimes=self.variance_regimes,apriori=self.apriori,mean_variance=self.mean_variance,has_delta=self.has_delta)
self.start_params = param
import scipy.optimize as opt
init_param = param.squeeze()
maxiters = self.kwargs.setdefault("maxiters",1e+3)
epsilon=self.kwargs.setdefault("eps",np.sqrt(np.finfo(float).eps))
# epsilon = self.kwargs.setdefault('epsilon', np.sqrt(np.finfo(float).eps))
gtol = self.kwargs.setdefault("gtol",1.0000000000000001e-05)
norm = self.kwargs.setdefault('norm', np.Inf)
proper_input = isinstance(maxiters,float) == isinstance(epsilon,float) == isinstance(gtol,float)
if not proper_input:
raise Exception("pls enter the correct input")
options = {"maxiter":maxiters,"eps":epsilon,"gtol":gtol,"norm":norm}
# options = {"maxiter":maxiters,"gtol":gtol}
# gtol = self.kwargs.setdefault('gtol', 1.0000000000000001e-05)
# norm = self.kwargs.setdefault('norm', np.Inf)
# epsilon = self.kwargs.setdefault('epsilon', 1.4901161193847656e-08)
rounds = 0
while True:
rounds += 1
# print(rounds)
# ,callback=progress_bar(maxiters)
res = opt.minimize(self.llf,init_param,method="BFGS",options=options)
self.results["unconstrained"] = res.x[:,None]
self.results['likelihoods'] = res.fun
if res.status == 0:
final_parameters = res.x
if final_parameters.ndim <2:
final_parameters = final_parameters[:,None]
parameter = self.transform_params(final_parameters)
self.results['constrained'] = parameter
self.results['jac'] = res.jac
self.results['status'] = 0
return parameter
elif res.status == 2:
init_param = res.x
options['eps'] *= (options['gtol'])
else:
print(res)
raise Exception("something wrong pls check ")
if rounds>=10:
print("out of space")
return res
def llf(self,param):
param = _2dim(param)
parameter = self.transform_params(param)
results = self._filter(parameter)
return results
def convert_param(self,param):
"""transforms a vector of parameters in transition probability and ergodic matrices
beta coefficient and var-cov matrices
"""
cut_off = self.indices
from Multivariate_Markov_Switching_Model.tools import _p_transition,_cov_mat,_p_ergodic
p_trans = _p_transition(param[cut_off[0]:cut_off[1]], self.k_regimes)
p_ergodic = _p_ergodic(p_trans, self.k_regimes)
b = param[cut_off[1]:cut_off[2]].reshape(self.neqs_x, self.neqs_y * self.k_regimes)
sig_mat, inv_sig_mat, det_inv_sig_mat \
= _cov_mat(param[cut_off[2]:cut_off[3]].reshape(self.cov_obs, self.variance_regimes), self.neqs_y, self.covariance_type, self.variance_regimes)
d = np.kron(np.ones((1, self.k_regimes)), param[cut_off[3]:cut_off[4]].reshape(self.neqs_z, self.neqs_y)) if self.has_delta else 0
return p_ergodic, p_trans, b, d, sig_mat, inv_sig_mat, det_inv_sig_mat
def _filter(self,parameter):
"""apply hamilton filter"""
p_j, p_ij, b, d, var_mat, inv_var_mat,det_inv_var_mat = self.convert_param(parameter)
self.current_params = parameter
if np.sometrue(np.greater_equal(p_ij,1)):
return np.inf
nobs = self.nobs
y_hat = np.zeros((nobs,self.neqs_y))
p_predicted_joint = np.zeros((nobs,self.k_regimes))
joint_likelihoods = np.zeros((nobs,1))
filtered_probabilities = np.zeros((nobs+1,self.k_regimes))
filtered_probabilities[0,...] = p_j.T
mu = self.x.dot(b)+self.z.dot(d)
_y = np.kron(np.ones((1,self.k_regimes)),self.y)
residual = _y-mu
if np.sometrue(np.less(det_inv_var_mat,0)):
return np.inf
# filtered joint probabilities
for i in np.arange(nobs):
cond_likelihoods = self._cond_densities(residual[i,:].T,inv_var_mat,det_inv_var_mat).T
# P(S(t)=i,Y(t)|I(t-1))
p_predicted_joint[i,...] = p_ij.dot(filtered_probabilities[i,...])
tmp = cond_likelihoods*p_predicted_joint[i,...]
joint_likelihoods[i] = tmp.sum()
if np.isnan(joint_likelihoods[i]):
return np.inf
filtered_probabilities[i+1,...] = tmp/joint_likelihoods[i]
y_hat[i,:] = filtered_probabilities[i+1,...].dot(mu[i,:].reshape(self.k_regimes,self.neqs_y))
resid = self.y-y_hat
likelihoods = -(np.log(joint_likelihoods).sum())
if np.isnan(likelihoods):
raise Exception("Please Check the Calculation ")
self.results["resid"] = resid
self.results["joint_likelihoods"] = joint_likelihoods
self.results['filtered_probabilities'] = filtered_probabilities
self.results['p_predicted_joint'] = p_predicted_joint
# ,y_hat,resid,joint_likelihoods,filtered_probabilities,p_predicted_joint
return likelihoods
def _cond_densities(self,res,inv_var_mat, det_inv_var_mat):
"""compute the conditional densities """
k_regimes = self.k_regimes
neqs_y = self.neqs_y
_resid = res.reshape(neqs_y,k_regimes).T
if self.variance_regimes == 1:
sig = np.kron(np.ones((1,k_regimes)),inv_var_mat)
det_sig = np.kron(np.ones((1,k_regimes)),det_inv_var_mat).T
else:
sig = inv_var_mat
det_sig = det_inv_var_mat[:,np.newaxis]
_resid = _resid.flatten(order="F")[:,np.newaxis]
aux = _resid*np.kron(np.eye(k_regimes),np.ones((neqs_y,1)))
sigma = np.kron(np.eye(k_regimes),np.ones((neqs_y,neqs_y)))*(np.kron(np.ones((k_regimes,1)),sig))
w = aux.T.dot(sigma).dot(aux)
v = np.diag(w)[:,np.newaxis]
eta = (1/np.sqrt(2*np.pi))**neqs_y*np.sqrt(det_sig)*np.exp(-0.5*v)
return eta
def transform_params(self,unconstrained):
"""create a constrained parameter g=g(theta) to enter the likelihood function"""
unconstrained = _2dim(unconstrained)
k_regimes = self.k_regimes
neqs_y = self.neqs_y
slices = self.indices
aux_p = unconstrained[:(k_regimes-1)*k_regimes].reshape((k_regimes-1),k_regimes)
# FIXME over flow ignore
_ = np.seterr(over='ignore')
B = np.exp(aux_p)
masks = np.isinf(B)
sumx = B.sum(axis=0)+np.ones((1,k_regimes))[0]
aux_p = B/sumx
aux_p = np.where(masks,1,aux_p)
from copy import deepcopy
c = deepcopy(unconstrained)
# FIXME
prob = np.hstack([aux_p.flatten()[:,np.newaxis],0.001*np.ones(((k_regimes-1)*k_regimes,1))])
# c.extend(prob.max(axis=1))
c[:(k_regimes-1)*k_regimes] = prob.max(axis=1)[:,np.newaxis]
v = c[slices[2]:slices[3],:]
aux_v = v.reshape(self.cov_obs,self.variance_regimes)
for i in np.arange(self.variance_regimes):
if self.covariance_type == 'full':
_value = aux_v[:,i]
m_aux = xpnd(_value)
diag_mat = np.abs(np.diag(m_aux))*(np.eye(neqs_y))
rho_mat = m_aux-diag_mat
rho_mat = rho_constraints(rho_mat)+np.eye(neqs_y)
m_aux = diag_mat.dot(rho_mat).dot(diag_mat)
aux_v[:,i] = vech(m_aux)
else:
aux_v[:,i] = aux_v[:,i]**2
c[slices[2]:slices[3]] = vecr(aux_v)[:,np.newaxis]
return c
def summary(self):
s = MSVARResults(self)
return s.summary()
def smooth_probabilities(self):
results = self.results
p_filtered_joint = results['filtered_probabilities'][1:]
p_predicted_joint = results['p_predicted_joint']
parameter = results['constrained']
p_j, p_ij, b, d, var_mat, inv_var_mat,det_inv_var_mat = self.convert_param(parameter)
_ = convert_smooth(p_filtered_joint, p_predicted_joint, p_ij)
return _
i=0
import time
import sys
class progress_bar(object):
def __init__(self,iter):
# self.max_sec = max_sec
self.start = time.time()
self.iter = iter
def __call__(self, xk=None):
global i
i+=1
elapsed = time.time()-self.start
rate = i/self.iter
percentage = rate*100
l_bar = '{0:3.0f}%|'.format(percentage)
bar_length,frac_bar = divmod(int(percentage), 10)
bar = chr(0x2588) * bar_length
frac_bar = chr(0x2590-frac_bar)
remaining_time = elapsed/rate - elapsed
speed = elapsed/i
full_bar = bar + frac_bar +' ' * max(10 - bar_length, 0)
r_bar = '| {0}/{1} [{2}<{3}, {4}s/iter]'.format(
i, self.iter, np.round(elapsed,4),np.round(remaining_time,4),np.round(speed,4))
bar = l_bar+full_bar+r_bar
sys.stdout.write(bar)
sys.stdout.write('\n')
# def filter_probabilities(parameter):
# llf, y_hat, resid,joint_likelihoods, filtered_probabilities, p_predicted_joint = filter(parameter,K,M,covariance_type,variance_regimes,n_x,n_z)
# ptrans_res = convert_param(param,K,M,n_x,n_z,covariance_type,variance_regimes)
# param, K, M, n_x, n_z, covariance_type, variance_regimes
# PR_SMO = MSVAR_smooth(filtered_probabilities, p_predicted_joint,ptrans_res)
#