redflag.markov moduleÂ¶
Functions related to Markov chains. This code was originally implemented in https://github.com/agilescientific/striplog.
- class redflag.markov.Markov_chain(observed_counts, states=None, step=1, include_self=None)Â¶
Bases:
object
- chi_squared(q: float = 0.95) tuple Â¶
The chi-squared statistic for the given transition frequencies.
Also returns the critical statistic at the given confidence level q (default 95%).
If the first number is bigger than the second number, then you can reject the hypothesis that the sequence is randomly ordered.
- Parameters:
q (float) â€“ The confidence level, as a float in the range 0 to 1. Default: 0.95.
- Returns:
The chi-squared statistic.
- Return type:
float
- property degrees_of_freedom: intÂ¶
- property expected_freqsÂ¶
The expected frequencies of each state, given the previous state.
- classmethod from_sequence(sequence, states=None, strings_are_states=False, include_self=False, step=1)Â¶
Parse a sequence and make the transition matrix of the specified order.
You must provide sequence(s) in causal order (e.g. time order).
- Parameters:
sequence (list-like) â€“ A list-like, or list-like of list-likes. The inner list-likes represent sequences of states. For example, can be a string or list of strings, or a list or list of lists.
states (list-like) â€“ A list or array of the names of the states. If not provided, it will be inferred from the data.
strings_are_states (bool) â€“
True if the strings are themselves states (i.e. words or tokens) and not sequences of one-character states. For example, set to True if you provide something like:
[â€˜sstâ€™, â€˜mudâ€™, â€˜mudâ€™, â€˜sstâ€™, â€˜lstâ€™, â€˜lstâ€™]
include_self (bool) â€“ Whether to include self-to-self transitions (default is False: do not include them).
step (integer) â€“ The distance to step. Default is 1: use the previous state only. If 2, then the previous-but- one state is used as well as the previous state (and the matrix has one more dimension).
- generate_states(n: int = 10, current_state=None)Â¶
Generates the next states of the system.
- Parameters:
n (int) â€“ The number of future states to generate.
current_state (str) â€“ The state of the current random variable.
- Returns:
list. The next n states.
- property normalized_differenceÂ¶
The normalized difference between observed and expected counts.
- property observed_freqsÂ¶
The observed frequencies of each state, given the previous state.
- redflag.markov.hollow_matrix(M)Â¶
Utility funtion to return hollow matrix (zeros on diagonal).
- Args
M (ndarray): a â€˜squareâ€™ ndarray.
- Returns
ndarray. The same array with zeros on the diagonal.
- redflag.markov.observations(seq_of_seqs, states, step=1, include_self=False)Â¶
Compute observation matrix.
Returns the matrix of transition counts between states.
- Parameters:
seq_of_seqs (list-like) â€“ A list-like, or list-like of list-likes. The inner list-likes represent sequences of states. For example, can be a string or list of strings, or a list or list of lists.
states (list-like) â€“ A list or array of the names of the states. If not provided, it will be inferred from the data.
step (integer) â€“ The distance to step. Default is 1: use the previous state only. If 2, then the previous-but- one state is used as well as the previous state (and the matrix has one more dimension).
include_self (bool) â€“ Whether to include self-to-self transitions (default is False: do not include them).
- Returns:
ndarray. The observation matrix.
- redflag.markov.regularize(sequence, strings_are_states=False) tuple Â¶
Turn a sequence or sequence of sequences into a tuple of the unique elements in the sequence(s), plus a sequence of sequences (sort of equivalent to np.atleast_2d()).
- Args
- sequence (list-like): A list-like container of either
states, or of list-likes of states.
- strings_are_states (bool): True if the strings are
themselves states (i.e. words or tokens) and not sequences of one-character states. For example, set to True if you provide something like:
[â€˜sstâ€™, â€˜mudâ€™, â€˜mudâ€™, â€˜sstâ€™, â€˜lstâ€™, â€˜lstâ€™]
- Returns
- tuple. A tuple of the unique states, and a sequence
of sequences.