**Last updated:** 2021-04-20

**Checks:** 2 0

**Knit directory:** `fiveMinuteStats/analysis/`

This reproducible R Markdown analysis was created with workflowr (version 1.6.2). The *Checks* tab describes the reproducibility checks that were applied when the results were created. The *Past versions* tab lists the development history.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version b4bb124. See the *Past versions* tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use `wflow_publish`

or `wflow_git_commit`

). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:

```
Ignored files:
Ignored: .Rproj.user/
```

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.

These are the previous versions of the repository in which changes were made to the R Markdown (`analysis/em_variational.Rmd`

) and HTML (`docs/em_variational.html`

) files. If you’ve configured a remote Git repository (see `?wflow_git_remote`

), click on the hyperlinks in the table below to view the files as they were in that past version.

File | Version | Author | Date | Message |
---|---|---|---|---|

Rmd | b4bb124 | Matthew Stephens | 2021-04-20 | workflowr::wflow_publish(“analysis/em_variational.Rmd”) |

html | bf50b1a | Matthew Stephens | 2021-04-20 | Build site. |

Rmd | c3a3662 | Matthew Stephens | 2021-04-20 | workflowr::wflow_publish(“em_variational.Rmd”) |

This vignette describes the EM algorithm, using the very useful view due to Neal and Hinton. [Note on notation: Neal and Hinton use \(Z\) to denote observed data, but I use it to denote latent unobserved values. So be sure to keep this in mind if you are trying to relate their paper to my summary here.]

Suppose we have data \(X\) from a distribution \(p(X|\theta)\) where \(\theta\) denotes parameters whose values are unknown and live in some space \(\Theta\). We aim to find the maximum likelihood estimate for \(\theta\) \[\hat\theta:= \arg \max_{\theta \in \Theta} p(X | \theta).\] Suppose further that we can write \(p(X | \theta)\) as an integration (or sum) over latent variables \(Z\). That is \[p(X | \theta) = \int p(X, Z| \theta)\] for some joint distribution \(p(X,Z| \theta)\). (When considered as a function of \(\theta\) this distribution, \(p(X,Z | \theta)\), is sometimes referred to as ``the complete data likelihood".)

The EM algorithm provides a convenient way to find \(\hat\theta\) when the complete data log-likelihood has certain “friendly” features, which will become apparent below.

Note: throughout this note I treat \(Z\) as a continuous random variable, using integrals and density functions. However, the same ideas apply if \(Z\) is discrete: you can just replace integrals with sums and replace density functions with probability mass functions.

Note: \(X\) and \(Z\) willl typically both be multi-dimensional, not just scalars.

Let \(Q\) denote the space of all “distributions on \(Z\)”. So \(q \in Q\) means that \(q\) is a distribution on \(Z\), with density \(q(Z)\) say.

Define the following function \(F\) that maps \((\Theta, Q)\) to the real line: \[F(\theta,q):= E_q \log p(X, Z | \theta) + H(q)\] where \(H(q)\) denotes the entropy of \(q\): \[H(q) = - E_q \log(q(Z))\] Note that \(F\) can be rewritten as: \[F(q,\theta) = E_q(\log p(Z|X,\theta) p(X| \theta)/ q(Z)) = KL[ q || p(Z|X,\theta) ] + \log p(X| \theta).\]

where \(KL(q,p)\) denotes the Kullback–Leibler divergence from \(q\) to \(p\).

There are two key results.

For any given \(\theta\) let \(\hat{q}_\theta\) denote the value of \(q\) that maximizes \(F(q,\theta)\). That is, \[\hat{q}_\theta:= \arg \max_q F(\theta,q).\] This optimal \(q\) is given by the conditional distribution of the latent variables \(Z\) given the data \(X\) and \(\theta\): \[\hat{q}_\theta(z) = p(Z | X, \theta)\]

The likelihood \(l(\theta):=\log p(X| \theta)\) is related to \(F\) by: \[l(\theta) = \max_q F(\theta,q)\] Both results follow directly from \[F(q,\theta) = KL[ q || p(Z|X,\theta) ] + \log p(X| \theta).\] and the fact that the minimum of \[KL(q || p)\] over \(q\) is 0, attained at \(q=p\).

It follow from Result 2 above that the maximum likelihood estimate \(\hat\theta\) can be obtained by *jointly maximizing \(F\) over \(q\) and \(\theta\)*.

The EM algorithm simply does this by iterating the following two steps:

Step 1: \(q \leftarrow \arg \max_q F(q,\theta)\)

Step 2: \(\theta \leftarrow \arg \max_\theta F(q,\theta)\)

From result 1, step 1 is achieved by \(q = p(Z | X,\theta)\). Step 2 involves maximizing the expected complete-data log-likelihood, \[\theta \leftarrow \arg \max_\theta E_q \log p(X,Z|\theta).\] Of course the EM algorithm is only useful if both steps are straightforward.

This site was created with R Markdown