Skip to content

Commit 95edcd8

Browse files
HumphreyYangmmcky
andauthored
Revert JAX Content in Newton's Method (#340)
* update newton_method * add a comment on import * update seealso at the beginning of the lecture * add pip install * update to accept error * TMP: remove cache for a full build * fix bugs in career and mccall model * revert changes in CI * remove the time magic * use try-catch instead relying on myst * add raises-exception tag * catch all errors * add a clear example for the function * check output for the solution * add max iter * update max_iter * check the output of the line before * fix convergence issue * finalize the code * remove try-catch to raise errors * update the runtime comparison * revert temprary bug fixes * move exercises in mccall model --------- Co-authored-by: mmcky <[email protected]>
1 parent 5d4c8d6 commit 95edcd8

File tree

2 files changed

+175
-196
lines changed

2 files changed

+175
-196
lines changed

lectures/mccall_model.md

Lines changed: 50 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,56 @@ Repeat a large number of times and take the average.
661661
Plot mean unemployment duration as a function of $c$ in `c_vals`.
662662
```
663663

664+
```{solution-start} mm_ex1
665+
:class: dropdown
666+
```
667+
668+
Here's one solution
669+
670+
```{code-cell} python3
671+
cdf = np.cumsum(q_default)
672+
673+
@jit(nopython=True)
674+
def compute_stopping_time(w_bar, seed=1234):
675+
676+
np.random.seed(seed)
677+
t = 1
678+
while True:
679+
# Generate a wage draw
680+
w = w_default[qe.random.draw(cdf)]
681+
# Stop when the draw is above the reservation wage
682+
if w >= w_bar:
683+
stopping_time = t
684+
break
685+
else:
686+
t += 1
687+
return stopping_time
688+
689+
@jit(nopython=True)
690+
def compute_mean_stopping_time(w_bar, num_reps=100000):
691+
obs = np.empty(num_reps)
692+
for i in range(num_reps):
693+
obs[i] = compute_stopping_time(w_bar, seed=i)
694+
return obs.mean()
695+
696+
c_vals = np.linspace(10, 40, 25)
697+
stop_times = np.empty_like(c_vals)
698+
for i, c in enumerate(c_vals):
699+
mcm = McCallModel(c=c)
700+
w_bar = compute_reservation_wage_two(mcm)
701+
stop_times[i] = compute_mean_stopping_time(w_bar)
702+
703+
fig, ax = plt.subplots()
704+
705+
ax.plot(c_vals, stop_times, label="mean unemployment duration")
706+
ax.set(xlabel="unemployment compensation", ylabel="months")
707+
ax.legend()
708+
709+
plt.show()
710+
```
711+
712+
```{solution-end}
713+
```
664714

665715
```{exercise-start}
666716
:label: mm_ex2
@@ -722,60 +772,6 @@ Once your code is working, investigate how the reservation wage changes with $c$
722772
```{exercise-end}
723773
```
724774

725-
## Solutions
726-
727-
```{solution-start} mm_ex1
728-
:class: dropdown
729-
```
730-
731-
Here's one solution
732-
733-
```{code-cell} python3
734-
cdf = np.cumsum(q_default)
735-
736-
@jit(nopython=True)
737-
def compute_stopping_time(w_bar, seed=1234):
738-
739-
np.random.seed(seed)
740-
t = 1
741-
while True:
742-
# Generate a wage draw
743-
w = w_default[qe.random.draw(cdf)]
744-
# Stop when the draw is above the reservation wage
745-
if w >= w_bar:
746-
stopping_time = t
747-
break
748-
else:
749-
t += 1
750-
return stopping_time
751-
752-
@jit(nopython=True)
753-
def compute_mean_stopping_time(w_bar, num_reps=100000):
754-
obs = np.empty(num_reps)
755-
for i in range(num_reps):
756-
obs[i] = compute_stopping_time(w_bar, seed=i)
757-
return obs.mean()
758-
759-
c_vals = np.linspace(10, 40, 25)
760-
stop_times = np.empty_like(c_vals)
761-
for i, c in enumerate(c_vals):
762-
mcm = McCallModel(c=c)
763-
w_bar = compute_reservation_wage_two(mcm)
764-
stop_times[i] = compute_mean_stopping_time(w_bar)
765-
766-
fig, ax = plt.subplots()
767-
768-
ax.plot(c_vals, stop_times, label="mean unemployment duration")
769-
ax.set(xlabel="unemployment compensation", ylabel="months")
770-
ax.legend()
771-
772-
plt.show()
773-
```
774-
775-
```{solution-end}
776-
```
777-
778-
779775
```{solution-start} mm_ex2
780776
:class: dropdown
781777
```

0 commit comments

Comments
 (0)