Skip to content

Commit b24e737

Browse files
authored
Merge pull request #454 from QuantEcon/update_markov_chain_I
update_markov_chains_I.md
2 parents da3422e + b3fe3c9 commit b24e737

File tree

1 file changed

+34
-47
lines changed

1 file changed

+34
-47
lines changed

lectures/markov_chains_I.md

Lines changed: 34 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,15 @@ In other words,
8282

8383
If $P$ is a stochastic matrix, then so is the $k$-th power $P^k$ for all $k \in \mathbb N$.
8484

85-
Checking this in {ref}`the first exercises <mc1_ex_3>` below.
85+
You are asked to check this in {ref}`an exercise <mc1_ex_3>` below.
8686

8787

8888
### Markov chains
89+
8990
Now we can introduce Markov chains.
9091

9192
Before defining a Markov chain rigorously, we'll give some examples.
9293

93-
(Among other things, defining a Markov chain will clarify a connection between **stochastic matrices** and **Markov chains**.)
94-
9594

9695
(mc_eg2)=
9796
#### Example 1
@@ -110,7 +109,7 @@ Here there are three **states**
110109
* "mr" represents mild recession
111110
* "sr" represents severe recession
112111

113-
The arrows represent **transition probabilities** over one month.
112+
The arrows represent transition probabilities over one month.
114113

115114
For example, the arrow from mild recession to normal growth has 0.145 next to it.
116115

@@ -120,7 +119,7 @@ The arrow from normal growth back to normal growth tells us that there is a
120119
97% probability of transitioning from normal growth to normal growth (staying
121120
in the same state).
122121

123-
Note that these are *conditional* probabilities --- the probability of
122+
Note that these are conditional probabilities --- the probability of
124123
transitioning from one state to another (or staying at the same one) conditional on the
125124
current state.
126125

@@ -258,22 +257,20 @@ Here is a visualization, with darker colors indicating higher probability.
258257
:tags: [hide-input]
259258
260259
G = nx.MultiDiGraph()
261-
edge_ls = []
262-
label_dict = {}
263260
264261
for start_idx, node_start in enumerate(nodes):
265262
for end_idx, node_end in enumerate(nodes):
266263
value = P[start_idx][end_idx]
267264
if value != 0:
268-
G.add_edge(node_start,node_end, weight=value, len=100)
265+
G.add_edge(node_start,node_end, weight=value)
269266
270267
pos = nx.spring_layout(G, seed=10)
271268
fig, ax = plt.subplots()
272269
nx.draw_networkx_nodes(G, pos, node_size=600, edgecolors='black', node_color='white')
273270
nx.draw_networkx_labels(G, pos)
274271
275272
arc_rad = 0.2
276-
curved_edges = [edge for edge in G.edges()]
273+
277274
edges = nx.draw_networkx_edges(G, pos, ax=ax, connectionstyle=f'arc3, rad = {arc_rad}', edge_cmap=cm.Blues, width=2,
278275
edge_color=[G[nodes[0]][nodes[1]][0]['weight'] for nodes in G.edges])
279276
@@ -317,7 +314,7 @@ This means that, for any date $t$ and any state $y \in S$,
317314
= \mathbb P \{ X_{t+1} = y \,|\, X_t, X_{t-1}, \ldots \}
318315
```
319316

320-
This means that once we know the current state $X_t$, adding knowledge of earlier states $X_{t-1}, X_{t-2}$ provides no additional information about probabilities of **future** states.
317+
This means that once we know the current state $X_t$, adding knowledge of earlier states $X_{t-1}, X_{t-2}$ provides no additional information about probabilities of *future* states.
321318

322319
Thus, the dynamics of a Markov chain are fully determined by the set of **conditional probabilities**
323320

@@ -356,7 +353,7 @@ By construction, the resulting process satisfies {eq}`mpp`.
356353
```{index} single: Markov Chains; Simulation
357354
```
358355

359-
A good way to study a Markov chains is to simulate it.
356+
A good way to study Markov chains is to simulate them.
360357

361358
Let's start by doing this ourselves and then look at libraries that can help
362359
us.
@@ -434,7 +431,7 @@ P = [[0.4, 0.6],
434431
Here's a short time series.
435432

436433
```{code-cell} ipython3
437-
mc_sample_path(P, ψ_0=[1.0, 0.0], ts_length=10)
434+
mc_sample_path(P, ψ_0=(1.0, 0.0), ts_length=10)
438435
```
439436

440437
It can be shown that for a long series drawn from `P`, the fraction of the
@@ -448,7 +445,7 @@ $X_0$ is drawn.
448445
The following code illustrates this
449446

450447
```{code-cell} ipython3
451-
X = mc_sample_path(P, ψ_0=[0.1, 0.9], ts_length=1_000_000)
448+
X = mc_sample_path(P, ψ_0=(0.1, 0.9), ts_length=1_000_000)
452449
np.mean(X == 0)
453450
```
454451

@@ -488,11 +485,11 @@ The following code illustrates
488485

489486
```{code-cell} ipython3
490487
mc = qe.MarkovChain(P, state_values=('unemployed', 'employed'))
491-
mc.simulate(ts_length=4, init='employed')
488+
mc.simulate(ts_length=4, init='employed') # Start at employed initial state
492489
```
493490

494491
```{code-cell} ipython3
495-
mc.simulate(ts_length=4, init='unemployed')
492+
mc.simulate(ts_length=4, init='unemployed') # Start at unemployed initial state
496493
```
497494

498495
```{code-cell} ipython3
@@ -570,7 +567,7 @@ This is very important, so let's repeat it
570567
X_0 \sim \psi_0 \quad \implies \quad X_m \sim \psi_0 P^m
571568
```
572569

573-
The general rule is that post-multiplying a distribution by $P^m$ shifts it forward $m$ units of time.
570+
The general rule is that postmultiplying a distribution by $P^m$ shifts it forward $m$ units of time.
574571

575572
Hence the following is also valid.
576573

@@ -625,12 +622,12 @@ $$
625622

626623

627624
(mc_eg1-1)=
628-
### Example 2: Cross-sectional distributions
625+
### Example 2: cross-sectional distributions
629626

630627
The distributions we have been studying can be viewed either
631628

632629
1. as probabilities or
633-
1. as cross-sectional frequencies that the Law of Large Numbers leads us to anticipate for large samples.
630+
1. as cross-sectional frequencies that the law of large numbers leads us to anticipate for large samples.
634631

635632
To illustrate, recall our model of employment/unemployment dynamics for a given worker {ref}`discussed above <mc_eg1>`.
636633

@@ -641,21 +638,21 @@ workers' processes.
641638

642639
Let $\psi_t$ be the current *cross-sectional* distribution over $\{ 0, 1 \}$.
643640

644-
The cross-sectional distribution records fractions of workers employed and unemployed at a given moment t.
641+
The cross-sectional distribution records fractions of workers employed and unemployed at a given moment $t$.
645642

646-
* For example, $\psi_t(0)$ is the unemployment rate.
643+
* For example, $\psi_t(0)$ is the unemployment rate at time $t$.
647644

648645
What will the cross-sectional distribution be in 10 periods hence?
649646

650647
The answer is $\psi_t P^{10}$, where $P$ is the stochastic matrix in
651648
{eq}`p_unempemp`.
652649

653650
This is because each worker's state evolves according to $P$, so
654-
$\psi_t P^{10}$ is a marginal distribution for a single randomly selected
651+
$\psi_t P^{10}$ is a [marginal distribution](https://en.wikipedia.org/wiki/Marginal_distribution) for a single randomly selected
655652
worker.
656653

657-
But when the sample is large, outcomes and probabilities are roughly equal (by an application of the Law
658-
of Large Numbers).
654+
But when the sample is large, outcomes and probabilities are roughly equal (by an application of the law
655+
of large numbers).
659656

660657
So for a very large (tending to infinite) population,
661658
$\psi_t P^{10}$ also represents fractions of workers in
@@ -688,11 +685,11 @@ Such distributions are called **stationary** or **invariant**.
688685
(mc_stat_dd)=
689686
Formally, a distribution $\psi^*$ on $S$ is called **stationary** for $P$ if $\psi^* P = \psi^* $.
690687

691-
Notice that, post-multiplying by $P$, we have $\psi^* P^2 = \psi^* P = \psi^*$.
688+
Notice that, postmultiplying by $P$, we have $\psi^* P^2 = \psi^* P = \psi^*$.
692689

693-
Continuing in the same way leads to $\psi^* = \psi^* P^t$ for all $t$.
690+
Continuing in the same way leads to $\psi^* = \psi^* P^t$ for all $t \ge 0$.
694691

695-
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$.
692+
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 \ge 0$.
696693

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

@@ -767,7 +764,7 @@ For example, we have the following result
767764

768765
(strict_stationary)=
769766
```{prf:theorem}
770-
Theorem: If there exists an integer $m$ such that all entries of $P^m$ are
767+
If there exists an integer $m$ such that all entries of $P^m$ are
771768
strictly positive, with unique stationary distribution $\psi^*$, then
772769
773770
$$
@@ -801,11 +798,10 @@ First, we write a function to iterate the sequence of distributions for `ts_leng
801798
def iterate_ψ(ψ_0, P, ts_length):
802799
n = len(P)
803800
ψ_t = np.empty((ts_length, n))
804-
ψ = ψ_0
805-
for t in range(ts_length):
806-
ψ_t[t] = ψ
807-
ψ = ψ @ P
808-
return np.array(ψ_t)
801+
ψ_t[0 ]= ψ_0
802+
for t in range(1, ts_length):
803+
ψ_t[t] = ψ_t[t-1] @ P
804+
return ψ_t
809805
```
810806

811807
Now we plot the sequence
@@ -814,12 +810,7 @@ Now we plot the sequence
814810
ψ_0 = (0.0, 0.2, 0.8) # Initial condition
815811
816812
fig = plt.figure()
817-
ax = fig.add_subplot(111, projection='3d')
818-
819-
ax.set(xlim=(0, 1), ylim=(0, 1), zlim=(0, 1),
820-
xticks=(0.25, 0.5, 0.75),
821-
yticks=(0.25, 0.5, 0.75),
822-
zticks=(0.25, 0.5, 0.75))
813+
ax = fig.add_subplot(projection='3d')
823814
824815
ψ_t = iterate_ψ(ψ_0, P, 20)
825816
@@ -852,13 +843,9 @@ First, we write a function to draw initial distributions $\psi_0$ of size `num_d
852843
```{code-cell} ipython3
853844
def generate_initial_values(num_distributions):
854845
n = len(P)
855-
ψ_0s = np.empty((num_distributions, n))
856-
857-
for i in range(num_distributions):
858-
draws = np.random.randint(1, 10_000_000, size=n)
859-
860-
# Scale them so that they add up into 1
861-
ψ_0s[i,:] = np.array(draws/sum(draws))
846+
847+
draws = np.random.randint(1, 10_000_000, size=(num_distributions,n))
848+
ψ_0s = draws/draws.sum(axis=1)[:, None]
862849
863850
return ψ_0s
864851
```
@@ -917,7 +904,7 @@ The convergence to $\psi^*$ holds for different initial distributions.
917904

918905

919906

920-
#### Example: Failure of convergence
907+
#### Example: failure of convergence
921908

922909

923910
In the case of a periodic chain, with
@@ -1077,7 +1064,7 @@ Solution 1:
10771064
10781065
```
10791066

1080-
Since the matrix is everywhere positive, there is a unique stationary distribution.
1067+
Since the matrix is everywhere positive, there is a unique stationary distribution $\psi^*$ such that $\psi_t\to \psi^*$ as $t\to \infty$.
10811068

10821069
Solution 2:
10831070

0 commit comments

Comments
 (0)