Пример #1
0
    def test__compute_log_prob_spherical_shared_compression(self):
        """ Compare the log_prob computation to that of sklearn with
        shared compression. Spherical covariances. """
        cov_type = 'spherical'
        rs = np.random.RandomState(10)
        gmm = GaussianMixture(n_components=3,
                              num_feat_full=5,
                              num_feat_comp=3,
                              num_feat_shared=3,
                              num_samp=4,
                              transform=None,
                              mask=None,
                              D_indices=None,
                              covariance_type=cov_type,
                              random_state=rs)
        gmm.fit_sparsifier(X=self.td.X)
        means = rs.rand(gmm.n_components, gmm.num_feat_full)
        covariances = rs.rand(gmm.n_components)
        log_prob_test = gmm._compute_log_prob(means, covariances, cov_type)

        log_prob_true = np.zeros((gmm.num_samp, gmm.n_components))
        for data_ind in range(gmm.num_samp):
            for comp_ind in range(gmm.n_components):
                true_const = gmm.num_feat_comp * np.log(2 * np.pi)
                true_logdet = gmm.num_feat_comp * np.log(covariances[comp_ind])
                true_mahadist = 1/covariances[comp_ind] * \
                    np.linalg.norm(gmm.RHDX[data_ind] -
                        means[comp_ind][gmm.mask[data_ind]])**2
                log_prob_true[data_ind, comp_ind] = -.5*(true_const + \
                    true_logdet + true_mahadist)
        self.assertArrayEqual(log_prob_test, log_prob_true)
Пример #2
0
 def test__compute_log_prob_diagonal_no_compression(self):
     """ Compare the log_prob computation to that of sklearn with no
     compression. Implemented as a precursor to testing it with
     compression, to follow. Diagonal covariances. """
     cov_type = 'diag'
     gmm = GaussianMixture(n_components=3,
                           num_feat_full=5,
                           num_feat_comp=5,
                           num_feat_shared=5,
                           num_samp=4,
                           transform=None,
                           mask=None,
                           D_indices=None,
                           covariance_type=cov_type)
     gmm.fit_sparsifier(X=self.td.X)
     means = np.random.rand(gmm.n_components, gmm.num_feat_comp)
     covariances = np.random.rand(gmm.n_components, gmm.num_feat_comp)
     log_prob_test = gmm._compute_log_prob(means, covariances, cov_type)
     precisions = _compute_precision_cholesky(covariances, cov_type)
     log_prob_true = _estimate_log_gaussian_prob(self.td.X, means,
                                                 precisions, cov_type)
     self.assertArrayEqual(log_prob_test, log_prob_true)