def test_posterior__p_f__l_f(self):

        expected = (0.3 * 0.8) / ((0.3 * 0.8) + (0.7 * 0.2))
        bayes_rule = BinaryBayesRule(prior=self.prior_float,
                                     likelihood=self.likelihood_float)
        actual = bayes_rule.posterior()
        self.assertAlmostEqual(expected, actual, 10)
    def test_posterior__p_b__l_f(self):

        posterior = BinaryBayesRule(
            prior=self.prior_beta,
            likelihood=self.likelihood_float).posterior()
        self.assertEqual(
            f'({str(self.prior_beta)} * {str(self.likelihood_float)}) / '
            f'(({str(self.prior_beta)} * {str(self.likelihood_float)}) + '
            f'((1 - {self.prior_beta}) * {str(1 - self.likelihood_float)}))',
            posterior.name)
        output = posterior.output()
        self.assertAlmostEqual(output.mean(), 0.64, 2)
        self.assertAlmostEqual(output.std(), 0.14, 2)
    def test_posterior__p_f__l_b(self):

        posterior = BinaryBayesRule(
            prior=self.prior_float,
            likelihood=self.likelihood_beta).posterior()
        self.assertEqual(
            f'({str(self.prior_float)} * {str(self.likelihood_beta)}) / '
            f'(({str(self.prior_float)} * {str(self.likelihood_beta)}) + '
            f'({1 - self.prior_float} * (1 - {str(self.likelihood_beta)})))',
            posterior.name)
        output = posterior.output()
        self.assertAlmostEqual(output.mean(), 0.58, 2)
        self.assertAlmostEqual(output.std(), 0.15, 2)
    def test_posterior__p_f__l_fm(self):

        expected = Series({
            '$100': (0.3 * 0.8) / ((0.3 * 0.8) + 0.7 * 0.2),
            '$200': (0.3 * 0.6) / ((0.3 * 0.6) + 0.7 * 0.4)
        })
        bayes_rule = BinaryBayesRule(prior=self.prior_float,
                                     likelihood=self.likelihood_float_map)
        actual = bayes_rule.posterior()
        self.assertIsInstance(actual, Series)
        self.assertListEqual(list(expected.keys()), list(actual.keys()))
        for key in expected.keys():
            self.assertAlmostEqual(expected[key], actual[key])
    def test_posterior__p_b__l_fm(self):

        posterior = BinaryBayesRule(
            prior=self.prior_beta,
            likelihood=self.likelihood_float_map).posterior()
        for key, likelihood in self.likelihood_float_map.items():
            self.assertEqual(
                f'({str(self.prior_beta)} * {str(likelihood)}) / '
                f'(({str(self.prior_beta)} * {str(likelihood)}) + '
                f'((1 - {self.prior_beta}) * {str(1 - likelihood)}))',
                posterior[key].name)
    def test_posterior__p_f__l_bm(self):

        posterior = BinaryBayesRule(
            prior=self.prior_float,
            likelihood=self.likelihood_beta_map).posterior()
        for key, likelihood in self.likelihood_beta_map.items():
            self.assertEqual(
                f'({str(self.prior_float)} * {str(likelihood)}) / '
                f'(({str(self.prior_float)} * {str(likelihood)}) + '
                f'({1 - self.prior_float} * (1 - {str(likelihood)})))',
                posterior[key].name)
    def test_posterior__p_bm__l_bm(self):

        prior = self.prior_beta_map
        likelihood = self.likelihood_beta_map
        posterior = BinaryBayesRule(prior=prior,
                                    likelihood=likelihood).posterior()
        for key in self.prior_beta_map.keys():
            self.assertEqual(
                f'({prior[key]} * {str(likelihood[key])}) / '
                f'(({prior[key]} * {str(likelihood[key])}) + '
                f'(({str(1 - prior[key])}) * (1 - {str(likelihood[key])})))',
                posterior[key].name)
    def test_posterior__p_fm__l_fm(self):

        posterior = BinaryBayesRule(
            prior=self.prior_float_map,
            likelihood=self.likelihood_float_map).posterior()
        for key in self.prior_float_map.keys():
            prior = self.prior_float_map[key]
            likelihood = self.likelihood_float_map[key]
            self.assertEqual(
                (prior * likelihood) / ((prior * likelihood) +
                                        ((1 - prior) * (1 - likelihood))),
                posterior[key])
    def test_posterior__p_fm__l_f(self):

        posterior = BinaryBayesRule(
            prior=self.prior_float_map,
            likelihood=self.likelihood_float).posterior()
        normalization = ((self.prior_float_map * self.likelihood_float) +
                         ((1 - self.prior_float_map) *
                          (1 - self.likelihood_float)))
        for key in self.prior_float_map.keys():
            self.assertEqual(
                self.prior_float_map[key] * self.likelihood_float /
                normalization[key], posterior[key])
Beispiel #10
0
wins = wins.round(0).astype(int)
losses: Series = \
    700 * Poisson(lambda_=10).pmf().at(range(1, 51)).rename('loss')
losses.index = range(100_000, 5_000_001, 100_000)
losses = losses.round(0).astype(int)

data = concat([wins, losses], axis=1)[['loss', 'win']]
axf = AxesFormatter()
data.plot.bar(ax=axf.axes)
axf.show()


def plot_probs(probs: DataFrame, weight):

    axf = AxesFormatter()
    data = probs.stack(
        level=['likelihood', 'prior']).rename('posterior').reset_index()
    boxplot(data=data, x='likelihood', y='posterior', hue='prior')
    axf.rotate_x_tick_labels(90)
    axf.set_y_lim(0, 1.05)
    axf.set_axis_below().grid()
    axf.set_text(title=str(weight))
    axf.show()


for weight in (0, 0.001, 0.01, 0.1, 1.0, 1000):

    br = BinaryBayesRule.from_counts(data, prior_weight=weight)
    samples = br.posterior(10000)
    plot_probs(samples, weight)