def test_set_alpha_with_array(self): d = Dirichlet([0.1, 0.2, 0.3, 0.4]) expected = Series({'α1': 0.4, 'α2': 0.3, 'α3': 0.2, 'α4': 0.1}) d.alpha = [0.4, 0.3, 0.2, 0.1] actual = d.alpha self.assertTrue(expected.equals(actual))
def setUp(self) -> None: self.prior_dirichlet = Dirichlet([1 + 70, 1 + 25, 1 + 5]) self.likelihood_dirichlet = Dirichlet([1 + 6, 1 + 3, 1 + 1]) self.likelihood_dirichlet_map = Series({ '$100': Dirichlet([1 + 8, 1 + 1, 1 + 1]), '$200': Dirichlet([1 + 6, 1 + 3, 1 + 1]) }) self.prior_float_map = Series({'$100': 0.3, '$200': 0.2, '$300': 0.5}) self.likelihood_float_map = Series({ '$100': 0.1, '$200': 0.2, '$300': 0.3 })
def from_counts( data: DataFrame, prior_weight: float = 1.0 ) -> 'MultipleBayesRule': """ Return a new BayesRule class using a DataFrame of counts. :param data: DataFrame of counts where the index represents the different states of the evidence B, and each column represents the likelihood for one value of the prior A. :param prior_weight: Proportion of the overall counts to use as the prior probability. """ prior = Dirichlet(1 + data.sum() * prior_weight) likelihood = Series({ key: Dirichlet(1 + row) for key, row in data.iterrows() }) return MultipleBayesRule(prior=prior, likelihood=likelihood)
def setUp(self) -> None: self.b1 = Beta(700, 300) self.b2 = Beta(600, 400) self.b3 = Beta(500, 500) self.d1 = Dirichlet([500, 300, 200]) self.d2 = Dirichlet({'x': 100, 'y': 200, 'z': 300}) self.b1__mul__b2 = self.b1 * self.b2 self.b3__mul__b1__mul__b2 = self.b3 * self.b1__mul__b2 self.b1__mul__comp__b1 = self.b1 * (1 - self.b1) self.b_series = Series({'b1': self.b1, 'b2': self.b2, 'b3': self.b3}) self.b_frame = DataFrame({ 'c1': { 'r1': self.b1, 'r2': self.b2 }, 'c2': { 'r1': self.b2, 'r2': self.b3 } }) self.float_series = Series({'$100': 0.8, '$200': 0.6})
def test_infer_posteriors(self): expected = DataFrame(data=[(1, 1, 'e', Dirichlet({ 1: 3, 2: 2, 3: 2 })), (2, 1, 'e', Dirichlet({ 1: 2, 2: 3, 3: 2 })), (1, 2, 'e', Dirichlet({ 1: 2, 2: 2, 3: 3 })), (2, 2, 'e', Dirichlet({ 1: 3, 2: 2, 3: 2 })), (1, 1, 'f', Dirichlet({ 2: 4, 3: 2 })), (2, 1, 'f', Dirichlet({ 2: 3, 3: 3 })), (1, 2, 'f', Dirichlet({ 2: 2, 3: 4 })), (2, 2, 'f', Dirichlet({ 2: 4, 3: 2 }))], columns=['a', 'b', 'prob_var', 'Dirichlet']) actual = DirichletMultinomialConjugate.infer_posteriors( data=self.multinomial_data, prob_vars=['e', 'f'], cond_vars=['a', 'b']) for _, row in expected.iterrows(): actual_dirichlet = actual.loc[ (actual['a'] == row['a']) & (actual['b'] == row['b']) & (actual['prob_var'] == row['prob_var']), 'Dirichlet'].iloc[0] self.assertTrue(row['Dirichlet'] == actual_dirichlet)
def setUp(self) -> None: self.alpha = Series({'a': 0.4, 'b': 0.3, 'c': 0.2, 'd': 0.1}) self.d_array = Dirichlet(alpha=self.alpha.values) self.d_series = Dirichlet(alpha=self.alpha) self.d_dict = Dirichlet(alpha=self.alpha.to_dict())
def plot_distribution(): ax = new_axes(width=10, height=10) Dirichlet(alpha=[1, 2, 3]).plot(x=x, ax=ax) plt.show()
def plot_pdf_simplex(): ax = new_axes(width=10, height=10) Dirichlet(alpha=[1, 2, 3]).pdf().plot_simplex(ax=ax) plt.show()
def test_infer_posterior(self): expected = Dirichlet(alpha={'a': 5 + 1, 'b': 3 + 1, 'c': 2 + 1}) actual = DirichletMultinomialConjugate.infer_posterior(self.series) self.assertEqual(expected, actual)