Exercise - A Simple Example for the EM Algorithm

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 zi{1,2}z_i \in \{1,2\}. When we observe a data point xix_i, we do not know if it corresponds to class 11 or 22. But we know the value of xix_i, which is either aa, bb, or cc and we know that xix_i must be class 22 if its value is cc and it must be class 11 if its value is bb.

Formally, this can be described with:

  • Observations xi{a,b,c}x_i \in \{a,b,c\}
  • Hidden states: zi{1,2}z_i \in \{1,2\}
  • Probability p(zi=1)=αp(z_i=1)=\alpha
  • Parameters: θ={θ0,θ1,θ3=α\theta = \{\theta_0, \theta_1, \theta_3=\alpha}
  • ii: data index

and P(zx)P(z\mid x):

We now observe some data points x\bf x:

  • we observe kk times "aa"
  • we observe ll times "bb"
  • we observe mm times "cc"

Task:

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

  1. Infere the equations for the E-Step and the M-Step (pen & paper)
  2. Implement the corresponding functions
  3. Choose concrete values for kk, ll, mm together with some starting values for θ\theta and test your implementation

E-Step

q(zi)=p(zixi,θ)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}$$

qc(zi=1)=p(zi=1xi=c,θ)=0q_c(z_i=1) = p(z_i=1 \mid x_i=c, \theta ) = 0
q_c(z_i=2) = p(z_i=2 \mid x_i=c, \theta ) &= 1 \end{align}$$

M-Step

From

θ(t+1)=argmaxθL(q,θ)=argmaxθiEqi[logp(xi,ziθ)]\begin{aligned} \theta^{(t+1)} &= \text{arg} \max_{\theta} L (q, \theta) \\ &=\text{arg} \max_{\theta} \sum_{i} \mathbb E_{q_i} \left[ \log {p({x}_i, {z_i} \mid \theta )} \right]\\ \end{aligned}

Show that:

θ0=11+lkqa(zi=1) \theta_0 = \frac{1}{1+ \frac{l}{kq_a(z_i=1)}}
\theta_1 = \frac{1}{1+ \frac{m}{ k q_a(z_i=2) }} \\
α=11+kqa(zi=2)+mkqa(zi=1)+l \alpha= \frac{1}{1+\frac{kq_a(z_i=2)+m}{kq_a(z_i=1) + l}}
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.