Skip to content

Instantly share code, notes, and snippets.

@sachinsdate
Created November 26, 2021 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sachinsdate/c40644eece4897e6439abda098684d78 to your computer and use it in GitHub Desktop.
Save sachinsdate/c40644eece4897e6439abda098684d78 to your computer and use it in GitHub Desktop.
def __init__(self, endog, exog, k_regimes=2, loglike=None, score=None, hessian=None,
missing='none', extra_params_names=None, **kwds):
super(PoissonHMM, self).__init__(endog=endog, exog=exog, loglike=loglike, score=score,
hessian=hessian, missing=missing,
extra_params_names=extra_params_names, kwds=kwds)
self.y = np.array(self.endog)
self.k_regimes = k_regimes
#k_regimes x exog.shape[1] size matrix of regime specific regression coefficients
self.beta_matrix = np.ones([self.k_regimes, self.exog.shape[1]])
# k x k matrix of psuedo transition probabilities which can range from -inf to +inf during
# optimization. Initialized to 1.0/k
self.q_matrix = np.ones([self.k_regimes,self.k_regimes])*(1.0/self.k_regimes)
print('self.q_matrix='+str(self.q_matrix))
#The regime wise matrix of Poisson means. These would be updated during the optimization
# loop
self.mu_matrix = []
# k x k matrix of the real Markov transition probabilities which will be calculated from
# the q-matrix using a standardization technique. Initialized to 1.0/k
self.gamma_matrix = np.ones([self.k_regimes, self.k_regimes])*(1.0/self.k_regimes)
print('self.gamma_matrix='+str(self.gamma_matrix))
# The Markov state probabilities. Also referred to as pi. but we'll use delta since pi is
# often used to refer to the mean
self.delta_matrix = np.ones([self.exog.shape[0],self.k_regimes])*(1.0/self.k_regimes)
print('self.delta_matrix='+str(self.delta_matrix))
#The vector of initial values for all the parameters, beta and q, that the optimizer will
# optimize
self.start_params = np.repeat(np.ones(self.exog.shape[1]), repeats=self.k_regimes)
self.start_params = np.append(self.start_params, self.q_matrix.flatten())
print('self.start_params='+str(self.start_params))
#A very tiny number (machine specific). Used by the LL function.
self.EPS = np.MachAr().eps
#Optimization iteration counter
self.iter_num=0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment