Skip to content

Revert JAX Content in Newton's Method #340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 26 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
1099889
update newton_method
HumphreyYang May 9, 2023
2b08451
add a comment on import
HumphreyYang May 9, 2023
d35e20e
update seealso at the beginning of the lecture
HumphreyYang May 9, 2023
ac5063d
add pip install
HumphreyYang May 9, 2023
2a5705f
update to accept error
HumphreyYang May 9, 2023
1e813e7
TMP: remove cache for a full build
mmcky May 10, 2023
45b13d1
Merge branch 'main' of https://github.com/QuantEcon/lecture-python.my…
HumphreyYang May 15, 2023
c6fc30c
fix bugs in career and mccall model
HumphreyYang May 15, 2023
d6ddfcf
revert changes in CI
HumphreyYang May 15, 2023
a829aeb
remove the time magic
HumphreyYang May 15, 2023
1aa6b99
use try-catch instead relying on myst
HumphreyYang May 15, 2023
ef994e0
add raises-exception tag
HumphreyYang May 15, 2023
1cdb306
catch all errors
HumphreyYang May 15, 2023
e97d187
add a clear example for the function
HumphreyYang May 16, 2023
734410c
check output for the solution
HumphreyYang May 16, 2023
dc7d1e5
add max iter
HumphreyYang May 16, 2023
1e988ac
update max_iter
HumphreyYang May 16, 2023
801ea6f
check the output of the line before
HumphreyYang May 16, 2023
5e6dfb4
fix convergence issue
HumphreyYang May 16, 2023
3d0456d
finalize the code
HumphreyYang May 16, 2023
b801b83
Merge branch 'main' into revert-newton
mmcky May 17, 2023
5a14609
remove try-catch to raise errors
HumphreyYang May 23, 2023
a0b588a
update the runtime comparison
HumphreyYang May 23, 2023
8a475c6
revert temprary bug fixes
HumphreyYang May 30, 2023
a56dd0d
move exercises in mccall model
HumphreyYang May 30, 2023
ed4f1f5
Merge branch 'main' into revert-newton
HumphreyYang Jun 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 50 additions & 54 deletions lectures/mccall_model.md
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,56 @@ Repeat a large number of times and take the average.
Plot mean unemployment duration as a function of $c$ in `c_vals`.
```

```{solution-start} mm_ex1
:class: dropdown
```

Here's one solution

```{code-cell} python3
cdf = np.cumsum(q_default)

@jit(nopython=True)
def compute_stopping_time(w_bar, seed=1234):

np.random.seed(seed)
t = 1
while True:
# Generate a wage draw
w = w_default[qe.random.draw(cdf)]
# Stop when the draw is above the reservation wage
if w >= w_bar:
stopping_time = t
break
else:
t += 1
return stopping_time

@jit(nopython=True)
def compute_mean_stopping_time(w_bar, num_reps=100000):
obs = np.empty(num_reps)
for i in range(num_reps):
obs[i] = compute_stopping_time(w_bar, seed=i)
return obs.mean()

c_vals = np.linspace(10, 40, 25)
stop_times = np.empty_like(c_vals)
for i, c in enumerate(c_vals):
mcm = McCallModel(c=c)
w_bar = compute_reservation_wage_two(mcm)
stop_times[i] = compute_mean_stopping_time(w_bar)

fig, ax = plt.subplots()

ax.plot(c_vals, stop_times, label="mean unemployment duration")
ax.set(xlabel="unemployment compensation", ylabel="months")
ax.legend()

plt.show()
```

```{solution-end}
```

```{exercise-start}
:label: mm_ex2
Expand Down Expand Up @@ -722,60 +772,6 @@ Once your code is working, investigate how the reservation wage changes with $c$
```{exercise-end}
```

## Solutions

```{solution-start} mm_ex1
:class: dropdown
```

Here's one solution

```{code-cell} python3
cdf = np.cumsum(q_default)

@jit(nopython=True)
def compute_stopping_time(w_bar, seed=1234):

np.random.seed(seed)
t = 1
while True:
# Generate a wage draw
w = w_default[qe.random.draw(cdf)]
# Stop when the draw is above the reservation wage
if w >= w_bar:
stopping_time = t
break
else:
t += 1
return stopping_time

@jit(nopython=True)
def compute_mean_stopping_time(w_bar, num_reps=100000):
obs = np.empty(num_reps)
for i in range(num_reps):
obs[i] = compute_stopping_time(w_bar, seed=i)
return obs.mean()

c_vals = np.linspace(10, 40, 25)
stop_times = np.empty_like(c_vals)
for i, c in enumerate(c_vals):
mcm = McCallModel(c=c)
w_bar = compute_reservation_wage_two(mcm)
stop_times[i] = compute_mean_stopping_time(w_bar)

fig, ax = plt.subplots()

ax.plot(c_vals, stop_times, label="mean unemployment duration")
ax.set(xlabel="unemployment compensation", ylabel="months")
ax.legend()

plt.show()
```

```{solution-end}
```


```{solution-start} mm_ex2
:class: dropdown
```
Expand Down
Loading