Mini Markov

Mini Markov is a Python class and subclass that can be used to perform Markov Chain Monte Carlo simulations. A tutorial is also available.

Dependencies

random
pandas
numpy
math
optional progressbar

API

Class mini_markov

This is the main class and has two exposed methods
from Markov import markov as mk
m = mk()
rset = m.run(self, df, trials=100, epochs=100, startstate=0, log=None, logtype='none', seed=-1, progress=None) :
rset is an object of the result_set subclass as defined below
df is properly formatted dataframe of the transition matrix.
trials is number of trials
epochs is the maximum number of epochs per trial
startstate is the starting state for each trial
log is the log file, None means no log file
logtype may be 'none' (redundant with log=None) or 'text' or 'csv'
seed is the random seed

transition_matrix = build_df(self, columns, transition_list)
Builds and returns a dataframe properly formatted for this class. columns is a list with the names of the states. The transition_list is an array of arrays of the transition probabilities from state to state.

class result_set(self, result_frame, transition_matrix)

result_frame is a DataFrame output from a mini_markov run
transition_matrix is a DataFrame created by the build_df function, a formatted DataFrame used by this API

result_set has the following methods defined def counts(self) -> tuple :
Creates and returns frequency data for states
Returns:
tuple: two DataFrames. val_counts is the number of events for each state
val_counts_per_trial is the mean number of events for each state on a per trial basis
def mean_time(self) -> float :
Mean time through the simulation
Returns:
float: mean time through the simulation
def results_range(self, valsarray = None) -> pd.DataFrame :
Calculates the min, max, mean, and standard deviation for each state
Also used for value() with optional valsarray (values array)
Returns:
DataFrame
Rows are the states. Columns are Min, Max, Mean, and SD
def value(self, valuearray) -> list :
Parameters
----------
valuearray : list
list of values for each state
Returns
-------
dataframe with stats from results_range
product of the valuearray list and the state count for each trial
(could be considered cost of each trial)

Source code
Tutorial