Skip to content

Instantly share code, notes, and snippets.

@sachinsdate
Created November 26, 2021 11:24
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/eb47ed5203b21467089d8285bef3575c to your computer and use it in GitHub Desktop.
Save sachinsdate/eb47ed5203b21467089d8285bef3575c to your computer and use it in GitHub Desktop.
def compute_loglikelihood(self):
#Init the list of loglikelihhod values, one value for each y observation
ll = []
for t in range(len(self.y)):
prob_y_t = 0
mu_t = 0
for j in range(self.k_regimes):
#To use the law of total probability, uncomment this row and comment out the next
# two rows
#prob_y_t += poisson.pmf(y[t], mu[t][j]) * self.delta_matrix[t][j]
#Calculate the Poisson mean mu_t as an expectation over all Markov state
# probabilities
mu_t += self.mu_matrix[t][j] * self.delta_matrix[t][j]
prob_y_t += poisson.pmf(self.y[t], mu_t)
#This is a bit of a kludge. If the likelihood turns out to be real tiny, fix it to
# the EPS value for the machine
if prob_y_t < self.EPS:
prob_y_t = self.EPS
#Push the LL into the list of LLs
ll.append(math.log(prob_y_t))
ll = np.array(ll)
return ll
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment