Skip to content

Commit 120f41e

Browse files
committed
Revert "update markov_chain_animation"
This reverts commit f699174.
1 parent f699174 commit 120f41e

File tree

1 file changed

+23
-28
lines changed

1 file changed

+23
-28
lines changed

lectures/markov_chains_I.md

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

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$.
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$
816816

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

818819
```{code-cell} ipython3
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
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+
```
823828

824-
# Define the vertices of the unit simplex
825-
v = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0]])
829+
Now we plot the sequence
826830

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-
]
831+
```{code-cell} ipython3
832+
ψ_0 = (0.0, 0.2, 0.8) # Initial condition
834833
835834
fig = plt.figure()
836835
ax = fig.add_subplot(projection='3d')
837836
838-
def update(n):
837+
def update(n):
838+
ψ_t = iterate_ψ(ψ_0, P, n+1)
839+
839840
ax.clear()
840841
ax.set_xlim([0, 1])
841842
ax.set_ylim([0, 1])
842843
ax.set_zlim([0, 1])
843-
ax.view_init(45, 45)
844+
ax.view_init(30, 210)
844845
845-
simplex = Poly3DCollection(faces, alpha=0.03)
846-
ax.add_collection3d(simplex)
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))
847848
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-
854849
mc = qe.MarkovChain(P)
855850
ψ_star = mc.stationary_distributions[0]
856-
ax.scatter(ψ_star[0], ψ_star[1], ψ_star[2], c='yellow', s=60)
851+
ax.scatter(ψ_star[0], ψ_star[1], ψ_star[2], c='k', s=60)
857852
858853
return fig,
859854
@@ -865,9 +860,9 @@ HTML(anim.to_jshtml())
865860
Here
866861

867862
* $P$ is the stochastic matrix for recession and growth {ref}`considered above <mc_eg2>`.
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^*$.
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^*$.
871866

872867
You might like to try experimenting with different initial conditions.
873868

0 commit comments

Comments
 (0)