Skip to content

Commit 37c0d3c

Browse files
committed
fix kalman_2
1 parent c4c4b86 commit 37c0d3c

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

lectures/kalman_2.md

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ jupytext:
44
extension: .md
55
format_name: myst
66
format_version: 0.13
7-
jupytext_version: 1.14.4
7+
jupytext_version: 1.16.1
88
kernelspec:
99
display_name: Python 3 (ipykernel)
1010
language: python
@@ -63,8 +63,18 @@ from quantecon import Kalman, LinearStateSpace
6363
from collections import namedtuple
6464
from scipy.stats import multivariate_normal
6565
import matplotlib as mpl
66-
mpl.rcParams['text.usetex'] = True
67-
mpl.rcParams['text.latex.preamble'] = r'\usepackage{{amsmath}}'
66+
# Configure Matplotlib to use pdfLaTeX and CJKutf8
67+
mpl.rcParams.update({
68+
'text.usetex': True,
69+
'text.latex.preamble': r'''
70+
\usepackage{{CJKutf8}}
71+
\usepackage{{amsmath}}
72+
'''
73+
})
74+
75+
# Function to wrap Chinese text in CJK environment
76+
def cjk(text):
77+
return rf'\begin{{CJK}}{{UTF8}}{{gbsn}}{text}\end{{CJK}}'
6878
```
6979

7080
## 工人的产出
@@ -252,21 +262,20 @@ u_hat_t = x_hat_t[1, :]
252262
我们可以观察公司对工人工作伦理的推断 $E [u_0 | y^{t-1}]$ 如何逐渐收敛于隐藏的 $u_0$,而 $u_0$ 是公司无法直接观察到的。
253263

254264
```{code-cell} ipython3
255-
256265
fig, ax = plt.subplots(1, 2)
257266
258267
ax[0].plot(y_hat_t, label=r'$E[y_t| y^{t-1}]$')
259-
ax[0].set_xlabel('时间')
268+
ax[0].set_xlabel(cjk('时间'))
260269
ax[0].set_ylabel(r'$E[y_t]$')
261-
ax[0].set_title(r'$E[y_t]$ 随时间变化')
270+
ax[0].set_title(cjk('$E[y_t]$ 随时间变化'))
262271
ax[0].legend()
263272
264273
ax[1].plot(u_hat_t, label=r'$E[u_t|y^{t-1}]$')
265274
ax[1].axhline(y=u_0, color='grey',
266275
linestyle='dashed', label=fr'$u_0={u_0:.2f}$')
267-
ax[1].set_xlabel('时间')
276+
ax[1].set_xlabel(cjk('时间'))
268277
ax[1].set_ylabel(r'$E[u_t|y^{t-1}]$')
269-
ax[1].set_title('推断的工作伦理随时间变化')
278+
ax[1].set_title(cjk('推断的工作伦理随时间变化'))
270279
ax[1].legend()
271280
272281
fig.tight_layout()
@@ -290,7 +299,6 @@ print(Σ_t[:, :, -1])
290299
通过在不同时间 $t$ 绘制 $E [x_t |y^{t-1}] $ 周围的置信椭圆,我们可以生动地展示条件协方差矩阵 $\Sigma_t$ 如何演化。
291300

292301
```{code-cell} ipython3
293-
294302
# 创建用于等高线绘制的点网格
295303
h_range = np.linspace(x_hat_t[0, :].min()-0.5*Σ_t[0, 0, 1],
296304
x_hat_t[0, :].max()+0.5*Σ_t[0, 0, 1], 100)
@@ -314,7 +322,7 @@ for i, t in enumerate(np.linspace(0, T-1, 3, dtype=int)):
314322
# 创建 PDF 的等高线图
315323
con = axs[i].contour(h, u, pdf_values, cmap='viridis')
316324
axs[i].clabel(con, inline=1, fontsize=10)
317-
axs[i].set_title(f'时间步 {t+1}')
325+
axs[i].set_title(cjk('时间步')+f'{t+1}')
318326
axs[i].set_xlabel(r'$h_{{{}}}$'.format(str(t+1)))
319327
axs[i].set_ylabel(r'$u_{{{}}}$'.format(str(t+1)))
320328
@@ -337,7 +345,6 @@ plt.show()
337345
这是实现这个目标的一种方式:
338346

339347
```{code-cell} ipython3
340-
341348
# 例如,我们可能想要 h_0 = 0 和 u_0 = 4
342349
mu_0 = np.array([0.0, 4.0])
343350
@@ -360,7 +367,6 @@ print('u_0 =', u_0)
360367
实现相同目标的另一种方式是使用以下代码:
361368

362369
```{code-cell} ipython3
363-
364370
# 如果我们想要设置初始
365371
# h_0 = hhat_0 = 0 和 u_0 = uhhat_0 = 4.0:
366372
worker = create_worker(hhat_0=0.0, uhat_0=4.0)
@@ -405,17 +411,17 @@ for t in range(1, T):
405411
fig, ax = plt.subplots(1, 2)
406412
407413
ax[0].plot(y_hat_t, label=r'$E[y_t| y^{t-1}]$')
408-
ax[0].set_xlabel('时间')
414+
ax[0].set_xlabel(cjk('时间'))
409415
ax[0].set_ylabel(r'$E[y_t]$')
410-
ax[0].set_title(r'$E[y_t]$ 随时间变化')
416+
ax[0].set_title(cjk('$E[y_t]$ 随时间变化'))
411417
ax[0].legend()
412418
413419
ax[1].plot(u_hat_t, label=r'$E[u_t|y^{t-1}]$')
414420
ax[1].axhline(y=u_0, color='grey',
415421
linestyle='dashed', label=fr'$u_0={u_0:.2f}$')
416-
ax[1].set_xlabel('时间')
422+
ax[1].set_xlabel(cjk('时间'))
417423
ax[1].set_ylabel(r'$E[u_t|y^{t-1}]$')
418-
ax[1].set_title('推断的工作伦理随时间变化')
424+
ax[1].set_title(cjk('推断的工作伦理随时间变化'))
419425
ax[1].legend()
420426
421427
fig.tight_layout()
@@ -427,7 +433,6 @@ plt.show()
427433
这是一个例子:
428434

429435
```{code-cell} ipython3
430-
431436
# 我们可以在创建工人时设置这些参数 -- 就像类一样!
432437
hard_working_worker = create_worker(α=.4, β=.8,
433438
hhat_0=7.0, uhat_0=100, σ_h=2.5, σ_u=3.2)
@@ -477,31 +482,30 @@ def simulate_workers(worker, T, ax, mu_0=None, Sigma_0=None,
477482
u_hat_t[i] = x_hat[1]
478483
479484
if diff == True:
480-
title = ('推断的工作伦理与真实工作伦理的差异随时间变化'
485+
title = (cjk('推断的工作伦理与真实工作伦理的差异随时间变化')
481486
if title == None else title)
482487
483488
ax.plot(u_hat_t - u_0, alpha=.5)
484489
ax.axhline(y=0, color='grey', linestyle='dashed')
485-
ax.set_xlabel('时间')
490+
ax.set_xlabel(cjk('时间'))
486491
ax.set_ylabel(r'$E[u_t|y^{t-1}] - u_0$')
487492
ax.set_title(title)
488493
489494
else:
490495
label_line = (r'$E[u_t|y^{t-1}]$' if name == None
491496
else name)
492-
title = ('推断的工作伦理随时间变化'
497+
title = (cjk('推断的工作伦理随时间变化')
493498
if title == None else title)
494499
495500
u_hat_plot = ax.plot(u_hat_t, label=label_line)
496501
ax.axhline(y=u_0, color=u_hat_plot[0].get_color(),
497502
linestyle='dashed', alpha=0.5)
498-
ax.set_xlabel('时间')
503+
ax.set_xlabel(cjk('时间'))
499504
ax.set_ylabel(r'$E[u_t|y^{t-1}]$')
500505
ax.set_title(title)
501506
```
502507

503508
```{code-cell} ipython3
504-
505509
num_workers = 3
506510
T = 50
507511
fig, ax = plt.subplots(figsize=(7, 7))
@@ -514,7 +518,6 @@ plt.show()
514518
```
515519

516520
```{code-cell} ipython3
517-
518521
# 我们还可以生成 u_t 的图:
519522
520523
T = 50
@@ -537,7 +540,6 @@ plt.show()
537540
```
538541

539542
```{code-cell} ipython3
540-
541543
# 我们还可以为所有工人使用精确的 u_0=1 和 h_0=2
542544
543545
T = 50
@@ -566,7 +568,6 @@ plt.show()
566568
```
567569

568570
```{code-cell} ipython3
569-
570571
# 我们可以只为其中一个工人生成图:
571572
572573
T = 50
@@ -584,11 +585,11 @@ uhat_0s = 100
584585
585586
worker = create_worker(uhat_0=uhat_0, α=α, β=β)
586587
simulate_workers(worker, T, ax, mu_0=mu_0_1, Sigma_0=Sigma_0,
587-
diff=False, name=r'勤奋的工人')
588+
diff=False, name=cjk('勤奋的工人'))
588589
simulate_workers(worker, T, ax, mu_0=mu_0_2, Sigma_0=Sigma_0,
589590
diff=False,
590-
title='一个勤奋的工人和一个不太勤奋的工人',
591-
name=r'普通工人')
591+
title=cjk('一个勤奋的工人和一个不太勤奋的工人'),
592+
name=cjk('普通工人'))
592593
ax.axhline(y=u_0, xmin=0, xmax=0, color='grey',
593594
linestyle='dashed', label=r'$u_{i, 0}$')
594595
ax.legend(bbox_to_anchor=(1, 0.5))
@@ -597,4 +598,4 @@ plt.show()
597598

598599
## 未来扩展
599600

600-
我们可以通过创建新类型的工人,并让公司仅通过观察他们的产出历史来了解他们的隐藏(对公司来说)状态,来进行许多富有启发性的实验。
601+
我们可以通过创建新类型的工人,并让公司仅通过观察他们的产出历史来了解他们的隐藏(对公司来说)状态,来进行许多富有启发性的实验。

0 commit comments

Comments
 (0)