Markov Chain (original) (raw)

Last Updated : 31 Jul, 2025

A Markov chain is a way to describe a system that moves between different situations called "states", where the chain assumes the probability of being in a particular state at the next step depends solely on the current state. This key property of ignoring historical states is called the Markov Property or memorylessness.

Fundamental Properties

1. **Irreducibility: It is possible to reach any state from any other (maybe through several steps).

2. **Periodicity: A state is periodic if returns are only possible at multiples of some step (>1).

  1. Transience and Recurrence:
  1. Absorbing States: Once entered, the process cannot leave i.e. no outgoing transitions.

Visualizing a Markov Chain

Consider a Markov chain with two possible states, A and E. The process can either stay in the same state or transition to the other, each with a particular probability.

**Example: Two-State Markov Process

If in state A:

If in state E:

mc

State Transition Diagram

A Markov chain can be illustrated as a directed graph, where:

Transition Matrix

A Markov chain can be represented by a transition matrix:

A E
A 0.6 0.4
E 0.7 0.3

Each row corresponds to the current state and each column to the future state. The value in each cell is the probability of moving from the row’s state to the column’s state. Each row sums to 1.

N-Step Transition Matrices

To find probabilities of transitions over multiple steps, raise the transition matrix to the power of N :

P_{\text{n steps}} = \left(P_{\text{1 step}}\right)^n

The entry at position (A, E) in the resulting matrix gives the chance of moving from A to E in N steps.

Types of Markov Chains

**1. Discrete-Time Markov Chains (DTMC):

**2. Continuous-Time Markov Chains (CTMC): State changes can occur at any instant (time is continuous).

Step-by-step Implementation of Markov Chain

Step 1: Import Required Libraries.

import numpy as np import scipy.linalg

`

Step 2: Define States and Transition Matrix

**transition_matrix**is a 2x2 array where each row gives the probabilities of moving from one state to another.

states = ["A", "E"] transition_matrix = np.array([[0.6, 0.4], [0.7, 0.3]])

`

Step 3: Simulate a Random Walk on the Markov Chain

n_steps = 20 current_state = 0

print(states[current_state], end=" -> ") for _ in range(n_steps - 1): current_state = np.random.choice( [0, 1], p=transition_matrix[current_state]) print(states[current_state], end=" -> ") print("stop")

`

Step 4: Find the Stationary Distribution

eigvals, left_eigvecs = scipy.linalg.eig( transition_matrix, left=True, right=False) stationary = left_eigvecs[:, 0].real stationary /= stationary.sum() print("Stationary distribution:", stationary)

`

Applications of Markov Chain

Advantages

Limitations