Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Copy, switch, and impute with a Li--Stephens HMM

We ask one question. A target haplotype resembles a mosaic of reference haplotypes. Which reference haplotype is copied at each marker, and what allele is likely at an untyped marker?

Start from first principles

  • A target haplotype is a mosaic of references along the chromosome (state sequence).

  • The copied reference at each marker is unobserved (hidden state).

  • The model represents linkage disequilibrium by retaining a local source (state persistence).

  • Recombination can redraw the copying source (transition probability).

  • Mutation can make the copied allele differ from its source (emission probability).

  • Typed markers provide observed alleles; untyped markers do not (missing observation).

This worked example connects haplotypes, linkage disequilibrium, recombination, phase, and imputation with an HMM, posterior probabilities, and a sum over hidden copying paths.

1. Concept and intuition

Suppose we know a panel of phased reference haplotypes. At marker ss, the hidden state ZsZ_s records which reference haplotype is being copied.

  • Staying in the same state means continuing to copy the same donor.

  • Changing state represents a historical recombination event.

  • An observed allele usually matches the copied donor. The original Li--Stephens model uses mutation to allow an imperfect copy.

The worked example contains 20 reference haplotypes and 100 markers. A target haplotype is generated by copying the panel with occasional switches and mismatches, and only 30 markers are observed. The true copying path and the missing alleles are retained only for checking the answer.

Reference panel: 20 haplotypes by 100 markers
Target: 30 typed markers and 70 markers to impute
Loading...

2. Turn the copying idea into probabilities

The HMM is defined by three probability statements.

Initial state

We treat all KK reference haplotypes as equally likely at the first marker.

P(Z1=k)=1/K.P(Z_1=k)=1/K.

Transition probability

Between adjacent markers, continue copying with probability 1−ρ1-\rho. With probability ρ\rho, redraw a donor uniformly from the KK references.

P(Zs=k∣Zs−1=j)=(1−ρ) I(k=j)+ρK.P(Z_s=k\mid Z_{s-1}=j)=(1-\rho)\,I(k=j)+\frac{\rho}{K}.

A redraw may select the same donor again. Every row of the transition matrix therefore sums to one. A full Li--Stephens model lets ρ\rho vary with genetic distance and reference-panel size. Here it is fixed so that the idea remains visible.

Emission probability

If the observed allele is xsx_s and reference haplotype kk carries HskH_{sk},

P(xs∣Zs=k)={1−ϵ,xs=Hsk,ϵ,xs≠Hsk.P(x_s\mid Z_s=k)=\begin{cases}1-\epsilon,&x_s=H_{sk},\\ \epsilon,&x_s\neq H_{sk}.\end{cases}

In the original Li--Stephens model, this mismatch represents mutation in an imperfect copy. The paper derives it from a population mutation parameter. Here ϵ\epsilon is the direct probability of a mismatch between the copied and target alleles. No genotyping error is simulated.

Modern phasing and imputation implementations often broaden this term to include target or reference allele errors and imperfect fit of the copying model.

Untyped marker

At an untyped marker there is no observed xsx_s. We set the likelihood factor to the same neutral value for every state,

P(no observation at s∣Zs=k)=1.P(\text{no observation at }s\mid Z_s=k)=1.

This does not assign an allele probability of 1. It lets neighboring typed markers determine the posterior copying source without adding local evidence.

Loading...
Loading...

3. Sum over the hidden copying paths

There are KMK^M possible copying paths. This example already has 20100 paths, so direct enumeration is impossible.

The first HMM has the same problem. Its two hidden states give 2T2^T paths. Li--Stephens has KK possible donors at every marker, giving KMK^M paths.

The forward probability carries evidence from the left,

αs(k)=P(x1:s,Zs=k),\alpha_s(k)=P(x_{1:s},Z_s=k),

and the backward probability carries evidence from the right,

βs(k)=P(xs+1:M∣Zs=k).\beta_s(k)=P(x_{s+1:M}\mid Z_s=k).

Combining them gives the posterior copying probability

P(Zs=k∣x1:M)∝αs(k)βs(k).P(Z_s=k\mid x_{1:M})\propto\alpha_s(k)\beta_s(k).

Forward--backward reuses partial sums to sum over hidden paths without listing them (dynamic programming). Scaling prevents numerical underflow without changing the posterior probabilities.

Log-likelihood: -18.56

4. Read the posterior copying path

The heatmap below shows the posterior probability of each reference haplotype at each marker. A high posterior probability marks a plausible donor after using all typed markers on both sides.

The black line is the simulated copying path, retained as a control. The most probable donor need not always be the true donor because several reference haplotypes can carry the same local allele pattern.

Loading...
plot without title

5. Impute a missing allele

At an untyped marker, the emission factor 1 contributes no local evidence. It is not the probability of the missing allele. Typed markers on both sides tell us which donor is likely. We then average the reference alleles using the posterior copying probabilities:

P(xs=1∣xtyped)=∑k=1KP(Zs=k∣xtyped)Hsk.P(x_s=1\mid x_{\mathrm{typed}})=\sum_{k=1}^{K}P(Z_s=k\mid x_{\mathrm{typed}})H_{sk}.

This posterior probability is an allele dosage between 0 and 1. Thresholding at 0.5 gives a hard call, but the dosage retains uncertainty and is usually the more informative output.

Loading...
Missing alleles correctly imputed: 59 of 70 (84.3%)

What to remember

  1. Concept and intuition. A haplotype receives high probability when it is an imperfect mosaic of reference haplotypes. The original mismatch model represents mutation during copying.

  2. Insight behind the approach. The donor is the hidden state, recombination controls transitions, and allele matches control emissions. Forward--backward uses dynamic programming to sum over copying paths.

  3. Worked example. An untyped marker contributes no local emission evidence. Typed markers identify likely donors, and their alleles give a posterior dosage.

The copying process is a tractable approximation to shared ancestry and recombination.

Scope of this notebook

This example uses one phased haploid target so that one copying path remains visible. Diploid genotype imputation uses two unknown paths through the reference panel and jointly addresses haplotype phase. Full models also use genetic distance, larger panels, and genotype uncertainty.

Source record and further detail

  • The simulation uses seed 534, 20 reference haplotypes, 100 markers, a typed fraction of 0.30, ρ=0.05\rho=0.05, and ϵ=0.02\epsilon=0.02.

  • The transition matrix uses (1−ρ)I+(ρ/K)11T(1-\rho)I+(\rho/K)\mathbf{1}\mathbf{1}^{T}. It implements Pritchard’s generate-or-redraw description and guarantees that every row sums to one.

  • The original paper uses mutation to motivate imperfect copying. Its main analysis assumes known haplotypes with no missing genotypes or genotyping errors.

  • This notebook uses a direct biallelic mismatch probability ϵ\epsilon in place of the paper’s sample-size-based θ~\widetilde{\theta} parameterization.

  • Pritchard, An Owner’s Guide to the Human Genome, Chapter 2.3, especially the haplotype-copying and phasing/imputation sections.

  • Li and Stephens (2003), Modeling Linkage Disequilibrium and Identifying Recombination Hotspots Using Single-Nucleotide Polymorphism Data.

References
  1. Li, N., & Stephens, M. (2003). Modeling Linkage Disequilibrium and Identifying Recombination Hotspots Using Single-Nucleotide Polymorphism Data. Genetics, 165(4), 2213–2233. 10.1093/genetics/165.4.2213