Closed
Description
Dear John @jstac,
I’ve put together some code to animate the convergence to a stationary distribution in our Markov Chain lecture (markov_chain_I). I hope this will be helpful if we decide to include animations in the future. Here are some details.
It requires the following imports,
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.animation import FuncAnimation
from IPython.display import HTML
And putting the rest of the code in the Jupyter notebook after we define iterate_ψ()
ψ_0 = (0.0, 0.2, 0.8)
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
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,
anim = FuncAnimation(fig, update, frames=range(20), blit=False, repeat=False)
plt.close()
HTML(anim.to_jshtml())
It will render an interactive plot with a sliding bar and a play button and options as shown below:
animation.mov
Best ❤️
Longye
Metadata
Metadata
Assignees
Labels
No labels