def compute_moments_and_cgf(self, phi, mask=True): r""" Compute the moments and :math:`g(\phi)`. .. math:: \overline{\mathbf{u}} = \mathrm{E}[x] = N \cdot \begin{bmatrix} \frac{e^{\phi_1}}{\sum_i e^{\phi_i}} & \cdots & \frac{e^{\phi_D}}{\sum_i e^{\phi_i}} \end{bmatrix} """ # Compute the normalized probabilities in a numerically stable way (p, logsum_p) = misc.normalized_exp(phi[0]) N = np.expand_dims(self.N, -1) u0 = N * p u = [u0] g = -np.squeeze(N * logsum_p, axis=-1) return (u, g)
def random(self, *phi, plates=None): r""" Draw a random sample from the distribution. """ (p, _) = misc.normalized_exp(phi[0]) return random.multinomial(self.N, p, size=plates)