Skip to content

Commit 51b2d5b

Browse files
HumphreyYangmmcky
andauthored
[markov chains] Use Consistent Math Notations (#108)
* clean up math notations * clean code to match math * revert one change in distribution definition * update according to feedback --------- Co-authored-by: mmcky <mamckay@gmail.com>
1 parent 9a3a5ac commit 51b2d5b

File tree

1 file changed

+46
-46
lines changed

1 file changed

+46
-46
lines changed

lectures/markov_chains.md

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ $$
289289
Going the other way, if we take a stochastic matrix $P$, we can generate a Markov
290290
chain $\{X_t\}$ as follows:
291291

292-
* draw $X_0$ from a distribution $\psi$ on $S$
292+
* draw $X_0$ from a distribution $\psi_0$ on $S$
293293
* for each $t = 0, 1, \ldots$, draw $X_{t+1}$ from $P(X_t,\cdot)$
294294

295295
By construction, the resulting process satisfies {eq}`mpp`.
@@ -317,11 +317,11 @@ In these exercises, we'll take the state space to be $S = 0,\ldots, n-1$.
317317
To simulate a Markov chain, we need
318318

319319
1. a stochastic matrix $P$ and
320-
1. a probability mass function $\psi$ of length $n$ from which to draw a realization of $X_0$.
320+
1. a probability mass function $\psi_0$ of length $n$ from which to draw a initial realization of $X_0$.
321321

322322
The Markov chain is then constructed as follows:
323323

324-
1. At time $t=0$, draw a realization of $X_0$ from the distribution $\psi$.
324+
1. At time $t=0$, draw a realization of $X_0$ from the distribution $\psi_0$.
325325
1. At each subsequent time $t$, draw a realization of the new state $X_{t+1}$ from $P(X_t, \cdot)$.
326326

327327
(That is, draw from row $X_t$ of $P$.)
@@ -335,8 +335,8 @@ To use `random.draw`, we first need to convert the probability mass function
335335
to a cumulative distribution
336336

337337
```{code-cell} ipython3
338-
ψ = (0.3, 0.7) # probabilities over {0, 1}
339-
cdf = np.cumsum(ψ) # convert into cumulative distribution
338+
ψ_0 = (0.3, 0.7) # probabilities over {0, 1}
339+
cdf = np.cumsum(ψ_0) # convert into cumulative distribution
340340
qe.random.draw(cdf, 5) # generate 5 independent draws from ψ
341341
```
342342

@@ -345,11 +345,11 @@ qe.random.draw(cdf, 5) # generate 5 independent draws from ψ
345345
We'll write our code as a function that accepts the following three arguments
346346

347347
* A stochastic matrix `P`
348-
* An initial distribution `ψ`
348+
* An initial distribution `ψ_0`
349349
* A positive integer `ts_length` representing the length of the time series the function should return
350350

351351
```{code-cell} ipython3
352-
def mc_sample_path(P, ψ=None, ts_length=1_000):
352+
def mc_sample_path(P, ψ_0=None, ts_length=1_000):
353353
354354
# set up
355355
P = np.asarray(P)
@@ -360,8 +360,8 @@ def mc_sample_path(P, ψ=None, ts_length=1_000):
360360
P_dist = np.cumsum(P, axis=1) # Convert rows into cdfs
361361
362362
# draw initial state, defaulting to 0
363-
if ψ is not None:
364-
X_0 = qe.random.draw(np.cumsum(ψ))
363+
if ψ_0 is not None:
364+
X_0 = qe.random.draw(np.cumsum(ψ_0))
365365
else:
366366
X_0 = 0
367367
@@ -387,7 +387,7 @@ P = [[0.4, 0.6],
387387
Here's a short time series.
388388

389389
```{code-cell} ipython3
390-
mc_sample_path(P, ψ=[1.0, 0.0], ts_length=10)
390+
mc_sample_path(P, ψ_0=[1.0, 0.0], ts_length=10)
391391
```
392392

393393
+++ {"user_expressions": []}
@@ -403,7 +403,7 @@ $X_0$ is drawn.
403403
The following code illustrates this
404404

405405
```{code-cell} ipython3
406-
X = mc_sample_path(P, ψ=[0.1, 0.9], ts_length=1_000_000)
406+
X = mc_sample_path(P, ψ_0=[0.1, 0.9], ts_length=1_000_000)
407407
np.mean(X == 0)
408408
```
409409

@@ -475,7 +475,7 @@ mc.simulate_indices(ts_length=4)
475475
(mc_md)=
476476
## Distributions over Time
477477

478-
Suppose that
478+
We learnt that
479479

480480
1. $\{X_t\}$ is a Markov chain with stochastic matrix $P$
481481
1. the distribution of $X_t$ is known to be $\psi_t$
@@ -581,12 +581,12 @@ Recall the stochastic matrix $P$ for recession and growth {ref}`considered above
581581

582582
Suppose that the current state is unknown --- perhaps statistics are available only at the *end* of the current month.
583583

584-
We guess that the probability that the economy is in state $x$ is $\psi(x)$.
584+
We guess that the probability that the economy is in state $x$ is $\psi_t(x)$ at time t.
585585

586586
The probability of being in recession (either mild or severe) in 6 months time is given by
587587

588588
$$
589-
(\psi P^6)(1) + (\psi P^6)(2)
589+
(\psi_t P^6)(1) + (\psi_t P^6)(2)
590590
$$
591591

592592
+++ {"user_expressions": []}
@@ -606,26 +606,26 @@ described by the specified dynamics, with each worker's outcomes being
606606
realizations of processes that are statistically independent of all other
607607
workers' processes.
608608

609-
Let $\psi$ be the current *cross-sectional* distribution over $\{ 0, 1 \}$.
609+
Let $\psi_t$ be the current *cross-sectional* distribution over $\{ 0, 1 \}$.
610610

611-
The cross-sectional distribution records fractions of workers employed and unemployed at a given moment.
611+
The cross-sectional distribution records fractions of workers employed and unemployed at a given moment t.
612612

613-
* For example, $\psi(0)$ is the unemployment rate.
613+
* For example, $\psi_t(0)$ is the unemployment rate.
614614

615615
What will the cross-sectional distribution be in 10 periods hence?
616616

617-
The answer is $\psi P^{10}$, where $P$ is the stochastic matrix in
617+
The answer is $\psi_t P^{10}$, where $P$ is the stochastic matrix in
618618
{eq}`p_unempemp`.
619619

620620
This is because each worker's state evolves according to $P$, so
621-
$\psi P^{10}$ is a marginal distribution for a single randomly selected
621+
$\psi_t P^{10}$ is a marginal distribution for a single randomly selected
622622
worker.
623623

624624
But when the sample is large, outcomes and probabilities are roughly equal (by an application of the Law
625625
of Large Numbers).
626626

627627
So for a very large (tending to infinite) population,
628-
$\psi P^{10}$ also represents fractions of workers in
628+
$\psi_t P^{10}$ also represents fractions of workers in
629629
each state.
630630

631631
This is exactly the cross-sectional distribution.
@@ -778,13 +778,13 @@ Notice that `ψ @ P` is the same as `ψ`
778778
Such distributions are called **stationary** or **invariant**.
779779

780780
(mc_stat_dd)=
781-
Formally, a distribution $\psi^*$ on $S$ is called **stationary** for $P$ if $\psi P = \psi $.
781+
Formally, a distribution $\psi^*$ on $S$ is called **stationary** for $P$ if $\psi^* P = \psi^* $.
782782

783-
Notice that, post-multiplying by $P$, we have $\psi P^2 = \psi P = \psi$.
783+
Notice that, post-multiplying by $P$, we have $\psi^* P^2 = \psi^* P = \psi^*$.
784784

785-
Continuing in the same way leads to $\psi = \psi P^t$ for all $t$.
785+
Continuing in the same way leads to $\psi^* = \psi^* P^t$ for all $t$.
786786

787-
This tells us an important fact: If the distribution of $X_0$ is a stationary distribution, then $X_t$ will have this same distribution for all $t$.
787+
This tells us an important fact: If the distribution of $\psi_0$ is a stationary distribution, then $\psi_t$ will have this same distribution for all $t$.
788788

789789
The following theorem is proved in Chapter 4 of {cite}`sargent2023economic` and numerous other sources.
790790

@@ -1065,19 +1065,19 @@ P @ P
10651065

10661066
+++ {"user_expressions": []}
10671067

1068-
Let's pick an initial distribution $\psi$ and trace out the sequence of distributions $\psi P^t$.
1068+
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$
10691069

1070-
First, we write a function to iterate the sequence of distributions for `n` period
1070+
First, we write a function to iterate the sequence of distributions for `ts_length` period
10711071

10721072
```{code-cell} ipython3
10731073
def iterate_ψ(ψ_0, P, ts_length):
10741074
n = len(P)
1075-
ψs = np.empty((ts_length, n))
1075+
ψ_t = np.empty((ts_length, n))
10761076
ψ = ψ_0
10771077
for t in range(ts_length):
1078-
ψs[t] = ψ
1078+
ψ_t[t] = ψ
10791079
ψ = ψ @ P
1080-
return np.array(ψs)
1080+
return np.array(ψ_t)
10811081
```
10821082

10831083
Now we plot the sequence
@@ -1093,9 +1093,9 @@ ax.set(xlim=(0, 1), ylim=(0, 1), zlim=(0, 1),
10931093
yticks=(0.25, 0.5, 0.75),
10941094
zticks=(0.25, 0.5, 0.75))
10951095
1096-
ψs = iterate_ψ(ψ_0, P, 20)
1096+
ψ_t = iterate_ψ(ψ_0, P, 20)
10971097
1098-
ax.scatter(ψs[:,0], ψs[:,1], ψs[:,2], c='r', s=60)
1098+
ax.scatter(ψ_t[:,0], ψ_t[:,1], ψ_t[:,2], c='r', s=60)
10991099
ax.view_init(30, 210)
11001100
11011101
mc = qe.MarkovChain(P)
@@ -1105,13 +1105,13 @@ ax.scatter(ψ_star[0], ψ_star[1], ψ_star[2], c='k', s=60)
11051105
plt.show()
11061106
```
11071107

1108-
+++ {"user_expressions": []}
1108+
+++ {"user_expressions": [], "tags": []}
11091109

11101110
Here
11111111

11121112
* $P$ is the stochastic matrix for recession and growth {ref}`considered above <mc_eg2>`.
1113-
* The highest red dot is an arbitrarily chosen initial marginal probability distribution $\psi$, represented as a vector in $\mathbb R^3$.
1114-
* The other red dots are the marginal distributions $\psi P^t$ for $t = 1, 2, \ldots$.
1113+
* The highest red dot is an arbitrarily chosen initial marginal probability distribution $\psi_0$, represented as a vector in $\mathbb R^3$.
1114+
* The other red dots are the marginal distributions $\psi_0 P^t$ for $t = 1, 2, \ldots$.
11151115
* The black dot is $\psi^*$.
11161116

11171117
You might like to try experimenting with different initial conditions.
@@ -1121,9 +1121,7 @@ You might like to try experimenting with different initial conditions.
11211121

11221122
We can show this in a slightly different way by focusing on the probability that $\psi_t$ puts on each state.
11231123

1124-
The following figure shows the dynamics of $(\psi P^t)(i)$ as $t$ gets large, for each state $i$.
1125-
1126-
First, we write a function to draw `n` initial values
1124+
First, we write a function to draw initial distributions $\psi_0$ of size `num_distributions`
11271125

11281126
```{code-cell} ipython3
11291127
def generate_initial_values(num_distributions, n):
@@ -1139,6 +1137,8 @@ def generate_initial_values(num_distributions, n):
11391137
return ψ_0s
11401138
```
11411139

1140+
The following figure shows the dynamics of $(\psi_0 P^t)(i)$ as $t$ gets large, for each state $i$ with different initial distributions
1141+
11421142
```{code-cell} ipython3
11431143
# Define the number of iterations
11441144
# and initial distributions
@@ -1155,23 +1155,23 @@ plt.subplots_adjust(wspace=0.35)
11551155
11561156
ψ_0s = generate_initial_values(num_distributions, n)
11571157
for ψ_0 in ψ_0s:
1158-
ψs = iterate_ψ(ψ_0, P, ts_length)
1158+
ψ_t = iterate_ψ(ψ_0, P, ts_length)
11591159
11601160
# Obtain and plot distributions at each state
11611161
for i in range(n):
1162-
axes[i].plot(range(0, ts_length), ψs[:,i], alpha=0.3)
1162+
axes[i].plot(range(0, ts_length), ψ_t[:,i], alpha=0.3)
11631163
11641164
for i in range(n):
11651165
axes[i].axhline(ψ_star[i], linestyle='dashed', lw=2, color = 'black',
11661166
label = fr'$\psi^*({i})$')
11671167
axes[i].set_xlabel('t')
1168-
axes[i].set_ylabel(fr'$\psi({i})$')
1168+
axes[i].set_ylabel(fr'$\psi_t({i})$')
11691169
axes[i].legend()
11701170
11711171
plt.show()
11721172
```
11731173

1174-
The convergence to $\psi^*$ holds for different initial values.
1174+
The convergence to $\psi^*$ holds for different initial distributions.
11751175

11761176
+++ {"user_expressions": []}
11771177

@@ -1184,25 +1184,25 @@ In the case of our periodic chain, we find the distribution is oscillating
11841184
P = np.array([[0, 1],
11851185
[1, 0]])
11861186
1187-
ts_length = 50
1188-
num_distributions = 25
1187+
ts_length = 20
1188+
num_distributions = 30
11891189
n = len(P)
11901190
mc = qe.MarkovChain(P)
11911191
ψ_star = mc.stationary_distributions[0]
11921192
fig, axes = plt.subplots(nrows=1, ncols=n)
11931193
11941194
ψ_0s = generate_initial_values(num_distributions, n)
11951195
for ψ_0 in ψ_0s:
1196-
ψs = iterate_ψ(ψ_0, P, ts_length)
1196+
ψ_t = iterate_ψ(ψ_0, P, ts_length)
11971197
11981198
# Obtain and plot distributions at each state
11991199
for i in range(n):
1200-
axes[i].plot(range(20, ts_length), ψs[20:,i], alpha=0.3)
1200+
axes[i].plot(range(ts_length), ψ_t[:,i], alpha=0.3)
12011201
12021202
for i in range(n):
12031203
axes[i].axhline(ψ_star[i], linestyle='dashed', lw=2, color = 'black', label = fr'$\psi^*({i})$')
12041204
axes[i].set_xlabel('t')
1205-
axes[i].set_ylabel(fr'$\psi({i})$')
1205+
axes[i].set_ylabel(fr'$\psi_t({i})$')
12061206
axes[i].legend()
12071207
12081208
plt.show()

0 commit comments

Comments
 (0)