Skip to content

Commit 8b9175d

Browse files
authored
[markov_chain_I] Update animation for different initial distributions (#515)
* 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 * Revert "update markov_chain_animation" This reverts commit f699174. * Update markov_chains_I.md * Update markov_chains_I.md * Update markov_chains_I.md
1 parent fac9a2f commit 8b9175d

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

lectures/markov_chains_I.md

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ 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 an initial distribution $\psi_1, \psi_2, \psi_3$ and trace out the sequence of distributions $\psi_i P^t$ for $t = 0, 1, 2, \ldots$, for $i=1, 2, 3$.
816816

817817
First, we write a function to iterate the sequence of distributions for `ts_length` period
818818

@@ -829,26 +829,46 @@ def iterate_ψ(ψ_0, P, ts_length):
829829
Now we plot the sequence
830830

831831
```{code-cell} ipython3
832-
ψ_0 = (0.0, 0.2, 0.8) # Initial condition
832+
:tags: [hide-input]
833+
834+
ψ_1 = (0.0, 0.0, 1.0)
835+
ψ_2 = (1.0, 0.0, 0.0)
836+
ψ_3 = (0.0, 1.0, 0.0) # Three initial conditions
837+
colors = ['blue','red', 'green'] # Different colors for each initial point
838+
839+
# Define the vertices of the unit simplex
840+
v = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0]])
841+
842+
# Define the faces of the unit simplex
843+
faces = [
844+
[v[0], v[1], v[2]],
845+
[v[0], v[1], v[3]],
846+
[v[0], v[2], v[3]],
847+
[v[1], v[2], v[3]]
848+
]
833849
834850
fig = plt.figure()
835851
ax = fig.add_subplot(projection='3d')
836852
837-
def update(n):
838-
ψ_t = iterate_ψ(ψ_0, P, n+1)
839-
853+
def update(n):
840854
ax.clear()
841855
ax.set_xlim([0, 1])
842856
ax.set_ylim([0, 1])
843857
ax.set_zlim([0, 1])
844-
ax.view_init(30, 210)
858+
ax.view_init(45, 45)
845859
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))
860+
simplex = Poly3DCollection(faces, alpha=0.03)
861+
ax.add_collection3d(simplex)
848862
863+
for idx, ψ_0 in enumerate([ψ_1, ψ_2, ψ_3]):
864+
ψ_t = iterate_ψ(ψ_0, P, n+1)
865+
866+
for i, point in enumerate(ψ_t):
867+
ax.scatter(point[0], point[1], point[2], color=colors[idx], s=60, alpha=(i+1)/len(ψ_t))
868+
849869
mc = qe.MarkovChain(P)
850870
ψ_star = mc.stationary_distributions[0]
851-
ax.scatter(ψ_star[0], ψ_star[1], ψ_star[2], c='k', s=60)
871+
ax.scatter(ψ_star[0], ψ_star[1], ψ_star[2], c='yellow', s=60)
852872
853873
return fig,
854874
@@ -860,9 +880,9 @@ HTML(anim.to_jshtml())
860880
Here
861881

862882
* $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^*$.
883+
* The red, blue and green dots are initial marginal probability distributions $\psi_1, \psi_2, \psi_3$, each of which is represented as a vector in $\mathbb R^3$.
884+
* The transparent dots are the marginal distributions $\psi_i P^t$ for $t = 1, 2, \ldots$, for $i=1,2,3.$.
885+
* The yellow dot is $\psi^*$.
866886

867887
You might like to try experimenting with different initial conditions.
868888

@@ -899,6 +919,8 @@ We can see similar phenomena in higher dimensions.
899919
The next figure illustrates this for a periodic Markov chain with three states.
900920

901921
```{code-cell} ipython3
922+
:tags: [hide-input]
923+
902924
ψ_1 = (0.0, 0.0, 1.0)
903925
ψ_2 = (0.5, 0.5, 0.0)
904926
ψ_3 = (0.25, 0.25, 0.5)

0 commit comments

Comments
 (0)