# HTW Berlin - Angewandte Informatik - Advanced Topics - Exercise - Bayes Rule

## Introduction

In this notebook you will find exercises about:

• Bayes Theorem (also Bayes Rule)
• Chain Rule

In order to detect errors in your own code, execute the notebook cells containing assert or assert_almost_equal. These statements raise exceptions, as long as the calculated result is not yet correct.

## Requirements

### Knowledge

To complete this exercise notebook you should possess knowledge about the following topics.

• Bayes Theorem
• Bayes Rule / Chain Rule

### Python Modules

# External Modules
import numpy as np
from numpy.testing import assert_almost_equal

%matplotlib inline

## Exercise - Bayes Rule

Peter is going to buy a surprise egg. Surprise eggs are chocolate eggs, which contain a random toy. Some may include a rare figurine instead. We denote an egg containing a rare figurine by the variable$A = 1$, the absence of a rare figurine by$A = 0$. Peter has heard rumors, that a test (by shaking an egg) might reveal if it contains a rare figurine or not. The test result is denoted by$B = 1$ (figurine expected) and$B = 0$ (no figurine expected). The test however is only 70% reliable, meaning:

• In 70% of cases, the egg contains a figurine, the test result is positive
• In 70% of cases, the egg does not include a figurine, the test result is negative

If Peter applys the test to an egg and the test is positive, what is the probability that Peter really receives a figurine when he buys the egg?

Hint:

We are searching for $P(A=1\mid B=1)$

# Calculate the posterior by completing this cell

# Likelihood
pB1_A1 =

# Prior
pA1 =

# Normalizing constant
pB1 =

# Posterior [Solution P(A=1|B=1)]:
pA1_B1 = 
# Executing this cell must not throw an Exception
# The solution is obfuscated so you can solve the exercise without unintendedly spoiling yourself

obfuscated_solution = 149672.03999999998/534543
assert_almost_equal(pA1_B1, obfuscated_solution)

## Literature

The following license applies to the complete notebook, including code cells. It does however not apply to any referenced external media (e.g., images).

HTW Berlin - Angewandte Informatik - Advanced Topics - Exercise - Bayes Rule
by Christian Herta, Klaus Strohmenger