Fundamentals 7 min read

Unlocking Markov Chains: From Weather Forecasts to Keyboard Predictions

This article introduces Markov chains as a mathematical model of state transitions, explains definitions, transition matrices, n‑step and steady‑state distributions, and demonstrates practical Python simulations for weather forecasting and simple keyboard word prediction.

Model Perspective
Model Perspective
Model Perspective
Unlocking Markov Chains: From Weather Forecasts to Keyboard Predictions

Markov chains are a mathematical model that describes how a system moves between states, where the next state depends only on the current state and not on earlier history.

Definition

Consider a discrete‑time, discrete‑state stochastic process with time index \(n\) (a non‑negative integer) and state \(X_n\). If the probability of moving to a new state depends solely on the present state, the process is a Markov chain.

Transition Probability

The process can be characterized by a transition probability matrix \(P\), where each element \(p_{ij}\) gives the probability of moving from state \(i\) to state \(j\). Each row of \(P\) sums to 1.

n‑step Transition Probability

The probability of moving from state \(i\) to state \(j\) in \(n\) steps is obtained by raising the matrix \(P\) to the \(n\)‑th power: \(P^{(n)} = P^n\).

Steady‑State Distribution

If a limiting distribution \(\pi\) exists as \(n\) approaches infinity, it satisfies \(\pi P = \pi\) and the components of \(\pi\) sum to 1. This distribution describes the long‑run behavior of the chain.

Example: Weather Model

Assume two weather states, Sunny (S) and Rainy (R), with the following transition probabilities:

From Sunny: stay Sunny with probability 0.9, change to Rainy with probability 0.1.

From Rainy: stay Rainy with probability 0.5, change to Sunny with probability 0.5.

The corresponding transition matrix is:

<code>[[0.9, 0.1],
[0.5, 0.5]]</code>

Python code simulation

We can simulate the weather Markov chain using Python:

<code>import numpy as np

def predict_weather(start_state, days):
    # Transition probability matrix
    P = np.array([[0.9, 0.1],
                  [0.5, 0.5]])
    # State mapping
    states = ["Sunny", "Rainy"]
    current_state_idx = states.index(start_state)
    forecast = [start_state]
    for _ in range(days - 1):
        next_state_idx = np.random.choice([0, 1], p=P[current_state_idx])
        forecast.append(states[next_state_idx])
        current_state_idx = next_state_idx
    return forecast

# Predict the next 5 days starting from Sunny
forecast_result = predict_weather("Sunny", 5)
print(forecast_result)
# Example output: ['Sunny', 'Sunny', 'Sunny', 'Sunny', 'Sunny']
</code>

The simulation above predicts five consecutive sunny days, but due to randomness different runs may yield other sequences.

Markov Chain Keyboard Prediction Model

Using a tiny text corpus:

<code>I love Python programming.
I love machine learning.
Python is great.
Learning Python is fun.
</code>

We build a bigram (order‑2) Markov chain where each word depends only on the preceding word.

<code>from collections import defaultdict, Counter
import numpy as np

def train_markov_chain(text):
    words = text.split()
    model = defaultdict(Counter)
    for prev_word, next_word in zip(words[:-1], words[1:]):
        model[prev_word][next_word] += 1
    return model

def predict_next_word(model, current_word):
    if current_word not in model:
        return None
    next_words = list(model[current_word].keys())
    next_word_probs = list(model[current_word].values())
    total = sum(next_word_probs)
    next_word_probs = [x/total for x in next_word_probs]
    return np.random.choice(next_words, p=next_word_probs)

text = """
I love Python programming.
I love machine learning.
Python is great.
Learning Python is fun.
"""
model = train_markov_chain(text)
predicted_word = predict_next_word(model, "I")
print(predicted_word)  # Expected output: 'love'
</code>

Given the current word "I", the model predicts the next word "love". Real keyboard prediction systems use larger corpora and more sophisticated models, but this example illustrates the basic Markov‑chain approach.

machine learningsimulationPythonprobabilityMarkov Chain
Model Perspective
Written by

Model Perspective

Insights, knowledge, and enjoyment from a mathematical modeling researcher and educator. Hosted by Haihua Wang, a modeling instructor and author of "Clever Use of Chat for Mathematical Modeling", "Modeling: The Mathematics of Thinking", "Mathematical Modeling Practice: A Hands‑On Guide to Competitions", and co‑author of "Mathematical Modeling: Teaching Design and Cases".

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.