Skip to content

Commit f699174

Browse files
committed
update markov_chain_animation
Dear John, This pull request updates the Markov chain animation for different initial distributions. In addition, this pull request also updates the descriptions of the code. The previous pull request #492 can be closed as this one has no merge conflict. Best, Longye
1 parent fac9a2f commit f699174

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

lectures/markov_chains_I.md

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -812,43 +812,48 @@ P = np.array([[0.971, 0.029, 0.000],
812812
P @ P
813813
```
814814

815-
Let's pick an initial distribution $\psi_0$ and trace out the sequence of distributions $\psi_0 P^t$ for $t = 0, 1, 2, \ldots$
815+
Let's pick some initial distributions $\psi_1, \psi_2, \psi_3$ and trace out the sequence of distributions $\psi_iP^t$ for $t = 0, 1, 2, \ldots$ and $i=1, 2, 3$.
816816

817-
First, we write a function to iterate the sequence of distributions for `ts_length` period
818817

819818
```{code-cell} ipython3
820-
def iterate_ψ(ψ_0, P, ts_length):
821-
n = len(P)
822-
ψ_t = np.empty((ts_length, n))
823-
ψ_t[0 ]= ψ_0
824-
for t in range(1, ts_length):
825-
ψ_t[t] = ψ_t[t-1] @ P
826-
return ψ_t
827-
```
819+
ψ_1 = (0.0, 0.0, 1.0)
820+
ψ_2 = (1.0, 0.0, 0.0)
821+
ψ_3 = (0.0, 1.0, 0.0) # Three initial conditions
822+
colors = ['blue','red', 'green'] # Different colors for each initial point
828823
829-
Now we plot the sequence
824+
# Define the vertices of the unit simplex
825+
v = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0]])
830826
831-
```{code-cell} ipython3
832-
ψ_0 = (0.0, 0.2, 0.8) # Initial condition
827+
# Define the faces of the unit simplex
828+
faces = [
829+
[v[0], v[1], v[2]],
830+
[v[0], v[1], v[3]],
831+
[v[0], v[2], v[3]],
832+
[v[1], v[2], v[3]]
833+
]
833834
834835
fig = plt.figure()
835836
ax = fig.add_subplot(projection='3d')
836837
837-
def update(n):
838-
ψ_t = iterate_ψ(ψ_0, P, n+1)
839-
838+
def update(n):
840839
ax.clear()
841840
ax.set_xlim([0, 1])
842841
ax.set_ylim([0, 1])
843842
ax.set_zlim([0, 1])
844-
ax.view_init(30, 210)
843+
ax.view_init(45, 45)
845844
846-
for i, point in enumerate(ψ_t):
847-
ax.scatter(point[0], point[1], point[2], color='r', s=60, alpha=(i+1)/len(ψ_t))
845+
simplex = Poly3DCollection(faces, alpha=0.03)
846+
ax.add_collection3d(simplex)
848847
848+
for idx, ψ_0 in enumerate([ψ_1, ψ_2, ψ_3]):
849+
ψ_t = iterate_ψ(ψ_0, P, n+1)
850+
851+
for i, point in enumerate(ψ_t):
852+
ax.scatter(point[0], point[1], point[2], color=colors[idx], s=60, alpha=(i+1)/len(ψ_t))
853+
849854
mc = qe.MarkovChain(P)
850855
ψ_star = mc.stationary_distributions[0]
851-
ax.scatter(ψ_star[0], ψ_star[1], ψ_star[2], c='k', s=60)
856+
ax.scatter(ψ_star[0], ψ_star[1], ψ_star[2], c='yellow', s=60)
852857
853858
return fig,
854859
@@ -860,9 +865,9 @@ HTML(anim.to_jshtml())
860865
Here
861866

862867
* $P$ is the stochastic matrix for recession and growth {ref}`considered above <mc_eg2>`.
863-
* The highest red dot is an arbitrarily chosen initial marginal probability distribution $\psi_0$, represented as a vector in $\mathbb R^3$.
864-
* The other red dots are the marginal distributions $\psi_0 P^t$ for $t = 1, 2, \ldots$.
865-
* The black dot is $\psi^*$.
868+
* The red, blue and green dots are initial marginal probability distribution $\psi_1, \psi_2, \psi_3$, represented as a vector in $\mathbb R^3$.
869+
* The transparent dots are the marginal distributions $\psi_i P^t$ for $t = 1, 2, \ldots$, for $i=1, 2, 3$.
870+
* The yellow dot is $\psi^*$.
866871

867872
You might like to try experimenting with different initial conditions.
868873

0 commit comments

Comments
 (0)