Beispiel #1
0
 def E_log_p_mu_lambda_samples(self, mus, lambdas):
     prior_lambda = tfd.WishartTriL(df=self.dof0, #, 
                                    scale_tril=tf.linalg.cholesky(self.W0))
     log_prior_lambdas = tf.reduce_sum( prior_lambda.log_prob(lambdas), -1 )
     
     def get_prior_mu(beta0, m0, lambdas):
         precisions = (lambdas*beta0)
         covs = tf.linalg.inv(precisions)
         covs = 0.5*(covs + tf.transpose(covs, [0, 1, 3, 2])) # numerical stability workaround
         d = tfd.MultivariateNormalTriL(loc=m0, 
                                        scale_tril=tf.linalg.cholesky(covs))
         return d
     prior_mu = get_prior_mu(self.beta0, #, 
                             self.m0, 
                             lambdas)
     log_prior_mus = tf.reduce_sum( prior_mu.log_prob(mus), -1)  
     return tf.reduce_mean(log_prior_lambdas+log_prior_mus)
Beispiel #2
0
    def sample_posteriors_global(self, nsamples=1000):
        posterior_pi = tfd.Dirichlet(self.alpha)
        pis = posterior_pi.sample(nsamples)

        W1 = np.tile(self.W, [nsamples,1,1,1]) #nsamples x K x N=2 x N=2
        posterior_lambda = tfd.WishartTriL(df=self.dof, 
                                           scale_tril=tf.linalg.cholesky(W1))
        lambdas = posterior_lambda.sample() #nsamples x K x N=2 x N=2

        def get_posterior_mu(beta, mu, lambdas):
            locations = np.broadcast_to(mu, lambdas.shape[0:1]+mu.shape)
            precisions = (lambdas*beta[None,:,None,None])
            covs = tf.linalg.inv(precisions) #!
            covs = 0.5*(covs + tf.transpose(covs, [0, 1, 3, 2])) # numerical stability workaround
            d = tfd.MultivariateNormalTriL(loc=locations, 
                                           scale_tril=tf.linalg.cholesky(covs))
            return d
        posterior_mu = get_posterior_mu(self.beta, 
                                          self.mu, 
                                          lambdas)
        mus = posterior_mu.sample()

        return posterior_pi, posterior_lambda, posterior_mu, pis, lambdas, mus
Beispiel #3
0
 def _init_distribution(conditions, **kwargs):
     df, scale = conditions["df"], conditions["scale"]
     return tfd.WishartTriL(df=df, scale_tril=scale, **kwargs)