Skip to content

Commit feebd22

Browse files
committed
update_markov_chains_I.md
This pull request updates lecture markov_chains_I.md. In particular, ## Content - The first exercise -> The last exercise. - Remove unused variables and shorten the visualization code for the network graph. - unbold future in Defiinig Markov Chains section. - study a Markov chains -> study Markov chains. - Use tuples whenever possible. - Give some comments on the code in the `qe` library. - Add a link for marginal probability $\psi_t(x)$. - Unify postmultiply (using postmultiply instead of post-multiply in lieu with wikipedia) - Add a sentence clarifying $P^m(x,y)$. - Law of Large Numbers -> law of large numbers. - Check the capitalization of subtitles. - for all $t$ -> for all $t \ge 0$. - $\psi_t(0)$ is employment at [time $t$]. - Delete the word 'Theorem' in the Theorem box. - Update the solution for exercise 1. ## Code - Update visualization code for the section on the Hamilton matrix.
1 parent da3422e commit feebd22

File tree

1 file changed

+31
-43
lines changed

1 file changed

+31
-43
lines changed

lectures/markov_chains_I.md

Lines changed: 31 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ 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+
Checking this in {ref}`the last exercise <mc1_ex_3>` below.
8686

8787

8888
### Markov chains
@@ -258,22 +258,20 @@ Here is a visualization, with darker colors indicating higher probability.
258258
:tags: [hide-input]
259259
260260
G = nx.MultiDiGraph()
261-
edge_ls = []
262-
label_dict = {}
263261
264262
for start_idx, node_start in enumerate(nodes):
265263
for end_idx, node_end in enumerate(nodes):
266264
value = P[start_idx][end_idx]
267265
if value != 0:
268-
G.add_edge(node_start,node_end, weight=value, len=100)
266+
G.add_edge(node_start,node_end, weight=value)
269267
270268
pos = nx.spring_layout(G, seed=10)
271269
fig, ax = plt.subplots()
272270
nx.draw_networkx_nodes(G, pos, node_size=600, edgecolors='black', node_color='white')
273271
nx.draw_networkx_labels(G, pos)
274272
275273
arc_rad = 0.2
276-
curved_edges = [edge for edge in G.edges()]
274+
277275
edges = nx.draw_networkx_edges(G, pos, ax=ax, connectionstyle=f'arc3, rad = {arc_rad}', edge_cmap=cm.Blues, width=2,
278276
edge_color=[G[nodes[0]][nodes[1]][0]['weight'] for nodes in G.edges])
279277
@@ -317,7 +315,7 @@ This means that, for any date $t$ and any state $y \in S$,
317315
= \mathbb P \{ X_{t+1} = y \,|\, X_t, X_{t-1}, \ldots \}
318316
```
319317

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.
318+
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.
321319

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

@@ -356,7 +354,7 @@ By construction, the resulting process satisfies {eq}`mpp`.
356354
```{index} single: Markov Chains; Simulation
357355
```
358356

359-
A good way to study a Markov chains is to simulate it.
357+
A good way to study Markov chains is to simulate it.
360358

361359
Let's start by doing this ourselves and then look at libraries that can help
362360
us.
@@ -434,7 +432,7 @@ P = [[0.4, 0.6],
434432
Here's a short time series.
435433

436434
```{code-cell} ipython3
437-
mc_sample_path(P, ψ_0=[1.0, 0.0], ts_length=10)
435+
mc_sample_path(P, ψ_0=(1.0, 0.0), ts_length=10)
438436
```
439437

440438
It can be shown that for a long series drawn from `P`, the fraction of the
@@ -448,7 +446,7 @@ $X_0$ is drawn.
448446
The following code illustrates this
449447

450448
```{code-cell} ipython3
451-
X = mc_sample_path(P, ψ_0=[0.1, 0.9], ts_length=1_000_000)
449+
X = mc_sample_path(P, ψ_0=(0.1, 0.9), ts_length=1_000_000)
452450
np.mean(X == 0)
453451
```
454452

@@ -488,11 +486,11 @@ The following code illustrates
488486

489487
```{code-cell} ipython3
490488
mc = qe.MarkovChain(P, state_values=('unemployed', 'employed'))
491-
mc.simulate(ts_length=4, init='employed')
489+
mc.simulate(ts_length=4, init='employed'). # Start at employed initial state
492490
```
493491

494492
```{code-cell} ipython3
495-
mc.simulate(ts_length=4, init='unemployed')
493+
mc.simulate(ts_length=4, init='unemployed') # Start at unemployed initial state
496494
```
497495

498496
```{code-cell} ipython3
@@ -570,7 +568,7 @@ This is very important, so let's repeat it
570568
X_0 \sim \psi_0 \quad \implies \quad X_m \sim \psi_0 P^m
571569
```
572570

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

575573
Hence the following is also valid.
576574

@@ -625,12 +623,12 @@ $$
625623

626624

627625
(mc_eg1-1)=
628-
### Example 2: Cross-sectional distributions
626+
### Example 2: cross-sectional distributions
629627

630628
The distributions we have been studying can be viewed either
631629

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

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

@@ -641,21 +639,21 @@ workers' processes.
641639

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

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

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

648646
What will the cross-sectional distribution be in 10 periods hence?
649647

650648
The answer is $\psi_t P^{10}$, where $P$ is the stochastic matrix in
651649
{eq}`p_unempemp`.
652650

653651
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
652+
$\psi_t P^{10}$ is a [marginal distribution](https://en.wikipedia.org/wiki/Marginal_distribution) for a single randomly selected
655653
worker.
656654

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

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

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

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

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$.
693+
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$.
696694

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

@@ -767,7 +765,7 @@ For example, we have the following result
767765

768766
(strict_stationary)=
769767
```{prf:theorem}
770-
Theorem: If there exists an integer $m$ such that all entries of $P^m$ are
768+
If there exists an integer $m$ such that all entries of $P^m$ are
771769
strictly positive, with unique stationary distribution $\psi^*$, then
772770
773771
$$
@@ -801,11 +799,10 @@ First, we write a function to iterate the sequence of distributions for `ts_leng
801799
def iterate_ψ(ψ_0, P, ts_length):
802800
n = len(P)
803801
ψ_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)
802+
ψ_t[0 ]= ψ_0
803+
for t in range(1, ts_length):
804+
ψ_t[t] = ψ_t[t-1] @ P
805+
return ψ_t
809806
```
810807

811808
Now we plot the sequence
@@ -814,12 +811,7 @@ Now we plot the sequence
814811
ψ_0 = (0.0, 0.2, 0.8) # Initial condition
815812
816813
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))
814+
ax = fig.add_subplot(projection='3d')
823815
824816
ψ_t = iterate_ψ(ψ_0, P, 20)
825817
@@ -852,13 +844,9 @@ First, we write a function to draw initial distributions $\psi_0$ of size `num_d
852844
```{code-cell} ipython3
853845
def generate_initial_values(num_distributions):
854846
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))
847+
848+
draws = np.random.randint(1, 10_000_000, size=(num_distributions,n))
849+
ψ_0s = draws/draws.sum(axis=1)[:, None]
862850
863851
return ψ_0s
864852
```
@@ -917,7 +905,7 @@ The convergence to $\psi^*$ holds for different initial distributions.
917905

918906

919907

920-
#### Example: Failure of convergence
908+
#### Example: failure of convergence
921909

922910

923911
In the case of a periodic chain, with
@@ -1077,7 +1065,7 @@ Solution 1:
10771065
10781066
```
10791067

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

10821070
Solution 2:
10831071

0 commit comments

Comments
 (0)