def __init__(self, N): if not utils.isinteger(N): raise ValueError("Number of trials must be integer") if np.any(N < 0): raise ValueError("Number of trials must be non-negative") self.N = np.asanyarray(N) super().__init__()
def compute_fixed_moments(self, x): """ Compute the moments for a fixed value """ # Make sure the values are integers in valid range x = np.asanyarray(x) if not utils.isinteger(x): raise ValueError("Count not integer") # Now, the moments are just the counts return [x]
def __init__(self, trials): """ Create VMP formula node for a multinomial variable `trials` is the total number of trials. """ if not utils.isinteger(trials): raise ValueError("Number of trials must be integer") if np.any(trials < 0): raise ValueError("Number of trials must be non-negative") self.N = trials super().__init__()
def compute_fixed_moments_and_f(self, x, mask=True): """ Compute the moments and :math:`f(x)` for a fixed value. """ # Make sure the values are integers in valid range x = np.asanyarray(x) if not utils.isinteger(x): raise ValueError("Counts must be integer") if np.any(x < 0) or np.any(x > self.N): raise ValueError("Invalid count") # Now, the moments are just the counts u = [x] f = (special.gammaln(self.N+1) - special.gammaln(x+1) - special.gammaln(self.N-x+1)) return (u, f)
def compute_fixed_moments(self, x): """ Compute the moments for a fixed value """ # Check that x is valid x = np.asanyarray(x) if not utils.isinteger(x): raise ValueError("Values must be integers") if np.any(x < 0) or np.any(x >= self.D): raise ValueError("Invalid category index") u0 = np.zeros((np.size(x), self.D)) u0[[np.arange(np.size(x)), np.ravel(x)]] = 1 u0 = np.reshape(u0, np.shape(x) + (self.D,)) return [u0]
def compute_fixed_moments(self, x): """ Compute the moments for a fixed value `x` must be a vector of counts. """ # Check that counts are valid x = np.asanyarray(x) if not utils.isinteger(x): raise ValueError("Counts must be integer") if np.any(x < 0): raise ValueError("Counts must be non-negative") # Moments is just the counts vector u0 = x.copy() return [u0]
def compute_fixed_moments_and_f(self, x, mask=True): """ Compute the moments and :math:`f(x)` for a fixed value. """ # Check the validity of x x = np.asanyarray(x) if not utils.isinteger(x): raise ValueError("Values must be integers") if np.any(x < 0): raise ValueError("Values must be positive") # Compute moments u0 = np.copy(x) u = [u0] # Compute f(x) f = -special.gammaln(x+1) return (u, f)
def compute_fixed_moments_and_f(self, x, mask=True): """ Compute the moments and :math:`f(x)` for a fixed value. """ # Check that counts are valid x = np.asanyarray(x) if not utils.isinteger(x): raise ValueError("Counts must be integers") if np.any(x < 0): raise ValueError("Counts must be non-negative") if np.any(np.sum(x, axis=-1) != self.N): raise ValueError("Counts must sum to the number of trials") # Moments is just the counts vector u0 = x.copy() u = [u0] f = special.gammaln(self.N+1) - np.sum(special.gammaln(x+1), axis=-1) return (u, f)
def compute_fixed_moments_and_f(self, x, mask=True): """ Compute the moments and :math:`f(x)` for a fixed value. """ # Check the validity of x x = np.asanyarray(x) if not utils.isinteger(x): raise ValueError("Values must be integers") if np.any(x < 0) or np.any(x >= self.D): raise ValueError("Invalid category index") # Form a binary matrix with only one non-zero (1) in the last axis u0 = np.zeros((np.size(x), self.D)) u0[[np.arange(np.size(x)), np.ravel(x)]] = 1 u0 = np.reshape(u0, np.shape(x) + (self.D,)) u = [u0] # f(x) is zero f = 0 return (u, f)