DeepNull

Stochastic Series - Pt 6 - Stationary Distribution

Imagine you are working at a medium-sized SaaS company with exactly 1000 customers. The customers have three different plans to choose from: Starter, Professional and Enterprise.

At the start of the month, the distribution of customers across the plans looks like this:

Now imagine you check the distribution next quarter and it looks exactly the same:

You scratch your head and think something is wrong with your internal dashboard. You refresh the dashboard multiple times and debug your SQL query, but you find nothing wrong.

Did time stand still? What about upgrades and downgrades between plans?

Let's look at the expected customer movements during a month:

From / To Starter Professional Enterprise
Starter 720 30 0
Professional 30 160 10
Enterprise 0 10 40

It turns out that customers actually moved between plans, but with one caveat: the movements cancelled each other out.

This is a great example of a stationary distribution.

"A stationary distribution is a probability distribution for a Markov process that does not change over time."

The distribution (q)

For each month, the distribution over plans looks the same.

q0 = (0.75, 0.20, 0.05)

The row vector above (referred to as q) represents the probability of a customer being on the Starter, Professional or Enterprise plan at month 0.

Now let's look at months 1 and 2.

q1 = (0.75, 0.20, 0.05)

q2 = (0.75, 0.20, 0.05)

As you can see, the distribution is stationary. Nothing changes.

But how can this be possible? Well, it turns out that if our transition matrix is arranged in a particular way, the result of the matrix multiplication yields the same values forever.

Validating the math

Let

P=(0.960.040.000.150.800.050.000.200.80)

And

q0 = (0.75, 0.20, 0.05)

If the distribution is indeed stationary, then this needs to be true:

q1 = q0 × P = q0

Let's validate:

Starter:

(0.75 × 0.96) + (0.20 × 0.15) + (0.05 × 0) = 0.75

Professional:

(0.75 × 0.04) + (0.20 × 0.80) + (0.05 × 0.20) = 0.20

Enterprise:

(0.75 × 0) + (0.20 × 0.05) + (0.05 × 0.80) = 0.05

q1 = (0.75, 0.20, 0.05)

As you can see, we are stuck at a distribution that does not change as long as P stays the same.

When does this happen?

Irreducible Markov chains

It turns out that if we randomly generate a finite, valid transition matrix, it will always have at least one stationary distribution.

In our example, the chain is irreducible, so it has exactly one stationary distribution, which happens to be (0.75, 0.20, 0.05).

Just a reminder, irreducibility means that all states can eventually reach each other. In our example, customers can move between all plans.

Reducible Markov chains

But what if customers never change plans? Then P would look like this:

Let

P=(0.960.040.000.150.800.050.000.200.80)

Read the transition matrix like this:

If you start on Plan 1, there is a 100% probability that you will "transition", or actually stay, on Plan 1. The same goes for Plan 2 (second row).

No matter what distribution we assign to q, it will stay that way. Every possible distribution between Plan 1 and Plan 2 is stationary.

This means that our reducible chain has infinitely many stationary distributions.

Converging to a stationary distribution

We don't have to start a Markov chain with its stationary distribution. For a finite Markov chain, it will converge to a unique stationary distribution, regardless of the initial distribution, if the following conditions are met:

  1. Irreducible
    • Every plan can eventually be reached from any other plan.
  2. Aperiodic
    • The chain is not forced to follow a fixed repeating cycle. In our example, customers have a probability of staying on their current plan.

If these conditions are met (like in our first example), the chain will eventually converge to its stationary distribution.

Math and symbols

We denote a stationary distribution as:

π (Pi)

So in our first example:

π = (0.75, 0.20, 0.05)

For a stationary distribution, the following is true:

π = π × P

This also means that if q is stationary, the distribution won't change:

q1 = q0 × P = q0