Exemplo n.º 1
0
    def likelihood_under_the_prior(self, x):
        """ Computes the likelihood of x under the prior

        Parameters
        ----------
        x, array of shape (self.n_samples,self.dim)

        returns
        -------
        w, the likelihood of x under the prior model (unweighted)
        """
        if self.prior_dens is not None:
            return self.prior_dens * np.ones(x.shape[0])

        a = self._prior_dof
        tau = self._prior_shrinkage
        tau /= (1 + tau)
        m = self._prior_means
        b = self._prior_scale
        ib = np.linalg.inv(b[0])
        ldb = np.log(detsh(b[0]))

        scalar_w = np.log(tau / np.pi) * self.dim
        scalar_w += 2 * gammaln((a + 1) / 2)
        scalar_w -= 2 * gammaln((a - self.dim) / 2)
        scalar_w -= ldb * a
        w = scalar_w * np.ones(x.shape[0])

        for i in range(x.shape[0]):
            w[i] -= (a + 1) * np.log(detsh(ib + tau * (m - x[i:i + 1]) *
                                           (m - x[i:i + 1]).T))

        w /= 2
        return np.exp(w)
Exemplo n.º 2
0
Arquivo: imm.py Projeto: sashka/nipy
    def likelihood_under_the_prior(self, x):
        """ Computes the likelihood of x under the prior

        Parameters
        ----------
        x, array of shape (self.n_samples,self.dim)

        returns
        -------
        w, the likelihood of x under the prior model (unweighted)
        """
        if self.prior_dens is not None:
            return self.prior_dens * np.ones(x.shape[0])

        a = self._prior_dof
        tau = self._prior_shrinkage
        tau /= (1 + tau)
        m = self._prior_means
        b = self._prior_scale
        ib = np.linalg.inv(b[0])
        ldb = np.log(detsh(b[0]))

        scalar_w = np.log(tau / np.pi) * self.dim
        scalar_w += 2 * gammaln((a + 1) / 2)
        scalar_w -= 2 * gammaln((a - self.dim) / 2)
        scalar_w -= ldb * a
        w = scalar_w * np.ones(x.shape[0])

        for i in range(x.shape[0]):
            w[i] -= (a + 1) * np.log(
                detsh(ib + tau * (m - x[i:i + 1]) * (m - x[i:i + 1]).T))

        w /= 2
        return np.exp(w)
Exemplo n.º 3
0
    def set_priors(self, x):
        """ Set the priors in order of having them weakly uninformative
        this is from  Fraley and raftery;
        Journal of Classification 24:155-181 (2007)

        Parameters
        ----------
        x, array of shape (n_samples,self.dim)
           the data used in the estimation process
        """
        # a few parameters
        small = 0.01
        elshape = (1, self.dim, self.dim)
        mx = np.reshape(x.mean(0), (1, self.dim))
        dx = x - mx
        vx = np.maximum(1.e-15, np.dot(dx.T, dx) / x.shape[0])
        px = np.reshape(np.diag(1.0 / np.diag(vx)), elshape)

        # set the priors
        self._prior_means = mx
        self.prior_means = mx
        self.prior_weights = self.alpha
        self._prior_scale = px
        self.prior_scale = px
        self._prior_dof = self.dim + 2
        self.prior_dof = [self._prior_dof]
        self._prior_shrinkage = small
        self.prior_shrinkage = [self._prior_shrinkage]

        # cache some pre-computations
        self._dets_ = detsh(px[0])
        self._dets = [self._dets_]
        self._inv_prior_scale_ = np.reshape(np.linalg.inv(px[0]), elshape)
        self.prior_dens = None
Exemplo n.º 4
0
Arquivo: imm.py Projeto: sashka/nipy
    def set_priors(self, x):
        """ Set the priors in order of having them weakly uninformative
        this is from  Fraley and raftery;
        Journal of Classification 24:155-181 (2007)

        Parameters
        ----------
        x, array of shape (n_samples,self.dim)
           the data used in the estimation process
        """
        # a few parameters
        small = 0.01
        elshape = (1, self.dim, self.dim)
        mx = np.reshape(x.mean(0), (1, self.dim))
        dx = x - mx
        vx = np.maximum(1.e-15, np.dot(dx.T, dx) / x.shape[0])
        px = np.reshape(np.diag(1.0 / np.diag(vx)), elshape)

        # set the priors
        self._prior_means = mx
        self.prior_means = mx
        self.prior_weights = self.alpha
        self._prior_scale = px
        self.prior_scale = px
        self._prior_dof = self.dim + 2
        self.prior_dof = [self._prior_dof]
        self._prior_shrinkage = small
        self.prior_shrinkage = [self._prior_shrinkage]

        # cache some pre-computations
        self._dets_ = detsh(px[0])
        self._dets = [self._dets_]
        self._inv_prior_scale_ = np.reshape(np.linalg.inv(px[0]), elshape)
        self.prior_dens = None