Skip to content

Likelihood (CPU)

CPU reference implementation of Felsenstein's pruning algorithm.

log_likelihood

log_likelihood(tree: HifukuTree, alignment: Alignment, model: EigenDecomp) -> float

Compute total log-likelihood via the Felsenstein pruning algorithm.

The total is the sum over the site patterns of each pattern's log-likelihood, from :func:pattern_log_likelihoods, weighted by the number of sites that show it. The weighted sum is float64.

Parameters:

Name Type Description Default
tree HifukuTree
required
alignment Alignment - patterns uint8 (n_local, n_patterns),
    weights int32 (n_patterns,)
required
model EigenDecomp
required

Returns:

Name Type Description
float total log-likelihood (sum over all sites)
Source code in src/hifuku/pruning.py
def log_likelihood(
    tree: HifukuTree,
    alignment: Alignment,
    model: EigenDecomp,
) -> float:
    """
    Compute total log-likelihood via the Felsenstein pruning algorithm.

    The total is the sum over the site patterns of each pattern's
    log-likelihood, from :func:`pattern_log_likelihoods`, weighted by the
    number of sites that show it.  The weighted sum is float64.

    Parameters
    ----------
    tree      : HifukuTree (unrooted binary, node_order set)
    alignment : Alignment - patterns uint8 (n_local, n_patterns),
                weights int32 (n_patterns,)
    model     : EigenDecomp - DNA (4-state) or protein (20-state)

    Returns
    -------
    float : total log-likelihood (sum over all sites)
    """
    site_logL = pattern_log_likelihoods(tree, alignment, model)
    return float(np.dot(alignment.weights.astype(np.float64), site_logL))