def VB_estimate(self,x,niter = 100,delta = 0.0001): """ Estimation of the BGMM using a Variational Bayes approach Parameters ---------- x array of shape (nbitems,dim) the input data niter = 100, the maximal number of iterations of the VB algo delta = 0.0001, the increment in log-likelihood to declare convergence Returns ------- label: array of shape nbitems: resulting MAP labelling """ x = self.check_data(x) # pre_cluster the data (this improves convergence...) label = np.zeros(x.shape[0]) nit = 10 mean,label,J = fc.kmeans(x,self.k,label,nit) label, mean, meansc, prec, we, dof, Li = fc.bayesian_gmm (x,self.prior_means,self.prior_precisions,self.prior_shrinkage,self.prior_weights, self.prior_dof,label,niter,delta) self.estimated = 1 self.means = mean self.shrinkage = meansc self.precisions = prec self.weights = we self.dof = dof return label
def VB_estimate_and_sample(self,x,niter = 1000,delta = 0.0001,gd = None,verbose = 0): """ Estimation of the BGMM using a Variational Bayes approach, and sampling of the model on test points in order to have an estimate of the posterior on these points Parameters ---------- x array of shape (nbitems,dim) the input data niter = 100, the maximal number of iterations of the VB algo delta = 0.0001, the increment in log-likelihood to declare convergence gd = None a grid descriptor, i.e. the grid on chich the model is sampled if gd==None, x is used as Grid verbose = 0: the verbosity mode Returns ------- Li : array of shape (nbnodes): the average log-posterior label: array of shape nbitems: resulting MAP labelling """ x = self.check_data(x) # pre_cluster the data (this improves convergence...) label = np.zeros(x.shape[0]) nit = 10 mean,label,J = fc.kmeans(x,self.k,label,nit) if gd==None: grid = x else: grid = gd.make_grid() label, mean, meansc, prec, we,dof,Li = fc.bayesian_gmm (x, self.prior_means, self.prior_precisions, self.prior_shrinkage, self.prior_weights, self.prior_dof, label, niter, delta, grid) self.estimated = 1 self.means = mean self.shrinkage = meansc self.precisions = prec self.weights = we self.dof = dof if verbose: self.show(x,gd,np.exp(Li)) return Li,label