def construct_proposal(self, y):
     assert(len(shape(y)) == 1)
     m = MixtureDistribution(self.distribution.dimension, self.num_eigen)
     m.mixing_proportion = Discrete((self.eigvalues + 1) / (sum(self.eigvalues) + self.num_eigen))
     # print "current mixing proportion: ", m.mixing_proportion.omega
     for ii in range(self.num_eigen):
         L = sqrt(self.dwscale[ii] * self.eigvalues[ii]) * reshape(self.eigvectors[:, ii], (self.distribution.dimension, 1))
         m.components[ii] = Gaussian(y, L, is_cholesky=True, ell=1)
     # Z=m.sample(1000).samples
     # Visualise.plot_data(Z)
     return m
Example #2
0
    def fit_gmm(self, samples):
        """
        Runs a couple of em instances on random starting points and returns
        internal GMM representation of best instance
        """
        features = RealFeatures(samples.T)

        gmms = []
        log_likelihoods = zeros(self.num_runs_em)
        for i in range(self.num_runs_em):
            # set up Shogun's GMM class and run em (corresponds to random
            # initialisation)
            gmm = GMM(self.num_components)
            gmm.set_features(features)
            log_likelihoods[i] = gmm.train_em()
            gmms.append(gmm)

        max_idx = log_likelihoods.argmax()

        # construct Gaussian mixture components in internal representation
        components = []
        for i in range(self.num_components):
            mu = gmms[max_idx].get_nth_mean(i)
            Sigma = gmms[max_idx].get_nth_cov(i)
            components.append(Gaussian(mu, Sigma))

        # construct a Gaussian mixture model based on the best EM run
        pie = gmms[max_idx].get_coef()
        proposal = MixtureDistribution(components[0].dimension,
                                       self.num_components, components,
                                       Discrete(pie))

        return proposal
Example #3
0
    def construct_proposal(self, y):
        """
        proposal is a mixture of normals,
        centred at y and with covariance gamma^2 I + nu^2 MHaa'HM',
        where a are the eigenvectors of centred kernel matrix Kc=HKH
        """
        assert len(shape(y)) == 1
        m = MixtureDistribution(self.distribution.dimension, self.num_eigen)
        m.mixing_proportion = Discrete((self.eigvalues + 1) / (sum(self.eigvalues) + self.num_eigen))
        # print "current mixing proportion: ", m.mixing_proportion.omega
        M = 2 * self.kernel.gradient(y, self.Z)
        H = Kernel.centring_matrix(len(self.Z))

        for ii in range(self.num_eigen):
            Sigma = self.gamma ** 2 * eye(len(y)) + self.nu2 * (M.T).dot(
                H.dot(outer(self.eigvectors[:, ii], self.eigvectors[:, ii]).dot(H.dot(M)))
            )
            m.components[ii] = Gaussian(y, Sigma)
        return m
Example #4
0
    def construct_proposal(self, y):
        """
        proposal is a mixture of normals,
        centred at y and with covariance gamma^2 I + nu^2 MHaa'HM',
        where a are the eigenvectors of centred kernel matrix Kc=HKH
        """
        assert (len(shape(y)) == 1)
        m = MixtureDistribution(self.distribution.dimension, self.num_eigen)
        m.mixing_proportion = Discrete(
            (self.eigvalues + 1) / (sum(self.eigvalues) + self.num_eigen))
        # print "current mixing proportion: ", m.mixing_proportion.omega
        M = 2 * self.kernel.gradient(y, self.Z)
        H = Kernel.centring_matrix(len(self.Z))

        for ii in range(self.num_eigen):
            Sigma = self.gamma ** 2 * eye(len(y)) + \
            self.nu2 * (M.T).dot(H.dot(outer(self.eigvectors[:, ii], self.eigvectors[:, ii]).dot(H.dot(M))))
            m.components[ii] = Gaussian(y, Sigma)
        return m