Skip to content

Commit

Permalink
[markov_chain_I] Hamilton's chain animation (#457)
Browse files Browse the repository at this point in the history
This pull request include the animation for the convergence to a stationary distribution in our Markov Chain lecture (markov_chain_I, Hamilton's chain).
  • Loading branch information
longye-tian authored Jun 17, 2024
1 parent f4c78ca commit 6fbd7d1
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions lectures/markov_chains_I.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ import numpy as np
import networkx as nx
from matplotlib import cm
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.animation import FuncAnimation
from IPython.display import HTML
```

## Definitions and examples
Expand Down Expand Up @@ -812,16 +815,27 @@ Now we plot the sequence
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ψ_t = iterate_ψ(ψ_0, P, 20)
ax.scatter(ψ_t[:,0], ψ_t[:,1], ψ_t[:,2], c='r', s=60)
ax.view_init(30, 210)
mc = qe.MarkovChain(P)
ψ_star = mc.stationary_distributions[0]
ax.scatter(ψ_star[0], ψ_star[1], ψ_star[2], c='k', s=60)
def update(n):
ψ_t = iterate_ψ(ψ_0, P, n+1)
ax.clear()
ax.set_xlim([0, 1])
ax.set_ylim([0, 1])
ax.set_zlim([0, 1])
ax.view_init(30, 210)
for i, point in enumerate(ψ_t):
ax.scatter(point[0], point[1], point[2], color='r', s=60, alpha=(i+1)/len(ψ_t))
mc = qe.MarkovChain(P)
ψ_star = mc.stationary_distributions[0]
ax.scatter(ψ_star[0], ψ_star[1], ψ_star[2], c='k', s=60)
return fig,
plt.show()
anim = FuncAnimation(fig, update, frames=range(20), blit=False, repeat=False)
plt.close()
HTML(anim.to_jshtml())
```

Here
Expand Down

0 comments on commit 6fbd7d1

Please sign in to comment.