mchmm.MarkovChain

class mchmm.MarkovChain(states: list | ndarray | None = None, obs: list | ndarray | None = None, obs_p: list | ndarray | None = None)

Bases: object

__init__(states: list | ndarray | None = None, obs: list | ndarray | None = None, obs_p: list | ndarray | None = None)

Discrete Markov Chain.

Parameters:
  • states (Optional[Union[numpy.ndarray, list]) – State names list.

  • obs (Optional[Union[numpy.ndarray, list]) – Observed transition frequency matrix.

  • obs_p (Optional[Union[numpy.ndarray, list]) – Observed transition probability matrix.

_transition_matrix(seq: str | ndarray | list | None = None, states: str | ndarray | list | None = None) ndarray

Calculate a transition frequency matrix.

Parameters:
  • seq (Optional[Union[str, list, numpy.ndarray]]) – Observations sequence.

  • states (Optional[Union[str, list, numpy.ndarray]]) – List of states.

Returns:

matrix – Transition frequency matrix.

Return type:

numpy.ndarray

chisquare(obs: ndarray = None, exp: ndarray = None, **kwargs) Tuple[float | ndarray, float | ndarray]

Wrapper function for carrying out a chi-squared test using scipy.stats.chisquare method.

Parameters:
  • obs (numpy.ndarray) – Observed transition frequency matrix.

  • exp (numpy.ndarray) – Expected transition frequency matrix.

  • kwargs (optional) – Keyword arguments passed to scipy.stats.chisquare method.

Returns:

  • chisq (float or numpy.ndarray) – Chi-squared test statistic.

  • p (float or numpy.ndarray) – P value of the test.

from_data(seq: str | ndarray | list) object

Infer a Markov chain from data. States, frequency and probability matrices are automatically calculated and assigned to as class attributes.

Parameters:

seq (Union[str, np.ndarray, list]) – Sequence of events. A string or an array-like object exposing the array interface and containing strings or ints.

Returns:

MarkovChain – Trained MarkovChain class instance.

Return type:

object

graph_make(*args, **kwargs) Digraph

Make a directed graph of a Markov chain using graphviz.

Parameters:
  • args (optional) – Arguments passed to the underlying graphviz.Digraph method.

  • kwargs (optional) – Keyword arguments passed to the underlying graphviz.Digraph method.

Returns:

graph – Digraph object with its own methods.

Return type:

graphviz.dot.Digraph

Note

graphviz.dot.Digraph.render method should be used to output a file.

n_order_matrix(mat: ndarray = None, order: int = 2) ndarray

Create Nth order transition probability matrix.

Parameters:
  • mat (numpy.ndarray, optional) – Observed transition probability matrix.

  • order (int, optional) – Order of transition probability matrix to return. Default is 2.

Returns:

x – Nth order transition probability matrix.

Return type:

numpy.ndarray

prob_to_freq_matrix(mat: ndarray = None, row_totals: ndarray = None) ndarray

Calculate a transition frequency matrix given a transition probability matrix and row totals. This method is meant to be used to calculate a frequency matrix for a Nth order transition probability matrix.

Parameters:
  • mat (numpy.ndarray, optional) – Transition probability matrix.

  • row_totals (numpy.ndarray, optional) – Row totals of transition frequency matrix.

Returns:

x – Transition frequency matrix.

Return type:

numpy.ndarray

simulate(n: int, tf: ndarray = None, states: list | ndarray | None = None, start: str | int | None = None, ret: str = 'both', seed: list | ndarray | None = None) ndarray | Tuple[ndarray, ndarray]

Markov chain simulation based on scipy.stats.multinomial.

Parameters:
  • n (int) – Number of states to simulate.

  • tf (numpy.ndarray, optional) – Transition frequency matrix. If None, observed_matrix instance attribute is used.

  • states (Optional[Union[np.ndarray, list]]) – State names. If None, states instance attribute is used.

  • start (Optional[str, int]) – Event to begin with. If integer is passed, the state is chosen by index. If string is passed, the state is chosen by name. If random string is passed, a random state is taken. If left unspecified (None), an event with maximum probability is chosen.

  • ret (str, optional) – Return state indices if indices is passed. If states is passed, return state names. Return both if both is passed.

  • seed (Optional[Union[list, numpy.ndarray]]) – Random states used to draw random variates (of size n). Passed to scipy.stats.multinomial method.

Returns:

  • x (numpy.ndarray) – Sequence of state indices.

  • y (numpy.ndarray, optional) – Sequence of state names. Returned if return arg is set to ‘states’ or ‘both’.