ivy.chars.mk module

ivy.chars.mk.create_likelihood_function_mk(tree, chars, Qtype, pi=u'Equal', findmin=True)[source]

Create a function that takes values for Q and returns likelihood.

Specify the Q to be ER, Sym, or ARD

Returned function to be passed into scipy.optimize

Parameters:
  • tree (Node) – Root node of a tree. All branch lengths must be greater than 0 (except root)
  • chars (dict) –

    Dict mapping character states to tip labels. Character states should be coded 0,1,2...

    Can also be a list with tip states in preorder sequence

  • Qtype (str) – What type of Q matrix to use. Either ER (equal rates), Sym (symmetric rates), or ARD (All rates different).
  • pi (str) – Either “Equal”, “Equilibrium”, or “Fitzjohn”. How to weight values at root node.
  • min (bool) – Whether the function is to be minimized (False means it will be maximized)
Returns:

Function accepting a list of parameters and returning

log-likelihood. To be optmimized with NLOPT

Return type:

function

ivy.chars.mk.create_mk_ar(tree, chars, findmin=True)[source]

Create preallocated arrays. For use in mk function

Nodelist = edgelist of nodes in postorder sequence

ivy.chars.mk.fitMkARD(tree, chars, pi=u'Equal')[source]

Estimate parameters of an all-rates-different Q matrix Return log-likelihood of mk equation using fitted Q

Multi-parameter model: all rates different

Parameters:
  • tree (Node) – Root node of a tree. All branch lengths must be greater than 0 (except root)
  • chars (dict) –

    Dict mapping character states to tip labels. Character states should be coded 0,1,2...

    Can also be a list with tip states in preorder sequence

  • pi (str) – Either “Equal” or “Fitzjohn”. How to weight values at root node. Defaults to “Equal” Method “Fitzjohn” is currently untested
Returns:

Fitted parameter, log-likelihood, and dictionary of weightings

at the root.

Return type:

tuple

ivy.chars.mk.fitMkER(tree, chars, pi=u'Equal')[source]

Estimate parameter of an equal-rate Q matrix Return log-likelihood of mk equation using fitted Q

One-parameter model: alpha = beta

Parameters:
  • tree (Node) – Root node of a tree. All branch lengths must be greater than 0 (except root)
  • chars (dict) –

    Dict mapping character states to tip labels. Character states should be coded 0,1,2...

    Can also be a list with tip states in preorder sequence

  • pi (str) – Either “Equal” or “Fitzjohn”. How to weight values at root node. Defaults to “Equal”
Returns:

Fitted parameter, log-likelihood, and dictionary of weightings

at the root.

Return type:

tuple

ivy.chars.mk.fitMkSym(tree, chars, pi=u'Equal')[source]

Estimate parameter of a symmetrical-rate Q matrix Return log-likelihood of mk equation using fitted Q

Multi-parameter model: forward = reverse

Parameters:
  • tree (Node) – Root node of a tree. All branch lengths must be greater than 0 (except root)
  • chars (dict) –

    Dict mapping character states to tip labels. Character states should be coded 0,1,2...

    Can also be a list with tip states in preorder sequence

  • pi (str) – Either “Equal” or “Fitzjohn”. How to weight values at root node. Defaults to “Equal” Method “Fitzjohn” is currently untested
Returns:

Fitted parameter, log-likelihood, and dictionary of weightings

at the root.

Return type:

tuple

ivy.chars.mk.fit_Mk(tree, chars, Q=u'Equal', pi=u'Equal')[source]

Fit an mk model to a given tree and list of characters. Return fitted Q matrix and calculated likelihood.

Parameters:
  • tree (Node) – Root node of a tree. All branch lengths must be greater than 0 (except root)
  • chars (dict) –

    Dict mapping character states to tip labels. Character states should be coded 0,1,2...

    Can also be a list with tip states in preorder sequence

  • pi – Either “Equal”, “Equilibrium”, or “Fitzjohn”. How to weight values at root node. Defaults to “Equal” Method “Fitzjohn” is not thouroughly tested, use with caution
Returns:

Log-likelihood, fitted Q matrix, root prior, root likelihood

Return type:

dict

Examples

from ivy.examples import primates, primate_data primate_eq = fit_Mk(primates,primate_data,Q=”Equal”) primate_ARD = fit_Mk(primates, primate_data,Q=”ARD”)

ivy.chars.mk.mk(tree, chars, Q, p=None, pi=u'Equal', returnPi=False, ar=None)[source]

Fit mk model and return likelihood for the root node of a tree given a list of characters and a Q matrix

Convert tree and character data into a form that can be input into mk, which fits an mk model.

Note: internal calculations are log-transformed to avoid numeric underflow

Parameters:
  • tree (Node) – Root node of a tree. All branch lengths must be greater than 0 (except root)
  • chars (dict) –

    Dict mapping character states to tip labels. Character states should be coded 0,1,2...

    Can also be a list with tip states in preorder sequence

  • Q (np.array) – Instantaneous rate matrix
  • p (np.array) – Optional pre-allocated p matrix
  • pi (str or np.array) –

    Option to weight the root node by given values. Either a string containing the method or an array of weights. Weights should be given in order.

    Accepted methods of weighting root:

    Equal: flat prior Equilibrium: Prior equal to stationary distribution

    of Q matrix
    Fitzjohn: Root states weighted by how well they
    explain the data at the tips.
  • returnPi (bool) – Whether or not to return the final values of root node weighting
  • ar (dict) – Dict of pre-allocated arrays to improve speed by avoiding creating and destroying new arrays
Returns:

log-likelihood of model

Return type:

float

Examples

from ivy.examples import primates, primate_data Q = np.array([[-0.1,0.1],[0.1,-0.1]]) mk(primates,primate_data,Q)

ivy.chars.mk.qsd(Q)[source]

Calculate stationary distribution of Q, assuming each state has the same diversification rate.

Parameters:Q (np.array) – Instantaneous rate matrix
Returns:Stationary distribution of pi
Return type:(np.array)

Eqn from Maddison et al 2007

Referenced from Phytools (Revell 2013)