def _coeffs(self, k, n, sum_n): alpha = array(self.alpha[k][1:]) sum_alpha = self.alpha[k][0] q = self.mixture[k] # Calculate the coefficient using logarithms log_coeff = log_beta(n+alpha) - log_beta(alpha) return q * exp(log_coeff)
def _pos_probs(self, counts): n = array(counts) # *** NEW CODE FROM HERE **** sum_n = sum(n) p = array([0.0]*len(counts)) log_coeffs = array([0.0]*self.num_distr) q = array(self.mixture) for j in xrange(self.num_distr): alpha_j = array(self.alpha[j][1:]) log_coeffs[j] = log_beta(n+alpha_j) - log_beta(alpha_j) log_coeffs = log_coeffs - max(log_coeffs) coeffs = q * exp(log_coeffs) for i in xrange(len(counts)): a = array([self.alpha[j][i+1] for j in xrange(self.num_distr)]) sum_a_j = array([sum(self.alpha[j][1:]) for j in xrange(self.num_distr)]) N = (a+n[i])/(sum_a_j + sum(n)) p[i] = sum(coeffs*N) return p / sum(p)