pub struct MarkovChain {
pub states: usize,
pub transition: Vec<Vec<f64>>,
}Expand description
Discrete-time Markov chain with a finite state space.
Fields§
§states: usizeNumber of states.
transition: Vec<Vec<f64>>Row-stochastic transition matrix: transition[i][j] = P(X_{n+1}=j | X_n=i).
Implementations§
Source§impl MarkovChain
impl MarkovChain
Sourcepub fn new(transition: Vec<Vec<f64>>) -> Self
pub fn new(transition: Vec<Vec<f64>>) -> Self
Create from a transition matrix. Rows must sum to 1.
Sourcepub fn is_stochastic(&self) -> bool
pub fn is_stochastic(&self) -> bool
Validate that this is a proper stochastic matrix.
Sourcepub fn step(&self, rng: &mut Rng, current_state: usize) -> usize
pub fn step(&self, rng: &mut Rng, current_state: usize) -> usize
Single step: sample next state from current.
Sourcepub fn simulate(
&self,
rng: &mut Rng,
initial: usize,
steps: usize,
) -> Vec<usize>
pub fn simulate( &self, rng: &mut Rng, initial: usize, steps: usize, ) -> Vec<usize>
Simulate a trajectory of steps states starting from initial.
Sourcepub fn stationary_distribution(&self) -> Vec<f64>
pub fn stationary_distribution(&self) -> Vec<f64>
Compute stationary distribution via power iteration. Finds pi such that pi * P = pi.
Sourcepub fn is_irreducible(&self) -> bool
pub fn is_irreducible(&self) -> bool
Check if the chain is irreducible (all states reachable from all states).
Sourcepub fn is_ergodic(&self) -> bool
pub fn is_ergodic(&self) -> bool
Check if the chain is ergodic (irreducible and aperiodic). Aperiodicity is checked by verifying gcd of return times = 1, which we approximate by checking if P^n has all positive entries for some n.
Sourcepub fn absorbing_states(&self) -> Vec<usize>
pub fn absorbing_states(&self) -> Vec<usize>
Find absorbing states (states i where P(i,i) = 1).
Sourcepub fn mean_first_passage(&self, from: usize, to: usize) -> f64
pub fn mean_first_passage(&self, from: usize, to: usize) -> f64
Mean first passage time from state from to state to.
Computed by solving the system: m_i = 1 + sum_{j != to} P(i,j) * m_j
using iterative method.
Auto Trait Implementations§
impl Freeze for MarkovChain
impl RefUnwindSafe for MarkovChain
impl Send for MarkovChain
impl Sync for MarkovChain
impl Unpin for MarkovChain
impl UnsafeUnpin for MarkovChain
impl UnwindSafe for MarkovChain
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.