Skip to content

Commit 85e02b6

Browse files
committed
updates
1 parent 32d4339 commit 85e02b6

11 files changed

+911
-1538
lines changed

lectures/March_26_DMD_junk.md

-535
This file was deleted.

lectures/aiyagari.md

+183-149
Large diffs are not rendered by default.

lectures/ak2.md

+300-375
Large diffs are not rendered by default.

lectures/amf_var.py

+44-45
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import numpy as np
32
import scipy as sp
43
import scipy.linalg as la
@@ -10,16 +9,15 @@
109

1110
class AMF_LSS_VAR:
1211
"""
13-
This class transforms an additive (multipilcative)
14-
functional into a QuantEcon linear state space system.
12+
此类将加性(乘性)泛函转换为QuantEcon线性状态空间系统。
1513
"""
1614

1715
def __init__(self, A, B, D, F=None, nu=None, x_0=None):
18-
# Unpack required elements
16+
# 解包必需的元素
1917
self.nx, self.nk = B.shape
2018
self.A, self.B = A, B
2119

22-
# checking the dimension of D (extended from the scalar case)
20+
# 检查D的维度(从标量情况扩展)
2321
if len(D.shape) > 1 and D.shape[0] != 1:
2422
self.nm = D.shape[0]
2523
self.D = D
@@ -30,17 +28,17 @@ def __init__(self, A, B, D, F=None, nu=None, x_0=None):
3028
self.nm = 1
3129
self.D = np.expand_dims(D, 0)
3230

33-
# Create space for additive decomposition
31+
# 为加性分解创建空间
3432
self.add_decomp = None
3533
self.mult_decomp = None
3634

37-
# Set F
35+
# 设置F
3836
if not np.any(F):
3937
self.F = np.zeros((self.nk, 1))
4038
else:
4139
self.F = F
4240

43-
# Set nu
41+
# 设置nu
4442
if not np.any(nu):
4543
self.nu = np.zeros((self.nm, 1))
4644
elif type(nu) == float:
@@ -51,21 +49,20 @@ def __init__(self, A, B, D, F=None, nu=None, x_0=None):
5149
self.nu = nu
5250

5351
if self.nu.shape[0] != self.D.shape[0]:
54-
raise ValueError("The dimension of nu is inconsistent with D!")
52+
raise ValueError("nu的维度与D不一致!")
5553

56-
# Initialize the simulator
54+
# 初始化模拟器
5755
self.x_0 = x_0
5856

59-
# Construct BIG state space representation
57+
# 构建大型状态空间表示
6058
self.lss = self.construct_ss()
6159

6260
def construct_ss(self):
6361
"""
64-
This creates the state space representation that can be passed
65-
into the quantecon LSS class.
62+
这将创建可以传递到quantecon LSS类的状态空间表示。
6663
"""
6764

68-
# Pull out useful info
65+
# 提取有用信息
6966
nx, nk, nm = self.nx, self.nk, self.nm
7067
A, B, D, F, nu = self.A, self.B, self.D, self.F, self.nu
7168

@@ -74,7 +71,7 @@ def construct_ss(self):
7471
else:
7572
nu, H, g = self.additive_decomp()
7673

77-
# Auxiliary blocks with 0's and 1's to fill out the lss matrices
74+
# 用0和1填充lss矩阵的辅助块
7875
nx0c = np.zeros((nx, 1))
7976
nx0r = np.zeros(nx)
8077
nx1 = np.ones(nx)
@@ -85,31 +82,31 @@ def construct_ss(self):
8582
ny0m = np.zeros((nm, nm))
8683
nyx0m = np.zeros_like(D)
8784

88-
# Build A matrix for LSS
89-
# Order of states is: [1, t, xt, yt, mt]
90-
A1 = np.hstack([1, 0, nx0r, ny0r, ny0r]) # Transition for 1
91-
A2 = np.hstack([1, 1, nx0r, ny0r, ny0r]) # Transition for t
92-
A3 = np.hstack([nx0c, nx0c, A, nyx0m.T, nyx0m.T]) # Transition for x_{t+1}
93-
A4 = np.hstack([nu, ny0c, D, ny1m, ny0m]) # Transition for y_{t+1}
94-
A5 = np.hstack([ny0c, ny0c, nyx0m, ny0m, ny1m]) # Transition for m_{t+1}
85+
# 为LSS构建A矩阵
86+
# 状态顺序为:[1, t, xt, yt, mt]
87+
A1 = np.hstack([1, 0, nx0r, ny0r, ny0r]) # 1的转移
88+
A2 = np.hstack([1, 1, nx0r, ny0r, ny0r]) # t的转移
89+
A3 = np.hstack([nx0c, nx0c, A, nyx0m.T, nyx0m.T]) # x_{t+1}的转移
90+
A4 = np.hstack([nu, ny0c, D, ny1m, ny0m]) # y_{t+1}的转移
91+
A5 = np.hstack([ny0c, ny0c, nyx0m, ny0m, ny1m]) # m_{t+1}的转移
9592
Abar = np.vstack([A1, A2, A3, A4, A5])
9693

97-
# Build B matrix for LSS
94+
# 为LSS构建B矩阵
9895
Bbar = np.vstack([nk0, nk0, B, F, H])
9996

100-
# Build G matrix for LSS
101-
# Order of observation is: [xt, yt, mt, st, tt]
102-
G1 = np.hstack([nx0c, nx0c, np.eye(nx), nyx0m.T, nyx0m.T]) # Selector for x_{t}
103-
G2 = np.hstack([ny0c, ny0c, nyx0m, ny1m, ny0m]) # Selector for y_{t}
104-
G3 = np.hstack([ny0c, ny0c, nyx0m, ny0m, ny1m]) # Selector for martingale
105-
G4 = np.hstack([ny0c, ny0c, -g, ny0m, ny0m]) # Selector for stationary
106-
G5 = np.hstack([ny0c, nu, nyx0m, ny0m, ny0m]) # Selector for trend
97+
# 为LSS构建G矩阵
98+
# 观测顺序为:[xt, yt, mt, st, tt]
99+
G1 = np.hstack([nx0c, nx0c, np.eye(nx), nyx0m.T, nyx0m.T]) # x_{t}的选择器
100+
G2 = np.hstack([ny0c, ny0c, nyx0m, ny1m, ny0m]) # y_{t}的选择器
101+
G3 = np.hstack([ny0c, ny0c, nyx0m, ny0m, ny1m]) # 鞅的选择器
102+
G4 = np.hstack([ny0c, ny0c, -g, ny0m, ny0m]) # 平稳部分的选择器
103+
G5 = np.hstack([ny0c, nu, nyx0m, ny0m, ny0m]) # 趋势的选择器
107104
Gbar = np.vstack([G1, G2, G3, G4, G5])
108105

109-
# Build H matrix for LSS
106+
# 为LSS构建H矩阵
110107
Hbar = np.zeros((Gbar.shape[0], nk))
111108

112-
# Build LSS type
109+
# 构建LSS类型
113110
if not np.any(self.x_0):
114111
x0 = np.hstack([1, 0, nx0r, ny0r, ny0r])
115112
else:
@@ -122,11 +119,11 @@ def construct_ss(self):
122119

123120
def additive_decomp(self):
124121
"""
125-
Return values for the martingale decomposition
126-
- nu : unconditional mean difference in Y
127-
- H : coefficient for the (linear) martingale component (kappa_a)
128-
- g : coefficient for the stationary component g(x)
129-
- Y_0 : it should be the function of X_0 (for now set it to 0.0)
122+
返回鞅分解的值
123+
- nu : Y的无条件均值差异
124+
- H : (线性)鞅分量的系数 (kappa_a)
125+
- g : 平稳分量g(x)的系数
126+
- Y_0 : 应该是X_0的函数(目前设置为0.0)
130127
"""
131128
I = np.eye(self.nx)
132129
A_res = la.solve(I - self.A, I)
@@ -137,9 +134,9 @@ def additive_decomp(self):
137134

138135
def multiplicative_decomp(self):
139136
"""
140-
Return values for the multiplicative decomposition (Example 5.4.4.)
141-
- nu_tilde : eigenvalue
142-
- H : vector for the Jensen term
137+
返回乘性分解的值(示例5.4.4
138+
- nu_tilde : 特征值
139+
- H : Jensen项的向量
143140
"""
144141
nu, H, g = self.additive_decomp()
145142
nu_tilde = nu + (.5)*np.expand_dims(np.diag(H @ H.T), 1)
@@ -151,28 +148,30 @@ def multiplicative_decomp(self):
151148

152149

153150
def future_moments(amf_future, N=25):
154-
151+
"""
152+
计算未来时刻的矩
153+
"""
155154
nx, nk, nm = amf_future.nx, amf_future.nk, amf_future.nm
156155
nu_tilde, H, g = amf_future.multiplicative_decomp()
157156

158-
# Allocate space (nm is the number of additive functionals)
157+
# 分配空间(nm是加性泛函的数量)
159158
mbounds = np.zeros((3, N))
160159
sbounds = np.zeros((3, N))
161160
ybounds = np.zeros((3, N))
162161
#mbounds_mult = np.zeros((3, N))
163162
#sbounds_mult = np.zeros((3, N))
164163

165-
# Simulate for as long as we wanted
164+
# 模拟所需的时长
166165
moment_generator = amf_future.lss.moment_sequence()
167166
tmoms = next(moment_generator)
168167

169-
# Pull out population moments
168+
# 提取总体矩
170169
for t in range (N-1):
171170
tmoms = next(moment_generator)
172171
ymeans = tmoms[1]
173172
yvar = tmoms[3]
174173

175-
# Lower and upper bounds - for each additive functional
174+
# 每个加性泛函的上下界
176175
yadd_dist = norm(ymeans[nx], np.sqrt(yvar[nx, nx]))
177176
ybounds[:2, t+1] = yadd_dist.ppf([0.1, .9])
178177
ybounds[2, t+1] = yadd_dist.mean()

lectures/cross_product_trick.md

+41-57
Original file line numberDiff line numberDiff line change
@@ -11,73 +11,64 @@ kernelspec:
1111
name: python3
1212
---
1313

14-
# Eliminating Cross Products
14+
# 消除交叉项
1515

16-
## Overview
16+
## 概述
1717

18-
This lecture describes formulas for eliminating
18+
本讲座描述了消除以下内容的公式:
1919

20-
* cross products between states and control in linear-quadratic dynamic programming problems
20+
* 线性二次动态规划问题中状态和控制之间的交叉项
2121

22-
* covariances between state and measurement noises in Kalman filtering problems
22+
* 卡尔曼滤波问题中状态噪声和测量噪声之间的协方差
2323

24+
对于线性二次动态规划问题,主要思路包括以下步骤:
2425

25-
For a linear-quadratic dynamic programming problem, the idea involves these steps
26-
27-
* transform states and controls in a way that leads to an equivalent problem with no cross-products between transformed states and controls
28-
* solve the transformed problem using standard formulas for problems with no cross-products between states and controls presented in this lecture {doc}`Linear Control: Foundations <lqcontrol>`
29-
* transform the optimal decision rule for the altered problem into the optimal decision rule for the original problem with cross-products between states and controls
26+
* 对状态和控制进行变换,得到一个等价问题,其中转换后的状态和控制之间没有交叉项
27+
* 使用本讲座 {doc}`线性控制:基础 <lqcontrol>` 中介绍的标准公式求解转换后的问题
28+
* 将转换后问题的最优决策规则转换回原始问题(即有状态和控制交叉项的问题)的最优决策规则
3029

3130
+++
3231

33-
## Undiscounted Dynamic Programming Problem
34-
35-
Here is a nonstochastic undiscounted LQ dynamic programming with cross products between
36-
states and controls in the objective function.
37-
38-
32+
## 无折现动态规划问题
3933

40-
The problem is defined by the 5-tuple of matrices $(A, B, R, Q, H)$
41-
where $R$ and $Q$ are positive definite symmetric matrices and
42-
$A \sim m \times m, B \sim m \times k, Q \sim k \times k, R \sim m \times m$ and $H \sim k \times m$.
34+
这里是一个目标函数中包含状态和控制交叉项的非随机无折现线性二次动态规划问题。
4335

36+
该问题由矩阵5元组 $(A, B, R, Q, H)$ 定义,其中 $R$ 和 $Q$ 是正定对称矩阵,且
37+
$A \sim m \times m, B \sim m \times k, Q \sim k \times k, R \sim m \times m$ 以及 $H \sim k \times m$。
4438

45-
The problem is to choose $\{x_{t+1}, u_t\}_{t=0}^\infty$ to maximize
39+
问题是选择 $\{x_{t+1}, u_t\}_{t=0}^\infty$ 以最大化
4640

4741
$$
4842
- \sum_{t=0}^\infty (x_t' R x_t + u_t' Q u_t + 2 u_t H x_t)
4943
$$
5044

51-
subject to the linear constraints
45+
受限于线性约束
5246

5347
$$ x_{t+1} = A x_t + B u_t, \quad t \geq 0 $$
5448

55-
where $x_0$ is a given initial condition.
49+
其中 $x_0$ 是给定的初始条件。
5650

57-
The solution to this undiscounted infinite-horizon problem is a time-invariant feedback rule
51+
这个无限期无折现问题的解是一个时不变的反馈规则
5852

5953
$$ u_t = -F x_t $$
6054

61-
where
55+
其中
6256

6357
$$ F = -(Q + B'PB)^{-1} B'PA $$
6458

65-
and $P \sim m \times m $ is a positive definite solution of the algebraic matrix Riccati equation
59+
$P \sim m \times m $ 是代数矩阵Riccati方程的正定解
6660

6761
$$
6862
P = R + A'PA - (A'PB + H')(Q + B'PB)^{-1}(B'PA + H).
6963
$$
7064

71-
7265
+++
7366

74-
It can be verified that an **equivalent** problem without cross-products between states and controls
75-
is defined by a 4-tuple of matrices : $(A^*, B, R^*, Q) $.
67+
可以验证,一个**等价的**没有状态和控制交叉项的问题可以由矩阵4元组定义:$(A^*, B, R^*, Q)$。
7668

77-
That the omitted matrix $H=0$ indicates that there are no cross products between states and controls
78-
in the equivalent problem.
69+
省略的矩阵 $H=0$ 表示在等价问题中没有状态和控制之间的交叉项。
7970

80-
The matrices $(A^*, B, R^*, Q) $ defining the equivalent problem and the value function, policy function matrices $P, F^*$ that solve it are related to the matrices $(A, B, R, Q, H)$ defining the original problem and the value function, policy function matrices $P, F$ that solve the original problem by
71+
定义等价问题的矩阵 $(A^*, B, R^*, Q)$ 及其值函数、策略函数矩阵 $P, F^*$ 与定义原始问题的矩阵 $(A, B, R, Q, H)$ 及其值函数、策略函数矩阵 $P, F$ 之间的关系如下:
8172

8273
\begin{align*}
8374
A^* & = A - B Q^{-1} H, \\
@@ -89,75 +80,68 @@ F & = F^* + Q^{-1} H.
8980

9081
+++
9182

92-
## Kalman Filter
93-
94-
The **duality** that prevails between a linear-quadratic optimal control and a Kalman filtering problem means that there is an analogous transformation that allows us to transform a Kalman filtering problem
95-
with non-zero covariance matrix between between shocks to states and shocks to measurements to an equivalent Kalman filtering problem with zero covariance between shocks to states and measurments.
83+
## 卡尔曼滤波
9684

97-
Let's look at the appropriate transformations.
85+
线性二次最优控制和卡尔曼滤波问题之间存在的**对偶性**意味着存在一个类似的变换,允许我们将状态噪声和测量噪声之间具有非零协方差矩阵的卡尔曼滤波问题转换为一个等价的、状态噪声和测量噪声之间协方差为零的卡尔曼滤波问题。
9886

87+
让我们看看适当的变换。
9988

100-
First, let's recall the Kalman filter with covariance between noises to states and measurements.
89+
首先,让我们回顾一下具有状态噪声和测量噪声之间协方差的卡尔曼滤波。
10190

102-
The hidden Markov model is
91+
隐马尔可夫模型为:
10392

10493
\begin{align*}
10594
x_{t+1} & = A x_t + B w_{t+1}, \\
10695
z_{t+1} & = D x_t + F w_{t+1},
10796
\end{align*}
10897

109-
where $A \sim m \times m, B \sim m \times p $ and $D \sim k \times m, F \sim k \times p $,
110-
and $w_{t+1}$ is the time $t+1$ component of a sequence of i.i.d. $p \times 1$ normally distibuted
111-
random vectors with mean vector zero and covariance matrix equal to a $p \times p$ identity matrix.
98+
其中 $A \sim m \times m, B \sim m \times p $ 且 $D \sim k \times m, F \sim k \times p $,
99+
且 $w_{t+1}$ 是一个独立同分布的 $p \times 1$ 正态分布随机向量序列的时间 $t+1$ 分量,其均值向量为零,协方差矩阵等于 $p \times p$ 单位矩阵。
112100

113-
Thus, $x_t$ is $m \times 1$ and $z_t$ is $k \times 1$.
114-
115-
The Kalman filtering formulas are
101+
因此,$x_t$ 是 $m \times 1$ 且 $z_t$ 是 $k \times 1$。
116102

103+
卡尔曼滤波公式为:
117104

118105
\begin{align*}
119106
K(\Sigma_t) & = (A \Sigma_t D' + BF')(D \Sigma_t D' + FF')^{-1}, \\
120107
\Sigma_{t+1}& = A \Sigma_t A' + BB' - (A \Sigma_t D' + BF')(D \Sigma_t D' + FF')^{-1} (D \Sigma_t A' + FB').
121108
\end{align*} (eq:Kalman102)
122-
123109

124-
Define tranformed matrices
110+
定义转换后的矩阵:
125111

126112
\begin{align*}
127113
A^* & = A - BF' (FF')^{-1} D, \\
128114
B^* {B^*}' & = BB' - BF' (FF')^{-1} FB'.
129115
\end{align*}
130116

131-
### Algorithm
117+
### 算法
132118

133-
A consequence of formulas {eq}`eq:Kalman102} is that we can use the following algorithm to solve Kalman filtering problems that involve non zero covariances between state and signal noises.
119+
公式 {eq}`eq:Kalman102` 的一个结果是,我们可以使用以下算法来求解涉及状态噪声和信号噪声之间非零协方差的卡尔曼滤波问题。
134120

135-
First, compute $\Sigma, K^*$ using the ordinary Kalman filtering formula with $BF' = 0$, i.e.,
136-
with zero covariance matrix between random shocks to states and random shocks to measurements.
121+
首先,使用普通卡尔曼滤波公式计算 $\Sigma, K^*$,其中 $BF' = 0$,即状态随机噪声和测量随机噪声之间的协方差矩阵为零。
137122

138-
That is, compute $K^*$ and $\Sigma$ that satisfy
123+
也就是说,计算满足以下条件的 $K^*$ $\Sigma$
139124

140125
\begin{align*}
141126
K^* & = (A^* \Sigma D')(D \Sigma D' + FF')^{-1} \\
142127
\Sigma & = A^* \Sigma {A^*}' + B^* {B^*}' - (A^* \Sigma D')(D \Sigma D' + FF')^{-1} (D \Sigma {A^*}').
143128
\end{align*}
144129

145-
The Kalman gain for the original problem **with non-zero covariance** between shocks to states and measurements is then
130+
原始问题(具有**非零协方差**的状态和测量噪声)的卡尔曼增益为:
146131

147132
$$
148133
K = K^* + BF' (FF')^{-1},
149134
$$
150135

151-
The state reconstruction covariance matrix $\Sigma$ for the original problem equals the state reconstrution covariance matrix for the transformed problem.
136+
原始问题的状态重构协方差矩阵 $\Sigma$ 等于转换后问题的状态重构协方差矩阵。
152137

153138
+++
154139

155-
## Duality table
156-
157-
Here is a handy table to remember how the Kalman filter and dynamic program are related.
140+
## 对偶表
158141

142+
这是一个便于记忆卡尔曼滤波和动态规划关系的表格。
159143

160-
| Dynamic Program | Kalman Filter |
144+
| 动态规划 | 卡尔曼滤波 |
161145
| :-------------: | :-----------: |
162146
| $A$ | $A'$ |
163147
| $B$ | $D'$ |

0 commit comments

Comments
 (0)