What is stationary distribution in markov chain? Explain it intuitively using numerical example
Assistant
In a Markov chain, a stationary distribution is a probability distribution that remains unchanged in the long run. Basically, it's when the probabilities of being in different states (nodes) stabilize after a certain number of transitions (steps), no matter what the initial distribution was.
Intuitively, each state/node in the Markov chain acts as a "hub", where a random "walker" transitions between different states with defined probabilities. In the long run, no matter where a walker starts, they will eventually experience the same overall proportions of time spent in each state. The stationary distribution captures these proportions.
User
What is the intuition behind this process. Why it's correct mathematically?
Assistant
The intuition behind the stationary distribution and why it's mathematically correct relies on the properties of Markov chains and the transition probabilities between states.
As a Markov chain satisfies the Markov property, it means that the transition probabilities depend only on the present state and not on the history of states. Intuitively, this "memoryless" property creates a certain stability in the long run, allowing for the existence of a stationary distribution.
User
So we can get the stationary distribution by calculating the eigenvector directly? Also show why this is true "This equilibrium is achieved because of the memoryless property of Markov chains and the constraints imposed by the transition matrix itself. T"
Assistant
Yes, we can obtain the stationary distribution by calculating the eigenvector associated with the eigenvalue of 1 for the transition matrix. This eigenvector, when normalized, represents the unique probability distribution that remains unchanged after applying the transition matrix, P.
Now, let's explain why the equilibrium is achieved due to the memoryless property of Markov chains and the constraints imposed by the transition matrix.
User
Can you show numerically how this is happening "These constraints inherently preventing any probabilities from being “lost” or “created”, but rather redistributed" please show gradual explanation
Assistant
Certainly! I will illustrate this concept with a simple example using the same 2-state Markov chain we discussed earlier. Recall the transition matrix for the Markov chain is as follows:
A B
A 0.6 0.4
B 0.7 0.3
Let's start with an arbitrary initial probability distribution for the Markov chain. Let's call it vector :
User
Why this convergence happens, please explain the math behind it intuitively.
Assistant
User
I mean how the matrix redistribute the vector value when we multiply it many times? What the matrix do exactly mathematically to redistribute the values of the initial vector towards the convergence values?
Assistant
When we multiply the transition matrix with the initial probability distribution vector, the probabilities in the vector are updated based on the transition probabilities defined by the matrix. As we multiply the transition matrix with the updated probability vector multiple times, it continuously redistributes and adjusts the probability mass. Let's examine more closely what happens mathematically during this process.
User
Ok, imagine the initial vector as an geometric arrow in space, what the transition matrix do to the vector geometrically when we multiply it. And the process goes on geometrically in each iteration?
Assistant
When we multiply the transition matrix with the initial vector (geometric arrow), it geometrically transforms the vector, moving it in the vector space. With each successive multiplication (iteration), the transition matrix transforms the vector further, influencing both its direction and magnitude based on the transition probabilities encoded in the matrix. Let's delve deeper into how the matrix transforms the vector geometrically in each iteration:
User
Can you show me how the matrix move the initial vector towards the eigenvector by each iteration using a visuallized python code?
Assistant
User
Why the transition matrix transform the initial vector towards its eigenvector. Explain the math behind it easily.
Assistant
The transition matrix transforms the initial vector towards its eigenvector associated with the eigenvalue 1 due to how the matrix multiplication influences the vector geometrically with each iteration. Let's try to break down the math behind it with a simple explanation:
User
Could you clarify what you by " As we apply the transition matrix to the initial vector multiple times, we are effectively raising the matrix to higher powers" show how the matrix raised to higher powers using the numerical example you used before.
Assistant
I apologize for any confusion. I can certainly clarify this statement and demonstrate how the matrix is raised to higher powers using the numerical example.
When we say "applying the transition matrix to the initial vector multiple times," it means that we are computing the probability distribution after several iterations (or steps) in the Markov chain. In each step, we multiply the transition matrix with the current probability distribution. The matrix power comes into play when we want to find the probability distribution after multiple steps in the chain.
Let's consider a simple numerical example:
Imagine we have a Markov chain with two states, A and B, and the following transition probabilities:
From state A, there is a 0.6 probability to stay in A and a 0.4 probability to move to B.
From state B, there is a 0.3 probability to stay in B and a 0.7 probability to move to A.
This can be represented by a transition matrix:
A B
A 0.6 0.4
B 0.7 0.3
Now, let's suppose we have to find the stationary distribution (vector) of this Markov chain. Let's denote it as (x, y), where x is the long-term probability of being in state A, and y is the long-term probability of being in state B.
By definition, applying the transition matrix to the stationary distribution vector should give back the same vector:
(P is the transition matrix)
P * (x, y) = (x, y)
Multiplying it out:
0.6x + 0.7y = x
0.4x + 0.3y = y
Solving these linear equations (along with x+y=1 as it is a probability distribution), we get:
x = 0.625
y = 0.375
Hence, the stationary distribution is approximately (0.625, 0.375).
This means that after some time and many transitions, no matter where a random "walker" started from, they would spend around 62.5% of their time in state A and 37.5% of their time in state B.
Mathematically, the stationary distribution is the eigenvector of the transition matrix with an eigenvalue of 1 (probability is conserved). Let's break down why this is the case:
The transition matrix, P, represents the probabilities of transitioning between states in one step. By design, it is a stochastic matrix (meaning each row sums up to 1, as rows represent probability distributions) and its entries are non-negative.
The Perron-Frobenius theorem guarantees that a stochastic matrix will always have an eigenvalue of 1, and there exists a unique corresponding eigenvector with all entries being non-negative (i.e., a probability distribution). This eigenvector is the stationary distribution.
Since the stationary distribution vector, v, is an eigenvector of P with eigenvalue 1, it satisfies:
P * v = 1 * v
This means when we apply the transition matrix P to the stationary distribution v, we get back the same distribution v. In other words, the probability distribution remains unchanged in the long run after successive transitions.
Intuitively, this means that after a sufficient number of steps, the system reaches an equilibrium, where the probabilities of being in different states become stable. This equilibrium is achieved because of the memoryless property of Markov chains and the constraints imposed by the transition matrix itself. The stationary distribution reflects the balance between the inflows and outflows of probability mass among the states of the chain in the long run, despite its initial distribution.
Memoryless property: In a Markov chain, each state transition is dependent only on the current state and is independent of the sequence of past states. This means that the future behavior of the system is solely determined by its current state, regardless of how it arrived at that state. This property ensures that probability mass is "spread" among the states over time, ultimately converging to an equilibrium where the stationary distribution probabilities are adjusted according to the characteristic properties of the system captured by the transition matrix P.
Constraints imposed by the transition matrix: The transition matrix, P, is a stochastic matrix, meaning all its entries are non-negative, and the sum of the entries in each row is equal to 1 reflecting a probability distribution. These constraints inherently force the probabilities to be conserved during state transitions, preventing any probabilities from being "lost" or "created", but rather redistributed.
The conservation of probability mass is a key reason why the system reaches equilibrium in the long run. As the Markov chain transitions between states, the probability mass gets redistributed across states according to the transition probabilities. Over time, the cumulative effects of these redistributions eventually lead to a balance in the inflows and outflows of probability mass among states. This balanced distribution is the stationary distribution.
In summary, the memoryless property helps distribute the probability mass among states in the long run, while the constraints imposed by the transition matrix ensure the conservation of probability mass. Together, these properties contribute to the convergence of the Markov chain towards an equilibrium represented by the stationary distribution.
v0
v0 = [0.8, 0.2] (80% chance of being in state A and 20% chance of being in state B)
Now we will see how the probabilities evolve over time (iterations) and demonstrate that no probability mass is lost or created in each step:
The new probability distribution (after 3 steps) is [0.6254, 0.3746]. Once again, the sum of probabilities remains 1:
0.6254 + 0.3746 = 1
In each iteration, we observe that the total probability mass remains conserved (sum equals 1) as probabilities are redistributed among states. This is a direct result of the constraints imposed by the transition matrix. The constant sum of probabilities in the transition matrix rows ensures that the probabilities are neither lost nor created but only redistributed among the states.
As the number of iterations increases, the probability distribution gradually converges towards the stationary distribution, reflecting the equilibrium achieved by the redistribution of probability mass through the memoryless nature of Markov chains and constraints of the transition matrix. In our example, as we continue applying the transition matrix to the current distribution, it will get closer and closer to the stationary distribution of approximately [0.625, 0.375].
The convergence of the probability distribution towards the stationary distribution in a Markov chain is a result of repeated applications of the transition matrix to the current distribution. Intuitively, this can be thought of as taking many steps in the Markov chain, gradually updating the probability distribution based on the memoryless nature of the chain and the constraints of the transition matrix. Let's break down the factors contributing to this convergence:
Memoryless nature: The probability of transitioning to a certain state depends only on the current state and not on the previous history. This implies that the chain continuously moves between states based on the current state and the probability transition matrix, causing the probability mass to be spread out and redistributed among states. Eventually, the Markov chain reaches an equilibrium in which the probabilities stabilize, balancing the inflows and outflows of probability mass among states.
Constraints of the transition matrix: The transition matrix is filled with non-negative entries, and each row sums up to 1 (representing a probability distribution). The probability mass is conserved during state transitions, which means no probability is lost or created. These constraints ensure that the probability mass is redistributed but preserved in the process. As more steps are taken in the chain, the probability distribution for the entire Markov chain gradually gets adjusted according to the characteristic properties of the system captured by the transition matrix.
Repeated application of the transition matrix: In each step, the transition matrix is multiplied by the current probability distribution. This multiplication continuously updates the probability distribution based on the transition probabilities. Since the system is memoryless and the probability mass is conserved, the repeated application drives the distribution towards an eventual equilibrium - the stationary distribution.
Mathematically, this convergence can be understood by recognizing that the Markov chain's stationary distribution corresponds to the eigenvector associated with the eigenvalue of 1 in the transition matrix. As the matrix is repeatedly applied (i.e., raising the matrix to higher powers), the effects of other eigenvalues diminish, and the dominant eigenvalue (1 in this case) becomes apparent. This leads to the convergence towards the stationary distribution, which represents the proportion of time spent in each state in the long run.
In summary, the convergence towards the stationary distribution is a result of the memoryless nature of the Markov chain, the constraints of the transition matrix, and the repeated application of the matrix. These factors drive the system towards an equilibrium where the probabilities get stabilized, reflecting the balance in the inflows and outflows of probability mass among states.
Consider a transition matrix P (dimensions nxn) and an initial probability distribution vector v0 (dimensions nx1). When we multiply P with v0, we get a new vector v1:
v1 = P * v0
The elements of v1 are calculated as:
v1_i = Σ (P_ij * v0_j) for j = 1 to n
Here, v1_i represents the probability of being in state i after one step. It is calculated as the sum of the product of the transition probabilities (P_ij) and the corresponding probabilities in the initial vector (v0_j). This operation mathematically redistributes the probabilities based on the transition probabilities defined by the matrix.
Now, to update the probabilities beyond one step, we apply the transition matrix multiple times. The probability distribution after k steps can be computed as:
v_k = P^k * v0
With each subsequent multiplication, the transition matrix adjusts the probability mass in the distribution vector based on the inherent properties of the matrix (i.e., its transition probabilities). Since the transition matrix has an eigenvalue of 1, as the matrix is raised to higher powers (P^k), the influence of other eigenvalues diminishes, and the dominant eigenvalue (1) affects the system. This leads to the convergence towards the eigenvector associated with the eigenvalue 1 – the stationary distribution.
Essentially, when we multiply the transition matrix with the probability vector repeatedly, the matrix enforces both the redistribution of probability mass via the memoryless property of the Markov chain and conservation of the total probability mass via the constraints of the matrix. This repeated application continuously reshapes the probability distribution towards the stationary distribution, where the balance between the inflows and outflows of probability mass among states is achieved. By redistributing the vector value with each multiplication, the matrix guides the probability distribution towards convergence with the stationary distribution.
Direction change: When multiplying the transition matrix with the probability vector, each component (axis) of the vector gets updated based on the transition probabilities for each state. This causes the vector to change its direction in the vector space, representing the redistribution of probability mass among the states. In other words, the vector "rotates" or changes its orientation in the space upon each multiplication, reflecting the updated probability distribution after each step.
Magnitude change: Due to the constraints of the transition matrix (non-negative entries and row sums equal to 1), the vector's magnitude remains conserved – it remains a probability distribution with components summing up to 1. However, as the matrix gets repeated applied, the components of the vector get adjusted and redistributed based on the transition probabilities. Through this adjustment process, the vector moves towards an equilibrium in the vector space where its magnitude represents the stationary distribution.
As we iteratively apply the transition matrix to the vector multiple times, the geometric consequences of these transformations cause the vector to gradually approach the stationary distribution. In vector space, this process can be understood as the vector moving towards the fixed point (eigenvector) that corresponds to the stationary distribution.
During each iteration, as the transition matrix P is multiplied with the current probability vector, the geometric transformation resulting from the matrix multiplication (rotation and redistribution) pushes the vector closer to the stationary distribution. This is because the repeated application of the matrix emphasizes the properties of the dominant eigenvalue (1), eventually guiding the vector to converge towards the associated eigenvector – the stationary distribution.
Certainly! We can visualize the geometric transitions of an initial vector towards the eigenvector associated with the stationary distribution using Python and matplotlib. Here's a Python example using the same 2-state Markov chain we discussed earlier:
import numpy as np
import matplotlib.pyplot as plt
# Transition matrix P
P = np.array([[0.6, 0.4],
[0.7, 0.3]])
# Initial probability distribution vector
v0 = np.array([0.8, 0.2])
# Calculate the eigenvector for the eigenvalue 1 (stationary distribution)
eigenvalues, eigenvectors = np.linalg.eig(P.T)
stationary_distribution = eigenvectors[:, np.argmax(eigenvalues)].real
stationary_distribution /= np.sum(stationary_distribution)
# Visualizing the geometric transitions
fig, ax = plt.subplots(figsize=(6, 6))
ax.quiver([0], [0], v0[0], v0[1], angles='xy', scale_units='xy', scale=1, color='b', label='Initial vector')
iterations = 5
vector = v0
colors = ['g', 'orange', 'purple', 'magenta', 'red']
for i in range(iterations):
vector = P @ vector
ax.quiver([0], [0], vector[0], vector[1], angles='xy', scale_units='xy', scale=1, color=colors[i], label=f'After {i+1} steps')
ax.quiver([0], [0], stationary_distribution[0], stationary_distribution[1], angles='xy', scale_units='xy', scale=1, color='black', label='Stationary distribution')
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_xlabel('State A')
ax.set_ylabel('State B')
ax.legend(loc='upper right')
plt.grid()
plt.show()
In this code, we start with the same transition matrix and initial vector that we previously discussed. We then calculate the eigenvector associated with the stationary distribution using NumPy's linalg.eig function. The resulting eigenvector is normalized to ensure it sums up to 1, as it's a probability distribution.
We iterate through 5 steps (you can choose any number of iterations), and in each step, we multiply the current vector with the transition matrix. This represents a geometric transformation of the probability vector.
Finally, we use matplotlib to visualize the geometric transitions of the initial vector towards the stationary distribution by plotting arrows representing the vectors at each step. The different colors indicate the vector transformation after each step in the iteration. The stationary distribution is marked in black color.
From the generated plot, you can observe how the initial vector (in blue) gradually moves through different iterations (green, orange, purple, magenta, and red) as it converges towards the eigenvector representing the stationary distribution (in black).
Dominant eigenvalue: In a Markov chain, the transition matrix P is a stochastic matrix, which means its rows sum up to 1 (representing probability distributions). Due to the properties of stochastic matrices, the eigenvalue 1 is guaranteed to be the dominant eigenvalue, and there is also a unique corresponding eigenvector with all non-negative components (i.e., a probability distribution).
Repeated application of the transition matrix: As we apply the transition matrix to the initial vector multiple times, we are effectively raising the matrix to higher powers, like P^k for some k. When computing the matrix power P^k, the effects of all other eigenvalues to the power k (except the eigenvalue 1) will gradually diminish, while the dominant eigenvalue 1 remains unchanged. This is because values other than 1 will either grow (if larger than 1, but not possible with stochastic matrices) or shrink (if smaller than 1) when raised to larger powers.
Multiplication with the vector: When multiplying a matrix raised to high powers (P^k) with a vector, the dominant eigenvalue's effects in the matrix become more prominent compared to others. In our case, the dominant eigenvalue is 1. Consequently, the multiplication influences the vector geometrically and transforms it towards the eigenvector associated with the eigenvalue 1 – the stationary distribution.
Convergence: Due to the repeated application of the transition matrix, the influence of the dominant eigenvalue (1) becomes dominant, and the probability vector leans towards the associated eigenvector. The initial vector converges to the eigenvector corresponding to the eigenvalue of 1, regardless of the starting point, as the system evolves over time (i.e., after many iterations).
In summary, the transition matrix transforms the initial vector towards its eigenvector due to the dominance of the eigenvalue 1 when the matrix is raised to high powers in a stochastic matrix. Repeated applications of the matrix gradually reshape and guide the vector towards the associated eigenvector, which is the stationary distribution in the case of Markov chains.
Using the previous numerical example:
Transition matrix P:
A B
A 0.6 0.4
B 0.7 0.3
Initial probability distribution vector v0:
[0.8, 0.2]
Let's say we want to compute the probability distribution after 3 steps. In this case, instead of applying the transition matrix one step at a time, we can raise the transition matrix to the power of 3 and then multiply it with the initial vector:
v_3 = P^3 * v0
Calculating the matrix power P^3:
P^3 = P * P * P
P^2:
A B
A 0.58 0.42
B 0.67 0.33
P^3:
A B
A 0.610 0.390
B 0.637 0.363
Now, we can multiply P^3 with the initial vector v0 to find the probability distribution after 3 steps:
Raising the matrix to higher powers allows us to find the probability distribution vector after multiple steps in the chain with a single matrix-vector multiplication, rather than iteratively multiplying the matrix with the current vector one step at a time.
When we repeatedly apply the transition matrix (i.e., raise the matrix to higher powers and then apply it to the initial vector), the influences of the higher and lower eigenvalues in the matrix diminish more significantly compared to the dominant eigenvalue 1. Consequently, the vector converges to the eigenvector associated with eigenvalue 1 - the stationary distribution.