Evolutionary Analysis Using Likelihood

Specifying substitution models

The available pre-defined substitution models

In cases where code takes a substitution model as an argument, you can use the value under “Abbreviation” as a string.

Getting a substitution model with get_model()

Rate heterogeneity models

We illustrate this for the gamma distributed case using examples of the canned models displayed above. Creating rate heterogeneity variants of the canned models can be done by using optional arguments that get passed to the substitution model class.

For nucleotide

We specify a general time reversible nucleotide model with gamma distributed rate heterogeneity.

For codon

We specify a conditional nucleotide frequency codon model with nucleotide general time reversible parameters and a parameter for the ratio of nonsynonymous to synonymous substitutions (omega) with gamma distributed rate heterogeneity.

For protein

We specify a Jones, Taylor and Thornton 1992 empirical protein substitution model with gamma distributed rate heterogeneity.

Making a likelihood function

You start by specifying a substitution model and use that to construct a likelihood function for a specific tree.

Providing an alignment to a likelihood function

You need to load an alignment and then provide it a likelihood function. I construct very simple trees and alignments for this example.

Scoping parameters on trees – time heterogeneous models

For many evolutionary analyses, it’s desirable to allow different branches on a tree to have different values of a parameter. We show this for a simple codon model case here where we want the great apes (the clade that includes human and orangutan) to have a different value of the ratio of nonsynonymous to synonymous substitutions. This parameter is identified in the precanned CNFGTR model as omega.

We’ve set an initial value for this clade so that the edges affected by this rule are evident below.

A more extensive description of capabilities is in Allowing substitution model parameters to differ between branches.

Specifying a parameter as constant

This means the parameter will not be modified during likelihood maximisation. We show this here by making the omega parameter constant at the value 1 – essentially the condition of selective neutrality.

Providing a starting value for a parameter

This can be useful to improve performance, the closer you are to the maximum likelihood estimator the quicker optimisation will be.

Setting parameter bounds for optimisation

This can be useful for stopping optimisers from getting stuck in a bad part of parameter space. The following is for omega in a codon model. I’m also providing an initial guess for the parameter (init=0.1) as well as a lower bound. An initial guess that is close to the maximum likelihood estimate will speed up optimisation.

Setting an upper bound for branch length

If the branch length estimates seem too large, setting just an upper bound can be sensible. This will apply to all edges on the tree.

Note

If, after optimising, the branch lengths equal to the upper value you set then the function has not been fully maximised and you should consider adjusting the boundary again.

Specifying rate heterogeneity functions

We extend the simple gamma distributed rate heterogeneity case for nucleotides from above to construction of the actual likelihood function. We do this for 4 bins and constraint the bin probabilities to be equal.

For more detailed discussion of defining and using these models see Analysis of rate heterogeneity.

Specifying Phylo-HMMs

For more detailed discussion of defining and using these models see Evaluate process heterogeneity using a Hidden Markov Model.

Fitting likelihood functions - Choice of optimisers

There are 2 types of optimiser: simulated annealing, a global optimiser; and Powell, a local optimiser. The simulated annealing method is slow compared to Powell and in general Powell is an adequate choice. I setup a simple nucleotide model to illustrate these.

The default is to use Powell. For Powell, it’s recommended to set the max_restarts argument since this provides a mechanism for Powell to attempt restarting the optimisation from a slightly different spot which can help in overcoming local maxima.

We might want to do crude simulated annealing following by more rigorous Powell. To do this we first need to use the global optimiser, setting local=False setting a large value for global_tolerance.

Followed by a standard call to optimise().

How to check your optimisation was successful

There is no guarantee that an optimised function has achieved a global maximum. We can, however, be sure that a maximum was achieved by validating that the optimiser stopped because the specified tolerance condition was met, rather than exceeding the maximum number of evaluations. The latter number is set to ensure optimisation doesn’t proceed endlessly. If the optimiser exited because this limit was exceeded you can be sure that the function has not been successfully optimised.

We can monitor this situation using the limit_action argument to optimise. Providing the value raise causes an exception to be raised if this condition occurs, as shown below. Providing warn (default) instead will cause a warning message to be printed to screen but execution will continue. The value ignore hides any such message.

Note

We recommend using limit_action='raise' and catching the ArithmeticError error explicitly (as demonstrated above). You really shouldn’t be using results from such an optimisation run.

Overview of the fitted likelihood function

In Jupyter, the likelihood function object presents a representation of the main object features.

Log likelihood and number of free parameters

Reusing the optimised lf object from above, we can get the log-likelihood and the number of free parameters.

Warning

The number of free parameters (nfp) refers only to the number of parameters that were modifiable by the optimiser. Typically, the degrees-of-freedom of a likelihood ratio test statistic is computed as the difference in nfp between models. This will not be correct for models in which a boundary conditions exist (rate heterogeneity models where a parameter value boundary is set between bins).

Aikake Information Criterion

Reusing the optimised lf object from above.

We can also get the second-order AIC.

Bayesian Information Criterion

Reusing the optimised lf object from above.

Getting maximum likelihood estimates

Reusing the optimised lf object from above.

One at a time

We get the statistics out individually. We get the length for the Human edge and the exchangeability parameter A/G.

Just the motif probabilities
As tables

Testing Hypotheses - Using Likelihood Ratio Tests

We test the molecular clock hypothesis for human and chimpanzee lineages. The null has these two branches constrained to be equal.

The alternate allows the human and chimpanzee branches to differ by just setting all lengths to be independent.

We import the function for computing the probability of a chi-square test statistic, compute the likelihood ratio test statistic, degrees of freedom and the corresponding probability.

Testing Hypotheses - By parametric bootstrapping

If we can’t rely on the asymptotic behaviour of the LRT, e.g. due to small alignment length, we can use a parametric bootstrap. Convenience functions for that are described in more detail here Performing a parametric bootstrap.

In general, however, this capability derives from the ability of any defined evolve likelihood function to simulate an alignment. This property is provided as simulate_alignment method on likelihood function objects.

Determining confidence intervals on MLEs

The profile method is used to calculate a confidence interval for a named parameter. We show it here for a global substitution model exchangeability parameter (kappa, the ratio of transition to transversion rates) and for an edge specific parameter (just the human branch length).

Saving results

The best approach is to use the json string from the to_json() method. The saved data can be later reloaded using cogent3.util.deserialise.deserialise_object(). The json data contains the alignment, tree topology, substitution model, parameter values, etc..

To illustrate this, I create a very simple likelihood function. The json variable below is just a string that can be saved to disk.

We deserialise the object from the string.

Reconstructing ancestral sequences

We first fit a likelihood function.

We then get the most likely ancestral sequences.

Or we can get the posterior probabilities (returned as a DictArray) of sequence states at each node.


There’s nothing that improves performance quite like being close to the maximum likelihood values. So using the set_param_rule method to provide good starting values can be very useful. As this can be difficult to do one easy way is to build simpler models that are nested within the one you’re interested in. Fitting those models and then relaxing constraints until you’re at the parameterisation of interest can markedly improve optimisation speed.