Skip to content

Commit 338a4a4

Browse files
authored
Reformat the Exercise and Solution (#271)
* update formats * temporarily change ci * set ci back to PR
1 parent 1ec45f4 commit 338a4a4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1284
-1377
lines changed

lectures/ar1_processes.md

Lines changed: 88 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,52 @@ Confirm this by simulation at a range of $k$ using the default parameters from t
359359
```
360360

361361

362+
```{solution-start} ar1p_ex1
363+
:class: dropdown
364+
```
365+
366+
Here is one solution:
367+
368+
```{code-cell} python3
369+
from numba import njit
370+
from scipy.special import factorial2
371+
372+
@njit
373+
def sample_moments_ar1(k, m=100_000, mu_0=0.0, sigma_0=1.0, seed=1234):
374+
np.random.seed(seed)
375+
sample_sum = 0.0
376+
x = mu_0 + sigma_0 * np.random.randn()
377+
for t in range(m):
378+
sample_sum += (x - mu_star)**k
379+
x = a * x + b + c * np.random.randn()
380+
return sample_sum / m
381+
382+
def true_moments_ar1(k):
383+
if k % 2 == 0:
384+
return std_star**k * factorial2(k - 1)
385+
else:
386+
return 0
387+
388+
k_vals = np.arange(6) + 1
389+
sample_moments = np.empty_like(k_vals)
390+
true_moments = np.empty_like(k_vals)
391+
392+
for k_idx, k in enumerate(k_vals):
393+
sample_moments[k_idx] = sample_moments_ar1(k)
394+
true_moments[k_idx] = true_moments_ar1(k)
395+
396+
fig, ax = plt.subplots()
397+
ax.plot(k_vals, true_moments, label="true moments")
398+
ax.plot(k_vals, sample_moments, label="sample moments")
399+
ax.legend()
400+
401+
plt.show()
402+
```
403+
404+
```{solution-end}
405+
```
406+
407+
362408
```{exercise}
363409
:label: ar1p_ex2
364410
@@ -405,95 +451,6 @@ of these distributions?)
405451
```
406452

407453

408-
```{exercise}
409-
:label: ar1p_ex3
410-
411-
In the lecture we discussed the following fact: for the $AR(1)$ process
412-
413-
$$
414-
X_{t+1} = a X_t + b + c W_{t+1}
415-
$$
416-
417-
with $\{ W_t \}$ iid and standard normal,
418-
419-
$$
420-
\psi_t = N(\mu, s^2) \implies \psi_{t+1}
421-
= N(a \mu + b, a^2 s^2 + c^2)
422-
$$
423-
424-
Confirm this, at least approximately, by simulation. Let
425-
426-
- $a = 0.9$
427-
- $b = 0.0$
428-
- $c = 0.1$
429-
- $\mu = -3$
430-
- $s = 0.2$
431-
432-
First, plot $\psi_t$ and $\psi_{t+1}$ using the true
433-
distributions described above.
434-
435-
Second, plot $\psi_{t+1}$ on the same figure (in a different
436-
color) as follows:
437-
438-
1. Generate $n$ draws of $X_t$ from the $N(\mu, s^2)$
439-
distribution
440-
1. Update them all using the rule
441-
$X_{t+1} = a X_t + b + c W_{t+1}$
442-
1. Use the resulting sample of $X_{t+1}$ values to produce a
443-
density estimate via kernel density estimation.
444-
445-
Try this for $n=2000$ and confirm that the
446-
simulation based estimate of $\psi_{t+1}$ does converge to the
447-
theoretical distribution.
448-
```
449-
450-
451-
## Solutions
452-
453-
```{solution-start} ar1p_ex1
454-
:class: dropdown
455-
```
456-
457-
```{code-cell} python3
458-
from numba import njit
459-
from scipy.special import factorial2
460-
461-
@njit
462-
def sample_moments_ar1(k, m=100_000, mu_0=0.0, sigma_0=1.0, seed=1234):
463-
np.random.seed(seed)
464-
sample_sum = 0.0
465-
x = mu_0 + sigma_0 * np.random.randn()
466-
for t in range(m):
467-
sample_sum += (x - mu_star)**k
468-
x = a * x + b + c * np.random.randn()
469-
return sample_sum / m
470-
471-
def true_moments_ar1(k):
472-
if k % 2 == 0:
473-
return std_star**k * factorial2(k - 1)
474-
else:
475-
return 0
476-
477-
k_vals = np.arange(6) + 1
478-
sample_moments = np.empty_like(k_vals)
479-
true_moments = np.empty_like(k_vals)
480-
481-
for k_idx, k in enumerate(k_vals):
482-
sample_moments[k_idx] = sample_moments_ar1(k)
483-
true_moments[k_idx] = true_moments_ar1(k)
484-
485-
fig, ax = plt.subplots()
486-
ax.plot(k_vals, true_moments, label="true moments")
487-
ax.plot(k_vals, sample_moments, label="sample moments")
488-
ax.legend()
489-
490-
plt.show()
491-
```
492-
493-
```{solution-end}
494-
```
495-
496-
497454
```{solution-start} ar1p_ex2
498455
:class: dropdown
499456
```
@@ -553,6 +510,48 @@ distribution is smooth but less so otherwise.
553510
```
554511

555512

513+
```{exercise}
514+
:label: ar1p_ex3
515+
516+
In the lecture we discussed the following fact: for the $AR(1)$ process
517+
518+
$$
519+
X_{t+1} = a X_t + b + c W_{t+1}
520+
$$
521+
522+
with $\{ W_t \}$ iid and standard normal,
523+
524+
$$
525+
\psi_t = N(\mu, s^2) \implies \psi_{t+1}
526+
= N(a \mu + b, a^2 s^2 + c^2)
527+
$$
528+
529+
Confirm this, at least approximately, by simulation. Let
530+
531+
- $a = 0.9$
532+
- $b = 0.0$
533+
- $c = 0.1$
534+
- $\mu = -3$
535+
- $s = 0.2$
536+
537+
First, plot $\psi_t$ and $\psi_{t+1}$ using the true
538+
distributions described above.
539+
540+
Second, plot $\psi_{t+1}$ on the same figure (in a different
541+
color) as follows:
542+
543+
1. Generate $n$ draws of $X_t$ from the $N(\mu, s^2)$
544+
distribution
545+
1. Update them all using the rule
546+
$X_{t+1} = a X_t + b + c W_{t+1}$
547+
1. Use the resulting sample of $X_{t+1}$ values to produce a
548+
density estimate via kernel density estimation.
549+
550+
Try this for $n=2000$ and confirm that the
551+
simulation based estimate of $\psi_{t+1}$ does converge to the
552+
theoretical distribution.
553+
```
554+
556555
```{solution-start} ar1p_ex3
557556
:class: dropdown
558557
```

lectures/cake_eating_numerical.md

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -502,17 +502,6 @@ Make the required changes to value function iteration code and plot the value an
502502
Try to reuse as much code as possible.
503503
```
504504

505-
506-
```{exercise}
507-
:label: cen_ex2
508-
509-
Implement time iteration, returning to the original case (i.e., dropping the
510-
modification in the exercise above).
511-
```
512-
513-
514-
## Solutions
515-
516505
```{solution-start} cen_ex1
517506
:class: dropdown
518507
```
@@ -593,6 +582,14 @@ Consumption is higher when $\alpha < 1$ because, at least for large $x$, the ret
593582
```
594583

595584

585+
```{exercise}
586+
:label: cen_ex2
587+
588+
Implement time iteration, returning to the original case (i.e., dropping the
589+
modification in the exercise above).
590+
```
591+
592+
596593
```{solution-start} cen_ex2
597594
:class: dropdown
598595
```

lectures/cake_eating_problem.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,8 +526,6 @@ In doing so, you will need to use the definition of the value function and the
526526
Bellman equation.
527527
```
528528

529-
## Solutions
530-
531529
```{solution} cep_ex1
532530
:class: dropdown
533531

lectures/career.md

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -383,43 +383,6 @@ To generate the draws from the distributions $F$ and $G$, use `quantecon.random.
383383
```
384384

385385

386-
```{exercise}
387-
:label: career_ex2
388-
389-
Let's now consider how long it takes for the worker to settle down to a
390-
permanent job, given a starting point of $(\theta, \epsilon) = (0, 0)$.
391-
392-
In other words, we want to study the distribution of the random variable
393-
394-
$$
395-
T^* := \text{the first point in time from which the worker's job no longer changes}
396-
$$
397-
398-
Evidently, the worker's job becomes permanent if and only if $(\theta_t, \epsilon_t)$ enters the
399-
"stay put" region of $(\theta, \epsilon)$ space.
400-
401-
Letting $S$ denote this region, $T^*$ can be expressed as the
402-
first passage time to $S$ under the optimal policy:
403-
404-
$$
405-
T^* := \inf\{t \geq 0 \,|\, (\theta_t, \epsilon_t) \in S\}
406-
$$
407-
408-
Collect 25,000 draws of this random variable and compute the median (which should be about 7).
409-
410-
Repeat the exercise with $\beta=0.99$ and interpret the change.
411-
```
412-
413-
414-
```{exercise}
415-
:label: career_ex3
416-
417-
Set the parameterization to `G_a = G_b = 100` and generate a new optimal policy
418-
figure -- interpret.
419-
```
420-
421-
## Solutions
422-
423386
```{solution-start} career_ex1
424387
:class: dropdown
425388
```
@@ -469,6 +432,32 @@ plt.show()
469432
```{solution-end}
470433
```
471434

435+
```{exercise}
436+
:label: career_ex2
437+
438+
Let's now consider how long it takes for the worker to settle down to a
439+
permanent job, given a starting point of $(\theta, \epsilon) = (0, 0)$.
440+
441+
In other words, we want to study the distribution of the random variable
442+
443+
$$
444+
T^* := \text{the first point in time from which the worker's job no longer changes}
445+
$$
446+
447+
Evidently, the worker's job becomes permanent if and only if $(\theta_t, \epsilon_t)$ enters the
448+
"stay put" region of $(\theta, \epsilon)$ space.
449+
450+
Letting $S$ denote this region, $T^*$ can be expressed as the
451+
first passage time to $S$ under the optimal policy:
452+
453+
$$
454+
T^* := \inf\{t \geq 0 \,|\, (\theta_t, \epsilon_t) \in S\}
455+
$$
456+
457+
Collect 25,000 draws of this random variable and compute the median (which should be about 7).
458+
459+
Repeat the exercise with $\beta=0.99$ and interpret the change.
460+
```
472461

473462
```{solution-start} career_ex2
474463
:class: dropdown
@@ -519,10 +508,19 @@ Not surprisingly, more patient workers will wait longer to settle down to their
519508
```
520509

521510

511+
```{exercise}
512+
:label: career_ex3
513+
514+
Set the parameterization to `G_a = G_b = 100` and generate a new optimal policy
515+
figure -- interpret.
516+
```
517+
522518
```{solution-start} career_ex3
523519
:class: dropdown
524520
```
525521

522+
Here is one solution
523+
526524
```{code-cell} python3
527525
cw = CareerWorkerProblem(G_a=100, G_b=100)
528526
T, get_greedy = operator_factory(cw)
@@ -536,7 +534,7 @@ ax.contourf(tg, eg, greedy_star.T, levels=lvls, cmap=cm.winter, alpha=0.5)
536534
ax.contour(tg, eg, greedy_star.T, colors='k', levels=lvls, linewidths=2)
537535
ax.set(xlabel='θ', ylabel='ϵ')
538536
ax.text(1.8, 2.5, 'new life', fontsize=14)
539-
ax.text(4.5, 2.5, 'new job', fontsize=14, rotation='vertical')
537+
ax.text(4.5, 1.5, 'new job', fontsize=14, rotation='vertical')
540538
ax.text(4.0, 4.5, 'stay put', fontsize=14)
541539
plt.show()
542540
```

lectures/cass_koopmans_1.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -875,8 +875,6 @@ state in which $f'(K)=\rho +\delta$.
875875
- Why does the saving rate respond as it does?
876876
```
877877
878-
### Solution
879-
880878
```{solution-start} ck1_ex1
881879
:class: dropdown
882880
```

lectures/coleman_policy_iter.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,6 @@ Set `γ = 1.5`.
438438
Compute and plot the optimal policy.
439439
```
440440

441-
## Solutions
442-
443441
```{solution-start} cpi_ex1
444442
:class: dropdown
445443
```

lectures/complex_and_trig.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,13 +306,13 @@ x1 = 2 * r * sqrt(2)
306306
## Note: we choose the solution near 0
307307
eq1 = Eq(x1/x0 - r * cos(ω+θ) / cos(ω), 0)
308308
ω = nsolve(eq1, ω, 0)
309-
ω = np.float(ω)
309+
ω = float(ω)
310310
print(f'ω = {ω:1.3f}')
311311
312312
# Solve for p
313313
eq2 = Eq(x0 - 2 * p * cos(ω), 0)
314314
p = nsolve(eq2, p, 0)
315-
p = np.float(p)
315+
p = float(p)
316316
print(f'p = {p:1.3f}')
317317
```
318318

@@ -485,6 +485,9 @@ integrate(cos(ω) * sin(ω), (ω, -π, π))
485485

486486
### Exercises
487487

488+
```{exercise}
489+
:label: complex_ex1
490+
488491
We invite the reader to verify analytically and with the `sympy` package the following two equalities:
489492
490493
$$
@@ -495,3 +498,4 @@ $$
495498
\int_{-\pi}^{\pi} \sin (\omega)^2 \, d\omega = \frac{\pi}{2}
496499
$$
497500
501+
```

0 commit comments

Comments
 (0)