striplog.markov module#

Markov chains for the striplog package.

exception striplog.markov.MarkovError#

Bases: Exception

class striplog.markov.Markov_chain(observed_counts, states=None, step=1, include_self=None)#

Bases: object

Markov_chain object.


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.


q (float) – The confidence level, as a float in the range 0 to 1. Default: 0.95.


The chi-squared statistic.

Return type


property degrees_of_freedom: int#
property expected_freqs#
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.

Provide sequence(s) ordered in upwards direction.

  • 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) –

    rue 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).

  • return_states (bool) – Whether to return the states.

generate_states(n=10, current_state=None)#

Generates the next states of the system.

  • n (int) – The number of future states to generate.

  • current_state (str) – The state of the current random variable.


list. The next n states.

property normalized_difference#
property observed_freqs#
plot_graph(ax=None, figsize=None, max_size=1000, directed=True, edge_labels=False, draw_neg=False, seed=None)#
plot_norm_diff(ax=None, cmap='RdBu', vminmax=None, rotation=0, annotate=False)#

A visualization of the normalized difference matrix.

  • ax (Axes) – The axes to plot on. If None, a new figure will be created.

  • cmap (str) – The name of a matplotlib colormap.

  • vminmax (tuple) – The minimum and maximum values to use for the colormap. If None, the min and max of the matrix will be used.

  • rotation (float) – The angle to rotate the labels.

  • annotate (bool) – Whether to annotate the matrix with the values.


The axes on which the plot was drawn.

Return type


striplog.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()).

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’]

tuple. A tuple of the unique states, and a sequence

of sequences.