🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
Search courses…
💼 Servicesℹ️ About✉️ ContactView Pricing Plansfrom $10

Markov Chains

ProbabilityStochastic Processes🟢 Free Lesson

Advertisement

Why It Matters


Markov Property


Transition Matrix


State Distribution

The state distribution at time is obtained by multiplying the initial distribution by . If we start in a specific state , then (the -th standard basis vector), and is the -th row of .


n-Step Transitions

This is equivalent to matrix multiplication: .


Stationary Distribution


Ergodicity


Absorbing States

For an absorbing chain with transient states and absorbing states , the fundamental matrix is:

where is the submatrix of transitions among transient states. The entry of gives the expected number of times the chain visits transient state starting from transient state before absorption.


Python Implementation


Applications in AI/ML

PageRank

def pagerank(adjacency: np.ndarray, d: float = 0.85, max_iter: int = 100) -> np.ndarray:
    """Compute PageRank via power iteration."""
    n = adjacency.shape[0]
    col_sums = adjacency.sum(axis=0)
    col_sums[col_sums == 0] = 1  # Handle dangling nodes
    M = adjacency / col_sums
    r = np.ones(n) / n
    for _ in range(max_iter):
        r_new = (1 - d) / n + d * M @ r
        if np.allclose(r, r_new, atol=1e-8):
            break
        r = r_new
    return r

# Example: 4-page web
links = np.array([[0, 1, 0, 1],
                  [1, 0, 1, 0],
                  [0, 1, 0, 1],
                  [1, 0, 1, 0]])
print("PageRank:", pagerank(links))

Markov Chain Monte Carlo (MCMC)

def metropolis_hastings(target_log_prob, proposal, n_samples, x0):
    """Generic Metropolis-Hastings sampler."""
    samples = [x0]
    x = x0
    for _ in range(n_samples):
        x_prop = proposal(x)
        log_alpha = target_log_prob(x_prop) - target_log_prob(x)
        if np.log(np.random.uniform()) < log_alpha:
            x = x_prop
        samples.append(x)
    return np.array(samples)

# Example: Sample from N(0,1)
target = lambda x: -0.5 * x**2
proposal = lambda x: x + np.random.normal(0, 0.5)
samples = metropolis_hastings(target, proposal, 10000, 0.0)
print(f"Mean: {samples.mean():.4f}, Std: {samples.std():.4f}")

Hidden Markov Models (HMMs)


Common Mistakes

MistakeWhy It's WrongCorrect Approach
Assuming rows sum to 1 for column-stochastic matricesPageRank uses column-stochastic matrices; rows may not sum to 1Check the convention; normalize columns for PageRank
Confusing left and right eigenvectorsStationary distribution is a left eigenvector: Solve , not
Forgetting the normalization constraintThe stationary distribution must satisfy Always normalize the eigenvector to sum to 1
Assuming all chains have a stationary distributionPeriodic or reducible chains may not convergeVerify ergodicity (irreducible + aperiodic) first
Ignoring dangling nodes in PageRankPages with no outgoing links break the Markov chainAdd teleportation or connect dangling nodes to all pages
Using power iteration without convergence checkMay run too many or too few iterationsCheck
Confusing transient with absorbing statesNot all states with are absorbingAn absorbing state requires exactly

Interview Questions


Practice Problems


Quick Reference


Cross-References

Need Expert Mathematics Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement