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

## Table of Contents

## 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

The following literature can help you to acquire this knowledge:

- Read Chapter 3 "Probability and Information Theory" of the Deep Learning Book
- (Optional) Notebook bayes-theorem.ipynb

### 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

Additionally, advertisements of those eggs guarantee a figurine to be in every seventh egg.

**Task:**

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

## Licenses

### Notebook License (CC-BY-SA 4.0)

*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

is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Based on a work at https://gitlab.com/deep.TEACHING.

### Code License (MIT)

*The following license only applies to code cells of the notebook.*

Copyright 2018 Christian Herta, Klaus Strohmenger

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.