**Last updated:** 2022-04-19

**Checks:** 2 0

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

This reproducible R Markdown analysis was created with workflowr (version 1.7.0). 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 203cb51. 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: .Rhistory
Ignored: .Rproj.user/
Ignored: analysis/.Rhistory
Ignored: analysis/bernoulli_poisson_process_cache/
Untracked files:
Untracked: _workflowr.yml
Untracked: analysis/CI.Rmd
Untracked: analysis/gibbs_structure.Rmd
Untracked: analysis/libs/
Untracked: analysis/results.Rmd
Untracked: analysis/shiny/tester/
Unstaged changes:
Modified: analysis/LR_and_BF.Rmd
Modified: analysis/MH-examples1.Rmd
Modified: analysis/MH_intro.Rmd
Deleted: analysis/r_simplemix_extended.Rmd
```

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 | 203cb51 | Matthew Stephens | 2022-04-19 | workflowr::wflow_publish(“analysis/em_variational.Rmd”) |

html | 2ed9634 | Matthew Stephens | 2022-01-20 | Build site. |

Rmd | a918be9 | Matthew Stephens | 2022-01-20 | workflowr::wflow_publish(“analysis/em_variational.Rmd”) |

html | 0fb57d1 | Matthew Stephens | 2022-01-20 | Build site. |

Rmd | 3af0a34 | Matthew Stephens | 2022-01-20 | workflowr::wflow_publish(“analysis/em_variational.Rmd”) |

html | 515f0a6 | Matthew Stephens | 2021-04-20 | Build site. |

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

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

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) dZ\] for some joint distribution \(p(X,Z| \theta)\).

The pair \(X,Z\) is sometimes referred to as the “complete data” (based on the idea that \(Z\) is something that we wished we observed, but do not), and \(p(X,Z | \theta)\), as a function of \(\theta\), is sometimes referred to as “the complete data likelihood” – it is the likelihood we would have if we actually observed \(Z\) as well as \(X\).

The EM algorithm provides a convenient way to find \(\hat\theta\) when the complete data 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(\theta,q) = E_q(\log p(Z|X,\theta) p(X| \theta)/ q(Z)) = \log p(X| \theta) - KL[ q || p(Z|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(\theta,q)\). That is, \[\hat{q}_\theta:= \arg \max_q F(\theta,q).\] The first key result is that 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 log-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(\theta,q) = \log p(X| \theta) - KL[ q || p(Z|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(\theta,q)\)

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

From result 1, step 1 is achieved by \(q = p(Z | X,\theta)\). Step 2 involves *M*aximizing the *E*xpected “complete-data log-likelihood”, \[\theta \leftarrow \arg \max_\theta E_q \log p(X,Z|\theta).\] (Note: the E and M in the EM name come from the “Expectation” and “Maximization” in Step 2.)

Of course the EM algorithm is only useful if both steps are straightforward, which they are in some important problems. The reason for this is that the complete data log-likelihood \(\log p(X,Z| \theta)\) is often a simpler function of \(\theta\) than is the log-likelihod \(\log p(X | \theta)\).

One of the nice things about this view of the EM algorithm is that it naturally suggests extensions of the EM algorithm that may be useful when the regular EM is hard. For example, suppose that in Step 2 it is hard to optimize \(F\) over \(\theta\), but it is easy to find a new value of \(\theta\) that *increases* \(F\). Then using this “incremental” step instead of Step 2 may still be enough to produce an algorithm that optimizes \(F\), and therefore optimizes \(l(\theta)\).

Another variation on the EM algorithm arises if we replace step 1 with a constrained optimization \(q \leftarrow \arg \max_{q \in Q'} F(\theta,q)\) where \(Q'\) is a family of distributions chosen to make this optimization tractible. In this case the resulting algorithm would no longer optimize \(l(\theta)\) but it can be thought of as an approximation. This approach comes up in many “variational approximation” algorithms, and studying the accuracy of such approximations remains an active area of research.

This site was created with R Markdown