ivy.chars.mk_mr module

class ivy.chars.mk_mr.ModelOrderMetropolis(stochastic)[source]

Bases: pymc.StepMethods.Metropolis

Custom step method for model order

competence()[source]
propose()[source]
reject()[source]
class ivy.chars.mk_mr.SwitchpointMetropolis(stochastic, tree, seg_map, stepsize=0.05, seglen=0.02)[source]

Bases: pymc.StepMethods.Metropolis

Custom step algorithm for selecting a new switchpoint

competence()[source]
global_step()[source]
local_step()[source]
propose()[source]
reject()[source]
ivy.chars.mk_mr.create_likelihood_function_multimk(tree, chars, Qtype, nregime, pi=u'Equal', findmin=True)[source]
ivy.chars.mk_mr.create_likelihood_function_multimk_mods(tree, chars, mods, pi=u'Equal', findmin=True, orderedparams=True)[source]

Create a likelihood function for testing the parameter values of user- specified models

ivy.chars.mk_mr.create_mkmr_ar(tree, chars, nregime, findmin=True)[source]

Create preallocated arrays. For use in mk function

Nodelist = edgelist of nodes in postorder sequence

ivy.chars.mk_mr.create_mkmr_mb_ar(tree, chars, nregime, findmin=True)[source]

Preallocated arrays for midbranch mk_mr

Create preallocated arrays. For use in mk_mr midbranch function

Nodelist = edgelist of nodes in postorder sequence

ivy.chars.mk_mr.fill_model_mr_Q(Qparams, mods, Q)[source]

Fill a Q-matrix with Qparams corresponding to the model.

Individual Q matrices are filled rowwise

Function alters Q in place

ivy.chars.mk_mr.inds_from_switchpoint_cython(tree, switches_ni, ar, debug=False)[source]
ivy.chars.mk_mr.lf_mk_mr_midbranch(tree, chars, Qtype, nregime, pi=u'Equal', findmin=True)[source]
ivy.chars.mk_mr.lf_mk_mr_midbranch_mods(tree, chars, mods, pi=u'Equal', findmin=True, orderedparams=True)[source]

Create a likelihood function for testing the parameter values of user- specified models with switchpoints allowed in the middle of branches

ivy.chars.mk_mr.locs_from_switchpoint(tree, switches, locs=None)[source]

Given a tree and a list of nodes to be switchpoints, return an array of all node indices in one regime vs the other

Parameters:
  • tree (Node) – Root node of tree
  • switches (list) – List of internal nodes to act as switchpoints. Switchpoint nodes are part of their own regimes.
  • locs (np.array) – Optional pre-allocated array to store output.
Returns:

Array of indices of all nodes associated with each switchpoint, plus

all nodes “outside” the switches in the last list

Return type:

np.array

ivy.chars.mk_mr.make_modelorder_stoch(mods, name='modorder')[source]
ivy.chars.mk_mr.make_switchpoint_stoch(seg_map, name='switch')[source]
ivy.chars.mk_mr.mk_mr_midbranch(tree, chars, Qs, switchpoint, pi=u'Equal', returnPi=False, ar=None, debug=False)[source]

Calculate likelhiood of mk model with BAMM-like multiple regimes

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

  • Qs (np.array) – Array of instantaneous rate matrices
  • locs (np.array) – Array of the same length as Qs containing the node indices that correspond to each Q 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.
Returns:

Log-likelihood of tree/characters given the Q matrices.

Return type:

(float)

ivy.chars.mk_mr.mk_multi_bayes(tree, chars, nregime, qidx, pi=u'Equal', seglen=0.02, stepsize=0.05)[source]

Create a Bayesian multi-mk model. User specifies which regime models to use and the Bayesian model finds the switchpoints.

Parameters:
  • tree (Node) – Root node of tree.
  • chars (dict) – Dict mapping tip labels to discrete character states. Character states must be in the form of [0,1,2...]
  • regime (int) – The number of distinct regimes to test. Set to 1 for an Mk model, set to greater than 1 for a multi-regime Mk model.
  • qidx (np.array) –

    Index specifying the model to test

    columns:
    0, 1, 2 - index axes of q 3 - index of params

    This scheme allows flexible specification of models. E.g.: Symmetric mk2:

    params = [0.2]; qidx = [[0,0,1,0],
    [0,1,0,0]]
    Asymmetric mk2:
    params = [0.2,0.6]; qidx = [[0,0,1,0],
    [0,1,0,1]]

    Note

    The qidx corresponding to the first q matrix (first column 0) is always the root regime

  • 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.
  • seglen (float) – Size of segments to break tree into. The smaller this value, the more “fine-grained” the analysis will be. Optional, defaults to 2% of the root-to-tip length.
  • stepsize (float) – Maximum size of steps for switchpoints to take. Optional, defaults to 5% of root-to-tip length.
ivy.chars.mk_mr.random_tree_location(seg_map)[source]

Select random location on tree with uniform probability

Returns:
node and float, which represents the how far along the branch to
the parent node the switch occurs on
Return type:tuple
ivy.chars.mk_mr.round_step_size(step_size, seg_size)[source]

Round step_size to the nearest segment

ivy.chars.mk_mr.tree_map(tree, seglen=0.02)[source]

Make a map of the tree cut into segments of size (seglen*root_to_tip_length)