Li–Stephens haplotype copying model
AbbreviationSite observation in the Li–Stephens model#✓fully proved
The observation at a site: the column of reference alleles and the target allele.
abbrev Obs (K : ℕ) (A : Type*) := (Fin K → A) × A
Used by 13
DefinitionObservation sequence of a reference panel and target haplotype#✓fully proved
The observation sequence for the reference panel H (haplotype k carries allele
H k t at site t) and the target haplotype x.
def obs (H : Fin K → ℕ → A) (x : ℕ → A) (t : ℕ) : Obs K A := (fun k => H k t, x t)
abbrev Obs (K : ℕ) (A : Type*) := (Fin K → A) × A
CSLibExt/Bioinformatics/PopulationStructure/LiStephens.lean:98
Uses 1
DefinitionLi–Stephens copying transition probability#✓fully proved
The copying transition probability: switch with probability ρ to a uniformly random
reference haplotype (possibly the same one), otherwise keep copying k:
(1 - ρ) [k = k'] + ρ / K.
def trans (ρ : R) (k k' : Fin K) : R := (1 - ρ) * (if k = k' then 1 else 0) + ρ / K
DefinitionLi–Stephens emission probability#✓fully proved
The emission probability: 1 - ε if the target allele equals the allele of the copied
haplotype k, and the mismatch probability ε otherwise.
def emit (ε : R) (k : Fin K) (o : Obs K A) : R := if o.1 k = o.2 then 1 - ε else ε
abbrev Obs (K : ℕ) (A : Type*) := (Fin K → A) × A
CSLibExt/Bioinformatics/PopulationStructure/LiStephens.lean:98
Uses 1 · Used by 8
DefinitionThe Li–Stephens copying model#✓fully proved
The Li–Stephens copying model with K reference haplotypes, switch probability ρ and
mismatch probability ε, as a hidden Markov model with a uniform initial copied haplotype.
def hmm (K : ℕ) (ρ ε : R) : HMM (Fin K) (Obs K A) R where
init _ := 1 / K
trans := trans ρ
emit := emit ε
structure HMM (S O R : Type*) where
/-- The initial weight of each state. -/
init : S → R
/-- The weight of a transition from the first state to the second. -/
trans : S → S → R
/-- The weight of emitting an observation from a state. -/
emit : S → O → R
CSLibExt/MachineLearning/Sequence/HiddenMarkov.lean:59
abbrev Obs (K : ℕ) (A : Type*) := (Fin K → A) × A
CSLibExt/Bioinformatics/PopulationStructure/LiStephens.lean:98
init : S → R
CSLibExt/MachineLearning/Sequence/HiddenMarkov.lean:61
def trans (ρ : R) (k k' : Fin K) : R := (1 - ρ) * (if k = k' then 1 else 0) + ρ / K
CSLibExt/Bioinformatics/PopulationStructure/LiStephens.lean:107
def emit (ε : R) (k : Fin K) (o : Obs K A) : R := if o.1 k = o.2 then 1 - ε else ε
CSLibExt/Bioinformatics/PopulationStructure/LiStephens.lean:111
Uses 4 · Used by 5
TheoremLi–Stephens transition rows sum to one#✓fully proved
Each transition row sums to 1 (when K is invertible in R, e.g. K ≠ 0 over ℝ).
theorem trans_row_sum (hK : (K : R) ≠ 0) (ρ : R) (k : Fin K) : ∑ k', trans ρ k k' = 1
def trans (ρ : R) (k k' : Fin K) : R := (1 - ρ) * (if k = k' then 1 else 0) + ρ / K
CSLibExt/Bioinformatics/PopulationStructure/LiStephens.lean:107
Uses 1
TheoremUniform initial law over haplotypes sums to one#✓fully proved
The initial law (uniform over the K haplotypes) sums to 1.
theorem init_sum (hK : (K : R) ≠ 0) (ρ ε : R) :
∑ k, (hmm (A := A) K ρ ε).init k = 1
def hmm (K : ℕ) (ρ ε : R) : HMM (Fin K) (Obs K A) R where
init _ := 1 / K
trans := trans ρ
emit := emit ε
CSLibExt/Bioinformatics/PopulationStructure/LiStephens.lean:115
Uses 3
TheoremBiallelic emission probabilities sum to one#✓fully proved
With biallelic sites, the two emission probabilities at a site sum to 1 over the target
allele, for each fixed panel column col (the emissions are conditional on the panel).
theorem emit_sum_bool (ε : R) (k : Fin K) (col : Fin K → Bool) :
∑ a : Bool, emit ε k (col, a) = 1
def emit (ε : R) (k : Fin K) (o : Obs K A) : R := if o.1 k = o.2 then 1 - ε else ε
CSLibExt/Bioinformatics/PopulationStructure/LiStephens.lean:111
Uses 1
DefinitionOne step of the collapsed forward recursion#✓fully proved
One step of the collapsed forward recursion on the previous site's forward variables v,
at output state k: e_k ((1 - ρ) v k + ρ (∑ j, v j) / K). This is the pointwise
specification; each call evaluates the length-K sum, so use fastStepTab for the O(K)
per-site step.
def fastStep (ρ ε : R) (o : Obs K A) (v : Fin K → R) (k : Fin K) : R :=
emit ε k o * ((1 - ρ) * v k + ρ * (∑ j, v j) / K)
abbrev Obs (K : ℕ) (A : Type*) := (Fin K → A) × A
CSLibExt/Bioinformatics/PopulationStructure/LiStephens.lean:98
def emit (ε : R) (k : Fin K) (o : Obs K A) : R := if o.1 k = o.2 then 1 - ε else ε
CSLibExt/Bioinformatics/PopulationStructure/LiStephens.lean:111
Uses 2 · Used by 1
DefinitionForward variables of the collapsed recursion#✓fully proved
The forward variables of the collapsed recursion, as a function of site and state. This is
the untabulated specification (it re-evaluates earlier sites inside each sum); fastForwardTab
is the O(t K) implementation.
def fastForward (ρ ε : R) (y : ℕ → Obs K A) : ℕ → Fin K → R
| 0, k => 1 / K * emit ε k (y 0)
| t + 1, k => fastStep ρ ε (y (t + 1)) (fastForward ρ ε y t) k
abbrev Obs (K : ℕ) (A : Type*) := (Fin K → A) × A
CSLibExt/Bioinformatics/PopulationStructure/LiStephens.lean:98
def emit (ε : R) (k : Fin K) (o : Obs K A) : R := if o.1 k = o.2 then 1 - ε else ε
CSLibExt/Bioinformatics/PopulationStructure/LiStephens.lean:111
def fastStep (ρ ε : R) (o : Obs K A) (v : Fin K → R) (k : Fin K) : R :=
emit ε k o * ((1 - ρ) * v k + ρ * (∑ j, v j) / K)
CSLibExt/Bioinformatics/PopulationStructure/LiStephens.lean:143
Uses 3 · Used by 3
TheoremCollapse of the sum over predecessor haplotypes#✓fully proved
The sum over predecessors collapses: ∑ j, v j * trans ρ j k = (1 - ρ) v k + ρ (∑ v) / K.
theorem sum_mul_trans (ρ : R) (v : Fin K → R) (k : Fin K) :
∑ j, v j * trans ρ j k = (1 - ρ) * v k + ρ * (∑ j, v j) / K
def trans (ρ : R) (k k' : Fin K) : R := (1 - ρ) * (if k = k' then 1 else 0) + ρ / K
CSLibExt/Bioinformatics/PopulationStructure/LiStephens.lean:107
Uses 1 · Used by 1
TheoremThe collapsed forward recursion computes the forward variables#✓fully proved
The collapsed forward recursion computes the forward variables of the Li–Stephens HMM.
theorem fastForward_eq_forward (ρ ε : R) (y : ℕ → Obs K A) :
∀ (t : ℕ) (k : Fin K), fastForward ρ ε y t k = forward (hmm K ρ ε) y t k
abbrev Obs (K : ℕ) (A : Type*) := (Fin K → A) × A
CSLibExt/Bioinformatics/PopulationStructure/LiStephens.lean:98
def fastForward (ρ ε : R) (y : ℕ → Obs K A) : ℕ → Fin K → R
| 0, k => 1 / K * emit ε k (y 0)
| t + 1, k => fastStep ρ ε (y (t + 1)) (fastForward ρ ε y t) k
CSLibExt/Bioinformatics/PopulationStructure/LiStephens.lean:149
def forward : ℕ → S → R
| 0, s => h.init s * h.emit s (x 0)
| t + 1, s => (∑ s', forward t s' * h.trans s' s) * h.emit s (x (t + 1))
CSLibExt/MachineLearning/Sequence/HiddenMarkov.lean:83
def hmm (K : ℕ) (ρ ε : R) : HMM (Fin K) (Obs K A) R where
init _ := 1 / K
trans := trans ρ
emit := emit ε
CSLibExt/Bioinformatics/PopulationStructure/LiStephens.lean:115
Uses 8 · Used by 2
TheoremThe likelihood of the target haplotype#✓fully proved
The likelihood of the target haplotype (given the panel) is the sum of the collapsed forward variables.
theorem likelihood_eq_sum_fastForward (ρ ε : R) (y : ℕ → Obs K A) (t : ℕ) :
likelihood (hmm K ρ ε) y t = ∑ k, fastForward ρ ε y t k
abbrev Obs (K : ℕ) (A : Type*) := (Fin K → A) × A
CSLibExt/Bioinformatics/PopulationStructure/LiStephens.lean:98
def likelihood (t : ℕ) : R := ∑ z : Fin (t + 1) → S, pathProb h x t z
CSLibExt/MachineLearning/Sequence/HiddenMarkov.lean:79
def hmm (K : ℕ) (ρ ε : R) : HMM (Fin K) (Obs K A) R where
init _ := 1 / K
trans := trans ρ
emit := emit ε
CSLibExt/Bioinformatics/PopulationStructure/LiStephens.lean:115
def fastForward (ρ ε : R) (y : ℕ → Obs K A) : ℕ → Fin K → R
| 0, k => 1 / K * emit ε k (y 0)
| t + 1, k => fastStep ρ ε (y (t + 1)) (fastForward ρ ε y t) k
CSLibExt/Bioinformatics/PopulationStructure/LiStephens.lean:149
DefinitionTabulated O(K) forward step#✓fully proved
One tabulated O(K) step: the total of the previous table is computed once.
def fastStepTab (ρ ε : R) (o : Obs K A) (v : Vector R K) : Vector R K :=
let S := sumVec v
Vector.ofFn fun k => emit ε k o * ((1 - ρ) * v[k] + ρ * S / K)
abbrev Obs (K : ℕ) (A : Type*) := (Fin K → A) × A
CSLibExt/Bioinformatics/PopulationStructure/LiStephens.lean:98
def sumVec [AddCommMonoid R] (v : Vector R n) : R := ∑ s : Fin n, v[s]
CSLibExt/MachineLearning/Sequence/HiddenMarkov.lean:198
def emit (ε : R) (k : Fin K) (o : Obs K A) : R := if o.1 k = o.2 then 1 - ε else ε
CSLibExt/Bioinformatics/PopulationStructure/LiStephens.lean:111
Uses 3 · Used by 1
DefinitionTabulated O(K) forward variables#✓fully proved
The O(K) forward variables, tabulated site by site: O(t K) operations in total.
def fastForwardTab (ρ ε : R) (y : ℕ → Obs K A) : ℕ → Vector R K
| 0 => Vector.ofFn fun k => 1 / K * emit ε k (y 0)
| t + 1 => fastStepTab ρ ε (y (t + 1)) (fastForwardTab ρ ε y t)
abbrev Obs (K : ℕ) (A : Type*) := (Fin K → A) × A
CSLibExt/Bioinformatics/PopulationStructure/LiStephens.lean:98
def emit (ε : R) (k : Fin K) (o : Obs K A) : R := if o.1 k = o.2 then 1 - ε else ε
CSLibExt/Bioinformatics/PopulationStructure/LiStephens.lean:111
def fastStepTab (ρ ε : R) (o : Obs K A) (v : Vector R K) : Vector R K :=
let S := sumVec v
Vector.ofFn fun k => emit ε k o * ((1 - ρ) * v[k] + ρ * S / K)
CSLibExt/Bioinformatics/PopulationStructure/LiStephens.lean:180
Uses 3 · Used by 3
TheoremTabulated recursion computes the Li–Stephens forward variables#✓fully proved
The tabulated O(K) recursion computes the forward variables of the Li–Stephens HMM.
theorem fastForwardTab_get (ρ ε : R) (y : ℕ → Obs K A) :
∀ (t : ℕ) (k : Fin K), (fastForwardTab ρ ε y t)[k] = forward (hmm K ρ ε) y t k
abbrev Obs (K : ℕ) (A : Type*) := (Fin K → A) × A
CSLibExt/Bioinformatics/PopulationStructure/LiStephens.lean:98
def fastForwardTab (ρ ε : R) (y : ℕ → Obs K A) : ℕ → Vector R K
| 0 => Vector.ofFn fun k => 1 / K * emit ε k (y 0)
| t + 1 => fastStepTab ρ ε (y (t + 1)) (fastForwardTab ρ ε y t)
CSLibExt/Bioinformatics/PopulationStructure/LiStephens.lean:185
def forward : ℕ → S → R
| 0, s => h.init s * h.emit s (x 0)
| t + 1, s => (∑ s', forward t s' * h.trans s' s) * h.emit s (x (t + 1))
CSLibExt/MachineLearning/Sequence/HiddenMarkov.lean:83
def hmm (K : ℕ) (ρ ε : R) : HMM (Fin K) (Obs K A) R where
init _ := 1 / K
trans := trans ρ
emit := emit ε
CSLibExt/Bioinformatics/PopulationStructure/LiStephens.lean:115
Uses 7 · Used by 1
DefinitionThe tabulated Li–Stephens likelihood#✓fully proved
The tabulated Li–Stephens likelihood in O(t K) operations.
def likelihoodTab (ρ ε : R) (y : ℕ → Obs K A) (t : ℕ) : R := sumVec (fastForwardTab ρ ε y t)
abbrev Obs (K : ℕ) (A : Type*) := (Fin K → A) × A
CSLibExt/Bioinformatics/PopulationStructure/LiStephens.lean:98
def sumVec [AddCommMonoid R] (v : Vector R n) : R := ∑ s : Fin n, v[s]
CSLibExt/MachineLearning/Sequence/HiddenMarkov.lean:198
def fastForwardTab (ρ ε : R) (y : ℕ → Obs K A) : ℕ → Vector R K
| 0 => Vector.ofFn fun k => 1 / K * emit ε k (y 0)
| t + 1 => fastStepTab ρ ε (y (t + 1)) (fastForwardTab ρ ε y t)
CSLibExt/Bioinformatics/PopulationStructure/LiStephens.lean:185
Uses 3 · Used by 1
TheoremTabulated recursion computes the haplotype likelihood#✓fully proved
The tabulated O(K) recursion computes the likelihood of the target haplotype.
theorem likelihoodTab_eq (ρ ε : R) (y : ℕ → Obs K A) (t : ℕ) :
likelihoodTab ρ ε y t = likelihood (hmm K ρ ε) y t
abbrev Obs (K : ℕ) (A : Type*) := (Fin K → A) × A
CSLibExt/Bioinformatics/PopulationStructure/LiStephens.lean:98
def likelihoodTab (ρ ε : R) (y : ℕ → Obs K A) (t : ℕ) : R := sumVec (fastForwardTab ρ ε y t)
CSLibExt/Bioinformatics/PopulationStructure/LiStephens.lean:199
def likelihood (t : ℕ) : R := ∑ z : Fin (t + 1) → S, pathProb h x t z
CSLibExt/MachineLearning/Sequence/HiddenMarkov.lean:79
def hmm (K : ℕ) (ρ ε : R) : HMM (Fin K) (Obs K A) R where
init _ := 1 / K
trans := trans ρ
emit := emit ε
CSLibExt/Bioinformatics/PopulationStructure/LiStephens.lean:115
Loading dependency explorer…
The dependency explorer needs JavaScript and its graph data.