# HTW Berlin - Angewandte Informatik - Advanced Topics - Exercise - Expected Value

## Table of Contents

## Introduction

In this notebook you will find exercises about the expected value. You will calculate it analytically and programatically using discrete values.

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.

- Probability density function (pdf)
- Probability mass function (pmf)
- Expected value

The following literature can help you to acquire this knowledge:

- Read Chapter 3 "Probability and Information Theory" of the Deep Learning Book

### Python Modules

```
# External Modules
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
```

## Exercise - Expected Value

### Exercise - Probability Density Function

#### Pen & Paper Exercise

Given a probability density function $p(x)$. Its probability density between $0$ and $4$ is calculated with $p(x)=Cx$ and $p(x)=0$ otherwise.

Or formally:

$p(x) = \begin{cases} Cx & \text{for } x \in [0, 4] \\ 0 & \text{for } x \notin [0, 4] \\ \end{cases}$

**Task:**

Calculate the value of the constant $C$.

```
# Complete this cell
C = # Assign the right value you've calculated
```

```
# Executing this cell must not throw an Exception
# The solution is obfuscated so you can solve the exercise without unintendedly spoiling yourself
obfuscated_solution_1 = (23424+234-1024-23424+-2342-442+4422-444) / 3232
assert C == obfuscated_solution_1
```

### Exercise - Expected Value

**Task:**

Calculate the expected value of $\mathbb E [f(x)] \text{ with }f(x)=x^2$ with respect to the probability density $p$. Formally:

- Analytically
**(Pen & Paper Exercise)** - Using numpy with discrete values for $x$: x = np.linspace(0,4, int(10e5))

```
# Complete this cell for Task #2
x = np.linspace(0,4, int(10e5))
# (...)
s =
```

```
### Plot visualizing p and f
plt.plot(x, x*obfuscated_solution_1, label='p(x)')
plt.plot(x, x**2, label='f(x)')
plt.ylim(0,16)
plt.xlabel('x')
plt.legend()
```

## Summary and Outlook

In this Notebook you used a basic property of probability density functions to compute an unknown constant. You computed the extepected value of a function with respect to a pdf analytically and you approximated it using discrete values.

In **Exercise - Monte Carlo Estimator** you will see, that an approximation using `np.linspace(...)`

can sometimes be a very bad idea. Instead you will estimate the function using Monte Carlo estimator and inverse transform sampling (also known as Smirnov Transform) in order to draw samples using the correct distribution.

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

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.