Skip to content

Commit 8f186c0

Browse files
committed
[markov_chain_I] Update animation using multiple initial conditions with unit simplex
Dear John, This pull request is corresponding to #482 . I have updated the animation in the markov_chain_I lecture with multiple initial conditions. Best, Longye
1 parent e8975ab commit 8f186c0

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

lectures/markov_chains_I.md

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ import numpy as np
5858
import networkx as nx
5959
from matplotlib import cm
6060
import matplotlib as mpl
61+
from matplotlib.patches import Polygon
62+
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
6163
from mpl_toolkits.mplot3d import Axes3D
6264
from matplotlib.animation import FuncAnimation
6365
from IPython.display import HTML
@@ -810,26 +812,44 @@ def iterate_ψ(ψ_0, P, ts_length):
810812
Now we plot the sequence
811813

812814
```{code-cell} ipython3
813-
ψ_0 = (0.0, 0.2, 0.8) # Initial condition
815+
ψ_1 = (0.0, 0.0, 1.0)
816+
ψ_2 = (1.0, 0.0, 0.0)
817+
ψ_3 = (0.0, 1.0, 0.0) # Three initial conditions
818+
colors = ['blue','red', 'green'] # Different colors for each initial point
819+
820+
# Define the vertices of the unit simplex
821+
v = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0]])
822+
823+
# Define the faces of the unit simplex
824+
faces = [
825+
[v[0], v[1], v[2]],
826+
[v[0], v[1], v[3]],
827+
[v[0], v[2], v[3]],
828+
[v[1], v[2], v[3]]
829+
]
814830
815831
fig = plt.figure()
816832
ax = fig.add_subplot(projection='3d')
817833
818-
def update(n):
819-
ψ_t = iterate_ψ(ψ_0, P, n+1)
820-
834+
def update(n):
821835
ax.clear()
822836
ax.set_xlim([0, 1])
823837
ax.set_ylim([0, 1])
824838
ax.set_zlim([0, 1])
825-
ax.view_init(30, 210)
839+
ax.view_init(45, 45)
826840
827-
for i, point in enumerate(ψ_t):
828-
ax.scatter(point[0], point[1], point[2], color='r', s=60, alpha=(i+1)/len(ψ_t))
841+
simplex = Poly3DCollection(faces, alpha=0.03)
842+
ax.add_collection3d(simplex)
829843
844+
for idx, ψ_0 in enumerate([ψ_1, ψ_2, ψ_3]):
845+
ψ_t = iterate_ψ(ψ_0, P, n+1)
846+
847+
for i, point in enumerate(ψ_t):
848+
ax.scatter(point[0], point[1], point[2], color=colors[idx], s=60, alpha=(i+1)/len(ψ_t))
849+
830850
mc = qe.MarkovChain(P)
831851
ψ_star = mc.stationary_distributions[0]
832-
ax.scatter(ψ_star[0], ψ_star[1], ψ_star[2], c='k', s=60)
852+
ax.scatter(ψ_star[0], ψ_star[1], ψ_star[2], c='yellow', s=60)
833853
834854
return fig,
835855

0 commit comments

Comments
 (0)