def E_step(self): """ Find the likelihoods for each datum for each component and each translation The likelihood is also computed """ for cur_component, vals in enumerate(zip(self.means, self.covs, self.norm_constants)): mean, cov, norm_constant = vals self.affinities[cur_component] = np.sum(- .5 *(self.trans_data_mat - mean)**2 * cov,2) + norm_constant self.max_affinities[:] = np.max( np.max(self.affinities, 0), 1) subtract_max_affinities(self.affinities,self.max_affinities, self.num_mix, self.num_data, self.trans_amount) self.affinities = np.exp(self.affinities) self.log_likelihood = np.sum(np.log(np.sum( np.sum( self.affinities * self.trans_weights, 2).T * self.mix_weights,1)) + self.max_affinities) affinity_sums = np.sum(self.affinities.sum(0),1) normalize_affinities(self.affinities,affinity_sums, self.num_mix, self.num_data, self.trans_amount) self.mix_weights = np.sum(np.sum(self.affinities,1),1) self.mix_weights /= np.sum(self.mix_weights)
affinities[cur_component] = np.sum((trans_data_mat - mean)**2 * cov,2) + norm_constant # # # from _translation_invariant_Gauss_EM import subtract_max_affinities, normalize_affinities affinities *=2 normalize_affinities(affinities,np.sum(affinities.sum(0),1), num_mix, num_data, trans_amount) subtract_max_affinities(affinities,np.sum(affinities.sum(0),1), num_mix, num_data, trans_amount) import translation_invariant_Gauss_EM reload(translation_invariant_Gauss_EM) TranslationInvariantGaussEM = translation_invariant_Gauss_EM.TranslationInvariantGaussEM tig = TranslationInvariantGaussEM(1,data_mat,12) tig.run_EM(.0001) datamat = np.zeros((100,10)) component_length = 9 for i in xrange(100): if i % 2 == 0: datamat[i,:component_length] = 100 * np.ones(component_length)