import numpy as np
= np.array([
P 0.7, 0.2, 0.1],
[0.3, 0.4, 0.3],
[0.3, 0.2, 0.5]
[
]) P
array([[0.7, 0.2, 0.1],
[0.3, 0.4, 0.3],
[0.3, 0.2, 0.5]])
Understanding Markov Chains: Why They Matter in Data Science
stochastic, markov-chains, steady-state-probability, network-visualization
In the world of data science, systems often evolve over time in ways that depend only on their current state - not their entire history. This concept is at the heart of Markov chains, a powerful mathematical tool for modeling sequences of events where the next state depends only on the current one. Whether you’re analyzing user behavior on a website, modeling weather patterns, simulating queues, or building recommender systems, Markov chains offer an elegant and effective way to understand and predict dynamic systems.
At their core, Markov chains rely on transition matrices that define the probability of moving from one state to another. By analyzing these matrices, data scientists can compute steady-state probabilities - a long-term distribution of where the system is likely to be. Simulating these chains also allows us to observe patterns over time and visualize how the system behaves dynamically.
You are given the following 3×3 transition matrix representing the state changes of a system over time:
P = [
0.7 0.2 0.1
0.3 0.4 0.3
0.3 0.2 0.5
]
A stochastic matrix has non-negative entries and each row sums to 1.
# Check non-negativity
is_non_negative = np.all(P >= 0)
# Check if each row sums to 1
row_sums = np.sum(P, axis=1)
rows_sum_to_one = np.allclose(row_sums, 1)
is_valid_stochastic = is_non_negative and rows_sum_to_one
print("Valid stochastic matrix:", is_valid_stochastic)
Valid stochastic matrix: True
Solve for π
such that πP = π
and sum(π) = 1
# Solve for steady-state vector π
A = P.T - np.eye(3)
A = np.vstack((A, np.ones(3)))
b = np.array([0, 0, 0, 1])
steady_state = np.linalg.lstsq(A, b, rcond=None)[0]
print("Steady-state probabilities:", steady_state)
Steady-state probabilities: [0.5 0.25 0.25]
Verify πP = π
Using the same transition matrix P in question One, simulate a Markov chain for 50 time steps, starting from state 0.
def simulate_markov_chain(P, start_state, steps):
current_state = start_state
states = [current_state]
for _ in range(steps):
current_state = np.random.choice(P.shape[0], p=P[current_state])
states.append(int(current_state))
return states
simulated_states = simulate_markov_chain(P, start_state=0, steps=50)
Using the transition matrix P in question One again, create a directed graph of the Markov chain.
networkx
to create a directed graph where:
matplotlib
.networkx
pos = nx.circular_layout(G)
edge_weights = [
G[u][v]['weight'] * 5
for u, v
in G.edges()
]
plt.figure(figsize=(6, 6))
nx.draw(
G,
pos,
with_labels=True,
node_color='lightgreen',
node_size=1500,
edge_color='gray',
width=edge_weights,
arrowsize=20)
# Add edge labels
edge_labels = {
(u, v): f"{d['weight']:.2f}"
for u, v, d
in G.edges(data=True)
}
nx.draw_networkx_edge_labels(
G,
pos,
edge_labels=edge_labels)
plt.title("Markov Chain Transition Graph")
plt.show()
Disclaimer: For information only. Accuracy or completeness not guaranteed. Illegal use prohibited. Not professional advice or solicitation. Read more: /terms-of-service
@misc{kabui2025,
author = {{Kabui, Charles}},
title = {Stochastic: {Steady-State} {Probability,} {Markov} {Chains}
and {Network} {Visualization}},
date = {2025-04-14},
url = {https://toknow.ai/posts/computational-techniques-in-data-science/stochastic_steady-state-probability_markov-chains_network-visualization/index.html},
langid = {en-GB}
}