def _update_trans(self, elngamma, elnxi, n_obs): for i in range(self.n_states): for j in range(self.n_states): numerator = -np.inf denominator = -np.inf for k in range(n_obs - 1): numerator = elnsum(numerator, elnxi[i, j, k]) denominator = elnsum(denominator, elngamma[i, k]) self.trans[i, j] = eexp(elnproduct(numerator, -denominator))
def _update_emis(self, elngamma, obs_seq): for e in range(self.emis.shape[0]): for i in range(self.n_states): numerator = -np.inf denominator = -np.inf for k in range(len(obs_seq)): if obs_seq[k] - 1 == e: numerator = elnsum(numerator, elngamma[i, k]) denominator = elnsum(denominator, elngamma[i, k]) self.emis[e, i] = eexp(elnproduct(numerator, -denominator))
def _update_init_eln(self, elngammas): """ This function calculate the update value of the initial probabilities as defined by the Berkeley notes references in the _m_step algorithm. It is the average of the sum of probabilities of x_1 over all sequences. i.e. the expected frequency of state Si at time t = 1 """ for i in range(self.n_states): numerator = -np.inf denominator = 0 for idx in range(len(self.sequences)): numerator = elnsum(numerator, elngammas[idx][i, 0]) denominator = denominator + 1 self.init[i] = eexp(numerator) / denominator
def _generate_gamma(self, elnalpha, elnbeta): elngamma = np.zeros((self.n_states, self.n_obs + 1)) gamma = np.zeros((self.n_states, self.n_obs + 1)) for k in range(0, self.n_obs + 1): norm = -np.inf for i in range(self.n_states): elngamma[i, k] = elnproduct(elnalpha[i, k], elnbeta[i, k]) norm = elnsum(norm, elngamma[i, k]) for i in range(self.n_states): elngamma[i, k] = elnproduct(elngamma[i, k], -norm) gamma[i, k] = eexp(elngamma[i, k]) return elngamma, gamma
def _update_emis_eln(self, elngammas): """ This function calculates the update of the transition probabilities as defined by the Berkeley notes references in the _m_step algorithm. The numerator is the eln_xi values summed across all sequences and across all time steps. The denominator is the eln_gamma values summed across all sequences and across all time steps. """ for e in range(self.emis.shape[0]): for i in range(self.n_states): numerator = -np.inf denominator = -np.inf for idx, obs_seq in enumerate(self.sequences): for k in range(0, len(obs_seq)): if obs_seq[k] == e: numerator = elnsum(numerator, elngammas[idx][i, k]) denominator = elnsum(denominator, elngammas[idx][i, k]) self.emis[e, i] = eexp(elnproduct(numerator, -denominator))
def _update_trans_eln(self, gammas, xis): """ This function calculates the update of the transition probabilities as defined by the Berkeley notes references in the _m_step algorithm. The numerator is the eln_gamma values summed across all sequences and across all time steps. The denominator is the eln_gamma values summed across all sequences and across all time steps. """ for i in range(self.n_states): for j in range(self.n_states): numerator = -np.inf denominator = -np.inf for idx, obs_seq in enumerate(self.sequences): for k in range(1, len(obs_seq)): numerator = elnsum(numerator, elnxis[idx][i, j, k - 1]) denominator = elnsum(denominator, elngammas[idx][i, k - 1]) self.trans[i, j] = eexp(elnproduct(numerator, -denominator))
def _eln_xi(self, elnalpha, elnbeta, obs_seq): """ This function calculates P(S_i_t, S_j_t+1) i.e. the probability of being in state S_i at time t and state S_j at time t+1. """ elnxi = np.zeros((self.n_states, self.n_states, len(obs_seq) - 1)) xi = np.zeros((self.n_states, self.n_states, len(obs_seq) - 1)) for k in range(len(obs_seq) - 1): normalizer = -np.inf for i in range(self.n_states): for j in range(self.n_states): elnxi[i, j, k] = elnproduct( elnalpha[i, k], elnproduct( eln(self.trans.transpose()[i, j]), elnproduct(eln(self.emis[obs_seq[k + 1], j]), elnbeta[j, k + 1]))) normalizer = elnsum(normalizer, elnxi[i, j, k]) for i in range(self.n_states): for j in range(self.n_states): elnxi[i, j, k] = elnproduct(elnxi[i, j, k], -normalizer) xi[i, j, k] = eexp(elnxi[i, j, k]) return elnxi, xi
def _update_init(self, elngamma): for i in range(self.n_states): self.init[i] = eexp(elngamma[i, 1])