From f69917425e418e2d723bc49c96075a42751b9e1e Mon Sep 17 00:00:00 2001 From: Longye Tian Date: Wed, 17 Jul 2024 12:44:51 +1000 Subject: [PATCH 1/5] 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 --- lectures/markov_chains_I.md | 51 ++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/lectures/markov_chains_I.md b/lectures/markov_chains_I.md index 2c308ce2f..e29c8bd90 100644 --- a/lectures/markov_chains_I.md +++ b/lectures/markov_chains_I.md @@ -812,43 +812,48 @@ P = np.array([[0.971, 0.029, 0.000], P @ P ``` -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$ +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$. -First, we write a function to iterate the sequence of distributions for `ts_length` period ```{code-cell} ipython3 -def iterate_ψ(ψ_0, P, ts_length): - n = len(P) - ψ_t = np.empty((ts_length, n)) - ψ_t[0 ]= ψ_0 - for t in range(1, ts_length): - ψ_t[t] = ψ_t[t-1] @ P - return ψ_t -``` +ψ_1 = (0.0, 0.0, 1.0) +ψ_2 = (1.0, 0.0, 0.0) +ψ_3 = (0.0, 1.0, 0.0) # Three initial conditions +colors = ['blue','red', 'green'] # Different colors for each initial point -Now we plot the sequence +# Define the vertices of the unit simplex +v = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0]]) -```{code-cell} ipython3 -ψ_0 = (0.0, 0.2, 0.8) # Initial condition +# Define the faces of the unit simplex +faces = [ + [v[0], v[1], v[2]], + [v[0], v[1], v[3]], + [v[0], v[2], v[3]], + [v[1], v[2], v[3]] +] fig = plt.figure() ax = fig.add_subplot(projection='3d') -def update(n): - ψ_t = iterate_ψ(ψ_0, P, n+1) - +def update(n): ax.clear() ax.set_xlim([0, 1]) ax.set_ylim([0, 1]) ax.set_zlim([0, 1]) - ax.view_init(30, 210) + ax.view_init(45, 45) - for i, point in enumerate(ψ_t): - ax.scatter(point[0], point[1], point[2], color='r', s=60, alpha=(i+1)/len(ψ_t)) + simplex = Poly3DCollection(faces, alpha=0.03) + ax.add_collection3d(simplex) + for idx, ψ_0 in enumerate([ψ_1, ψ_2, ψ_3]): + ψ_t = iterate_ψ(ψ_0, P, n+1) + + for i, point in enumerate(ψ_t): + ax.scatter(point[0], point[1], point[2], color=colors[idx], 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) + ax.scatter(ψ_star[0], ψ_star[1], ψ_star[2], c='yellow', s=60) return fig, @@ -860,9 +865,9 @@ HTML(anim.to_jshtml()) Here * $P$ is the stochastic matrix for recession and growth {ref}`considered above `. -* The highest red dot is an arbitrarily chosen initial marginal probability distribution $\psi_0$, represented as a vector in $\mathbb R^3$. -* The other red dots are the marginal distributions $\psi_0 P^t$ for $t = 1, 2, \ldots$. -* The black dot is $\psi^*$. +* 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$. +* The transparent dots are the marginal distributions $\psi_i P^t$ for $t = 1, 2, \ldots$, for $i=1, 2, 3$. +* The yellow dot is $\psi^*$. You might like to try experimenting with different initial conditions. From 120f41e3534ce9c03ec63580add5ba18257edbce Mon Sep 17 00:00:00 2001 From: Longye Tian Date: Wed, 17 Jul 2024 12:57:21 +1000 Subject: [PATCH 2/5] Revert "update markov_chain_animation" This reverts commit f69917425e418e2d723bc49c96075a42751b9e1e. --- lectures/markov_chains_I.md | 51 +++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 28 deletions(-) diff --git a/lectures/markov_chains_I.md b/lectures/markov_chains_I.md index e29c8bd90..2c308ce2f 100644 --- a/lectures/markov_chains_I.md +++ b/lectures/markov_chains_I.md @@ -812,48 +812,43 @@ P = np.array([[0.971, 0.029, 0.000], P @ P ``` -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$. +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$ +First, we write a function to iterate the sequence of distributions for `ts_length` period ```{code-cell} ipython3 -ψ_1 = (0.0, 0.0, 1.0) -ψ_2 = (1.0, 0.0, 0.0) -ψ_3 = (0.0, 1.0, 0.0) # Three initial conditions -colors = ['blue','red', 'green'] # Different colors for each initial point +def iterate_ψ(ψ_0, P, ts_length): + n = len(P) + ψ_t = np.empty((ts_length, n)) + ψ_t[0 ]= ψ_0 + for t in range(1, ts_length): + ψ_t[t] = ψ_t[t-1] @ P + return ψ_t +``` -# Define the vertices of the unit simplex -v = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0]]) +Now we plot the sequence -# Define the faces of the unit simplex -faces = [ - [v[0], v[1], v[2]], - [v[0], v[1], v[3]], - [v[0], v[2], v[3]], - [v[1], v[2], v[3]] -] +```{code-cell} ipython3 +ψ_0 = (0.0, 0.2, 0.8) # Initial condition fig = plt.figure() ax = fig.add_subplot(projection='3d') -def update(n): +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(45, 45) + ax.view_init(30, 210) - simplex = Poly3DCollection(faces, alpha=0.03) - ax.add_collection3d(simplex) + for i, point in enumerate(ψ_t): + ax.scatter(point[0], point[1], point[2], color='r', s=60, alpha=(i+1)/len(ψ_t)) - for idx, ψ_0 in enumerate([ψ_1, ψ_2, ψ_3]): - ψ_t = iterate_ψ(ψ_0, P, n+1) - - for i, point in enumerate(ψ_t): - ax.scatter(point[0], point[1], point[2], color=colors[idx], 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='yellow', s=60) + ax.scatter(ψ_star[0], ψ_star[1], ψ_star[2], c='k', s=60) return fig, @@ -865,9 +860,9 @@ HTML(anim.to_jshtml()) Here * $P$ is the stochastic matrix for recession and growth {ref}`considered above `. -* 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$. -* The transparent dots are the marginal distributions $\psi_i P^t$ for $t = 1, 2, \ldots$, for $i=1, 2, 3$. -* The yellow dot is $\psi^*$. +* The highest red dot is an arbitrarily chosen initial marginal probability distribution $\psi_0$, represented as a vector in $\mathbb R^3$. +* The other red dots are the marginal distributions $\psi_0 P^t$ for $t = 1, 2, \ldots$. +* The black dot is $\psi^*$. You might like to try experimenting with different initial conditions. From 34b0d72b35c289585c966b377cb9fcd2895bd8c9 Mon Sep 17 00:00:00 2001 From: Longye Tian Date: Wed, 17 Jul 2024 13:00:08 +1000 Subject: [PATCH 3/5] Update markov_chains_I.md --- lectures/markov_chains_I.md | 42 ++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/lectures/markov_chains_I.md b/lectures/markov_chains_I.md index 2c308ce2f..2ef93341c 100644 --- a/lectures/markov_chains_I.md +++ b/lectures/markov_chains_I.md @@ -812,7 +812,7 @@ P = np.array([[0.971, 0.029, 0.000], P @ P ``` -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$ +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$. First, we write a function to iterate the sequence of distributions for `ts_length` period @@ -829,26 +829,44 @@ def iterate_ψ(ψ_0, P, ts_length): Now we plot the sequence ```{code-cell} ipython3 -ψ_0 = (0.0, 0.2, 0.8) # Initial condition +ψ_1 = (0.0, 0.0, 1.0) +ψ_2 = (1.0, 0.0, 0.0) +ψ_3 = (0.0, 1.0, 0.0) # Three initial conditions +colors = ['blue','red', 'green'] # Different colors for each initial point + +# Define the vertices of the unit simplex +v = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0]]) + +# Define the faces of the unit simplex +faces = [ + [v[0], v[1], v[2]], + [v[0], v[1], v[3]], + [v[0], v[2], v[3]], + [v[1], v[2], v[3]] +] fig = plt.figure() ax = fig.add_subplot(projection='3d') -def update(n): - ψ_t = iterate_ψ(ψ_0, P, n+1) - +def update(n): ax.clear() ax.set_xlim([0, 1]) ax.set_ylim([0, 1]) ax.set_zlim([0, 1]) - ax.view_init(30, 210) + ax.view_init(45, 45) - for i, point in enumerate(ψ_t): - ax.scatter(point[0], point[1], point[2], color='r', s=60, alpha=(i+1)/len(ψ_t)) + simplex = Poly3DCollection(faces, alpha=0.03) + ax.add_collection3d(simplex) + for idx, ψ_0 in enumerate([ψ_1, ψ_2, ψ_3]): + ψ_t = iterate_ψ(ψ_0, P, n+1) + + for i, point in enumerate(ψ_t): + ax.scatter(point[0], point[1], point[2], color=colors[idx], 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) + ax.scatter(ψ_star[0], ψ_star[1], ψ_star[2], c='yellow', s=60) return fig, @@ -860,9 +878,9 @@ HTML(anim.to_jshtml()) Here * $P$ is the stochastic matrix for recession and growth {ref}`considered above `. -* The highest red dot is an arbitrarily chosen initial marginal probability distribution $\psi_0$, represented as a vector in $\mathbb R^3$. -* The other red dots are the marginal distributions $\psi_0 P^t$ for $t = 1, 2, \ldots$. -* The black dot is $\psi^*$. +* 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$. +* The transparent dots are the marginal distributions $\psi_i P^t$ for $t = 1, 2, \ldots$, for $i=1,2,3.$. +* The yellow dot is $\psi^*$. You might like to try experimenting with different initial conditions. From f695b08ae3ef9d420d360b555c36296db7f10e26 Mon Sep 17 00:00:00 2001 From: Longye Tian Date: Thu, 18 Jul 2024 20:25:20 +1000 Subject: [PATCH 4/5] Update markov_chains_I.md --- lectures/markov_chains_I.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lectures/markov_chains_I.md b/lectures/markov_chains_I.md index 2ef93341c..449fa8fa6 100644 --- a/lectures/markov_chains_I.md +++ b/lectures/markov_chains_I.md @@ -829,6 +829,8 @@ def iterate_ψ(ψ_0, P, ts_length): Now we plot the sequence ```{code-cell} ipython3 +:tags: hide-input + ψ_1 = (0.0, 0.0, 1.0) ψ_2 = (1.0, 0.0, 0.0) ψ_3 = (0.0, 1.0, 0.0) # Three initial conditions @@ -878,7 +880,7 @@ HTML(anim.to_jshtml()) Here * $P$ is the stochastic matrix for recession and growth {ref}`considered above `. -* 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$. +* 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$. * The transparent dots are the marginal distributions $\psi_i P^t$ for $t = 1, 2, \ldots$, for $i=1,2,3.$. * The yellow dot is $\psi^*$. @@ -917,6 +919,8 @@ We can see similar phenomena in higher dimensions. The next figure illustrates this for a periodic Markov chain with three states. ```{code-cell} ipython3 +:tags: hide-input + ψ_1 = (0.0, 0.0, 1.0) ψ_2 = (0.5, 0.5, 0.0) ψ_3 = (0.25, 0.25, 0.5) From 3c0b75fd388f0924c29bcf3147b0adf3157e5c9f Mon Sep 17 00:00:00 2001 From: Longye Tian Date: Thu, 18 Jul 2024 20:35:13 +1000 Subject: [PATCH 5/5] Update markov_chains_I.md --- lectures/markov_chains_I.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lectures/markov_chains_I.md b/lectures/markov_chains_I.md index 449fa8fa6..631dd70fb 100644 --- a/lectures/markov_chains_I.md +++ b/lectures/markov_chains_I.md @@ -829,7 +829,7 @@ def iterate_ψ(ψ_0, P, ts_length): Now we plot the sequence ```{code-cell} ipython3 -:tags: hide-input +:tags: [hide-input] ψ_1 = (0.0, 0.0, 1.0) ψ_2 = (1.0, 0.0, 0.0) @@ -919,7 +919,7 @@ We can see similar phenomena in higher dimensions. The next figure illustrates this for a periodic Markov chain with three states. ```{code-cell} ipython3 -:tags: hide-input +:tags: [hide-input] ψ_1 = (0.0, 0.0, 1.0) ψ_2 = (0.5, 0.5, 0.0)