Skip to content

Commit 7924eb0

Browse files
committed
misc
1 parent eff5342 commit 7924eb0

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

lectures/markov_chains.md

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@ Notice that the theorem is related to the law of large numbers.
867867
TODO -- link to our undergrad lln and clt lecture
868868

869869
It tells us that, in some settings, the law of large numbers sometimes holds even when the
870-
sequence of random variables is not IID.
870+
sequence of random variables is [not IID](iid_violation).
871871

872872

873873
(mc_eg1-2)=
@@ -912,15 +912,15 @@ n_state = P.shape[1]
912912
fig, axes = plt.subplots(nrows=1, ncols=n_state)
913913
ψ_star = mc.stationary_distributions[0]
914914
plt.subplots_adjust(wspace=0.35)
915+
915916
for i in range(n_state):
916917
axes[i].grid()
917-
axes[i].set_ylim(ψ_star[i]-0.2, ψ_star[i]+0.2)
918-
axes[i].axhline(ψ_star[i], linestyle='dashed', lw=2, color = 'black',
919-
label = fr'$\psi^*(X={i})$')
918+
axes[i].axhline(ψ_star[i], linestyle='dashed', lw=2, color = 'black',
919+
label = fr'$\psi^*({i})$')
920920
axes[i].set_xlabel('t')
921-
axes[i].set_ylabel(fr'average time spent at X={i}')
921+
axes[i].set_ylabel(f'fraction of time spent at {i}')
922922
923-
# Compute the fraction of time spent, for each X=x
923+
# Compute the fraction of time spent, starting from different x_0s
924924
for x0, col in ((0, 'blue'), (1, 'green'), (2, 'red')):
925925
# Generate time series that starts at different x0
926926
X = mc.simulate(n, init=x0)
@@ -949,6 +949,8 @@ $$
949949
The diagram of the Markov chain shows that it is **irreducible**
950950

951951
```{code-cell} ipython3
952+
:tags: [hide-input]
953+
952954
dot = Digraph(comment='Graph')
953955
dot.attr(rankdir='LR')
954956
dot.node("0")
@@ -976,15 +978,16 @@ mc = MarkovChain(P)
976978
n_state = P.shape[1]
977979
fig, axes = plt.subplots(nrows=1, ncols=n_state)
978980
ψ_star = mc.stationary_distributions[0]
981+
979982
for i in range(n_state):
980983
axes[i].grid()
981984
axes[i].set_ylim(0.45, 0.55)
982-
axes[i].axhline(ψ_star[i], linestyle='dashed', lw=2, color = 'black',
983-
label = fr'$\psi^*(X={i})$')
985+
axes[i].axhline(ψ_star[i], linestyle='dashed', lw=2, color = 'black',
986+
label = fr'$\psi^*({i})$')
984987
axes[i].set_xlabel('t')
985-
axes[i].set_ylabel(fr'average time spent at X={i}')
988+
axes[i].set_ylabel(f'fraction of time spent at {i}')
986989
987-
# Compute the fraction of time spent, for each X=x
990+
# Compute the fraction of time spent, for each x
988991
for x0 in range(n_state):
989992
# Generate time series starting at different x_0
990993
X = mc.simulate(n, init=x0)
@@ -1078,6 +1081,7 @@ In the case of Hamilton's Markov chain, the distribution $\psi P^t$ converges to
10781081
P = np.array([[0.971, 0.029, 0.000],
10791082
[0.145, 0.778, 0.077],
10801083
[0.000, 0.508, 0.492]])
1084+
10811085
# Define the number of iterations
10821086
n = 50
10831087
n_state = P.shape[0]
@@ -1097,8 +1101,8 @@ for i in range(n):
10971101
# Loop through many initial values
10981102
for x0 in x0s:
10991103
x = x0
1100-
X = np.zeros((n,n_state))
1101-
1104+
X = np.zeros((n, n_state))
1105+
11021106
# Obtain and plot distributions at each state
11031107
for t in range(0, n):
11041108
x = x @ P
@@ -1107,10 +1111,10 @@ for x0 in x0s:
11071111
axes[i].plot(range(0, n), X[:,i], alpha=0.3)
11081112
11091113
for i in range(n_state):
1110-
axes[i].axhline(ψ_star[i], linestyle='dashed', lw=2, color = 'black',
1111-
label = fr'$\psi^*(X={i})$')
1114+
axes[i].axhline(ψ_star[i], linestyle='dashed', lw=2, color = 'black',
1115+
label = fr'$\psi^*({i})$')
11121116
axes[i].set_xlabel('t')
1113-
axes[i].set_ylabel(fr'$\psi(X={i})$')
1117+
axes[i].set_ylabel(fr'$\psi({i})$')
11141118
axes[i].legend()
11151119
11161120
plt.show()
@@ -1147,9 +1151,9 @@ for x0 in x0s:
11471151
axes[i].plot(range(20, n), X[20:,i], alpha=0.3)
11481152
11491153
for i in range(n_state):
1150-
axes[i].axhline(ψ_star[i], linestyle='dashed', lw=2, color = 'black', label = fr'$\psi^* (X={i})$')
1154+
axes[i].axhline(ψ_star[i], linestyle='dashed', lw=2, color = 'black', label = fr'$\psi^*({i})$')
11511155
axes[i].set_xlabel('t')
1152-
axes[i].set_ylabel(fr'$\psi(X={i})$')
1156+
axes[i].set_ylabel(fr'$\psi({i})$')
11531157
axes[i].legend()
11541158
11551159
plt.show()
@@ -1295,7 +1299,7 @@ In this exercise,
12951299
12961300
1. show this process is asymptotically stationary and calculate the stationary distribution using simulations.
12971301
1298-
1. use simulation to show ergodicity.
1302+
1. use simulations to demonstrate ergodicity of this process.
12991303
13001304
````
13011305

@@ -1323,7 +1327,7 @@ codes_B = ( '1','2','3','4','5','6','7','8')
13231327
np.linalg.matrix_power(P_B, 10)
13241328
```
13251329

1326-
We find rows transition matrix converge to the stationary distribution
1330+
We find that rows of the transition matrix converge to the stationary distribution
13271331

13281332
```{code-cell} ipython3
13291333
mc = qe.MarkovChain(P_B)
@@ -1344,17 +1348,17 @@ ax.axhline(0, linestyle='dashed', lw=2, color = 'black', alpha=0.4)
13441348
13451349
13461350
for x0 in range(8):
1347-
# Calculate the average time for each worker
1351+
# Calculate the fraction of time for each worker
13481352
X_bar = (X == x0).cumsum() / (1 + np.arange(N, dtype=float))
13491353
ax.plot(X_bar - ψ_star[x0], label=f'$X = {x0+1} $')
13501354
ax.set_xlabel('t')
1351-
ax.set_ylabel(fr'average time spent in a state $- \psi^* (X=x)$')
1355+
ax.set_ylabel(r'fraction of time spent in a state $- \psi^* (x)$')
13521356
13531357
ax.legend()
13541358
plt.show()
13551359
```
13561360

1357-
We can see that the time spent at each state quickly converges to the stationary distribution.
1361+
Note that the fraction of time spent at each state quickly converges to the probability assigned to that state by the stationary distribution.
13581362

13591363
```{solution-end}
13601364
```
@@ -1452,7 +1456,7 @@ However, another way to verify irreducibility is by checking whether $A$ satisfi
14521456
14531457
Assume A is an $n \times n$ $A$ is irreducible if and only if $\sum_{k=0}^{n-1}A^k$ is a positive matrix.
14541458
1455-
(see more at \cite{zhao_power_2012} and [here](https://math.stackexchange.com/questions/3336616/how-to-prove-this-matrix-is-a-irreducible-matrix))
1459+
(see more: {cite}`zhao_power_2012` and [here](https://math.stackexchange.com/questions/3336616/how-to-prove-this-matrix-is-a-irreducible-matrix))
14561460
14571461
Based on this claim, write a function to test irreducibility.
14581462

0 commit comments

Comments
 (0)