Example #1
0
 def from_observation(cls, obs, m, n):
     p = cls()
     if m > 0:
         sum_obs = np.sum(obs[0:m])
         p.log_c = utils.gammaln(sum_obs + 1) - utils.gammaln(sum_obs + m)
         p.alpha = np.asarray(obs[0:m]) + 1
     if n > 0:
         p.a = np.asarray(obs[m:]) + 1
         p.b = np.ones(n)
     return p
Example #2
0
 def from_observation(cls, obs, m, n):
     p = cls()
     if m > 0:
         sum_obs = np.sum(obs[0:m])
         p.log_c = utils.gammaln(sum_obs+1) - utils.gammaln(sum_obs+m)
         p.alpha = np.asarray(obs[0:m]) + 1
     if n > 0:
         p.a = np.asarray(obs[m:]) +1
         p.b = np.ones(n)
     return p
Example #3
0
 def __mul__(self, other):
     p = copy.deepcopy(self)
     p.log_c += other.log_c
     # Multiply Dirichlet component
     if len(self.alpha) > 0:
         p.alpha = self.alpha + other.alpha - 1
         p.log_c += utils.gammaln(np.sum(self.alpha)) - np.sum(utils.gammaln(self.alpha))
         p.log_c += utils.gammaln(np.sum(other.alpha)) - np.sum(utils.gammaln(other.alpha))
         p.log_c += np.sum(utils.gammaln(p.alpha)) - utils.gammaln(np.sum(p.alpha))
     # Multiply Gamma components
     if len(self.a) > 0:
         p.a = self.a + other.a - 1
         p.b = (self.b * other.b) / (self.b + other.b)
         p.log_c += np.sum(utils.gammaln(p.a) + p.a * np.log(p.b))
         p.log_c -= np.sum(utils.gammaln(self.a) + self.a * np.log(self.b))
         p.log_c -= np.sum(utils.gammaln(other.a) + other.a * np.log(other.b))
     return p
Example #4
0
 def __mul__(self, other):
     p = copy.deepcopy(self)
     p.log_c += other.log_c
     # Multiply Dirichlet component
     if len(self.alpha) > 0:
         p.alpha = self.alpha + other.alpha - 1
         p.log_c += utils.gammaln(np.sum(self.alpha)) - np.sum(
             utils.gammaln(self.alpha))
         p.log_c += utils.gammaln(np.sum(other.alpha)) - np.sum(
             utils.gammaln(other.alpha))
         p.log_c += np.sum(utils.gammaln(p.alpha)) - utils.gammaln(
             np.sum(p.alpha))
     # Multiply Gamma components
     if len(self.a) > 0:
         p.a = self.a + other.a - 1
         p.b = (self.b * other.b) / (self.b + other.b)
         p.log_c += np.sum(utils.gammaln(p.a) + p.a * np.log(p.b))
         p.log_c -= np.sum(utils.gammaln(self.a) + self.a * np.log(self.b))
         p.log_c -= np.sum(
             utils.gammaln(other.a) + other.a * np.log(other.b))
     return p