(sec_likelihood_model)=

# The likelihood model

Let $n$ be the total number of nodes in the tree sequence. At site $j$ the
available copying states $S_j$ are the nodes with a parent or at least one
child in the local tree.

## Copying paths

Let $r_j$ be the recombination probability on entering site $j$. A
recombination chooses uniformly among all $n$ nodes. For $u$ at the
previous site and $v\in S_j$, the transition weight is

$$
T_j(u,v) = r_j/n + (1-r_j) I_j(u,v),
$$

where $I_j(u,v)=1$ if $u=v$ and that node is present in every tree
between the two sites, including both endpoint trees. Otherwise it is
zero. Returning to the same node after a gap in its presence requires a
recombination. Probability assigned to unavailable states is lost.

Forward starts with weight $1/n$ on each node in $S_0$ and applies the
first site's transition. Before the first emission, each such node
therefore has weight $(1-r_0+r_0 |S_0|/n)/n$.

## Alleles and missing observations

At site $j$ with $A_j$ alleles, the copied node emits its own allele with
probability $m_j=1-(A_j-1)\mu_j$ and each other allele with probability
$\mu_j$. Choose probabilities so that $0\le r_j,\mu_j\le1$ and
$(A_j-1)\mu_j\le1$. An explicit allele list sets $A_j$ to its length.
By default tsls counts distinct ancestral and derived states at the site,
with a minimum of two.

For a missing query allele, every node receives the match weight $m_j$.
When positive, this factor cancels from relative path weights and
contributes $\log m_j$ to the log likelihood compared with omitting the
emission at that site.

## Interpreting results

A path's weight is the product of its initial weight at the first site,
its emission weights, and its transitions between sites. Viterbi selects
a maximum-weight path. The first-site weight is common to all starting
nodes and does not affect this choice.

Forward sums the path weights. Its log likelihood is the sum of the logs
of its per-site normalisation factors. For a fully observed haplotype,
this is the log probability of observing the haplotype **and** surviving
through the last site. Backward uses the factors from the same forward
run. The elementwise product of the decoded matrices gives the copying
probabilities conditional on the observations and survival. These
identities describe the recursions before numerical flooring or pruning,
which can change the results.

## Viterbi pruning

Relative to continuous copying, a transition requiring recombination has
weight $q_j=(r_j/n)/(1-r_j+r_j/n)$. Relative to a match with $m_j>0$, a
mismatch has weight $e_j=\mu_j/m_j$. {func}`tsls.path_threshold` uses
these ratios to choose a prune threshold for a given budget of mismatches
and switches. Count a return after a gap as a switch.

When all ratios are at most one, a path's product of ratios can only
decrease as it extends. The product of the $k$ smallest mismatch ratios
and the $s$ smallest switch ratios thus bounds every prefix of a path
with at most $k$ mismatches and $s$ switches from below. Normalising by
the best prefix can only increase that relative weight. If an optimal
path meets the budget, pruning values strictly below this bound preserves
an optimum in exact arithmetic, provided flooring does not alter the
competing path weights. The helper reduces the bound by a relative
tolerance for rounding.
