BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Probability and statistics22 of 24 in topic

801. Find the stationary distribution of a Markov chain

MediumProbability and statistics

Why it matters PageRank, MCMC and LLM token-level Markov approximations all use the long-run distribution of a chain.

Run a Markov chain long enough and the share of time spent in each state settles to a fixed vector pi with pi @ P = pi. PageRank, MCMC samplers and queue models all rely on this.

Write stationary(P, tol=1e-12, max_iter=10000) where P is a (n, n) row-stochastic matrix (every row sums to 1). Start from the uniform vector and repeat pi = pi @ P until the L1 change is below tol or max_iter steps pass. Return pi as a float array that sums to 1.

Assume the chain is irreducible and aperiodic, so the answer is unique.

Inputs can be Python lists or NumPy arrays, so convert them with np.asarray first.

Functiondef stationary(P, tol=1e-12, max_iter=10000)

Example 1

Input: stationary([[0.9, 0.1], [0.5, 0.5]])

Output: [0.8333, 0.1667]

Example 2

Input: P = [[0.5, 0.5], [0.5, 0.5]]

Output: [0.5, 0.5]

Example 3

Input: P = [[0.1, 0.9, 0.0], [0.5, 0.0, 0.5], [0.0, 0.8, 0.2]]

Output: [0.254777, 0.458599, 0.286624]

Constraints

  • Python 3 with NumPy. Code runs in your browser.
  • Your code must pass all 4 tests to be accepted.
Hint 1

A distribution is a row vector; one chain step is pi @ P.

Hint 2

Start uniform and repeat the step until the L1 change is tiny.

Hint 3

Renormalise at the end so rounding never leaves a sum slightly off 1.

Common mistakes
  • Using P @ pi, which treats P as column-stochastic.
  • Stopping after a fixed small number of steps on slowly mixing chains.
Understand the idea

Multiplying a distribution by P moves it one step forward. If the chain mixes, repeated steps forget the starting point and converge to pi with pi @ P = pi, the left eigenvector of P with eigenvalue 1.

Worked example

P = [[0.9, 0.1], [0.5, 0.5]]. Balance says 0.1 pi_0 = 0.5 pi_1, so pi_0 = 5 pi_1 and pi = [5/6, 1/6].

iterate pi = pi @ P until it stops changing
Topics
Probability and statistics
Similar questions
  • Decide if a new prompt really converts better with a two-proportion z-testMedium
  • Update your belief about a model's pass rate with a Beta priorMedium
  • Put a bootstrap confidence interval on an evaluation scoreMedium
  • Compare two eval runs with Welch's t-test statisticMedium
All Probability and statistics problems
Report
801 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import numpy as np
​
def stationary(P, tol=1e-12, max_iter=10000):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks all 4 tests.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS