Skip to content

Commit 0f3e233

Browse files
committed
update lectures
1 parent 217d349 commit 0f3e233

Some content is hidden

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

50 files changed

+1196
-1188
lines changed

lectures/aiyagari.md

Lines changed: 105 additions & 148 deletions
Large diffs are not rendered by default.

lectures/cake_eating_numerical.md

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ v(x) = \max_{0\leq c \leq x} \{u(c) + \beta v(x-c)\}
5353

5454
价值函数和最优策略的解析解如下所示。
5555

56-
```{code-cell} python3
56+
```{code-cell} ipython3
5757
:load: _static/lecture_specific/cake_eating_numerical/analytical.py
5858
```
5959
我们的首要目标是以数值方式获得这些解析解。
@@ -118,7 +118,7 @@ $$
118118

119119
下面的`maximize`函数是一个小型辅助函数,它将SciPy的最小化程序转换为最大化程序。
120120

121-
```{code-cell} python3
121+
```{code-cell} ipython3
122122
def maximize(g, a, b, args):
123123
"""
124124
在区间[a, b]上最大化函数g。
@@ -139,7 +139,7 @@ def maximize(g, a, b, args):
139139

140140
这个类还将提供一个名为 `state_action_value` 的方法,该方法根据特定状态和对 $v$ 的猜测返回消费选择的价值。
141141

142-
```{code-cell} python3
142+
```{code-cell} ipython3
143143
class CakeEating:
144144
145145
def __init__(self,
@@ -181,7 +181,7 @@ class CakeEating:
181181
```
182182
现在我们定义贝尔曼算子:
183183

184-
```{code-cell} python3
184+
```{code-cell} ipython3
185185
def T(v, ce):
186186
"""
187187
贝尔曼算子。更新值函数的估计值。
@@ -202,14 +202,14 @@ def T(v, ce):
202202

203203
让我们先用默认参数创建一个`CakeEating`实例。
204204

205-
```{code-cell} python3
205+
```{code-cell} ipython3
206206
ce = CakeEating()
207207
```
208208
现在让我们看看值函数的迭代过程。
209209

210210
我们从初始猜测值$v$开始,对每个网格点$x$,令$v(x) = u(x)$。
211211

212-
```{code-cell} python3
212+
```{code-cell} ipython3
213213
x_grid = ce.x_grid
214214
v = ce.u(x_grid) # 初始猜测
215215
n = 12 # 迭代次数
@@ -232,7 +232,7 @@ plt.show()
232232
```
233233
为了更系统地完成这项工作,我们引入一个名为`compute_value_function`的包装函数,该函数会一直迭代直到满足某些收敛条件。
234234

235-
```{code-cell} python3
235+
```{code-cell} ipython3
236236
def compute_value_function(ce,
237237
tol=1e-4,
238238
max_iter=1000,
@@ -264,12 +264,12 @@ def compute_value_function(ce,
264264
```
265265
现在让我们调用它,注意运行需要一点时间。
266266

267-
```{code-cell} python3
267+
```{code-cell} ipython3
268268
v = compute_value_function(ce)
269269
```
270270
现在我们可以绘图查看收敛后的值函数是什么样子。
271271

272-
```{code-cell} python3
272+
```{code-cell} ipython3
273273
fig, ax = plt.subplots()
274274
275275
ax.plot(x_grid, v, label='近似值函数')
@@ -281,10 +281,10 @@ plt.show()
281281
```
282282
接下来让我们将其与解析解进行比较。
283283

284-
```{code-cell} python3
284+
```{code-cell} ipython3
285285
v_analytical = v_star(ce.x_grid, ce.β, ce.γ)
286286
```
287-
```{code-cell} python3
287+
```{code-cell} ipython3
288288
fig, ax = plt.subplots()
289289
290290
ax.plot(x_grid, v_analytical, label='解析解')
@@ -323,7 +323,7 @@ $$
323323

324324
这是相关函数:
325325

326-
```{code-cell} python3
326+
```{code-cell} ipython3
327327
def σ(ce, v):
328328
"""
329329
最优策略函数。给定价值函数,
@@ -344,13 +344,13 @@ def σ(ce, v):
344344
```
345345
现在让我们传入近似值函数并计算最优消费:
346346

347-
```{code-cell} python3
347+
```{code-cell} ipython3
348348
c = σ(ce, v)
349349
```
350350
(pol_an)=
351351
让我们将其与真实的解析解进行对比绘图
352352

353-
```{code-cell} python3
353+
```{code-cell} ipython3
354354
c_analytical = c_star(ce.x_grid, ce.β, ce.γ)
355355
356356
fig, ax = plt.subplots()
@@ -445,7 +445,7 @@ $$
445445

446446
我们将使用[继承](https://en.wikipedia.org/wiki/Inheritance_%28object-oriented_programming%29)来最大化代码重用。
447447

448-
```{code-cell} python3
448+
```{code-cell} ipython3
449449
class OptimalGrowth(CakeEating):
450450
"""
451451
CakeEating的一个子类,添加了参数α并重写了
@@ -473,12 +473,12 @@ class OptimalGrowth(CakeEating):
473473
474474
return u(c) + β * v((x - c)**α)
475475
```
476-
```{code-cell} python3
476+
```{code-cell} ipython3
477477
og = OptimalGrowth()
478478
```
479479
这是计算得到的值函数。
480480

481-
```{code-cell} python3
481+
```{code-cell} ipython3
482482
v = compute_value_function(og, verbose=False)
483483
484484
fig, ax = plt.subplots()
@@ -491,7 +491,7 @@ plt.show()
491491
```
492492
这是计算得出的策略,与我们之前推导的标准蛋糕食用情况($\alpha=1$)的解决方案相结合。
493493

494-
```{code-cell} python3
494+
```{code-cell} ipython3
495495
c_new = σ(og, v)
496496
497497
fig, ax = plt.subplots()
@@ -525,7 +525,7 @@ plt.show()
525525

526526
这是实现时间迭代的一种方法。
527527

528-
```{code-cell} python3
528+
```{code-cell} ipython3
529529
def K(σ_array, ce):
530530
"""
531531
策略函数算子。给定策略函数,
@@ -556,7 +556,7 @@ def K(σ_array, ce):
556556
557557
return σ_new
558558
```
559-
```{code-cell} python3
559+
```{code-cell} ipython3
560560
def iterate_euler_equation(ce,
561561
max_iter=500,
562562
tol=1e-5,
@@ -588,12 +588,12 @@ def iterate_euler_equation(ce,
588588
589589
return σ
590590
```
591-
```{code-cell} python3
591+
```{code-cell} ipython3
592592
ce = CakeEating(x_grid_min=0.0)
593593
c_euler = iterate_euler_equation(ce)
594594
```
595595

596-
```{code-cell} python3
596+
```{code-cell} ipython3
597597
fig, ax = plt.subplots()
598598
599599
ax.plot(ce.x_grid, c_analytical, label='解析解')

lectures/cake_eating_problem.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ u(c) = \frac{c^{1-\gamma}}{1-\gamma} \qquad (\gamma \gt 0, \, \gamma \neq 1)
7070

7171
用Python表示为
7272

73-
```{code-cell} python3
73+
```{code-cell} ipython3
7474
def u(c, γ):
7575
7676
return c**(1 - γ) / (1 - γ)
@@ -201,14 +201,14 @@ v^*(x_t) = \left( 1-\beta^{1/\gamma} \right)^{-\gamma}u(x_t)
201201

202202
以下是值函数的Python表示:
203203

204-
```{code-cell} python3
204+
```{code-cell} ipython3
205205
def v_star(x, β, γ):
206206
207207
return (1 - β**(1 / γ))**(-γ) * u(x, γ)
208208
```
209209
下面是一个显示固定参数下函数的图表:
210210

211-
```{code-cell} python3
211+
```{code-cell} ipython3
212212
β, γ = 0.95, 1.2
213213
x_grid = np.linspace(0.1, 5, 100)
214214
@@ -255,14 +255,14 @@ $$
255255
从{eq}`crra_opt_pol`可以看出,事实确实如此。
256256
这里有一些图表来说明。
257257

258-
```{code-cell} python3
258+
```{code-cell} ipython3
259259
def c_star(x, β, γ):
260260
261261
return (1 - β ** (1/γ)) * x
262262
```
263263
继续使用上面的$\beta$和$\gamma$值,绘图如下
264264

265-
```{code-cell} python3
265+
```{code-cell} ipython3
266266
fig, ax = plt.subplots()
267267
ax.plot(x_grid, c_star(x_grid, β, γ), label='默认参数')
268268
ax.plot(x_grid, c_star(x_grid, β + 0.02, γ), label=r'更高的$\beta$')

lectures/career.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ $$
153153

154154
下图展示了当$n=50$时,不同形状参数对概率质量函数的影响。
155155

156-
```{code-cell} python3
156+
```{code-cell} ipython3
157157
def gen_probs(n, a, b):
158158
probs = np.zeros(n+1)
159159
for k in range(n+1):
@@ -174,7 +174,7 @@ plt.show()
174174

175175
我们首先创建一个类 `CareerWorkerProblem`,它将保存模型的默认参数化和价值函数的初始猜测。
176176

177-
```{code-cell} python3
177+
```{code-cell} ipython3
178178
class CareerWorkerProblem:
179179
180180
def __init__(self,
@@ -204,7 +204,7 @@ class CareerWorkerProblem:
204204

205205
在此模型中,$T$由$Tv(\theta, \epsilon) = \max\{I, II, III\}$定义,其中$I$、$II$和$III$如{eq}`eyes`所示。
206206

207-
```{code-cell} python3
207+
```{code-cell} ipython3
208208
def operator_factory(cw, parallel_flag=True):
209209
210210
"""
@@ -257,7 +257,7 @@ def operator_factory(cw, parallel_flag=True):
257257
```
258258
最后,`solve_model`将接收一个`CareerWorkerProblem`实例,并使用贝尔曼算子进行迭代,以找到贝尔曼方程的不动点。
259259

260-
```{code-cell} python3
260+
```{code-cell} ipython3
261261
def solve_model(cw,
262262
use_parallel=True,
263263
tol=1e-4,
@@ -290,7 +290,7 @@ def solve_model(cw,
290290
```
291291
这是模型的解决方案 -- 一个近似值函数
292292

293-
```{code-cell} python3
293+
```{code-cell} ipython3
294294
cw = CareerWorkerProblem()
295295
T, get_greedy = operator_factory(cw)
296296
v_star = solve_model(cw, verbose=False)
@@ -311,7 +311,7 @@ plt.show()
311311
```
312312
这就是最优策略
313313

314-
```{code-cell} python3
314+
```{code-cell} ipython3
315315
fig, ax = plt.subplots(figsize=(6, 6))
316316
tg, eg = np.meshgrid(cw.θ, cw.ϵ)
317317
lvls = (0.5, 1.5, 2.5, 3.5)
@@ -366,7 +366,7 @@ plt.show()
366366

367367
在阅读代码时,请注意`optimal_policy[i, j]` = 在$(\theta_i, \epsilon_j)$处的策略 = 1、2或3;分别表示'保持现状'、'新工作'和'新生活'。
368368

369-
```{code-cell} python3
369+
```{code-cell} ipython3
370370
F = np.cumsum(cw.F_probs)
371371
G = np.cumsum(cw.G_probs)
372372
v_star = solve_model(cw, verbose=False)
@@ -433,7 +433,7 @@ $$
433433
```
434434

435435
原始参数下的中位数可以按如下方式计算
436-
```{code-cell} python3
436+
```{code-cell} ipython3
437437
cw = CareerWorkerProblem()
438438
F = np.cumsum(cw.F_probs)
439439
G = np.cumsum(cw.G_probs)
@@ -485,7 +485,7 @@ median_time(greedy_star, F, G)
485485

486486
这是一个解决方案
487487

488-
```{code-cell} python3
488+
```{code-cell} ipython3
489489
cw = CareerWorkerProblem(G_a=100, G_b=100)
490490
T, get_greedy = operator_factory(cw)
491491
v_star = solve_model(cw, verbose=False)

0 commit comments

Comments
 (0)