Ejemplo n.º 1
0
 def predict_cluster(self, X, with_covariance=False):
     """Backward prediction
     If with_covariance is True, the importance of one cluster is computed with the height of gaussian as well."""
     N = X.shape[0]
     prob = np.empty((self.K, N))
     if with_covariance:
         chols = np.linalg.cholesky(self.full_SigmakList)
         dets = np.sum(np.log(np.array([np.diag(c) for c in chols])),
                       axis=1)
     for k, ck, Gammak, pik in zip(range(self.K), self.ckList,
                                   self.GammakList, self.pikList):
         r = chol_loggausspdf(X.T, ck[:, None], Gammak) + np.log(pik)
         if with_covariance:  # poids = pik / sqrt( det(Sigma))
             r = r - dets[k]
         prob[k] = r
     choice = np.argmax(prob, axis=0)
     prob = np.exp(prob)
     prob = prob / prob.sum(axis=0)
     return choice, prob.T
Ejemplo n.º 2
0
    def _helper_backward_conditionnal_density(self, X):
        """
        Compute the mean Ak*Y + Bk and the quantities alpha depending of X in (6)
        :param X: shape (L,D)
        :return: mean shape(D,N,K) alpha shape (N,K)
        """
        N = X.shape[0]
        X = X.reshape((N, self.L))

        proj = np.empty((self.D, N, self.K))  # AkS * X + BkS
        logalpha = np.zeros((N, self.K))  # log N(ckS,GammakS)(X)

        for (k, pik, Ak, bk, ck, Gammak) in zip(range(self.K), self.pikList,
                                                self.AkList, self.bkList,
                                                self.ckList, self.GammakList):
            proj[:, :, k] = Ak.dot(X.T) + np.expand_dims(bk, axis=1)
            logalpha[:, k] = np.log(pik) + chol_loggausspdf(
                X.T, ck.reshape((self.L, 1)), Gammak)

        log_density = logsumexp(logalpha, axis=1, keepdims=True)
        logalpha -= log_density
        alpha = np.exp(logalpha)
        return proj, alpha
Ejemplo n.º 3
0
    def forward_density(self, Y, X_points, marginals=None, sub_densities=0):
        """Return conditionnal density of X knowing Y, evaluated at X_points.
        Return shape (N ,len(X_points) ).
        If marginals is given, compute marginal density. In this case, X_points needs to have the marginal dimension.
        Is sub_densities is a non negative integer, returns the density of sub_densitites dominant components."""

        if (not marginals) and not X_points.shape[1] == self.L:
            raise WrongContextError(
                "Dimension of X samples doesn't match the choosen Lw")
        proj, alpha, _ = self._helper_forward_conditionnal_density(Y)

        NX, D = X_points.shape
        N = Y.shape[0]
        if marginals:
            proj = proj[:, :, marginals]  # len(marginals) , N , K
            covs = self.SigmakListS[:,
                                    marginals, :][:, :,
                                                  marginals]  # K, len(marginals), len(marginals)
        else:
            covs = self.SigmakListS

        densites = np.empty((N, NX))
        sub_dens = np.empty((sub_densities, N, NX))
        t = time.time()
        for n, meann, alphan in zip(range(N), proj, alpha):
            densites[n] = densite_melange(X_points, alphan, meann, covs)
            if sub_densities:
                dominants = dominant_components(alphan, meann,
                                                covs)[0:sub_densities]
                for i, (_, w, m, c) in enumerate(dominants):
                    sub_dens[i, n] = np.exp(
                        chol_loggausspdf(X_points.T, m.reshape((D, 1)), c)) * w
        if self.verbose:
            logging.debug("Density calcul time {:.3f}".format(time.time() - t))

        return densites, sub_dens
Ejemplo n.º 4
0
    def _helper_forward_conditionnal_density(self, Y):
        """
        Compute the mean Ak*Y + Bk and the quantities alpha depending of Y in (7)
        :param Y: shape (N,D)
        :return: mean shape(N,K,L) alpha shape (N,K) , normalisation shape (N,1)
        """
        N = Y.shape[0]
        Y = Y.reshape((N, self.D))
        YT = np.array(Y.T, dtype=float)

        proj = np.empty((self.L, N, self.K))  # AkS * Y + BkS
        logalpha = np.zeros((N, self.K))  # log N(ckS,GammakS)(Y)

        for (k, pik, Ak, bk, ck,
             Gammak) in zip(range(self.K), self.pikList, self.AkListS,
                            self.bkListS, self.ckListS, self.GammakListS):
            proj[:, :, k] = Ak.dot(YT) + np.expand_dims(bk, axis=1)
            logalpha[:, k] = np.log(pik) + chol_loggausspdf(
                YT, ck.reshape((self.D, 1)), Gammak)

        log_density = logsumexp(logalpha, axis=1, keepdims=True)
        logalpha -= log_density
        alpha, normalisation = np.exp(logalpha), np.exp(log_density)
        return proj.transpose((1, 2, 0)), alpha, normalisation
Ejemplo n.º 5
0
def test_Hapke():
    mu = np.ones(11)
    T = np.tril(np.ones((11, 1 & 1))) * 0.456
    cov = np.dot(T, T.T)
    d = chol_loggausspdf(a.T, mu[:, None], cov)