Skip to content

Commit 925aaba

Browse files
committed
latest edits to mc I lecture
1 parent 2b7dd96 commit 925aaba

File tree

1 file changed

+84
-76
lines changed

1 file changed

+84
-76
lines changed

lectures/markov_chains_I.md

Lines changed: 84 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ import matplotlib as mpl
6161
from mpl_toolkits.mplot3d import Axes3D
6262
from matplotlib.animation import FuncAnimation
6363
from IPython.display import HTML
64+
from matplotlib.patches import Polygon
65+
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
6466
```
6567

6668
## Definitions and examples
@@ -743,6 +745,11 @@ This is, in some sense, a steady state probability of unemployment.
743745

744746
Not surprisingly it tends to zero as $\beta \to 0$, and to one as $\alpha \to 0$.
745747

748+
749+
750+
751+
752+
746753
### Calculating stationary distributions
747754

748755
A stable algorithm for computing stationary distributions is implemented in [QuantEcon.py](http://quantecon.org/quantecon-py).
@@ -757,6 +764,11 @@ mc = qe.MarkovChain(P)
757764
mc.stationary_distributions # Show all stationary distributions
758765
```
759766

767+
768+
769+
770+
771+
760772
### Asymptotic stationarity
761773

762774
Consider an everywhere positive stochastic matrix with unique stationary distribution $\psi^*$.
@@ -768,16 +780,21 @@ For example, we have the following result
768780
(strict_stationary)=
769781
```{prf:theorem}
770782
If there exists an integer $m$ such that all entries of $P^m$ are
771-
strictly positive, with unique stationary distribution $\psi^*$, then
783+
strictly positive, then
772784
773785
$$
774786
\psi_0 P^t \to \psi^*
775787
\quad \text{ as } t \to \infty
776788
$$
789+
790+
where $\psi^*$ is the unique stationary distribution.
777791
```
778792

793+
This situation is often referred to as **asymptotic stationarity** or **global stability**.
794+
795+
A proof of the theorem can be found in Chapter 4 of {cite}`sargent2023economic`, as well as many other sources.
796+
779797

780-
See, for example, {cite}`sargent2023economic` Chapter 4.
781798

782799

783800

@@ -848,103 +865,94 @@ Here
848865
You might like to try experimenting with different initial conditions.
849866

850867

851-
#### An alternative illustration
852-
853-
We can show this in a slightly different way by focusing on the probability that $\psi_t$ puts on each state.
854868

855-
First, we write a function to draw initial distributions $\psi_0$ of size `num_distributions`
856-
857-
```{code-cell} ipython3
858-
def generate_initial_values(num_distributions):
859-
n = len(P)
860-
861-
draws = np.random.randint(1, 10_000_000, size=(num_distributions,n))
862-
ψ_0s = draws/draws.sum(axis=1)[:, None]
863869

864-
return ψ_0s
865-
```
870+
#### Example: failure of convergence
866871

867-
We then write a function to plot the dynamics of $(\psi_0 P^t)(i)$ as $t$ gets large, for each state $i$ with different initial distributions
868872

869-
```{code-cell} ipython3
870-
def plot_distribution(P, ts_length, num_distributions):
873+
Consider the periodic chain with stochastic matrix
871874

872-
# Get parameters of transition matrix
873-
n = len(P)
874-
mc = qe.MarkovChain(P)
875-
ψ_star = mc.stationary_distributions[0]
876-
877-
## Draw the plot
878-
fig, axes = plt.subplots(nrows=1, ncols=n, figsize=[11, 5])
879-
plt.subplots_adjust(wspace=0.35)
875+
$$
876+
P =
877+
\begin{bmatrix}
878+
0 & 1 \\
879+
1 & 0 \\
880+
\end{bmatrix}
881+
$$
880882

881-
ψ_0s = generate_initial_values(num_distributions)
883+
This matrix does not satisfy the conditions of
884+
{ref}`prf:theorem` because, as you can readily check, $P^m = P$ when $m$ is odd
885+
and $P^m = I$, the identity matrix, when $m$ is even.
882886

883-
# Get the path for each starting value
884-
for ψ_0 in ψ_0s:
885-
ψ_t = iterate_ψ(ψ_0, P, ts_length)
887+
Hence there is no $m$ such that all elements of $P^m$ are strictly positive.
886888

887-
# Obtain and plot distributions at each state
888-
for i in range(n):
889-
axes[i].plot(range(0, ts_length), ψ_t[:,i], alpha=0.3)
889+
Moreover, we can see that global stability does not hold.
890890

891-
# Add labels
892-
for i in range(n):
893-
axes[i].axhline(ψ_star[i], linestyle='dashed', lw=2, color = 'black',
894-
label = fr'$\psi^*({i})$')
895-
axes[i].set_xlabel('t')
896-
axes[i].set_ylabel(fr'$\psi_t({i})$')
897-
axes[i].legend()
891+
For instance, if we start at $\psi_0 = (1,0)$, then $\psi_m = \psi_0 P^m$ is $(1, 0)$ when $m$ is even and $(0,1)$ when $m$ is odd.
898892

899-
plt.show()
900-
```
893+
We can see similar phenomena in higher dimensions.
901894

902-
The following figure shows
895+
The next figure illustrates this for a periodic Markov chain with three states.
903896

904897
```{code-cell} ipython3
905-
# Define the number of iterations
906-
# and initial distributions
907-
ts_length = 50
908-
num_distributions = 25
909-
910-
P = np.array([[0.971, 0.029, 0.000],
911-
[0.145, 0.778, 0.077],
912-
[0.000, 0.508, 0.492]])
913-
914-
plot_distribution(P, ts_length, num_distributions)
915-
```
916-
917-
The convergence to $\psi^*$ holds for different initial distributions.
918-
898+
ψ_1 = (0.0, 0.0, 1.0)
899+
ψ_2 = (0.5, 0.5, 0.0)
900+
ψ_3 = (0.25, 0.25, 0.5)
901+
ψ_4 = (1/3, 1/3, 1/3)
919902
903+
P = np.array([[0.0, 1.0, 0.0],
904+
[0.0, 0.0, 1.0],
905+
[1.0, 0.0, 0.0]])
920906
921-
#### Example: failure of convergence
907+
fig = plt.figure()
908+
ax = fig.add_subplot(projection='3d')
909+
colors = ['red','yellow', 'green', 'blue'] # Different colors for each initial point
922910
911+
# Define the vertices of the unit simplex
912+
v = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0]])
923913
924-
In the case of a periodic chain, with
914+
# Define the faces of the unit simplex
915+
faces = [
916+
[v[0], v[1], v[2]],
917+
[v[0], v[1], v[3]],
918+
[v[0], v[2], v[3]],
919+
[v[1], v[2], v[3]]
920+
]
925921
926-
$$
927-
P =
928-
\begin{bmatrix}
929-
0 & 1 \\
930-
1 & 0 \\
931-
\end{bmatrix}
932-
$$
922+
def update(n):
923+
ax.clear()
924+
ax.set_xlim([0, 1])
925+
ax.set_ylim([0, 1])
926+
ax.set_zlim([0, 1])
927+
ax.view_init(45, 45)
928+
929+
# Plot the 3D unit simplex as planes
930+
simplex = Poly3DCollection(faces,alpha=0.05)
931+
ax.add_collection3d(simplex)
932+
933+
for idx, ψ_0 in enumerate([ψ_1, ψ_2, ψ_3, ψ_4]):
934+
ψ_t = iterate_ψ(ψ_0, P, n+1)
935+
936+
point = ψ_t[-1]
937+
ax.scatter(point[0], point[1], point[2], color=colors[idx], s=60)
938+
points = np.array(ψ_t)
939+
ax.plot(points[:, 0], points[:, 1], points[:, 2], color=colors[idx],linewidth=0.75)
940+
941+
return fig,
933942
934-
we find the distribution oscillates
943+
anim = FuncAnimation(fig, update, frames=range(20), blit=False, repeat=False)
944+
plt.close()
945+
HTML(anim.to_jshtml())
946+
```
947+
This animation demonstrates the behavior of an irreducible and periodic stochastic matrix.
935948

936-
```{code-cell} ipython3
937-
P = np.array([[0, 1],
938-
[1, 0]])
949+
The red, yellow, and green dots represent different initial probability distributions.
939950

940-
ts_length = 20
941-
num_distributions = 30
951+
The blue dot represents the unique stationary distribution.
942952

943-
plot_distribution(P, ts_length, num_distributions)
944-
```
953+
Unlike Hamilton’s Markov chain, these initial distributions do not converge to the unique stationary distribution.
945954

946-
Indeed, this $P$ fails our asymptotic stationarity condition, since, as you can
947-
verify, $P^t$ is not everywhere positive for any $t$.
955+
Instead, they cycle periodically around the probability simplex, illustrating that asymptotic stability fails.
948956

949957

950958
(finite_mc_expec)=

0 commit comments

Comments
 (0)