Esempio n. 1
0
 def sample_atom(self, data, cell):
     a = self.base_measure.params.a
                 
     for item in cell.items:
         a += data[item].x
     
     n = cell.size
     b = self.base_measure.params.b
     
     b = b / (n * b + 1)
     
     return GammaData(gamma_rvs(a, b))
Esempio n. 2
0
def draw_from_prior(base_measure, size):
    alpha = gamma_rvs(1, 1)

    partition = sample_from_crp(alpha, size, base_measure)

    #    partition = Partition()
    #
    #    partition.add_cell(base_measure.random())
    #
    #    for item in range(size):
    #        partition.add_item(item, 0)

    return alpha, partition
Esempio n. 3
0
    def sample(self, old_value, num_clusters, num_data_points):
        a = self.a
        b = self.b

        k = num_clusters
        n = num_data_points

        eta = beta_rvs(old_value + 1, n)

        x = (a + k - 1) / (n * (b - log(eta)))

        pi = x / (1 + x)

        label = discrete_rvs([pi, 1 - pi])

        scale = b - log(eta)

        if label == 0:
            new_value = gamma_rvs(a + k, scale)
        else:
            new_value = gamma_rvs(a + k - 1, scale)

        return new_value
Esempio n. 4
0
 def _sample_precision(self, sample_size, sample_mean, sample_variance):
     prior_alpha = self.base_measure.params.alpha
     
     prior_beta = self.base_measure.params.beta
     
     prior_mean = self.base_measure.params.mean
     
     prior_size = self.base_measure.params.size
     
     posterior_alpha = prior_alpha + sample_size / 2
     
     posterior_beta = prior_beta + \
                      (sample_size * sample_variance) / 2 + \
                      ((prior_size * sample_size) / (2 * (prior_size + sample_size))) * (sample_mean - prior_mean) ** 2
     
     return gamma_rvs(posterior_alpha, posterior_beta)
Esempio n. 5
0
    def random(self):
        precision = gamma_rvs(self.params.alpha, self.params.beta) + 1e-10

        mean = gaussian_rvs(self.params.mean, self.params.size * precision)

        return GaussianGammaData(mean, precision)
Esempio n. 6
0
    def random(self):
        x = gamma_rvs(self.params.a, self.params.b)

        return GammaData(x)
Esempio n. 7
0
    def random(self, params):
        a, b = self._get_params(params.x)

        return GammaData(gamma_rvs(a, b))