Expectation Maximization
Coin Toss
Statistical Inference
Machine Learning
Probability Models

Expectation Maximization coin toss examples

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Expectation Maximization (EM) is a powerful statistical technique used to find maximum likelihood estimates of parameters in probabilistic models with latent variables. One of the classic examples to illustrate the workings of EM is the coin toss problem, where we determine the biases of coins given incomplete data about coin tosses.

Problem Setup

Imagine we have two coins, A and B, with biases πA\pi_A and πB\pi_B for heads, respectively. We perform a series of experiments where each experiment involves flipping one of these coins a number of times, but do not know which coin was used for each experiment. The goal of employing the EM algorithm is to estimate the biases πA\pi_A and πB\pi_B based on the observed data.

The EM Algorithm

The EM algorithm involves two main steps: Expectation (E-step) and Maximization (M-step). Initially, we start with an arbitrary guess of the parameter values and iterate through the E and M steps until convergence.

E-Step (Expectation)

In this step, we calculate the expected value of the latent variables given the current estimates of the parameters. For each experiment ii, we calculate the likelihood of the outcomes being generated by each coin. We determine the expected proportion of heads and tails that are contributed to coin A or coin B. Formally, for a given experiment, we compute:

PiP_i: The probability that experiment ii was performed with coin A.

Using Bayes' theorem, PiP_i can be computed as:

P_i(Adata)=L(dataπ_A)L(dataπ_A)+L(dataπ_B)P\_i(A | \text{data}) = \frac{L(\text{data} | \pi\_A)}{L(\text{data} | \pi\_A) + L(\text{data} | \pi\_B)}

where L(dataπ)L(\text{data} | \pi) is the likelihood of the data given the coin bias.

M-Step (Maximization)

Given the expected contributions calculated in the E-step, the M-step updates the parameters so that they maximize the expected log-likelihood. For the coin biases, we update πA\pi_A and πB\pi_B as follows:

π_Anew=_iP_i×heads_i_iP_i×(heads_i+tails_i)\pi\_A^{\text{new}} = \frac{\sum\_i P\_i \times \text{heads}\_i}{\sum\_i P\_i \times (\text{heads}\_i + \text{tails}\_i)}

π_Bnew=_i(1P_i)×heads_i_i(1P_i)×(heads_i+tails_i)\pi\_B^{\text{new}} = \frac{\sum\_i (1 - P\_i) \times \text{heads}\_i}{\sum\_i (1 - P\_i) \times (\text{heads}\_i + \text{tails}\_i)}

where headsi\text{heads}_i and tailsi\text{tails}_i are the number of heads and tails in experiment ii.

An Example

Consider the following observed data from experiments where coins A and B are flipped, but we do not know which coin was used:

ExperimentHeadsTails
155
291
382
446

Initial Guess

Suppose we initialize with πA=0.6\pi_A = 0.6 and πB=0.5\pi_B = 0.5.

Iterating EM

First E-step: Calculate PiP_i for each experiment using the initial guesses.

First M-step: Update πA\pi_A and πB\pi_B using the formulae provided.

Repeat these steps iteratively, updating the probabilities and biases until the estimates converge.

Convergence

The EM algorithm guarantees that the likelihood does not decrease with iteration, and it usually converges to a local maximum. In our coin toss problem, convergence means that the estimated biases of the coins stabilize.

Key Considerations

Initialization: The choice of initial values can affect convergence and may lead to sub-optimal solutions. • Convergence Criteria: Commonly thresholding the improvement in the log-likelihood or changes in parameter estimates. • Global Optima: EM can converge to local optima. Multiple runs with different initializations can help verify solutions.

Summary Table

StepDescription
Problem SetupDefine the coins' biases and unseen data.
E-StepCompute expected values of latent variables using current estimates.
M-StepUpdate parameters to maximize expected log-likelihood.
ConvergenceIterate until parameter estimates stabilize.

Conclusion

The EM algorithm is a versatile technique for parameter estimation in models with incomplete data. Its application to the coin toss example provides a clear and insightful introduction to its two-step iterative process. While EM guarantees non-decreasing likelihood, care must be taken with initialization and convergence verification to ensure meaningful results.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.