Instance Methods
- class pydtmc.MarkovChain[source]
- absorption_probabilities()[source]
The method computes the absorption probabilities of the Markov chain.
Notes:If the Markov chain has no transient states, then
None
is returned.
- aggregate(s, method='adaptive')[source]
The method attempts to reduce the state space of the Markov chain to the given number of states through a Kullback-Leibler divergence minimization approach.
Notes:The spectral theory based aggregation is described in Optimal Kullback-Leibler Aggregation via Spectral Theory of Markov Chains (Deng et al., 2011).
- Parameters:
s (
int
) – the number of states of the reduced Markov chain.method (
str
)spectral-bottom-up for a spectral theory based aggregation, bottom-up (more suitable for reducing a large number of states).
spectral-top-down for a spectral theory based aggregation, top-down (more suitable for reducing a small number of states).
adaptive for automatically selecting the best aggregation method.
- Raises:
ValidationError
– if any input argument is not compliant.ValueError
– if the Markov chain defines only two states or is not ergodic.
- Return Type:
- are_communicating(state1, state2)[source]
The method verifies whether the given states of the Markov chain are communicating.
- closest_reversible(initial_distribution=None, weighted=False)[source]
The method computes the closest reversible of the Markov chain.
Notes:The algorithm is described in Computing the Nearest Reversible Markov chain (Nielsen & Weber, 2015).
- Parameters:
- Raises:
ValidationError
– if any input argument is not compliant.ValueError
– if the closest reversible could not be computed.
- Return Type:
- committor_probabilities(committor_type, states1, states2)[source]
The method computes the committor probabilities between the given subsets of the state space defined by the Markov chain.
Notes:If the Markov chain is not ergodic, then
None
is returned.The method can be accessed through the following aliases: crp.
- Parameters:
- Raises:
ValidationError
– if any input argument is not compliant.- Return Type:
- conditional_probabilities(state)[source]
The method computes the probabilities, for all the states of the Markov chain, conditioned on the process being at the given state.
Notes:The method can be accessed through the following aliases: conditional_distribution, cd, cp.
- Parameters:
- Raises:
ValidationError
– if any input argument is not compliant.- Return Type:
- expected_rewards(steps, rewards)[source]
The method computes the expected rewards of the Markov chain after N steps, given the reward value of each state.
Notes:The method can be accessed through the following aliases: er.
- expected_transitions(steps, initial_distribution=None)[source]
The method computes the expected number of transitions performed by the Markov chain after N steps, given the initial distribution of the states.
Notes:The method can be accessed through the following aliases: et.
- first_passage_probabilities(steps, initial_state, first_passage_states=None)[source]
The method computes the first passage probabilities of the Markov chain after N steps, given the initial state and, optionally, the first passage states.
Notes:The method can be accessed through the following aliases: fpp.
- first_passage_reward(steps, initial_state, first_passage_states, rewards)[source]
The method computes the first passage reward of the Markov chain after N steps, given the reward value of each state, the initial state and the first passage states.
Notes:The method can be accessed through the following aliases: fpr.
- Parameters:
- Raises:
ValidationError
– if any input argument is not compliant.ValueError
– if the Markov chain defines only two states.
- Return Type:
- hitting_probabilities(targets=None)[source]
The method computes the hitting probability, for the states of the Markov chain, to the given set of states.
Notes:The method can be accessed through the following aliases: hp.
- hitting_times(targets=None)[source]
The method computes the hitting times, for all the states of the Markov chain, to the given set of states.
Notes:The method can be accessed through the following aliases: ht.
- is_absorbing_state(state)[source]
The method verifies whether the given state of the Markov chain is absorbing.
- Parameters:
- Raises:
ValidationError
– if any input argument is not compliant.- Return Type:
- is_accessible(state_target, state_origin)[source]
The method verifies whether the given target state is reachable from the given origin state.
- is_cyclic_state(state)[source]
The method verifies whether the given state is cyclic.
- Parameters:
- Raises:
ValidationError
– if any input argument is not compliant.- Return Type:
- is_recurrent_state(state)[source]
The method verifies whether the given state is recurrent.
- Parameters:
- Raises:
ValidationError
– if any input argument is not compliant.- Return Type:
- is_transient_state(state)[source]
The method verifies whether the given state is transient.
- Parameters:
- Raises:
ValidationError
– if any input argument is not compliant.- Return Type:
- lump(partitions)[source]
The method attempts to reduce the state space of the Markov chain with respect to the given partitions following the ordinary lumpability criterion.
- Parameters:
partitions (
Union
[List
[List
[int
]],List
[List
[str
]]]) – the partitions of the state space.- Raises:
ValidationError
– if any input argument is not compliant.ValueError
– if the Markov chain defines only two states or is not lumpable with respect to the given partitions.
- Return Type:
- mean_absorption_times()[source]
The method computes the mean absorption times of the Markov chain.
Notes:If the Markov chain is not absorbing or has no transient states, then
None
is returned.The method can be accessed through the following aliases: mat.
- mean_first_passage_times_between(origins, targets)[source]
The method computes the mean first passage times between the given subsets of the state space.
Notes:If the Markov chain is not ergodic, then
None
is returned.The method can be accessed through the following aliases: mfpt_between, mfptb.
- mean_first_passage_times_to(targets=None)[source]
The method computes the mean first passage times, for all the states, to the given set of states.
Notes:If the Markov chain is not ergodic, then
None
is returned.The method can be accessed through the following aliases: mfpt_to, mfptt.
- mean_number_visits()[source]
The method computes the mean number of visits of the Markov chain.
Notes:The method can be accessed through the following aliases: mnv.
- mean_recurrence_times()[source]
The method computes the mean recurrence times of the Markov chain.
Notes:If the Markov chain is not ergodic, then
None
is returned.The method can be accessed through the following aliases: mrt.
- merge_with(other, gamma)[source]
The method returns a Markov chain whose transition matrix is defined below.
\(p_{new} = (1 - \gamma) p_{current} + \gamma p_{other}\)- Parameters:
other (
MarkovChain
) – the other Markov chain to be merged.gamma (
float
) – the merger blending factor.
- Raises:
ValidationError
– if any input argument is not compliant.- Return Type:
- mixing_time(initial_distribution=None, jump=1, cutoff_type='natural')[source]
The method computes the mixing time of the Markov chain, given the initial distribution of the states.
Notes:If the Markov chain is not ergodic, then
None
is returned.The method can be accessed through the following aliases: mt.
- Parameters:
initial_distribution (
Optional
[Union
[ndarray
,spmatrix
]]) – the initial distribution of the states (if omitted, the states are assumed to be uniformly distributed).jump (
int
) – the number of steps in each iteration.cutoff_type (
str
)natural for the natural cutoff;
traditional for the traditional cutoff.
- Raises:
ValidationError
– if any input argument is not compliant.- Return Type:
- next(initial_state, output_index=False, seed=None)[source]
The method simulates a single step in a random walk.
- Parameters:
- Raises:
ValidationError
– if any input argument is not compliant.- Return Type:
- predict(steps, initial_state, output_indices=False)[source]
The method computes the most probable sequence of states produced by a random walk of N steps, given the initial state.
Notes:In presence of probability ties
None
is returned.
- redistribute(steps, initial_status=None, output_last=True)[source]
The method performs a redistribution of states of N steps.
- Parameters:
steps (
int
) – the number of steps.initial_status (
Optional
[Union
[int
,str
,ndarray
,spmatrix
]]) – the initial state or the initial distribution of the states (if omitted, the states are assumed to be uniformly distributed).output_last (
bool
) – a boolean indicating whether to output only the last distributions.
- Raises:
ValidationError
– if any input argument is not compliant.- Return Type:
- sensitivity(state)[source]
The method computes the sensitivity matrix of the stationary distribution with respect to a given state.
Notes:If the Markov chain is not irreducible, then
None
is returned.
- sequence_probability(sequence)[source]
The method computes the probability of a given sequence of states.
- simulate(steps, initial_state=None, final_state=None, output_indices=False, seed=None)[source]
The method simulates a random walk of the given number of steps.
- Parameters:
steps (
int
) – the number of steps.initial_state (
Optional
[Union
[int
,str
]]) – the initial state (if omitted, it is chosen uniformly at random).final_state (
Optional
[Union
[int
,str
]]) – the final state of the walk (if specified, the simulation stops as soon as it is reached even if not all the steps have been performed).output_indices (
bool
) – a boolean indicating whether to output the state indices.seed (
Optional
[int
]) – a seed to be used as RNG initializer for reproducibility purposes.
- Raises:
ValidationError
– if any input argument is not compliant.- Return Type:
- time_correlations(sequence1, sequence2=None, time_points=1)[source]
The method computes the time autocorrelations of a single observed sequence of states or the time cross-correlations of two observed sequences of states.
Notes:If the Markov chain has multiple stationary distributions, then
None
is returned.If a single time point is provided, then a
float
is returned.The method can be accessed through the following aliases: tc.
- Parameters:
- Raises:
ValidationError
– if any input argument is not compliant.- Return Type:
- time_relaxations(sequence, initial_distribution=None, time_points=1)[source]
The method computes the time relaxations of an observed sequence of states with respect to the given initial distribution of the states.
Notes:If the Markov chain has multiple stationary distributions, then
None
is returned.If a single time point is provided, then a
float
is returned.The method can be accessed through the following aliases: tr.
- Parameters:
sequence (
Union
[List
[int
],List
[str
]]) – the observed sequence of states.initial_distribution (
Optional
[Union
[ndarray
,spmatrix
]]) – the initial distribution of the states (if omitted, the states are assumed to be uniformly distributed).time_points (
Union
[int
,List
[int
]]) – the time point or a list of time points at which the computation is performed.
- Raises:
ValidationError
– if any input argument is not compliant.- Return Type:
- to_bounded_chain(boundary_condition)[source]
The method returns a bounded Markov chain by adjusting the transition matrix of the original process using the specified boundary condition.
Notes:The method can be accessed through the following aliases: to_bounded.
- Parameters:
boundary_condition (
Union
[float
,int
,str
])a number representing the first probability of the semi-reflecting condition;
a string representing the boundary condition type (either absorbing or reflecting).
- Raises:
ValidationError
– if any input argument is not compliant.- Return Type:
- to_canonical_form()[source]
The method returns the canonical form of the Markov chain.
Notes:The method can be accessed through the following aliases: to_canonical.
- Return Type:
- to_dictionary()[source]
The method returns a dictionary representing the Markov chain.
- to_file(file_path)[source]
The method writes a Markov chain to the given file.
Only csv, json, txt and xml files are supported; data format is inferred from the file extension.- Parameters:
file_path (
Union
[str
,Path
]) – the location of the file in which the Markov chain must be written.- Raises:
OSError
– if the file cannot be written.ValidationError
– if any input argument is not compliant.
- to_graph()[source]
The method returns a directed graph representing the Markov chain.
- Return Type:
- to_lazy_chain(inertial_weights=0.5)[source]
The method returns a lazy Markov chain by adjusting the state inertia of the original process.
Notes:The method can be accessed through the following aliases: to_lazy.
- to_matrix()[source]
The method returns the transition matrix of the Markov chain.
- Return Type:
- to_subchain(states)[source]
The method returns a subchain containing all the given states plus all the states reachable from them.
Notes:The method can be accessed through the following aliases: to_sub.
- Parameters:
states (
Union
[int
,str
,List
[int
],List
[str
]]) – the states to include in the subchain.- Raises:
ValidationError
– if any input argument is not compliant.ValueError
– if the subchain is not a valid Markov chain.
- Return Type:
- transition_probability(state_target, state_origin)[source]
The method computes the probability of a given state, conditioned on the process being at a given state.