Esempio n. 1
0
    def Gibbs_estimate(self,x,niter = 1000,method = 1):
        """
        Estimation of the BGMM using Gibbs sampling
        
        Parameters
        ----------
        x array of shape (nbitems,dim) the input data
        niter = 1000, the maximal number of iterations of the Gibbs sampling
        method = 1: boolean to state whether covariance
               are fixed (0 ; normal model) or variable 
               (1 ; normal-wishart model)

        Returns
        -------
        label: array of shape nbitems, resulting MAP labelling
        """
        x = self.check_data(x)
        label, mean, meansc, prec, we,dof,Li = fc.gibbs_gmm (x,\
               self.prior_means, self.prior_precisions, self.prior_shrinkage,
               self.prior_weights, self.prior_dof, niter, method)
        self.estimated = 1
        self.means = mean
        self.shrinkage = meansc
        self.precisions = prec
        self.weights = we
        self.dof = dof
        label = np.argmax(label,1)
        return label
Esempio n. 2
0
 def test_Gibbs_GMM(self, verbose=0):
     k = 2
     dim = 2
     # prior_means = np.zeros((k,dim),'d')
     prior_means = np.concatenate([np.zeros((1,dim)),np.ones((1,dim))])
     prior_precision_scale =  1*np.ones((k,dim),'d') # 0.01
     prior_mean_scale = 1*np.ones(k,'d')
     prior_weights = np.ones(k,'d')
     prior_dof =  (dim+1)*np.ones(k,'d') # 100
     X = nr.randn(100,dim)-1
     X[-30:] = X[-30:]+ 4
     membership, mean, mean_scale,precision_scale, weights,dof,density = fc.gibbs_gmm(X, prior_means,prior_precision_scale,prior_mean_scale,prior_weights,prior_dof,1000)
     expectC = np.array([[-1,-1],[3,3]])
     if verbose:
         print expectC,mean
     self.assert_( np.allclose(expectC, mean,0.3,0.3))
Esempio n. 3
0
    def Gibbs_estimate_and_sample(self, x, niter = 1000, method = 1, 
                                        gd = None, nsamp = 1000, verbose=0):
        """
        Estimation of the BGMM using Gibbs sampling
        and sampling of the posterior on test points
        
        Parameters
        ----------
        x array of shape (nbitems,dim) the input data
        niter = 1000, the maximal number of iterations of the Gibbs sampling
        method = 1: boolean to state whether covariance
               are fixed (0 ; normal model) 
               or variable (1 ; normal-wishart model)
        gd = None,  a grid descriptor, i.e. 
           the grid on chich the model is sampled
           if gd==None, x is used as Grid
        nsamp = 1000 number of draws of the posterior
        verbose = 0: the verboseity level
        
        Returns
        -------
        Li : array of shape (nbnodes): the average log-posterior
        label: array of shape (nbitems): resulting MAP labelling
        """
        x = self.check_data(x)
        if gd==None:
            grid = x
        else:
            grid = gd.make_grid()
            
        label, mean, meansc, prec, we,dof,Li = fc.gibbs_gmm (x, \
               self.prior_means, self.prior_precisions, self.prior_shrinkage,
               self.prior_weights, self.prior_dof, niter, method, grid, nsamp)
        self.estimated = 1
        self.means = mean
        self.shrinkage = meansc
        self.precisions = prec
        self.weights = we
        self.dof = dof
        if verbose:
            self.show(x,gd,Li)

        label = np.argmax(label,1)
        return np.log(Li),label