def show(self): """ Print the distribution using standard parameterization. """ logsum_p = utils.logsumexp(self.phi[0], axis=-1, keepdims=True) p = np.exp(self.phi[0] - logsum_p) p /= np.sum(p, axis=-1, keepdims=True) print("%s ~ Multinomial(p)" % self.name) print(" p = ") print(p) return
def compute_moments_and_cgf(self, phi, mask=True): """ Compute the moments and :math:`g(\phi)`. """ # Compute the normalized probabilities in a numerically stable way logsum_p = utils.logsumexp(phi[0], axis=-1, keepdims=True) logp = phi[0] - logsum_p p = np.exp(logp) # Because of small numerical inaccuracy, normalize the probabilities # again for more accurate results N = np.expand_dims(self.N, -1) u0 = N * p / np.sum(p, axis=-1, keepdims=True) u = [u0] g = -N * np.squeeze(logsum_p, axis=-1) return (u, g)