# Exercise - A Simple Example for the EM Algorithm

## Table of Contents

## Introduction

[**TODO**]

Remark: 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:

- Maximum-Likelihood
- Notes by Christian Herta [HER18a]
- Expectation Maximization:
- Notes by Christian Herta [HER18b]
- Coursera Video [NOV18]

### Python Modules

`import numpy as np`

## Exercises

Given are two classes $z_i \in \{1,2\}$. When we observe a data point $x_i$, we do not know if it corresponds to class $1$ or $2$. But we know the value of $x_i$, which is either $a$, $b$, or $c$ and we know that $x_i$ must be class $2$ if its value is $c$ and it must be class $1$ if its value is $b$.

Formally, this can be described with:

- Observations $x_i \in \{a,b,c\}$
- Hidden states: $z_i \in \{1,2\}$
- Probability $p(z_i=1)=\alpha$
- Parameters: $\theta = \{\theta_0, \theta_1, \theta_3=\alpha$}
- $i$: data index

and $P(z\mid x)$:

We now observe some data points $\bf x$:

- we observe $k$ times "$a$"
- we observe $l$ times "$b$"
- we observe $m$ times "$c$"

**Task:**

Use the expectation maximization algorithm to find point estimates for the probability $P(z=1 \mid \bf x)$ and $\theta$.

- Infere the equations for the E-Step and the M-Step (pen & paper)
- Implement the corresponding functions
- Choose concrete values for $k$, $l$, $m$ together with some starting values for $\theta$ and test your implementation

#### E-Step

$q(z_i) = p(z_i \mid x_i, \theta )$

(...)

Compute the folowing quantities.

```
q_a(z_i=1) = p(z_i=1 \mid x_i=a, \theta ) &= ?\\
\end{align}$$
```

q_a(z_i=2) = p(z_i=2 \mid x_i=a, \theta ) = ?

```
# Implement it in functions:
def e_step_q_a1(theta_0, theta_1, alpha):
raise NotImplementedError()
```

```
def e_step_q_a2(theta_0, theta_1, alpha):
raise NotImplementedError()
```

Show that:

```
q_b(z_i=1) = p(z_i=1 \mid x_i=b, \theta ) &= 1
\end{align}$$
```

q_c(z_i=2) = p(z_i=2 \mid x_i=b, \theta ) &= 0 \end{align}$$

```
q_c(z_i=2) = p(z_i=2 \mid x_i=c, \theta ) &= 1
\end{align}$$
```

#### M-Step

From

Show that:

```
\theta_1 =
\frac{1}{1+ \frac{m}{ k q_a(z_i=2) }} \\
```

```
def m_step_theta0(q_a1, k, l):
raise NotImplementedError()
```

```
def log_likelihood(theta_0, theta_1, alpha, k, l, m):
raise NotImplementedError()
```

```
# chose concrete values for your observation
k=4200
l=420
m=42
# chose some starting values
theta_0 = 0.424242
theta_1 = 0.4242
alpha = 0.42
# Run your implementation. In order to pass the tests below,
# update your theta and alpha values within the same variable.
# Or at least assign your final values to theta_0, theta_1 and alpha
```

```
# To test your implementation run this cell
n = k + l + m
n_1 = alpha*n
n_2 = n - n_1
k_ = n_1 * theta_0 + n_2 * theta_1
l_ = n_1 * (1-theta_0)
m_ = n_2 * (1-theta_1)
np.testing.assert_almost_equal(k_, k)
np.testing.assert_almost_equal(l_, l)
np.testing.assert_almost_equal(m_, m)
```

## 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).*

Exercise - A Simple Example for the EM Algorithm

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

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.