diff --git a/lectures/_config.yml b/lectures/_config.yml index 9674181..29b9ade 100644 --- a/lectures/_config.yml +++ b/lectures/_config.yml @@ -39,7 +39,8 @@ sphinx: config: bibtex_reference_style: author_year # false-positive links - linkcheck_ignore: ['https://doi.org/https://doi.org/10.2307/1235116', 'https://math.stackexchange.com/*', 'https://stackoverflow.com/*'] + linkcheck_ignore: ['https://doi.org/https://doi.org/10.2307/1235116', 'https://unctad.org/publication/commodities-and-development-report-2019', + 'https://math.stackexchange.com/*', 'https://stackoverflow.com/*'] # myst-nb config nb_render_image_options: width: 80% diff --git a/lectures/about.md b/lectures/about.md index 0b419cc..5f43d70 100644 --- a/lectures/about.md +++ b/lectures/about.md @@ -1,62 +1,40 @@ -# About These Lectures +# 关于这些讲座 +## 简介 -## About +这个讲座系列使用基础数学、统计学及[Python](https://www.python.org/)编程代码介绍数量经济学。 -This lecture series introduces quantitative economics using elementary -mathematics and statistics plus computer code written in -[Python](https://www.python.org/). +讲座强调通过代码进行模拟和可视化来传达思想,而不是侧重于数学细节。 -The lectures emphasize simulation and visualization through code as a way to -convey ideas, rather than focusing on mathematical details. +虽然这种呈现方式相当新颖,但讨论的观点却是基础性的。 -Although the presentation is quite novel, the ideas are rather foundational. +我们强调经济理论的深刻和根本重要性,同时也重视分析数据和理解风格化事实的价值。 -We emphasize the deep and fundamental importance of economic theory, as well -as the value of analyzing data and understanding stylized facts. +这些讲座可以用于大学课程、自学、读书小组或研讨会。 -The lectures can be used for university courses, self-study, reading groups or -workshops. +研究人员和政策专业人士也可能会发现系列中的某些部分对他们的工作有价值。 -Researchers and policy professionals might also find some parts of the series -valuable for their work. +我们希望这些讲座对那些既想学习经济学又想学习计算的经济学学生感兴趣,以及对经济学感兴趣的计算机科学和工程领域的学生。 -We hope the lectures will be of interest to students of economics -who want to learn both economics and computing, as well as students from -fields such as computer science and engineering who are curious about -economics. +## 层级 -## Level +这个讲座系列面向本科学生。 -The lecture series is aimed at undergraduate students. +讲座的难度从真正的入门级(适合一年级本科生甚至高中生)到更中级的内容。 -The level of the lectures varies from truly introductory (suitable for first -year undergraduates or even high school students) to more intermediate. +更中级的讲座需要对线性代数有一定的了解,并且具有一些数学成熟度(例如,平静地阅读定理并试图理解它们的含义)。 -The -more intermediate lectures require comfort with linear algebra and some -mathematical maturity (e.g., calmly reading theorems and trying to understand -their meaning). +总的来说,较容易的讲座出现在讲座系列的前面,较难的讲座出现在后面。 -In general, easier lectures occur earlier in the lecture -series and harder lectures occur later. +我们假设读者已经学习了QuantEcon关于Python编程的基础部分[Python编程](https://python-programming.quantecon.org/intro.html)。 -We assume that readers have covered the easier parts of the QuantEcon lecture -series [on Python -programming](https://python-programming.quantecon.org/intro.html). +特别是,读者应该熟悉基本的Python语法,包括Python函数。了解类和Matplotlib会有帮助,但不是必须的。 -In -particular, readers should be familiar with basic Python syntax including -Python functions. Knowledge of classes and Matplotlib will be beneficial but -not essential. +## 致谢 -## Credits +在构建这个讲座系列时,我们得到了QuantEcon的研究助理以及我们QuantEcon同事的宝贵帮助。没有他们的帮助,这个系列是无法完成的。 -In building this lecture series, we had invaluable assistance from research -assistants at QuantEcon, as well as our QuantEcon colleagues. Without their -help this series would not have been possible. - -In particular, we sincerely thank and give credit to +特别感谢并致谢 - [Aakash Gupta](https://github.com/AakashGfude) - [Shu Hu](https://github.com/shlff) @@ -64,7 +42,8 @@ In particular, we sincerely thank and give credit to - [Smit Lunagariya](https://github.com/Smit-create) - [Matthew McKay](https://github.com/mmcky) - [Maanasee Sharma](https://github.com/maanasee) +- [Longye Tian](https://github.com/longye-tian) - [Humphrey Yang](https://github.com/HumphreyYang) +- [Sylvia Zhao](https://github.com/SylviaZhaooo) -We also thank Noritaka Kudoh for encouraging us to start this project and providing thoughtful suggestions. - +我们还感谢Noritaka Kudoh鼓励我们开始这个项目并提供了深思熟虑的建议。 \ No newline at end of file diff --git a/lectures/ar1_processes.md b/lectures/ar1_processes.md index fe54d3d..93cf599 100644 --- a/lectures/ar1_processes.md +++ b/lectures/ar1_processes.md @@ -19,36 +19,34 @@ kernelspec: ``` (ar1_processes)= -# AR(1) Processes +# AR(1) 过程 -```{index} single: Autoregressive processes +```{index} single: 自回归过程 ``` -## Overview +## 概览 -In this lecture we are going to study a very simple class of stochastic -models called AR(1) processes. +在这个讲座中,我们将研究一类非常简单的随机模型,称为 AR(1) 过程。 -These simple models are used again and again in economic research to represent the dynamics of series such as +这些简单的模型在经济研究中一次又一次地被用来表示诸如 -* labor income -* dividends -* productivity, etc. +* 劳动收入 +* 股息 +* 生产力等序列的动态。 -We are going to study AR(1) processes partly because they are useful and -partly because they help us understand important concepts. +我们之所以研究 AR(1) 过程,部分是因为它们很有用,部分是因为它们帮助我们理解很多非常重要的概念。 -Let's start with some imports: +让我们从一些导入开始: ```{code-cell} ipython import numpy as np import matplotlib.pyplot as plt -plt.rcParams["figure.figsize"] = (11, 5) #set default figure size +plt.rcParams["figure.figsize"] = (11, 5) # 设置默认图像大小 ``` -## The AR(1) model +## AR(1) 模型 -The **AR(1) model** (autoregressive model of order 1) takes the form +**AR(1) 模型**(一阶自回归模型)形式是: ```{math} :label: can_ar1 @@ -56,40 +54,34 @@ The **AR(1) model** (autoregressive model of order 1) takes the form X_{t+1} = a X_t + b + c W_{t+1} ``` -where $a, b, c$ are scalar-valued parameters +其中 $a, b, c$ 是标量参数 -(Equation {eq}`can_ar1` is sometimes called a **stochastic difference equation**.) +(方程 {eq}`can_ar1` 有时被称为 **随机差分方程**。) ```{prf:example} :label: ar1_ex_ar -For example, $X_t$ might be +例如,$X_t$ 可能是 -* the log of labor income for a given household, or -* the log of money demand in a given economy. +* 某个家庭的劳动收入对数,或 +* 某个经济体的货币需求对数。 -In either case, {eq}`can_ar1` shows that the current value evolves as a linear function -of the previous value and an IID shock $W_{t+1}$. +在两种情况下,{eq}`can_ar1` 显示当前值是通过上一个值的线性函数以及一个独立同分布的冲击 $W_{t+1}$ 来演变的。 -(We use $t+1$ for the subscript of $W_{t+1}$ because this random variable is not -observed at time $t$.) +(我们使用 $t+1$ 作为 $W_{t+1}$ 的下标,因为在时间 $t$ 这个随机变量还未被观察到。) ``` -The specification {eq}`can_ar1` generates a time series $\{ X_t\}$ as soon as we -specify an initial condition $X_0$. +一旦我们指定一个初始条件 $X_0$,我们就可以用{eq}`can_ar1` 生成一个时间序列 $\{ X_t\}$。 -To make things even simpler, we will assume that +为了使事情变得更简单,我们将假设 -* the process $\{ W_t \}$ is {ref}`IID ` and standard normal, -* the initial condition $X_0$ is drawn from the normal distribution $N(\mu_0, v_0)$ and -* the initial condition $X_0$ is independent of $\{ W_t \}$. +* 过程 $\{ W_t \}$ 是 {ref}`独立同分布 ` 且符合标准正态分布, +* 初始条件 $X_0$ 从正态分布 $N(\mu_0, v_0)$ 中抽取, +* 初始条件 $X_0$ 与 $\{ W_t \}$ 独立。 +### 移动平均表示 - - -### Moving average representation - -Iterating backwards from time $t$, we obtain +从时间 $t$ 向后迭代,我们得到 $$ X_t = a X_{t-1} + b + c W_t @@ -98,7 +90,7 @@ X_t = a X_{t-1} + b + c W_t = \cdots $$ -If we work all the way back to time zero, we get +如果我们一直追溯到零时,我们得到 ```{math} :label: ar1_ma @@ -107,66 +99,62 @@ X_t = a^t X_0 + b \sum_{j=0}^{t-1} a^j + c \sum_{j=0}^{t-1} a^j W_{t-j} ``` -Equation {eq}`ar1_ma` shows that $X_t$ is a well defined random variable, the value of which depends on +方程 {eq}`ar1_ma` 显示 $X_t$ 是一个明确定义的随机变量,其值取决于 -* the parameters, -* the initial condition $X_0$ and -* the shocks $W_1, \ldots W_t$ from time $t=1$ to the present. +* 参数, +* 初始条件 $X_0$ 和 +* 从时间 $t=1$ 到现在的冲击 $W_1, \ldots W_t$。 -Throughout, the symbol $\psi_t$ will be used to refer to the -density of this random variable $X_t$. +在整个过程中,符号 $\psi_t$ 将用来指代这个随机变量 $X_t$ 的密度。 -### Distribution dynamics +### 分布动态 -One of the nice things about this model is that it's so easy to trace out the sequence of distributions $\{ \psi_t \}$ corresponding to the time -series $\{ X_t\}$. +这个模型的一个好处是很容易追踪一系列分布 $\{ \psi_t \}$,这些分布对应于时间 +序列 $\{ X_t\}$ 的现实演变。具体来说,我们可以在每个日期 $t$ 上追踪观测到的 $X_t$ 的边际分布。 -To see this, we first note that $X_t$ is normally distributed for each $t$. +让我们看看我们如何做到这一点。 -This is immediate from {eq}`ar1_ma`, since linear combinations of independent -normal random variables are normal. +首先我们指出,对于每个时间 $t$,$X_t$ 是正态分布的。 -Given that $X_t$ is normally distributed, we will know the full distribution -$\psi_t$ if we can pin down its first two [moments](https://en.wikipedia.org/wiki/Moment_(mathematics)). +这是从 {eq}`ar1_ma` 显见的,因为独立正态随机变量的线性组合是正态分布的。 -Let $\mu_t$ and $v_t$ denote the mean and variance of $X_t$ respectively. +鉴于 $X_t$ 是正态分布的,如果我们能确定它的前两个[矩](https://en.wikipedia.org/wiki/Moment_(mathematics)),就可以知道完整的分布 $\psi_t$。 -We can pin down these values from {eq}`ar1_ma` or we can use the following -recursive expressions: +设 $\mu_t$ 和 $v_t$ 分别表示 $X_t$ 的均值和方差。 + +我们可以从 {eq}`ar1_ma` 确定这些值,或者我们可以使用以下递归表达式: ```{math} :label: dyn_tm \mu_{t+1} = a \mu_t + b -\quad \text{and} \quad +\quad ext{和} \quad v_{t+1} = a^2 v_t + c^2 ``` -These expressions are obtained from {eq}`can_ar1` by taking, respectively, the expectation and variance of both sides of the equality. +通过分别取等式两侧的期望和方差,从 {eq}`can_ar1` 得到这些表达式。 -In calculating the second expression, we are using the fact that $X_t$ -and $W_{t+1}$ are independent. +在计算第二个表达式时,我们利用了 $X_t$ 和 $W_{t+1}$ 的独立性。 -(This follows from our assumptions and {eq}`ar1_ma`.) +(这是根据我们的假设和 {eq}`ar1_ma` 得出的。) -Given the dynamics in {eq}`ar1_ma` and initial conditions $\mu_0, -v_0$, we obtain $\mu_t, v_t$ and hence +给定 {eq}`ar1_ma` 中的动态和初始条件 $\mu_0, v_0$,我们得到 $\mu_t, v_t$,因此 $$ \psi_t = N(\mu_t, v_t) $$ -The following code uses these facts to track the sequence of marginal distributions $\{ \psi_t \}$. +下面的代码利用这些事实来追踪边际分布序列 $\{ \psi_t \}$。 -The parameters are +参数是 ```{code-cell} python3 a, b, c = 0.9, 0.1, 0.5 -mu, v = -3.0, 0.6 # initial conditions mu_0, v_0 +mu, v = -3.0, 0.6 # 初始条件 mu_0, v_0 ``` -Here's the sequence of distributions: +以下是分布序列: ```{code-cell} python3 from scipy.stats import norm @@ -188,23 +176,19 @@ ax.legend(bbox_to_anchor=[1.05,1],loc=2,borderaxespad=1) plt.show() ``` +## 平稳性和渐近稳定性 +当我们使用模型来研究现实世界时,通常希望我们的模型具有清晰准确的预测。 -## Stationarity and asymptotic stability - -When we use models to study the real world, it is generally preferable that our -models have clear, sharp predictions. - -For dynamic problems, sharp predictions are related to stability. +对于动态问题,清晰的预测与稳定性有关。 -For example, if a dynamic model predicts that inflation always converges to some -kind of steady state, then the model gives a sharp prediction. +例如,如果一个动态模型预测通货膨胀总是收敛到某种稳态,那么这个模型提供了一个明确的预测。 -(The prediction might be wrong, but even this is helpful, because we can judge the quality of the model.) +(预测可能是错误的,但即便如此,它也是有帮助的,因为我们可以判断模型的质量。) -Notice that, in the figure above, the sequence $\{ \psi_t \}$ seems to be converging to a limiting distribution, suggesting some kind of stability. +注意,在上图中,序列 $\{ \psi_t \}$ 似乎正在收敛到一个极限分布,这表明存在某种稳定性。 -This is even clearer if we project forward further into the future: +如果我们进一步向未来进行投影,这一点就更加明显: ```{code-cell} python3 def plot_density_seq(ax, mu_0=-3.0, v_0=0.6, sim_length=40): @@ -221,75 +205,69 @@ plot_density_seq(ax) plt.show() ``` -Moreover, the limit does not depend on the initial condition. +此外,极限不依赖于初始条件。 -For example, this alternative density sequence also converges to the same limit. +例如,另一个密度序列也会收敛到相同的极限。 ```{code-cell} python3 fig, ax = plt.subplots() plot_density_seq(ax, mu_0=4.0) plt.show() ``` +事实上,可以很容易地证明,只要 $|a| < 1$,不管初始条件如何,都会发生这种收敛。 -In fact it's easy to show that such convergence will occur, regardless of the initial condition, whenever $|a| < 1$. +为了看到这一点,我们只需查看前两个矩的动态, +如 {eq}`dyn_tm` 中所给出的。 -To see this, we just have to look at the dynamics of the first two moments, as -given in {eq}`dyn_tm`. - -When $|a| < 1$, these sequences converge to the respective limits +当 $|a| < 1$ 时,这些序列会收敛到各自的极限 ```{math} :label: mu_sig_star \mu^* := \frac{b}{1-a} -\quad \text{and} \quad +\quad 和 \quad v^* = \frac{c^2}{1 - a^2} ``` -(See our {doc}`lecture on one dimensional dynamics ` for background on deterministic convergence.) +(请参阅我们的 {doc}`一维动力学讲座 `,了解确定性收敛的背景。) -Hence +因此 ```{math} :label: ar1_psi_star \psi_t \to \psi^* = N(\mu^*, v^*) -\quad \text{as } +\quad 当 t \to \infty ``` -We can confirm this is valid for the sequence above using the following code. +我们可以使用以下代码确认这对于上面的序列是有效的。 ```{code-cell} python3 fig, ax = plt.subplots() plot_density_seq(ax, mu_0=4.0) mu_star = b / (1 - a) -std_star = np.sqrt(c**2 / (1 - a**2)) # square root of v_star +std_star = np.sqrt(c**2 / (1 - a**2)) # v_star的平方根 psi_star = norm.pdf(grid, loc=mu_star, scale=std_star) ax.plot(grid, psi_star, 'k-', lw=2, label="$\psi^*$") ax.legend() plt.show() ``` +请注意,根据上述参数,我们看到序列 $\{ \psi_t \}$ 收敛到 $\psi^*$。 -As claimed, the sequence $\{ \psi_t \}$ converges to $\psi^*$. - -We see that, at least for these parameters, the AR(1) model has strong stability -properties. - - - +我们看到,至少对于这些参数,AR(1) 模型具有很强的稳定性特性。 -### Stationary distributions +### 平稳分布 -Let's try to better understand the limiting distribution $\psi^*$. +让我们更好地理解极限分布 $\psi^*$。 -A stationary distribution is a distribution that is a "fixed point" of the update rule for the AR(1) process. +平稳分布是 AR(1) 过程更新规则的一个“固定点”。 -In other words, if $\psi_t$ is stationary, then $\psi_{t+j} = \psi_t$ for all $j$ in $\mathbb N$. +换句话说,如果 $\psi_t$ 是平稳的,那么对所有 $j$,$\psi_{t+j} = \psi_t$ 在 $\mathbb N$ 中成立。 -A different way to put this, specialized to the current setting, is as follows: a density $\psi$ on $\mathbb R$ is **stationary** for the AR(1) process if +另一种针对当前设置的说法是:如果一个在 $\mathbb R$ 上的密度 $\psi$ 对 AR(1) 过程是**平稳的**,则有 $$ X_t \sim \psi @@ -297,96 +275,91 @@ X_t \sim \psi a X_t + b + c W_{t+1} \sim \psi $$ -The distribution $\psi^*$ in {eq}`ar1_psi_star` has this property --- -checking this is an exercise. +$\psi^*$ 在 {eq}`ar1_psi_star` 中具有这一性质——检查这一点是一个练习。 -(Of course, we are assuming that $|a| < 1$ so that $\psi^*$ is -well defined.) +(当然,我们假设 $|a| < 1$ 从而 $\psi^*$ 是 +良定义的。) -In fact, it can be shown that no other distribution on $\mathbb R$ has this property. +事实上,可以证明 $\mathbb R$ 上没有其他分布具有这一性质。 -Thus, when $|a| < 1$, the AR(1) model has exactly one stationary density and that density is given by $\psi^*$. +因此,当 $|a| < 1$ 时,AR(1) 模型恰好有一个平稳密度,那就是 $\psi^*$。 -## Ergodicity +## 遍历性 -The concept of ergodicity is used in different ways by different authors. +不同的作者使用遍历性这一概念有不同的方式。 -One way to understand it in the present setting is that a version of the law -of large numbers is valid for $\{X_t\}$, even though it is not IID. +在当前设定中理解它的一种方式是,即使 $\{X_t\}$ 不是独立同分布的,大数定律也是有效的。 -In particular, averages over time series converge to expectations under the -stationary distribution. +特别是,时间序列的平均值收敛于平稳分布下的期望值。 -Indeed, it can be proved that, whenever $|a| < 1$, we have +实际上,可以证明,只要 $|a| < 1$,我们就有 ```{math} :label: ar1_ergo -\frac{1}{m} \sum_{t = 1}^m h(X_t) \to +\frac{1}{m} \sum_{t = 1}^m h(X_t) \to \int h(x) \psi^*(x) dx - \quad \text{as } m \to \infty + \quad 当 m \to \infty ``` -whenever the integral on the right hand side is finite and well defined. +只要右侧的积分是有限且良定义的。 -Notes: +注意: -* In {eq}`ar1_ergo`, convergence holds with probability one. -* The textbook by {cite}`MeynTweedie2009` is a classic reference on ergodicity. +* 在 {eq}`ar1_ergo` 中,收敛性以概率1成立。 +* 由 {cite}`MeynTweedie2009` 编写的教科书是关于遍历性的经典参考。 ```{prf:example} :label: ar1_ex_id -If we consider the identity function $h(x) = x$, we get +如果我们考虑恒等函数 $h(x) = x$,我们得到 $$ -\frac{1}{m} \sum_{t = 1}^m X_t \to +\frac{1}{m} \sum_{t = 1}^m X_t \to \int x \psi^*(x) dx - \quad \text{as } m \to \infty + \quad \text{当 } m \to \infty $$ -In other words, the time series sample mean converges to the mean of the stationary distribution. +即,时间序列样本均值收敛于平稳分布的均值。 ``` -Ergodicity is important for a range of reasons. +出于多种原因,遍历性非常重要。 -For example, {eq}`ar1_ergo` can be used to test theory. +例如,{eq}`ar1_ergo` 可用来测试理论。 -In this equation, we can use observed data to evaluate the left hand side of {eq}`ar1_ergo`. +在这个方程中,我们可以使用观测数据来评估 {eq}`ar1_ergo` 的左侧。 -And we can use a theoretical AR(1) model to calculate the right hand side. +我们可以使用理论的 AR(1) 模型来计算右侧。 -If $\frac{1}{m} \sum_{t = 1}^m X_t$ is not close to $\psi^(x)$, even for many -observations, then our theory seems to be incorrect and we will need to revise -it. +如果 $\frac{1}{m} \sum_{t = 1}^m X_t$ 即使在很多观测下也不接近 $\psi^*(x)$,那么我们的理论似乎是错误的,我们将需要修订它。 -## Exercises +## 练习 ```{exercise} :label: ar1p_ex1 -Let $k$ be a natural number. +假设 $k$ 是一个自然数。 -The $k$-th central moment of a random variable is defined as +随机变量的 $k$ 阶中心矩定义为 $$ M_k := \mathbb E [ (X - \mathbb E X )^k ] $$ -When that random variable is $N(\mu, \sigma^2)$, it is known that +当这个随机变量是 $N(\mu, \sigma^2)$ 时,已知 $$ M_k = \begin{cases} - 0 & \text{ if } k \text{ is odd} \\ - \sigma^k (k-1)!! & \text{ if } k \text{ is even} + 0 & 如果 k 是奇数 \\ + \sigma^k (k-1)!! & 如果 k 是偶数 \end{cases} $$ -Here $n!!$ is the [double factorial](https://en.wikipedia.org/wiki/Double_factorial). +这里 $n!!$ 是[双阶乘](https://en.wikipedia.org/wiki/Double_factorial)。 -According to {eq}`ar1_ergo`, we should have, for any $k \in \mathbb N$, +根据 {eq}`ar1_ergo`, 对于任何 $k \in \mathbb N$, $$ \frac{1}{m} \sum_{t = 1}^m @@ -394,9 +367,9 @@ $$ \approx M_k $$ -when $m$ is large. +当 $m$ 是较大时。 -Confirm this by simulation at a range of $k$ using the default parameters from the lecture. +通过仿真验证一系列 $k$,使用讲座中的默认参数。 ``` @@ -404,7 +377,7 @@ Confirm this by simulation at a range of $k$ using the default parameters from t :class: dropdown ``` -Here is one solution: +这是一个解法: ```{code-cell} python3 from numba import njit @@ -435,8 +408,8 @@ for k_idx, k in enumerate(k_vals): true_moments[k_idx] = true_moments_ar1(k) fig, ax = plt.subplots() -ax.plot(k_vals, true_moments, label="true moments") -ax.plot(k_vals, sample_moments, label="sample moments") +ax.plot(k_vals, true_moments, label="真实矩") +ax.plot(k_vals, sample_moments, label="样本矩") ax.legend() plt.show() @@ -445,58 +418,42 @@ plt.show() ```{solution-end} ``` - ```{exercise} :label: ar1p_ex2 -Write your own version of a one dimensional [kernel density -estimator](https://en.wikipedia.org/wiki/Kernel_density_estimation), -which estimates a density from a sample. +编写一个一维[核密度估计](https://en.wikipedia.org/wiki/Kernel_density_estimation)的版本,用于从样本中估计密度。 -Write it as a class that takes the data $X$ and bandwidth -$h$ when initialized and provides a method $f$ such that +将其写为一个类,该类在初始化时接受数据 $X$ 和带宽 $h$,并提供一个方法 $f$,使得 $$ f(x) = \frac{1}{hn} \sum_{i=1}^n K \left( \frac{x-X_i}{h} \right) $$ -For $K$ use the Gaussian kernel ($K$ is the standard normal -density). +对于 $K$,使用高斯核($K$ 是标准正态密度函数)。 -Write the class so that the bandwidth defaults to Silverman’s rule (see -the “rule of thumb” discussion on [this -page](https://en.wikipedia.org/wiki/Kernel_density_estimation)). Test -the class you have written by going through the steps +编写该类,使得带宽默认遵循 Silverman 的规则(参见[此页面](https://en.wikipedia.org/wiki/Kernel_density_estimation)中的“经验规则”讨论)。通过以下步骤测试你编写的类: -1. simulate data $X_1, \ldots, X_n$ from distribution $\phi$ -1. plot the kernel density estimate over a suitable range -1. plot the density of $\phi$ on the same figure +1. 从分布 $\phi$ 中模拟数据 $X_1, \ldots, X_n$ +1. 在适当的范围内绘制核密度估计 +1. 在同一图形上绘制 $\phi$ 的密度 -for distributions $\phi$ of the following types +分布 $\phi$ 类型如下: -- [beta - distribution](https://en.wikipedia.org/wiki/Beta_distribution) - with $\alpha = \beta = 2$ -- [beta - distribution](https://en.wikipedia.org/wiki/Beta_distribution) - with $\alpha = 2$ and $\beta = 5$ -- [beta - distribution](https://en.wikipedia.org/wiki/Beta_distribution) - with $\alpha = \beta = 0.5$ +- [beta 分布](https://en.wikipedia.org/wiki/Beta_distribution),$\alpha = \beta = 2$ +- [beta 分布](https://en.wikipedia.org/wiki/Beta_distribution),$\alpha = 2$ 且 $\beta = 5$ +- [beta 分布](https://en.wikipedia.org/wiki/Beta_distribution),$\alpha = \beta = 0.5$ -Use $n=500$. +使用 $n=500$。 -Make a comment on your results. (Do you think this is a good estimator -of these distributions?) +对结果进行评论。(你认为这是这些分布的良好估计吗?) ``` - ```{solution-start} ar1p_ex2 :class: dropdown ``` -Here is one solution: +这是一个解决方案: ```{code-cell} ipython3 K = norm.pdf @@ -529,8 +486,8 @@ def plot_kde(ϕ, x_min=-0.2, x_max=1.2): x_grid = np.linspace(-0.2, 1.2, 100) fig, ax = plt.subplots() - ax.plot(x_grid, kde.f(x_grid), label="estimate") - ax.plot(x_grid, ϕ.pdf(x_grid), label="true density") + ax.plot(x_grid, kde.f(x_grid), label="估计值") + ax.plot(x_grid, ϕ.pdf(x_grid), label="真实密度") ax.legend() plt.show() ``` @@ -544,8 +501,7 @@ for α, β in parameter_pairs: plot_kde(beta(α, β)) ``` -We see that the kernel density estimator is effective when the underlying -distribution is smooth but less so otherwise. +我们可以看到,当基础分布是平滑时,核密度估计器是有效的,但在其他情况下则效果不佳。 ```{solution-end} ``` @@ -554,20 +510,20 @@ distribution is smooth but less so otherwise. ```{exercise} :label: ar1p_ex3 -In the lecture we discussed the following fact: for the $AR(1)$ process +在讲座中我们讨论了以下事实:对于 $AR(1)$ 过程 $$ X_{t+1} = a X_t + b + c W_{t+1} $$ -with $\{ W_t \}$ iid and standard normal, +其中 $\{ W_t \}$ 独立同分布且标准正态, $$ \psi_t = N(\mu, s^2) \implies \psi_{t+1} = N(a \mu + b, a^2 s^2 + c^2) $$ -Confirm this, at least approximately, by simulation. Let +通过仿真确认这一点,至少是近似的。设定 - $a = 0.9$ - $b = 0.0$ @@ -575,29 +531,22 @@ Confirm this, at least approximately, by simulation. Let - $\mu = -3$ - $s = 0.2$ -First, plot $\psi_t$ and $\psi_{t+1}$ using the true -distributions described above. +首先,使用上述真实分布绘制 $\psi_t$ 和 $\psi_{t+1}$。 -Second, plot $\psi_{t+1}$ on the same figure (in a different -color) as follows: +其次,在同一个图形上(用不同的颜色),绘制 $\psi_{t+1}$ 如下: -1. Generate $n$ draws of $X_t$ from the $N(\mu, s^2)$ - distribution -1. Update them all using the rule - $X_{t+1} = a X_t + b + c W_{t+1}$ -1. Use the resulting sample of $X_{t+1}$ values to produce a - density estimate via kernel density estimation. +1. 从分布 $N(\mu, s^2)$ 中生成 $n$ 次 $X_t$ 抽样 +1. 使用规则 $X_{t+1} = a X_t + b + c W_{t+1}$ 更新它们所有 +1. 使用 $X_{t+1}$ 的结果样本产生通过核密度估计的密度估计。 -Try this for $n=2000$ and confirm that the -simulation based estimate of $\psi_{t+1}$ does converge to the -theoretical distribution. +尝试 $n=2000$ 并确认基于抽样实验的 $\psi_{t+1}$ 估计会收敛到理论分布。 ``` ```{solution-start} ar1p_ex3 :class: dropdown ``` -Here is our solution +这是我们的解决方案 ```{code-cell} ipython3 a = 0.9 @@ -606,28 +555,25 @@ c = 0.1 μ = -3 s = 0.2 ``` +```python +n = 2000 # 样本数 -```{code-cell} ipython3 +# 理论上的分布 μ_next = a * μ + b s_next = np.sqrt(a**2 * s**2 + c**2) -``` -```{code-cell} ipython3 -ψ = lambda x: K((x - μ) / s) -ψ_next = lambda x: K((x - μ_next) / s_next) -``` - -```{code-cell} ipython3 +# 理论密度 ψ = norm(μ, s) ψ_next = norm(μ_next, s_next) -``` -```{code-cell} ipython3 -n = 2000 -x_draws = ψ.rvs(n) -x_draws_next = a * x_draws + b + c * np.random.randn(n) +# 使用仿真值来估计ψ_{t+1} +x_draws = ψ.rvs(n) # 从ψ生成样本 +x_draws_next = a * x_draws + b + c * np.random.randn(n) # 更新规则 + +# 对仿真结果进行核密度估计 kde = KDE(x_draws_next) +# 绘制结果 x_grid = np.linspace(μ - 1, μ + 1, 100) fig, ax = plt.subplots() @@ -639,8 +585,7 @@ ax.legend() plt.show() ``` -The simulated distribution approximately coincides with the theoretical -distribution, as predicted. +正如预测的那样,模拟分布与理论分布大致吻合。 ```{solution-end} ``` diff --git a/lectures/cagan_adaptive.md b/lectures/cagan_adaptive.md index f3d48c3..3cb8296 100644 --- a/lectures/cagan_adaptive.md +++ b/lectures/cagan_adaptive.md @@ -11,91 +11,81 @@ kernelspec: name: python3 --- -# Monetarist Theory of Price Levels with Adaptive Expectations +# 具有自适应预期的价格水平货币主义理论 -## Overview +## 引言 +本讲座是另一讲座 {doc}`价格水平的货币主义理论 ` 的续集或前传。 -This lecture is a sequel or prequel to {doc}`cagan_ree`. +我们将使用线性代数对另一种"货币主义"或"财政"价格水平理论进行一些实验。 -We'll use linear algebra to do some experiments with an alternative "monetarist" or "fiscal" theory of price levels. +与 {doc}`价格水平的货币主义理论 ` 讲座中的模型类似,该模型断言,当政府持续支出超过税收并通过印钞来弥补赤字时,会对价格水平产生上行压力并产生持续通货膨胀。 -Like the model in {doc}`cagan_ree`, the model asserts that when a government persistently spends more than it collects in taxes and prints money to finance the shortfall, it puts upward pressure on the price level and generates persistent inflation. +与 {doc}`价格水平的货币主义理论 ` 讲座中的"完美预见"或"理性预期"版本不同,本讲座中的模型是菲利普·凯根 {cite}`Cagan` 用于研究恶性通货膨胀货币动态的"自适应预期"版本。 -Instead of the "perfect foresight" or "rational expectations" version of the model in {doc}`cagan_ree`, our model in the present lecture is an "adaptive expectations" version of a model that {cite}`Cagan` used to study the monetary dynamics of hyperinflations. +它结合了以下组成部分: -It combines these components: +* 一个实际货币余额需求函数,断言所需实际余额对数与公众预期通货膨胀率成反比 -* a demand function for real money balances that asserts that the logarithm of the quantity of real balances demanded depends inversely on the public's expected rate of inflation +* 一个**自适应预期**模型,描述公众预期通货膨胀率如何对过去实际通货膨胀值做出反应 -* an **adaptive expectations** model that describes how the public's anticipated rate of inflation responds to past values of actual inflation +* 一个使货币需求等于供给的均衡条件 -* an equilibrium condition that equates the demand for money to the supply +* 货币供应增长率的外生序列 -* an exogenous sequence of rates of growth of the money supply +我们的模型非常接近凯根的原始规范。 -Our model stays quite close to Cagan's original specification. +与 {doc}`现值 ` 和 {doc}`消费平滑 ` 讲座一样,我们将使用的唯一线性代数运算是矩阵乘法和矩阵求逆。 -As in {doc}`pv` and {doc}`cons_smooth`, the only linear algebra operations that we'll be using are matrix multiplication and matrix inversion. +为了便于使用线性矩阵代数作为我们的主要数学工具,我们将使用模型的有限时域版本。 -To facilitate using linear matrix algebra as our principal mathematical tool, we'll use a finite horizon version of -the model. +## 模型结构 -## Structure of the model +令 -Let +* $ m_t $ 为名义货币余额供给的对数; +* $\mu_t = m_{t+1} - m_t $ 为名义余额的净增长率; +* $p_t $ 为价格水平的对数; +* $\pi_t = p_{t+1} - p_t $ 为 $t$ 和 $ t+1$ 之间的净通货膨胀率; +* $\pi_t^*$ 为公众预期的 $t$ 和 $t+1$ 之间的通货膨胀率; +* $T$ 为时域 -- 即模型将确定 $p_t$ 的最后一个时期 +* $\pi_0^*$ 为公众最初预期的时间 0 和时间 1 之间的通货膨胀率。 + +实际余额 $\exp\left(\frac{m_t^d}{p_t}\right)$ 的需求由以下版本的Cagan需求函数支配 -* $ m_t $ be the log of the supply of nominal money balances; -* $\mu_t = m_{t+1} - m_t $ be the net rate of growth of nominal balances; -* $p_t $ be the log of the price level; -* $\pi_t = p_{t+1} - p_t $ be the net rate of inflation between $t$ and $ t+1$; -* $\pi_t^*$ be the public's expected rate of inflation between $t$ and $t+1$; -* $T$ the horizon -- i.e., the last period for which the model will determine $p_t$ -* $\pi_0^*$ public's initial expected rate of inflation between time $0$ and time $1$. - - -The demand for real balances $\exp\left(m_t^d-p_t\right)$ is governed by the following version of the Cagan demand function - $$ m_t^d - p_t = -\alpha \pi_t^* \: , \: \alpha > 0 ; \quad t = 0, 1, \ldots, T . $$ (eq:caganmd_ad) +该方程断言,实际余额需求与公众预期的通货膨胀率成反比。 -This equation asserts that the demand for real balances -is inversely related to the public's expected rate of inflation with sensitivity $\alpha$. - -Equating the logarithm $m_t^d$ of the demand for money to the logarithm $m_t$ of the supply of money in equation {eq}`eq:caganmd_ad` and solving for the logarithm $p_t$ -of the price level gives +在方程 {eq}`eq:caganmd_ad` 中将货币需求的对数 $m_t^d$ 等同于货币供给的对数 $m_t$,并求解价格水平的对数 $p_t$,得到 $$ p_t = m_t + \alpha \pi_t^* $$ (eq:eqfiscth1) -Taking the difference between equation {eq}`eq:eqfiscth1` at time $t+1$ and at time -$t$ gives +取方程 {eq}`eq:eqfiscth1` 在时间 $t+1$ 和时间 $t$ 的差,得到 $$ \pi_t = \mu_t + \alpha \pi_{t+1}^* - \alpha \pi_t^* $$ (eq:eqpipi) -We assume that the expected rate of inflation $\pi_t^*$ is governed -by the following adaptive expectations scheme proposed by {cite}`Friedman1956` and {cite}`Cagan`, where $\lambda\in [0,1]$ denotes the weight on expected inflation. +我们假设预期通货膨胀率 $\pi_t^*$ 由弗里德曼-凯根自适应预期方案支配 $$ \pi_{t+1}^* = \lambda \pi_t^* + (1 -\lambda) \pi_t $$ (eq:adaptexpn) -As exogenous inputs into the model, we take initial conditions $m_0, \pi_0^*$ -and a money growth sequence $\mu = \{\mu_t\}_{t=0}^T$. +作为模型的外生输入,我们采用初始条件 $m_0, \pi_0^*$ 和货币增长序列 $\mu = \{\mu_t\}_{t=0}^T$。 -As endogenous outputs of our model we want to find sequences $\pi = \{\pi_t\}_{t=0}^T, p = \{p_t\}_{t=0}^T$ as functions of the exogenous inputs. +作为模型的内生输出,我们希望找到序列 $\pi = \{\pi_t\}_{t=0}^T, p = \{p_t\}_{t=0}^T$ 作为内生输入的函数。 -We'll do some mental experiments by studying how the model outputs vary as we vary -the model inputs. +我们将通过研究模型输出如何随模型输入的变化而变化来进行一些思想实验。 -## Representing key equations with linear algebra +## 用线性代数表示关键方程 -We begin by writing the equation {eq}`eq:adaptexpn` adaptive expectations model for $\pi_t^*$ for $t=0, \ldots, T$ as +我们首先将方程 {eq}`eq:adaptexpn` 自适应预期模型写成 $t=0, \ldots, T$ 的形式 $$ \begin{bmatrix} 1 & 0 & 0 & \cdots & 0 & 0 \cr @@ -121,16 +111,15 @@ $$ \begin{bmatrix} \pi_0^* \cr 0 \cr 0 \cr \vdots \cr 0 \end{bmatrix} $$ -Write this equation as +将此方程写成 $$ A \pi^* = (1-\lambda) B \pi + \pi_0^* $$ (eq:eq1) -where the $(T+2) \times (T+2) $matrix $A$, the $(T+2)\times (T+1)$ matrix $B$, and the vectors $\pi^* , \pi_0, \pi_0^*$ -are defined implicitly by aligning these two equations. +其中 $(T+2) \times (T+2)$ 矩阵 $A$、$(T+2)\times (T+1)$ 矩阵 $B$ 以及向量 $\pi^* , \pi_0, \pi_0^*$ 通过对齐这两个方程隐式定义。 -Next we write the key equation {eq}`eq:eqpipi` in matrix notation as +接下来,我们将关键方程 {eq}`eq:eqpipi` 写成矩阵形式 $$ \begin{bmatrix} @@ -151,21 +140,19 @@ $$ \end{bmatrix} $$ -Represent the previous equation system in terms of vectors and matrices as +用向量和矩阵表示上述方程系统 $$ \pi = \mu + C \pi^* $$ (eq:eq2) -where the $(T+1) \times (T+2)$ matrix $C$ is defined implicitly to align this equation with the preceding -equation system. +其中 $(T+1) \times (T+2)$ 矩阵 $C$ 隐式定义,以使此方程与前面的方程系统对齐。 -## Harvesting insights from our matrix formulation +## 从矩阵表述中获得洞见 -We now have all of the ingredients we need to solve for $\pi$ as -a function of $\mu, \pi_0, \pi_0^*$. +我们现在拥有了求解 $\pi$ 作为 $\mu, \pi_0, \pi_0^*$ 函数所需的所有要素。 -Combine equations {eq}`eq:eq1`and {eq}`eq:eq2` to get +结合方程 {eq}`eq:eq1` 和 {eq}`eq:eq2`,得到 $$ \begin{aligned} @@ -174,43 +161,35 @@ A \pi^* & = (1-\lambda) B \pi + \pi_0^* \cr \end{aligned} $$ -which implies that +这意味着 $$ \left[ A - (1-\lambda) B C \right] \pi^* = (1-\lambda) B \mu+ \pi_0^* $$ -Multiplying both sides of the above equation by the inverse of the matrix on the left side gives +将上述方程两边乘以左侧矩阵的逆,得到 $$ \pi^* = \left[ A - (1-\lambda) B C \right]^{-1} \left[ (1-\lambda) B \mu+ \pi_0^* \right] $$ (eq:eq4) -Having solved equation {eq}`eq:eq4` for $\pi^*$, we can use equation {eq}`eq:eq2` to solve for $\pi$: +求解方程 {eq}`eq:eq4` 得到 $\pi^*$ 后,我们可以使用方程 {eq}`eq:eq2` 求解 $\pi$: $$ \pi = \mu + C \pi^* $$ +我们因此解决了我们模型所决定的两个关键内生时间序列,即预期通货膨胀率序列 $\pi^*$ 和实际通货膨胀率序列 $\pi$。 -We have thus solved for two of the key endogenous time series determined by our model, namely, the sequence $\pi^*$ -of expected inflation rates and the sequence $\pi$ of actual inflation rates. - -Knowing these, we can then quickly calculate the associated sequence $p$ of the logarithm of the price level -from equation {eq}`eq:eqfiscth1`. - -Let's fill in the details for this step. - -Since we now know $\mu$ it is easy to compute $m$. - -Thus, notice that we can represent the equations +知道了这些,我们就可以从方程 {eq}`eq:eqfiscth1` 快速计算出相关的价格水平对数序列 $p$。 +让我们填补这一步骤的细节。 +既然我们现在知道了 $\mu$,计算 $m$ 就很容易了。 +因此,注意到我们可以将方程 $$ m_{t+1} = m_t + \mu_t , \quad t = 0, 1, \ldots, T $$ - -as the matrix equation - +表示为矩阵方程 $$ \begin{bmatrix} 1 & 0 & 0 & \cdots & 0 & 0 \cr @@ -231,25 +210,19 @@ m_0 \cr 0 \cr 0 \cr \vdots \cr 0 \cr 0 \end{bmatrix} $$ (eq:eq101_ad) -Multiplying both sides of equation {eq}`eq:eq101_ad` with the inverse of the matrix on the left will give +将方程 {eq}`eq:eq101_ad` 的两边都乘以左侧矩阵的逆矩阵,将得到 $$ m_t = m_0 + \sum_{s=0}^{t-1} \mu_s, \quad t =1, \ldots, T+1 $$ (eq:mcum_ad) -Equation {eq}`eq:mcum_ad` shows that the log of the money supply at $t$ equals the log $m_0$ of the initial money supply -plus accumulation of rates of money growth between times $0$ and $t$. - -We can then compute $p_t$ for each $t$ from equation {eq}`eq:eqfiscth1`. - -We can write a compact formula for $p $ as - +方程 {eq}`eq:mcum_ad` 表明,时间 $t$ 的货币供应对数等于初始货币供应对数 $m_0$ 加上 $0$ 到 $t$ 时间之间货币增长率的累积。 +然后我们可以从方程 {eq}`eq:eqfiscth1` 计算每个 $t$ 的 $p_t$。 +我们可以为 $p$ 写一个紧凑的公式 $$ p = m + \alpha \hat \pi^* $$ - -where - +其中 $$ \hat \pi^* = \begin{bmatrix} \pi_0^* \cr \pi_1^* \cr @@ -258,32 +231,26 @@ $$ \pi_{T}^* \end{bmatrix}, $$ - -which is just $\pi^*$ with the last element dropped. +这只是去掉最后一个元素的 $\pi^*$。 -## Forecast errors and model computation - -Our computations will verify that +## 预测误差 +我们的计算将验证 $$ \hat \pi^* \neq \pi, $$ - -so that in general - +因此通常 $$ \pi_t^* \neq \pi_t, \quad t = 0, 1, \ldots , T $$ (eq:notre) -This outcome is typical in models in which adaptive expectations hypothesis like equation {eq}`eq:adaptexpn` appear as a -component. +这种结果在包含像方程 {eq}`eq:adaptexpn` 这样的适应性预期假设作为组成部分的模型中很典型。 -In {doc}`cagan_ree`, we studied a version of the model that replaces hypothesis {eq}`eq:adaptexpn` with -a "perfect foresight" or "rational expectations" hypothesis. +在讲座 {doc}`价格水平的货币主义理论 ` 中,我们研究了用"完美预见"或"理性预期"假设替代假设 {eq}`eq:adaptexpn` 的模型版本。 -But now, let's dive in and do some computations with the adaptive expectations version of the model. +但现在,让我们深入并用适应性预期版本的模型进行一些计算。 +像往常一样,我们将从导入一些 Python 模块开始。 -As usual, we'll start by importing some Python modules. ```{code-cell} ipython3 import numpy as np @@ -295,39 +262,58 @@ import matplotlib.pyplot as plt Cagan_Adaptive = namedtuple("Cagan_Adaptive", ["α", "m0", "Eπ0", "T", "λ"]) -def create_cagan_adaptive_model(α = 5, m0 = 1, Eπ0 = 0.5, T=80, λ = 0.9): +def create_cagan_model(α, m0, Eπ0, T, λ): return Cagan_Adaptive(α, m0, Eπ0, T, λ) +``` ++++ {"user_expressions": []} -md = create_cagan_adaptive_model() +这里我们定义这些参数。 + +```{code-cell} ipython3 +# 参数 +T = 80 +T1 = 60 +α = 5 +λ = 0.9 # 0.7 +m0 = 1 + +μ0 = 0.5 +μ_star = 0 + +md = create_cagan_model(α=α, m0=m0, Eπ0=μ0, T=T, λ=λ) ``` +++ {"user_expressions": []} -We solve the model and plot variables of interests using the following functions. + +我们用以下的函数来求解模型并且绘制这些变量。 + ```{code-cell} ipython3 -def solve_cagan_adaptive(model, μ_seq): - " Solve the Cagan model in finite time. " - α, m0, Eπ0, T, λ = model +def solve(model, μ_seq): + " 在求解有限时间的凯根模型" + + model_params = model.α, model.m0, model.Eπ0, model.T, model.λ + α, m0, Eπ0, T, λ = model_params A = np.eye(T+2, T+2) - λ*np.eye(T+2, T+2, k=-1) B = np.eye(T+2, T+1, k=-1) C = -α*np.eye(T+1, T+2) + α*np.eye(T+1, T+2, k=1) Eπ0_seq = np.append(Eπ0, np.zeros(T+1)) - # Eπ_seq is of length T+2 - Eπ_seq = np.linalg.solve(A - (1-λ)*B @ C, (1-λ) * B @ μ_seq + Eπ0_seq) + # Eπ_seq 的长度为 T+2 + Eπ_seq = np.linalg.inv(A - (1-λ)*B @ C) @ ((1-λ) * B @ μ_seq + Eπ0_seq) - # π_seq is of length T+1 + # π_seq 的长度为 T+1 π_seq = μ_seq + C @ Eπ_seq - D = np.eye(T+1, T+1) - np.eye(T+1, T+1, k=-1) # D is the coefficient matrix in Equation (14.8) + D = np.eye(T+1, T+1) - np.eye(T+1, T+1, k=-1) m0_seq = np.append(m0, np.zeros(T)) - # m_seq is of length T+2 - m_seq = np.linalg.solve(D, μ_seq + m0_seq) + # m_seq 的长度为 T+2 + m_seq = np.linalg.inv(D) @ (μ_seq + m0_seq) m_seq = np.append(m0, m_seq) - # p_seq is of length T+2 + # p_seq 的长度为 T+2 p_seq = m_seq + α * Eπ_seq return π_seq, Eπ_seq, m_seq, p_seq @@ -338,7 +324,7 @@ def solve_cagan_adaptive(model, μ_seq): ```{code-cell} ipython3 def solve_and_plot(model, μ_seq): - π_seq, Eπ_seq, m_seq, p_seq = solve_cagan_adaptive(model, μ_seq) + π_seq, Eπ_seq, m_seq, p_seq = solve(model, μ_seq) T_seq = range(model.T+2) @@ -351,12 +337,10 @@ def solve_and_plot(model, μ_seq): ax[4].plot(T_seq, p_seq) y_labs = [r'$\mu$', r'$\pi$', r'$m - p$', r'$m$', r'$p$'] - subplot_title = [r'Money supply growth', r'Inflation', r'Real balances', r'Money supply', r'Price level'] for i in range(5): ax[i].set_xlabel(r'$t$') ax[i].set_ylabel(y_labs[i]) - ax[i].set_title(subplot_title[i]) ax[1].legend() plt.tight_layout() @@ -367,17 +351,15 @@ def solve_and_plot(model, μ_seq): +++ {"user_expressions": []} +## 稳定性的技术条件 - -## Technical condition for stability - -In constructing our examples, we shall assume that $(\lambda, \alpha)$ satisfy +在构建我们的示例时,我们假设 $(\lambda, \alpha)$ 满足 $$ \Bigl| \frac{\lambda-\alpha(1-\lambda)}{1-\alpha(1-\lambda)} \Bigr| < 1 $$ (eq:suffcond) -The source of this condition is the following string of deductions: +这个条件的来源是以下一系列推导: $$ \begin{aligned} @@ -385,72 +367,64 @@ $$ \end{aligned} $$ -By assuring that the coefficient on $\pi_t$ is less than one in absolute value, condition {eq}`eq:suffcond` assures stability of the dynamics of $\{\pi_t\}$ described by the last line of our string of deductions. +通过确保 $\pi_t$ 的系数绝对值小于1,条件{eq}`eq:suffcond`保证了由我们推导过程最后一行描述的 $\{\pi_t\}$ 动态的稳定性。 -The reader is free to study outcomes in examples that violate condition {eq}`eq:suffcond`. +读者可以自由研究违反条件{eq}`eq:suffcond`的示例结果。 ```{code-cell} ipython3 -print(np.abs((md.λ - md.α*(1-md.λ))/(1 - md.α*(1-md.λ)))) +print(np.abs((λ - α*(1-λ))/(1 - α*(1-λ)))) ``` -## Experiments - -Now we'll turn to some experiments. - -### Experiment 1 +```{code-cell} ipython3 +print(λ - α*(1-λ)) +``` -We'll study a situation in which the rate of growth of the money supply is $\mu_0$ -from $t=0$ to $t= T_1$ and then permanently falls to $\mu^*$ at $t=T_1$. +现在我们来看一些实验。 -Thus, let $T_1 \in (0, T)$. +### 实验1 +我们将研究一种情况,其中货币供应量的增长率从t=0到t=T_1时为$\mu_0$,然后在t=T_1时永久下降到$\mu^*$。 -So where $\mu_0 > \mu^*$, we assume that +因此,设$T_1 \in (0, T)$。 +所以当$\mu_0 > \mu^*$时,我们假设 $$ -\mu_{t} = \begin{cases} +\mu_{t+1} = \begin{cases} \mu_0 , & t = 0, \ldots, T_1 -1 \\ \mu^* , & t \geq T_1 \end{cases} $$ -Notice that we studied exactly this experiment in a rational expectations version of the model in {doc}`cagan_ree`. - -So by comparing outcomes across the two lectures, we can learn about consequences of assuming adaptive expectations, as we do here, instead of rational expectations as we assumed in that other lecture. +注意,我们在这个讲座{doc}`货币主义价格水平理论`中的理性预期版本模型中研究了完全相同的实验。 +因此,通过比较这两个讲座的结果,我们可以了解假设适应性预期(如我们在这里所做的)而不是理性预期(如我们在另一个讲座中所假设的)的后果。 ```{code-cell} ipython3 -# Parameters for the experiment 1 -T1 = 60 -μ0 = 0.5 -μ_star = 0 +μ_seq_1 = np.append(μ0*np.ones(T1), μ_star*np.ones(T+1-T1)) -μ_seq_1 = np.append(μ0*np.ones(T1), μ_star*np.ones(md.T+1-T1)) - -# solve and plot +# 求解并绘图 π_seq_1, Eπ_seq_1, m_seq_1, p_seq_1 = solve_and_plot(md, μ_seq_1) ``` -We invite the reader to compare outcomes with those under rational expectations studied in {doc}`cagan_ree`. +我们邀请读者将结果与在另一讲座 {doc}`货币主义价格水平理论 ` 中研究的理性预期下的结果进行比较。 -Please note how the actual inflation rate $\pi_t$ "overshoots" its ultimate steady-state value at the time of the sudden reduction in the rate of growth of the money supply at time $T_1$. +请注意实际通货膨胀率 $\pi_t$ 在时间 $T_1$ 货币供应增长率突然减少时如何"超调"其最终稳态值。 -We invite you to explain to yourself the source of this overshooting and why it does not occur in the rational expectations version of the model. +我们邀请您向自己解释这种超调的来源,以及为什么在模型的理性预期版本中不会出现这种情况。 -### Experiment 2 +### 实验2 -Now we'll do a different experiment, namely, a gradual stabilization in which the rate of growth of the money supply smoothly -decline from a high value to a persistently low value. +现在我们将进行一个不同的实验,即渐进式稳定化,其中货币供应增长率从高值平稳下降到持续的低值。 -While price level inflation eventually falls, it falls more slowly than the driving force that ultimately causes it to fall, namely, the falling rate of growth of the money supply. +虽然价格水平通货膨胀最终会下降,但它下降的速度比最终导致它下降的驱动力(即货币供应增长率的下降)要慢。 -The sluggish fall in inflation is explained by how anticipated inflation $\pi_t^*$ persistently exceeds actual inflation $\pi_t$ during the transition from a high inflation to a low inflation situation. +通货膨胀缓慢下降的原因可以解释为在从高通胀向低通胀过渡期间,预期通货膨胀率 $\pi_t^*$ 持续高于实际通货膨胀率 $\pi_t$。 ```{code-cell} ipython3 -# parameters +# 参数 ϕ = 0.9 -μ_seq_2 = np.array([ϕ**t * μ0 + (1-ϕ**t)*μ_star for t in range(md.T)]) +μ_seq_2 = np.array([ϕ**t * μ0 + (1-ϕ**t)*μ_star for t in range(T)]) μ_seq_2 = np.append(μ_seq_2, μ_star) -# solve and plot +# 求解并绘图 π_seq_2, Eπ_seq_2, m_seq_2, p_seq_2 = solve_and_plot(md, μ_seq_2) -``` +``` \ No newline at end of file diff --git a/lectures/cagan_ree.md b/lectures/cagan_ree.md index f0274b5..e7a5dc5 100644 --- a/lectures/cagan_ree.md +++ b/lectures/cagan_ree.md @@ -11,131 +11,120 @@ kernelspec: name: python3 --- -# A Monetarist Theory of Price Levels +# 价格水平的货币主义理论 -## Overview +## 概述 +我们将首先使用线性代数来解释,然后对"价格水平的货币主义理论"进行一些实验。 -We'll use linear algebra first to explain and then do some experiments with a "monetarist theory of price levels". +经济学家称之为价格水平的"货币"或"货币主义"理论,因为价格水平的影响是通过中央银行决定印制货币供应量而发生的。 -Economists call it a "monetary" or "monetarist" theory of price levels because effects on price levels occur via a central bank's decisions to print money supply. + * 政府的财政政策决定了其支出是否超过税收 + * 如果支出超过税收,政府可以指示中央银行通过印钞来弥补差额 + * 这导致价格水平发生变化,价格水平路径调整以使货币供给等于货币需求 - * a goverment's fiscal policies determine whether its _expenditures_ exceed its _tax collections_ - * if its expenditures exceed its tax collections, the government can instruct the central bank to cover the difference by _printing money_ - * that leads to effects on the price level as price level path adjusts to equate the supply of money to the demand for money +这种价格水平理论由托马斯·萨金特和尼尔·华莱士在{cite}`sargent2013rational`的第5章中描述,该章节重印了1981年明尼阿波利斯联邦储备银行题为"令人不快的货币主义算术"的文章。 -Such a theory of price levels was described by Thomas Sargent and Neil Wallace in chapter 5 of -{cite}`sargent2013rational`, which reprints a 1981 Federal Reserve Bank of Minneapolis article entitled "Unpleasant Monetarist Arithmetic". +有时这种理论也被称为"价格水平的财政理论",以强调财政赤字在塑造货币供应变化中的重要性。 -Sometimes this theory is also called a "fiscal theory of price levels" to emphasize the importance of fiscal deficits in shaping changes in the money supply. +约翰·科克伦 {cite}`cochrane2023fiscal`对该理论进行了扩展、批评和应用。 -The theory has been extended, criticized, and applied by John Cochrane {cite}`cochrane2023fiscal`. +在另一个讲座{doc}`价格水平历史 `中,我们描述了第一次世界大战后发生的一些欧洲恶性通货膨胀。 -In another lecture {doc}`price level histories `, we described some European hyperinflations that occurred in the wake of World War I. +价格水平财政理论中起作用的基本力量有助于理解这些事件。 -Elemental forces at work in the fiscal theory of the price level help to understand those episodes. +根据这个理论,当政府持续花费超过税收并印钞来为赤字融资("赤字"被称为"政府赤字")时,它会对价格水平产生上行压力并产生持续通货膨胀。 +"货币主义"或"价格水平的财政理论"断言: -According to this theory, when the government persistently spends more than it collects in taxes and prints money to finance the shortfall (the "shortfall" is called the "government deficit"), it puts upward pressure on the price level and generates -persistent inflation. +* 要开始持续通货膨胀,政府开始持续运行货币融资的政府赤字 -The "monetarist" or "fiscal theory of price levels" asserts that +* 要停止持续通货膨胀,政府停止持续运行货币融资的政府赤字 -* to _start_ a persistent inflation the government begins persistently to run a money-financed government deficit +本讲座中的模型是菲利普·凯根 {cite}`Cagan`用来研究恶性通货膨胀货币动态的模型的"理性预期"(或"完全预见")版本。 -* to _stop_ a persistent inflation the government stops persistently running a money-financed government deficit +虽然Cagan没有使用模型的"理性预期"版本,但托马斯·萨金特 {cite}`sargent1982ends`在研究第一次世界大战后欧洲四大通货膨胀结束时使用了这个版本。 -The model in this lecture is a "rational expectations" (or "perfect foresight") version of a model that Philip Cagan {cite}`Cagan` used to study the monetary dynamics of hyperinflations. +* 这个讲座{doc}`基于适应性预期的价格水平财政理论 `描述了一个不施加"理性预期"而是使用凯根和他的老师米尔顿·弗里德曼所称的"适应性预期"的模型版本 -While Cagan didn't use that "rational expectations" version of the model, Thomas Sargent {cite}`sargent1982ends` did when he studied the Ends of Four Big Inflations in Europe after World War I. + * 读者会注意到,目前的理性预期版本模型的代数比较简单 + * 代数复杂性的差异可以追溯到以下来源:适应性预期版本的模型有更多的内生变量和更多的自由参数 -* this lecture {doc}`fiscal theory of the price level with adaptive expectations ` describes a version of the model that does not impose "rational expectations" but instead uses - what Cagan and his teacher Milton Friedman called "adaptive expectations" +我们对理性预期版本模型的一些定量实验旨在说明财政理论如何解释那些大通货膨胀的突然结束。 - * a reader of both lectures will notice that the algebra is less complicated in the present rational expectations version of the model - * the difference in algebra complications can be traced to the following source: the adaptive expectations version of the model has more endogenous variables and more free parameters +在这些实验中,我们会遇到一个"速度红利"的例子,这有时伴随着成功的通货膨胀稳定计划。 -Some of our quantitative experiments with the rational expectations version of the model are designed to illustrate how the fiscal theory explains the abrupt end of those big inflations. +为了方便使用线性矩阵代数作为我们的主要数学工具,我们将使用该模型的有限时间范围版本。 -In those experiments, we'll encounter an instance of a "velocity dividend" that has sometimes accompanied successful inflation stabilization programs. +与{doc}`现值 `和{doc}`消费平滑`讲座一样,我们的数学工具是矩阵乘法和矩阵求逆。 -To facilitate using linear matrix algebra as our main mathematical tool, we'll use a finite horizon version of the model. +## 模型结构 -As in the {doc}`present values ` and {doc}`consumption smoothing` lectures, our mathematical tools are matrix multiplication and matrix inversion. +该模型包括 +* 一个函数,表示政府印制货币的实际余额需求是公众预期通货膨胀率的反函数 -## Structure of the model +* 外生的货币供应增长率序列。货币供应增长是因为政府印钞来支付商品和服务 +* 使货币需求等于供给的均衡条件 -The model consists of - -* a function that expresses the demand for real balances of government printed money as an inverse function of the public's expected rate of inflation - -* an exogenous sequence of rates of growth of the money supply. The money supply grows because the government prints it to pay for goods and services - -* an equilibrium condition that equates the demand for money to the supply - -* a "perfect foresight" assumption that the public's expected rate of inflation equals the actual rate of inflation. +* 一个"完全预见"假设,即公众预期的通货膨胀率等于实际通货膨胀率 -To represent the model formally, let +为了正式表示该模型,让 -* $ m_t $ be the log of the supply of nominal money balances; -* $\mu_t = m_{t+1} - m_t $ be the net rate of growth of nominal balances; -* $p_t $ be the log of the price level; -* $\pi_t = p_{t+1} - p_t $ be the net rate of inflation between $t$ and $ t+1$; -* $\pi_t^*$ be the public's expected rate of inflation between $t$ and $t+1$; -* $T$ the horizon -- i.e., the last period for which the model will determine $p_t$ -* $\pi_{T+1}^*$ the terminal rate of inflation between times $T$ and $T+1$. +* $ m_t $ 为名义货币余额供应的对数; +* $\mu_t = m_{t+1} - m_t $ 为名义余额的净增长率; +* $p_t $ 为价格水平的对数; +* $\pi_t = p_{t+1} - p_t $ 为 $t$ 和 $ t+1$ 之间的净通货膨胀率; +* $\pi_t^*$ 为公众预期的 $t$ 和 $t+1$ 之间的通货膨胀率; +* $T$ 为时间范围 -- 即模型将确定 $p_t$ 的最后一个时期 +* $\pi_{T+1}^*$ 为 $T$ 和 $T+1$ 之间的终端通货膨胀率。 -The demand for real balances $\exp\left(m_t^d - p_t\right)$ is governed by the following version of the Cagan demand function +实际余额 $\exp\left(m_t^d - p_t\right)$ 的需求由以下版本的Cagan需求函数决定 $$ m_t^d - p_t = -\alpha \pi_t^* \: , \: \alpha > 0 ; \quad t = 0, 1, \ldots, T . $$ (eq:caganmd) -This equation asserts that the demand for real balances -is inversely related to the public's expected rate of inflation with sensitivity $\alpha$. +这个方程断言,实际货币余额的需求与公众预期的通货膨胀率成反比。 -People somehow acquire **perfect foresight** by their having solved a forecasting -problem. +人们通过解决预测问题获得了**完全预见**。 -This lets us set +这让我们设置 $$ \pi_t^* = \pi_t , % \forall t $$ (eq:ree) -while equating demand for money to supply lets us set $m_t^d = m_t$ for all $t \geq 0$. +同时使货币需求等于供给让我们对所有 $t \geq 0$ 设置 $m_t^d = m_t$。 -The preceding equations then imply +前面的方程然后意味着 $$ m_t - p_t = -\alpha(p_{t+1} - p_t) $$ (eq:cagan) -To fill in details about what it means for private agents -to have perfect foresight, we subtract equation {eq}`eq:cagan` at time $ t $ from the same equation at $ t+1$ to get +为了填充私人代理拥有完全预见的细节,我们从时间 $ t $ 的方程 {eq}`eq:cagan` 中减去 $ t+1 $ 时的相同方程得到 $$ \mu_t - \pi_t = -\alpha \pi_{t+1} + \alpha \pi_t , $$ -which we rewrite as a forward-looking first-order linear difference -equation in $\pi_s$ with $\mu_s$ as a "forcing variable": +我们将其重写为关于 $\pi_s$ 的前瞻性一阶线性差分方程,其中 $\mu_s$ 作为"强制变量": $$ \pi_t = \frac{\alpha}{1+\alpha} \pi_{t+1} + \frac{1}{1+\alpha} \mu_t , \quad t= 0, 1, \ldots , T $$ -where $ 0< \frac{\alpha}{1+\alpha} <1 $. +其中 $ 0< \frac{\alpha}{1+\alpha} <1 $。 -Setting $\delta =\frac{\alpha}{1+\alpha}$, let's us represent the preceding equation as +设 $\delta =\frac{\alpha}{1+\alpha}$,让我们将前面的方程表示为 $$ \pi_t = \delta \pi_{t+1} + (1-\delta) \mu_t , \quad t =0, 1, \ldots, T $$ -Write this system of $T+1$ equations as the single matrix equation +将这个 $T+1$ 个方程的系统写成单个矩阵方程 $$ \begin{bmatrix} 1 & -\delta & 0 & 0 & \cdots & 0 & 0 \cr @@ -154,26 +143,26 @@ $$ \end{bmatrix} $$ (eq:pieq) -By multiplying both sides of equation {eq}`eq:pieq` by the inverse of the matrix on the left side, we can calculate +通过将方程 {eq}`eq:pieq` 两边乘以左侧矩阵的逆,我们可以计算 $$ \pi \equiv \begin{bmatrix} \pi_0 \cr \pi_1 \cr \pi_2 \cr \vdots \cr \pi_{T-1} \cr \pi_T \end{bmatrix} $$ -It turns out that +结果是 $$ \pi_t = (1-\delta) \sum_{s=t}^T \delta^{s-t} \mu_s + \delta^{T+1-t} \pi_{T+1}^* $$ (eq:fisctheory1) -We can represent the equations +我们可以将方程 $$ m_{t+1} = m_t + \mu_t , \quad t = 0, 1, \ldots, T $$ -as the matrix equation +表示为矩阵方程 $$ \begin{bmatrix} @@ -195,75 +184,71 @@ m_0 \cr 0 \cr 0 \cr \vdots \cr 0 \cr 0 \end{bmatrix} $$ (eq:eq101) -Multiplying both sides of equation {eq}`eq:eq101` with the inverse of the matrix on the left will give +将方程 {eq}`eq:eq101` 两边乘以左侧矩阵的逆将得到 $$ m_t = m_0 + \sum_{s=0}^{t-1} \mu_s, \quad t =1, \ldots, T+1 $$ (eq:mcum) -Equation {eq}`eq:mcum` shows that the log of the money supply at $t$ equals the log of the initial money supply $m_0$ -plus accumulation of rates of money growth between times $0$ and $T$. +方程 {eq}`eq:mcum` 显示,时间 $t$ 的货币供应对数等于初始货币供应对数 $m_0$ 加上从时间 $0$ 到 $T$ 之间的货币增长率累积。 -## Continuation values +## 延续值 -To determine the continuation inflation rate $\pi_{T+1}^*$ we shall proceed by applying the following infinite-horizon -version of equation {eq}`eq:fisctheory1` at time $t = T+1$: +为确定延续通胀率 $\pi_{T+1}^*$,我们将在 $t = T+1$ 时应用以下方程 {eq}`eq:fisctheory1` 的无限期版本: $$ \pi_t = (1-\delta) \sum_{s=t}^\infty \delta^{s-t} \mu_s , $$ (eq:fisctheory2) -and by also assuming the following continuation path for $\mu_t$ beyond $T$: +并假设 $T$ 之后 $\mu_t$ 的延续路径如下: $$ \mu_{t+1} = \gamma^* \mu_t, \quad t \geq T . $$ -Plugging the preceding equation into equation {eq}`eq:fisctheory2` at $t = T+1$ and rearranging we can deduce that +将上述方程代入 $t = T+1$ 时的方程 {eq}`eq:fisctheory2` 并重新排列,我们可以推导出: $$ \pi_{T+1}^* = \frac{1 - \delta}{1 - \delta \gamma^*} \gamma^* \mu_T $$ (eq:piterm) -where we require that $\vert \gamma^* \delta \vert < 1$. +其中我们要求 $\vert \gamma^* \delta \vert < 1$。 -Let's implement and solve this model. +让我们实现并解决这个模型。 - -As usual, we'll start by importing some Python modules. +像往常一样,我们将从导入一些 Python 模块开始。 ```{code-cell} ipython3 import numpy as np from collections import namedtuple import matplotlib.pyplot as plt +plt.rcParams['figure.dpi'] = 200 ``` - -First, we store parameters in a `namedtuple`: +首先,我们将参数存储在一个`namedtuple`中: ```{code-cell} ipython3 -# Create the rational expectation version of Cagan model in finite time +# "在有限时间内创建凯根模型的理性预期版本" CaganREE = namedtuple("CaganREE", - ["m0", # initial money supply - "μ_seq", # sequence of rate of growth - "α", # sensitivity parameter + ["m0", # 初始货币供给 + "μ_seq", # 增长率序列 + "α", # 敏感度参数 "δ", # α/(1 + α) - "π_end" # terminal expected inflation + "π_end" # 终止期预期通货膨胀率 ]) def create_cagan_model(m0=1, α=5, μ_seq=None): δ = α/(1 + α) - π_end = μ_seq[-1] # compute terminal expected inflation + π_end = μ_seq[-1] # 计算终止期预期通货膨胀率 return CaganREE(m0, μ_seq, α, δ, π_end) ``` - -Now we can solve the model to compute $\pi_t$, $m_t$ and $p_t$ for $t =1, \ldots, T+1$ using the matrix equation above +现在我们可以求解这个模型通过矩阵方程来计算$\pi_t$, $m_t$ 和 $p_t$ 当 $t =1, \ldots, T+1$ ```{code-cell} ipython3 def solve(model, T): m0, π_end, μ_seq, α, δ = (model.m0, model.π_end, model.μ_seq, model.α, model.δ) - # Create matrix representation above + # 创建矩阵表达 A1 = np.eye(T+1, T+1) - δ * np.eye(T+1, T+1, k=1) A2 = np.eye(T+1, T+1) - np.eye(T+1, T+1, k=-1) @@ -281,34 +266,22 @@ def solve(model, T): return π_seq, m_seq, p_seq ``` -### Some quantitative experiments +### 一些定量实验 -In the experiments below, we'll use formula {eq}`eq:piterm` as our terminal condition for expected inflation. - -In devising these experiments, we'll make assumptions about $\{\mu_t\}$ that are consistent with formula -{eq}`eq:piterm`. - -We describe several such experiments. - -In all of them, +在下面的实验中,我们将使用公式 {eq}`eq:piterm` 作为预期通货膨胀的终止条件。在设计这些实验时,我们将对 $\{\mu_t\}$ 做出与公式 {eq}`eq:piterm` 一致的假设。我们将描述几个这样的实验。 +在所有这些实验中, $$ \mu_t = \mu^* , \quad t \geq T_1 $$ +因此,根据我们上面的符号和 $\pi_{T+1}^*$ 的公式,$\tilde \gamma = 1$。 -so that, in terms of our notation and formula for $\pi_{T+1}^*$ above, $\gamma^* = 1$. - -#### Experiment 1: Foreseen sudden stabilization - -In this experiment, we'll study how, when $\alpha >0$, a foreseen inflation stabilization has effects on inflation that proceed it. - -We'll study a situation in which the rate of growth of the money supply is $\mu_0$ -from $t=0$ to $t= T_1$ and then permanently falls to $\mu^*$ at $t=T_1$. - -Thus, let $T_1 \in (0, T)$. +#### 实验1:可预见的突然稳定 -So where $\mu_0 > \mu^*$, we assume that +在这个实验中,我们将研究当 $\alpha >0$ 时,一个可预见的通货膨胀稳定如何对其之前的通货膨胀产生影响。我们将研究一种情况,即货币供应增长率从 $t=0$ 到 $t= T_1$ 为 $\mu_0$,然后在 $t=T_1$ 时永久降至 $\mu^*$。 +因此,令 $T_1 \in (0, T)$。 +所以当 $\mu_0 > \mu^*$ 时,我们假设 $$ \mu_{t+1} = \begin{cases} \mu_0 , & t = 0, \ldots, T_1 -1 \\ @@ -316,9 +289,9 @@ $$ \end{cases} $$ -We'll start by executing a version of our "experiment 1" in which the government implements a _foreseen_ sudden permanent reduction in the rate of money creation at time $T_1$. +我们将从执行"实验1"的一个版本开始,在这个版本中,政府在时间 $T_1$ 实施一个*可预见的*突然永久性货币创造率减少。 -Let's experiment with the following parameters +让我们用以下参数进行实验 ```{code-cell} ipython3 T1 = 60 @@ -330,15 +303,15 @@ T = 80 cm = create_cagan_model(μ_seq=μ_seq_1) -# solve the model +# 求解模型 π_seq_1, m_seq_1, p_seq_1 = solve(cm, T) ``` -Now we use the following function to plot the result +我们用下面的函数来进行绘图 ```{code-cell} ipython3 def plot_sequences(sequences, labels): - fig, axs = plt.subplots(len(sequences), 1, figsize=(5, 12), dpi=200) + fig, axs = plt.subplots(len(sequences), 1, figsize=(5, 12)) for ax, seq, label in zip(axs, sequences, labels): ax.plot(range(len(seq)), seq, label=label) ax.set_ylabel(label) @@ -351,83 +324,78 @@ sequences = (μ_seq_1, π_seq_1, m_seq_1 - p_seq_1, m_seq_1, p_seq_1) plot_sequences(sequences, (r'$\mu$', r'$\pi$', r'$m - p$', r'$m$', r'$p$')) ``` -The plot of the money growth rate $\mu_t$ in the top level panel portrays -a sudden reduction from $.5$ to $0$ at time $T_1 = 60$. +顶部面板中货币增长率 $\mu_t$ 的图表显示在时间 $T_1 = 60$ 时从 $.5$ 突然降至 $0$。 -This brings about a gradual reduction of the inflation rate $\pi_t$ that precedes the -money supply growth rate reduction at time $T_1$. +这导致通货膨胀率 $\pi_t$ 在时间 $T_1$ 之前逐渐降低。 -Notice how the inflation rate declines smoothly (i.e., continuously) to $0$ at $T_1$ -- -unlike the money growth rate, it does not suddenly "jump" downward at $T_1$. +注意通货膨胀率如何平滑(即连续)地降至 $T_1$ 时的 $0$ —— +与货币增长率不同,它在 $T_1$ 时并没有突然"跳跃"下降。 -This is because the reduction in $\mu$ at $T_1$ has been foreseen from the start. +这是因为 $T_1$ 时 $\mu$ 的减少从一开始就被预见到了。 -While the log money supply portrayed in the bottom panel has a kink at $T_1$, the log price level does not -- it is "smooth" -- once again a consequence of the fact that the -reduction in $\mu$ has been foreseen. +虽然底部面板中的对数货币供应在 $T_1$ 处有一个拐点,但对数价格水平没有 —— 它是"平滑的" —— 这再次是 $\mu$ 减少被预见到的结果。 -To set the stage for our next experiment, we want to study the determinants of the price level a little more. +为了为我们的下一个实验做准备,我们想更深入地研究价格水平的决定因素。 +### 对数价格水平 -### The log price level - -We can use equations {eq}`eq:caganmd` and {eq}`eq:ree` -to discover that the log of the price level satisfies +我们可以使用方程 {eq}`eq:caganmd` 和 {eq}`eq:ree` +来发现对数价格水平满足 $$ p_t = m_t + \alpha \pi_t $$ (eq:pformula2) -or, by using equation {eq}`eq:fisctheory1`, +或者,通过使用方程 {eq}`eq:fisctheory1`, $$ p_t = m_t + \alpha \left[ (1-\delta) \sum_{s=t}^T \delta^{s-t} \mu_s + \delta^{T+1-t} \pi_{T+1}^* \right] $$ (eq:pfiscaltheory2) -In our next experiment, we'll study a "surprise" permanent change in the money growth that beforehand -was completely unanticipated. +在我们的下一个实验中,我们将研究一个"意外"的永久性货币增长变化,这在之前是完全未预料到的。 -At time $T_1$ when the "surprise" money growth rate change occurs, to satisfy -equation {eq}`eq:pformula2`, the log of real balances jumps -_upward_ as $\pi_t$ jumps _downward_. +在时间 $T_1$ 当"意外"货币增长率变化发生时,为了满足 +方程 {eq}`eq:pformula2`,实际余额的对数 +随着 $\pi_t$ 向下跳跃而向上跳跃。 -But in order for $m_t - p_t$ to jump, which variable jumps, $m_{T_1}$ or $p_{T_1}$? +但为了让 $m_t - p_t$ 跳跃,哪个变量跳跃,$m_{T_1}$ 还是 $p_{T_1}$? -We'll study that interesting question next. +我们接下来将研究这个有趣的问题。 -### What jumps? +### 什么跳跃? -What jumps at $T_1$? +在 $T_1$ 时什么跳跃? -Is it $p_{T_1}$ or $m_{T_1}$? +是 $p_{T_1}$ 还是 $m_{T_1}$? -If we insist that the money supply $m_{T_1}$ is locked at its value $m_{T_1}^1$ inherited from the past, then formula {eq}`eq:pformula2` implies that the price level jumps downward at time $T_1$, to coincide with the downward jump in -$\pi_{T_1}$ +如果我们坚持认为货币供应 $m_{T_1}$ 锁定在从过去继承的 $m_{T_1}^1$ 值,那么公式 {eq}`eq:pformula2` 意味着价格水平在时间 $T_1$ 向下跳跃,以与 +$\pi_{T_1}$ 的向下跳跃一致 -An alternative assumption about the money supply level is that as part of the "inflation stabilization", -the government resets $m_{T_1}$ according to +关于货币供应水平的另一个假设是,作为"通货膨胀稳定"的一部分, +政府根据以下公式重置 $m_{T_1}$: $$ m_{T_1}^2 - m_{T_1}^1 = \alpha (\pi_{T_1}^1 - \pi_{T_1}^2), $$ (eq:eqnmoneyjump) -which describes how the government could reset the money supply at $T_1$ in response to the jump in expected inflation associated with monetary stabilization. +这描述了政府如何在 $T_1$ 时重置货币供应,以响应与货币稳定相关的预期通货膨胀的跳跃。 -Doing this would let the price level be continuous at $T_1$. +这样做将使价格水平在 $T_1$ 时保持连续。 -By letting money jump according to equation {eq}`eq:eqnmoneyjump` the monetary authority prevents the price level from _falling_ at the moment that the unanticipated stabilization arrives. +通过让货币按照方程 {eq}`eq:eqnmoneyjump` 跳跃,货币当局防止了价格水平在意外稳定到来时下降。 -In various research papers about stabilizations of high inflations, the jump in the money supply described by equation {eq}`eq:eqnmoneyjump` has been called -"the velocity dividend" that a government reaps from implementing a regime change that sustains a permanently lower inflation rate. +在关于高通货膨胀稳定的各种研究论文中,方程 {eq}`eq:eqnmoneyjump` 描述的货币供应跳跃被称为 +政府通过实施维持永久性较低通货膨胀率的制度变革而获得的"速度红利"。 -#### Technical details about whether $p$ or $m$ jumps at $T_1$ +#### 关于 $p$ 还是 $m$ 在 $T_1$ 时跳跃的技术细节 -We have noted that with a constant expected forward sequence $\mu_s = \bar \mu$ for $s\geq t$, $\pi_{t} =\bar{\mu}$. +我们注意到,对于 $s\geq t$ 的常数预期前向序列 $\mu_s = \bar \mu$,$\pi_{t} =\bar{\mu}$。 -A consequence is that at $T_1$, either $m$ or $p$ must "jump" at $T_1$. +一个结果是在 $T_1$ 时,$m$ 或 $p$ 必须在 $T_1$ "跳跃"。 -We'll study both cases. +我们将研究这两种情况。 -#### $m_{T_{1}}$ does not jump. +#### $m_{T_{1}}$ 不跳跃。 $$ \begin{aligned} @@ -435,64 +403,62 @@ m_{T_{1}}&=m_{T_{1}-1}+\mu_{0}\\\pi_{T_{1}}&=\mu^{*}\\p_{T_{1}}&=m_{T_{1}}+\alph \end{aligned} $$ -Simply glue the sequences $t\leq T_1$ and $t > T_1$. +简单地将序列 $t\leq T_1$ 和 $t > T_1$ 粘合在一起。 -#### $m_{T_{1}}$ jumps. +#### $m_{T_{1}}$ 跳跃。 -We reset $m_{T_{1}}$ so that $p_{T_{1}}=\left(m_{T_{1}-1}+\mu_{0}\right)+\alpha\mu_{0}$, with $\pi_{T_{1}}=\mu^{*}$. +我们重置 $m_{T_{1}}$ 使得 $p_{T_{1}}=\left(m_{T_{1}-1}+\mu_{0}\right)+\alpha\mu_{0}$,其中 $\pi_{T_{1}}=\mu^{*}$。 -Then, +然后, $$ m_{T_{1}}=p_{T_{1}}-\alpha\pi_{T_{1}}=\left(m_{T_{1}-1}+\mu_{0}\right)+\alpha\left(\mu_{0}-\mu^{*}\right) $$ -We then compute for the remaining $T-T_{1}$ periods with $\mu_{s}=\mu^{*},\forall s\geq T_{1}$ and the initial condition $m_{T_{1}}$ from above. +我们然后计算剩余的 $T-T_{1}$ 期,其中 $\mu_{s}=\mu^{*},\forall s\geq T_{1}$ 和上面的初始条件 $m_{T_{1}}$。 -We are now technically equipped to discuss our next experiment. +我们现在在技术上准备好讨论我们的下一个实验。 -#### Experiment 2: an unforeseen sudden stabilization +#### 实验2:不可预见的突然稳定 -This experiment deviates a little bit from a pure version of our "perfect foresight" -assumption by assuming that a sudden permanent reduction in $\mu_t$ like that -analyzed in experiment 1 is completely unanticipated. +这个实验稍微偏离了我们的"完美预见" +假设的纯粹版本,假设像实验1中分析的那样突然永久性减少 $\mu_t$ +是完全未预料到的。 -Such a completely unanticipated shock is popularly known as an "MIT shock". +这种完全未预料到的冲击通常被称为"MIT冲击"。 -The mental experiment involves switching at time $T_1$ from an initial "continuation path" for $\{\mu_t, \pi_t\} $ to another path that involves a permanently lower inflation rate. +这个思想实验涉及在时间 $T_1$ 从 $\{\mu_t, \pi_t\}$ 的初始"继续路径"切换到另一个涉及永久性较低通货膨胀率的路径。 -**Initial Path:** $\mu_t = \mu_0$ for all $t \geq 0$. So this path is for $\{\mu_t\}_{t=0}^\infty$; the associated -path for $\pi_t$ has $\pi_t = \mu_0$. +**初始路径:** $\mu_t = \mu_0$ 对于所有 $t \geq 0$。所以这个路径是 $\{\mu_t\}_{t=0}^\infty$;相关的 +$\pi_t$ 路径有 $\pi_t = \mu_0$。 -**Revised Continuation Path** Where $ \mu_0 > \mu^*$, we construct a continuation path $\{\mu_s\}_{s=T_1}^\infty$ -by setting $\mu_s = \mu^*$ for all $s \geq T_1$. The perfect foresight continuation path for -$\pi$ is $\pi_s = \mu^*$ +**修订后的继续路径** 其中 $ \mu_0 > \mu^*$,我们构建一个继续路径 $\{\mu_s\}_{s=T_1}^\infty$ +通过设置 $\mu_s = \mu^*$ 对于所有 $s \geq T_1$。$\pi$ 的完美预见继续路径是 $\pi_s = \mu^*$ -To capture a "completely unanticipated permanent shock to the $\{\mu_t\}$ process at time $T_1$, we simply glue the $\mu_t, \pi_t$ -that emerges under path 2 for $t \geq T_1$ to the $\mu_t, \pi_t$ path that had emerged under path 1 for $ t=0, \ldots, -T_1 -1$. +为了捕捉在时间 $T_1$ 对 $\{\mu_t\}$ 过程的"完全未预料到的永久性冲击",我们只需将路径2下在 $t \geq T_1$ 时出现的 $\mu_t, \pi_t$ +粘合到路径1下在 $ t=0, \ldots, T_1 -1$ 时出现的 $\mu_t, \pi_t$ 路径上。 -We can do the MIT shock calculations mostly by hand. +我们可以主要通过手工进行MIT冲击计算。 -Thus, for path 1, $\pi_t = \mu_0 $ for all $t \in [0, T_1-1]$, while for path 2, -$\mu_s = \mu^*$ for all $s \geq T_1$. +因此,对于路径1,$\pi_t = \mu_0 $ 对于所有 $t \in [0, T_1-1]$,而对于路径2, +$\mu_s = \mu^*$ 对于所有 $s \geq T_1$。 -We now move on to experiment 2, our "MIT shock", completely unforeseen -sudden stabilization. +我们现在进入实验2,我们的"MIT冲击",完全不可预见的 +突然稳定。 -We set this up so that the $\{\mu_t\}$ sequences that describe the sudden stabilization -are identical to those for experiment 1, the foreseen sudden stabilization. +我们设置这个使得描述突然稳定的 $\{\mu_t\}$ 序列 +与实验1(可预见的突然稳定)的序列相同。 -The following code does the calculations and plots outcomes. +以下代码进行计算并绘制结果。 ```{code-cell} ipython3 -# path 1 +# 路径 1 μ_seq_2_path1 = μ0 * np.ones(T+1) cm1 = create_cagan_model(μ_seq=μ_seq_2_path1) π_seq_2_path1, m_seq_2_path1, p_seq_2_path1 = solve(cm1, T) -# continuation path +# 继续路径 μ_seq_2_cont = μ_star * np.ones(T-T1) cm2 = create_cagan_model(m0=m_seq_2_path1[T1+1], @@ -500,7 +466,7 @@ cm2 = create_cagan_model(m0=m_seq_2_path1[T1+1], π_seq_2_cont, m_seq_2_cont1, p_seq_2_cont1 = solve(cm2, T-1-T1) -# regime 1 - simply glue π_seq, μ_seq +# 方案1 - 简单粘合 π_seq, μ_seq μ_seq_2 = np.concatenate((μ_seq_2_path1[:T1+1], μ_seq_2_cont)) π_seq_2 = np.concatenate((π_seq_2_path1[:T1+1], @@ -510,7 +476,7 @@ m_seq_2_regime1 = np.concatenate((m_seq_2_path1[:T1+1], p_seq_2_regime1 = np.concatenate((p_seq_2_path1[:T1+1], p_seq_2_cont1)) -# regime 2 - reset m_T1 +# 方案 2 - 重制 m_T1 m_T1 = (m_seq_2_path1[T1] + μ0) + cm2.α*(μ0 - μ_star) cm3 = create_cagan_model(m0=m_T1, μ_seq=μ_seq_2_cont) @@ -527,10 +493,10 @@ p_seq_2_regime2 = np.concatenate((p_seq_2_path1[:T1+1], T_seq = range(T+2) -# plot both regimes -fig, ax = plt.subplots(5, 1, figsize=(5, 12), dpi=200) +# 绘制两个方案 +fig, ax = plt.subplots(5, 1, figsize=(5, 12)) -# Configuration for each subplot +# 每个子图的配置 plot_configs = [ {'data': [(T_seq[:-1], μ_seq_2)], 'ylabel': r'$\mu$'}, {'data': [(T_seq, π_seq_2)], 'ylabel': r'$\pi$'}, @@ -545,13 +511,13 @@ plot_configs = [ ] def experiment_plot(plot_configs, ax): - # Loop through each subplot configuration + #遍历每个子图配置 for axi, config in zip(ax, plot_configs): for data in config['data']: - if len(data) == 3: # Plot with label for legend + if len(data) == 3: # 绘制图表并添加图例标签 axi.plot(data[0], data[1], label=data[2]) axi.legend() - else: # Plot without label + else: # 绘制无标签图表 axi.plot(data[0], data[1]) axi.set_ylabel(config['ylabel']) axi.set_xlabel(r'$t$') @@ -560,33 +526,22 @@ def experiment_plot(plot_configs, ax): experiment_plot(plot_configs, ax) ``` +我们邀请您将这些图表与上面实验1中分析的预期稳定化的相应图表进行比较。 +请注意,第二个面板中的通货膨胀图现在与顶部面板中的货币增长图完全相同,以及现在在第三个面板中描绘的实际余额的对数在时间$T_1$时向上跳跃。 +底部两个面板绘制了在$m_{T_1}$可能调整的两种方式下的$m$和$p$,以满足在$T_1$时$m - p$的向上跳跃的要求。 +* 橙色线让$m_{T_1}$向上跳跃,以确保对数价格水平$p_{T_1}$不会下降。 +* 蓝色线让$p_{T_1}$下降,同时阻止货币供应量跳跃。 -We invite you to compare these graphs with corresponding ones for the foreseen stabilization analyzed in experiment 1 above. - -Note how the inflation graph in the second panel is now identical to the -money growth graph in the top panel, and how now the log of real balances portrayed in the third panel jumps upward at time $T_1$. - -The bottom two panels plot $m$ and $p$ under two possible ways that $m_{T_1}$ might adjust -as required by the upward jump in $m - p$ at $T_1$. - -* the orange line lets $m_{T_1}$ jump upward in order to make sure that the log price level $p_{T_1}$ does not fall. - -* the blue line lets $p_{T_1}$ fall while stopping the money supply from jumping. - -Here is a way to interpret what the government is doing when the orange line policy is in place. - -The government prints money to finance expenditure with the "velocity dividend" that it reaps from the increased demand for real balances brought about by the permanent decrease in the rate of growth of the money supply. - -The next code generates a multi-panel graph that includes outcomes of both experiments 1 and 2. - -That allows us to assess how important it is to understand whether the sudden permanent drop in $\mu_t$ at $t=T_1$ is fully unanticipated, as in experiment 1, or completely -unanticipated, as in experiment 2. +以下是一种解释政府在橙色线政策实施时所做的事情的方法。 +政府通过印钞来资助支出,利用从货币供应增长率永久性下降带来的实际余额需求增加中获得的"速度红利"。 +接下来的代码生成一个多面板图,包括实验1和实验2的结果。 +这使我们能够评估理解$\mu_t$在$t=T_1$时的突然永久性下降是完全预期的(如实验1中)还是完全未预期的(如实验2中)的重要性。 ```{code-cell} ipython3 :tags: [hide-input] -# compare foreseen vs unforeseen shock -fig, ax = plt.subplots(5, figsize=(5, 12), dpi=200) +# 比较可预见冲击与不可预见冲击 +fig, ax = plt.subplots(5, figsize=(5, 12)) plot_configs = [ {'data': [(T_seq[:-1], μ_seq_2)], 'ylabel': r'$\mu$'}, @@ -604,37 +559,30 @@ plot_configs = [ experiment_plot(plot_configs, ax) ``` +将前面的图表与{doc}`这篇讲座 `中描述的四次大通货膨胀数据的对数价格水平和通货膨胀率图表进行比较是很有启发性的。 -It is instructive to compare the preceding graphs with graphs of log price levels and inflation rates for data from four big inflations described in -{doc}`this lecture `. - -In particular, in the above graphs, notice how a gradual fall in inflation precedes the "sudden stop" when it has been anticipated long beforehand, but how -inflation instead falls abruptly when the permanent drop in money supply growth is unanticipated. - -It seems to the author team at quantecon that the drops in inflation near the ends of the four hyperinflations described in {doc}`this lecture ` -more closely resemble outcomes from the experiment 2 "unforeseen stabilization". +特别是在上述图表中,注意当通货膨胀早已被预见时,通货膨胀率的逐渐下降是如何先于"突然停止"的;但当货币供应增长的永久性下降是未预料到的时,通货膨胀率反而会突然下降。 -(It is fair to say that the preceding informal pattern recognition exercise should be supplemented with a more formal structural statistical analysis.) +quantecon的作者团队认为,{doc}`这篇讲座 `中描述的四次恶性通货膨胀接近尾声时的通货膨胀率下降,更接近实验2"不可预见的稳定"的结果。 -#### Experiment 3 +(公平地说,前面的非正式模式识别练习应该辅以更正式的结构统计分析。) -**Foreseen gradual stabilization** +#### 实验3 +**可预见的渐进稳定** -Instead of a foreseen sudden stabilization of the type studied with experiment 1, -it is also interesting to study the consequences of a foreseen gradual stabilization. - -Thus, suppose that $\phi \in (0,1)$, that $\mu_0 > \mu^*$, and that for $t = 0, \ldots, T-1$ +除了研究实验1中那种可预见的突然稳定外,研究可预见的渐进稳定的后果也很有意思。 +因此,假设$\phi \in (0,1)$,$\mu_0 > \mu^*$,且对于$t = 0, \ldots, T-1$,有 $$ \mu_t = \phi^t \mu_0 + (1 - \phi^t) \mu^* . $$ -Next we perform an experiment in which there is a perfectly foreseen _gradual_ decrease in the rate of growth of the money supply. +接下来,我们进行一个实验,其中货币供应增长率会完全可预见地逐渐下降。 -The following code does the calculations and plots the results. +以下代码进行计算并绘制结果。 ```{code-cell} ipython3 -# parameters +# 参数 ϕ = 0.9 μ_seq_stab = np.array([ϕ**t * μ0 + (1-ϕ**t)*μ_star for t in range(T)]) μ_seq_stab = np.append(μ_seq_stab, μ_star) @@ -649,10 +597,8 @@ plot_sequences(sequences, (r'$\mu$', r'$\pi$', r'$m - p$', r'$m$', r'$p$')) ``` -## Sequel - -Another lecture {doc}`monetarist theory of price levels with adaptive expectations ` describes an "adaptive expectations" version of Cagan's model. - -The dynamics become more complicated and so does the algebra. +## 续篇 -Nowadays, the "rational expectations" version of the model is more popular among central bankers and economists advising them. +另一篇讲座 {doc}`带有适应性预期的价格水平货币主义理论 ` 描述了凯根模型的"适应性预期"版本。 +这个版本的动态变得更加复杂,代数运算也更加繁琐。 +如今,在中央银行家和为他们提供建议的经济学家中,该模型的"理性预期"版本更受欢迎。 diff --git a/lectures/cobweb.md b/lectures/cobweb.md index 31d7181..dad2e6c 100644 --- a/lectures/cobweb.md +++ b/lectures/cobweb.md @@ -12,65 +12,43 @@ kernelspec: --- (cobweb)= -# The Cobweb Model +# 蛛网模型 -The cobweb model is a model of prices and quantities in a given market, and how they evolve over time. +蛛网模型是一个描述特定市场中价格和数量及其随时间演变的模型。 -## Overview +## 概述 -The cobweb model dates back to the 1930s and, while simple, it remains significant -because it shows the fundamental importance of *expectations*. +蛛网模型可以追溯到20世纪30年代,虽然简单,但它仍然具有重要意义,因为它展示了*预期*的根本重要性。 -To give some idea of how the model operates, and why expectations matter, imagine the following scenario. +为了说明模型如何运作以及为什么预期很重要,想象以下场景: -There is a market for soybeans, say, where prices and traded quantities -depend on the choices of buyers and sellers. +有一个大豆市场,价格和交易数量取决于买家和卖家的选择。买家由需求曲线表示——在低价时购买更多,高价时购买更少。卖家有一条供应曲线——他们希望在高价时卖出更多,低价时卖出更少。 -The buyers are represented by a demand curve --- they buy more at low prices -and less at high prices. +然而,卖家(农民)需要时间来种植作物。假设现在价格很高。看到这个高价,可能预期高价会持续一段时间,农民在许多田地里种植大豆。 -The sellers have a supply curve --- they wish to sell more at high prices and -less at low prices. +下一个周期,由此产生的高供应量充斥市场,导致价格下跌。看到这个低价,农民现在减少大豆种植,限制供应,导致价格再次上涨。 -However, the sellers (who are farmers) need time to grow their crops. +你可以想象这种动态如何导致价格和数量的周期性波动持续一段时间。 -Suppose now that the price is currently high. +蛛网模型将这些想法转化为方程,这样我们就可以尝试量化它们,并研究周期持续(或消失)的条件。 -Seeing this high price, and perhaps expecting that the high price will remain -for some time, the farmers plant many fields with soybeans. +在本讲座中,我们将在不同的生产者形成预期的假设下调查和模拟基本模型。 -Next period the resulting high supply floods the market, causing the price to drop. +我们的讨论和模拟借鉴了[Cars Hommes](https://www.uva.nl/en/profile/h/o/c.h.hommes/c.h.hommes.html)的[高质量讲座](https://comp-econ.org/CEF_2013/downloads/Complex%20Econ%20Systems%20Lecture%20II.pdf)。 -Seeing this low price, the farmers now shift out of soybeans, restricting -supply and causing the price to climb again. - -You can imagine how these dynamics could cause cycles in prices and quantities -that persist over time. - -The cobweb model puts these ideas into equations so we can try to quantify -them, and to study conditions under which cycles persist (or disappear). - -In this lecture, we investigate and simulate the basic model under different -assumptions regarding the way that producers form expectations. - -Our discussion and simulations draw on [high quality lectures](https://comp-econ.org/CEF_2013/downloads/Complex%20Econ%20Systems%20Lecture%20II.pdf) by [Cars Hommes](https://www.uva.nl/en/profile/h/o/c.h.hommes/c.h.hommes.html). - -We will use the following imports. +我们将使用以下导入。 ```{code-cell} ipython3 import numpy as np import matplotlib.pyplot as plt ``` -## History +## 历史 -Early papers on the cobweb cycle include {cite}`cobweb_model` and {cite}`hog_cycle`. - -The paper {cite}`hog_cycle` uses the cobweb theorem to explain the prices of hog in the US over 1920--1950. - -The next plot replicates part of Figure 2 from that paper, which plots the price of hogs at yearly frequency. - -Notice the cyclical price dynamics, which match the kind of cyclical soybean price dynamics discussed above. +关于蛛网循环的早期论文包括{cite}`cobweb_model`和{cite}`hog_cycle`。 +论文{cite}`hog_cycle`使用蛛网定理解释了1920年至1950年间美国猪肉价格的变化。 +下一个图表复制了该论文中图2的一部分,展示了年度猪肉价格。 +注意价格动态的周期性变化,这与上文讨论的大豆价格动态的周期性变化相匹配。 ```{code-cell} ipython3 hog_prices = [55, 57, 80, 70, 60, 65, 72, 65, 51, 49, 45, 80, 85, @@ -78,51 +56,50 @@ hog_prices = [55, 57, 80, 70, 60, 65, 72, 65, 51, 49, 45, 80, 85, 69, 65, 62, 85, 87, 65, 63, 75, 80, 62] years = np.arange(1924, 1960) fig, ax = plt.subplots() -ax.plot(years, hog_prices, '-o', ms=4, label='hog price') -ax.set_xlabel('year') -ax.set_ylabel('dollars') +ax.plot(years, hog_prices, '-o', ms=4, label='猪肉价格') +ax.set_xlabel('年') +ax.set_ylabel('美元') ax.legend() ax.grid() plt.show() ``` -## The model +## 模型 -Let's return to our discussion of a hypothetical soybean market, where price is determined by supply and demand. +让我们回到讨论假设的大豆市场,其中价格由供需决定。 -We suppose that demand for soybeans is given by +我们假设大豆的需求由以下公式给出: $$ D(p_t) = a - b p_t $$ -where $a, b$ are nonnegative constants and $p_t$ is the spot (i.e, current market) price at time $t$. +其中 $a, b$ 是非负常数,$p_t$ 是 $t$ 时刻的现货(即当前市场)价格。 +($D(p_t)$ 是以某个固定单位表示的需求量,如千吨。) -($D(p_t)$ is the quantity demanded in some fixed unit, such as thousands of tons.) +由于 $t$ 时刻的大豆作物是在 $t-1$ 时种植的,$t$ 时刻的大豆供应取决于 $t$ 时刻的*预期*价格,我们用 $p^e_t$ 表示。 -Because the crop of soybeans for time $t$ is planted at $t-1$, supply of soybeans at time $t$ depends on *expected* prices at time $t$, which we denote $p^e_t$. - -We suppose that supply is nonlinear in expected prices, and takes the form +我们假设供应对预期价格是非线性的,并采用以下形式: $$ S(p^e_t) = \tanh(\lambda(p^e_t - c)) + d $$ -where $\lambda$ is a positive constant, $c, d$ are nonnegative constants and $\tanh$ is a type of [hyperbolic function](https://en.wikipedia.org/wiki/Hyperbolic_functions). +其中 $\lambda$ 是正常数,$c, d$ 是非负常数,$\tanh$ 是一种[双曲函数](https://en.wikipedia.org/wiki/Hyperbolic_functions)。 -Let's make a plot of supply and demand for particular choices of the parameter values. +让我们为特定参数值选择绘制供需图。 -First we store the parameters in a class and define the functions above as methods. +首先,我们将参数存储在一个类中,并将上述函数定义为方法。 ```{code-cell} ipython3 class Market: def __init__(self, - a=8, # demand parameter - b=1, # demand parameter - c=6, # supply parameter - d=1, # supply parameter - λ=2.0): # supply parameter + a=8, # 需求参数 + b=1, # 需求参数 + c=6, # 供给参数 + d=1, # 供给参数 + λ=2.0): # 供给参数 self.a, self.b, self.c, self.d = a, b, c, d self.λ = λ @@ -135,7 +112,7 @@ class Market: return np.tanh(λ * (p - c)) + d ``` -Now let's plot. +现在让我们来绘制。 ```{code-cell} ipython3 p_grid = np.linspace(5, 8, 200) @@ -144,118 +121,100 @@ fig, ax = plt.subplots() ax.plot(p_grid, m.demand(p_grid), label="$D$") ax.plot(p_grid, m.supply(p_grid), label="$S$") -ax.set_xlabel("price") -ax.set_ylabel("quantity") +ax.set_xlabel("价格") +ax.set_ylabel("数量") ax.legend() plt.show() ``` -Market equilibrium requires that supply equals demand, or - +市场均衡要求供给等于需求,即 $$ a - b p_t = S(p^e_t) $$ - -Rewriting in terms of $p_t$ gives - +用$p_t$重写可得 $$ p_t = - \frac{1}{b} [S(p^e_t) - a] $$ - -Finally, to complete the model, we need to describe how price expectations are formed. - -We will assume that expected prices at time $t$ depend on past prices. - -In particular, we suppose that +最后,为了完成这个模型,我们需要描述价格预期是如何形成的。 +我们假设t时刻的预期价格取决于过去的价格。 +特别地,我们假设 ```{math} :label: p_et p^e_t = f(p_{t-1}, p_{t-2}) ``` -where $f$ is some function. - -Thus, we are assuming that producers expect the time-$t$ price to be some function of lagged prices, up to $2$ lags. - -(We could of course add additional lags and readers are encouraged to experiment with such cases.) - -Combining the last two equations gives the dynamics for prices: +其中$f$是某个函数。 +因此,我们假设生产者预期t时刻的价格是滞后价格的某个函数,最多滞后2期。 +(当然,我们可以添加更多的滞后期,鼓励读者尝试这样的情况。) +结合最后两个等式,我们得到价格的动态方程: ```{math} :label: price_t p_t = - \frac{1}{b} [ S(f(p_{t-1}, p_{t-2})) - a] ``` +价格动态取决于参数值以及决定生产者如何形成预期的函数 $f$。 -The price dynamics depend on the parameter values and also on the function $f$ that determines how producers form expectations. - -## Naive expectations +## 简单预期 -To go further in our analysis we need to specify the function $f$; that is, how expectations are formed. +为了进一步分析,我们需要指定函数 $f$;也就是说,预期是如何形成的。 -Let's start with naive expectations, which refers to the case where producers expect the next period spot price to be whatever the price is in the current period. +让我们从简单预期开始,它指的是生产者预期下一期的现货价格将是当前期的价格。 -In other words, +换句话说, $$ p_t^e = p_{t-1} $$ -Using {eq}`price_t`, we then have +使用方程 {eq}`price_t`,我们得到 $$ p_t = - \frac{1}{b} [ S(p_{t-1}) - a] $$ -We can write this as +我们可以将其写为 $$ p_t = g(p_{t-1}) $$ -where $g$ is the function defined by +其中 $g$ 是由以下定义的函数 ```{math} :label: def_g g(p) = - \frac{1}{b} [ S(p) - a] ``` -Here we represent the function $g$ +这里我们来表示函数$g$。 ```{code-cell} ipython3 def g(model, current_price): """ - Function to find the next price given the current price - and Market model + 给定当前价格和市场模型,用于找到下一个价格的函数 """ a, b = model.a, model.b next_price = - (model.supply(current_price) - a) / b return next_price ``` -Let's try to understand how prices will evolve using a 45-degree diagram, which is a tool for studying one-dimensional dynamics. - -The function `plot45` defined below helps us draw the 45-degree diagram. +让我们尝试使用 45 度图来理解价格将如何演变,这是研究一维动态的工具。 +下面定义的函数 `plot45` 帮助我们绘制 45 度图。 ```{code-cell} ipython3 :tags: [hide-input] def plot45(model, pmin, pmax, p0, num_arrows=5): """ - Function to plot a 45 degree plot - - Parameters + 绘制 45 度图的函数 + 参数 ========== - - model: Market model - - pmin: Lower price limit - - pmax: Upper price limit - - p0: Initial value of price (needed to simulate prices) - - num_arrows: Number of simulations to plot + model: 市场模型 + pmin: 价格下限 + pmax: 价格上限 + p0: 价格初始值(用于模拟价格) + num_arrows: 要绘制的模拟次数 """ pgrid = np.linspace(pmin, pmax, 200) @@ -312,7 +271,7 @@ def plot45(model, pmin, pmax, p0, num_arrows=5): plt.show() ``` -Now we can set up a market and plot the 45-degree diagram. +现在我们可以设置一个市场并绘制 45 度图。 ```{code-cell} ipython3 m = Market() @@ -322,35 +281,34 @@ m = Market() plot45(m, 0, 9, 2, num_arrows=3) ``` -The plot shows the function $g$ defined in {eq}`def_g` and the 45-degree line. +图表显示了方程 {eq}`def_g` 中定义的函数 $g$ 和 45 度线。 -Think of $ p_t $ as a value on the horizontal axis. +将 $p_t$ 视为横轴上的值。 -Since $p_{t+1} = g(p_t)$, we use the graph of $g$ to see $p_{t+1}$ on the vertical axis. +由于 $p_{t+1} = g(p_t)$,我们使用 $g$ 的图形在纵轴上看到 $p_{t+1}$。 -Clearly, +显然, +- 如果 $g$ 在 $p_t$ 处位于 45 度线上方,那么我们有 $p_{t+1} > p_t$。 +- 如果 $g$ 在 $p_t$ 处位于 45 度线下方,那么我们有 $p_{t+1} < p_t$。 +- 如果 $g$ 在 $p_t$ 处与 45 度线相交,那么我们有 $p_{t+1} = p_t$,所以 $p_t$ 是一个稳态。 -- If $ g $ lies above the 45-degree line at $p_t$, then we have $ p_{t+1} > p_t $. -- If $ g $ lies below the 45-degree line at $p_t$, then we have $ p_{t+1} < p_t $. -- If $ g $ hits the 45-degree line at $p_t$, then we have $ p_{t+1} = p_t $, so $ p_t $ is a steady state. +考虑从 $p_0$ 开始的价格序列,如图所示。 -Consider the sequence of prices starting at $p_0$, as shown in the figure. +我们在纵轴上找到 $p_1$,然后使用 45 度线(两个轴上的值相等)将其移到横轴上。 -We find $p_1$ on the vertical axis and then shift it to the horizontal axis using the 45-degree line (where values on the two axes are equal). +然后从 $p_1$ 得到 $p_2$ 并继续。 -Then from $p_1$ we obtain $p_2$ and continue. +我们可以看到一个循环的开始。 -We can see the start of a cycle. - -To confirm this, let's plot a time series. +为了确认这一点,让我们绘制一个时间序列图。 ```{code-cell} ipython3 -def ts_plot_price(model, # Market model - p0, # Initial price - y_a=3, y_b= 12, # Controls y-axis - ts_length=10): # Length of time series +def ts_plot_price(model, # 市场模型 + p0, # 初始价格 + y_a=3, y_b= 12, # 控制 y-轴 + ts_length=10): # 时间序列的长度 """ - Function to simulate and plot the time series of price. + 用来模拟和绘制价格时间序列的函数 """ fig, ax = plt.subplots() @@ -376,31 +334,23 @@ def ts_plot_price(model, # Market model ts_plot_price(m, 4, ts_length=15) ``` -We see that a cycle has formed and the cycle is persistent. - -(You can confirm this by plotting over a longer time horizon.) +我们看到一个循环已经形成,而且这个循环是持续的。 +(您可以通过绘制更长时间范围的图表来确认这一点。) +这个循环是"稳定的",意味着从大多数起始条件开始,价格都会收敛到这个循环。 +例如, -The cycle is "stable", in the sense that prices converge to it from most starting conditions. - -For example, ```{code-cell} ipython3 ts_plot_price(m, 10, ts_length=15) ``` -## Adaptive expectations - -Naive expectations are quite simple and also important in driving the cycle that we found. - -What if expectations are formed in a different way? - -Next we consider adaptive expectations. - -This refers to the case where producers form expectations for -the next period price as a weighted average of their last guess and the -current spot price. +## 适应性预期 -That is, +朴素预期相当简单,也在我们发现的周期中起着重要的驱动作用。 +如果预期以不同的方式形成会怎样? +接下来我们考虑适应性预期。 +这指的是生产者对下一期价格的预期形成是基于他们上一次猜测和当前现货价格的加权平均。 +也就是说, ```{math} :label: pe_adaptive @@ -408,36 +358,34 @@ p_t^e = \alpha p_{t-1} + (1-\alpha) p^e_{t-1} \qquad (0 \leq \alpha \leq 1) ``` -Another way to write this is +另一种写法为 ```{math} :label: pe_adaptive_2 p_t^e = p^e_{t-1} + \alpha (p_{t-1} - p_{t-1}^e) ``` -This equation helps to show that expectations shift +这个方程有助于说明预期会在以下情况下发生变化: +1. 当上一期价格高于预期时,预期上调 +1. 当上一期价格低于预期时,预期下调 -1. up when prices last period were above expectations -1. down when prices last period were below expectations - -Using {eq}`pe_adaptive`, we obtain the dynamics +使用{eq}`pe_adaptive`,我们得到以下动态方程: $$ p_t = - \frac{1}{b} [ S(\alpha p_{t-1} + (1-\alpha) p^e_{t-1}) - a] $$ -Let's try to simulate the price and observe the dynamics using different values of $\alpha$. +让我们尝试模拟价格,并观察在不同$\alpha$值下的动态变化。 ```{code-cell} ipython3 def find_next_price_adaptive(model, curr_price_exp): """ - Function to find the next price given the current price expectation - and Market model + 给定当前价格预期和市场模型,计算下一个价格的函数 """ return - (model.supply(curr_price_exp) - model.a) / model.b ``` -The function below plots price dynamics under adaptive expectations for different values of $\alpha$. +下面的函数将在不同的$\alpha$值下绘制适应性预期下的价格动态。 ```{code-cell} ipython3 def ts_price_plot_adaptive(model, p0, ts_length=10, α=[1.0, 0.9, 0.75]): @@ -453,31 +401,27 @@ def ts_price_plot_adaptive(model, p0, ts_length=10, α=[1.0, 0.9, 0.75]): axs[i_plot].plot(np.arange(ts_length), p_values) axs[i_plot].set_title(r'$\alpha={}$'.format(a)) axs[i_plot].set_xlabel('t') - axs[i_plot].set_ylabel('price') + axs[i_plot].set_ylabel('价格') plt.show() ``` -Let's call the function with prices starting at $p_0 = 5$. +让我们调用这个函数,初始价格设为$p_0 = 5$。 ```{code-cell} ipython3 ts_price_plot_adaptive(m, 5, ts_length=30) ``` -Note that if $\alpha=1$, then adaptive expectations are just naive expectation. - -Decreasing the value of $\alpha$ shifts more weight to the previous -expectations, which stabilizes expected prices. +请注意,如果$\alpha=1$,那么适应性预期就等同于朴素预期。 +减小$\alpha$的值会使更多权重转移到先前的预期上,这会稳定预期价格。 +这种增加的稳定性可以在图表中看出。 -This increased stability can be seen in the figures. - -## Exercises +## 练习 ```{exercise-start} :label: cobweb_ex1 ``` -Using the default `Market` class and naive expectations, plot a time series simulation of supply (rather than the price). - -Show, in particular, that supply also cycles. +使用默认的`Market`类和简单的预期,绘制一个供给(而不是价格)的时间序列模拟。 +特别展示供给也会循环变化。 ```{exercise-end} ``` @@ -489,15 +433,14 @@ Show, in particular, that supply also cycles. ```{code-cell} ipython3 def ts_plot_supply(model, p0, ts_length=10): """ - Function to simulate and plot the supply function - given the initial price. + 给定初始价格,模拟和绘制供给函数的函数。 """ pe_last = p0 s_values = np.empty(ts_length) for i in range(ts_length): - # store quantity + # 库存数量 s_values[i] = model.supply(pe_last) - # update price + # 更新价格 pe_last = - (s_values[i] - model.a) / model.b @@ -511,8 +454,8 @@ def ts_plot_supply(model, p0, ts_length=10): ax.legend(loc='best', fontsize=10) ax.set_xticks(np.arange(ts_length)) - ax.set_xlabel("time") - ax.set_ylabel("quantity") + ax.set_xlabel("时间") + ax.set_ylabel("数量") plt.show() ``` @@ -527,13 +470,11 @@ ts_plot_supply(m, 5, 15) ```{exercise-start} :label: cobweb_ex2 ``` -**Backward looking average expectations** +**回顾性平均预期** -Backward looking average expectations refers to the case where producers form -expectations for the next period price as a linear combination of their last -guess and the second last guess. +回顾性平均预期是指生产者对下一期价格的预期形成为他们最后一次猜测和倒数第二次猜测的线性组合。 -That is, +也就是说, ```{math} :label: pe_blae @@ -541,7 +482,7 @@ p_t^e = \alpha p_{t-1} + (1-\alpha) p_{t-2} ``` -Simulate and plot the price dynamics for $\alpha \in \{0.1, 0.3, 0.5, 0.8\}$ where $p_0=1$ and $p_1=2.5$. +模拟并绘制 $\alpha \in \{0.1, 0.3, 0.5, 0.8\}$ 时的价格动态,其中 $p_0=1$ 且 $p_1=2.5$。 ```{exercise-end} ``` @@ -553,8 +494,7 @@ Simulate and plot the price dynamics for $\alpha \in \{0.1, 0.3, 0.5, 0.8\}$ whe ```{code-cell} ipython3 def find_next_price_blae(model, curr_price_exp): """ - Function to find the next price given the current price expectation - and Market model + 给定当前价格预期和市场模型,找出下一个价格的函数。 """ return - (model.supply(curr_price_exp) - model.a) / model.b ``` @@ -562,8 +502,7 @@ def find_next_price_blae(model, curr_price_exp): ```{code-cell} ipython3 def ts_plot_price_blae(model, p0, p1, alphas, ts_length=15): """ - Function to simulate and plot the time series of price - using backward looking average expectations. + 使用回顾性平均预期模拟并绘制价格时间序列的函数。 """ fig, axes = plt.subplots(len(alphas), 1, figsize=(8, 16)) diff --git a/lectures/commod_price.md b/lectures/commod_price.md index efab814..e7e1d53 100644 --- a/lectures/commod_price.md +++ b/lectures/commod_price.md @@ -12,36 +12,34 @@ kernelspec: -# Commodity Prices +# 商品价格 -## Outline +## 大纲 -For more than half of all countries around the globe, [commodities](https://en.wikipedia.org/wiki/Commodity) account for [the majority of total exports](https://unctad.org/publication/commodities-and-development-report-2019). +在全球超过一半的国家中,[商品](https://en.wikipedia.org/wiki/Commodity) 占 [总出口的大部分](https://unctad.org/publication/commodities-and-development-report-2019)。 -Examples of commodities include copper, diamonds, iron ore, lithium, cotton -and coffee beans. +商品的例子包括铜、钻石、铁矿石、锂、棉花和咖啡豆。 -In this lecture we give an introduction to the theory of commodity prices. +在本讲中,我们将介绍商品价格理论。 -The lecture is quite advanced relative to other lectures in this series. +相对于本系列的其他讲座,这一讲座内容较为高级。 -We need to compute an equilibrium, and that equilibrium is described by a -price function. +我们需要计算一个均衡,该均衡由价格函数描述。 -We will solve an equation where the price function is the unknown. +我们将解一个方程,其中价格函数是未知量。 -This is harder than solving an equation for an unknown number, or vector. +这比解一个未知数或向量的方程更难。 -The lecture will discuss one way to solve a [functional equation](https://en.wikipedia.org/wiki/Functional_equation) (an equation where the unknown object is a function). +讲座将讨论一种解 [函数方程](https://en.wikipedia.org/wiki/Functional_equation)(未知对象是函数的方程)的方法。 -For this lecture we need the `yfinance` library. +对于本讲座,我们需要使用 `yfinance` 库。 ```{code-cell} ipython3 :tags: [hide-output] !pip install yfinance ``` -We will use the following imports +我们将使用以下导入 ```{code-cell} ipython3 @@ -53,9 +51,10 @@ from scipy.optimize import brentq from scipy.stats import beta ``` -## Data +## 数据 + +下图显示了自 2016 年初以来以美元计价的棉花价格。 -The figure below shows the price of cotton in USD since the start of 2016. ```{code-cell} ipython3 :tags: [hide-input, hide-output] @@ -75,167 +74,136 @@ ax.set_xlabel('date', fontsize=12) plt.show() ``` -The figure shows surprisingly large movements in the price of cotton. - -What causes these movements? - -In general, prices depend on the choices and actions of - -1. suppliers, -2. consumers, and -3. speculators. +该图显示了棉花价格的巨大波动,令人惊讶。 -Our focus will be on the interaction between these parties. +是什么导致了这些波动? -We will connect them together in a dynamic model of supply and demand, called -the *competitive storage model*. +一般来说,价格取决于以下各方的选择和行为: -This model was developed by -{cite}`samuelson1971stochastic`, -{cite}`wright1982economic`, {cite}`scheinkman1983simple`, -{cite}`deaton1992on`, {cite}`deaton1996competitive`, and -{cite}`chambers1996theory`. +1. 供应商, +2. 消费者,以及 +3. 投机者。 +我们的重点将是这些方之间的互动。 +我们将通过一个动态的供需模型将它们联系在一起,称为 *竞争性储存模型*。 +该模型由 {cite}`samuelson1971stochastic`、{cite}`wright1982economic`、{cite}`scheinkman1983simple`、{cite}`deaton1992on`、{cite}`deaton1996competitive` 和 {cite}`chambers1996theory` 开发。 -## The competitive storage model +## 竞争性储存模型 -In the competitive storage model, commodities are assets that +在竞争性储存模型中,商品是一种资产,它们: -1. can be traded by speculators and -1. have intrinsic value to consumers. +1. 可以被投机者交易,并且 +2. 对消费者有内在价值。 -Total demand is the sum of consumer demand and demand by speculators. +总需求是消费者需求和投机者需求的总和。 -Supply is exogenous, depending on "harvests". +供应是外生的,取决于“收成”。 ```{note} -These days, goods such as basic computer chips and integrated circuits are -often treated as commodities in financial markets, being highly standardized, -and, for these kinds of commodities, the word "harvest" is not -appropriate. +如今,像基本的计算机芯片和集成电路这样的商品在金融市场上通常被视为商品,因为它们高度标准化。对于这些类型的商品,使用“收成”一词并不合适。 -Nonetheless, we maintain it for simplicity. +尽管如此,为了简化问题,我们仍然保留了这个术语。 ``` -The equilibrium price is determined competitively. +均衡价格是通过竞争决定的。 -It is a function of the current state (which determines -current harvests and predicts future harvests). +它是当前状态的一个函数(决定当前的收成并预测未来的收成)。 -## The model +## 模型 -Consider a market for a single commodity, whose price is given at $t$ by -$p_t$. +考虑一个单一商品的市场,其价格在时间 $t$ 为 $p_t$。 -The harvest of the commodity at time $t$ is $Z_t$. +该商品在时间 $t$ 的收成为 $Z_t$。 -We assume that the sequence $\{ Z_t \}_{t \geq 1}$ is IID with common density function $\phi$, where $\phi$ is nonnegative. +我们假设序列 $\{ Z_t \}_{t \geq 1}$ 是独立同分布(IID)的,具有共同的密度函数 $\phi$,其中 $\phi$ 为非负。 -Speculators can store the commodity between periods, with $I_t$ units -purchased in the current period yielding $\alpha I_t$ units in the next. +投机者可以在各期之间储存该商品,当前期购买的 $I_t$ 单位在下一期将产生 $\alpha I_t$ 单位。 -Here the parameter $\alpha \in (0,1)$ is a depreciation rate for the commodity. +这里的参数 $\alpha \in (0,1)$ 是该商品的贬值率。 -For simplicity, the risk free interest rate is taken to be -zero, so expected profit on purchasing $I_t$ units is +为了简化问题,风险自由利率取为零,因此购买 $I_t$ 单位的预期利润为 $$ \mathbb{E}_t \, p_{t+1} \cdot \alpha I_t - p_t I_t = (\alpha \mathbb{E}_t \, p_{t+1} - p_t) I_t $$ +其中 $\mathbb{E}_t \, p_{t+1}$ 是在时间 $t$ 对 $p_{t+1}$ 的期望。 -Here $\mathbb{E}_t \, p_{t+1}$ is the expectation of $p_{t+1}$ taken at time -$t$. +## 均衡 +在本节中,我们定义均衡并讨论如何计算它。 -## Equilibrium +### 均衡条件 -In this section we define the equilibrium and discuss how to compute it. +假设投机者是风险中性的,这意味着他们在预期利润为正时会购买商品。 -### Equilibrium conditions +因此,如果预期利润为正,则市场不处于均衡状态。 -Speculators are assumed to be risk neutral, which means that they buy the -commodity whenever expected profits are positive. - -As a consequence, if expected profits are positive, then the market is not in -equilibrium. - -Hence, to be in equilibrium, prices must satisfy the "no-arbitrage" -condition +因此,要达到均衡,价格必须满足“无套利”条件 $$ \alpha \mathbb{E}_t \, p_{t+1} - p_t \leq 0 $$ (eq:arbi) -This means that if the expected price is lower than the current price, there is no room for arbitrage. +这意味着如果预期价格低于当前价格,则没有套利空间。 -Profit maximization gives the additional condition +利润最大化给出了额外条件 $$ - \alpha \mathbb{E}_t \, p_{t+1} - p_t < 0 \text{ implies } I_t = 0 + \alpha \mathbb{E}_t \, p_{t+1} - p_t < 0 \text{ 意味着 } I_t = 0 $$ (eq:pmco) +我们还要求市场清算,即每期供应等于需求。 -We also require that the market clears, with supply equaling demand in each period. - -We assume that consumers generate demand quantity $D(p)$ corresponding to -price $p$. +我们假设消费者根据价格 $p$ 生成需求量 $D(p)$。 -Let $P := D^{-1}$ be the inverse demand function. +令 $P := D^{-1}$ 为逆需求函数。 +关于数量, -Regarding quantities, +* 供应是投机者的持有量和当前收成的总和,且 +* 需求是消费者购买和投机者购买的总和。 -* supply is the sum of carryover by speculators and the current harvest, and -* demand is the sum of purchases by consumers and purchases by speculators. +在数学上, -Mathematically, +* 供应由 $X_t = \alpha I_{t-1} + Z_t$ 给出,其取值在 $S := \mathbb R_+$ 中,而 +* 需求为 $D(p_t) + I_t$ -* supply is given by $X_t = \alpha I_{t-1} + Z_t$, which takes values in $S := \mathbb R_+$, while -* demand $ = D(p_t) + I_t$ - -Thus, the market equilibrium condition is +因此,市场均衡条件为 $$ \alpha I_{t-1} + Z_t = D(p_t) + I_t $$ (eq:mkeq) - -The initial condition $X_0 \in S$ is treated as given. - +初始条件 $X_0 \in S$ 被视为给定。 -### An equilibrium function +### 一个均衡函数 -How can we find an equilibrium? +我们如何找到均衡? -Our path of attack will be to seek a system of prices that depend only on the -current state. +我们的攻击路径将是寻找一个仅依赖于当前状态的价格系统。 -(Our solution method involves using an [ansatz](https://en.wikipedia.org/wiki/Ansatz), which is an educated guess --- in this case for the price function.) +(我们的解法涉及使用 [ansatz](https://en.wikipedia.org/wiki/Ansatz),这是一种经过推测的猜测——在这种情况下是针对价格函数的猜测。) -In other words, we take a function $p$ on $S$ and set $p_t = p(X_t)$ for every $t$. +换句话说,我们在 $S$ 上取一个函数 $p$,并为每个 $t$ 设置 $p_t = p(X_t)$。 -Prices and quantities then follow +价格和数量随后遵循 $$ p_t = p(X_t), \quad I_t = X_t - D(p_t), \quad X_{t+1} = \alpha I_t + Z_{t+1} $$ (eq:eosy) +我们选择 $p$ 使得这些价格和数量满足上述均衡条件。 -We choose $p$ so that these prices and quantities satisfy the equilibrium -conditions above. - -More precisely, we seek a $p$ such that [](eq:arbi) and [](eq:pmco) hold for -the corresponding system [](eq:eosy). - +更准确地说,我们寻找一个 $p$,使得 [](eq:arbi) 和 [](eq:pmco) 对应的系统 [](eq:eosy) 成立。 $$ p^*(x) = \max @@ -245,17 +213,16 @@ $$ \qquad (x \in S) $$ (eq:dopf) -where +其中 $$ I(x) := x - D(p^*(x)) \qquad (x \in S) $$ (eq:einvf) -It turns out that such a $p^*$ will suffice, in the sense that [](eq:arbi) -and [](eq:pmco) hold for the corresponding system [](eq:eosy). +事实证明,这样的 $p^*$ 是足够的,因为 [](eq:arbi) 和 [](eq:pmco) 对应的系统 [](eq:eosy) 成立。 -To see this, observe first that +要看到这一点,首先观察 $$ \mathbb{E}_t \, p_{t+1} @@ -264,53 +231,48 @@ $$ = \int_0^\infty p^*(\alpha I(X_t) + z) \phi(z)dz $$ -Thus [](eq:arbi) requires that +因此,条件 [](eq:arbi) 要求 $$ \alpha \int_0^\infty p^*(\alpha I(X_t) + z) \phi(z)dz \leq p^*(X_t) $$ -This inequality is immediate from [](eq:dopf). +这个不等式是从 [](eq:dopf) 直接得到的。 -Second, regarding [](eq:pmco), suppose that +其次,关于 [](eq:pmco),假设 $$ \alpha \int_0^\infty p^*(\alpha I(X_t) + z) \phi(z)dz < p^*(X_t) $$ -Then by [](eq:dopf) we have $p^*(X_t) = P(X_t)$ +那么根据 [](eq:dopf),我们有 $p^*(X_t) = P(X_t)$。 -But then $D(p^*(X_t)) = X_t$ and $I_t = I(X_t) = 0$. +但这时 $D(p^*(X_t)) = X_t$,并且 $I_t = I(X_t) = 0$。 -As a consequence, both [](eq:arbi) and [](eq:pmco) hold. +因此,条件 [](eq:arbi) 和 [](eq:pmco) 都成立。 -We have found an equilibrium, which verifies the ansatz. +我们找到了一个均衡,验证了 ansatz。 -### Computing the equilibrium +### 计算均衡 -We now know that an equilibrium can be obtained by finding a function $p^*$ -that satisfies [](eq:dopf). +我们现在知道,均衡可以通过找到一个满足 [](eq:dopf) 的函数 $p^*$ 来获得。 -It can be shown that, under mild conditions there is exactly one function on -$S$ satisfying [](eq:dopf). +可以证明,在温和的条件下,$S$ 上恰好存在一个满足 [](eq:dopf) 的函数。 -Moreover, we can compute this function using successive approximation. +此外,我们可以通过逐次逼近来计算这个函数。 -This means that we start with a guess of the function and then update it using -[](eq:dopf). +这意味着我们从对函数的一个猜测开始,然后使用 [](eq:dopf) 更新它。 -This generates a sequence of functions $p_1, p_2, \ldots$ +这会生成一系列函数 $p_1, p_2, \ldots$ -We continue until this process converges, in the sense that $p_k$ and -$p_{k+1}$ are very close together. +我们继续这个过程,直到它收敛,即 $p_k$ 和 $p_{k+1}$ 非常接近。 -Then we take the final $p_k$ that we computed as our approximation of $p^*$. +然后,我们将计算得到的最终 $p_k$ 作为 $p^*$ 的近似值。 -To implement our update step, it is helpful if we put [](eq:dopf) and -[](eq:einvf) together. +为了实现我们的更新步骤,将 [](eq:dopf) 和 [](eq:einvf) 放在一起是有帮助的。 -This leads us to the update rule +这使我们得到了更新规则 $$ p_{k+1}(x) = \max @@ -319,7 +281,7 @@ $$ \right\} $$ (eq:dopf2) -In other words, we take $p_k$ as given and, at each $x$, solve for $q$ in +换句话说,我们将 $p_k$ 视为给定,并在每个 $x$ 处求解 $q$ $$ q = \max @@ -328,25 +290,22 @@ $$ \right\} $$ (eq:dopf3) -Actually we can't do this at every $x$, so instead we do it on a grid of -points $x_1, \ldots, x_n$. +实际上,我们无法在每个 $x$ 处进行此操作,因此我们在一系列点 $x_1, \ldots, x_n$ 上进行。 -Then we get the corresponding values $q_1, \ldots, q_n$. +然后我们得到对应的值 $q_1, \ldots, q_n$。 -Then we compute $p_{k+1}$ as the linear interpolation of -the values $q_1, \ldots, q_n$ over the grid $x_1, \ldots, x_n$. +接着,我们将 $p_{k+1}$ 计算为在网格 $x_1, \ldots, x_n$ 上对值 $q_1, \ldots, q_n$ 的线性插值。 -Then we repeat, seeking convergence. +然后我们重复这个过程,寻求收敛。 +## 代码 -## Code +下面的代码实现了这个迭代过程,从 $p_0 = P$ 开始。 -The code below implements this iterative process, starting from $p_0 = P$. +分布 $\phi$ 被设定为一个偏移的贝塔分布(尽管可以选择许多其他的分布)。 -The distribution $\phi$ is set to a shifted Beta distribution (although many -other choices are possible). +在 [](eq:dopf3) 中的积分通过 {ref}`Monte Carlo ` 方法计算。 -The integral in [](eq:dopf3) is computed via {ref}`Monte Carlo `. ```{code-cell} ipython3 @@ -358,7 +317,7 @@ grid_max = 35 grid = np.linspace(a, grid_max, gridsize) beta_dist = beta(5, 5) -Z = a + beta_dist.rvs(mc_draw_size) * c # Shock observations +Z = a + beta_dist.rvs(mc_draw_size) * c # 冲击观察 D = P = lambda x: 1.0 / x tol = 1e-4 @@ -367,13 +326,14 @@ def T(p_array): new_p = np.empty_like(p_array) - # Interpolate to obtain p as a function. + # 插值以获得p 作为函数。 + p = interp1d(grid, p_array, fill_value=(p_array[0], p_array[-1]), bounds_error=False) - # Update + # 更新 for i, x in enumerate(grid): h = lambda q: q - max(α * np.mean(p(α * (x - D(q)) + Z)), P(x)) @@ -385,7 +345,7 @@ def T(p_array): fig, ax = plt.subplots() price = P(grid) -ax.plot(grid, price, alpha=0.5, lw=1, label="inverse demand curve") +ax.plot(grid, price, alpha=0.5, lw=1, label="反需求曲线") error = tol + 1 while error > tol: new_price = T(price) @@ -395,20 +355,18 @@ while error > tol: ax.plot(grid, price, 'k-', alpha=0.5, lw=2, label=r'$p^*$') ax.legend() ax.set_xlabel('$x$') -ax.set_ylabel("prices") +ax.set_ylabel("价格") plt.show() ``` -The figure above shows the inverse demand curve $P$, which is also $p_0$, as -well as our approximation of $p^*$. +上图显示了逆需求曲线 $P$,也就是 $p_0$,以及我们对 $p^*$ 的近似。 -Once we have an approximation of $p^*$, we can simulate a time series of -prices. +一旦我们得到了 $p^*$ 的近似值,就可以模拟价格的时间序列。 ```{code-cell} ipython3 -# Turn the price array into a price function +# 将价格数组转化为价格函数。 p_star = interp1d(grid, price, fill_value=(price[0], price[-1]), @@ -426,8 +384,8 @@ def generate_cp_ts(init=1, n=50): return p_star(X) fig, ax = plt.subplots() -ax.plot(generate_cp_ts(), label="price") -ax.set_xlabel("time") +ax.plot(generate_cp_ts(), label="价格") +ax.set_xlabel("时间") ax.legend() plt.show() ``` diff --git a/lectures/complex_and_trig.md b/lectures/complex_and_trig.md index 7f40497..68f4aaf 100644 --- a/lectures/complex_and_trig.md +++ b/lectures/complex_and_trig.md @@ -21,169 +21,118 @@ kernelspec: ```{index} single: python ``` -# Complex Numbers and Trigonometry - -## Overview - -This lecture introduces some elementary mathematics and trigonometry. - -Useful and interesting in its own right, these concepts reap substantial rewards when studying dynamics generated -by linear difference equations or linear differential equations. - -For example, these tools are keys to understanding outcomes attained by Paul -Samuelson (1939) {cite}`Samuelson1939` in his classic paper on interactions -between the investment accelerator and the Keynesian consumption function, our -topic in the lecture {doc}`Samuelson Multiplier Accelerator `. - -In addition to providing foundations for Samuelson's work and extensions of -it, this lecture can be read as a stand-alone quick reminder of key results -from elementary high school trigonometry. - -So let's dive in. - -### Complex Numbers - -A complex number has a **real part** $x$ and a purely **imaginary part** $y$. - -The Euclidean, polar, and trigonometric forms of a complex number $z$ are: - +# 复数和三角函数 +## 概述 +本讲座介绍一些基础数学和三角函数知识。 +这些概念本身既有用又有趣,在研究由线性差分方程或线性微分方程生成的动力学时,会带来巨大回报。 +例如,这些工具是理解Paul Samuelson(1939年){cite}`Samuelson1939`在其经典论文中关于投资加速器与凯恩斯消费函数相互作用的成果的关键,这是我们在讲座{doc}`Samuelson乘数加速器`中的主题。 +除了为Samuelson的工作及其扩展提供基础外,本讲座还可以作为独立的快速回顾,回顾高中基础三角函数的关键结果。 +那么让我们开始吧。 + +### 复数 +复数有一个**实部**$x$和一个纯**虚部**$y$。 +复数$z$的欧几里得形式、极坐标形式和三角形式是: $$ z = x + iy = re^{i\theta} = r(\cos{\theta} + i \sin{\theta}) $$ +上面的第二个等式被称为**欧拉公式** +- [欧拉](https://en.wikipedia.org/wiki/Leonhard_Euler)还贡献了许多其他公式! -The second equality above is known as **Euler's formula** - -- [Euler](https://en.wikipedia.org/wiki/Leonhard_Euler) contributed many other formulas too! - -The complex conjugate $\bar z$ of $z$ is defined as - +$z$的复共轭$\bar z$定义为 $$ \bar z = x - iy = r e^{-i \theta} = r (\cos{\theta} - i \sin{\theta} ) $$ +$x$是$z$的**实部**,$y$是$z$的**虚部**。 -The value $x$ is the **real** part of $z$ and $y$ is the -**imaginary** part of $z$. - -The symbol $| z |$ = $\sqrt{\bar{z}\cdot z} = r$ represents the **modulus** of $z$. - -The value $r$ is the Euclidean distance of vector $(x,y)$ from the -origin: - +符号$| z |$ = $\sqrt{\bar{z}\cdot z} = r$表示$z$的**模**。 +$r$是向量$(x,y)$到原点的欧几里得距离: $$ r = |z| = \sqrt{x^2 + y^2} $$ - -The value $\theta$ is the angle of $(x,y)$ with respect to the real axis. - -Evidently, the tangent of $\theta$ is $\left(\frac{y}{x}\right)$. - -Therefore, - +$\theta$是$(x,y)$相对于实轴的角度。 +显然,$\theta$的正切是$\left(\frac{y}{x}\right)$。 +因此, $$ \theta = \tan^{-1} \Big( \frac{y}{x} \Big) $$ - -Three elementary trigonometric functions are - +三个基本三角函数是 $$ \cos{\theta} = \frac{x}{r} = \frac{e^{i\theta} + e^{-i\theta}}{2} , \quad \sin{\theta} = \frac{y}{r} = \frac{e^{i\theta} - e^{-i\theta}}{2i} , \quad \tan{\theta} = \frac{y}{x} $$ - -We'll need the following imports: +我们需要以下导入: ```{code-cell} ipython import matplotlib.pyplot as plt -plt.rcParams["figure.figsize"] = (11, 5) #set default figure size +plt.rcParams["figure.figsize"] = (11, 5) #设计默认的图像大小 import numpy as np from sympy import (Symbol, symbols, Eq, nsolve, sqrt, cos, sin, simplify, init_printing, integrate) ``` - -### An Example - -```{prf:example} -:label: ct_ex_com - -Consider the complex number $z = 1 + \sqrt{3} i$. - -For $z = 1 + \sqrt{3} i$, $x = 1$, $y = \sqrt{3}$. - -It follows that $r = 2$ and -$\theta = \tan^{-1}(\sqrt{3}) = \frac{\pi}{3} = 60^o$. -``` - -Let's use Python to plot the trigonometric form of the complex number -$z = 1 + \sqrt{3} i$. +### 一个例子 +考虑复数 $z = 1 + \sqrt{3} i$。 +对于 $z = 1 + \sqrt{3} i$,$x = 1$,$y = \sqrt{3}$。 +由此可得 $r = 2$ 且 +$\theta = \tan^{-1}(\sqrt{3}) = \frac{\pi}{3} = 60^o$。 +让我们使用Python来绘制复数 $z = 1 + \sqrt{3} i$ 的三角形式。 ```{code-cell} python3 -# Abbreviate useful values and functions +# 将值和函数简写 π = np.pi -# Set parameters +# 设置参数 r = 2 θ = π/3 x = r * np.cos(θ) x_range = np.linspace(0, x, 1000) θ_range = np.linspace(0, θ, 1000) -# Plot +# 画图 fig = plt.figure(figsize=(8, 8)) ax = plt.subplot(111, projection='polar') -ax.plot((0, θ), (0, r), marker='o', color='b') # Plot r -ax.plot(np.zeros(x_range.shape), x_range, color='b') # Plot x -ax.plot(θ_range, x / np.cos(θ_range), color='b') # Plot y -ax.plot(θ_range, np.full(θ_range.shape, 0.1), color='r') # Plot θ +ax.plot((0, θ), (0, r), marker='o', color='b') # 绘制 r +ax.plot(np.zeros(x_range.shape), x_range, color='b') # 绘制 x +ax.plot(θ_range, x / np.cos(θ_range), color='b') # 绘制 y +ax.plot(θ_range, np.full(θ_range.shape, 0.1), color='r') # 绘制 θ -ax.margins(0) # Let the plot starts at origin +ax.margins(0) # 从原点开始绘制 -ax.set_title("Trigonometry of complex numbers", va='bottom', +ax.set_title("复数的三角函数", va='bottom', fontsize='x-large') ax.set_rmax(2) -ax.set_rticks((0.5, 1, 1.5, 2)) # Less radial ticks -ax.set_rlabel_position(-88.5) # Get radial labels away from plotted line +ax.set_rticks((0.5, 1, 1.5, 2)) # 减少标记 +ax.set_rlabel_position(-88.5) # 将标记远离图像 -ax.text(θ, r+0.01 , r'$z = x + iy = 1 + \sqrt{3}\, i$') # Label z -ax.text(θ+0.2, 1 , '$r = 2$') # Label r -ax.text(0-0.2, 0.5, '$x = 1$') # Label x -ax.text(0.5, 1.2, r'$y = \sqrt{3}$') # Label y -ax.text(0.25, 0.15, r'$\theta = 60^o$') # Label θ +ax.text(θ, r+0.01 , r'$z = x + iy = 1 + \sqrt{3}\, i$') # 标记 z +ax.text(θ+0.2, 1 , '$r = 2$') # 标记 r +ax.text(0-0.2, 0.5, '$x = 1$') # 标记 x +ax.text(0.5, 1.2, r'$y = \sqrt{3}$') # 标记 y +ax.text(0.25, 0.15, r'$\theta = 60^o$') # 标记 θ ax.grid(True) plt.show() ``` - -## De Moivre's Theorem - -de Moivre's theorem states that: - +## 德莫瓦定理 +德莫瓦定理指出: $$ (r(\cos{\theta} + i \sin{\theta}))^n = r^n e^{in\theta} = r^n(\cos{n\theta} + i \sin{n\theta}) $$ - -To prove de Moivre's theorem, note that - +要证明德莫瓦定理,注意到 $$ (r(\cos{\theta} + i \sin{\theta}))^n = \big( re^{i\theta} \big)^n $$ +然后进行计算。 -and compute. - -## Applications of de Moivre's Theorem - -### Example 1 - -We can use de Moivre's theorem to show that -$r = \sqrt{x^2 + y^2}$. - -We have - +## 德莫瓦定理的应用 +### 例1 +我们可以使用德莫瓦定理来证明 $r = \sqrt{x^2 + y^2}$。 +我们有 $$ \begin{aligned} 1 &= e^{i\theta} e^{-i\theta} \\ @@ -193,30 +142,19 @@ $$ &= \frac{x^2}{r^2} + \frac{y^2}{r^2} \end{aligned} $$ - -and thus - +因此 $$ x^2 + y^2 = r^2 $$ +我们认识到这是**勾股定理**。 -We recognize this as a theorem of **Pythagoras**. - -### Example 2 - -Let $z = re^{i\theta}$ and $\bar{z} = re^{-i\theta}$ so that $\bar{z}$ is the **complex conjugate** of $z$. - -$(z, \bar z)$ form a **complex conjugate pair** of complex numbers. - -Let $a = pe^{i\omega}$ and $\bar{a} = pe^{-i\omega}$ be -another complex conjugate pair. - -For each element of a sequence of integers $n = 0, 1, 2, \ldots, $. - -To do so, we can apply de Moivre's formula. - -Thus, - +### 例2 +设 $z = re^{i\theta}$ 且 $\bar{z} = re^{-i\theta}$,其中 $\bar{z}$ 是 $z$ 的**复共轭**。 +$(z, \bar z)$ 构成一对**复共轭对**。 +设 $a = pe^{i\omega}$ 和 $\bar{a} = pe^{-i\omega}$ 是另一对复共轭对。 +对于整数序列 $n = 0, 1, 2, \ldots, $ 中的每个元素。 +为此,我们可以应用德莫瓦公式。 +因此, $$ \begin{aligned} x_n &= az^n + \bar{a}\bar{z}^n \\ @@ -228,151 +166,119 @@ x_n &= az^n + \bar{a}\bar{z}^n \\ \end{aligned} $$ -### Example 3 - -This example provides machinery that is at the heard of Samuelson's analysis of his multiplier-accelerator model {cite}`Samuelson1939`. - -Thus, consider a **second-order linear difference equation** - +### 例3 +这个例子提供了Samuelson在分析其乘数-加速器模型时所使用的核心机制 {cite}`Samuelson1939`。 +因此,考虑一个**二阶线性差分方程** $$ x_{n+2} = c_1 x_{n+1} + c_2 x_n $$ - -whose **characteristic polynomial** is - +其**特征多项式**为 $$ z^2 - c_1 z - c_2 = 0 $$ - -or - +或 $$ (z^2 - c_1 z - c_2 ) = (z - z_1)(z- z_2) = 0 $$ +具有根 $z_1, z_1$。 +**解**是满足差分方程的序列 $\{x_n\}_{n=0}^\infty$。 +在以下情况下,我们可以应用例2的公式来解决差分方程 +- 差分方程特征多项式的根 $z_1, z_2$ 构成一对复共轭 +- 给定初始条件 $x_0, x_1$ 的值 -has roots $z_1, z_1$. - -A **solution** is a sequence $\{x_n\}_{n=0}^\infty$ that satisfies -the difference equation. - -Under the following circumstances, we can apply our example 2 formula to -solve the difference equation - -- the roots $z_1, z_2$ of the characteristic polynomial of the - difference equation form a complex conjugate pair -- the values $x_0, x_1$ are given initial conditions - -To solve the difference equation, recall from example 2 that - +要解决差分方程,回想例2中 $$ x_n = 2 pr^n \cos{(\omega + n\theta)} $$ - -where $\omega, p$ are coefficients to be determined from -information encoded in the initial conditions $x_1, x_0$. - -Since -$x_0 = 2 p \cos{\omega}$ and $x_1 = 2 pr \cos{(\omega + \theta)}$ -the ratio of $x_1$ to $x_0$ is - +其中 $\omega, p$ 是需要从初始条件 $x_1, x_0$ 中编码的信息确定的系数。 +由于 +$x_0 = 2 p \cos{\omega}$ 且 $x_1 = 2 pr \cos{(\omega + \theta)}$ +$x_1$ 与 $x_0$ 的比率为 $$ \frac{x_1}{x_0} = \frac{r \cos{(\omega + \theta)}}{\cos{\omega}} $$ +我们可以解这个方程得到 $\omega$,然后用 $x_0 = 2 pr^0 \cos{(\omega + n\theta)}$ 解出 $p$。 -We can solve this equation for $\omega$ then solve for $p$ using $x_0 = 2 pr^0 \cos{(\omega + n\theta)}$. - -With the `sympy` package in Python, we are able to solve and plot the -dynamics of $x_n$ given different values of $n$. +使用Python中的`sympy`包,我们能够解决并绘制给定不同 $n$ 值时 $x_n$ 的动态。 +在这个例子中,我们设置初始值: +- $r = 0.9$ +- $\theta = \frac{1}{4}\pi$ +- $x_0 = 4$ +- $x_1 = r \cdot 2\sqrt{2} = 1.8 \sqrt{2}$ -In this example, we set the initial values: - $r = 0.9$ - -$\theta = \frac{1}{4}\pi$ - $x_0 = 4$ - -$x_1 = r \cdot 2\sqrt{2} = 1.8 \sqrt{2}$. - -We first numerically solve for $\omega$ and $p$ using -`nsolve` in the `sympy` package based on the above initial -condition: +我们首先使用`sympy`包中的`nsolve`基于上述初始条件数值求解 $\omega$ 和 $p$: ```{code-cell} python3 -# Set parameters +# 设置参数 r = 0.9 θ = π/4 x0 = 4 x1 = 2 * r * sqrt(2) -# Define symbols to be calculated +# 定义要计算的符号 ω, p = symbols('ω p', real=True) -# Solve for ω -## Note: we choose the solution near 0 +# 求解 ω +## 注意:我们选择在 0 附近的解 eq1 = Eq(x1/x0 - r * cos(ω+θ) / cos(ω), 0) ω = nsolve(eq1, ω, 0) ω = float(ω) print(f'ω = {ω:1.3f}') -# Solve for p +# 求解 p eq2 = Eq(x0 - 2 * p * cos(ω), 0) p = nsolve(eq2, p, 0) p = float(p) print(f'p = {p:1.3f}') ``` -Using the code above, we compute that -$\omega = 0$ and $p = 2$. - -Then we plug in the values we solve for $\omega$ and $p$ -and plot the dynamic. +使用上面的代码,我们计算得出 +$\omega = 0$ 和 $p = 2$。 +然后我们将解出的 $\omega$ 和 $p$ 的值代入 +并绘制动态图。 ```{code-cell} python3 -# Define range of n +# 设定 n 的范围 max_n = 30 n = np.arange(0, max_n+1, 0.01) -# Define x_n +# 设定 x_n x = lambda n: 2 * p * r**n * np.cos(ω + n * θ) -# Plot +# 绘图 fig, ax = plt.subplots(figsize=(12, 8)) ax.plot(n, x(n)) ax.set(xlim=(0, max_n), ylim=(-5, 5), xlabel='$n$', ylabel='$x_n$') -# Set x-axis in the middle of the plot +# 将x轴放在图像中间 ax.spines['bottom'].set_position('center') ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') ax.xaxis.set_ticks_position('bottom') ax.yaxis.set_ticks_position('left') -ticklab = ax.xaxis.get_ticklabels()[0] # Set x-label position +ticklab = ax.xaxis.get_ticklabels()[0] # 设定x标记的位置 trans = ticklab.get_transform() ax.xaxis.set_label_coords(31, 0, transform=trans) -ticklab = ax.yaxis.get_ticklabels()[0] # Set y-label position +ticklab = ax.yaxis.get_ticklabels()[0] # 设定y标记的位置 trans = ticklab.get_transform() ax.yaxis.set_label_coords(0, 5, transform=trans) ax.grid() plt.show() ``` - -### Trigonometric Identities - -We can obtain a complete suite of trigonometric identities by -appropriately manipulating polar forms of complex numbers. - -We'll get many of them by deducing implications of the equality - +### 三角恒等式 +我们可以通过适当操作复数的极坐标形式来获得一套完整的三角恒等式。 +我们将通过推导等式 $$ e^{i(\omega + \theta)} = e^{i\omega} e^{i\theta} $$ +的含义来得到许多恒等式。 -For example, we'll calculate identities for - -$\cos{(\omega + \theta)}$ and $\sin{(\omega + \theta)}$. - -Using the sine and cosine formulas presented at the beginning of this -lecture, we have: - +例如,我们将计算 $\cos{(\omega + \theta)}$ 和 $\sin{(\omega + \theta)}$ 的恒等式。 +使用本讲座开始时给出的正弦和余弦公式,我们有: $$ \begin{aligned} \cos{(\omega + \theta)} = \frac{e^{i(\omega + \theta)} + e^{-i(\omega + \theta)}}{2} \\ @@ -380,8 +286,7 @@ $$ \end{aligned} $$ -We can also obtain the trigonometric identities as follows: - +我们还可以通过以下方式获得三角恒等式: $$ \begin{aligned} \cos{(\omega + \theta)} + i \sin{(\omega + \theta)} @@ -393,9 +298,7 @@ i (\cos{\omega}\sin{\theta} + \sin{\omega}\cos{\theta}) \end{aligned} $$ -Since both real and imaginary parts of the above formula should be -equal, we get: - +由于上述公式的实部和虚部都应相等,我们得到: $$ \begin{aligned} \cos{(\omega + \theta)} = \cos{\omega}\cos{\theta} - \sin{\omega}\sin{\theta} \\ @@ -403,34 +306,27 @@ $$ \end{aligned} $$ -The equations above are also known as the **angle sum identities**. We -can verify the equations using the `simplify` function in the -`sympy` package: +上述方程也被称为**角和恒等式**。我们可以使用`sympy`包中的`simplify`函数来验证这些方程: ```{code-cell} python3 -# Define symbols +# 设定符号 ω, θ = symbols('ω θ', real=True) -# Verify +# 检查 print("cos(ω)cos(θ) - sin(ω)sin(θ) =", simplify(cos(ω)*cos(θ) - sin(ω) * sin(θ))) print("cos(ω)sin(θ) + sin(ω)cos(θ) =", simplify(cos(ω)*sin(θ) + sin(ω) * cos(θ))) ``` +### 三角积分 +我们也可以使用复数的极坐标形式来计算三角积分。 -### Trigonometric Integrals - -We can also compute the trigonometric integrals using polar forms of -complex numbers. - -For example, we want to solve the following integral: - +例如,我们要解决以下积分: $$ \int_{-\pi}^{\pi} \cos(\omega) \sin(\omega) \, d\omega $$ -Using Euler's formula, we have: - +使用欧拉公式,我们有: $$ \begin{aligned} \int \cos(\omega) \sin(\omega) \, d\omega @@ -458,42 +354,38 @@ e^{2i\omega} - e^{-2i\omega} \end{aligned} $$ -and thus: - +因此: $$ \int_{-\pi}^{\pi} \cos(\omega) \sin(\omega) \, d\omega = \frac{1}{2}\sin^2(\pi) - \frac{1}{2}\sin^2(-\pi) = 0 $$ -We can verify the analytical as well as numerical results using -`integrate` in the `sympy` package: +我们可以使用`sympy`包中的`integrate`来验证分析结果和数值结果: ```{code-cell} python3 -# Set initial printing +# 设置初始打印 init_printing(use_latex="mathjax") ω = Symbol('ω') -print('The analytical solution for integral of cos(ω)sin(ω) is:') +print('cos(ω)sin(ω)积分的解析解为:') integrate(cos(ω) * sin(ω), ω) ``` ```{code-cell} python3 -print('The numerical solution for the integral of cos(ω)sin(ω) \ -from -π to π is:') +print('cos(ω)sin(ω)从 -π 到 π 的积分的数值解为:') integrate(cos(ω) * sin(ω), (ω, -π, π)) ``` + ### Exercises ```{exercise} :label: complex_ex1 -We invite the reader to verify analytically and with the `sympy` package the following two equalities: - +我们邀请读者通过解析方法和使用 `sympy` 包来验证以下两个等式: $$ \int_{-\pi}^{\pi} \cos (\omega)^2 \, d\omega = \pi $$ - $$ \int_{-\pi}^{\pi} \sin (\omega)^2 \, d\omega = \pi $$ @@ -502,27 +394,24 @@ $$ ```{solution-start} complex_ex1 :class: dropdown ``` - -Let's import symbolic $\pi$ from `sympy` +让我们从 `sympy` 导入符号 $\pi$ ```{code-cell} ipython3 -# Import symbolic π from sympy +# 从 sympy 导入符号 π from sympy import pi ``` ```{code-cell} ipython3 -print('The analytical solution for the integral of cos(ω)**2 \ -from -π to π is:') +print('cos(ω)**2 从 -π 到 π 的积分的解析解为:') integrate(cos(ω)**2, (ω, -pi, pi)) ``` ```{code-cell} ipython3 -print('The analytical solution for the integral of sin(ω)**2 \ -from -π to π is:') +print('sin(ω)**2 从 -π 到 π 的积分的解析解为:') integrate(sin(ω)**2, (ω, -pi, pi)) ``` ```{solution-end} -``` +``` \ No newline at end of file diff --git a/lectures/cons_smooth.md b/lectures/cons_smooth.md index 2719baa..af4ded6 100644 --- a/lectures/cons_smooth.md +++ b/lectures/cons_smooth.md @@ -11,35 +11,24 @@ kernelspec: name: python3 --- +# 消费平滑 +## 概述 +在本讲座中,我们将研究Milton Friedman {cite}`Friedman1956`和Robert Hall {cite}`Hall1978`提出的一个著名的"消费函数"模型,该模型旨在拟合一些实证数据模式,这些模式是原始凯恩斯消费函数(在QuantEcon讲座{doc}`几何级数 `中描述的)所忽略的。 -# Consumption Smoothing +在本讲座中,我们将使用矩阵乘法和矩阵求逆来研究通常称为"消费平滑模型"的内容,这些工具与我们在QuantEcon讲座{doc}`现值 `中使用的工具相同。 -## Overview +{doc}`现值公式`中提出的公式是消费平滑模型的核心,因为我们将使用它们来定义消费者的"人力财富"。 - -In this lecture, we'll study a famous model of the "consumption function" that Milton Friedman {cite}`Friedman1956` and Robert Hall {cite}`Hall1978`) proposed to fit some empirical data patterns that the original Keynesian consumption function described in this QuantEcon lecture {doc}`geometric series ` missed. - -In this lecture, we'll study what is often called the "consumption-smoothing model" using matrix multiplication and matrix inversion, the same tools that we used in this QuantEcon lecture {doc}`present values `. - -Formulas presented in {doc}`present value formulas` are at the core of the consumption-smoothing model because we shall use them to define a consumer's "human wealth". - -The key idea that inspired Milton Friedman was that a person's non-financial income, i.e., his or -her wages from working, could be viewed as a dividend stream from that person's ''human capital'' -and that standard asset-pricing formulas could be applied to compute a person's -''non-financial wealth'' that capitalizes the earnings stream. +Milton Friedman的关键想法是,一个人的非金融收入(即他或她从工作中获得的工资)可以被视为该人"人力资本"的股息流,并且可以应用标准资产定价公式来计算一个人的"非金融财富",这种财富将收入流资本化。 ```{note} -As we'll see in this QuantEcon lecture {doc}`equalizing difference model `, -Milton Friedman had used this idea in his PhD thesis at Columbia University, -eventually published as {cite}`kuznets1939incomes` and {cite}`friedman1954incomes`. +正如我们将在QuantEcon讲座{doc}`均衡差异模型 `中看到的,Milton Friedman在他在哥伦比亚大学的博士论文中使用了这个想法,最终发表为{cite}`kuznets1939incomes`和{cite}`friedman1954incomes`。 ``` -It will take a while for a "present value" or asset price explicilty to appear in this lecture, but when it does it will be a key actor. +在本讲座中,"现值"或资产价格显式出现需要一段时间,但当它出现时,它将成为一个关键角色。 - -## Analysis - -As usual, we'll start by importing some Python modules. +## 分析 +像往常一样,我们将从导入一些Python模块开始。 ```{code-cell} ipython3 import numpy as np @@ -47,94 +36,76 @@ import matplotlib.pyplot as plt from collections import namedtuple ``` +该模型描述了一个从时间 $t=0, 1, \ldots, T$ 生活的消费者,接收非金融收入流 $\{y_t\}_{t=0}^T$,并选择消费流 $\{c_t\}_{t=0}^T$。 +我们通常认为非金融收入流来自于个人提供劳动的工资。 +该模型将非金融收入流作为输入,将其视为"外生",即不由模型决定。 +消费者面临固定的总利率 $R >1$,她可以自由借贷,但有限制条件,我们将在下面描述。 -The model describes a consumer who lives from time $t=0, 1, \ldots, T$, receives a stream $\{y_t\}_{t=0}^T$ of non-financial income and chooses a consumption stream $\{c_t\}_{t=0}^T$. - -We usually think of the non-financial income stream as coming from the person's salary from supplying labor. - -The model takes a non-financial income stream as an input, regarding it as "exogenous" in the sense of not being determined by the model. - -The consumer faces a gross interest rate of $R >1$ that is constant over time, at which she is free to borrow or lend, up to limits that we'll describe below. - -To set up the model, let - - * $T \geq 2$ be a positive integer that constitutes a time-horizon. - * $y = \{y_t\}_{t=0}^T$ be an exogenous sequence of non-negative non-financial incomes $y_t$. - * $a = \{a_t\}_{t=0}^{T+1}$ be a sequence of financial wealth. - * $c = \{c_t\}_{t=0}^T$ be a sequence of non-negative consumption rates. - * $R \geq 1$ be a fixed gross one period rate of return on financial assets. - * $\beta \in (0,1)$ be a fixed discount factor. - * $a_0$ be a given initial level of financial assets - * $a_{T+1} \geq 0$ be a terminal condition on final assets. - -The sequence of financial wealth $a$ is to be determined by the model. - -We require it to satisfy two **boundary conditions**: - - * it must equal an exogenous value $a_0$ at time $0$ - * it must equal or exceed an exogenous value $a_{T+1}$ at time $T+1$. - -The **terminal condition** $a_{T+1} \geq 0$ requires that the consumer not leave the model in debt. +为设置模型,让: + * $T \geq 2$ 为一个正整数,构成时间范围。 + * $y = \{y_t\}_{t=0}^T$ 为外生的非负非金融收入 $y_t$ 序列。 + * $a = \{a_t\}_{t=0}^{T+1}$ 为金融财富序列。 + * $c = \{c_t\}_{t=0}^T$ 为非负消费率序列。 + * $R \geq 1$ 为固定的金融资产一期总回报率。 + * $\beta \in (0,1)$ 为固定的折现因子。 + * $a_0$ 为给定的初始金融资产水平。 + * $a_{T+1} \geq 0$ 为最终资产的终端条件。 -(We'll soon see that a utility maximizing consumer won't want to die leaving positive assets, so she'll arrange her affairs to make -$a_{T+1} = 0$.) +金融财富序列 $a$ 由模型决定。 +它必须满足两个**边界条件**: + * 在时间 0 时必须等于外生值 $a_0$ + * 在时间 $T+1$ 时必须等于或超过外生值 $a_{T+1}$。 +**终端条件** $a_{T+1} \geq 0$ 要求消费者不能带着债务离开模型。 +(我们很快会看到,一个效用最大化的消费者不会想留下正资产就死去,所以她会安排她的事务使得 $a_{T+1} = 0$。) -The consumer faces a sequence of budget constraints that constrains sequences $(y, c, a)$ +消费者面临一系列预算约束,这些约束限制了序列 $(y, c, a)$ $$ a_{t+1} = R (a_t+ y_t - c_t), \quad t =0, 1, \ldots T $$ (eq:a_t) -Equations {eq}`eq:a_t` constitute $T+1$ such budget constraints, one for each $t=0, 1, \ldots, T$. +方程 {eq}`eq:a_t` 构成了 $T+1$ 个这样的预算约束,每个 $t=0, 1, \ldots, T$ 对应一个。 +给定非金融收入序列 $y$,大量的(金融财富,消费)序列对 $(a, c)$ 满足预算约束序列 {eq}`eq:a_t`。 -Given a sequence $y$ of non-financial incomes, a large set of pairs $(a, c)$ of (financial wealth, consumption) sequences satisfy the sequence of budget constraints {eq}`eq:a_t`. - -Our model has the following logical flow. - - * start with an exogenous non-financial income sequence $y$, an initial financial wealth $a_0$, and - a candidate consumption path $c$. +我们的模型有以下逻辑流程: + * 从外生非金融收入序列 $y$、初始金融财富 $a_0$ 和候选消费路径 $c$ 开始。 - * use the system of equations {eq}`eq:a_t` for $t=0, \ldots, T$ to compute a path $a$ of financial wealth + * 使用方程系统 {eq}`eq:a_t`($t=0, \ldots, T$)计算金融财富路径 $a$。 - * verify that $a_{T+1}$ satisfies the terminal wealth constraint $a_{T+1} \geq 0$. + * 验证 $a_{T+1}$ 是否满足终端财富约束 $a_{T+1} \geq 0$。 - * If it does, declare that the candidate path is **budget feasible**. + * 如果满足,声明该候选路径是**预算可行的**。 - * if the candidate consumption path is not budget feasible, propose a less greedy consumption path and start over + * 如果候选消费路径不是预算可行的,提出一个不那么贪婪的消费路径并重新开始。 -Below, we'll describe how to execute these steps using linear algebra -- matrix inversion and multiplication. - -The above procedure seems like a sensible way to find "budget-feasible" consumption paths $c$, i.e., paths that are consistent -with the exogenous non-financial income stream $y$, the initial financial asset level $a_0$, and the terminal asset level $a_{T+1}$. - -In general, there are **many** budget feasible consumption paths $c$. +下面,我们将描述如何使用线性代数——矩阵求逆和乘法来执行这些步骤。 -Among all budget-feasible consumption paths, which one should a consumer want? +上述程序似乎是找到"预算可行"消费路径 $c$ 的合理方法,即与外生非金融收入流 $y$、初始金融资产水平 $a_0$ 和终端资产水平 $a_{T+1}$ 一致的路径。 - -To answer this question, we shall eventually evaluate alternative budget feasible consumption paths $c$ using the following utility functional or **welfare criterion**: +一般来说,有**许多**预算可行的消费路径 $c$。 +在所有预算可行的消费路径中,消费者应该想要哪一个? +为回答这个问题,我们最终将使用以下效用函数或**福利标准**来评估替代的预算可行消费路径 $c$: ```{math} :label: welfare W = \sum_{t=0}^T \beta^t (g_1 c_t - \frac{g_2}{2} c_t^2 ) ``` +其中 $g_1 > 0, g_2 > 0$。 -where $g_1 > 0, g_2 > 0$. - -When $\beta R \approx 1$, the fact that the utility function $g_1 c_t - \frac{g_2}{2} c_t^2$ has diminishing marginal utility imparts a preference for consumption that is very smooth. +当 $\beta R \approx 1$ 时,效用函数 $g_1 c_t - \frac{g_2}{2} c_t^2$ 具有递减的边际效用,这赋予了对非常平滑消费的偏好。 -Indeed, we shall see that when $\beta R = 1$ (a condition assumed by Milton Friedman {cite}`Friedman1956` and Robert Hall {cite}`Hall1978`), criterion {eq}`welfare` assigns higher welfare to smoother consumption paths. +事实上,我们将看到,当 $\beta R = 1$(这是Milton Friedman {cite}`Friedman1956` 和 Robert Hall {cite}`Hall1978` 所假设的条件)时,标准 {eq}`welfare` 会为更平滑的消费路径分配更高的福利。 -By **smoother** we mean as close as possible to being constant over time. +所谓**更平滑**,我们指的是尽可能接近随时间保持恒定。 -The preference for smooth consumption paths that is built into the model gives it the name "consumption-smoothing model". +模型中内置的对平滑消费路径的偏好使其被称为"消费平滑模型"。 -Let's dive in and do some calculations that will help us understand how the model works. +让我们深入进行一些计算,这将帮助我们理解模型的工作原理。 -Here we use default parameters $R = 1.05$, $g_1 = 1$, $g_2 = 1/2$, and $T = 65$. +这里我们使用默认参数 $R = 1.05$,$g_1 = 1$,$g_2 = 1/2$,以及 $T = 65$。 -We create a Python **namedtuple** to store these parameters with default values. +我们创建一个Python的**命名元组**来存储这些带有默认值的参数。 ```{code-cell} ipython3 ConsumptionSmoothing = namedtuple("ConsumptionSmoothing", @@ -147,84 +118,78 @@ def create_consumption_smoothing_model(R=1.05, g1=1, g2=1/2, T=65): β_seq, T) ``` +## 弗里德曼-霍尔消费平滑模型 -## Friedman-Hall consumption-smoothing model - -A key object is what Milton Friedman called "human" or "non-financial" wealth at time $0$: - +一个关键概念是弥尔顿·弗里德曼所称的时间 0 的"人力"或"非金融"财富: $$ h_0 \equiv \sum_{t=0}^T R^{-t} y_t = \begin{bmatrix} 1 & R^{-1} & \cdots & R^{-T} \end{bmatrix} \begin{bmatrix} y_0 \cr y_1 \cr \vdots \cr y_T \end{bmatrix} $$ -Human or non-financial wealth at time $0$ is evidently just the present value of the consumer's non-financial income stream $y$. - -Formally it very much resembles the asset price that we computed in this QuantEcon lecture {doc}`present values `. +时间 0 的人力或非金融财富显然就是消费者非金融收入流 $y$ 的现值。 +从形式上看,它与我们在 QuantEcon 讲座 {doc}`现值 ` 中计算的资产价格非常相似。 +事实上,这就是为什么弥尔顿·弗里德曼称之为"人力资本"。 -Indeed, this is why Milton Friedman called it "human capital". - -By iterating on equation {eq}`eq:a_t` and imposing the terminal condition +通过迭代方程 {eq}`eq:a_t` 并施加终端条件 $$ a_{T+1} = 0, $$ -it is possible to convert a sequence of budget constraints {eq}`eq:a_t` into a single intertemporal constraint +可以将一系列预算约束 {eq}`eq:a_t` 转换为单一的跨期约束 $$ \sum_{t=0}^T R^{-t} c_t = a_0 + h_0. $$ (eq:budget_intertemp) -Equation {eq}`eq:budget_intertemp` says that the present value of the consumption stream equals the sum of financial and non-financial (or human) wealth. +方程 {eq}`eq:budget_intertemp` 表明消费流的现值等于金融和非金融(或人力)财富的总和。 -Robert Hall {cite}`Hall1978` showed that when $\beta R = 1$, a condition Milton Friedman had also assumed, it is "optimal" for a consumer to smooth consumption by setting +罗伯特·霍尔 {cite}`Hall1978` 证明,当 $\beta R = 1$ 时(这也是弥尔顿·弗里德曼假设的条件),消费者通过设置 $$ c_t = c_0 \quad t =0, 1, \ldots, T $$ -(Later we'll present a "variational argument" that shows that this constant path maximizes -criterion {eq}`welfare` when $\beta R =1$.) +来平滑消费是"最优"的。 +(稍后我们将提出一个"变分论证",说明当 $\beta R =1$ 时,这个常数路径最大化了准则 {eq}`welfare`。) -In this case, we can use the intertemporal budget constraint to write +在这种情况下,我们可以使用跨期预算约束来写出 $$ c_t = c_0 = \left(\sum_{t=0}^T R^{-t}\right)^{-1} (a_0 + h_0), \quad t= 0, 1, \ldots, T. $$ (eq:conssmoothing) -Equation {eq}`eq:conssmoothing` is the consumption-smoothing model in a nutshell. - +方程 {eq}`eq:conssmoothing` 简明扼要地概括了消费平滑模型。 -## Mechanics of consumption-smoothing model +## 消费平滑模型的机制 -As promised, we'll provide step-by-step instructions on how to use linear algebra, readily implemented in Python, to compute all objects in play in the consumption-smoothing model. +按照承诺,我们将提供逐步说明,说明如何使用线性代数(在 Python 中易于实现)来计算消费平滑模型中涉及的所有对象。 -In the calculations below, we'll set default values of $R > 1$, e.g., $R = 1.05$, and $\beta = R^{-1}$. +在以下计算中,我们将设置 $R > 1$ 的默认值,例如 $R = 1.05$,并且 $\beta = R^{-1}$。 -### Step 1 +### 步骤 1 -For a $(T+1) \times 1$ vector $y$, use matrix algebra to compute $h_0$ +对于 $(T+1) \times 1$ 向量 $y$,使用矩阵代数计算 $h_0$ $$ h_0 = \sum_{t=0}^T R^{-t} y_t = \begin{bmatrix} 1 & R^{-1} & \cdots & R^{-T} \end{bmatrix} \begin{bmatrix} y_0 \cr y_1 \cr \vdots \cr y_T \end{bmatrix} $$ -### Step 2 +### 步骤 2 -Compute an time $0$ consumption $c_0 $ : +计算时间 0 的消费 $c_0$: $$ c_t = c_0 = \left( \frac{1 - R^{-1}}{1 - R^{-(T+1)}} \right) (a_0 + \sum_{t=0}^T R^{-t} y_t ) , \quad t = 0, 1, \ldots, T $$ -### Step 3 +### 步骤 3 -Use the system of equations {eq}`eq:a_t` for $t=0, \ldots, T$ to compute a path $a$ of financial wealth. - -To do this, we translate that system of difference equations into a single matrix equation as follows: +使用方程系统 {eq}`eq:a_t`(对于 $t=0, \ldots, T$)来计算金融财富路径 $a$。 +为此,我们将该差分方程系统转换为单个矩阵方程,如下所示: $$ \begin{bmatrix} @@ -242,37 +207,33 @@ $$ \end{bmatrix} $$ -Multiply both sides by the inverse of the matrix on the left side to compute +将两边都乘以左侧矩阵的逆矩阵来计算 $$ \begin{bmatrix} a_1 \cr a_2 \cr a_3 \cr \vdots \cr a_T \cr a_{T+1} \end{bmatrix} $$ - -Because we have built into our calculations that the consumer leaves the model with exactly zero assets, just barely satisfying the -terminal condition that $a_{T+1} \geq 0$, it should turn out that +因为我们在计算中已经设定消费者离开模型时恰好没有资产,刚好满足终端条件 $a_{T+1} \geq 0$,所以应该得到 $$ a_{T+1} = 0. $$ - -Let's verify this with Python code. - -First we implement the model with `compute_optimal` +让我们用 Python 代码来验证这一点。 +首先,我们用 `compute_optimal` 来实现模型 ```{code-cell} ipython3 def compute_optimal(model, a0, y_seq): R, T = model.R, model.T - # non-financial wealth + # 非金融财富 h0 = model.β_seq @ y_seq # since β = 1/R # c0 c0 = (1 - 1/R) / (1 - (1/R)**(T+1)) * (a0 + h0) c_seq = c0*np.ones(T+1) - # verify + # 检查 A = np.diag(-R*np.ones(T), k=-1) + np.eye(T+1) b = y_seq - c_seq b[0] = b[0] + a0 @@ -283,37 +244,33 @@ def compute_optimal(model, a0, y_seq): return c_seq, a_seq, h0 ``` -We use an example where the consumer inherits $a_0<0$. - -This can be interpreted as a student debt. - -The non-financial process $\{y_t\}_{t=0}^{T}$ is constant and positive up to $t=45$ and then becomes zero afterward. - -The drop in non-financial income late in life reflects retirement from work. +我们使用一个例子,其中消费者继承了 $a_0<0$。 +这可以被解释为学生贷款。 +非金融过程 $\{y_t\}_{t=0}^{T}$ 在 $t=45$ 之前保持恒定且为正值,之后变为零。 +生命后期非金融收入的下降反映了退休。 ```{code-cell} ipython3 -# Financial wealth -a0 = -2 # such as "student debt" +# 金融财富 +a0 = -2 # 比如学生贷款 -# non-financial Income process +# 非金融收入过程 y_seq = np.concatenate([np.ones(46), np.zeros(20)]) cs_model = create_consumption_smoothing_model() c_seq, a_seq, h0 = compute_optimal(cs_model, a0, y_seq) -print('check a_T+1=0:', +print('检查 a_T+1=0:', np.abs(a_seq[-1] - 0) <= 1e-8) ``` - -The graphs below show paths of non-financial income, consumption, and financial assets. +下图展示了非金融收入,消费和金融资产的路径。 ```{code-cell} ipython3 -# Sequence Length +# 序列长度 T = cs_model.T -plt.plot(range(T+1), y_seq, label='non-financial income') -plt.plot(range(T+1), c_seq, label='consumption') -plt.plot(range(T+2), a_seq, label='financial wealth') +plt.plot(range(T+1), y_seq, label='非金融收入') +plt.plot(range(T+1), c_seq, label='消费') +plt.plot(range(T+2), a_seq, label='金融财富') plt.plot(range(T+2), np.zeros(T+2), '--') plt.legend() @@ -321,10 +278,8 @@ plt.xlabel(r'$t$') plt.ylabel(r'$c_t,y_t,a_t$') plt.show() ``` - -Note that $a_{T+1} = 0$, as anticipated. - -We can evaluate welfare criterion {eq}`welfare` +其中 $a_{T+1} = 0$如我们所预期的一样。 +我们可以测量福利标准 {eq}`welfare` ```{code-cell} ipython3 def welfare(model, c_seq): @@ -333,33 +288,31 @@ def welfare(model, c_seq): u_seq = g1 * c_seq - g2/2 * c_seq**2 return β_seq @ u_seq -print('Welfare:', welfare(cs_model, c_seq)) +print('福利:', welfare(cs_model, c_seq)) ``` -### Experiments +### 实验 -In this section we describe how a consumption sequence would optimally respond to different sequences sequences of non-financial income. +在本节中,我们描述了消费序列如何对不同的非金融收入序列做出最佳反应。 -First we create a function `plot_cs` that generates graphs for different instances of the consumption-smoothing model `cs_model`. - -This will help us avoid rewriting code to plot outcomes for different non-financial income sequences. +首先,我们创建一个名为`plot_cs`的函数,用于为`cs_model`(消费平滑模型)的不同实例生成图表。这将有助于我们避免为不同的非金融收入序列重写绘图代码。 ```{code-cell} ipython3 -def plot_cs(model, # consumption-smoothing model - a0, # initial financial wealth - y_seq # non-financial income process +def plot_cs(model, # 消费平滑模型 + a0, # 初始金融财富 + y_seq # 非金融收入过程 ): - # Compute optimal consumption + # 计算最优消费 c_seq, a_seq, h0 = compute_optimal(model, a0, y_seq) - # Sequence length + # 序列长度 T = cs_model.T - # Generate plot - plt.plot(range(T+1), y_seq, label='non-financial income') - plt.plot(range(T+1), c_seq, label='consumption') - plt.plot(range(T+2), a_seq, label='financial wealth') + # 绘图 + plt.plot(range(T+1), y_seq, label='非金融收入') + plt.plot(range(T+1), c_seq, label='消费') + plt.plot(range(T+2), a_seq, label='金融财富') plt.plot(range(T+2), np.zeros(T+2), '--') plt.legend() @@ -367,37 +320,33 @@ def plot_cs(model, # consumption-smoothing model plt.ylabel(r'$c_t,y_t,a_t$') plt.show() ``` +在下面的实验中,请研究消费和金融资产序列如何随着不同的非金融收入序列而变化。 -In the experiments below, please study how consumption and financial asset sequences vary across different sequences for non-financial income. - -#### Experiment 1: one-time gain/loss +#### 实验1:一次性收益/损失 -We first assume a one-time windfall of $W_0$ in year 21 of the income sequence $y$. - -We'll make $W_0$ big - positive to indicate a one-time windfall, and negative to indicate a one-time "disaster". +我们首先假设在收入序列$y$的第21年有一次性的意外收入$W_0$。 +我们会使$W_0$的数值较大 - 正值表示一次性意外收入,负值表示一次性"灾难"。 ```{code-cell} ipython3 -# Windfall W_0 = 2.5 +# 一次性意外收入W_0 = 2.5 y_seq_pos = np.concatenate([np.ones(21), np.array([2.5]), np.ones(24), np.zeros(20)]) plot_cs(cs_model, a0, y_seq_pos) ``` ```{code-cell} ipython3 -# Disaster W_0 = -2.5 +# 灾难 W_0 = -2.5 y_seq_neg = np.concatenate([np.ones(21), np.array([-2.5]), np.ones(24), np.zeros(20)]) plot_cs(cs_model, a0, y_seq_neg) ``` +#### 实验2:永久性工资增加/减少 -#### Experiment 2: permanent wage gain/loss - -Now we assume a permanent increase in income of $W$ in year 21 of the $y$-sequence. - -Again we can study positive and negative cases +现在我们假设在$y$序列的第21年,收入永久性增加$W$。 +同样,我们可以研究正面和负面的情况。 ```{code-cell} ipython3 -# Positive permanent income change W = 0.5 when t >= 21 +# 永久收入增加 W = 0.5 档 t >= 21 y_seq_pos = np.concatenate( [np.ones(21), 1.5*np.ones(25), np.zeros(20)]) @@ -405,38 +354,35 @@ plot_cs(cs_model, a0, y_seq_pos) ``` ```{code-cell} ipython3 -# Negative permanent income change W = -0.5 when t >= 21 +# 永久收入减少 W = -0.5 档 t >= 21 y_seq_neg = np.concatenate( [np.ones(21), .5*np.ones(25), np.zeros(20)]) plot_cs(cs_model, a0, y_seq_neg) ``` +#### 实验3:晚期起步者 -#### Experiment 3: a late starter - -Now we simulate a $y$ sequence in which a person gets zero for 46 years, and then works and gets 1 for the last 20 years of life (a "late starter") +现在我们模拟一个$y$序列,其中一个人在前46年收入为零,然后在生命的最后20年工作并获得1的收入(一个"晚起步者")。 ```{code-cell} ipython3 -# Late starter +# 晚起步者 y_seq_late = np.concatenate( [np.zeros(46), np.ones(20)]) plot_cs(cs_model, a0, y_seq_late) ``` -#### Experiment 4: geometric earner - -Now we simulate a geometric $y$ sequence in which a person gets $y_t = \lambda^t y_0$ in first 46 years. - -We first experiment with $\lambda = 1.05$ +#### 实验4:几何增长收入者 +现在我们模拟一个几何 $y$ 序列,其中一个人在前46年获得 $y_t = \lambda^t y_0$ 的收入。 +我们首先尝试 $\lambda = 1.05$ ```{code-cell} ipython3 -# Geometric earner parameters where λ = 1.05 +# 几何收入增长者 λ = 1.05 λ = 1.05 y_0 = 1 t_max = 46 -# Generate geometric y sequence +# 制造几何序列 y geo_seq = λ ** np.arange(t_max) * y_0 y_seq_geo = np.concatenate( [geo_seq, np.zeros(20)]) @@ -444,7 +390,7 @@ y_seq_geo = np.concatenate( plot_cs(cs_model, a0, y_seq_geo) ``` -Now we show the behavior when $\lambda = 0.95$ +我现在我们展示当$\lambda = 0.95$的行为。 ```{code-cell} ipython3 λ = 0.95 @@ -456,7 +402,7 @@ y_seq_geo = np.concatenate( plot_cs(cs_model, a0, y_seq_geo) ``` -What happens when $\lambda$ is negative +当$\lambda$为负的时候会发生什么 ```{code-cell} ipython3 λ = -0.95 @@ -468,75 +414,63 @@ y_seq_geo = np.concatenate( plot_cs(cs_model, a0, y_seq_geo) ``` +### 可行的消费变化 -### Feasible consumption variations - -We promised to justify our claim that a constant consumption play $c_t = c_0$ for all -$t$ is optimal. - -Let's do that now. - -The approach we'll take is an elementary example of the "calculus of variations". +我们承诺证明恒定消费计划 $c_t = c_0$(对所有 $t$)是最优的。现在让我们来做这个证明。 -Let's dive in and see what the key idea is. +我们将采用的方法是"变分法"的一个基本例子。让我们深入了解其中的关键思想。 -To explore what types of consumption paths are welfare-improving, we shall create an **admissible consumption path variation sequence** $\{v_t\}_{t=0}^T$ -that satisfies +为了探索哪些类型的消费路径能改善福利,我们将创建一个**可接受的消费路径变化序列** $\{v_t\}_{t=0}^T$,满足: $$ \sum_{t=0}^T R^{-t} v_t = 0 $$ -This equation says that the **present value** of admissible consumption path variations must be zero. +这个等式表明可接受的消费路径变化的**现值**必须为零。 -So once again, we encounter a formula for the present value of an "asset": +所以我们再次遇到了一个"资产"现值的公式: + * 我们要求消费路径变化的现值为零。 - * we require that the present value of consumption path variations be zero. - -Here we'll restrict ourselves to a two-parameter class of admissible consumption path variations -of the form +在这里,我们将限制自己使用一个两参数类的可接受消费路径变化,形式如下: $$ v_t = \xi_1 \phi^t - \xi_0 $$ -We say two and not three-parameter class because $\xi_0$ will be a function of $(\phi, \xi_1; R)$ that guarantees that the variation sequence is feasible. - -Let's compute that function. +我们说是两个而不是三个参数类,因为 $\xi_0$ 将是 $(\phi, \xi_1; R)$ 的函数,以保证变化序列是可行的。 +让我们来计算这个函数。 -We require +我们要求: $$ \sum_{t=0}^T R^{-t}\left[ \xi_1 \phi^t - \xi_0 \right] = 0 $$ -which implies that +这意味着: $$ \xi_1 \sum_{t=0}^T \phi_t R^{-t} - \xi_0 \sum_{t=0}^T R^{-t} = 0 $$ -which implies that +进而意味着: $$ \xi_1 \frac{1 - (\phi R^{-1})^{T+1}}{1 - \phi R^{-1}} - \xi_0 \frac{1 - R^{-(T+1)}}{1-R^{-1} } =0 $$ -which implies that +最终得到: $$ \xi_0 = \xi_0(\phi, \xi_1; R) = \xi_1 \left(\frac{1 - R^{-1}}{1 - R^{-(T+1)}}\right) \left(\frac{1 - (\phi R^{-1})^{T+1}}{1 - \phi R^{-1}}\right) $$ -This is our formula for $\xi_0$. +这就是我们的 $\xi_0$ 公式。 -**Key Idea:** if $c^o$ is a budget-feasible consumption path, then so is $c^o + v$, -where $v$ is a budget-feasible variation. +**关键思想:**如果 $c^o$ 是一个预算可行的消费路径,那么 $c^o + v$ 也是,其中 $v$ 是一个预算可行的变化。 -Given $R$, we thus have a two parameter class of budget feasible variations $v$ that we can use -to compute alternative consumption paths, then evaluate their welfare. +给定 $R$,我们因此有一个两参数类的预算可行变化 $v$,可以用来计算替代消费路径,然后评估它们的福利。 -Now let's compute and plot consumption path variations +现在让我们计算并绘制消费路径变化。 ```{code-cell} ipython3 def compute_variation(model, ξ1, ϕ, a0, y_seq, verbose=1): @@ -546,7 +480,7 @@ def compute_variation(model, ξ1, ϕ, a0, y_seq, verbose=1): v_seq = np.array([(ξ1*ϕ**t - ξ0) for t in range(T+1)]) if verbose == 1: - print('check feasible:', np.isclose(β_seq @ v_seq, 0)) # since β = 1/R + print('检查:', np.isclose(β_seq @ v_seq, 0)) # since β = 1/R c_opt, _, _ = compute_optimal(model, a0, y_seq) cvar_seq = c_opt + v_seq @@ -554,8 +488,7 @@ def compute_variation(model, ξ1, ϕ, a0, y_seq, verbose=1): return cvar_seq ``` - -We visualize variations for $\xi_1 \in \{.01, .05\}$ and $\phi \in \{.95, 1.02\}$ +我们绘制$\xi_1 \in \{.01, .05\}$ and $\phi \in \{.95, 1.02\}$的变化 ```{code-cell} ipython3 fig, ax = plt.subplots() @@ -568,11 +501,11 @@ params = np.array(np.meshgrid(ξ1s, ϕs)).T.reshape(-1, 2) for i, param in enumerate(params): ξ1, ϕ = param - print(f'variation {i}: ξ1={ξ1}, ϕ={ϕ}') + print(f'变化 {i}: ξ1={ξ1}, ϕ={ϕ}') cvar_seq = compute_variation(model=cs_model, ξ1=ξ1, ϕ=ϕ, a0=a0, y_seq=y_seq) - print(f'welfare={welfare(cs_model, cvar_seq)}') + print(f'福利={welfare(cs_model, cvar_seq)}') print('-'*64) if i % 2 == 0: ls = '-.' @@ -583,7 +516,7 @@ for i, param in enumerate(params): label=fr'$\xi_1 = {ξ1}, \phi = {ϕ}$') plt.plot(range(T+1), c_seq, - color='orange', label=r'Optimal $\vec{c}$ ') + color='orange', label=r'最优 $\vec{c}$ ') plt.legend() plt.xlabel(r'$t$') @@ -591,19 +524,17 @@ plt.ylabel(r'$c_t$') plt.show() ``` - -We can even use the Python `np.gradient` command to compute derivatives of welfare with respect to our two parameters. - -We are teaching the key idea beneath the **calculus of variations**. - -First, we define the welfare with respect to $\xi_1$ and $\phi$ +我们甚至可以使用 Python 的 `np.gradient` 命令来计算福利对我们两个参数的导数。 +我们正在教授**变分法**背后的关键思想。 +首先,我们定义关于 $\xi_1$ 和 $\phi$ 的福利函数 ```{code-cell} ipython3 def welfare_rel(ξ1, ϕ): """ - Compute welfare of variation sequence - for given ϕ, ξ1 with a consumption-smoothing model + 计算给定 ϕ 和 ξ1 的变分序列的福利 + 使用消费平滑模型 """ + cvar_seq = compute_variation(cs_model, ξ1=ξ1, ϕ=ϕ, a0=a0, @@ -611,85 +542,76 @@ def welfare_rel(ξ1, ϕ): verbose=0) return welfare(cs_model, cvar_seq) -# Vectorize the function to allow array input +#向量化函数以允许数组输入 welfare_vec = np.vectorize(welfare_rel) ``` +现在我们可以将福利和$\xi_1$的关系可视化并且计算其导数 -Then we can visualize the relationship between welfare and $\xi_1$ and compute its derivatives ```{code-cell} ipython3 ξ1_arr = np.linspace(-0.5, 0.5, 20) plt.plot(ξ1_arr, welfare_vec(ξ1_arr, 1.02)) -plt.ylabel('welfare') +plt.ylabel('福利') plt.xlabel(r'$\xi_1$') plt.show() welfare_grad = welfare_vec(ξ1_arr, 1.02) welfare_grad = np.gradient(welfare_grad) plt.plot(ξ1_arr, welfare_grad) -plt.ylabel('derivative of welfare') +plt.ylabel('福利的导数') plt.xlabel(r'$\xi_1$') plt.show() ``` - - -The same can be done on $\phi$ +同理,对$\phi$ ```{code-cell} ipython3 ϕ_arr = np.linspace(-0.5, 0.5, 20) plt.plot(ξ1_arr, welfare_vec(0.05, ϕ_arr)) -plt.ylabel('welfare') +plt.ylabel('福利') plt.xlabel(r'$\phi$') plt.show() welfare_grad = welfare_vec(0.05, ϕ_arr) welfare_grad = np.gradient(welfare_grad) plt.plot(ξ1_arr, welfare_grad) -plt.ylabel('derivative of welfare') +plt.ylabel('福利的导数') plt.xlabel(r'$\phi$') plt.show() ``` +## 消费平滑模型总结 -## Wrapping up the consumption-smoothing model - -The consumption-smoothing model of Milton Friedman {cite}`Friedman1956` and Robert Hall {cite}`Hall1978`) is a cornerstone of modern macro that has important ramifications for the size of the Keynesian "fiscal policy multiplier" described briefly in -QuantEcon lecture {doc}`geometric series `. +米尔顿·弗里德曼 {cite}`Friedman1956` 和罗伯特·霍尔 {cite}`Hall1978` 的消费平滑模型是现代宏观经济学的基石,对QuantEcon讲座 {doc}`几何级数 ` 中简要描述的凯恩斯"财政政策乘数"的大小有重要影响。 -In particular, it **lowers** the government expenditure multiplier relative to one implied by -the original Keynesian consumption function presented in {doc}`geometric series `. +特别是,相对于 {doc}`几何级数 ` 中提出的原始凯恩斯消费函数所暗示的乘数,它**降低**了政府支出乘数。 -Friedman's work opened the door to an enlightening literature on the aggregate consumption function and associated government expenditure multipliers that -remains active today. +弗里德曼的工作为研究总消费函数和相关政府支出乘数开辟了一个富有启发性的领域,这一领域至今仍然活跃。 +## 附录:用线性代数解差分方程 -## Appendix: solving difference equations with linear algebra +在前面的章节中,我们使用线性代数来解决消费平滑模型。 -In the preceding sections we have used linear algebra to solve a consumption-smoothing model. +同样的线性代数工具——矩阵乘法和矩阵求逆——可以用来研究许多其他动态模型。 -The same tools from linear algebra -- matrix multiplication and matrix inversion -- can be used to study many other dynamic models. +我们将通过给出几个例子来结束本讲座。 -We'll conclude this lecture by giving a couple of examples. +我们将描述一种表示和"求解"线性差分方程的有用方法。 -We'll describe a useful way of representing and "solving" linear difference equations. +为了生成一些 $y$ 向量,我们只需写下一个具有适当初始条件的线性差分方程,然后使用线性代数来求解。 -To generate some $y$ vectors, we'll just write down a linear difference equation -with appropriate initial conditions and then use linear algebra to solve it. +### 一阶差分方程 -### First-order difference equation - -We'll start with a first-order linear difference equation for $\{y_t\}_{t=0}^T$: +我们将从 $\{y_t\}_{t=0}^T$ 的一阶线性差分方程开始: $$ y_{t} = \lambda y_{t-1}, \quad t = 1, 2, \ldots, T $$ -where $y_0$ is a given initial condition. - +其中 $y_0$ 是给定的初始条件。 -We can cast this set of $T$ equations as a single matrix equation +我们可以将这组 $T$ 个方程表示为单个矩阵方程 $$ \begin{bmatrix} @@ -708,8 +630,7 @@ y_1 \cr y_2 \cr y_3 \cr \vdots \cr y_T \end{bmatrix} $$ (eq:first_order_lin_diff) - -Multiplying both sides of {eq}`eq:first_order_lin_diff` by the inverse of the matrix on the left provides the solution +将 {eq}`eq:first_order_lin_diff` 两边乘以左侧矩阵的逆矩阵,得到解 ```{math} :label: fst_ord_inverse @@ -733,8 +654,7 @@ y_1 \cr y_2 \cr y_3 \cr \vdots \cr y_T ```{exercise} :label: consmooth_ex1 -To get {eq}`fst_ord_inverse`, we multiplied both sides of {eq}`eq:first_order_lin_diff` by the inverse of the matrix $A$. Please confirm that - +为了得到 {eq}`fst_ord_inverse`,我们将 {eq}`eq:first_order_lin_diff` 的两边都乘以矩阵 $A$ 的逆矩阵。请确认 $$ \begin{bmatrix} 1 & 0 & 0 & \cdots & 0 & 0 \cr @@ -744,22 +664,20 @@ $$ \lambda^{T-1} & \lambda^{T-2} & \lambda^{T-3} & \cdots & \lambda & 1 \end{bmatrix} $$ - -is the inverse of $A$ and check that $A A^{-1} = I$ +是 $A$ 的逆矩阵,并检查 $A A^{-1} = I$ 是否成立。 ``` +### 二阶差分方程 -### Second-order difference equation - -A second-order linear difference equation for $\{y_t\}_{t=0}^T$ is +对于序列 $\{y_t\}_{t=0}^T$,二阶线性差分方程为: $$ y_{t} = \lambda_1 y_{t-1} + \lambda_2 y_{t-2}, \quad t = 1, 2, \ldots, T $$ -where now $y_0$ and $y_{-1}$ are two given initial equations determined outside the model. +其中 $y_0$ 和 $y_{-1}$ 是在模型外部确定的两个给定初始条件。 -As we did with the first-order difference equation, we can cast this set of $T$ equations as a single matrix equation +与一阶差分方程类似,我们可以将这 $T$ 个方程表示为单个矩阵方程: $$ \begin{bmatrix} @@ -778,11 +696,62 @@ y_1 \cr y_2 \cr y_3 \cr \vdots \cr y_T \end{bmatrix} $$ -Multiplying both sides by inverse of the matrix on the left again provides the solution. +再次对等式两边乘以左侧矩阵的逆矩阵即可得到解。 ```{exercise} :label: consmooth_ex2 -As an exercise, we ask you to represent and solve a **third-order linear difference equation**. -How many initial conditions must you specify? +作为练习,我们要求你表示并求解一个**三阶线性差分方程**。 +你必须指定多少个初始条件? ``` + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/lectures/eigen_I.md b/lectures/eigen_I.md index 948b2f0..6e9f925 100644 --- a/lectures/eigen_I.md +++ b/lectures/eigen_I.md @@ -14,29 +14,24 @@ kernelspec: +++ {"user_expressions": []} (eigen)= -# Eigenvalues and Eigenvectors +# 特征值和特征向量 ```{index} single: Eigenvalues and Eigenvectors ``` -## Overview +## 概述 -Eigenvalues and eigenvectors are a relatively advanced topic in linear algebra. +特征值和特征向量是线性代数中一个相对高级的话题。 +同时,这些概念在以下领域非常有用: +* 经济建模(尤其是动态模型!) +* 统计学 +* 应用数学的某些部分 +* 机器学习 +* 以及许多其他科学领域 -At the same time, these concepts are extremely useful for - -* economic modeling (especially dynamics!) -* statistics -* some parts of applied mathematics -* machine learning -* and many other fields of science. - -In this lecture we explain the basics of eigenvalues and eigenvectors and introduce the Neumann Series Lemma. - -We assume in this lecture that students are familiar with matrices - and understand {doc}`the basics of matrix algebra`. - -We will use the following imports: +在本讲座中,我们将解释特征值和特征向量的基础知识,并介绍诺伊曼级数引理。 +我们假设学生已经熟悉矩阵,并理解{doc}`矩阵代数的基础知识`。 +我们将使用以下导入: ```{code-cell} ipython3 import matplotlib.pyplot as plt @@ -48,48 +43,39 @@ from mpl_toolkits.mplot3d import proj3d ``` (matrices_as_transformation)= -## Matrices as transformations - -Let's start by discussing an important concept concerning matrices. +## 矩阵作为变换 -### Mapping vectors to vectors +让我们从讨论一个关于矩阵的重要概念开始。 -One way to think about a matrix is as a rectangular collection of -numbers. +### 将向量映射到向量 -Another way to think about a matrix is as a *map* (i.e., as a function) that -transforms vectors to new vectors. +有两种思考矩阵的方式: +1. 将矩阵视为一个矩形的数字集合。 +2. 将矩阵视为一个将向量转换为新向量的*映射*(即函数)。 -To understand the second point of view, suppose we multiply an $n \times m$ -matrix $A$ with an $m \times 1$ column vector $x$ to obtain an $n \times 1$ -column vector $y$: +为了理解第二种观点,假设我们将一个 $n \times m$ 矩阵 $A$ 与一个 $m \times 1$ 列向量 $x$ 相乘,得到一个 $n \times 1$ 列向量 $y$: $$ Ax = y $$ -If we fix $A$ and consider different choices of $x$, we can understand $A$ as -a map transforming $x$ to $Ax$. +如果我们固定 $A$ 并考虑不同的 $x$,我们可以将 $A$ 理解为一个将 $x$ 转换为 $Ax$ 的映射。 -Because $A$ is $n \times m$, it transforms $m$-vectors to $n$-vectors. +因为 $A$ 是 $n \times m$ 的,所以它将 $m$ 维向量转换为 $n$ 维向量。 -We can write this formally as $A \colon \mathbb{R}^m \rightarrow \mathbb{R}^n$. +我们可以正式地将此写作 $A \colon \mathbb{R}^m \rightarrow \mathbb{R}^n$。 -You might argue that if $A$ is a function then we should write -$A(x) = y$ rather than $Ax = y$ but the second notation is more conventional. +你可能会说,如果 $A$ 是一个函数,那么我们应该写成 $A(x) = y$ 而不是 $Ax = y$,但后者的表示方法更为常见。 -### Square matrices +### 方阵 -Let's restrict our discussion to square matrices. +让我们将讨论限制在方阵上。 -In the above discussion, this means that $m=n$ and $A$ maps $\mathbb R^n$ to -itself. +在上述讨论中,这意味着 $m=n$,且 $A$ 将 $\mathbb R^n$ 映射到自身。 -This means $A$ is an $n \times n$ matrix that maps (or "transforms") a vector -$x$ in $\mathbb{R}^n$ to a new vector $y=Ax$ also in $\mathbb{R}^n$. +这表示 $A$ 是一个 $n \times n$ 矩阵,它将 $\mathbb{R}^n$ 中的向量 $x$ 映射(或"变换")为同样在 $\mathbb{R}^n$ 中的新向量 $y=Ax$。 -```{prf:example} -:label: eigen1_ex_sq +这里有一个例子: $$ \begin{bmatrix} @@ -107,7 +93,7 @@ $$ \end{bmatrix} $$ -Here, the matrix +在这里,矩阵 $$ A = \begin{bmatrix} 2 & 1 \\ @@ -115,11 +101,10 @@ $$ \end{bmatrix} $$ -transforms the vector $x = \begin{bmatrix} 1 \\ 3 \end{bmatrix}$ to the vector -$y = \begin{bmatrix} 5 \\ 2 \end{bmatrix}$. -``` +将向量 $x = \begin{bmatrix} 1 \\ 3 \end{bmatrix}$ 变换为向量 $y = \begin{bmatrix} 5 \\ 2 \end{bmatrix}$。 + +让我们用 Python 来可视化这个过程: -Let's visualize this using Python: ```{code-cell} ipython3 A = np.array([[2, 1], @@ -130,7 +115,7 @@ A = np.array([[2, 1], from math import sqrt fig, ax = plt.subplots() -# Set the axes through the origin +# 将坐标轴设置通过原点 for spine in ['left', 'bottom']: ax.spines[spine].set_position('zero') @@ -168,28 +153,18 @@ plt.show() +++ {"user_expressions": []} -One way to understand this transformation is that $A$ - -* first rotates $x$ by some angle $\theta$ and -* then scales it by some scalar $\gamma$ to obtain the image $y$ of $x$. - - - -## Types of transformations - -Let's examine some standard transformations we can perform with matrices. +理解这种变换的一种方式是 $A$ +* 首先将 $x$ 旋转某个角度 $\theta$,然后 +* 将其缩放某个标量 $\gamma$ 以获得 $x$ 的像 $y$。 -Below we visualize transformations by thinking of vectors as points -instead of arrows. - -We consider how a given matrix transforms - -* a grid of points and -* a set of points located on the unit circle in $\mathbb{R}^2$. - -To build the transformations we will use two functions, called `grid_transform` and `circle_transform`. - -Each of these functions visualizes the actions of a given $2 \times 2$ matrix $A$. +## 变换类型 +让我们来检查一些可以用矩阵执行的标准变换。 +下面我们通过将向量视为点而不是箭头来可视化变换。 +我们考虑给定矩阵如何变换 +* 一个点网格和 +* 位于 $\mathbb{R}^2$ 中单位圆上的一组点。 +为了构建这些变换,我们将使用两个函数,称为 `grid_transform` 和 `circle_transform`。 +这些函数中的每一个都可视化给定 $2 \times 2$ 矩阵 $A$ 的作用。 ```{code-cell} ipython3 :tags: [hide-input] @@ -220,13 +195,13 @@ def grid_transform(A=np.array([[1, -1], [1, 1]])): for spine in ['right', 'top']: axes.spines[spine].set_color('none') - # Plot x-y grid points + # 绘制x-y格点 ax[0].scatter(xygrid[0], xygrid[1], s=36, c=colors, edgecolor="none") # ax[0].grid(True) # ax[0].axis("equal") ax[0].set_title("points $x_1, x_2, \cdots, x_k$") - # Plot transformed grid points + # 绘制变换的格点 ax[1].scatter(uvgrid[0], uvgrid[1], s=36, c=colors, edgecolor="none") # ax[1].grid(True) # ax[1].axis("equal") @@ -265,7 +240,7 @@ def circle_transform(A=np.array([[-1, 2], [0, 1]])): ax[0].plot(x, y, color='black', zorder=1) ax[0].scatter(a_1, b_1, c=colors, alpha=1, s=60, edgecolors='black', zorder=2) - ax[0].set_title("unit circle in $\mathbb{R}^2$") + ax[0].set_title("在 $\mathbb{R}^2$的单位圆") x1 = x.reshape(1, -1) y1 = y.reshape(1, -1) @@ -277,31 +252,26 @@ def circle_transform(A=np.array([[-1, 2], [0, 1]])): transformed_circle[1, :], color='black', zorder=1) ax[1].scatter(transformed_ab[0, :], transformed_ab[1:,], color=colors, alpha=1, s=60, edgecolors='black', zorder=2) - ax[1].set_title("transformed circle") + ax[1].set_title("变换后的圆") plt.show() ``` +++ {"user_expressions": []} -### Scaling - -A matrix of the form - +### 缩放 +形如 $$ \begin{bmatrix} \alpha & 0 \\ 0 & \beta \end{bmatrix} $$ - -scales vectors across the x-axis by a factor $\alpha$ and along the y-axis by -a factor $\beta$. - -Here we illustrate a simple example where $\alpha = \beta = 3$. +的矩阵沿 x 轴将向量缩放 $\alpha$ 倍,沿 y 轴缩放 $\beta$ 倍。 +这里我们举一个简单的例子,其中 $\alpha = \beta = 3$。 ```{code-cell} ipython3 -A = np.array([[3, 0], # scaling by 3 in both directions +A = np.array([[3, 0], # 在两个方向放大三倍 [0, 3]]) grid_transform(A) circle_transform(A) @@ -309,22 +279,18 @@ circle_transform(A) +++ {"user_expressions": []} -### Shearing - -A "shear" matrix of the form - +### 剪切 +形如 $$ \begin{bmatrix} 1 & \lambda \\ 0 & 1 \end{bmatrix} $$ - -stretches vectors along the x-axis by an amount proportional to the -y-coordinate of a point. +的"剪切"矩阵沿 x 轴拉伸向量,拉伸量与点的 y 坐标成比例。 ```{code-cell} ipython3 -A = np.array([[1, 2], # shear along x-axis +A = np.array([[1, 2], # 沿x-轴进行剪切 [0, 1]]) grid_transform(A) circle_transform(A) @@ -332,22 +298,19 @@ circle_transform(A) +++ {"user_expressions": []} -### Rotation - -A matrix of the form - +### 旋转 +形如 $$ \begin{bmatrix} \cos \theta & \sin \theta \\ - \sin \theta & \cos \theta \end{bmatrix} $$ -is called a _rotation matrix_. - -This matrix rotates vectors clockwise by an angle $\theta$. +的矩阵被称为*旋转矩阵*。 +这个矩阵将向量顺时针旋转角度 $\theta$。 ```{code-cell} ipython3 -θ = np.pi/4 # 45 degree clockwise rotation +θ = np.pi/4 # 顺时针旋转45度 A = np.array([[np.cos(θ), np.sin(θ)], [-np.sin(θ), np.cos(θ)]]) grid_transform(A) @@ -355,17 +318,15 @@ grid_transform(A) +++ {"user_expressions": []} -### Permutation - -The permutation matrix - +### 置换 +置换矩阵 $$ \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix} $$ -interchanges the coordinates of a vector. +交换向量的坐标。 ```{code-cell} ipython3 A = np.column_stack([[0, 1], [1, 0]]) @@ -374,34 +335,29 @@ grid_transform(A) +++ {"user_expressions": []} -More examples of common transition matrices can be found [here](https://en.wikipedia.org/wiki/Transformation_matrix#Examples_in_2_dimensions). - -## Matrix multiplication as composition - -Since matrices act as functions that transform one vector to another, we can -apply the concept of function composition to matrices as well. +更多常见的变换矩阵示例可以在[这里](https://en.wikipedia.org/wiki/Transformation_matrix#Examples_in_2_dimensions)找到。 +## 矩阵乘法作为组合 -### Linear compositions +由于矩阵作为将一个向量转换为另一个向量的函数,我们也可以将函数组合的概念应用于矩阵。 -Consider the two matrices +### 线性组合 +考虑两个矩阵 $$ A = \begin{bmatrix} 0 & 1 \\ -1 & 0 \end{bmatrix} - \quad \text{and} \quad + \quad \text{和} \quad B = \begin{bmatrix} 1 & 2 \\ 0 & 1 \end{bmatrix} $$ - -What will the output be when we try to obtain $ABx$ for some $2 \times 1$ -vector $x$? +当我们尝试对某个 $2 \times 1$ 向量 $x$ 求 $ABx$ 时,输出会是什么? $$ \color{red}{\underbrace{ @@ -485,36 +441,23 @@ $$ }^{\textstyle y}} $$ -We can observe that applying the transformation $AB$ on the vector $x$ is the -same as first applying $B$ on $x$ and then applying $A$ on the vector $Bx$. - -Thus the matrix product $AB$ is the -[composition](https://en.wikipedia.org/wiki/Function_composition) of the -matrix transformations $A$ and $B$ - -This means first apply transformation $B$ and then -transformation $A$. +我们可以观察到,对向量 $x$ 应用变换 $AB$ 与先对 $x$ 应用 $B$,然后对向量 $Bx$ 应用 $A$ 是相同的。 -When we matrix multiply an $n \times m$ matrix $A$ with an $m \times k$ matrix -$B$ the obtained matrix product is an $n \times k$ matrix $AB$. +因此,矩阵乘积 $AB$ 是矩阵变换 $A$ 和 $B$ 的[组合](https://en.wikipedia.org/wiki/Function_composition)。 +这意味着先应用变换 $B$,然后应用变换 $A$。 -Thus, if $A$ and $B$ are transformations such that $A \colon \mathbb{R}^m \to -\mathbb{R}^n$ and $B \colon \mathbb{R}^k \to \mathbb{R}^m$, then $AB$ -transforms $\mathbb{R}^k$ to $\mathbb{R}^n$. +当我们将一个 $n \times m$ 矩阵 $A$ 与一个 $m \times k$ 矩阵 $B$ 相乘时,得到的矩阵乘积是一个 $n \times k$ 矩阵 $AB$。 -Viewing matrix multiplication as composition of maps helps us -understand why, under matrix multiplication, $AB$ is generally not equal to $BA$. +因此,如果 $A$ 和 $B$ 是变换,使得 $A \colon \mathbb{R}^m \to \mathbb{R}^n$ 且 $B \colon \mathbb{R}^k \to \mathbb{R}^m$,那么 $AB$ 将 $\mathbb{R}^k$ 变换到 $\mathbb{R}^n$。 -(After all, when we compose functions, the order usually matters.) +将矩阵乘法视为映射的组合有助于我们理解为什么在矩阵乘法下,$AB$ 通常不等于 $BA$。 +(毕竟,当我们组合函数时,顺序通常很重要。) -### Examples +### 示例 -Let $A$ be the $90^{\circ}$ clockwise rotation matrix given by -$\begin{bmatrix} 0 & 1 \\ -1 & 0 \end{bmatrix}$ and let $B$ be a shear matrix -along the x-axis given by $\begin{bmatrix} 1 & 2 \\ 0 & 1 \end{bmatrix}$. +设 $A$ 为顺时针旋转 $90^{\circ}$ 的矩阵,由 $\begin{bmatrix} 0 & 1 \\ -1 & 0 \end{bmatrix}$ 给出,设 $B$ 为沿 x 轴的剪切矩阵,由 $\begin{bmatrix} 1 & 2 \\ 0 & 1 \end{bmatrix}$ 给出。 -We will visualize how a grid of points changes when we apply the -transformation $AB$ and then compare it with the transformation $BA$. +我们将可视化当我们应用变换 $AB$ 时点的网格如何变化,然后将其与变换 $BA$ 进行比较。 ```{code-cell} ipython3 :tags: [hide-input] @@ -540,15 +483,15 @@ def grid_composition_transform(A=np.array([[1, -1], [1, 1]]), for spine in ['right', 'top']: axes.spines[spine].set_color('none') - # Plot grid points + # 绘制格点 ax[0].scatter(xygrid[0], xygrid[1], s=36, c=colors, edgecolor="none") ax[0].set_title("points $x_1, x_2, \cdots, x_k$") - # Plot intermediate grid points + # 绘制中间的格点 ax[1].scatter(uvgrid[0], uvgrid[1], s=36, c=colors, edgecolor="none") ax[1].set_title("points $Bx_1, Bx_2, \cdots, Bx_k$") - # Plot transformed grid points + # 绘制变换后的格点 ax[2].scatter(abgrid[0], abgrid[1], s=36, c=colors, edgecolor="none") ax[2].set_title("points $ABx_1, ABx_2, \cdots, ABx_k$") @@ -556,39 +499,37 @@ def grid_composition_transform(A=np.array([[1, -1], [1, 1]]), ``` ```{code-cell} ipython3 -A = np.array([[0, 1], # 90 degree clockwise rotation +A = np.array([[0, 1], # 顺时针旋转90度 [-1, 0]]) -B = np.array([[1, 2], # shear along x-axis +B = np.array([[1, 2], # 沿x-轴剪切 [0, 1]]) ``` +++ {"user_expressions": []} -#### Shear then rotate +#### 剪切后旋转 ```{code-cell} ipython3 -grid_composition_transform(A, B) # transformation AB +grid_composition_transform(A, B) # 变换 AB ``` +++ {"user_expressions": []} -#### Rotate then shear +#### 旋转后剪切 ```{code-cell} ipython3 -grid_composition_transform(B,A) # transformation BA +grid_composition_transform(B,A) # 变换 BA ``` +++ {"user_expressions": []} -It is evident that the transformation $AB$ is not the same as the transformation $BA$. +很显然,变换 $AB$ 与变换 $BA$ 是不同的。 -## Iterating on a fixed map +## 对固定映射进行迭代 -In economics (and especially in dynamic modeling), we are often interested in -analyzing behavior where we repeatedly apply a fixed matrix. +在经济学(尤其是动态建模)中,我们经常对重复应用固定矩阵的行为感兴趣。 -For example, given a vector $v$ and a matrix $A$, we are interested in -studying the sequence +例如,给定一个向量 $v$ 和一个矩阵 $A$,我们对研究以下序列感兴趣: $$ v, \quad @@ -596,8 +537,7 @@ $$ AAv = A^2v, \quad \ldots $$ -Let's first see examples of a sequence of iterates $(A^k v)_{k \geq 0}$ under -different maps $A$. +让我们首先看一下在不同映射 $A$ 下的迭代序列 $(A^k v)_{k \geq 0}$ 的例子。 (plot_series)= @@ -629,7 +569,7 @@ def plot_series(A, v, n): ax.plot(ellipse[0, :], ellipse[1, :], color='black', linestyle=(0, (5, 10)), linewidth=0.5) - # Initialize holder for trajectories + #初始化轨迹容器 colors = plt.cm.rainbow(np.linspace(0, 1, 20)) for i in range(n): @@ -658,9 +598,8 @@ plot_series(A, v, n) +++ {"user_expressions": []} -Here with each iteration the vectors get shorter, i.e., move closer to the origin. - -In this case, repeatedly multiplying a vector by $A$ makes the vector "spiral in". +每次迭代后,向量变得更短,即更靠近原点。 +在这种情况下,重复将向量乘以$A$会使向量"螺旋式地向内"。 ```{code-cell} ipython3 B = np.array([[sqrt(3) + 1, -2], @@ -674,10 +613,8 @@ plot_series(B, v, n) +++ {"user_expressions": []} -Here with each iteration vectors do not tend to get longer or shorter. - -In this case, repeatedly multiplying a vector by $A$ simply "rotates it around -an ellipse". +在这里,每次迭代向量不会变长或变短。 +在这种情况下,重复将向量乘以$A$只会使其"围绕一个椭圆旋转"。 ```{code-cell} ipython3 B = np.array([[sqrt(3) + 1, -2], @@ -691,43 +628,33 @@ plot_series(B, v, n) +++ {"user_expressions": []} -Here with each iteration vectors tend to get longer, i.e., farther from the -origin. - -In this case, repeatedly multiplying a vector by $A$ makes the vector "spiral out". +在这里,每次迭代向量趋向于变长,即离原点更远。 +在这种情况下,重复将向量乘以$A$会使向量"螺旋式地向外"。 +因此,我们观察到序列$(A^kv)_{k \geq 0}$的行为取决于映射$A$本身。 +现在我们讨论决定这种行为的$A$的性质。 -We thus observe that the sequence $(A^kv)_{k \geq 0}$ behaves differently depending on the map $A$ itself. - -We now discuss the property of A that determines this behavior. (la_eigenvalues)= -## Eigenvalues + +## 特征值 ```{index} single: Linear Algebra; Eigenvalues ``` +在本节中,我们引入特征值和特征向量的概念。 -In this section we introduce the notions of eigenvalues and eigenvectors. - -### Definitions - -Let $A$ be an $n \times n$ square matrix. - -If $\lambda$ is scalar and $v$ is a non-zero $n$-vector such that +### 定义 +设$A$为$n \times n$的方阵。 +如果存在标量$\lambda$和非零$n$维向量$v$,使得 $$ A v = \lambda v. $$ +则我们称$\lambda$为$A$的*特征值*,$v$为相应的*特征向量*。 +因此,$A$的特征向量是一个非零向量$v$,当映射$A$应用于它时,$v$仅仅被缩放。 -Then we say that $\lambda$ is an *eigenvalue* of $A$, and $v$ is the corresponding *eigenvector*. - -Thus, an eigenvector of $A$ is a nonzero vector $v$ such that when the map $A$ is -applied, $v$ is merely scaled. - -The next figure shows two eigenvectors (blue arrows) and their images under -$A$ (red arrows). - -As expected, the image $Av$ of each $v$ is just a scaled version of the original +下图显示了两个特征向量(蓝色箭头)及其在$A$下的像(红色箭头)。 +如预期的那样,每个$v$的像$Av$只是原始向量的缩放版本。 ```{code-cell} ipython3 :tags: [output_scroll] @@ -741,7 +668,7 @@ evals, evecs = eig(A) evecs = evecs[:, 0], evecs[:, 1] fig, ax = plt.subplots(figsize=(10, 8)) -# Set the axes through the origin +# 将坐标轴设置为通过原点 for spine in ['left', 'bottom']: ax.spines[spine].set_position('zero') for spine in ['right', 'top']: @@ -752,7 +679,7 @@ xmin, xmax = -3, 3 ymin, ymax = -3, 3 ax.set(xlim=(xmin, xmax), ylim=(ymin, ymax)) -# Plot each eigenvector +# 绘制每个特征向量 for v in evecs: ax.annotate('', xy=v, xytext=(0, 0), arrowprops=dict(facecolor='blue', @@ -760,7 +687,7 @@ for v in evecs: alpha=0.6, width=0.5)) -# Plot the image of each eigenvector +# 绘制每个特征向量 for v in evecs: v = A @ v ax.annotate('', xy=v, xytext=(0, 0), @@ -769,7 +696,7 @@ for v in evecs: alpha=0.6, width=0.5)) -# Plot the lines they run through +# 绘制它们经过的直线 x = np.linspace(xmin, xmax, 3) for v in evecs: a = v[1] / v[0] @@ -780,54 +707,36 @@ plt.show() +++ {"user_expressions": []} -### Complex values - -So far our definition of eigenvalues and eigenvectors seems straightforward. - -There is one complication we haven't mentioned yet: - -When solving $Av = \lambda v$, +### 复数值 +到目前为止,我们对特征值和特征向量的定义似乎很直观。 +但还有一个我们尚未提到的复杂情况: +在求解 $Av = \lambda v$ 时, +* $\lambda$ 可以是复数,并且 +* $v$ 可以是一个包含 n 个复数的向量。 +我们将在下面看到一些例子。 + +### 一些数学细节 +我们为更高级的读者注明一些数学细节。 +(其他读者可以跳到下一节。) +特征值方程等价于 $(A - \lambda I) v = 0$。 +只有当 $A - \lambda I$ 的列线性相关时,这个方程才有非零解 $v$。 +这反过来等价于行列式为零。 +因此,要找到所有特征值,我们可以寻找使 $A - \lambda I$ 的行列式为零的 $\lambda$。 +这个问题可以表示为求解一个 $\lambda$ 的 n 次多项式的根。 +这进而意味着在复平面上存在 n 个解,尽管有些可能是重复的。 + +### 事实 +关于方阵 $A$ 的特征值,有一些很好的事实: +1. $A$ 的行列式等于其特征值的乘积 +2. $A$ 的迹(主对角线上元素的和)等于其特征值的和 +3. 如果 $A$ 是对称的,那么它的所有特征值都是实数 +4. 如果 $A$ 可逆,且 $\lambda_1, \ldots, \lambda_n$ 是它的特征值,那么 $A^{-1}$ 的特征值是 $1/\lambda_1, \ldots, 1/\lambda_n$。 + +最后一个陈述的一个推论是,当且仅当矩阵的所有特征值都非零时,该矩阵才是可逆的。 + +### 计算 +使用 NumPy,我们可以按如下方式求解矩阵的特征值和特征向量 -* $\lambda$ is allowed to be a complex number and -* $v$ is allowed to be an $n$-vector of complex numbers. - -We will see some examples below. - -### Some mathematical details - -We note some mathematical details for more advanced readers. - -(Other readers can skip to the next section.) - -The eigenvalue equation is equivalent to $(A - \lambda I) v = 0$. - -This equation has a nonzero solution $v$ only when the columns of $A - \lambda I$ are linearly dependent. - -This in turn is equivalent to stating the determinant is zero. - -Hence, to find all eigenvalues, we can look for $\lambda$ such that the -determinant of $A - \lambda I$ is zero. - -This problem can be expressed as one of solving for the roots of a polynomial -in $\lambda$ of degree $n$. - -This in turn implies the existence of $n$ solutions in the complex -plane, although some might be repeated. - -### Facts - -Some nice facts about the eigenvalues of a square matrix $A$ are as follows: - -1. the determinant of $A$ equals the product of the eigenvalues -2. the trace of $A$ (the sum of the elements on the principal diagonal) equals the sum of the eigenvalues -3. if $A$ is symmetric, then all of its eigenvalues are real -4. if $A$ is invertible and $\lambda_1, \ldots, \lambda_n$ are its eigenvalues, then the eigenvalues of $A^{-1}$ are $1/\lambda_1, \ldots, 1/\lambda_n$. - -A corollary of the last statement is that a matrix is invertible if and only if all its eigenvalues are nonzero. - -### Computation - -Using NumPy, we can solve for the eigenvalues and eigenvectors of a matrix as follows ```{code-cell} ipython3 from numpy.linalg import eig @@ -837,41 +746,32 @@ A = ((1, 2), A = np.array(A) evals, evecs = eig(A) -evals # eigenvalues +evals # 特征值 ``` ```{code-cell} ipython3 -evecs # eigenvectors +evecs # 特征向量 ``` +++ {"user_expressions": []} -Note that the *columns* of `evecs` are the eigenvectors. - -Since any scalar multiple of an eigenvector is an eigenvector with the same -eigenvalue (which can be verified), the `eig` routine normalizes the length of each eigenvector -to one. - -The eigenvectors and eigenvalues of a map $A$ determine how a vector $v$ is transformed when we repeatedly multiply by $A$. - -This is discussed further later. - +请注意,`evecs` 的*列*是特征向量。 +由于特征向量的任何标量倍数都是具有相同特征值的特征向量(这可以被验证),`eig` 程序将每个特征向量的长度归一化为1。 +映射 $A$ 的特征向量和特征值决定了当我们反复乘以 $A$ 时,向量 $v$ 如何被变换。 +这一点将在后面进一步讨论。 (la_neumann)= -## The Neumann Series Lemma +## 诺伊曼级数引理 ```{index} single: Neumann's Lemma ``` -In this section we present a famous result about series of matrices that has -many applications in economics. - -### Scalar series +在本节中,我们将介绍一个关于矩阵级数的著名结果,它在经济学中有许多应用。 -Here's a fundamental result about series: - -If $a$ is a number and $|a| < 1$, then +### 标量级数 +以下是关于级数的一个基本结果: +如果 $a$ 是一个数,且 $|a| < 1$,那么 ```{math} :label: gp_sum @@ -879,122 +779,102 @@ If $a$ is a number and $|a| < 1$, then ``` -For a one-dimensional linear equation $x = ax + b$ where x is unknown we can thus conclude that the solution $x^{*}$ is given by: - +对于一维线性方程 $x = ax + b$,其中 x 未知,我们可以得出解 $x^{*}$ 由以下给出: $$ x^{*} = \frac{b}{1-a} = \sum_{k=0}^{\infty} a^k b $$ -### Matrix series +### 矩阵级数 -A generalization of this idea exists in the matrix setting. - -Consider the system of equations $x = Ax + b$ where $A$ is an $n \times n$ -square matrix and $x$ and $b$ are both column vectors in $\mathbb{R}^n$. - -Using matrix algebra we can conclude that the solution to this system of equations will be given by: +这个想法在矩阵设置中也有一个推广。 +考虑方程组 $x = Ax + b$,其中 $A$ 是一个 $n \times n$ 的方阵,$x$ 和 $b$ 都是 $\mathbb{R}^n$ 中的列向量。 +使用矩阵代数,我们可以得出这个方程组的解由以下给出: ```{math} :label: neumann_eqn x^{*} = (I-A)^{-1}b - + ``` -What guarantees the existence of a unique vector $x^{*}$ that satisfies -{eq}`neumann_eqn`? - -The following is a fundamental result in functional analysis that generalizes -{eq}`gp_sum` to a multivariate case. +什么保证了存在唯一的向量 $x^{*}$ 满足方程 {eq}`neumann_eqn`? +以下是泛函分析中的一个基本结果,它将 {eq}`gp_sum` 推广到多变量情况。 (neumann_series_lemma)= ```{prf:Theorem} Neumann Series Lemma :label: neumann_series_lemma -Let $A$ be a square matrix and let $A^k$ be the $k$-th power of $A$. - -Let $r(A)$ be the **spectral radius** of $A$, defined as $\max_i |\lambda_i|$, where - -* $\{\lambda_i\}_i$ is the set of eigenvalues of $A$ and -* $|\lambda_i|$ is the modulus of the complex number $\lambda_i$ - -Neumann's Theorem states the following: If $r(A) < 1$, then $I - A$ is invertible, and +设 $A$ 为方阵,$A^k$ 为 $A$ 的 $k$ 次幂。 +设 $r(A)$ 为 $A$ 的**谱半径**,定义为 $\max_i |\lambda_i|$,其中 +* $\{\lambda_i\}_i$ 是 $A$ 的特征值集,且 +* $|\lambda_i|$ 是复数 $\lambda_i$ 的模 +诺伊曼定理陈述如下:如果 $r(A) < 1$,那么 $I - A$ 是可逆的,且 $$ (I - A)^{-1} = \sum_{k=0}^{\infty} A^k $$ ``` -We can see the Neumann Series Lemma in action in the following example. +我们可以在以下例子中看到诺伊曼级数引理的应用。 ```{code-cell} ipython3 A = np.array([[0.4, 0.1], [0.7, 0.2]]) -evals, evecs = eig(A) # finding eigenvalues and eigenvectors +evals, evecs = eig(A) #求出特征值和特征向量 -r = max(abs(λ) for λ in evals) # compute spectral radius +r = max(abs(λ) for λ in evals) # 计算谱半径 print(r) ``` - -The spectral radius $r(A)$ obtained is less than 1. - -Thus, we can apply the Neumann Series Lemma to find $(I-A)^{-1}$. +获得的谱半径 $r(A)$ 小于1。 +因此,我们可以应用诺伊曼级数引理来求 $(I-A)^{-1}$。 ```{code-cell} ipython3 -I = np.identity(2) # 2 x 2 identity matrix +I = np.identity(2) # 2 x 2 单位矩阵 B = I - A ``` ```{code-cell} ipython3 -B_inverse = np.linalg.inv(B) # direct inverse method +B_inverse = np.linalg.inv(B) # 直接求逆 ``` ```{code-cell} ipython3 -A_sum = np.zeros((2, 2)) # power series sum of A +A_sum = np.zeros((2, 2)) # A 的幂级数和 A_power = I for i in range(50): A_sum += A_power A_power = A_power @ A ``` +让我们检查求和方法和逆序方法的结果是否相等。 -Let's check equality between the sum and the inverse methods. ```{code-cell} ipython3 np.allclose(A_sum, B_inverse) ``` -Although we truncate the infinite sum at $k = 50$, both methods give us the same -result which illustrates the result of the Neumann Series Lemma. +虽然我们在 $k = 50$ 时截断了无限级数,但两种方法给出了相同的结果,这说明了诺伊曼级数引理的结论。 - -## Exercises +## 练习 ```{exercise} :label: eig1_ex1 -Power iteration is a method for finding the greatest absolute eigenvalue of a diagonalizable matrix. - -The method starts with a random vector $b_0$ and repeatedly applies the matrix $A$ to it - +幂迭代法是一种用于寻找可对角化矩阵最大绝对特征值的方法。 +该方法从一个随机向量 $b_0$ 开始,重复地对其应用矩阵 $A$ $$ b_{k+1}=\frac{A b_k}{\left\|A b_k\right\|} $$ - -A thorough discussion of the method can be found [here](https://pythonnumericalmethods.berkeley.edu/notebooks/chapter15.02-The-Power-Method.html). - -In this exercise, first implement the power iteration method and use it to find the greatest absolute eigenvalue and its corresponding eigenvector. - -Then visualize the convergence. +关于该方法的详细讨论可以在[这里](https://pythonnumericalmethods.berkeley.edu/notebooks/chapter15.02-The-Power-Method.html)找到。 +在这个练习中,首先实现幂迭代方法,并用它来找出最大绝对特征值及其对应的特征向量。 +然后可视化收敛过程。 ``` ```{solution-start} eig1_ex1 :class: dropdown ``` -Here is one solution. - -We start by looking into the distance between the eigenvector approximation and the true eigenvector. +这里有一个解决方案。 +我们首先研究特征向量近似值与真实特征向量之间的距离。 ```{code-cell} ipython3 --- @@ -1003,49 +883,49 @@ mystnb: caption: Power iteration name: pow-dist --- -# Define a matrix A +# 定义矩阵A A = np.array([[1, 0, 3], [0, 2, 0], [3, 0, 1]]) num_iters = 20 -# Define a random starting vector b +# 定义一个随机的初始向量 b b = np.random.rand(A.shape[1]) -# Get the leading eigenvector of matrix A +# 获取矩阵A的主特征向量 eigenvector = np.linalg.eig(A)[1][:, 0] errors = [] res = [] -# Power iteration loop +# 幂迭代循环 for i in range(num_iters): # Multiply b by A b = A @ b - # Normalize b + # 归一化b b = b / np.linalg.norm(b) - # Append b to the list of eigenvector approximations + # 将b添加到特征向量近似值列表中 res.append(b) err = np.linalg.norm(np.array(b) - eigenvector) errors.append(err) greatest_eigenvalue = np.dot(A @ b, b) / np.dot(b, b) -print(f'The approximated greatest absolute eigenvalue is \ +print(f'近似的最大绝对特征值是 \ {greatest_eigenvalue:.2f}') -print('The real eigenvalue is', np.linalg.eig(A)[0]) +print('真实的特征值是', np.linalg.eig(A)[0]) -# Plot the eigenvector approximations for each iteration +# 绘制每次迭代的特征向量近似值 plt.figure(figsize=(10, 6)) -plt.xlabel('iterations') -plt.ylabel('error') +plt.xlabel('次数') +plt.ylabel('误差') _ = plt.plot(errors) ``` +++ {"user_expressions": []} -Then we can look at the trajectory of the eigenvector approximation. +然后我们可以观察特征向量近似值的轨迹。 ```{code-cell} ipython3 --- @@ -1054,11 +934,11 @@ mystnb: caption: Power iteration trajectory name: pow-trajectory --- -# Set up the figure and axis for 3D plot +# 设置3D图形和坐标轴 fig = plt.figure() ax = fig.add_subplot(111, projection='3d') -# Plot the eigenvectors +# 绘制特征向量 ax.scatter(eigenvector[0], eigenvector[1], eigenvector[2], @@ -1077,8 +957,8 @@ ax.tick_params(axis='both', which='major', labelsize=7) points = [plt.Line2D([0], [0], linestyle='none', c=i, marker='o') for i in ['r', 'b']] -ax.legend(points, ['actual eigenvector', - r'approximated eigenvector ($b_k$)']) +ax.legend(points, ['真正的特征向量', + r'近似的特征向量 ($b_k$)']) ax.set_box_aspect(aspect=None, zoom=0.8) plt.show() @@ -1092,11 +972,9 @@ plt.show() ```{exercise} :label: eig1_ex2 -We have discussed the trajectory of the vector $v$ after being transformed by $A$. - -Consider the matrix $A = \begin{bmatrix} 1 & 2 \\ 1 & 1 \end{bmatrix}$ and the vector $v = \begin{bmatrix} 2 \\ -2 \end{bmatrix}$. - -Try to compute the trajectory of $v$ after being transformed by $A$ for $n=4$ iterations and plot the result. +我们已经讨论了向量 $v$ 经过矩阵 $A$ 变换后的轨迹。 +考虑矩阵 $A = \begin{bmatrix} 1 & 2 \\ 1 & 1 \end{bmatrix}$ 和向量 $v = \begin{bmatrix} 2 \\ -2 \end{bmatrix}$。 +尝试计算向量 $v$ 经过矩阵 $A$ 变换 $n=4$ 次迭代后的轨迹,并绘制结果。 ``` @@ -1110,22 +988,20 @@ A = np.array([[1, 2], v = (0.4, -0.4) n = 11 -# Compute eigenvectors and eigenvalues +# 计算特征值和特征向量 eigenvalues, eigenvectors = np.linalg.eig(A) -print(f'eigenvalues:\n {eigenvalues}') -print(f'eigenvectors:\n {eigenvectors}') +print(f'特征值:\n {eigenvalues}') +print(f'特征向量:\n {eigenvectors}') plot_series(A, v, n) ``` +++ {"user_expressions": []} -The result seems to converge to the eigenvector of $A$ with the largest eigenvalue. - -Let's use a [vector field](https://en.wikipedia.org/wiki/Vector_field) to visualize the transformation brought by A. - -(This is a more advanced topic in linear algebra, please step ahead if you are comfortable with the math.) +结果似乎收敛于矩阵 $A$ 最大特征值对应的特征向量。 +让我们使用[向量场](https://en.wikipedia.org/wiki/Vector_field)来可视化矩阵 $A$ 带来的变换。 +(这是线性代数中的一个较高级话题,如果你对数学感到足够自在,请继续往下学习。) ```{code-cell} ipython3 --- @@ -1134,21 +1010,21 @@ mystnb: caption: Convergence towards eigenvectors name: eigen-conv --- -# Create a grid of points +# 创建格点 x, y = np.meshgrid(np.linspace(-5, 5, 15), np.linspace(-5, 5, 20)) -# Apply the matrix A to each point in the vector field +#将矩阵A应用于向量场中的每个点 vec_field = np.stack([x, y]) u, v = np.tensordot(A, vec_field, axes=1) -# Plot the transformed vector field +# 绘制转换后的向量场 c = plt.streamplot(x, y, u - x, v - y, density=1, linewidth=None, color='#A23BEC') c.lines.set_alpha(0.5) c.arrows.set_alpha(0.5) -# Draw eigenvectors +# 绘制特征向量 origin = np.zeros((2, len(eigenvectors))) parameters = {'color': ['b', 'g'], 'angles': 'xy', 'scale_units': 'xy', 'scale': 0.1, 'width': 0.01} @@ -1172,13 +1048,13 @@ plt.show() +++ {"user_expressions": []} -Note that the vector field converges to the eigenvector of $A$ with the largest eigenvalue and diverges from the eigenvector of $A$ with the smallest eigenvalue. +请注意,向量场收敛于$A$的最大特征值对应的特征向量,并从$A$的最小特征值对应的特征向量发散。 -In fact, the eigenvectors are also the directions in which the matrix $A$ stretches or shrinks the space. +实际上,特征向量也是矩阵$A$拉伸或压缩空间的方向。 -Specifically, the eigenvector with the largest eigenvalue is the direction in which the matrix $A$ stretches the space the most. +具体来说,最大特征值对应的特征向量是矩阵$A$最大程度拉伸空间的方向。 -We will see more intriguing examples in the following exercise. +我们将在接下来的练习中看到更多有趣的例子。 ```{solution-end} ``` @@ -1186,9 +1062,8 @@ We will see more intriguing examples in the following exercise. ```{exercise} :label: eig1_ex3 -{ref}`Previously `, we demonstrated the trajectory of the vector $v$ after being transformed by $A$ for three different matrices. - -Use the visualization in the previous exercise to explain the trajectory of the vector $v$ after being transformed by $A$ for the three different matrices. +{ref}`之前 `,我们展示了向量$v$被三种不同矩阵$A$变换后的轨迹。 +使用前面练习中的可视化来解释向量$v$被这三种不同矩阵$A$变换后的轨迹。 ``` @@ -1196,8 +1071,7 @@ Use the visualization in the previous exercise to explain the trajectory of the ```{solution-start} eig1_ex3 :class: dropdown ``` - -Here is one solution +这里是一个答案。 ```{code-cell} ipython3 --- @@ -1224,30 +1098,30 @@ examples = [A, B, C] for i, example in enumerate(examples): M = example - # Compute right eigenvectors and eigenvalues + # 计算特征向量和特征值 eigenvalues, eigenvectors = np.linalg.eig(M) print(f'Example {i+1}:\n') - print(f'eigenvalues:\n {eigenvalues}') - print(f'eigenvectors:\n {eigenvectors}\n') + print(f'特征值:\n {eigenvalues}') + print(f'特征向量:\n {eigenvectors}\n') eigenvalues_real = eigenvalues.real eigenvectors_real = eigenvectors.real - # Create a grid of points + # 创建格点 x, y = np.meshgrid(np.linspace(-20, 20, 15), np.linspace(-20, 20, 20)) - # Apply the matrix A to each point in the vector field + # 将矩阵A应用于向量场中的每个点 vec_field = np.stack([x, y]) u, v = np.tensordot(M, vec_field, axes=1) - # Plot the transformed vector field + # 绘制转换后的向量场 c = ax[i].streamplot(x, y, u - x, v - y, density=1, linewidth=None, color='#A23BEC') c.lines.set_alpha(0.5) c.arrows.set_alpha(0.5) - # Draw eigenvectors + # 绘制特征向量 parameters = {'color': ['b', 'g'], 'angles': 'xy', 'scale_units': 'xy', 'scale': 1, 'width': 0.01, 'alpha': 0.5} @@ -1269,11 +1143,11 @@ plt.show() +++ {"user_expressions": []} -The vector fields explain why we observed the trajectories of the vector $v$ multiplied by $A$ iteratively before. +这些向量场解释了为什么我们之前观察到向量$v$被矩阵$A$反复相乘后的轨迹。 -The pattern demonstrated here is because we have complex eigenvalues and eigenvectors. +这里展示的模式是因为我们有复数特征值和特征向量。 -We can plot the complex plane for one of the matrices using `Arrow3D` class retrieved from [stackoverflow](https://stackoverflow.com/questions/22867620/putting-arrowheads-on-vectors-in-a-3d-plot). +我们可以使用从[stackoverflow](https://stackoverflow.com/questions/22867620/putting-arrowheads-on-vectors-in-a-3d-plot)获取的`Arrow3D`类来为其中一个矩阵绘制复平面。 ```{code-cell} ipython3 --- @@ -1299,17 +1173,17 @@ class Arrow3D(FancyArrowPatch): eigenvalues, eigenvectors = np.linalg.eig(A) -# Create meshgrid for vector field +#为向量场创建网格 x, y = np.meshgrid(np.linspace(-2, 2, 15), np.linspace(-2, 2, 15)) -# Calculate vector field (real and imaginary parts) +# 计算向量场(实部和虚部) u_real = A[0][0] * x + A[0][1] * y v_real = A[1][0] * x + A[1][1] * y u_imag = np.zeros_like(x) v_imag = np.zeros_like(y) -# Create 3D figure +# 创建3D图像 fig = plt.figure() ax = fig.add_subplot(111, projection='3d') vlength = np.linalg.norm(eigenvectors) @@ -1320,7 +1194,7 @@ ax.quiver(x, y, u_imag, u_real-x, v_real-y, v_imag-u_imag, arrow_prop_dict = dict(mutation_scale=5, arrowstyle='-|>', shrinkA=0, shrinkB=0) -# Plot 3D eigenvectors +# 绘制3D特征向量 for c, i in zip(['b', 'g'], [0, 1]): a = Arrow3D([0, eigenvectors[0][i].real], [0, eigenvectors[1][i].real], @@ -1328,7 +1202,7 @@ for c, i in zip(['b', 'g'], [0, 1]): color=c, **arrow_prop_dict) ax.add_artist(a) -# Set axis labels and title +# 设置坐标轴标签和标题 ax.set_xlabel('x') ax.set_ylabel('y') ax.set_zlabel('Im') @@ -1341,4 +1215,4 @@ plt.show() +++ {"user_expressions": []} ```{solution-end} -``` +``` \ No newline at end of file diff --git a/lectures/eigen_II.md b/lectures/eigen_II.md index 5ccd9eb..24e701a 100644 --- a/lectures/eigen_II.md +++ b/lectures/eigen_II.md @@ -11,12 +11,11 @@ kernelspec: name: python3 --- -# The Perron-Frobenius Theorem - +# 佩龙-弗罗贝尼乌斯定理 ```{index} single: The Perron-Frobenius Theorem ``` -In addition to what's in Anaconda, this lecture will need the following libraries: +# 除了Anaconda中已有的库之外,本讲座还需要以下库: ```{code-cell} ipython3 :tags: [hide-output] @@ -24,11 +23,11 @@ In addition to what's in Anaconda, this lecture will need the following librarie !pip install quantecon ``` -In this lecture we will begin with the foundational concepts in spectral theory. +在本讲座中,我们将从谱理论的基本概念开始。 -Then we will explore the Perron-Frobenius theorem and connect it to applications in Markov chains and networks. +然后,我们将探讨佩龙-弗罗贝尼乌斯定理,并将其与马尔可夫链和网络的应用联系起来。 -We will use the following imports: +我们将使用以下导入: ```{code-cell} ipython3 import numpy as np @@ -37,37 +36,27 @@ import scipy as sp import quantecon as qe ``` -## Nonnegative matrices - -Often, in economics, the matrix that we are dealing with is nonnegative. - -Nonnegative matrices have several special and useful properties. +## 非负矩阵 -In this section we will discuss some of them --- in particular, the connection -between nonnegativity and eigenvalues. +在经济学中,我们经常处理的矩阵是非负的。非负矩阵具有几个特殊且有用的性质。在本节中,我们将讨论其中的一些性质——特别是非负性与特征值之间的联系。 -An $n \times m$ matrix $A$ is called **nonnegative** if every element of $A$ -is nonnegative, i.e., $a_{ij} \geq 0$ for every $i,j$. - -We denote this as $A \geq 0$. +一个 $n \times m$ 的矩阵 $A$ 被称为**非负**,如果 $A$ 的每个元素都是非负的,即对于每个 $i,j$,都有 $a_{ij} \geq 0$。我们将此表示为 $A \geq 0$。 (irreducible)= -### Irreducible matrices - -We introduced irreducible matrices in the [Markov chain lecture](mc_irreducible). +### 不可约矩阵 -Here we generalize this concept: +我们在[马尔可夫链讲座](mc_irreducible)中介绍了不可约矩阵。这里我们将推广这个概念: -Let $a^{k}_{ij}$ be element $(i,j)$ of $A^k$. +令 $a^{k}_{ij}$ 为 $A^k$ 的第 $(i,j)$ 个元素。 -An $n \times n$ nonnegative matrix $A$ is called irreducible if $A + A^2 + A^3 + \cdots \gg 0$, where $\gg 0$ indicates that every element in $A$ is strictly positive. +一个 $n \times n$ 的非负矩阵 $A$ 被称为不可约的,如果 $A + A^2 + A^3 + \cdots \gg 0$,其中 $\gg 0$ 表示 $A$ 的每个元素都严格为正。 -In other words, for each $i,j$ with $1 \leq i, j \leq n$, there exists a $k \geq 0$ such that $a^{k}_{ij} > 0$. +换句话说,对于每个 $1 \leq i, j \leq n$,存在一个 $k \geq 0$ 使得 $a^{k}_{ij} > 0$。 ```{prf:example} :label: eigen2_ex_irr -Here are some examples to illustrate this further: +以下是一些进一步说明的例子: $$ A = \begin{bmatrix} 0.5 & 0.1 \\ @@ -75,7 +64,7 @@ A = \begin{bmatrix} 0.5 & 0.1 \\ \end{bmatrix} $$ -$A$ is irreducible since $a_{ij}>0$ for all $(i,j)$. +$A$ 是不可约的,因为对于所有的 $(i,j)$,$a_{ij}>0$。 $$ B = \begin{bmatrix} 0 & 1 \\ @@ -87,7 +76,7 @@ B^2 = \begin{bmatrix} 1 & 0 \\ \end{bmatrix} $$ -$B$ is irreducible since $B + B^2$ is a matrix of ones. +$B$ 是不可约的,因为 $B + B^2$ 是一个全为 1 的矩阵。 $$ C = \begin{bmatrix} 1 & 0 \\ @@ -95,106 +84,89 @@ C = \begin{bmatrix} 1 & 0 \\ \end{bmatrix} $$ -$C$ is not irreducible since $C^k = C$ for all $k \geq 0$ and thus - $c^{k}_{12},c^{k}_{21} = 0$ for all $k \geq 0$. -``` - -### Left eigenvectors +$C$ 不是不可约的,因为对于所有 $k \geq 0$,$C^k = C$,因此 +对于所有 $k \geq 0$,$c^{k}_{12},c^{k}_{21} = 0$。 -Recall that we previously discussed eigenvectors in {ref}`Eigenvalues and Eigenvectors `. +``` -In particular, $\lambda$ is an eigenvalue of $A$ and $v$ is an eigenvector of $A$ if $v$ is nonzero and satisfy +### 左特征向量 +回想一下,我们之前在 {ref}`特征值和特征向量 ` 中讨论过特征向量。 +特别地,如果 $v$ 是非零向量,且满足 $$ -Av = \lambda v. +Av = \lambda v $$ +那么 $\lambda$ 是 $A$ 的一个特征值,而 $v$ 是 $A$ 的一个特征向量。 -In this section we introduce left eigenvectors. - -To avoid confusion, what we previously referred to as "eigenvectors" will be called "right eigenvectors". +在本节中,我们将介绍左特征向量。 +为避免混淆,我们之前称为"特征向量"的将被称为"右特征向量"。 +左特征向量在接下来的内容中将扮演重要角色,包括在马尔可夫假设下动态模型的随机稳态。 -Left eigenvectors will play important roles in what follows, including that of stochastic steady states for dynamic models under a Markov assumption. - -A vector $w$ is called a left eigenvector of $A$ if $w$ is a right eigenvector of $A^\top$. - -In other words, if $w$ is a left eigenvector of matrix $A$, then $A^\top w = \lambda w$, where $\lambda$ is the eigenvalue associated with the left eigenvector $v$. - -This hints at how to compute left eigenvectors +如果 $w$ 是 $A^\top$ 的右特征向量,那么 $w$ 被称为 $A$ 的左特征向量。 +换句话说,如果 $w$ 是矩阵 $A$ 的左特征向量,那么 $A^\top w = \lambda w$,其中 $\lambda$ 是与左特征向量 $v$ 相关的特征值。 +这暗示了如何计算左特征向量。 ```{code-cell} ipython3 A = np.array([[3, 2], [1, 4]]) -# Compute eigenvalues and right eigenvectors +# 计算特征值和右特征向量 λ, v = eig(A) -# Compute eigenvalues and left eigenvectors +# 计算特征值和左特征向量 λ, w = eig(A.T) -# Keep 5 decimals +# 保留5位小数 np.set_printoptions(precision=5) -print(f"The eigenvalues of A are:\n {λ}\n") -print(f"The corresponding right eigenvectors are: \n {v[:,0]} and {-v[:,1]}\n") -print(f"The corresponding left eigenvectors are: \n {w[:,0]} and {-w[:,1]}\n") +print(f"A的特征值为:\n {λ}\n") +print(f"右特征向量为: \n {v[:,0]} and {-v[:,1]}\n") +print(f"左特征向量为: \n {w[:,0]} and {-w[:,1]}\n") ``` -We can also use `scipy.linalg.eig` with argument `left=True` to find left eigenvectors directly +我们还可以使用 `scipy.linalg.eig` 函数并设置参数 `left=True` 来直接找到左特征向量。 ```{code-cell} ipython3 eigenvals, ε, e = sp.linalg.eig(A, left=True) -print(f"The eigenvalues of A are:\n {eigenvals.real}\n") -print(f"The corresponding right eigenvectors are: \n {e[:,0]} and {-e[:,1]}\n") -print(f"The corresponding left eigenvectors are: \n {ε[:,0]} and {-ε[:,1]}\n") +print(f"A的特征值为:\n {eigenvals.real}\n") +print(f"右特征向量为: \n {e[:,0]} and {-e[:,1]}\n") +print(f"左特征向量为: \n {ε[:,0]} and {-ε[:,1]}\n") ``` -The eigenvalues are the same while the eigenvectors themselves are different. - -(Also note that we are taking the nonnegative value of the eigenvector of {ref}`dominant eigenvalue `, this is because `eig` automatically normalizes the eigenvectors.) - -We can then take transpose to obtain $A^\top w = \lambda w$ and obtain $w^\top A= \lambda w^\top$. - -This is a more common expression and where the name left eigenvectors originates. +特征值是相同的,而特征向量本身是不同的。 +(还要注意,我们取的是 {ref}`主特征值 ` 的特征向量的非负值,这是因为 `eig` 函数会自动对特征向量进行归一化。) +然后我们可以对 $A^\top w = \lambda w$ 进行转置,得到 $w^\top A= \lambda w^\top$。 +这是一个更常见的表达式,也是左特征向量这个名称的由来。 (perron-frobe)= -### The Perron-Frobenius theorem +### 佩龙-弗罗贝尼乌斯定理 -For a square nonnegative matrix $A$, the behavior of $A^k$ as $k \to \infty$ is controlled by the eigenvalue with the largest -absolute value, often called the **dominant eigenvalue**. +对于一个非负方阵$A$,当$k \to \infty$时,$A^k$的行为由绝对值最大的特征值控制,通常称为**主特征值**。 +对于任何这样的矩阵$A$,佩龙-弗罗贝尼乌斯定理描述了主特征值及其对应特征向量的某些特性。 -For any such matrix $A$, the Perron-Frobenius theorem characterizes certain -properties of the dominant eigenvalue and its corresponding eigenvector. - -```{prf:Theorem} Perron-Frobenius Theorem +```{prf:Theorem} 佩龙-弗罗贝尼乌斯定理 :label: perron-frobenius -If a matrix $A \geq 0$ then, - -1. the dominant eigenvalue of $A$, $r(A)$, is real-valued and nonnegative. -2. for any other eigenvalue (possibly complex) $\lambda$ of $A$, $|\lambda| \leq r(A)$. -3. we can find a nonnegative and nonzero eigenvector $v$ such that $Av = r(A)v$. +如果矩阵$A \geq 0$,那么: +1. $A$的主特征值$r(A)$是实数且非负的。 +2. 对于$A$的任何其他特征值(可能是复数)$\lambda$,有$|\lambda| \leq r(A)$。 +3. 我们可以找到一个非负且非零的特征向量$v$,使得$Av = r(A)v$。 -Moreover if $A$ is also irreducible then, +此外,如果$A$还是不可约的,那么: +4. 与特征值$r(A)$相关的特征向量$v$是严格正的。 +5. 不存在其他与$r(A)$相关的正特征向量$v$(除了$v$的标量倍数)。 -4. the eigenvector $v$ associated with the eigenvalue $r(A)$ is strictly positive. -5. there exists no other positive eigenvector $v$ (except scalar multiples of $v$) associated with $r(A)$. - -(More of the Perron-Frobenius theorem about primitive matrices will be introduced {ref}`below `.) +(关于原始矩阵的佩龙-弗罗贝尼乌斯定理的更多内容将在{ref}`下文 `中介绍。) ``` -(This is a relatively simple version of the theorem --- for more details see -[here](https://en.wikipedia.org/wiki/Perron%E2%80%93Frobenius_theorem)). - -We will see applications of the theorem below. - -Let's build our intuition for the theorem using a simple example we have seen [before](mc_eg1). - -Now let's consider examples for each case. +(这是该定理的一个相对简单的版本——更多详细信息请参见[这里](https://en.wikipedia.org/wiki/Perron%E2%80%93Frobenius_theorem))。 +我们将在下面看到该定理的应用。 +让我们使用我们之前见过的一个简单[例子](mc_eg1)来建立对这个定理的直觉。 +现在让我们考虑每种情况的例子。 -#### Example: irreducible matrix - -Consider the following irreducible matrix $A$: +#### 示例:不可约矩阵 +考虑以下不可约矩阵$A$: ```{code-cell} ipython3 A = np.array([[0, 1, 0], @@ -202,35 +174,33 @@ A = np.array([[0, 1, 0], [0, 1, 0]]) ``` -We can compute the dominant eigenvalue and the corresponding eigenvector +我们可以计算主特征值和相应的特征向量 ```{code-cell} ipython3 eig(A) ``` -Now we can see the claims of the Perron-Frobenius theorem holds for the irreducible matrix $A$: +现在我们可以看到佩龙-弗罗贝尼乌斯定理对不可约矩阵$A$的声明成立: -1. The dominant eigenvalue is real-valued and non-negative. -2. All other eigenvalues have absolute values less than or equal to the dominant eigenvalue. -3. A non-negative and nonzero eigenvector is associated with the dominant eigenvalue. -4. As the matrix is irreducible, the eigenvector associated with the dominant eigenvalue is strictly positive. -5. There exists no other positive eigenvector associated with the dominant eigenvalue. +1. 主特征值是实数且非负的。 +2. 所有其他特征值的绝对值小于或等于主特征值。 +3. 存在与主特征值相关的非负且非零的特征向量。 +4. 由于矩阵是不可约的,与主特征值相关的特征向量是严格正的。 +5. 不存在其他与主特征值相关的正特征向量。 (prim_matrices)= -### Primitive matrices - -We know that in real world situations it's hard for a matrix to be everywhere positive (although they have nice properties). +### 原始矩阵 -The primitive matrices, however, can still give us helpful properties with looser definitions. +我们知道,在现实世界的情况下,很难让一个矩阵处处为正(尽管它们具有良好的性质)。 +然而,原始矩阵仍然可以在更宽松的定义下给我们提供有用的性质。 -Let $A$ be a square nonnegative matrix and let $A^k$ be the $k^{th}$ power of $A$. - -A matrix is called **primitive** if there exists a $k \in \mathbb{N}$ such that $A^k$ is everywhere positive. +设$A$是一个非负方阵,$A^k$是$A$的$k$次幂。 +如果存在一个$k \in \mathbb{N}$,使得$A^k$处处为正,则称该矩阵为**原始矩阵**。 ```{prf:example} :label: eigen2_ex_prim -Recall the examples given in irreducible matrices: +回顾一下在不可约矩阵中给出的例子: $$ A = \begin{bmatrix} 0.5 & 0.1 \\ @@ -238,7 +208,7 @@ A = \begin{bmatrix} 0.5 & 0.1 \\ \end{bmatrix} $$ -$A$ here is also a primitive matrix since $A^k$ is everywhere nonnegative for $k \in \mathbb{N}$. +这里的$A$也是一个原始矩阵,因为对于$k \in \mathbb{N}$,$A^k$处处非负。 $$ B = \begin{bmatrix} 0 & 1 \\ @@ -250,26 +220,27 @@ B^2 = \begin{bmatrix} 1 & 0 \\ \end{bmatrix} $$ -$B$ is irreducible but not primitive since there are always zeros in either principal diagonal or secondary diagonal. +$B$是不可约的,但不是原始矩阵,因为在主对角线或次对角线上总是有零。 ``` -We can see that if a matrix is primitive, then it implies the matrix is irreducible but not vice versa. +我们可以看到,如果一个矩阵是原始的,那么它意味着该矩阵是不可约的,但反之则不然。 -Now let's step back to the primitive matrices part of the Perron-Frobenius theorem +现在让我们回到佩龙-弗罗贝尼乌斯定理中关于原始矩阵的部分 -```{prf:Theorem} Continous of Perron-Frobenius Theorem +```{prf:Theorem} 佩龙-弗罗贝尼乌斯定理 :label: con-perron-frobenius -If $A$ is primitive then, +如果$A$是原始矩阵,那么: -6. the inequality $|\lambda| \leq r(A)$ is **strict** for all eigenvalues $\lambda$ of $A$ distinct from $r(A)$, and -7. with $v$ and $w$ normalized so that the inner product of $w$ and $v = 1$, we have -$ r(A)^{-m} A^m$ converges to $v w^{\top}$ when $m \rightarrow \infty$. The matrix $v w^{\top}$ is called the **Perron projection** of $A$. -``` +6. 对于$A$的所有不同于$r(A)$的特征值$\lambda$,不等式$|\lambda| \leq r(A)$是**严格的**,并且 -#### Example 1: primitive matrix +7. 当$v$和$w$被归一化使得$w$和$v$的内积等于1时,我们有: -Consider the following primitive matrix $B$: + 当$m \rightarrow \infty$时,$r(A)^{-m} A^m$收敛于$v w^{\top}$。矩阵$v w^{\top}$被称为$A$的**佩龙投影**。 +``` + +#### 示例1:原始矩阵 +考虑以下原始矩阵$B$: ```{code-cell} ipython3 B = np.array([[0, 1, 1], @@ -279,22 +250,22 @@ B = np.array([[0, 1, 1], np.linalg.matrix_power(B, 2) ``` -We compute the dominant eigenvalue and the corresponding eigenvector +我们计算主特征值和相应的特征向量 ```{code-cell} ipython3 eig(B) ``` -Now let's give some examples to see if the claims of the Perron-Frobenius theorem hold for the primitive matrix $B$: +现在让我们给出一些例子,看看佩龙-弗罗贝尼乌斯定理的声明是否对原始矩阵$B$成立: -1. The dominant eigenvalue is real-valued and non-negative. -2. All other eigenvalues have absolute values strictly less than the dominant eigenvalue. -3. A non-negative and nonzero eigenvector is associated with the dominant eigenvalue. -4. The eigenvector associated with the dominant eigenvalue is strictly positive. -5. There exists no other positive eigenvector associated with the dominant eigenvalue. -6. The inequality $|\lambda| < r(B)$ holds for all eigenvalues $\lambda$ of $B$ distinct from the dominant eigenvalue. +1. 主特征值是实数且非负的。 +2. 所有其他特征值的绝对值严格小于主特征值。 +3. 存在与主特征值相关的非负且非零的特征向量。 +4. 与主特征值相关的特征向量是严格正的。 +5. 不存在其他与主特征值相关的正特征向量。 +6. 对于$B$的所有不同于主特征值的特征值$\lambda$,不等式$|\lambda| < r(B)$成立。 -Furthermore, we can verify the convergence property (7) of the theorem on the following examples: +此外,我们可以在以下例子中验证定理的收敛性质(7): ```{code-cell} ipython3 def compute_perron_projection(M): @@ -304,38 +275,38 @@ def compute_perron_projection(M): r = np.max(eigval) - # Find the index of the dominant (Perron) eigenvalue + # 找出主要(佩龙)特征值的指数 i = np.argmax(eigval) - # Get the Perron eigenvectors + # 获取佩龙特征向量 v_P = v[:, i].reshape(-1, 1) w_P = w[:, i].reshape(-1, 1) - # Normalize the left and right eigenvectors + # 归一化左右特征向量 norm_factor = w_P.T @ v_P v_norm = v_P / norm_factor - # Compute the Perron projection matrix + # 计算佩龙投影矩阵 P = v_norm @ w_P.T return P, r def check_convergence(M): P, r = compute_perron_projection(M) - print("Perron projection:") + print("佩龙投影:") print(P) - # Define a list of values for n + # 定义n的值列表 n_list = [1, 10, 100, 1000, 10000] for n in n_list: - # Compute (A/r)^n + # 计算 (A/r)^n M_n = np.linalg.matrix_power(M/r, n) - # Compute the difference between A^n / r^n and the Perron projection + # 计算A^n / r^n与佩龙投影之间的差异 diff = np.abs(M_n - P) - # Calculate the norm of the difference matrix + # 计算差异矩阵的范数 diff_norm = np.linalg.norm(diff, 'fro') print(f"n = {n}, error = {diff_norm:.10f}") @@ -353,7 +324,7 @@ A3 = np.array([[0.971, 0.029, 0.1, 1], [0.2, 0.8, 0.71, 0.95]]) for M in A1, A2, A3: - print("Matrix:") + print("矩阵:") print(M) check_convergence(M) print() @@ -361,38 +332,32 @@ for M in A1, A2, A3: print() ``` -The convergence is not observed in cases of non-primitive matrices. - -Let's go through an example +在非原始矩阵的情况下,不会观察到收敛。 +让我们通过一个例子来说明 ```{code-cell} ipython3 B = np.array([[0, 1, 1], [1, 0, 0], [1, 0, 0]]) -# This shows that the matrix is not primitive -print("Matrix:") +# 这表明该矩阵不是原始矩阵 +print("矩阵:") print(B) -print("100th power of matrix B:") +print("B矩阵的100次方:") print(np.linalg.matrix_power(B, 100)) check_convergence(B) ``` -The result shows that the matrix is not primitive as it is not everywhere positive. - -These examples show how the Perron-Frobenius theorem relates to the eigenvalues and eigenvectors of positive matrices and the convergence of the power of matrices. - -In fact we have already seen the theorem in action before in {ref}`the Markov chain lecture `. +结果表明该矩阵不是原始矩阵,因为它并非处处为正。 +这些例子展示了佩龙-弗罗贝尼乌斯定理如何与正矩阵的特征值和特征向量以及矩阵幂的收敛性相关。 +事实上,我们在{ref}`马尔可夫链讲座 `中已经看到了该定理的应用。 (spec_markov)= -#### Example 2: connection to Markov chains - -We are now prepared to bridge the languages spoken in the two lectures. - -A primitive matrix is both irreducible and aperiodic. - -So Perron-Frobenius theorem explains why both {ref}`Imam and Temple matrix ` and [Hamilton matrix](https://en.wikipedia.org/wiki/Hamiltonian_matrix) converge to a stationary distribution, which is the Perron projection of the two matrices +#### 示例2:与马尔可夫链的联系 +我们现在准备好将这两节课中使用的语言联系起来。 +原始矩阵既是不可约的,又是非周期的。 +因此,佩龙-弗罗贝尼乌斯定理解释了为什么{ref}`伊玛目和寺庙矩阵 `和[哈密顿矩阵](https://en.wikipedia.org/wiki/Hamiltonian_matrix)都收敛到一个平稳分布,这就是这两个矩阵的佩龙投影。 ```{code-cell} ipython3 P = np.array([[0.68, 0.12, 0.20], @@ -422,89 +387,59 @@ mc = qe.MarkovChain(P_hamilton) ψ_star ``` -We can also verify other properties hinted by Perron-Frobenius in these stochastic matrices. - +我们还可以验证 Perron-Frobenius 定理暗示的这些随机矩阵的其他性质。 +++ - -Another example is the relationship between convergence gap and convergence rate. - -In the {ref}`exercise`, we stated that the convergence rate is determined by the spectral gap, the difference between the largest and the second largest eigenvalue. - -This can be proven using what we have learned here. - -Please note that we use $\mathbb{1}$ for a vector of ones in this lecture. - -With Markov model $M$ with state space $S$ and transition matrix $P$, we can write $P^t$ as - +另一个例子是收敛间隙和收敛速率之间的关系。 +在{ref}`练习`中,我们指出收敛速率由谱间隙决定,即最大特征值和第二大特征值之间的差异。 +利用我们在这里学到的知识,可以证明这一点。 +请注意,在本讲中我们使用 $\mathbb{1}$ 表示全1向量。 +对于具有状态空间 $S$ 和转移矩阵 $P$ 的马尔可夫模型,我们可以将 $P^t$ 写成 $$ P^t=\sum_{i=1}^{n-1} \lambda_i^t v_i w_i^{\top}+\mathbb{1} \psi^*, $$ - -This is proven in {cite}`sargent2023economic` and a nice discussion can be found [here](https://math.stackexchange.com/questions/2433997/can-all-matrices-be-decomposed-as-product-of-right-and-left-eigenvector). - -In this formula $\lambda_i$ is an eigenvalue of $P$ with corresponding right and left eigenvectors $v_i$ and $w_i$ . - -Premultiplying $P^t$ by arbitrary $\psi \in \mathscr{D}(S)$ and rearranging now gives - +这在{cite}`sargent2023economic`中得到证明,[这里](https://math.stackexchange.com/questions/2433997/can-all-matrices-be-decomposed-as-product-of-right-and-left-eigenvector)有一个很好的讨论。 +在这个公式中,$\lambda_i$ 是 $P$ 的特征值,$v_i$ 和 $w_i$ 分别是对应的右特征向量和左特征向量。 +现在用任意 $\psi \in \mathscr{D}(S)$ 左乘 $P^t$ 并重新排列,得到 $$ \psi P^t-\psi^*=\sum_{i=1}^{n-1} \lambda_i^t \psi v_i w_i^{\top} $$ - -Recall that eigenvalues are ordered from smallest to largest from $i = 1 ... n$. - -As we have seen, the largest eigenvalue for a primitive stochastic matrix is one. - -This can be proven using [Gershgorin Circle Theorem](https://en.wikipedia.org/wiki/Gershgorin_circle_theorem), -but it is out of the scope of this lecture. - -So by the statement (6) of Perron-Frobenius theorem, $\lambda_i<1$ for all $i` to find the solution $x^{*}$ if it exists. +解$x^{*}$由方程$x^{*} = (I-A)^{-1} d$给出 +1. 由于$A$是一个非负不可约矩阵,求$A$的Perron-Frobenius特征值。 +2. 使用{ref}`诺伊曼级数引理`求解$x^{*}$(如果存在)。 ```{exercise-end} ``` @@ -547,11 +479,11 @@ A = np.array([[0.3, 0.2, 0.3], evals, evecs = eig(A) -r = max(abs(λ) for λ in evals) #dominant eigenvalue/spectral radius +r = max(abs(λ) for λ in evals) # 主特征值/谱半径 print(r) ``` -Since we have $r(A) < 1$ we can thus find the solution using the Neumann Series Lemma. +由于 $r(A) < 1$,我们因此可以使用诺伊曼级数引理来找到解。 ```{code-cell} ipython3 I = np.identity(3) diff --git a/lectures/equalizing_difference.md b/lectures/equalizing_difference.md index c65bbf9..502fdb3 100644 --- a/lectures/equalizing_difference.md +++ b/lectures/equalizing_difference.md @@ -11,388 +11,346 @@ kernelspec: name: python3 --- -# Equalizing Difference Model +# 均衡差异模型 +## 概述 +本讲座介绍了一个大学-高中工资差距模型,其中"培养"大学毕业生的时间扮演了关键角色。 -## Overview +米尔顿·弗里德曼发明了这个模型,用来研究美国牙医和医生收入的差异是竞争性劳动力市场的结果,还是政府与医生专业组织合作施加的入行障碍的结果。 -This lecture presents a model of the college-high-school wage gap in which the -"time to build" a college graduate plays a key role. +詹妮弗·伯恩斯的著作{cite}`Burns_2023`第4章描述了米尔顿·弗里德曼与西蒙·库兹涅茨的合作研究,最终导致了{cite}`kuznets1939incomes`和{cite}`friedman1954incomes`的发表。 +将弗里德曼的应用映射到我们的模型中,可以将我们的高中生视为弗里德曼的牙医,将我们的大学毕业生视为弗里德曼的医生。 -Milton Friedman invented the model to study whether differences in earnings of US dentists and doctors were outcomes of competitive labor markets or whether -they reflected entry barriers imposed by governments working in conjunction with doctors' professional organizations. +我们的呈现是"不完整的",因为它基于一个单一方程,这个方程将是更完整模型中一组均衡条件的一部分。 -Chapter 4 of Jennifer Burns {cite}`Burns_2023` describes Milton Friedman's joint work with Simon Kuznets that eventually led to the publication of {cite}`kuznets1939incomes` and {cite}`friedman1954incomes`. +这个"均衡差异"方程确定了一个大学-高中工资比率,使高中毕业工人和大学毕业工人的现值相等。 -To map Friedman's application into our model, think of our high school students as Friedman's dentists and our college graduates as Friedman's doctors. +这个想法是,终身收入会以某种方式调整,使新高中毕业工人对是否上大学或立即工作无差别。 +(更完整模型中的"其他方程"的作用将是描述什么会调整以实现这个结果。) +我们的模型只是相对工资率"均衡差异"理论的一个例子,这类理论至少可以追溯到亚当·斯密的《国富论》{cite}`smith2010wealth`。 -Our presentation is "incomplete" in the sense that it is based on a single equation that would be part of set equilibrium conditions of a more fully articulated model. +在本讲座的大部分内容中,我们将只使用线性代数的数学工具,特别是矩阵乘法和矩阵求逆。 -This ''equalizing difference'' equation determines a college-high-school wage ratio that equalizes present values of a high school educated worker and a college educated worker. +然而,在讲座接近尾声时,我们会使用微积分,以防读者想看看如何通过计算偏导数来更简洁地呈现一些发现。 -The idea is that lifetime earnings somehow adjust to make a new high school worker indifferent between going to college and not going to college but instead going to work immediately. +这样做还能让我们展示Python在进行微积分计算方面有多么出色! -(The job of the "other equations" in a more complete model would be to describe what adjusts to bring about this outcome.) +但如果你不懂微积分,我们的线性代数工具肯定足够了。 -Our model is just one example of an "equalizing difference" theory of relative wage rates, a class of theories dating back at least to Adam Smith's **Wealth of Nations** {cite}`smith2010wealth`. +像往常一样,我们先导入一些Python模块。 -For most of this lecture, the only mathematical tools that we'll use are from linear algebra, in particular, matrix multiplication and matrix inversion. +```{code-cell} ipython3 +import numpy as np +import matplotlib.pyplot as plt +``` -However, near the end of the lecture, we'll use calculus just in case readers want to see how computing partial derivatives could let us present some findings more concisely. +## 无差异条件 -And doing that will let illustrate how good Python is at doing calculus! +关键思想是,大学入学级别的工资溢价必须调整,以使代表性工人对上大学和不上大学无差别。 -But if you don't know calculus, our tools from linear algebra are certainly enough. +设: -As usual, we'll start by importing some Python modules. +* $R > 1$ 为一期债券的总回报率 -```{code-cell} ipython3 -import numpy as np -import matplotlib.pyplot as plt -from collections import namedtuple -from sympy import Symbol, Lambda, symbols -``` +* $t = 0, 1, 2, \ldots T$ 表示一个人工作或上大学的年数 + +* $0$ 表示一个人如果不上大学可以工作的高中毕业后的第一个时期 -## The indifference condition +* $T$ 表示一个人工作的最后一个时期 -The key idea is that the entry level college wage premium has to adjust to make a representative worker indifferent between going to college and not going to college. +* $w_t^h$ 为高中毕业生在 $t$ 时的工资 -Let +* $w_t^c$ 为大学毕业生在 $t$ 时的工资 - * $R > 1$ be the gross rate of return on a one-period bond +* $\gamma_h > 1$ 为高中毕业生工资的(总)增长率,使得 +$ w_t^h = w_0^h \gamma_h^t$ - * $t = 0, 1, 2, \ldots T$ denote the years that a person either works or attends college - - * $0$ denote the first period after high school that a person can work if he does not go to college - - * $T$ denote the last period that a person works - - * $w_t^h$ be the wage at time $t$ of a high school graduate - - * $w_t^c$ be the wage at time $t$ of a college graduate - - * $\gamma_h > 1$ be the (gross) rate of growth of wages of a high school graduate, so that - $ w_t^h = w_0^h \gamma_h^t$ - - * $\gamma_c > 1$ be the (gross) rate of growth of wages of a college graduate, so that - $ w_t^c = w_0^c \gamma_c^t$ +* $\gamma_c > 1$ 为大学毕业生工资的(总)增长率,使得 +$ w_t^c = w_0^c \gamma_c^t$ - * $D$ be the upfront monetary costs of going to college +* $D$ 为上大学的前期货币成本 -We now compute present values that a new high school graduate earns if +现在我们计算新高中毕业生如果: - * he goes to work immediately and earns wages paid to someone without a college education - * he goes to college for four years and after graduating earns wages paid to a college graduate + * 立即工作并赚取无大学教育者的工资 + * 上大学四年,毕业后赚取大学毕业生的工资 -### Present value of a high school educated worker +所能获得的现值 -If someone goes to work immediately after high school and works for the $T+1$ years $t=0, 1, 2, \ldots, T$, she earns present value +### 高中教育工人的现值 + +如果某人高中毕业后立即工作,并在 $t=0, 1, 2, \ldots, T$ 的 $T+1$ 年内工作,她赚取的现值为: $$ h_0 = \sum_{t=0}^T R^{-t} w_t^h = w_0^h \left[ \frac{1 - (R^{-1} \gamma_h)^{T+1} }{1 - R^{-1} \gamma_h } \right] \equiv w_0^h A_h $$ -where +其中 $$ A_h = \left[ \frac{1 - (R^{-1} \gamma_h)^{T+1} }{1 - R^{-1} \gamma_h } \right]. $$ -The present value $h_0$ is the "human wealth" at the beginning of time $0$ of someone who chooses not to attend college but instead to go to work immediately at the wage of a high school graduate. - -### Present value of a college-bound new high school graduate +现值 $h_0$ 是选择不上大学而是立即以高中毕业生的工资开始工作的人在时间 $0$ 开始时的"人力财富"。 +### 准备上大学的新高中毕业生的现值 -If someone goes to college for the four years $t=0, 1, 2, 3$ during which she earns $0$, but then goes to work immediately after college and works for the $T-3$ years $t=4, 5, \ldots ,T$, she earns present value +如果某人在 $t=0, 1, 2, 3$ 的四年内上大学,期间收入为 $0$,但在大学毕业后立即工作,并在 $t=4, 5, \ldots ,T$ 的 $T-3$ 年内工作,她赚取的现值为: $$ c_0 = \sum_{t=4}^T R^{-t} w_t^c = w_0^c (R^{-1} \gamma_c)^4 \left[ \frac{1 - (R^{-1} \gamma_c)^{T-3} }{1 - R^{-1} \gamma_c } \right] \equiv w_0^c A_c $$ -where +其中 $$ A_c = (R^{-1} \gamma_c)^4 \left[ \frac{1 - (R^{-1} \gamma_c)^{T-3} }{1 - R^{-1} \gamma_c } \right] . $$ -The present value $c_0$ is the "human wealth" at the beginning of time $0$ of someone who chooses to attend college for four years and then start to work at time $t=4$ at the wage of a college graduate. - +现值 $c_0$ 是选择上大学四年然后在 $t=4$ 时以大学毕业生的工资开始工作的人在时间 $0$ 开始时的"人力财富"。 -Assume that college tuition plus four years of room and board amount to $D$ and must be paid at time $0$. +假设大学学费加上四年的食宿费用总计为 $D$,必须在时间 $0$ 支付。 -So net of monetary cost of college, the present value of attending college as of the first period after high school is +因此,扣除上大学的货币成本后,高中毕业后第一个时期上大学的现值为: $$ c_0 - D $$ -We now formulate a pure **equalizing difference** model of the initial college-high school wage gap $\phi$ that verifies +现在我们制定一个纯**均衡差异**模型,用于初始大学-高中工资差距 $\phi$,其中: $$ w_0^c = \phi w_0^h $$ -We suppose that $R, \gamma_h, \gamma_c, T$ and also $w_0^h$ are fixed parameters. - -We start by noting that the pure equalizing difference model asserts that the college-high-school wage gap $\phi$ solves an -"equalizing" equation that sets the present value not going to college equal to the present value of going to college: +我们假设 $R, \gamma_h, \gamma_c, T$ 以及 $w_0^h$ 是固定参数。 +我们首先注意到,纯均衡差异模型断言,大学-高中工资差距 $\phi$ 满足一个"均衡"方程,该方程将不上大学的现值设置为等于上大学的现值: $$ h_0 = c_0 - D $$ -or +或 $$ w_0^h A_h = \phi w_0^h A_c - D . $$ (eq:equalize) -This "indifference condition" is the heart of the model. +这个"无差异条件"是模型的核心。 -Solving equation {eq}`eq:equalize` for the college wage premium $\phi$ we obtain +求解方程 {eq}`eq:equalize` 得到大学工资溢价 $\phi$: $$ \phi = \frac{A_h}{A_c} + \frac{D}{w_0^h A_c} . $$ (eq:wagepremium) -In a **free college** special case $D =0$. +在**免费大学**的特殊情况下,$D =0$。 -Here the only cost of going to college is the forgone earnings from being a high school educated worker. +在这种情况下,上大学的唯一成本是放弃作为高中教育工人的收入。 -In that case, +因此, $$ \phi = \frac{A_h}{A_c} . $$ -In the next section we'll write Python code to compute $\phi$ and plot it as a function of its determinants. +我们很快将编写 Python 代码来计算 $\phi$ 并绘制它作为其决定因素的函数图。 -## Computations +但首先,我们将描述我们模型的一种替代解释,这主要只是重新标记变量。 +## 重新诠释模型:工人和企业家 -We can have some fun with examples that tweak various parameters, -prominently including $\gamma_h, \gamma_c, R$. +我们可以添加一个参数并重新解释变量,以得到一个企业家与工人的模型。 -Now let's write some Python code to compute $\phi$ and plot it as a function of some of its determinants. +现在让 $h$ 表示"工人"的现值。 +我们将企业家的现值定义为: -```{code-cell} ipython3 -# Define the namedtuple for the equalizing difference model -EqDiffModel = namedtuple('EqDiffModel', 'R T γ_h γ_c w_h0 D') - -def create_edm(R=1.05, # gross rate of return - T=40, # time horizon - γ_h=1.01, # high-school wage growth - γ_c=1.01, # college wage growth - w_h0=1, # initial wage (high school) - D=10, # cost for college - ): - - return EqDiffModel(R, T, γ_h, γ_c, w_h0, D) +$$ +c_0 = \pi \sum_{t=4}^T R^{-t} w_t^c +$$ -def compute_gap(model): - R, T, γ_h, γ_c, w_h0, D = model - - A_h = (1 - (γ_h/R)**(T+1)) / (1 - γ_h/R) - A_c = (1 - (γ_c/R)**(T-3)) / (1 - γ_c/R) * (γ_c/R)**4 - ϕ = A_h / A_c + D / (w_h0 * A_c) - - return ϕ -``` +其中 $\pi \in (0,1)$ 是企业家的"项目"成功的概率。 -Using vectorization instead of loops, -we build some functions to help do comparative statics . +对于我们的工人和企业模型,我们将把 $D$ 解释为成为企业家的成本。 +这个成本可能包括雇佣工人、办公空间和律师的费用。 +我们过去称之为大学、高中工资差距的 $\phi$ 现在变成了 +成功企业家收入与工人收入的比率。 +我们会发现,随着 $\pi$ 的减少,$\phi$ 会增加,这表明 +成为企业家的风险越大,成功项目的回报就必须越高。 -For a given instance of the class, we want to recompute $\phi$ when one parameter changes and others remain fixed. +## 计算 -Let's do an example. +我们可以通过调整各种参数来做一些有趣的例子, +主要包括 $\gamma_h, \gamma_c, R$。 +现在让我们编写一些 Python 代码来计算 $\phi$ 并将其作为某些决定因素的函数进行绘图。 ```{code-cell} ipython3 -ex1 = create_edm() -gap1 = compute_gap(ex1) - -gap1 +class equalizing_diff: + """ + 均等差异模型的一个类 + """ + + def __init__(self, R, T, γ_h, γ_c, w_h0, D=0, π=None): + # 通过设置 π 切换到弱模型 + self.R, self.γ_h, self.γ_c, self.w_h0, self.D = R, γ_h, γ_c, w_h0, D + self.T, self.π = T, π + + def compute_gap(self): + R, γ_h, γ_c, w_h0, D = self.R, self.γ_h, self.γ_c, self.w_h0, self.D + T, π = self.T, self.π + + A_h = (1 - (γ_h/R)**(T+1)) / (1 - γ_h/R) + A_c = (1 - (γ_c/R)**(T-3)) / (1 - γ_c/R) * (γ_c/R)**4 + + # 调整后的模型 + if π!=None: + A_c = π*A_c + + ϕ = A_h/A_c + D/(w_h0*A_c) + return ϕ ``` +我们使用向量化而不是循环来构建一些函数,以帮助进行比较静态分析。 +对于类的给定实例,我们想在一个参数变化而其他参数保持固定时重新计算 $\phi$。 +让我们举个例子。 -Let's not charge for college and recompute $\phi$. +```{code-cell} ipython3 +# ϕ_R +def ϕ_R(mc, R_new): + mc_new = equalizing_diff(R_new, mc.T, mc.γ_h, mc.γ_c, mc.w_h0, mc.D, mc.π) + return mc_new.compute_gap() -The initial college wage premium should go down. +ϕ_R = np.vectorize(ϕ_R) -```{code-cell} ipython3 -# free college -ex2 = create_edm(D=0) -gap2 = compute_gap(ex2) -gap2 -``` +# ϕ_γh +def ϕ_γh(mc, γh_new): + mc_new = equalizing_diff(mc.R, mc.T, γh_new, mc.γ_c, mc.w_h0, mc.D, mc.π) + return mc_new.compute_gap() -Let us construct some graphs that show us how the initial college-high-school wage ratio $\phi$ would change if one of its determinants were to change. +ϕ_γh = np.vectorize(ϕ_γh) -Let's start with the gross interest rate $R$. +# ϕ_γc +def ϕ_γc(mc, γc_new): + mc_new = equalizing_diff(mc.R, mc.T, mc.γ_h, γc_new, mc.w_h0, mc.D, mc.π) + return mc_new.compute_gap() -```{code-cell} ipython3 -R_arr = np.linspace(1, 1.2, 50) -models = [create_edm(R=r) for r in R_arr] -gaps = [compute_gap(model) for model in models] +ϕ_γc = np.vectorize(ϕ_γc) -plt.plot(R_arr, gaps) -plt.xlabel(r'$R$') -plt.ylabel(r'wage gap') -plt.show() -``` +# ϕ_π +def ϕ_π(mc, π_new): + mc_new = equalizing_diff(mc.R, mc.T, mc.γ_h, mc.γ_c, mc.w_h0, mc.D, π_new) + return mc_new.compute_gap() -Evidently, the initial wage ratio $\phi$ must rise to compensate a prospective high school student for **waiting** to start receiving income -- remember that while she is earning nothing in years $t=0, 1, 2, 3$, the high school worker is earning a salary. +ϕ_π = np.vectorize(ϕ_π) +``` -Not let's study what happens to the initial wage ratio $\phi$ if the rate of growth of college wages rises, holding constant other -determinants of $\phi$. +```{code-cell} ipython3 +# 设定标准参数 +R = 1.05 +T = 40 +γ_h, γ_c = 1.01, 1.01 +w_h0 = 1 +D = 10 + +# 创建一个例子 +ex1 = equalizing_diff(R=R, T=T, γ_h=γ_h, γ_c=γ_c, w_h0=w_h0, D=D) +gap1 = ex1.compute_gap() + +print(gap1) +``` +让我们设想不收取大学学费,然后重新计算 $\phi$。 +初始的大学工资溢价应该会降低。 ```{code-cell} ipython3 -γc_arr = np.linspace(1, 1.2, 50) -models = [create_edm(γ_c=γ_c) for γ_c in γc_arr] -gaps = [compute_gap(model) for model in models] +# 免费大学 +ex2 = equalizing_diff(R, T, γ_h, γ_c, w_h0, D=0) +gap2 = ex2.compute_gap() +print(gap2) +``` +让我们构建一些图表,展示如果初始大学-高中工资比率 $\phi$ 的某个决定因素发生变化,$\phi$ 将如何改变。 +我们先从总利率 $R$ 开始。 -plt.plot(γc_arr, gaps) -plt.xlabel(r'$\gamma_c$') -plt.ylabel(r'wage gap') +```{code-cell} ipython3 +R_arr = np.linspace(1, 1.2, 50) +plt.plot(R_arr, φ_R(ex1, R_arr)) +plt.xlabel(r'$R$') +plt.ylabel(r'工资差距') plt.show() ``` -Notice how the initial wage gap falls when the rate of growth $\gamma_c$ of college wages rises. - -The wage gap falls to "equalize" the present values of the two types of career, one as a high school worker, the other as a college worker. - -Can you guess what happens to the initial wage ratio $\phi$ when next we vary the rate of growth of high school wages, holding all other determinants of $\phi$ constant? - -The following graph shows what happens. +注意当大学工资增长率 $\gamma_c$ 上升时,初始工资差距是如何下降的。 +工资差距下降是为了"平衡"两种职业类型的现值,一种是高中工人,另一种是大学工人。 +你能猜到当我们接下来改变高中工资的增长率时,初始工资比率 $\phi$ 会发生什么变化吗?同时保持 $\phi$ 的所有其他决定因素不变。 +下图显示了会发生什么。 ```{code-cell} ipython3 γh_arr = np.linspace(1, 1.1, 50) -models = [create_edm(γ_h=γ_h) for γ_h in γh_arr] -gaps = [compute_gap(model) for model in models] - -plt.plot(γh_arr, gaps) +plt.plot(γh_arr, φ_γh(ex1, γh_arr)) plt.xlabel(r'$\gamma_h$') -plt.ylabel(r'wage gap') +plt.ylabel(r'工资差距') plt.show() ``` - -## Entrepreneur-worker interpretation - -We can add a parameter and reinterpret variables to get a model of entrepreneurs versus workers. - -We now let $h$ be the present value of a "worker". - -We define the present value of an entrepreneur to be - -$$ -c_0 = \pi \sum_{t=4}^T R^{-t} w_t^c -$$ - -where $\pi \in (0,1) $ is the probability that an entrepreneur's "project" succeeds. - -For our model of workers and firms, we'll interpret $D$ as the cost of becoming an entrepreneur. - -This cost might include costs of hiring workers, office space, and lawyers. - -What we used to call the college, high school wage gap $\phi$ now becomes the ratio -of a successful entrepreneur's earnings to a worker's earnings. - -We'll find that as $\pi$ decreases, $\phi$ increases, indicating that the riskier it is to -be an entrepreneur, the higher must be the reward for a successful project. - -Now let's adopt the entrepreneur-worker interpretation of our model - -```{code-cell} ipython3 -# Define a model of entrepreneur-worker interpretation -EqDiffModel = namedtuple('EqDiffModel', 'R T γ_h γ_c w_h0 D π') - -def create_edm_π(R=1.05, # gross rate of return - T=40, # time horizon - γ_h=1.01, # high-school wage growth - γ_c=1.01, # college wage growth - w_h0=1, # initial wage (high school) - D=10, # cost for college - π=0 # chance of business success - ): - - return EqDiffModel(R, T, γ_h, γ_c, w_h0, D, π) - - -def compute_gap(model): - R, T, γ_h, γ_c, w_h0, D, π = model - - A_h = (1 - (γ_h/R)**(T+1)) / (1 - γ_h/R) - A_c = (1 - (γ_c/R)**(T-3)) / (1 - γ_c/R) * (γ_c/R)**4 - - # Incorprate chance of success - A_c = π * A_c - - ϕ = A_h / A_c + D / (w_h0 * A_c) - return ϕ -``` - -If the probability that a new business succeeds is $0.2$, let's compute the initial wage premium for successful entrepreneurs. +## 企业家-工人解释 +现在让我们采用我们模型的企业家-工人解释。 +如果一个新企业成功的概率是 $0.2$,让我们计算成功企业家的初始工资溢价。 ```{code-cell} ipython3 -ex3 = create_edm_π(π=0.2) -gap3 = compute_gap(ex3) +# 企业家模型 +ex3 = equalizing_diff(R, T, γ_h, γ_c, w_h0, π=0.2) +gap3 = ex3.compute_gap() -gap3 +print(gap3) ``` -Now let's study how the initial wage premium for successful entrepreneurs depend on the success probability. +现在让我们研究成功企业家的初始工资溢价是如何依赖于成功概率的。 ```{code-cell} ipython3 π_arr = np.linspace(0.2, 1, 50) -models = [create_edm_π(π=π) for π in π_arr] -gaps = [compute_gap(model) for model in models] - -plt.plot(π_arr, gaps) -plt.ylabel(r'wage gap') +plt.plot(π_arr, φ_π(ex3, π_arr)) +plt.ylabel(r'工资差距') plt.xlabel(r'$\pi$') plt.show() ``` -Does the graph make sense to you? - - - -## An application of calculus +这个图表对你来说有意义吗? -So far, we have used only linear algebra and it has been a good enough tool for us to figure out how our model works. +## 微积分的应用 +到目前为止,我们只使用了线性代数,这对我们理解模型的运作原理已经足够了。 +然而,懂得微积分的人可能会希望我们直接求偏导数。 +现在我们就来做这个。 +不懂微积分的读者可以不用继续往下读,可以确信应用线性代数已经让我们了解了模型的主要特性。 +但对于那些有兴趣了解我们如何让 Python 完成计算偏导数的所有繁重工作的读者,我们现在会说一些相关的内容。 +我们将使用 Python 模块 'sympy' 来计算 $\phi$ 对决定它的参数的偏导数。 +让我们从 sympy 导入关键函数。 -However, someone who knows calculus might want us just to take partial derivatives. - -We'll do that now. - -A reader who doesn't know calculus could read no further and feel confident that applying linear algebra has taught us the main properties of the model. - -But for a reader interested in how we can get Python to do all the hard work involved in computing partial derivatives, we'll say a few things about that now. +```{code-cell} ipython3 +from sympy import Symbol, Lambda, symbols +``` -We'll use the Python module 'sympy' to compute partial derivatives of $\phi$ with respect to the parameters that determine it. +设定符号 -Define symbols ```{code-cell} ipython3 -γ_h, γ_c, w_h0, D = symbols('\gamma_h, \gamma_c, w_0^h, D', real=True) +γ_h, γ_c, w_h0, D = symbols('\gamma_h, \gamma_h_c, w_0^h, D', real=True) R, T = Symbol('R', real=True), Symbol('T', integer=True) ``` -Define function $A_h$ +设定函数$A_h$ ```{code-cell} ipython3 A_h = Lambda((γ_h, R, T), (1 - (γ_h/R)**(T+1)) / (1 - γ_h/R)) A_h ``` -Define function $A_c$ +设定函数 $A_c$ ```{code-cell} ipython3 A_c = Lambda((γ_c, R, T), (1 - (γ_c/R)**(T-3)) / (1 - γ_c/R) * (γ_c/R)**4) A_c ``` -Now, define $\phi$ +设定 $\phi$ ```{code-cell} ipython3 ϕ = Lambda((D, γ_h, γ_c, R, T, w_h0), A_h(γ_h, R, T)/A_c(γ_c, R, T) + D/(w_h0*A_c(γ_c, R, T))) @@ -401,8 +359,8 @@ Now, define $\phi$ ```{code-cell} ipython3 ϕ ``` +我们开始设定默认的参数值。 -We begin by setting default parameter values. ```{code-cell} ipython3 R_value = 1.05 @@ -412,7 +370,7 @@ w_h0_value = 1 D_value = 10 ``` -Now let's compute $\frac{\partial \phi}{\partial D}$ and then evaluate it at the default values +现在让我们计算 $\frac{\partial \phi}{\partial D}$然后测量其在默认值的值 ```{code-cell} ipython3 ϕ_D = ϕ(D, γ_h, γ_c, R, T, w_h0).diff(D) @@ -420,14 +378,14 @@ Now let's compute $\frac{\partial \phi}{\partial D}$ and then evaluate it at the ``` ```{code-cell} ipython3 -# Numerical value at default parameters +# 在默认值的值 ϕ_D_func = Lambda((D, γ_h, γ_c, R, T, w_h0), ϕ_D) ϕ_D_func(D_value, γ_h_value, γ_c_value, R_value, T_value, w_h0_value) ``` -Thus, as with our earlier graph, we find that raising $R$ increases the initial college wage premium $\phi$. - -Compute $\frac{\partial \phi}{\partial T}$ and evaluate it at default parameters +因此,与我们之前的图表一样,我们发现提高 $R$ 会增加初始大学工资溢价 $\phi$。 ++++ +计算 $\frac{\partial \phi}{\partial T}$ 并在默认参数下评估它 ```{code-cell} ipython3 ϕ_T = ϕ(D, γ_h, γ_c, R, T, w_h0).diff(T) @@ -435,16 +393,15 @@ Compute $\frac{\partial \phi}{\partial T}$ and evaluate it at default parameters ``` ```{code-cell} ipython3 -# Numerical value at default parameters +# 在默认值的值 ϕ_T_func = Lambda((D, γ_h, γ_c, R, T, w_h0), ϕ_T) ϕ_T_func(D_value, γ_h_value, γ_c_value, R_value, T_value, w_h0_value) ``` +我们发现提高 $T$ 会降低初始大学工资溢价 $\phi$。 +这是因为大学毕业生现在有更长的职业生涯来"收回"他们为上大学付出的时间和其他成本。 ++++ +让我们计算 $\frac{\partial \phi}{\partial \gamma_h}$ 并在默认参数下评估它。 -We find that raising $T$ decreases the initial college wage premium $\phi$. - -This is because college graduates now have longer career lengths to "pay off" the time and other costs they paid to go to college - -Let's compute $\frac{\partial \phi}{\partial γ_h}$ and evaluate it at default parameters. ```{code-cell} ipython3 ϕ_γ_h = ϕ(D, γ_h, γ_c, R, T, w_h0).diff(γ_h) @@ -452,14 +409,13 @@ Let's compute $\frac{\partial \phi}{\partial γ_h}$ and evaluate it at default p ``` ```{code-cell} ipython3 -# Numerical value at default parameters +# 在默认值的值 ϕ_γ_h_func = Lambda((D, γ_h, γ_c, R, T, w_h0), ϕ_γ_h) ϕ_γ_h_func(D_value, γ_h_value, γ_c_value, R_value, T_value, w_h0_value) ``` - -We find that raising $\gamma_h$ increases the initial college wage premium $\phi$, in line with our earlier graphical analysis. - -Compute $\frac{\partial \phi}{\partial γ_c}$ and evaluate it numerically at default parameter values +我们发现提高 $\gamma_h$ 会增加初始大学工资溢价 $\phi$,这与我们之前的图形分析结果一致。 ++++ +计算 $\frac{\partial \phi}{\partial \gamma_c}$ 并在默认参数值下对其进行数值评估 ```{code-cell} ipython3 ϕ_γ_c = ϕ(D, γ_h, γ_c, R, T, w_h0).diff(γ_c) @@ -467,14 +423,15 @@ Compute $\frac{\partial \phi}{\partial γ_c}$ and evaluate it numerically at def ``` ```{code-cell} ipython3 -# Numerical value at default parameters +# 在默认值的值 ϕ_γ_c_func = Lambda((D, γ_h, γ_c, R, T, w_h0), ϕ_γ_c) ϕ_γ_c_func(D_value, γ_h_value, γ_c_value, R_value, T_value, w_h0_value) ``` -We find that raising $\gamma_c$ decreases the initial college wage premium $\phi$, in line with our earlier graphical analysis. +我们发现提高 $\gamma_c$ 会降低初始大学工资溢价 $\phi$,这与我们之前的图形分析结果一致。 ++++ +让我们计算 $\frac{\partial \phi}{\partial R}$ 并在默认参数值下对其进行数值评估 -Let's compute $\frac{\partial \phi}{\partial R}$ and evaluate it numerically at default parameter values ```{code-cell} ipython3 ϕ_R = ϕ(D, γ_h, γ_c, R, T, w_h0).diff(R) @@ -482,9 +439,78 @@ Let's compute $\frac{\partial \phi}{\partial R}$ and evaluate it numerically at ``` ```{code-cell} ipython3 -# Numerical value at default parameters +# 在默认值的值 ϕ_R_func = Lambda((D, γ_h, γ_c, R, T, w_h0), ϕ_R) ϕ_R_func(D_value, γ_h_value, γ_c_value, R_value, T_value, w_h0_value) ``` -We find that raising the gross interest rate $R$ increases the initial college wage premium $\phi$, in line with our earlier graphical analysis. +我们发现提高总利率 $R$ 会增加初始大学工资溢价 $\phi$,这与我们之前的图形分析结果一致。 + + +```{code-cell} ipython3 + +``` + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/lectures/french_rev.md b/lectures/french_rev.md index a415aaa..a74e59f 100644 --- a/lectures/french_rev.md +++ b/lectures/french_rev.md @@ -855,8 +855,8 @@ plt.plot(bal[31:44], infl[31:44], '+', color='red', label='恐怖时期') plt.plot(bal[44:63], infl[44:63], '*', color='orange', label='经典的Cagan恶性通货膨胀') plt.plot(bal[44:63], a3 + bal[44:63] * b3, color='orange') -plt.xlabel('real balances') -plt.ylabel('inflation') +plt.xlabel('实际余额') +plt.ylabel('通货膨胀') plt.legend() plt.tight_layout() plt.show() diff --git a/lectures/geom_series.md b/lectures/geom_series.md index be698d0..8f9032d 100644 --- a/lectures/geom_series.md +++ b/lectures/geom_series.md @@ -23,132 +23,109 @@ kernelspec: ```{index} single: python ``` -# Geometric Series for Elementary Economics - -## Overview - -The lecture describes important ideas in economics that use the mathematics of geometric series. - -Among these are - -- the Keynesian **multiplier** -- the money **multiplier** that prevails in fractional reserve banking - systems -- interest rates and present values of streams of payouts from assets - -(As we shall see below, the term **multiplier** comes down to meaning **sum of a convergent geometric series**) - -These and other applications prove the truth of the wise crack that +# 基础经济学中的几何级数 +## 概述 +本讲座描述了使用几何级数数学的重要经济学概念。 +其中包括: +- 凯恩斯主义的**乘数** +- 在部分准备金银行系统中普遍存在的货币**乘数** +- 资产支付流的利率和现值 +(如我们下面将看到的,术语**乘数**实际上意味着**收敛几何级数的和**) +这些应用和其他应用证实了这句俏皮话的真实性: ```{epigraph} -"In economics, a little knowledge of geometric series goes a long way." +"在经济学中,一点几何级数的知识就能走很远" ``` -Below we'll use the following imports: +以下我们将使用这些导入: ```{code-cell} ipython +%matplotlib inline import matplotlib.pyplot as plt -plt.rcParams["figure.figsize"] = (11, 5) #set default figure size +plt.rcParams["figure.figsize"] = (11, 5) #设置默认的图像大小 import numpy as np import sympy as sym from sympy import init_printing from matplotlib import cm ``` -## Key formulas +## 关键公式 -To start, let $c$ be a real number that lies strictly between -$-1$ and $1$. +首先,让 $c$ 是一个严格介于 $-1$ 和 $1$ 之间的实数。 -- We often write this as $c \in (-1,1)$. -- Here $(-1,1)$ denotes the collection of all real numbers that - are strictly less than $1$ and strictly greater than $-1$. -- The symbol $\in$ means *in* or *belongs to the set after the symbol*. +- 我们通常写作 $c \in (-1,1)$。 +- 这里 $(-1,1)$ 表示所有严格小于 $1$ 且严格大于 $-1$ 的实数的集合。 +- 符号 $\in$ 的意思是 *属于* 或 *属于符号后面的集合*。 -We want to evaluate geometric series of two types -- infinite and finite. +我们想要评估两种类型的几何级数——无限和有限。 -### Infinite geometric series +### 无限几何级数 -The first type of geometric that interests us is the infinite series +我们感兴趣的第一种几何级数是无限级数 $$ 1 + c + c^2 + c^3 + \cdots $$ -Where $\cdots$ means that the series continues without end. +其中 $\cdots$ 表示级数无穷无尽地继续。 -The key formula is +关键公式是 ```{math} :label: infinite - 1 + c + c^2 + c^3 + \cdots = \frac{1}{1 -c } ``` -To prove key formula {eq}`infinite`, multiply both sides by $(1-c)$ and verify -that if $c \in (-1,1)$, then the outcome is the -equation $1 = 1$. +为了证明关键公式 {eq}`infinite`,将两边乘以 $(1-c)$ 并验证如果 $c \in (-1,1)$,则结果是 +方程 $1 = 1$。 -### Finite geometric series +### 有限几何级数 -The second series that interests us is the finite geometric series +我们感兴趣的第二种级数是有限几何级数 $$ 1 + c + c^2 + c^3 + \cdots + c^T $$ -where $T$ is a positive integer. +其中 $T$ 是一个正整数。 -The key formula here is +关键公式是 $$ 1 + c + c^2 + c^3 + \cdots + c^T = \frac{1 - c^{T+1}}{1-c} $$ -**Remark:** The above formula works for any value of the scalar -$c$. We don't have to restrict $c$ to be in the -set $(-1,1)$. +**备注:** 上述公式适用于任何标量 $c$ 的值。我们不必将 $c$ 限制在 +集合 $(-1,1)$ 中。 -We now move on to describe some famous economic applications of -geometric series. +我们现在继续描述一些几何级数的著名经济应用。 -## Example: The Money Multiplier in Fractional Reserve Banking +## 示例:部分准备金银行制度中的货币乘数 -In a fractional reserve banking system, banks hold only a fraction -$r \in (0,1)$ of cash behind each **deposit receipt** that they -issue +在部分准备金银行制度中,银行仅持有每张 **存款凭证** 背后的现金的一部分 $r \in (0,1)$ -* In recent times - - cash consists of pieces of paper issued by the government and - called dollars or pounds or $\ldots$ - - a *deposit* is a balance in a checking or savings account that - entitles the owner to ask the bank for immediate payment in cash -* When the UK and France and the US were on either a gold or silver - standard (before 1914, for example) - - cash was a gold or silver coin - - a *deposit receipt* was a *bank note* that the bank promised to - convert into gold or silver on demand; (sometimes it was also a - checking or savings account balance) +* 近年来 + - 现金是由政府发行的纸币,称为美元或英镑或 $\ldots$ + - *存款* 是支票或储蓄账户中的余额,持有人有权要求银行立即支付现金 +* 当英国、法国和美国采用金本位或银本位时(例如1914年之前) + - 现金是金或银币 + - *存款凭证* 是银行承诺按需兑换成金或银的 *银行票据*;(有时也是支票或储蓄账户余额) -Economists and financiers often define the **supply of money** as an -economy-wide sum of **cash** plus **deposits**. +经济学家和金融家通常将 **货币供应量** 定义为经济范围内 **现金** 加 **存款** 的总和。 -In a **fractional reserve banking system** (one in which the reserve -ratio $r$ satisfies $0 < r < 1$), **banks create money** by issuing deposits *backed* by fractional reserves plus loans that they make to their customers. +在 **部分准备金银行制度** 中(其中准备金率 $r$ 满足 $0 < r < 1$),**银行通过发行存款**(由部分准备金加上他们向客户发放的贷款支持)**创造货币**。 -A geometric series is a key tool for understanding how banks create -money (i.e., deposits) in a fractional reserve system. +几何级数是理解银行如何在部分准备金制度中创造货币(即存款)的关键工具。 -The geometric series formula {eq}`infinite` is at the heart of the classic model of the money creation process -- one that leads us to the celebrated -**money multiplier**. +几何级数公式 {eq}`infinite` 是经典货币创造过程模型的核心——这一模型引导我们到达著名的 **货币乘数**。 -### A simple model +### 一个简单的模型 -There is a set of banks named $i = 0, 1, 2, \ldots$. +有一组银行,命名为 $i = 0, 1, 2, \ldots$。 -Bank $i$'s loans $L_i$, deposits $D_i$, and -reserves $R_i$ must satisfy the balance sheet equation (because -**balance sheets balance**): +银行 $i$ 的贷款 $L_i$、存款 $D_i$ 和 +准备金 $R_i$ 必须满足资产负债表方程(因为 +**资产负债表平衡**): ```{math} :label: balance @@ -156,17 +133,17 @@ reserves $R_i$ must satisfy the balance sheet equation (because L_i + R_i = D_i ``` -The left side of the above equation is the sum of the bank's **assets**, -namely, the loans $L_i$ it has outstanding plus its reserves of -cash $R_i$. +上面方程的左侧是银行的 **资产** 之和, +即其未偿还贷款 $L_i$ 加上其现金准备金 +$R_i$。 -The right side records bank $i$'s liabilities, -namely, the deposits $D_i$ held by its depositors; these are -IOU's from the bank to its depositors in the form of either checking -accounts or savings accounts (or before 1914, bank notes issued by a -bank stating promises to redeem notes for gold or silver on demand). +右侧记录了银行 $i$ 的负债, +即其存款人持有的存款 $D_i$;这些是 +银行对其存款人的 IOU,以支票 +账户或储蓄账户的形式(或在1914年之前,银行发行的 +承诺按需兑换成金或银的银行票据)。 -Each bank $i$ sets its reserves to satisfy the equation +每个银行 $i$ 设置其准备金以满足方程 ```{math} :label: reserves @@ -174,14 +151,13 @@ Each bank $i$ sets its reserves to satisfy the equation R_i = r D_i ``` -where $r \in (0,1)$ is its **reserve-deposit ratio** or **reserve -ratio** for short +其中 $r \in (0,1)$ 是其 **准备金-存款比率** 或简称 **准备金比率** -- the reserve ratio is either set by a government or chosen by banks - for precautionary reasons +- 准备金比率由政府设定或由银行 + 出于预防原因选择 -Next we add a theory stating that bank $i+1$'s deposits depend -entirely on loans made by bank $i$, namely +接下来我们添加一个理论,指出银行 $i+1$ 的存款完全依赖于 +银行 $i$ 发放的贷款,即 ```{math} :label: deposits @@ -189,25 +165,25 @@ entirely on loans made by bank $i$, namely D_{i+1} = L_i ``` -Thus, we can think of the banks as being arranged along a line with -loans from bank $i$ being immediately deposited in $i+1$ +因此,我们可以认为银行按一条线排列, +银行 $i$ 的贷款立即存入 $i+1$ -- in this way, the debtors to bank $i$ become creditors of - bank $i+1$ +- 这样,银行 $i$ 的债务人变成了 + 银行 $i+1$ 的债权人 -Finally, we add an *initial condition* about an exogenous level of bank -$0$'s deposits +最后,我们添加一个关于银行 +$0$ 存款外生水平的 *初始条件* $$ -D_0 \ \text{ is given exogenously} +D_0 \ \text{ 是外生给定的} $$ -We can think of $D_0$ as being the amount of cash that a first -depositor put into the first bank in the system, bank number $i=0$. +我们可以认为 $D_0$ 是系统中第一个 +存款人存入第一个银行(即银行编号 $i=0$)的现金量。 -Now we do a little algebra. +现在我们做一些代数运算。 -Combining equations {eq}`balance` and {eq}`reserves` tells us that +结合方程 {eq}`balance` 和 {eq}`reserves` 告诉我们 ```{math} :label: fraction @@ -215,265 +191,224 @@ Combining equations {eq}`balance` and {eq}`reserves` tells us that L_i = (1-r) D_i ``` -This states that bank $i$ loans a fraction $(1-r)$ of its -deposits and keeps a fraction $r$ as cash reserves. +这表明银行 $i$ 贷款其 +存款的一部分 $(1-r)$ 并保留一部分 $r$ 作为现金准备金。 -Combining equation {eq}`fraction` with equation {eq}`deposits` tells us that +结合方程 {eq}`fraction` 和方程 {eq}`deposits` 告诉我们 $$ -D_{i+1} = (1-r) D_i \ \text{ for } i \geq 0 +D_{i+1} = (1-r) D_i \ \text{ 对 } i \geq 0 $$ -which implies that +这意味着 ```{math} :label: geomseries -D_i = (1 - r)^i D_0 \ \text{ for } i \geq 0 +D_i = (1 - r)^i D_0 \ \text{ 对 } i \geq 0 ``` -Equation {eq}`geomseries` expresses $D_i$ as the $i$ th term in the -product of $D_0$ and the geometric series +方程 {eq}`geomseries` 表示 $D_i$ 是 +几何级数 $D_0$ 和 $$ 1, (1-r), (1-r)^2, \cdots $$ -Therefore, the sum of all deposits in our banking system -$i=0, 1, 2, \ldots$ is +的第 $i$ 项的乘积。 + +因此,我们银行系统中所有存款的总和 +$i=0, 1, 2, \ldots$ 是 ```{math} :label: sumdeposits \sum_{i=0}^\infty (1-r)^i D_0 = \frac{D_0}{1 - (1-r)} = \frac{D_0}{r} ``` +### 货币乘数 -### Money multiplier +**货币乘数** 是一个数字,表示向银行 $0$ 注入外生现金导致银行系统总存款增加的乘数因子。 -The **money multiplier** is a number that tells the multiplicative -factor by which an exogenous injection of cash into bank $0$ leads -to an increase in the total deposits in the banking system. +方程 {eq}`sumdeposits` 断言 **货币乘数** 是 $\frac{1}{r}$ -Equation {eq}`sumdeposits` asserts that the **money multiplier** is -$\frac{1}{r}$ +- 在银行 $0$ 初始存入现金 $D_0$ 导致银行系统创造总存款 $\frac{D_0}{r}$。 +- 初始存款 $D_0$ 作为准备金持有,按照 $D_0 = \sum_{i=0}^\infty R_i$ 分布在整个银行系统中。 -- An initial deposit of cash of $D_0$ in bank $0$ leads - the banking system to create total deposits of $\frac{D_0}{r}$. -- The initial deposit $D_0$ is held as reserves, distributed - throughout the banking system according to $D_0 = \sum_{i=0}^\infty R_i$. +## 示例:凯恩斯乘数 -## Example: The Keynesian Multiplier +著名经济学家约翰·梅纳德·凯恩斯及其追随者创建了一个简单模型,旨在确定国民收入 $y$ 在以下情况下: -The famous economist John Maynard Keynes and his followers created a -simple model intended to determine national income $y$ in -circumstances in which +- 存在大量失业资源,特别是 **劳动力和资本的过剩供给** +- 价格和利率未能调整以使总 **供给等于需求**(例如,价格和利率被冻结) +- 国民收入完全由总需求决定 -- there are substantial unemployed resources, in particular **excess - supply** of labor and capital -- prices and interest rates fail to adjust to make aggregate **supply - equal demand** (e.g., prices and interest rates are frozen) -- national income is entirely determined by aggregate demand +### 静态版本 -### Static version +一个基本的凯恩斯国民收入决定模型由描述 $y$ 的总需求及其组成部分的三个方程组成。 -An elementary Keynesian model of national income determination consists -of three equations that describe aggregate demand for $y$ and its -components. - -The first equation is a national income identity asserting that -consumption $c$ plus investment $i$ equals national income -$y$: +第一个方程是一个国民收入恒等式,断言消费 $c$ 加上投资 $i$ 等于国民收入 $y$: $$ -c+ i = y +c + i = y $$ -The second equation is a Keynesian consumption function asserting that -people consume a fraction $b \in (0,1)$ of their income: +第二个方程是一个凯恩斯消费函数,断言人们消费其收入的一部分 $b \in (0,1)$: $$ c = b y $$ -The fraction $b \in (0,1)$ is called the **marginal propensity to -consume**. +部分 $b \in (0,1)$ 称为 **边际消费倾向**。 -The fraction $1-b \in (0,1)$ is called the **marginal propensity -to save**. +部分 $1-b \in (0,1)$ 称为 **边际储蓄倾向**。 -The third equation simply states that investment is exogenous at level -$i$. +第三个方程简单地说明投资在水平 $i$ 是外生的。 -- *exogenous* means *determined outside this model*. +- *外生* 意味着 *在此模型之外确定*。 -Substituting the second equation into the first gives $(1-b) y = i$. +将第二个方程代入第一个方程得到 $(1-b) y = i$。 -Solving this equation for $y$ gives +求解这个方程得到 $$ y = \frac{1}{1-b} i $$ -The quantity $\frac{1}{1-b}$ is called the **investment -multiplier** or simply the **multiplier**. +数量 $\frac{1}{1-b}$ 称为 **投资乘数** 或简称 **乘数**。 -Applying the formula for the sum of an infinite geometric series, we can -write the above equation as +应用无限几何级数的求和公式,我们可以将上述方程写为 $$ y = i \sum_{t=0}^\infty b^t $$ -where $t$ is a nonnegative integer. +其中 $t$ 是一个非负整数。 -So we arrive at the following equivalent expressions for the multiplier: +因此,我们得出乘数的以下等效表达式: $$ -\frac{1}{1-b} = \sum_{t=0}^\infty b^t +\frac{1}{1-b} = \sum_{t=0}^\infty b^t $$ -The expression $\sum_{t=0}^\infty b^t$ motivates an interpretation -of the multiplier as the outcome of a dynamic process that we describe -next. +表达式 $\sum_{t=0}^\infty b^t$ 激发了对乘数作为我们接下来描述的动态过程结果的解释。 -### Dynamic version +### 动态版本 -We arrive at a dynamic version by interpreting the nonnegative integer -$t$ as indexing time and changing our specification of the -consumption function to take time into account +我们通过将非负整数 $t$ 解释为时间索引并改变消费函数的规范以考虑时间因素,得出动态版本 -- we add a one-period lag in how income affects consumption +- 我们增加了收入影响消费的一个时期的滞后 -We let $c_t$ be consumption at time $t$ and $i_t$ be -investment at time $t$. +我们设 $c_t$ 为时间 $t$ 的消费,$i_t$ 为 +时间 $t$ 的投资。 -We modify our consumption function to assume the form +我们修改消费函数,使其形式为 $$ c_t = b y_{t-1} $$ -so that $b$ is the marginal propensity to consume (now) out of -last period's income. +这样 $b$ 就是上一期收入的边际消费倾向。 -We begin with an initial condition stating that +我们从一个初始条件开始,说明 $$ y_{-1} = 0 $$ -We also assume that +我们还假设 $$ -i_t = i \ \ \textrm {for all } t \geq 0 +i_t = i \ \ \textrm {对所有 } t \geq 0 $$ -so that investment is constant over time. +这样投资随时间保持不变。 -It follows that +因此 $$ y_0 = i + c_0 = i + b y_{-1} = i $$ -and +以及 $$ y_1 = c_1 + i = b y_0 + i = (1 + b) i $$ -and +以及 $$ y_2 = c_2 + i = b y_1 + i = (1 + b + b^2) i $$ -and more generally +更一般地 $$ y_t = b y_{t-1} + i = (1+ b + b^2 + \cdots + b^t) i $$ -or +或者 $$ y_t = \frac{1-b^{t+1}}{1 -b } i $$ -Evidently, as $t \rightarrow + \infty$, +显然,当 $t \rightarrow + \infty$ 时, $$ y_t \rightarrow \frac{1}{1-b} i $$ -**Remark 1:** The above formula is often applied to assert that an -exogenous increase in investment of $\Delta i$ at time $0$ -ignites a dynamic process of increases in national income by successive amounts +**备注 1:** 上述公式通常用于断言在时间 $0$ 投资增加 $\Delta i$ 引发的动态过程,使国民收入按连续金额增加 $$ \Delta i, (1 + b )\Delta i, (1+b + b^2) \Delta i , \cdots $$ -at times $0, 1, 2, \ldots$. +在时间 $0, 1, 2, \ldots$。 -**Remark 2** Let $g_t$ be an exogenous sequence of government -expenditures. +**备注 2** 设 $g_t$ 为政府支出的外生序列。 -If we generalize the model so that the national income identity -becomes +如果我们将模型推广,使国民收入恒等式变为 $$ c_t + i_t + g_t = y_t $$ -then a version of the preceding argument shows that the **government -expenditures multiplier** is also $\frac{1}{1-b}$, so that a -permanent increase in government expenditures ultimately leads to an -increase in national income equal to the multiplier times the increase -in government expenditures. +那么前述论证的一个版本表明 **政府支出乘数** 也是 $\frac{1}{1-b}$,因此政府支出的永久增加最终导致国民收入增加,等于乘数乘以政府支出的增加。 -## Example: Interest Rates and Present Values +## 示例:利率和现值 -We can apply our formula for geometric series to study how interest -rates affect values of streams of dollar payments that extend over time. +我们可以应用几何级数公式来研究利率如何影响延续一段时间的美元支付流的价值。 -We work in discrete time and assume that $t = 0, 1, 2, \ldots$ -indexes time. +我们在离散时间工作,并假设 $t = 0, 1, 2, \ldots$ 表示时间。 -We let $r \in (0,1)$ be a one-period **net nominal interest rate** +我们设 $r \in (0,1)$ 为一个时期的 **净名义利率** -- if the nominal interest rate is $5$ percent, - then $r= .05$ +- 如果名义利率是 $5$ 百分比, + 那么 $r= .05$ -A one-period **gross nominal interest rate** $R$ is defined as +一个时期的 **总名义利率** $R$ 定义为 $$ R = 1 + r \in (1, 2) $$ -- if $r=.05$, then $R = 1.05$ +- 如果 $r=.05$,那么 $R = 1.05$ -**Remark:** The gross nominal interest rate $R$ is an **exchange -rate** or **relative price** of dollars at between times $t$ and -$t+1$. The units of $R$ are dollars at time $t+1$ per -dollar at time $t$. +**备注:** 总名义利率 $R$ 是一个 **汇率** 或 **相对价格**,表示在时间 $t$ 和 $t+1$ 之间的美元。$R$ 的单位是时间 $t+1$ 的美元每时间 $t$ 的美元。 -When people borrow and lend, they trade dollars now for dollars later or -dollars later for dollars now. +当人们借贷时,他们用现在的美元换取以后的美元,或者用以后的美元换取现在的美元。 -The price at which these exchanges occur is the gross nominal interest -rate. +这些交换发生的价格是总名义利率。 -- If I sell $x$ dollars to you today, you pay me $R x$ - dollars tomorrow. -- This means that you borrowed $x$ dollars for me at a gross - interest rate $R$ and a net interest rate $r$. +- 如果我今天卖给你 $x$ 美元,你明天支付我 $R x$ + 美元。 +- 这意味着你以总利率 $R$ 和净利率 $r$ 向我借了 $x$ 美元。 -We assume that the net nominal interest rate $r$ is fixed over -time, so that $R$ is the gross nominal interest rate at times -$t=0, 1, 2, \ldots$. +我们假设净名义利率 $r$ 随时间固定,因此 $R$ 是时间 $t=0, 1, 2, \ldots$ 的总名义利率。 -Two important geometric sequences are +两个重要的几何序列是 ```{math} :label: geom1 @@ -481,7 +416,7 @@ Two important geometric sequences are 1, R, R^2, \cdots ``` -and +和 ```{math} :label: geom2 @@ -489,70 +424,66 @@ and 1, R^{-1}, R^{-2}, \cdots ``` -Sequence {eq}`geom1` tells us how dollar values of an investment **accumulate** -through time. +序列 {eq}`geom1` 告诉我们投资的美元价值如何随时间 **累积**。 -Sequence {eq}`geom2` tells us how to **discount** future dollars to get their -values in terms of today's dollars. +序列 {eq}`geom2` 告诉我们如何 **折现** 未来的美元以获得其以今天的美元计的价值。 -### Accumulation +### 累积 -Geometric sequence {eq}`geom1` tells us how one dollar invested and re-invested -in a project with gross one period nominal rate of return accumulates +几何序列 {eq}`geom1` 告诉我们一美元投资并再投资于一个项目,其总名义回报率如何累积 -- here we assume that net interest payments are reinvested in the - project -- thus, $1$ dollar invested at time $0$ pays interest - $r$ dollars after one period, so we have $r+1 = R$ - dollars at time$1$ -- at time $1$ we reinvest $1+r =R$ dollars and receive interest - of $r R$ dollars at time $2$ plus the *principal* - $R$ dollars, so we receive $r R + R = (1+r)R = R^2$ - dollars at the end of period $2$ -- and so on +- 这里我们假设净利息支付再投资于项目 +- 因此,时间 $0$ 投资的 $1$ 美元在一个时期后支付利息 + $r$ 美元,所以我们在时间 $1$ 有 $r+1 = R$ + 美元 +- 在时间 $1$ 我们再投资 $1+r =R$ 美元,并在时间 $2$ 收到利息 + $r R$ 美元加上 *本金* + $R$ 美元,所以我们在第 $2$ 期末收到 $r R + R = (1+r)R = R^2$ + 美元 +- 依此类推 -Evidently, if we invest $x$ dollars at time $0$ and -reinvest the proceeds, then the sequence +显然,如果我们在时间 $0$ 投资 $x$ 美元并 +再投资收益,那么序列 $$ x , xR , x R^2, \cdots $$ -tells how our account accumulates at dates $t=0, 1, 2, \ldots$. +告诉我们账户在时间 $t=0, 1, 2, \ldots$ 的累积情况。 -### Discounting +### 折现 -Geometric sequence {eq}`geom2` tells us how much future dollars are worth in terms of today's dollars. +几何序列 {eq}`geom2` 告诉我们未来的美元在今天的美元中的价值。 -Remember that the units of $R$ are dollars at $t+1$ per -dollar at $t$. +记住 $R$ 的单位是时间 $t+1$ 的美元每 +时间 $t$ 的美元。 -It follows that +因此 -- the units of $R^{-1}$ are dollars at $t$ per dollar at $t+1$ -- the units of $R^{-2}$ are dollars at $t$ per dollar at $t+2$ -- and so on; the units of $R^{-j}$ are dollars at $t$ per - dollar at $t+j$ +- $R^{-1}$ 的单位是时间 $t$ 的美元每时间 $t+1$ 的美元 +- $R^{-2}$ 的单位是时间 $t$ 的美元每时间 $t+2$ 的美元 +- 依此类推;$R^{-j}$ 的单位是时间 $t$ 的美元每 + 时间 $t+j$ 的美元 -So if someone has a claim on $x$ dollars at time $t+j$, it -is worth $x R^{-j}$ dollars at time $t$ (e.g., today). +所以如果某人在时间 $t+j$ 拥有 $x$ 美元的债权, +那么它在时间 $t$(例如今天)值 $x R^{-j}$ 美元。 -### Application to asset pricing +### 资产定价的应用 -A **lease** requires a payments stream of $x_t$ dollars at -times $t = 0, 1, 2, \ldots$ where +一个 **租赁** 需要在时间 $t = 0, 1, 2, \ldots$ 支付 +$x_t$ 美元,其中 $$ x_t = G^t x_0 $$ -where $G = (1+g)$ and $g \in (0,1)$. +其中 $G = (1+g)$ 且 $g \in (0,1)$。 -Thus, lease payments increase at $g$ percent per period. +因此,租赁支付每期增加 $g$ 百分比。 -For a reason soon to be revealed, we assume that $G < R$. +由于一个即将揭示的原因,我们假设 $G < R$。 -The **present value** of the lease is +租赁的 **现值** 是 $$ \begin{aligned} p_0 & = x_0 + x_1/R + x_2/(R^2) + \cdots \\ @@ -560,23 +491,23 @@ $$ & = x_0 \frac{1}{1 - G R^{-1}} \end{aligned} $$ -where the last line uses the formula for an infinite geometric series. +最后一行使用了无限几何级数的公式。 -Recall that $R = 1+r$ and $G = 1+g$ and that $R > G$ -and $r > g$ and that $r$ and $g$ are typically small -numbers, e.g., .05 or .03. +回想一下 $R = 1+r$ 和 $G = 1+g$ 并且 $R > G$ +和 $r > g$ 并且 $r$ 和 $g$ 通常是小 +数,例如 .05 或 .03。 -Use the [Taylor series](https://en.wikipedia.org/wiki/Taylor_series) of $\frac{1}{1+r}$ about $r=0$, -namely, +使用 $r=0$ 附近的 $\frac{1}{1+r}$ 的泰勒级数, +即, $$ \frac{1}{1+r} = 1 - r + r^2 - r^3 + \cdots $$ -and the fact that $r$ is small to approximate -$\frac{1}{1+r} \approx 1 - r$. +并且 $r$ 很小以近似 +$\frac{1}{1+r} \approx 1 - r$。 -Use this approximation to write $p_0$ as +使用此近似来写 $p_0$ 为 $$ \begin{aligned} @@ -587,99 +518,93 @@ $$ \end{aligned} $$ -where the last step uses the approximation $r g \approx 0$. +最后一步使用了近似 $r g \approx 0$。 -The approximation +近似 $$ p_0 = \frac{x_0 }{r -g } $$ -is known as the **Gordon formula** for the present value or current -price of an infinite payment stream $x_0 G^t$ when the nominal -one-period interest rate is $r$ and when $r > g$. +被称为 **戈登公式**,用于当名义 +单期利率为 $r$ 且 $r > g$ 时无限支付流 $x_0 G^t$ 的现值或当前价格。 -We can also extend the asset pricing formula so that it applies to finite leases. +我们还可以扩展资产定价公式,使其适用于有限租赁。 -Let the payment stream on the lease now be $x_t$ for $t= 1,2, \dots,T$, where again +现在让租赁的支付流为 $x_t$,其中 $t= 1,2, \dots,T$,再次 $$ x_t = G^t x_0 $$ -The present value of this lease is: +该租赁的现值为: $$ \begin{aligned} \begin{split}p_0&=x_0 + x_1/R + \dots +x_T/R^T \\ &= x_0(1+GR^{-1}+\dots +G^{T}R^{-T}) \\ &= \frac{x_0(1-G^{T+1}R^{-(T+1)})}{1-GR^{-1}} \end{split}\end{aligned} $$ -Applying the Taylor series to $R^{-(T+1)}$ about $r=0$ we get: +将泰勒级数应用于 $R^{-(T+1)}$ 关于 $r=0$ 我们得到: $$ \frac{1}{(1+r)^{T+1}}= 1-r(T+1)+\frac{1}{2}r^2(T+1)(T+2)+\dots \approx 1-r(T+1) $$ -Similarly, applying the Taylor series to $G^{T+1}$ about $g=0$: +类似地,将泰勒级数应用于 $G^{T+1}$ 关于 $g=0$: $$ (1+g)^{T+1} = 1+(T+1)g+\frac{T(T+1)}{2!}g^2+\frac{(T-1)T(T+1)}{3!}g^3+\dots \approx 1+ (T+1)g $$ -Thus, we get the following approximation: +因此,我们得到以下近似值: $$ p_0 =\frac{x_0(1-(1+(T+1)g)(1-r(T+1)))}{1-(1-r)(1+g) } $$ -Expanding: +展开: $$ -\begin{aligned} p_0 &=\frac{x_0(1-1+(T+1)^2 rg +r(T+1)-g(T+1))}{1-1+r-g+rg} \\&=\frac{x_0(T+1)((T+1)rg+r-g)}{r-g+rg} \\ &= \frac{x_0(T+1)(r-g)}{r-g + rg}+\frac{x_0rg(T+1)^2}{r-g+rg}\\ &\approx \frac{x_0(T+1)(r-g)}{r-g}+\frac{x_0rg(T+1)}{r-g}\\ &= x_0(T+1) + \frac{x_0rg(T+1)}{r-g} \end{aligned} +\begin{aligned} p_0 &=\frac{x_0(1-1+(T+1)^2 rg -r(T+1)+g(T+1))}{1-1+r-g+rg} \\&=\frac{x_0(T+1)((T+1)rg+r-g)}{r-g+rg} \\ &\approx \frac{x_0(T+1)(r-g)}{r-g}+\frac{x_0rg(T+1)}{r-g}\\ &= x_0(T+1) + \frac{x_0rg(T+1)}{r-g} \end{aligned} $$ -We could have also approximated by removing the second term -$rgx_0(T+1)$ when $T$ is relatively small compared to -$1/(rg)$ to get $x_0(T+1)$ as in the finite stream -approximation. +当 $T$ 相对于 $1/(rg)$ 较小时,我们也可以通过去除第二项 $rgx_0(T+1)$ 来近似,以获得有限流近似中的 $x_0(T+1)$。 -We will plot the true finite stream present-value and the two -approximations, under different values of $T$, and $g$ and $r$ in Python. +我们将在 Python 中绘制不同 $T$、$g$ 和 $r$ 值下的真实有限流现值和两个近似值。 -First we plot the true finite stream present-value after computing it -below +首先,我们在下面计算后绘制真实的有限流现值。 ```{code-cell} ipython3 -# True present value of a finite lease +# 有限租赁的真实现值 def finite_lease_pv_true(T, g, r, x_0): G = (1 + g) R = (1 + r) return (x_0 * (1 - G**(T + 1) * R**(-T - 1))) / (1 - G * R**(-1)) -# First approximation for our finite lease +# 有限租赁的第一次近似 def finite_lease_pv_approx_1(T, g, r, x_0): p = x_0 * (T + 1) + x_0 * r * g * (T + 1) / (r - g) return p -# Second approximation for our finite lease +# 有限租赁的第二次近似 def finite_lease_pv_approx_2(T, g, r, x_0): return (x_0 * (T + 1)) -# Infinite lease +# 无限期租赁 def infinite_lease(g, r, x_0): G = (1 + g) R = (1 + r) return x_0 / (1 - G * R**(-1)) ``` -Now that we have defined our functions, we can plot some outcomes. +现在我们已经定义了我们的函数,我们可以绘制一些结果。 -First we study the quality of our approximations +首先,我们研究我们的近似质量。 ```{code-cell} ipython3 --- mystnb: figure: - caption: "Finite lease present value $T$ periods ahead" + caption: "有限租赁的现值 $T$ 期前" name: finite_lease_present_value --- def plot_function(axes, x_vals, func, args): @@ -696,64 +621,59 @@ our_args = (T, g, r, x_0) funcs = [finite_lease_pv_true, finite_lease_pv_approx_1, finite_lease_pv_approx_2] - # the three functions we want to compare + # 三个我们想要比较的函数 fig, ax = plt.subplots() for f in funcs: plot_function(ax, T, f, our_args) ax.legend() -ax.set_xlabel('$T$ Periods Ahead') -ax.set_ylabel('Present Value, $p_0$') +ax.set_xlabel('$T$ 期之后') +ax.set_ylabel('现值, $p_0$') plt.show() ``` +显然,我们的近似在 $T$ 的小值时表现良好。 -Evidently our approximations perform well for small values of $T$. - -However, holding $g$ and r fixed, our approximations deteriorate as $T$ increases. +然而,保持 $g$ 和 $r$ 不变,我们的近似在 $T$ 增加时会恶化。 -Next we compare the infinite and finite duration lease present values -over different lease lengths $T$. +接下来,我们比较不同租赁期限 $T$ 的无限期和有限期租赁现值。 ```{code-cell} ipython3 --- mystnb: figure: - caption: "Infinite and finite lease present value $T$ periods ahead" + caption: "无限期和有限期租赁的现值 $T$ 期前" name: infinite_and_finite_lease_present_value --- -# Convergence of infinite and finite +# 无限和有限的收敛性 T_max = 1000 T = np.arange(0, T_max+1) fig, ax = plt.subplots() f_1 = finite_lease_pv_true(T, g, r, x_0) f_2 = np.full(T_max+1, infinite_lease(g, r, x_0)) -ax.plot(T, f_1, label='T-period lease PV') -ax.plot(T, f_2, '--', label='Infinite lease PV') -ax.set_xlabel('$T$ Periods Ahead') -ax.set_ylabel('Present Value, $p_0$') +ax.plot(T, f_1, label='T-期租赁现值') +ax.plot(T, f_2, '--', label='无限期租赁现值') +ax.set_xlabel('$T$ 期之后') +ax.set_ylabel('现值, $p_0$') ax.legend() plt.show() ``` +上图显示了当期限 $T \rightarrow +\infty$ 时,期限为 $T$ 的租赁价值接近永久租赁的价值。 -The graph above shows how as duration $T \rightarrow +\infty$, -the value of a lease of duration $T$ approaches the value of a -perpetual lease. +现在我们考虑 $r$ 和 $g$ 协变时的两种不同情况。 -Now we consider two different views of what happens as $r$ and -$g$ covary ```{code-cell} ipython3 --- mystnb: figure: - caption: "Value of lease of length $T$" + caption: "$T$-期租赁的价值" name: value_of_lease --- -# First view -# Changing r and g +# 第一种情况 +# 改变 r 和 g fig, ax = plt.subplots() -ax.set_ylabel('Present Value, $p_0$') -ax.set_xlabel('$T$ periods ahead') +ax.set_ylabel('现值, $p_0$') +ax.set_xlabel('$T$-期之后') T_max = 10 T=np.arange(0, T_max+1) @@ -765,25 +685,20 @@ for r, g, comp in zip(rs, gs, comparisons): ax.legend() plt.show() ``` +此图为为什么条件 $r > g$ 是必要的提供了一个重要提示,如果期限为 $T = +\infty$ 的租赁要有有限的价值。 -This graph gives a big hint for why the condition $r > g$ is -necessary if a lease of length $T = +\infty$ is to have finite -value. +对于喜欢 3D 图表的粉丝来说,以下图表也能说明同样的观点。 -For fans of 3-d graphs the same point comes through in the following -graph. - -If you aren't enamored of 3-d graphs, feel free to skip the next -visualization! +如果你不喜欢 3D 图表,可以跳过下一个可视化! ```{code-cell} ipython3 --- mystnb: figure: - caption: "Three period lease PV with varying $g$ and $r$" + caption: "三期租赁现值,$g$ 和 $r$ 变化" name: three_period_lease_PV --- -# Second view +# 第二种情况 fig = plt.figure(figsize = [16, 5]) T = 3 ax = plt.subplot(projection='3d') @@ -793,7 +708,7 @@ g = np.arange(0.011, 0.991, 0.005) rr, gg = np.meshgrid(r, g) z = finite_lease_pv_true(T, gg, rr, x_0) -# Removes points where undefined +# 移除未定义的点 same = (rr == gg) z[same] = np.nan surf = ax.plot_surface(rr, gg, z, cmap=cm.coolwarm, @@ -801,68 +716,58 @@ surf = ax.plot_surface(rr, gg, z, cmap=cm.coolwarm, fig.colorbar(surf, shrink=0.5, aspect=5) ax.set_xlabel('$r$') ax.set_ylabel('$g$') -ax.set_zlabel('Present Value, $p_0$') +ax.set_zlabel('现值, $p_0$') ax.view_init(20, 8) plt.show() ``` +我们可以用一些微积分来研究租赁现值 $p_0$ 如何随 $r$ 和 $g$ 变化。 -We can use a little calculus to study how the present value $p_0$ -of a lease varies with $r$ and $g$. - -We will use a library called [SymPy](https://www.sympy.org/). +我们将使用一个名为 SymPy 的库。 -SymPy enables us to do symbolic math calculations including -computing derivatives of algebraic equations. +SymPy 使我们能够进行符号数学计算,包括计算代数方程的导数。 -We will illustrate how it works by creating a symbolic expression that -represents our present value formula for an infinite lease. +我们将通过创建一个符号表达式来说明它的工作原理,该表达式表示我们无限期租赁的现值公式。 -After that, we'll use SymPy to compute derivatives +之后,我们将使用 SymPy 来计算导数。 ```{code-cell} ipython3 -# Creates algebraic symbols that can be used in an algebraic expression +# 创建可用于代数表达式的代数符号 g, r, x0 = sym.symbols('g, r, x0') G = (1 + g) R = (1 + r) p0 = x0 / (1 - G * R**(-1)) init_printing(use_latex='mathjax') -print('Our formula is:') +print('我们的公式是:') p0 ``` ```{code-cell} ipython3 -print('dp0 / dg is:') +print('dp0 / dg 为:') dp_dg = sym.diff(p0, g) dp_dg ``` ```{code-cell} ipython3 -print('dp0 / dr is:') +print('dp0 / dr 为:') dp_dr = sym.diff(p0, r) dp_dr ``` +我们可以看到,只要 $r>g$,$r>0$ 且 $g>0$ 并且 $x_0$ 为正,$\frac{\partial p_0}{\partial r}<0$,所以 $\frac{\partial p_0}{\partial r}$ 将始终为负。 -We can see that for $\frac{\partial p_0}{\partial r}<0$ as long as -$r>g$, $r>0$ and $g>0$ and $x_0$ is positive, -so $\frac{\partial p_0}{\partial r}$ will always be negative. +同样地,只要 $r>g$,$r>0$ 且 $g>0$ 并且 $x_0$ 为正,$\frac{\partial p_0}{\partial g}>0$,所以 $\frac{\partial p_0}{\partial g}$ 将始终为正。 -Similarly, $\frac{\partial p_0}{\partial g}>0$ as long as $r>g$, $r>0$ and $g>0$ and $x_0$ is positive, so $\frac{\partial p_0}{\partial g}$ -will always be positive. +## 回到凯恩斯乘数 -## Back to the Keynesian multiplier - -We will now go back to the case of the Keynesian multiplier and plot the -time path of $y_t$, given that consumption is a constant fraction -of national income, and investment is fixed. +现在我们将回到凯恩斯乘数的情况,并绘制 $y_t$ 的时间路径,假设消费是国民收入的一个常数比例,投资是固定的。 ```{code-cell} ipython3 --- mystnb: figure: - caption: "Path of aggregate output tver time" + caption: "总产出的时间路径" name: path_of_aggregate_output_over_time --- -# Function that calculates a path of y +# 计算 $y$ 路径的函数 def calculate_y(i, b, g, T, y_init): y = np.zeros(T+1) y[0] = i + b * y_init + g @@ -870,10 +775,10 @@ def calculate_y(i, b, g, T, y_init): y[t] = b * y[t-1] + i + g return y -# Initial values +# 初始值 i_0 = 0.3 g_0 = 0.3 -# 2/3 of income goes towards consumption +# 2/3 的收入为消费 b = 2/3 y_init = 0 T = 100 @@ -886,19 +791,15 @@ ax.plot(np.arange(0, T+1), calculate_y(i_0, b, g_0, T, y_init)) ax.hlines(i_0 / (1 - b) + g_0 / (1 - b), xmin=-1, xmax=101, linestyles='--') plt.show() ``` +在这个模型中,收入随着时间的推移而增长,直到逐渐收敛到收入的无限几何级数和。 -In this model, income grows over time, until it gradually converges to -the infinite geometric series sum of income. - -We now examine what will -happen if we vary the so-called **marginal propensity to consume**, -i.e., the fraction of income that is consumed +现在我们来研究如果我们改变所谓的**边际消费倾向**,即消费的收入比例,会发生什么。 ```{code-cell} ipython3 --- mystnb: figure: - caption: "Changing consumption as a fraction of income" + caption: "将消费作为收入的一部分进行变化" name: changing_consumption_as_fraction_of_income --- bs = (1/3, 2/3, 5/6, 0.9) @@ -914,16 +815,15 @@ ax.legend() plt.show() ``` -Increasing the marginal propensity to consume $b$ increases the -path of output over time. +增加边际消费倾向 $b$ 会增加随时间推移的产出路径。 -Now we will compare the effects on output of increases in investment and government spending. +现在我们将比较投资和政府支出增加对产出的影响。 ```{code-cell} ipython3 --- mystnb: figure: - caption: "Different increase on output" + caption: "对产出的不同增加" name: different_increase_on_output --- fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 10)) @@ -940,15 +840,63 @@ for g in values: ax2.plot(x, y, label=f"g={g}") axes = ax1, ax2 -param_labels = "Investment", "Government Spending" +param_labels = "投资", "政府支出" for ax, param in zip(axes, param_labels): - ax.set_title(f'An Increase in {param} on Output') + ax.set_title(f'产出增加了 {param}') ax.legend(loc ="lower right") ax.set_ylabel('$y_t$') ax.set_xlabel('$t$') plt.show() ``` +请注意,无论政府支出从 0.3 增加到 0.4,还是投资从 0.3 增加到 0.4,图表中的变化都是相同的。 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -Notice here, whether government spending increases from 0.3 to 0.4 or -investment increases from 0.3 to 0.4, the shifts in the graphs are -identical. diff --git a/lectures/greek_square.md b/lectures/greek_square.md index 8bf4a98..1bde647 100644 --- a/lectures/greek_square.md +++ b/lectures/greek_square.md @@ -11,248 +11,207 @@ kernelspec: name: python3 --- -# Computing Square Roots - - -## Introduction - -Chapter 24 of {cite}`russell2004history` about early Greek mathematics and astronomy contains this -fascinating passage: +# 计算平方根 +## 引言 +{cite}`russell2004history` 第24章关于早期希腊数学和天文学的内容中包含了这段引人入胜的段落: ```{epigraph} - The square root of 2, which was the first irrational to be discovered, was known to the early Pythagoreans, and ingenious methods of approximating to its value were discovered. The best was as follows: Form two columns of numbers, which we will call the $a$'s and the $b$'s; each starts with a $1$. The next $a$, at each stage, is formed by adding the last $a$ and the $b$ already obtained; the next $b$ is formed by adding twice the previous $a$ to the previous $b$. The first 6 pairs so obtained are $(1,1), (2,3), (5,7), (12,17), (29,41), (70,99)$. In each pair, $2 a - b$ is $1$ or $-1$. Thus $b/a$ is nearly the square root of two, and at each fresh step it gets nearer. For instance, the reader may satisy himself that the square of $99/70$ is very nearly equal to $2$. + 2的平方根是第一个被发现的无理数,早期的毕达哥拉斯学派就已知晓,并发明了巧妙的方法来逼近其值。最佳方法如下:形成两列数字,我们称之为 $a$ 列和 $b$ 列;每列都从1开始。在每一步中,下一个 $a$ 是通过加上最后一个 $a$ 和已获得的 $b$ 形成的;下一个 $b$ 是通过加上前一个 $a$ 的两倍和前一个 $b$ 形成的。这样获得的前6对数是 $(1,1), (2,3), (5,7), (12,17), (29,41), (70,99)$。在每一对中,$2a - b$ 等于1或-1。因此,$b/a$ 接近2的平方根,并且每进行一步就会更接近。例如,读者可以自己验证 $99/70$ 的平方非常接近2。 ``` + +本讲座深入研究了这种古老的计算平方根方法,运用了我们在之前quantecon讲座中学到的一些矩阵代数知识。 +特别地,本讲座可以被视为 {doc}`eigen_I` 的续篇。 +它提供了一个例子,说明特征向量如何分离出*不变子空间*,帮助构造和分析线性差分方程的解。 +当向量 $x_t$ 从不变子空间开始时,迭代差分方程会使 $x_{t+j}$ 对所有 $j \geq 1$ 保持在该子空间中。 +不变子空间方法在应用经济动力学中广泛使用,例如在 {doc}`money_inflation` 讲座中。 +我们的方法是用一个古老的例子来说明这种方法,这个例子是古希腊数学家用来计算正整数平方根的。 -This lecture drills down and studies this ancient method for computing square roots by using some of the matrix algebra that we've learned in earlier quantecon lectures. - -In particular, this lecture can be viewed as a sequel to {doc}`eigen_I`. - -It provides an example of how eigenvectors isolate *invariant subspaces* that help construct and analyze solutions of linear difference equations. - -When vector $x_t$ starts in an invariant subspace, iterating the different equation keeps $x_{t+j}$ -in that subspace for all $j \geq 1$. - -Invariant subspace methods are used throughout applied economic dynamics, for example, in the lecture {doc}`money_inflation`. - -Our approach here is to illustrate the method with an ancient example, one that ancient Greek mathematicians used to compute square roots of positive integers. - -## Perfect squares and irrational numbers - -An integer is called a **perfect square** if its square root is also an integer. - -An ordered sequence of perfect squares starts with +## 完全平方数和无理数 +如果一个整数的平方根也是整数,则称该整数为**完全平方数**。 +完全平方数的有序序列从以下数字开始: $$ 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, \ldots $$ -If an integer is not a perfect square, then its square root is an irrational number -- i.e., it cannot be expressed as a ratio of two integers, and its decimal expansion is indefinite. - -The ancient Greeks invented an algorithm to compute square roots of integers, including integers that are not perfect squares. - -Their method involved - - * computing a particular sequence of integers $\{y_t\}_{t=0}^\infty$; +如果一个整数不是完全平方数,那么它的平方根就是一个无理数 -- 即它不能表示为两个整数的比值,其小数展开是无限的。 +古希腊人发明了一种算法来计算整数的平方根,包括那些不是完全平方数的整数。 +他们的方法涉及: + * 计算特定的整数序列 $\{y_t\}_{t=0}^\infty$; - * computing $\lim_{t \rightarrow \infty} \left(\frac{y_{t+1}}{y_t}\right) = \bar r$; + * 计算 $\lim_{t \rightarrow \infty} \left(\frac{y_{t+1}}{y_t}\right) = \bar r$; - * deducing the desired square root from $\bar r$. + * 从 $\bar r$ 推导出所需的平方根。 -In this lecture, we'll describe this method. +在本讲座中,我们将描述这种方法。 +我们还将使用不变子空间来描述这种方法的变体,这些变体更快。 -We'll also use invariant subspaces to describe variations on this method that are faster. - -## Second-order linear difference equations - -Before telling how the ancient Greeks computed square roots, we'll provide a quick introduction -to second-order linear difference equations. - -We'll study the following second-order linear difference equation +## 二阶线性差分方程 +在讲述古希腊人如何计算平方根之前,我们将简要介绍二阶线性差分方程。 +我们将研究以下二阶线性差分方程: $$ y_t = a_1 y_{t-1} + a_2 y_{t-2}, \quad t \geq 0 $$ (eq:2diff1) -where $(y_{-1}, y_{-2})$ is a pair of given initial conditions. - -Equation {eq}`eq:2diff1` is actually an infinite number of linear equations in the sequence -$\{y_t\}_{t=0}^\infty$. - -There is one equation each for $t = 0, 1, 2, \ldots$. - -We could follow an approach taken in the lecture on {doc}`present values` and stack all of these equations into a single matrix equation that we would then solve by using matrix inversion. +其中 $(y_{-1}, y_{-2})$ 是给定的一对初始条件。 +方程 {eq}`eq:2diff1` 实际上是序列 $\{y_t\}_{t=0}^\infty$ 的无限多个线性方程。 +对于 $t = 0, 1, 2, \ldots$ 中的每一个 $t$,都有一个方程。 +我们可以采用 {doc}`现值` 讲座中的方法,将所有这些方程堆叠成一个单一的矩阵方程,然后使用矩阵求逆来求解。 ```{note} -In the present instance, the matrix equation would multiply a countably infinite dimensional square matrix by a countably infinite dimensional vector. With some qualifications, matrix multiplication and inversion tools apply to such an equation. +在当前情况下,矩阵方程将对一个可数无穷维的方阵与一个可数无穷维的向量进行乘法运算。在一些限定条件下,矩阵乘法和求逆工具适用于这样的方程。 ``` -But we won't pursue that approach here. - - -Instead, we'll seek to find a time-invariant function that *solves* our difference equation, meaning -that it provides a formula for a $\{y_t\}_{t=0}^\infty$ sequence that satisfies -equation {eq}`eq:2diff1` for each $t \geq 0$. - -We seek an expression for $y_t, t \geq 0$ as functions of the initial conditions $(y_{-1}, y_{-2})$: +但我们不会在这里采用这种方法。 +相反,我们将寻找一个时不变函数来*解决*我们的差分方程,这意味着 +它为满足方程{eq}`eq:2diff1`的$\{y_t\}_{t=0}^\infty$序列提供了一个公式,适用于每个$t \geq 0$。 +我们寻求$y_t, t \geq 0$的表达式,作为初始条件$(y_{-1}, y_{-2})$的函数: $$ -y_t = g((y_{-1}, y_{-2});t), \quad t \geq 0. +y_t = g((y_{-1}, y_{-2});t), \quad t \geq 0. $$ (eq:2diff2) -We call such a function $g$ a *solution* of the difference equation {eq}`eq:2diff1`. - -One way to discover a solution is to use a guess and verify method. - -We shall begin by considering a special initial pair of initial conditions -that satisfy +我们称这样的函数$g$为差分方程{eq}`eq:2diff1`的*解*。 +发现解的一种方法是使用猜测和验证法。 +我们将从考虑一对特殊的初始条件开始, +这对初始条件满足 $$ y_{-1} = \delta y_{-2} $$ (eq:2diff3) -where $\delta$ is a scalar to be determined. - -For initial condition that satisfy {eq}`eq:2diff3` -equation {eq}`eq:2diff1` impllies that +其中$\delta$是待确定的标量。 +对于满足{eq}`eq:2diff3`的初始条件, +方程{eq}`eq:2diff1`意味着 $$ y_0 = \left(a_1 + \frac{a_2}{\delta}\right) y_{-1}. $$ (eq:2diff4) -We want +我们希望 $$ \left(a_1 + \frac{a_2}{\delta}\right) = \delta $$ (eq:2diff5) -which we can rewrite as the *characteristic equation* +我们可以将其重写为*特征方程* $$ \delta^2 - a_1 \delta - a_2 = 0. $$ (eq:2diff6) -Applying the quadratic formula to solve for the roots of {eq}`eq:2diff6` we find that +应用二次公式求解{eq}`eq:2diff6`的根,我们得到 $$ \delta = \frac{ a_1 \pm \sqrt{a_1^2 + 4 a_2}}{2}. $$ (eq:2diff7) -For either of the two $\delta$'s that satisfy equation {eq}`eq:2diff7`, -a solution of difference equation {eq}`eq:2diff1` is +对于满足方程{eq}`eq:2diff7`的两个$\delta$中的任一个, +差分方程{eq}`eq:2diff1`的解为 $$ y_t = \delta^t y_0 , \forall t \geq 0 $$ (eq:2diff8) -provided that we set +前提是我们设置 $$ -y_0 = \delta y_{-1} . +y_0 = \delta y_{-1} . $$ -The *general* solution of difference equation {eq}`eq:2diff1` takes the form +差分方程{eq}`eq:2diff1`的*一般*解形式为 $$ y_t = \eta_1 \delta_1^t + \eta_2 \delta_2^t $$ (eq:2diff9) -where $\delta_1, \delta_2$ are the two solutions {eq}`eq:2diff7` of the characteristic equation {eq}`eq:2diff6`, and $\eta_1, \eta_2$ are two constants chosen to satisfy - +其中$\delta_1, \delta_2$是特征方程{eq}`eq:2diff6`的两个解{eq}`eq:2diff7`,而$\eta_1, \eta_2$是两个常数,选择它们以满足 + $$ - \begin{bmatrix} y_{-1} \cr y_{-2} \end{bmatrix} = \begin{bmatrix} \delta_1^{-1} & \delta_2^{-1} \cr \delta_1^{-2} & \delta_2^{-2} \end{bmatrix} \begin{bmatrix} \eta_1 \cr \eta_2 \end{bmatrix} + \begin{bmatrix} y_{-1} \cr y_{-2} \end{bmatrix} = \begin{bmatrix} \delta_1^{-1} & \delta_2^{-1} \cr \delta_1^{-2} & \delta_2^{-2} \end{bmatrix} \begin{bmatrix} \eta_1 \cr \eta_2 \end{bmatrix} $$ (eq:2diff10) -or +或 $$ -\begin{bmatrix} \eta_1 \cr \eta_2 \end{bmatrix} = \begin{bmatrix} \delta_1^{-1} & \delta_2^{-1} \cr \delta_1^{-2} & \delta_2^{-2} \end{bmatrix}^{-1} \begin{bmatrix} y_{-1} \cr y_{-2} \end{bmatrix} +\begin{bmatrix} \eta_1 \cr \eta_2 \end{bmatrix} = \begin{bmatrix} \delta_1^{-1} & \delta_2^{-1} \cr \delta_1^{-2} & \delta_2^{-2} \end{bmatrix}^{-1} \begin{bmatrix} y_{-1} \cr y_{-2} \end{bmatrix} $$ (eq:2diff11) -Sometimes we are free to choose the initial conditions $(y_{-1}, y_{-2})$, in which case we -use system {eq}`eq:2diff10` to find the associated $(\eta_1, \eta_2)$. - -If we choose $(y_{-1}, y_{-2})$ to set $(\eta_1, \eta_2) = (1, 0)$, then $y_t = \delta_1^t$ for all $t \geq 0$. - - -If we choose $(y_{-1}, y_{-2})$ to set $(\eta_1, \eta_2) = (0, 1)$, then $y_t = \delta_2^t$ for all $t \geq 0$. - -Soon we'll relate the preceding calculations to components an eigen decomposition of a transition matrix that represents difference equation {eq}`eq:2diff1` in a very convenient way. +有时我们可以自由选择初始条件$(y_{-1}, y_{-2})$,在这种情况下,我们 +使用系统{eq}`eq:2diff10`来找到相关的$(\eta_1, \eta_2)$。 +如果我们选择$(y_{-1}, y_{-2})$使$(\eta_1, \eta_2) = (1, 0)$,那么对于所有$t \geq 0$,$y_t = \delta_1^t$。 +如果我们选择$(y_{-1}, y_{-2})$使$(\eta_1, \eta_2) = (0, 1)$,那么对于所有$t \geq 0$,$y_t = \delta_2^t$。 +很快我们将把前面的计算与表示差分方程{eq}`eq:2diff1`的转移矩阵的特征分解的组成部分联系起来,这种方式非常方便。 +在我们描述古希腊人如何计算不是完全平方的正整数的平方根之前,我们将先转向这个问题。 -We'll turn to that after we describe how Ancient Greeks figured out how to compute square roots of positive integers that are not perfect squares. +## 古希腊人的算法 +设 $\sigma$ 为大于1的正整数。 -## Algorithm of the Ancient Greeks +因此 $\sigma \in {\mathcal I} \equiv \{2, 3, \ldots \}$。 -Let $\sigma$ be a positive integer greater than $1$. +我们想要一个算法来计算 $\sigma \in {\mathcal I}$ 的平方根。 -So $\sigma \in {\mathcal I} \equiv \{2, 3, \ldots \}$. +如果 $\sqrt{\sigma} \in {\mathcal I}$,则 $\sigma$ 被称为*完全平方数*。 -We want an algorithm to compute the square root of $\sigma \in {\mathcal I}$. +如果 $\sqrt{\sigma} \not\in {\mathcal I}$,事实证明它是无理数。 -If $\sqrt{\sigma} \in {\mathcal I}$, $\sigma $ is said to be a *perfect square*. +古希腊人使用递归算法来计算非完全平方数的整数的平方根。 -If $\sqrt{\sigma} \not\in {\mathcal I}$, it turns out that it is irrational. - -Ancient Greeks used a recursive algorithm to compute square roots of integers that are not perfect squares. - -The algorithm iterates on a second-order linear difference equation in the sequence $\{y_t\}_{t=0}^\infty$: +该算法在序列 $\{y_t\}_{t=0}^\infty$ 的二阶线性差分方程上迭代: $$ y_{t} = 2 y_{t-1} - (1 - \sigma) y_{t-2}, \quad t \geq 0 $$ (eq:second_order) -together with a pair of integers that are initial conditions for $y_{-1}, y_{-2}$. +同时还有一对整数作为 $y_{-1}, y_{-2}$ 的初始条件。 -First, we'll deploy some techniques for solving the difference equations that are also deployed in {doc}`dynam:samuelson`. +首先,我们将使用一些解差分方程的技巧,这些技巧在 {doc}`dynam:samuelson` 中也有应用。 -The characteristic equation associated with difference equation {eq}`eq:second_order` is +与差分方程 {eq}`eq:second_order` 相关的特征方程是: $$ c(x) \equiv x^2 - 2 x + (1 - \sigma) = 0 $$ (eq:cha_eq0) -(Notice how this is an instance of equation {eq}`eq:2diff6` above.) +(注意这是上面方程 {eq}`eq:2diff6` 的一个实例。) -Factoring the right side of equation {eq}`eq:cha_eq0`, we obtain +对方程 {eq}`eq:cha_eq0` 右侧进行因式分解,我们得到: $$ c(x)= (x - \lambda_1) (x-\lambda_2) = 0 $$(eq:cha_eq) - -where +其中 $$ c(x) = 0 $$ -for $x = \lambda_1$ or $x = \lambda_2$. - -These two special values of $x$ are sometimes called zeros or roots of $c(x)$. +当 $x = \lambda_1$ 或 $x = \lambda_2$ 时成立。 +这两个特殊的 $x$ 值有时被称为 $c(x)$ 的零点或根。 -By applying the quadratic formula to solve for the roots the characteristic equation -{eq}`eq:cha_eq0`, we find that +通过应用二次公式求解特征方程 {eq}`eq:cha_eq0` 的根,我们发现: $$ \lambda_1 = 1 + \sqrt{\sigma}, \quad \lambda_2 = 1 - \sqrt{\sigma}. $$ (eq:secretweapon) -Formulas {eq}`eq:secretweapon` indicate that $\lambda_1$ and $\lambda_2$ are each functions -of a single variable, namely, $\sqrt{\sigma}$, the object that we along with some Ancient Greeks want to compute. +公式 {eq}`eq:secretweapon` 表明 $\lambda_1$ 和 $\lambda_2$ 都是单变量函数,即 $\sqrt{\sigma}$,这正是我们和一些古希腊人想要计算的对象。 -Ancient Greeks had an indirect way of exploiting this fact to compute square roots of a positive integer. +古希腊人有一种间接的方法来利用这个事实来计算正整数的平方根。 -They did this by starting from particular initial conditions $y_{-1}, y_{-2}$ and iterating on the difference equation {eq}`eq:second_order`. +他们通过从特定的初始条件 $y_{-1}, y_{-2}$ 开始,然后在差分方程 {eq}`eq:second_order` 上迭代来实现这一点。 - -Solutions of difference equation {eq}`eq:second_order` take the form +差分方程 {eq}`eq:second_order` 的解的形式为: $$ y_t = \lambda_1^t \eta_1 + \lambda_2^t \eta_2 $$ -where $\eta_1$ and $\eta_2$ are chosen to satisfy prescribed initial conditions $y_{-1}, y_{-2}$: +其中 $\eta_1$ 和 $\eta_2$ 被选择以满足规定的初始条件 $y_{-1}, y_{-2}$: $$ \begin{aligned} @@ -261,78 +220,77 @@ $$ \end{aligned} $$(eq:leq_sq) -System {eq}`eq:leq_sq` of simultaneous linear equations will play a big role in the remainder of this lecture. +线性方程组 {eq}`eq:leq_sq` 在本讲座的剩余部分将发挥重要作用。 -Since $\lambda_1 = 1 + \sqrt{\sigma} > 1 > \lambda_2 = 1 - \sqrt{\sigma} $, -it follows that for *almost all* (but not all) initial conditions +由于 $\lambda_1 = 1 + \sqrt{\sigma} > 1 > \lambda_2 = 1 - \sqrt{\sigma}$, +因此对于*几乎所有*(但不是所有)初始条件,有: $$ \lim_{t \rightarrow \infty} \left(\frac{y_{t+1}}{y_t}\right) = 1 + \sqrt{\sigma}. $$ -Thus, +因此, $$ \sqrt{\sigma} = \lim_{t \rightarrow \infty} \left(\frac{y_{t+1}}{y_t}\right) - 1. $$ -However, notice that if $\eta_1 = 0$, then +然而,注意如果 $\eta_1 = 0$,则: $$ \lim_{t \rightarrow \infty} \left(\frac{y_{t+1}}{y_t}\right) = 1 - \sqrt{\sigma} $$ -so that +所以 $$ \sqrt{\sigma} = 1 - \lim_{t \rightarrow \infty} \left(\frac{y_{t+1}}{y_t}\right). $$ -Actually, if $\eta_1 =0$, it follows that +实际上,如果 $\eta_1 =0$,那么: $$ \sqrt{\sigma} = 1 - \left(\frac{y_{t+1}}{y_t}\right) \quad \forall t \geq 0, $$ -so that convergence is immediate and there is no need to take limits. - -Symmetrically, if $\eta_2 =0$, it follows that +因此收敛是立即的,无需取极限。 +对称地,如果 $\eta_2 =0$,那么: $$ \sqrt{\sigma} = \left(\frac{y_{t+1}}{y_t}\right) - 1 \quad \forall t \geq 0 $$ -so again, convergence is immediate, and we have no need to compute a limit. - +所以同样,收敛是立即的,我们不需要计算极限。 -System {eq}`eq:leq_sq` of simultaneous linear equations can be used in various ways. +线性方程组 {eq}`eq:leq_sq` 可以以多种方式使用。 - * we can take $y_{-1}, y_{-2}$ as given initial conditions and solve for $\eta_1, \eta_2$; + * 我们可以将 $y_{-1}, y_{-2}$ 作为给定的初始条件,并求解 $\eta_1, \eta_2$; - * we can instead take $\eta_1, \eta_2$ as given and solve for initial conditions $y_{-1}, y_{-2}$. + * 我们也可以将 $\eta_1, \eta_2$ 作为给定值,并求解初始条件 $y_{-1}, y_{-2}$。 -Notice how we used the second approach above when we set $\eta_1, \eta_2$ either to $(0, 1)$, for example, or $(1, 0)$, for example. +注意我们上面是如何使用第二种方法的,例如,我们将 $\eta_1, \eta_2$ 设为 $(0, 1)$ 或 $(1, 0)$。 -In taking this second approach, we constructed an *invariant subspace* of ${\bf R}^2$. +采用这种第二种方法,我们构造了 ${\bf R}^2$ 的一个*不变子空间*。 -Here is what is going on. +这里发生的情况是: -For $ t \geq 0$ and for most pairs of initial conditions $(y_{-1}, y_{-2}) \in {\bf R}^2$ for equation {eq}`eq:second_order`, $y_t$ can be expressed as a linear combination of $y_{t-1}$ and $y_{t-2}$. +对于 $t \geq 0$ 和方程 {eq}`eq:second_order` 的大多数初始条件对 $(y_{-1}, y_{-2}) \in {\bf R}^2$,$y_t$ 可以表示为 $y_{t-1}$ 和 $y_{t-2}$ 的线性组合。 -But for some special initial conditions $(y_{-1}, y_{-2}) \in {\bf R}^2$, $y_t$ can be expressed as a linear function of $y_{t-1}$ only. +但对于一些特殊的初始条件 $(y_{-1}, y_{-2}) \in {\bf R}^2$,$y_t$ 可以仅表示为 $y_{t-1}$ 的线性函数。 -These special initial conditions require that $y_{-1}$ be a linear function of $y_{-2}$. +这些特殊的初始条件要求 $y_{-1}$ 是 $y_{-2}$ 的线性函数。 -We'll study these special initial conditions soon. +我们很快将研究这些特殊的初始条件。 -But first let's write some Python code to iterate on equation {eq}`eq:second_order` starting from an arbitrary $(y_{-1}, y_{-2}) \in {\bf R}^2$. +但首先让我们编写一些 Python 代码,从任意的 $(y_{-1}, y_{-2}) \in {\bf R}^2$ 开始,在方程 {eq}`eq:second_order` 上迭代。 -## Implementation +## 实现 -We now implement the above algorithm to compute the square root of $\sigma$. +我们现在实现上述算法来计算 $\sigma$ 的平方根。 + +在本讲座中,我们使用以下导入: -In this lecture, we use the following import: ```{code-cell} ipython3 import numpy as np @@ -341,14 +299,14 @@ import matplotlib.pyplot as plt ```{code-cell} ipython3 def solve_λs(coefs): - # Calculate the roots using numpy.roots + # 用 numpy.roots来求根 λs = np.roots(coefs) - # Sort the roots for consistency + # 对根进行排序以保持一致性 return sorted(λs, reverse=True) def solve_η(λ_1, λ_2, y_neg1, y_neg2): - # Solve the system of linear equation + # 对线性系统求解 A = np.array([ [1/λ_1, 1/λ_2], [1/(λ_1**2), 1/(λ_2**2)] @@ -359,116 +317,110 @@ def solve_η(λ_1, λ_2, y_neg1, y_neg2): return ηs def solve_sqrt(σ, coefs, y_neg1, y_neg2, t_max=100): - # Ensure σ is greater than 1 + # 确保 σ 大于 1 if σ <= 1: - raise ValueError("σ must be greater than 1") + raise ValueError("σ 必须大于 1") - # Characteristic roots + # 特征根 λ_1, λ_2 = solve_λs(coefs) - # Solve for η_1 and η_2 + # 求解 η_1 和 η_2 η_1, η_2 = solve_η(λ_1, λ_2, y_neg1, y_neg2) - # Compute the sequence up to t_max + # 计算序列直到 t_max t = np.arange(t_max + 1) y = (λ_1 ** t) * η_1 + (λ_2 ** t) * η_2 - # Compute the ratio y_{t+1} / y_t for large t + # 计算大 t 时的比率 y_{t+1} / y_t sqrt_σ_estimate = (y[-1] / y[-2]) - 1 return sqrt_σ_estimate -# Use σ = 2 as an example +# 用 σ = 2 做个例子 σ = 2 -# Encode characteristic equation +# 特征方程 coefs = (1, -2, (1 - σ)) -# Solve for the square root of σ +# 求 σ 的平方根 sqrt_σ = solve_sqrt(σ, coefs, y_neg1=2, y_neg2=1) -# Calculate the deviation +# 计算误差 dev = abs(sqrt_σ-np.sqrt(σ)) -print(f"sqrt({σ}) is approximately {sqrt_σ:.5f} (error: {dev:.5f})") +print(f"sqrt({σ}) 大约为 {sqrt_σ:.5f} (error: {dev:.5f})") ``` - -Now we consider cases where $(\eta_1, \eta_2) = (0, 1)$ and $(\eta_1, \eta_2) = (1, 0)$ +现在我们考虑 $(\eta_1, \eta_2) = (0, 1)$ 和 $(\eta_1, \eta_2) = (1, 0)$ 的情况 ```{code-cell} ipython3 -# Compute λ_1, λ_2 +# 计算 λ_1, λ_2 λ_1, λ_2 = solve_λs(coefs) -print(f'Roots for the characteristic equation are ({λ_1:.5f}, {λ_2:.5f}))') +print(f'特征方程的根为 ({λ_1:.5f}, {λ_2:.5f}))') ``` ```{code-cell} ipython3 -# Case 1: η_1, η_2 = (0, 1) +# 情况 1: η_1, η_2 = (0, 1) ηs = (0, 1) -# Compute y_{t} and y_{t-1} with t >= 0 +# 计算 y_{t} 和 y_{t-1} 当 t >= 0 y = lambda t, ηs: (λ_1 ** t) * ηs[0] + (λ_2 ** t) * ηs[1] sqrt_σ = 1 - y(1, ηs) / y(0, ηs) -print(f"For η_1, η_2 = (0, 1), sqrt_σ = {sqrt_σ:.5f}") +print(f"对于 η_1, η_2 = (0, 1), sqrt_σ = {sqrt_σ:.5f}") ``` ```{code-cell} ipython3 -# Case 2: η_1, η_2 = (1, 0) +# 情况 2: η_1, η_2 = (1, 0) ηs = (1, 0) sqrt_σ = y(1, ηs) / y(0, ηs) - 1 -print(f"For η_1, η_2 = (1, 0), sqrt_σ = {sqrt_σ:.5f}") +print(f"对于 η_1, η_2 = (1, 0), sqrt_σ = {sqrt_σ:.5f}") ``` +我们发现收敛是立即的。 +接下来,我们将首先将我们的二阶差分方程 {eq}`eq:second_order` 向量化,然后使用相关状态转移矩阵的特征分解来表示前面的分析。 -We find that convergence is immediate. +## 差分方程的向量化 -Next, we'll represent the preceding analysis by first vectorizing our second-order difference equation {eq}`eq:second_order` and then using eigendecompositions of an associated state transition matrix. - -## Vectorizing the difference equation - - -Represent {eq}`eq:second_order` with the first-order matrix difference equation +用一阶矩阵差分方程表示 {eq}`eq:second_order` $$ \begin{bmatrix} y_{t+1} \cr y_{t} \end{bmatrix} = \begin{bmatrix} 2 & - ( 1 - \sigma) \cr 1 & 0 \end{bmatrix} \begin{bmatrix} y_{t} \cr y_{t-1} \end{bmatrix} $$ -or +或 $$ x_{t+1} = M x_t $$ -where +其中 $$ M = \begin{bmatrix} 2 & - (1 - \sigma ) \cr 1 & 0 \end{bmatrix}, \quad x_t= \begin{bmatrix} y_{t} \cr y_{t-1} \end{bmatrix} $$ -Construct an eigendecomposition of $M$: +构造 $M$ 的特征分解: $$ M = V \begin{bmatrix} \lambda_1 & 0 \cr 0 & \lambda_2 \end{bmatrix} V^{-1} $$ (eq:eigen_sqrt) -where columns of $V$ are eigenvectors corresponding to eigenvalues $\lambda_1$ and $\lambda_2$. +其中 $V$ 的列是对应于特征值 $\lambda_1$ 和 $\lambda_2$ 的特征向量。 +特征值可以排序,使得 $\lambda_1 > 1 > \lambda_2$。 -The eigenvalues can be ordered so that $\lambda_1 > 1 > \lambda_2$. - -Write equation {eq}`eq:second_order` as +将方程 {eq}`eq:second_order` 写为 $$ x_{t+1} = V \Lambda V^{-1} x_t $$ -Now we implement the algorithm above. - -First we write a function that iterates $M$ +现在我们实现上述算法。 +首先,我们编写一个迭代 $M$ 的函数 ```{code-cell} ipython3 def iterate_M(x_0, M, num_steps, dtype=np.float64): - # Eigendecomposition of M + # M 的特征分解 Λ, V = np.linalg.eig(M) V_inv = np.linalg.inv(V) @@ -476,58 +428,50 @@ def iterate_M(x_0, M, num_steps, dtype=np.float64): xs = np.zeros((x_0.shape[0], num_steps + 1)) - # Perform the iterations + # 进行迭代 xs[:, 0] = x_0 for t in range(num_steps): xs[:, t + 1] = M @ xs[:, t] return xs, Λ, V, V_inv -# Define the state transition matrix M +# 定义状态转移矩阵M M = np.array([ [2, -(1 - σ)], [1, 0]]) -# Initial condition vector x_0 +# 初始条件向量 x_0 x_0 = np.array([2, 2]) -# Perform the iteration +# 进行迭代 xs, Λ, V, V_inv = iterate_M(x_0, M, num_steps=100) -print(f"eigenvalues:\n{Λ}") -print(f"eigenvectors:\n{V}") -print(f"inverse eigenvectors:\n{V_inv}") +print(f"特征值:\n{Λ}") +print(f"特征向量:\n{V}") +print(f"逆特征向量:\n{V_inv}") ``` - -Let's compare the eigenvalues to the roots {eq}`eq:secretweapon` of equation -{eq}`eq:cha_eq0` that we computed above. +让我们将特征值与我们在上面计算的方程{eq}`eq:cha_eq0`的根{eq}`eq:secretweapon`进行比较。 ```{code-cell} ipython3 roots = solve_λs((1, -2, (1 - σ))) -print(f"roots: {np.round(roots, 8)}") +print(f"根: {np.round(roots, 8)}") ``` - -Hence we confirmed {eq}`eq:eigen_sqrt`. - -Information about the square root we are after is also contained -in the two eigenvectors. - -Indeed, each eigenvector is just a two-dimensional subspace of ${\mathbb R}^3$ pinned down by dynamics of the form +因此,我们确认了{eq}`eq:eigen_sqrt`。 +关于我们所寻求的平方根的信息也包含在两个特征向量中。 +实际上,每个特征向量只是${\mathbb R}^3$的一个二维子空间,由我们在上面方程{eq}`eq:2diff8`中遇到的以下形式的动态确定: $$ y_{t} = \lambda_i y_{t-1}, \quad i = 1, 2 $$ (eq:invariantsub101) -that we encountered above in equation {eq}`eq:2diff8` above. - -In equation {eq}`eq:invariantsub101`, the $i$th $\lambda_i$ equals the $V_{i, 1}/V_{i,2}$. -The following graph verifies this for our example. +在方程{eq}`eq:invariantsub101`中,第$i$个$\lambda_i$等于$V_{i, 1}/V_{i,2}$。 +下图验证了我们示例中的这一点。 ```{code-cell} ipython3 :tags: [hide-input] -# Plotting the eigenvectors +# 绘制特征向量 plt.figure(figsize=(8, 8)) plt.quiver(0, 0, V[0, 0], V[1, 0], angles='xy', scale_units='xy', @@ -535,7 +479,7 @@ plt.quiver(0, 0, V[0, 0], V[1, 0], angles='xy', scale_units='xy', plt.quiver(0, 0, V[0, 1], V[1, 1], angles='xy', scale_units='xy', scale=1, color='C1', label=fr'$\lambda_2={np.round(Λ[1], 4)}$') -# Annotating the slopes +# 标记斜率 plt.text(V[0, 0]-0.5, V[1, 0]*1.2, r'slope=$\frac{V_{1,1}}{V_{1,2}}=$'+f'{np.round(V[0, 0] / V[1, 0], 4)}', fontsize=12, color='C0') @@ -543,7 +487,7 @@ plt.text(V[0, 1]-0.5, V[1, 1]*1.2, r'slope=$\frac{V_{2,1}}{V_{2,2}}=$'+f'{np.round(V[0, 1] / V[1, 1], 4)}', fontsize=12, color='C1') -# Adding labels +# 添加标记 plt.axhline(0, color='grey', linewidth=0.5, alpha=0.4) plt.axvline(0, color='grey', linewidth=0.5, alpha=0.4) plt.legend() @@ -552,40 +496,34 @@ plt.xlim(-1.5, 1.5) plt.ylim(-1.5, 1.5) plt.show() ``` +## 不变子空间方法 -## Invariant subspace approach - -The preceding calculation indicates that we can use the eigenvectors $V$ to construct 2-dimensional *invariant subspaces*. - -We'll pursue that possibility now. - -Define the transformed variables - +前面的计算表明,我们可以使用特征向量 $V$ 构造 2 维*不变子空间*。 +现在我们将探讨这种可能性。 +定义变换后的变量 $$ x_t^* = V^{-1} x_t $$ -Evidently, we can recover $x_t$ from $x_t^*$: +显然,我们可以从 $x_t^*$ 恢复 $x_t$: $$ x_t = V x_t^* $$ - -The following notations and equations will help us. - -Let +以下符号和方程将对我们有所帮助。 +令 $$ - V = \begin{bmatrix} V_{1,1} & V_{1,2} \cr V_{2,1} & V_{2,2} \end{bmatrix}, \quad V^{-1} = \begin{bmatrix} V^{1,1} & V^{1,2} \cr V^{2,1} & V^{2,2} \end{bmatrix} $$ -Notice that it follows from +注意,从以下等式可以得出 + $$ \begin{bmatrix} V^{1,1} & V^{1,2} \cr @@ -593,150 +531,135 @@ $$ V_{2,1} & V_{2,2} \end{bmatrix} = \begin{bmatrix} 1 & 0 \cr 0 & 1 \end{bmatrix} $$ -that $$ V^{2,1} V_{1,1} + V^{2,2} V_{2,1} = 0 $$ -and +和 $$ V^{1,1}V_{1,2} + V^{1,2} V_{2,2} = 0. $$ -These equations will be very useful soon. - - -Notice that +这些方程很快就会非常有用。 +注意 $$ \begin{bmatrix} x_{1,t+1}^* \cr x_{2,t+1}^* \end{bmatrix} = \begin{bmatrix} \lambda_1 & 0 \cr 0 & \lambda_2 \end{bmatrix} \begin{bmatrix} x_{1,t}^* \cr x_{2,t}^* \end{bmatrix} $$ -To deactivate $\lambda_1$ we want to set +要使 $\lambda_1$ 失活,我们需要设置 $$ x_{1,0}^* = 0. $$ - -This can be achieved by setting +这可以通过设置以下内容实现 $$ x_{2,0} = -( V^{1,2})^{-1} V^{1,1} x_{1,0} = V_{2,2} V_{1,2}^{-1} x_{1,0}. $$ (eq:deactivate1) -To deactivate $\lambda_2$, we want to set +要使 $\lambda_2$ 失活,我们需要设置 $$ x_{2,0}^* = 0 $$ -This can be achieved by setting +这可以通过设置以下内容实现 $$ x_{2,0} = -(V^{2,2})^{-1} V^{2,1} x_{1,0} = V_{2,1} V_{1,1}^{-1} x_{1,0}. $$ (eq:deactivate2) -Let's verify {eq}`eq:deactivate1` and {eq}`eq:deactivate2` below - -To deactivate $\lambda_1$ we use {eq}`eq:deactivate1` +让我们在下面验证 {eq}`eq:deactivate1` 和 {eq}`eq:deactivate2` +要使 $\lambda_1$ 失活,我们使用 {eq}`eq:deactivate1` ```{code-cell} ipython3 xd_1 = np.array((x_0[0], V[1,1]/V[0,1] * x_0[0]), dtype=np.float64) -# Compute x_{1,0}^* +# 计算 x_{1,0}^* np.round(V_inv @ xd_1, 8) ``` +我们发现 $x_{1,0}^* = 0$. -We find $x_{1,0}^* = 0$. - -Now we deactivate $\lambda_2$ using {eq}`eq:deactivate2` +现在我们用使{eq}`eq:deactivate2` $\lambda_2$ 失活 ```{code-cell} ipython3 xd_2 = np.array((x_0[0], V[1,0]/V[0,0] * x_0[0]), dtype=np.float64) -# Compute x_{2,0}^* +# 计算 x_{2,0}^* np.round(V_inv @ xd_2, 8) ``` -We find $x_{2,0}^* = 0$. +我们发现 $x_{2,0}^* = 0$. ```{code-cell} ipython3 -# Simulate with muted λ1 λ2. +# 模拟消音的λ1 λ2。 num_steps = 10 xs_λ1 = iterate_M(xd_1, M, num_steps)[0] xs_λ2 = iterate_M(xd_2, M, num_steps)[0] -# Compute ratios y_t / y_{t-1} +# 计算比率 y_t / y_{t-1} ratios_λ1 = xs_λ1[1, 1:] / xs_λ1[1, :-1] ratios_λ2 = xs_λ2[1, 1:] / xs_λ2[1, :-1] ``` -The following graph shows the ratios $y_t / y_{t-1}$ for the two cases. - -We find that the ratios converge to $\lambda_2$ in the first case and $\lambda_1$ in the second case. +以下图表显示了两种情况下 $y_t / y_{t-1}$ 的比率。 +我们发现,在第一种情况下,比率收敛于 $\lambda_2$,而在第二种情况下,比率收敛于 $\lambda_1$。 ```{code-cell} ipython3 :tags: [hide-input] -# Plot the ratios for y_t / y_{t-1} +# 绘制比率 y_t / y_{t-1} fig, axs = plt.subplots(1, 2, figsize=(12, 6), dpi=500) -# First subplot +# 第一子图 axs[0].plot(np.round(ratios_λ1, 6), label=r'$\frac{y_t}{y_{t-1}}$', linewidth=3) axs[0].axhline(y=Λ[1], color='red', linestyle='--', label='$\lambda_2$', alpha=0.5) axs[0].set_xlabel('t', size=18) axs[0].set_ylabel(r'$\frac{y_t}{y_{t-1}}$', size=18) -axs[0].set_title(r'$\frac{y_t}{y_{t-1}}$ after Muting $\lambda_1$', +axs[0].set_title(r'$\frac{y_t}{y_{t-1}}$ 在消音 $\lambda_1$之后', size=13) axs[0].legend() -# Second subplot +# 第二子图 axs[1].plot(ratios_λ2, label=r'$\frac{y_t}{y_{t-1}}$', linewidth=3) axs[1].axhline(y=Λ[0], color='green', linestyle='--', label='$\lambda_1$', alpha=0.5) axs[1].set_xlabel('t', size=18) axs[1].set_ylabel(r'$\frac{y_t}{y_{t-1}}$', size=18) -axs[1].set_title(r'$\frac{y_t}{y_{t-1}}$ after Muting $\lambda_2$', +axs[1].set_title(r'$\frac{y_t}{y_{t-1}}$ 在消音 $\lambda_2$之后', size=13) axs[1].legend() plt.tight_layout() plt.show() ``` +## 结束语 +本讲为*不变子空间*方法的许多其他应用奠定了基础。 +所有这些应用都利用了基于特征分解的非常相似的方程。 +我们将在{doc}`money_inflation`和动态经济理论的许多其他地方遇到与{eq}`eq:deactivate1`和{eq}`eq:deactivate2`非常相似的方程。 -## Concluding remarks - -This lecture sets the stage for many other applications of the *invariant subspace* methods. - -All of these exploit very similar equations based on eigen decompositions. - -We shall encounter equations very similar to {eq}`eq:deactivate1` and {eq}`eq:deactivate2` -in {doc}`money_inflation` and in many other places in dynamic economic theory. - - -## Exercise +## 练习 ```{exercise-start} :label: greek_square_ex_a ``` -Please use matrix algebra to formulate the method described by Bertrand Russell at the beginning of this lecture. - -1. Define a state vector $x_t = \begin{bmatrix} a_t \cr b_t \end{bmatrix}$. -2. Formulate a first-order vector difference equation for $x_t$ of the form $x_{t+1} = A x_t$ and -compute the matrix $A$. -3. Use the system $x_{t+1} = A x_t$ to replicate the sequence of $a_t$'s and $b_t$'s described by Bertrand Russell. -4. Compute the eigenvectors and eigenvalues of $A$ and compare them to corresponding objects computed in the text of this lecture. +请使用矩阵代数来表述伯特兰·罗素在本讲座开始时描述的方法。 +1. 定义一个状态向量 $x_t = \begin{bmatrix} a_t \cr b_t \end{bmatrix}$。 +2. 为 $x_t$ 制定一个一阶向量差分方程,形式为 $x_{t+1} = A x_t$,并计算矩阵 $A$。 +3. 使用系统 $x_{t+1} = A x_t$ 来复制伯特兰·罗素描述的 $a_t$ 和 $b_t$ 序列。 +4. 计算 $A$ 的特征向量和特征值,并将它们与本讲座文本中计算的相应对象进行比较。 ```{exercise-end} ``` @@ -744,10 +667,9 @@ compute the matrix $A$. ```{solution-start} greek_square_ex_a :class: dropdown ``` +以下是一个解决方案: -Here is one soluition. - -According to the quote, we can formulate +根据引用,我们可以表述为: $$ \begin{aligned} @@ -756,40 +678,40 @@ b_{t+1} &= 2a_t + b_t \end{aligned} $$ (eq:gs_ex1system) -with $x_0 = \begin{bmatrix} a_0 \cr b_0 \end{bmatrix} = \begin{bmatrix} 1 \cr 1 \end{bmatrix}$ +其中 $x_0 = \begin{bmatrix} a_0 \cr b_0 \end{bmatrix} = \begin{bmatrix} 1 \cr 1 \end{bmatrix}$ -By {eq}`eq:gs_ex1system`, we can write matrix $A$ as +根据{eq}`eq:gs_ex1system`,我们可以将矩阵 $A$ 写为: $$ A = \begin{bmatrix} 1 & 1 \cr 2 & 1 \end{bmatrix} $$ -Then $x_{t+1} = A x_t$ for $t \in \{0, \dots, 5\}$ +然后,对于 $t \in \{0, \dots, 5\}$,有 $x_{t+1} = A x_t$ ```{code-cell} ipython3 -# Define the matrix A +# 定义矩阵 A A = np.array([[1, 1], [2, 1]]) -# Initial vector x_0 +# 初始向量 x_0 x_0 = np.array([1, 1]) -# Number of iterations +# 迭代次数 n = 6 -# Generate the sequence +# 生成序列 xs = np.array([x_0]) x_t = x_0 for _ in range(1, n): x_t = A @ x_t xs = np.vstack([xs, x_t]) -# Print the sequence +# 打印序列 for i, (a_t, b_t) in enumerate(xs): - print(f"Iter {i}: a_t = {a_t}, b_t = {b_t}") + print(f"迭代 {i}: a_t = {a_t}, b_t = {b_t}") -# Compute eigenvalues and eigenvectors of A +# 计算 A 的特征值和特征向量 eigenvalues, eigenvectors = np.linalg.eig(A) print(f'\nEigenvalues:\n{eigenvalues}') @@ -797,4 +719,4 @@ print(f'\nEigenvectors:\n{eigenvectors}') ``` ```{solution-end} -``` +``` \ No newline at end of file diff --git a/lectures/heavy_tails.md b/lectures/heavy_tails.md index f5636fc..44f7bb6 100644 --- a/lectures/heavy_tails.md +++ b/lectures/heavy_tails.md @@ -10,11 +10,10 @@ kernelspec: language: python name: python3 --- - (heavy_tail)= -# Heavy-Tailed Distributions +# 重尾分布 -In addition to what's in Anaconda, this lecture will need the following libraries: +除了Anaconda中的内容,本讲还需要以下库: ```{code-cell} ipython3 :tags: [hide-output] @@ -22,9 +21,10 @@ In addition to what's in Anaconda, this lecture will need the following librarie !pip install --upgrade yfinance pandas_datareader ``` -We use the following imports. +我们使用以下的导入。 ```{code-cell} ipython3 +import matplotlib as mpl import matplotlib.pyplot as plt import numpy as np import yfinance as yf @@ -35,33 +35,35 @@ from pandas_datareader import wb from scipy.stats import norm, cauchy from pandas.plotting import register_matplotlib_converters register_matplotlib_converters() + +FONTPATH = "fonts/SourceHanSerifSC-SemiBold.otf" +mpl.font_manager.fontManager.addfont(FONTPATH) +plt.rcParams['font.family'] = ['Source Han Serif SC'] ``` -## Overview +## 概览 -Heavy-tailed distributions are a class of distributions that generate "extreme" outcomes. +重尾分布是一类能产生“极端”结果的分布。 -In the natural sciences (and in more traditional economics courses), heavy-tailed distributions are seen as quite exotic and non-standard. +在自然科学(以及更多传统的经济学课程)中,重尾分布常被视为相当异乎寻常和非标准的。 -However, it turns out that heavy-tailed distributions play a crucial role in economics. +然而,事实证明,重尾分布在经济学中扮演着至关重要的角色。 -In fact many -- if not most -- of the important distributions in economics are heavy-tailed. +实际上,经济学中许多 -- 如果不是大部分 -- 重要的分布都是重尾的。 -In this lecture we explain what heavy tails are and why they are -- or at least -why they should be -- central to economic analysis. +在这次讲座中,我们将解释什么是重尾,以及为什么它们是 -- 或者至少应该是 -- 经济分析的核心。 -### Introduction: light tails +### 引言:轻尾分布 -Most {doc}`commonly used probability distributions ` in classical statistics and -the natural sciences have "light tails." +大多数{doc}`常用概率分布`在经典统计学和自然科学中都具有“轻尾”。 -To explain this concept, let's look first at examples. +为了解释这个概念,让我们先看一些例子。 ```{prf:example} :label: ht_ex_nd -The classic example is the [normal distribution](https://en.wikipedia.org/wiki/Normal_distribution), which has density +经典的例子是[正态分布](https://en.wikipedia.org/wiki/Normal_distribution),其密度公式为 $$ f(x) = \frac{1}{\sqrt{2\pi}\sigma} @@ -70,51 +72,47 @@ f(x) = \frac{1}{\sqrt{2\pi}\sigma} (-\infty < x < \infty) $$ +这里的两个参数 $\mu$ 和 $\sigma$ 分别代表均值和标准差。 -The two parameters $\mu$ and $\sigma$ are the mean and standard deviation -respectively. - -As $x$ deviates from $\mu$, the value of $f(x)$ goes to zero extremely -quickly. +随着 $x$ 从 $\mu$ 偏离,$f(x)$ 的值会非常快地趋向于零。 ``` -We can see this when we plot the density and show a histogram of observations, -as with the following code (which assumes $\mu=0$ and $\sigma=1$). +我们可以通过绘制密度图和展示观测值的直方图来看到这一点,如下代码所示(假设 $\mu=0$ 和 $\sigma=1$)。 ```{code-cell} ipython3 --- mystnb: figure: - caption: Histogram of observations + caption: 观测值直方图 name: hist-obs --- fig, ax = plt.subplots() X = norm.rvs(size=1_000_000) -ax.hist(X, bins=40, alpha=0.4, label='histogram', density=True) +ax.hist(X, bins=40, alpha=0.4, label='直方图', density=True) x_grid = np.linspace(-4, 4, 400) -ax.plot(x_grid, norm.pdf(x_grid), label='density') +ax.plot(x_grid, norm.pdf(x_grid), label='密度') ax.legend() plt.show() ``` -Notice how +请注意: -* the density's tails converge quickly to zero in both directions and -* even with 1,000,000 draws, we get no very large or very small observations. +* 密度的尾部在两个方向上迅速趋于零 +* 即使是1,000,000次抽样,我们也没有观察到非常大或非常小的值。 -We can see the last point more clearly by executing +我们可以通过执行以下代码更清楚地看到最后一点: ```{code-cell} ipython3 X.min(), X.max() ``` -Here's another view of draws from the same distribution: +这里是从同一分布中抽取的另一种视图: ```{code-cell} ipython3 --- mystnb: figure: - caption: Histogram of observations + caption: 观测值的直方图 name: hist-obs2 --- n = 2000 @@ -128,61 +126,51 @@ ax.set_ylabel('$X_i$', rotation=0) plt.show() ``` -We have plotted each individual draw $X_i$ against $i$. - -None are very large or very small. - -In other words, extreme observations are rare and draws tend not to deviate -too much from the mean. - -Putting this another way, light-tailed distributions are those that -rarely generate extreme values. +我们已经绘制了每个单独的抽样 $X_i$ 与 $i$ 的对应关系。 -(A more formal definition is given {ref}`below `.) +没有一个非常大或非常小。 -Many statisticians and econometricians -use rules of thumb such as "outcomes more than four or five -standard deviations from the mean can safely be ignored." +换句话说,极端的观测值很少,抽样倾向于不会太偏离均值。 -But this is only true when distributions have light tails. +换种说法,轻尾分布是那些很少产生极端值的分布。 +(更正式的定义见{ref}`下面 `。) -### When are light tails valid? +许多统计学家和计量经济学家使用经验法则,如“结果超出均值四到五个标准差可以安全忽略。” -In probability theory and in the real world, many distributions are -light-tailed. +但这只有在分布具有轻尾的时候才成立。 -For example, human height is light-tailed. +### 何时适用于轻尾分布? -Yes, it's true that we see some very tall people. +在概率理论和现实世界中,许多分布都是轻尾的。 -* For example, basketballer [Sun Mingming](https://en.wikipedia.org/wiki/Sun_Mingming) is 2.32 meters tall +例如,人类身高就是轻尾分布。 -But have you ever heard of someone who is 20 meters tall? Or 200? Or 2000? +是的,我们确实看到了一些非常高的人。 -Have you ever wondered why not? +* 例如,篮球运动员[孙明明](https://en.wikipedia.org/wiki/Sun_Mingming)身高2.32米 -After all, there are 8 billion people in the world! +但是你有没有听说过有人身高达20米、200米或2000米的吗? -In essence, the reason we don't see such draws is that the distribution of -human height has very light tails. +你有没有想过为什么没有? -In fact the distribution of human height obeys a bell-shaped curve similar to the normal distribution. +毕竟,全世界有80亿人! +本质上,我们看不到这样的数据是因为人类身高的分布具有非常轻的尾部。 -### Returns on assets +事实上,人类身高的分布符合类似正态分布的钟形曲线。 +### 资产回报 -But what about economic data? +但经济数据呢? -Let's look at some financial data first. +我们首先来看一些金融数据。 -Our aim is to plot the daily change in the price of Amazon (AMZN) stock for -the period from 1st January 2015 to 1st July 2022. +我们的目标是绘制自2015年1月1日至2022年7月1日期间亚马逊(AMZN)股价的每日变化。 -This equates to daily returns if we set dividends aside. +如果我们不考虑股息,这相当于每日回报。 -The code below produces the desired plot using Yahoo financial data via the `yfinance` library. +以下代码通过 `yfinance` 库使用雅虎财经数据生成所需的图表。 ```{code-cell} ipython3 :tags: [hide-output] @@ -194,7 +182,7 @@ data = yf.download('AMZN', '2015-1-1', '2022-7-1') --- mystnb: figure: - caption: Daily Amazon returns + caption: 每日亚马逊回报 name: dailyreturns-amzn --- s = data['Adj Close'] @@ -204,17 +192,17 @@ fig, ax = plt.subplots() ax.plot(r, linestyle='', marker='o', alpha=0.5, ms=4) ax.vlines(r.index, 0, r.values, lw=0.2) -ax.set_ylabel('returns', fontsize=12) -ax.set_xlabel('date', fontsize=12) +ax.set_ylabel('回报', fontsize=12) +ax.set_xlabel('日期', fontsize=12) plt.show() ``` -This data looks different to the draws from the normal distribution we saw above. +这些数据看起来与我们上面看到的正态分布的抽样有所不同。 -Several of observations are quite extreme. +有几个观测值非常极端。 -We get a similar picture if we look at other assets, such as Bitcoin +如果我们查看其他资产,比如比特币,我们会得到类似的图像。 ```{code-cell} ipython3 :tags: [hide-output] @@ -226,7 +214,7 @@ data = yf.download('BTC-USD', '2015-1-1', '2022-7-1') --- mystnb: figure: - caption: Daily Bitcoin returns + caption: 比特币每日回报 name: dailyreturns-btc --- s = data['Adj Close'] @@ -236,115 +224,101 @@ fig, ax = plt.subplots() ax.plot(r, linestyle='', marker='o', alpha=0.5, ms=4) ax.vlines(r.index, 0, r.values, lw=0.2) -ax.set_ylabel('returns', fontsize=12) -ax.set_xlabel('date', fontsize=12) +ax.set_ylabel('回报', fontsize=12) +ax.set_xlabel('日期', fontsize=12) plt.show() ``` -The histogram also looks different to the histogram of the normal -distribution: +这个直方图也与正态分布的直方图不同: ```{code-cell} ipython3 --- mystnb: figure: - caption: Histogram (normal vs bitcoin returns) + caption: 直方图(正常与比特币回报对比) name: hist-normal-btc --- r = np.random.standard_t(df=5, size=1000) fig, ax = plt.subplots() -ax.hist(r, bins=60, alpha=0.4, label='bitcoin returns', density=True) +ax.hist(r, bins=60, alpha=0.4, label='比特币回报', density=True) xmin, xmax = plt.xlim() x = np.linspace(xmin, xmax, 100) p = norm.pdf(x, np.mean(r), np.std(r)) -ax.plot(x, p, linewidth=2, label='normal distribution') +ax.plot(x, p, linewidth=2, label='正态分布') -ax.set_xlabel('returns', fontsize=12) +ax.set_xlabel('回报', fontsize=12) ax.legend() plt.show() ``` -If we look at higher frequency returns data (e.g., tick-by-tick), we often see -even more extreme observations. +如果我们查看更高频率的回报数据(例如,逐笔交易),我们经常会看到更极端的观测。 -See, for example, {cite}`mandelbrot1963variation` or {cite}`rachev2003handbook`. +例如,参见 {cite}`mandelbrot1963variation` 或 {cite}`rachev2003handbook`。 +### 其他数据 -### Other data +我们刚刚看到的数据被称为“重尾”。 -The data we have just seen is said to be "heavy-tailed". - -With heavy-tailed distributions, extreme outcomes occur relatively -frequently. +在重尾分布中,极端结果相对频繁地发生。 ```{prf:example} :label: ht_ex_od -Importantly, there are many examples of heavy-tailed distributions -observed in economic and financial settings! +重要的是,在经济和金融环境中观察到了许多重尾分布的例子! -For example, the income and the wealth distributions are heavy-tailed +例如,收入和财富分布是重尾的 -* You can imagine this: most people have low or modest wealth but some people - are extremely rich. +* 你可以想象这样的情景:大多数人的财富较低或适中,但有些人非常富有。 -The firm size distribution is also heavy-tailed +公司规模分布也是重尾的 -* You can imagine this too: most firms are small but some firms are enormous. +* 你也可以想象这样的情景:大多数公司都很小,但有些公司非常庞大。 -The distribution of town and city sizes is heavy-tailed +城镇和城市大小的分布是重尾的 -* Most towns and cities are small but some are very large. +* 大多数城镇和城市都很小,但有些非常大。 ``` -Later in this lecture, we examine heavy tails in these distributions. - -### Why should we care? - -Heavy tails are common in economic data but does that mean they are important? +在本讲座的后面部分,我们将研究这些分布中的重尾现象。 -The answer to this question is affirmative! +### 为什么我们应该关心? -When distributions are heavy-tailed, we need to think carefully about issues -like +重尾在经济数据中很常见,但这是否意味着它们很重要? -* diversification and risk -* forecasting -* taxation (across a heavy-tailed income distribution), etc. +对这个问题的回答是肯定的! -We return to these points {ref}`below `. +当分布是重尾的,我们需要仔细考虑一些问题,如 +* 多样化和风险 +* 预测 +* 税收(针对重尾收入分布),等等。 -## Visual comparisons -In this section, we will introduce important concepts such as the Pareto distribution, Counter CDFs, and Power laws, which aid in recognizing heavy-tailed distributions. +我们将回到这些点 {ref}`下面 `。 -Later we will provide a mathematical definition of the difference between -light and heavy tails. +## 视觉比较 +在本节中,我们将介绍帕雷托分布、计数器累积分布函数和幂律等重要概念,这些概念有助于识别重尾分布。 -But for now let's do some visual comparisons to help us build intuition on the -difference between these two types of distributions. +稍后我们将提供有关轻尾和重尾差异的数学定义。 +但现在让我们先进行一些视觉比较,以帮助我们建立对这两种类型分布差异的直觉。 -### Simulations +### 模拟 -The figure below shows a simulation. +下图显示了一次模拟。 -The top two subfigures each show 120 independent draws from the normal -distribution, which is light-tailed. +上面两个子图各显示来自正态分布的120个独立抽取,这是轻尾分布。 -The bottom subfigure shows 120 independent draws from [the Cauchy -distribution](https://en.wikipedia.org/wiki/Cauchy_distribution), which is -heavy-tailed. +下面的子图显示来自[柯西分布](https://en.wikipedia.org/wiki/Cauchy_distribution)的120个独立抽取,这是重尾分布。 ```{code-cell} ipython3 --- mystnb: figure: - caption: Draws from normal and Cauchy distributions + caption: 来自正态分布和柯西分布的抽取 name: draws-normal-cauchy --- n = 120 @@ -361,49 +335,44 @@ for ax, s in zip(axes[:2], s_vals): data = np.random.randn(n) * s ax.plot(list(range(n)), data, linestyle='', marker='o', alpha=0.5, ms=4) ax.vlines(list(range(n)), 0, data, lw=0.2) - ax.set_title(f"draws from $N(0, \sigma^2)$ with $\sigma = {s}$", fontsize=11) + ax.set_title(f"从 $N(0, \sigma^2)$ 抽取,$\sigma = {s}$", fontsize=11) ax = axes[2] distribution = cauchy() data = distribution.rvs(n) ax.plot(list(range(n)), data, linestyle='', marker='o', alpha=0.5, ms=4) ax.vlines(list(range(n)), 0, data, lw=0.2) -ax.set_title(f"draws from the Cauchy distribution", fontsize=11) +ax.set_title(f"来自柯西分布的抽取", fontsize=11) plt.subplots_adjust(hspace=0.25) plt.show() ``` -In the top subfigure, the standard deviation of the normal distribution is 2, -and the draws are clustered around the mean. +在顶部的子图中,正态分布的标准偏差为2,抽取值围绕均值聚集。 -In the middle subfigure, the standard deviation is increased to 12 and, as -expected, the amount of dispersion rises. +在中间的子图中,标准偏差增加到12,如预期的那样,分散度增加。 -The bottom subfigure, with the Cauchy draws, shows a different pattern: tight -clustering around the mean for the great majority of observations, combined -with a few sudden large deviations from the mean. +底部的子图中,柯西的抽取显示出一种不同的模式:大多数观察值紧密围绕均值聚集,但偶有几个从均值突然大偏差。 -This is typical of a heavy-tailed distribution. +这是典型的重尾分布特征。 -### Nonnegative distributions +### 非负分布 -Let's compare some distributions that only take nonnegative values. +让我们比较一些只取非负值的分布。 -One is the exponential distribution, which we discussed in {doc}`our lecture -on probability and distributions `. +其中一种是指数分布,我们在{doc}`our lecture on probability and distributions `中讨论过。 -The exponential distribution is a light-tailed distribution. +指数分布是一种轻尾分布。 -Here are some draws from the exponential distribution. +这里有一些来自指数分布的抽样。 ```{code-cell} ipython3 --- mystnb: figure: - caption: Draws of exponential distribution + caption: 指数分布的抽样 name: draws-exponential --- n = 120 @@ -419,48 +388,44 @@ ax.vlines(list(range(n)), 0, data, lw=0.2) plt.show() ``` -Another nonnegative distribution is the [Pareto distribution](https://en.wikipedia.org/wiki/Pareto_distribution). +另一个非负分布是[帕累托分布](https://en.wikipedia.org/wiki/Pareto_distribution)。 -If $X$ has the Pareto distribution, then there are positive constants $\bar x$ -and $\alpha$ such that +如果 $X$ 遵循帕累托分布,那么存在正常数 $x$ 和 $\alpha$,使得 ```{math} :label: pareto \mathbb P\{X > x\} = \begin{cases} - \left( \bar x/x \right)^{\alpha} - & \text{ if } x \geq \bar x + \left( \frac{\bar x}{x} \right)^{\alpha} + & \text{ 如果 } x \geq \bar x \\ 1 - & \text{ if } x < \bar x + & \text{ 如果 } x < \bar x \end{cases} ``` -The parameter $\alpha$ is called the **tail index** and $\bar x$ is called the -**minimum**. +参数 $\alpha$ 被称为**尾指数**,$\bar x$ 被称为**最小值**。 -The Pareto distribution is a heavy-tailed distribution. +帕累托分布是一个重尾分布。 -One way that the Pareto distribution arises is as the exponential of an -exponential random variable. +帕累托分布产生的一个方式是指数随机变量的指数。 -In particular, if $X$ is exponentially distributed with rate parameter $\alpha$, then +特别地,如果 $X$ 是以率参数 $\alpha$ 指数分布的,那么 $$ Y = \bar x \exp(X) $$ -is Pareto-distributed with minimum $\bar x$ and tail index $\alpha$. +具有最小值 $\bar x$ 和尾指数 $\alpha$ 的帕累托分布。 -Here are some draws from the Pareto distribution with tail index $1$ and minimum -$1$. +这里是一些从帕累托分布中抽取的数据,尾指数为 $1$,最小值为 $1$。 ```{code-cell} ipython3 --- mystnb: figure: - caption: Draws from Pareto distribution + caption: 从帕累托分布中抽取的数据 name: draws-pareto --- n = 120 @@ -476,51 +441,48 @@ ax.vlines(list(range(n)), 0, pareto_data, lw=0.2) plt.show() ``` -Notice how extreme outcomes are more common. +注意极端结果更常见。 -### Counter CDFs +### 对立累积分布函数 -For nonnegative random variables, one way to visualize the difference between -light and heavy tails is to look at the -**counter CDF** (CCDF). +对于非负随机变量,视觉上区分轻尾和重尾的一种方法是查看**对立累积分布函数**(CCDF)。 -For a random variable $X$ with CDF $F$, the CCDF is the function +对于一个具有CDF $F$ 的随机变量 $X$,CCDF定义为函数 $$ -G(x) := 1 - F(x) = \mathbb P\{X > x\} +G(x) := 1 - F(x) = \mathbb P\{X > x\} $$ -(Some authors call $G$ the "survival" function.) +(有些作者称$G$为“生存”函数。) -The CCDF shows how fast the upper tail goes to zero as $x \to \infty$. +CCDF显示随着 $x \to \infty$,上尾速度减少到零的快慢。 -If $X$ is exponentially distributed with rate parameter $\alpha$, then the CCDF is +如果$X$是具有速率参数$\alpha$的指数分布,则其CCDF为 $$ G_E(x) = \exp(- \alpha x) $$ -This function goes to zero relatively quickly as $x$ gets large. +随着 $x$ 增大,这个函数相对快速地趋于零。 -The standard Pareto distribution, where $\bar x = 1$, has CCDF +标准帕雷托分布,其中 $\beta x = 1$,具有CCDF $$ G_P(x) = x^{- \alpha} $$ -This function goes to zero as $x \to \infty$, but much slower than $G_E$. +这个函数在 $x \to \infty$ 时趋于零,但比 $G_E$ 更慢。 ```{exercise} :label: ht_ex_x1 -Show how the CCDF of the standard Pareto distribution can be derived from the CCDF of the exponential distribution. +展示如何从指数分布的CCDF推导出标准帕雷托分布的CCDF。 ``` ```{solution-start} ht_ex_x1 :class: dropdown ``` -Letting $G_E$ and $G_P$ be defined as above, letting $X$ be exponentially -distributed with rate parameter $\alpha$, and letting $Y = \exp(X)$, we have +设 $G_E$ 和 $G_P$ 如上定义,设 $X$ 是具有速率参数 $\alpha$ 的指数分布,并设 $Y = \exp(X)$,我们有 $$ \begin{aligned} @@ -528,88 +490,84 @@ $$ & = \mathbb P\{\exp(X) > y\} \\ & = \mathbb P\{X > \ln y\} \\ & = G_E(\ln y) \\ - & = \exp( - \alpha \ln y) \\ + & = \exp(- \alpha \ln y) \\ & = y^{-\alpha} \end{aligned} $$ ```{solution-end} ``` -Here's a plot that illustrates how $G_E$ goes to zero faster than $G_P$. +这是一个图示,展示了$G_E$比$G_P$衰减得更快。 ```{code-cell} ipython3 --- mystnb: figure: - caption: Pareto and exponential distribution comparison + caption: 帕累托分布与指数分布对比 name: compare-pareto-exponential --- x = np.linspace(1.5, 100, 1000) fig, ax = plt.subplots() alpha = 1.0 -ax.plot(x, np.exp(- alpha * x), label='exponential', alpha=0.8) -ax.plot(x, x**(- alpha), label='Pareto', alpha=0.8) -ax.set_xlabel('X value') +ax.plot(x, np.exp(- alpha * x), label='指数分布', alpha=0.8) +ax.plot(x, x**(- alpha), label='帕累托分布', alpha=0.8) +ax.set_xlabel('X值') ax.set_ylabel('CCDF') ax.legend() plt.show() ``` -Here's a log-log plot of the same functions, which makes visual comparison -easier. +下面是同一函数的对数对数图,便于视觉比较。 ```{code-cell} ipython3 --- mystnb: figure: - caption: Pareto and exponential distribution comparison (log-log) + caption: 帕累托与指数分布对比(对数对数图) name: compare-pareto-exponential-log-log --- fig, ax = plt.subplots() alpha = 1.0 -ax.loglog(x, np.exp(- alpha * x), label='exponential', alpha=0.8) -ax.loglog(x, x**(- alpha), label='Pareto', alpha=0.8) -ax.set_xlabel('log value') -ax.set_ylabel('log prob') +ax.loglog(x, np.exp(- alpha * x), label='指数分布', alpha=0.8) +ax.loglog(x, x**(- alpha), label='帕累托分布', alpha=0.8) +ax.set_xlabel('对数值') +ax.set_ylabel('对数概率') ax.legend() plt.show() ``` -In the log-log plot, the Pareto CCDF is linear, while the exponential one is -concave. +在对数对数图中,帕累托的互补累积分布函数是线性的,而指数的则是凹的。 -This idea is often used to separate light- and heavy-tailed distributions in -visualisations --- we return to this point below. +这个观点常用于在视觉化中区分轻尾分布和重尾分布——我们下面会再次讨论这一点。 +### 实验 CCDFs -### Empirical CCDFs +样本对应的 CCDF 函数是**经验 CCDF**。 -The sample counterpart of the CCDF function is the **empirical CCDF**. - -Given a sample $x_1, \ldots, x_n$, the empirical CCDF is given by +给定一个样本 $x_1, \ldots, x_n$,经验 CCDF 定义为 $$ \hat G(x) = \frac{1}{n} \sum_{i=1}^n \mathbb 1\{x_i > x\} $$ -Thus, $\hat G(x)$ shows the fraction of the sample that exceeds $x$. +因此,$\hat G(x)$ 显示样本中超过 $x$ 的比例。 ```{code-cell} ipython3 def eccdf(x, data): - "Simple empirical CCDF function." + "简单的经验 CCDF 函数。" return np.mean(data > x) ``` -Here's a figure containing some empirical CCDFs from simulated data. +下面是一些从模拟数据得到的经验 CCDFs 的图。 ```{code-cell} ipython3 --- mystnb: figure: - caption: Empirical CCDFs + caption: 实验 CCDFs name: ccdf-empirics --- -# Parameters and grid +# 参数和网格 x_grid = np.linspace(1, 1000, 1000) sample_size = 1000 np.random.seed(13) @@ -622,7 +580,7 @@ data_pareto = np.exp(np.random.exponential(size=sample_size)) data_list = [data_exp, data_logn, data_pareto] -# Build figure +# 构建图形 fig, axes = plt.subplots(3, 1, figsize=(6, 8)) axes = axes.flatten() labels = ['exponential', 'lognormal', 'Pareto'] @@ -631,8 +589,8 @@ for data, label, ax in zip(data_list, labels, axes): ax.loglog(x_grid, [eccdf(x, data) for x in x_grid], 'o', markersize=3.0, alpha=0.5, label=label) - ax.set_xlabel("log value") - ax.set_ylabel("log prob") + ax.set_xlabel("对数值") + ax.set_ylabel("对数概率") ax.legend() @@ -642,20 +600,19 @@ fig.subplots_adjust(hspace=0.4) plt.show() ``` -As with the CCDF, the empirical CCDF from the Pareto distributions is -approximately linear in a log-log plot. +与 CCDF 一样,帕累托分布的经验 CCDF 在对数-对数图中大致呈线性。 -We will use this idea [below](https://intro.quantecon.org/heavy_tails.html#heavy-tails-in-economic-cross-sections) when we look at real data. +我们将在下面使用这个想法 [here](https://intro.quantecon.org/heavy_tails.html#heavy-tails-in-economic-cross-sections) 当我们查看真实数据时。 +++ -#### Q-Q Plots +#### Q-Q图 -We can also use a [qq plot](https://en.wikipedia.org/wiki/Q%E2%80%93Q_plot) to do a visual comparison between two probability distributions. +我们也可以使用[qq图](https://en.wikipedia.org/wiki/Q%E2%80%93Q_plot)来可视化比较两个概率分布。 -The [statsmodels](https://www.statsmodels.org/stable/index.html) package provides a convenient [qqplot](https://www.statsmodels.org/stable/generated/statsmodels.graphics.gofplots.qqplot.html) function that, by default, compares sample data to the quintiles of the normal distribution. +[statsmodels](https://www.statsmodels.org/stable/index.html)包提供了一个方便的[qqplot](https://www.statsmodels.org/stable/generated/statsmodels.graphics.gofplots.qqplot.html)函数,该函数默认将样本数据与正态分布的分位数进行比较。 -If the data is drawn from a normal distribution, the plot would look like: +如果数据来自正态分布,该图看起来会像: ```{code-cell} ipython3 data_normal = np.random.normal(size=sample_size) @@ -663,89 +620,83 @@ sm.qqplot(data_normal, line='45') plt.show() ``` -We can now compare this with the exponential, log-normal, and Pareto distributions +我们现在可以将其与指数分布、对数正态分布和帕累托分布进行比较 ```{code-cell} ipython3 -# Build figure +# 构建图形 fig, axes = plt.subplots(1, 3, figsize=(12, 4)) axes = axes.flatten() labels = ['exponential', 'lognormal', 'Pareto'] for data, label, ax in zip(data_list, labels, axes): - sm.qqplot(data, line='45', ax=ax, ) + sm.qqplot(data, line='45', ax=ax) ax.set_title(label) plt.tight_layout() plt.show() ``` -### Power laws - +### 幂律 -One specific class of heavy-tailed distributions has been found repeatedly in -economic and social phenomena: the class of so-called power laws. +在经济和社会现象中,一类特定的重尾分布被反复发现:所谓的幂律。 -A random variable $X$ is said to have a **power law** if, for some $\alpha > 0$, +若随机变量 $X$ 满足**幂律**,则存在某个 $\alpha > 0$, ```{math} \mathbb P\{X > x\} \approx x^{-\alpha} -\quad \text{when $x$ is large} +\quad \text{当 $x$ 很大} ``` -We can write this more mathematically as +我们可以更数学化地写成 ```{math} :label: plrt \lim_{x \to \infty} x^\alpha \, \mathbb P\{X > x\} = c -\quad \text{for some $c > 0$} +\quad \text{对某个 $c > 0$} ``` -It is also common to say that a random variable $X$ with this property -has a **Pareto tail** with **tail index** $\alpha$. +通常说随机变量 $X$ 有这样的性质, +具有**帕累托尾**和**尾指数** $\alpha$。 -Notice that every Pareto distribution with tail index $\alpha$ -has a **Pareto tail** with **tail index** $\alpha$. +注意,每个具有尾指数 $\alpha$ 的帕累托分布 +都具有**帕累托尾**和**尾指数** $\alpha$。 -We can think of power laws as a generalization of Pareto distributions. +我们可以将幂律视为帕累托分布的一种概括。 -They are distributions that resemble Pareto distributions in their upper right -tail. +这些分布在它们的右上尾部类似于帕累托分布。 -Another way to think of power laws is a set of distributions with a specific -kind of (very) heavy tail. +另一种考虑幂律的方式是将其视为一组分布,具有特定类型的(非常)重尾。 -## Heavy tails in economic cross-sections +## 经济横截面中的重尾现象 -As mentioned above, heavy tails are pervasive in economic data. +如上所述,重尾在经济数据中非常普遍。 -In fact power laws seem to be very common as well. +实际上,幂律分布似乎也十分常见。 -We now illustrate this by showing the empirical CCDF of heavy tails. +我们现在通过展示重尾的经验互补累积分布函数(CCDF)来说明这一点。 -All plots are in log-log, so that a power law shows up as a linear log-log -plot, at least in the upper tail. +所有的图都是以对数-对数坐标绘制的,因此幂律在对数-对数图中表现为直线,至少在上尾部分是这样。 -We hide the code that generates the figures, which is somewhat complex, but -readers are of course welcome to explore the code (perhaps after examining the figures). +我们隐藏了生成图形的代码,该代码有些复杂,但读者当然可以探索代码(也许可以在查看图形后进行)。 ```{code-cell} ipython3 :tags: [hide-input] def empirical_ccdf(data, ax, - aw=None, # weights + aw=None, # 权重 label=None, xlabel=None, add_reg_line=False, title=None): """ - Take data vector and return prob values for plotting. - Upgraded empirical_ccdf + 接受数据向量并返回用于绘图的概率值。 + 升级版的 empirical_ccdf """ y_vals = np.empty_like(data, dtype='float64') p_vals = np.empty_like(data, dtype='float64') n = len(data) if aw is None: for i, d in enumerate(data): - # record fraction of sample above d + # 记录样本中大于 d 的分数 y_vals[i] = np.sum(data >= d) / n p_vals[i] = np.sum(data == d) / n else: @@ -771,11 +722,11 @@ def empirical_ccdf(data, ax.scatter(x, y, **kwargs) if add_reg_line: - ax.plot(x, x * a + b, 'k-', alpha=0.6, label=f"slope = ${a: 1.2f}$") + ax.plot(x, x * a + b, 'k-', alpha=0.6, label=f"斜率 = ${a: 1.2f}$") if not xlabel: - xlabel='log value' + xlabel='对数值' ax.set_xlabel(xlabel, fontsize=12) - ax.set_ylabel("log prob", fontsize=12) + ax.set_ylabel("对数概率", fontsize=12) if label: ax.legend(loc='lower left', fontsize=12) @@ -795,7 +746,7 @@ def extract_wb(varlist=['NY.GDP.MKTP.CD'], e=2021, varnames=None): if c == "all_countries": - # Keep countries only (no aggregated regions) + # 仅保留国家(不包括汇总区域) countries = wb.get_countries() countries_name = countries[countries['region'] != 'Aggregates']['name'].values c = "all" @@ -810,15 +761,15 @@ def extract_wb(varlist=['NY.GDP.MKTP.CD'], return df1 ``` -### Firm size +### 公司规模 -Here is a plot of the firm size distribution for the largest 500 firms in 2020 taken from Forbes Global 2000. +以下是2020年来自福布斯全球2000强的最大500家公司的公司规模分布图。 ```{code-cell} ipython3 --- mystnb: figure: - caption: Firm size distribution + caption: 公司规模分布 name: firm-size-dist tags: [hide-input] --- @@ -826,29 +777,29 @@ df_fs = pd.read_csv('https://media.githubusercontent.com/media/QuantEcon/high_di df_fs = df_fs[['Country', 'Sales', 'Profits', 'Assets', 'Market Value']] fig, ax = plt.subplots(figsize=(6.4, 3.5)) -label="firm size (market value)" -top = 500 # set the cutting for top +label="公司规模(市值)" +top = 500 # 设置排名前500的切断点 d = df_fs.sort_values('Market Value', ascending=False) empirical_ccdf(np.asarray(d['Market Value'])[:top], ax, label=label, add_reg_line=True) plt.show() ``` -### City size +### 城市规模 -Here are plots of the city size distribution for the US and Brazil in 2023 from the World Population Review. +以下是2023年来自世界人口审查的美国和巴西城市规模分布图。 -The size is measured by population. +大小由人口衡量。 ```{code-cell} ipython3 --- mystnb: figure: - caption: City size distribution + caption: 城市规模分布 name: city-size-dist tags: [hide-input] --- -# import population data of cities in 2023 United States and 2023 Brazil from world population review +# 导入2023年美国和2023年巴西城市的人口数据 df_cs_us = pd.read_csv('https://media.githubusercontent.com/media/QuantEcon/high_dim_data/main/cross_section/cities_us.csv') df_cs_br = pd.read_csv('https://media.githubusercontent.com/media/QuantEcon/high_dim_data/main/cross_section/cities_brazil.csv') @@ -860,17 +811,17 @@ empirical_ccdf(np.asarray(df_cs_br['pop2023']), axes[1], label="Brazil", add_reg plt.show() ``` -### Wealth +### 财富 -Here is a plot of the upper tail (top 500) of the wealth distribution. +这里是财富分布上尾部(前500名)的图表示。 -The data is from the Forbes Billionaires list in 2020. +数据来源于2020年的《福布斯亿万富翁》名单。 ```{code-cell} ipython3 --- mystnb: figure: - caption: Wealth distribution (Forbes billionaires in 2020) + caption: 财富分布(2020年福布斯亿万富翁) name: wealth-dist tags: [hide-input] --- @@ -887,12 +838,12 @@ axs = axs.flatten() for i, c in enumerate(countries): df_w_c = df_w[df_w['country'] == c].reset_index() z = np.asarray(df_w_c['realTimeWorth']) - # print('number of the global richest 2000 from '+ c, len(z)) - top = 500 # cut-off number: top 500 + # 打印来自该国的全球前2000位富豪的人数 + top = 500 # 截止数:前500名 if len(z) <= top: z = z[:top] - empirical_ccdf(z[:top], axs[i], label=c, xlabel='log wealth', add_reg_line=True) + empirical_ccdf(z[:top], axs[i], label=c, xlabel='对数财富', add_reg_line=True) fig.tight_layout() @@ -901,17 +852,17 @@ plt.show() ### GDP -Of course, not all cross-sectional distributions are heavy-tailed. +当然,并非所有的横截面分布都是重尾的。 -Here we show cross-country per capita GDP. +这里我们展示的是各国人均GDP。 ```{code-cell} ipython3 :tags: [hide-input] -# get gdp and gdp per capita for all regions and countries in 2021 +# 获取2021年所有地区和国家的GDP及人均GDP variable_code = ['NY.GDP.MKTP.CD', 'NY.GDP.PCAP.CD'] -variable_names = ['GDP', 'GDP per capita'] +variable_names = ['GDP', '人均GDP'] df_gdp1 = extract_wb(varlist=variable_code, c="all_countries", @@ -925,7 +876,7 @@ df_gdp1.dropna(inplace=True) --- mystnb: figure: - caption: GDP per capita distribution + caption: 人均GDP分布 name: gdppc-dist tags: [hide-input] --- @@ -937,24 +888,20 @@ for name, ax in zip(variable_names, axes): plt.show() ``` -The plot is concave rather than linear, so the distribution has light tails. +图形是凹形的而不是线形的,因此分布具有轻尾。 -One reason is that this is data on an aggregate variable, which involves some -averaging in its definition. +这其中一个原因是这是关于一个综合变量的数据,其定义中涉及了一些平均处理。 -Averaging tends to eliminate extreme outcomes. +平均处理往往会消除极端结果。 -## Failure of the LLN +## 大数定律的失败 -One impact of heavy tails is that sample averages can be poor estimators of -the underlying mean of the distribution. +重尾分布的一个影响是样本平均值可能是底层均值的糟糕估计。 -To understand this point better, recall {doc}`our earlier discussion ` -of the law of large numbers, which considered IID $X_1, \ldots, X_n$ with common distribution $F$ +为了更好地理解这一点,请回想一下{doc}`我们早期关于大数定律的讨论`,它考虑了具有共同分布$F$的独立同分布的$X_1, \ldots, X_n$ -If $\mathbb E |X_i|$ is finite, then -the sample mean $\bar X_n := \frac{1}{n} \sum_{i=1}^n X_i$ satisfies +如果$\mathbb E |X_i|$是有限的,那么样本平均值$\bar X_n := \frac{1}{n} \sum_{i=1}^n X_i$满足 ```{math} :label: lln_as2 @@ -962,15 +909,13 @@ the sample mean $\bar X_n := \frac{1}{n} \sum_{i=1}^n X_i$ satisfies \mathbb P \left\{ \bar X_n \to \mu \text{ as } n \to \infty \right\} = 1 ``` -where $\mu := \mathbb E X_i = \int x F(dx)$ is the common mean of the sample. +其中$\mu := \mathbb E X_i = \int x F(dx)$是样本的共同均值。 -The condition $\mathbb E | X_i | = \int |x| F(dx) < \infty$ holds -in most cases but can fail if the distribution $F$ is very heavy-tailed. +在大多数情况下,条件$\mathbb E | X_i | = \int |x| F(dx) < \infty$成立,但如果分布$F$是非常重尾的,则可能不成立。 -For example, it fails for the Cauchy distribution. +例如,柯西分布就是不成立的。 -Let's have a look at the behavior of the sample mean in this case, and see -whether or not the LLN is still valid. +让我们来看看这种情况下样本平均值的行为,看是否大数定律仍然有效。 ```{code-cell} ipython3 --- @@ -989,12 +934,12 @@ distribution = cauchy() fig, ax = plt.subplots() data = distribution.rvs(N) -# Compute sample mean at each n +# 计算每个n的样本平均值 sample_mean = np.empty(N) for n in range(1, N): sample_mean[n] = np.mean(data[:n]) -# Plot +# 绘图 ax.plot(range(N), sample_mean, alpha=0.6, label='$\\bar{X}_n$') ax.plot(range(N), np.zeros(N), 'k--', lw=0.5) ax.set_xlabel(r"$n$") @@ -1003,181 +948,162 @@ ax.legend() plt.show() ``` -The sequence shows no sign of converging. +序列显示没有收敛的迹象。 -We return to this point in the exercises. +我们在练习中会回到这一点。 (heavy-tail:application)= -## Why do heavy tails matter? +## 为什么重尾分布很重要? -We have now seen that +我们已经看到 -1. heavy tails are frequent in economics and -2. the law of large numbers fails when tails are very heavy. +1. 在经济学中,重尾分布非常常见; +2. 当尾部非常重时,大数定律失效。 -But what about in the real world? Do heavy tails matter? +但是在现实世界中,重尾分布重要吗?让我们简要讨论一下它们为什么重要。 -Let's briefly discuss why they do. +### 分散化投资 +投资中一个最重要的概念是使用分散化来降低风险。 -### Diversification +这是一个非常古老的想法——例如,考虑这个表达式“不要把所有的鸡蛋放在一个篮子里”。 -One of the most important ideas in investing is using diversification to -reduce risk. +为了说明这一点,设想一个拥有一美元财富的投资者,在$n$种资产中进行选择,这些资产的回报为$X_1, \ldots, X_n$。 -This is a very old idea --- consider, for example, the expression "don't put all your eggs in one basket". +假设不同资产的回报是独立的,每个回报有均值$\mu$和方差$\sigma^2$。 -To illustrate, consider an investor with one dollar of wealth and a choice over -$n$ assets with payoffs $X_1, \ldots, X_n$. +如果投资者将所有财富投资在一个资产上,那么该投资组合的预期收益为$\mu$,方差为$\sigma^2$。 -Suppose that returns on distinct assets are -independent and each return has mean $\mu$ and variance $\sigma^2$. - -If the investor puts all wealth in one asset, say, then the expected payoff of the -portfolio is $\mu$ and the variance is $\sigma^2$. - -If instead the investor puts share $1/n$ of her wealth in each asset, then the portfolio payoff is +如果投资者将她的财富均分到每一个资产上,即每个资产的份额为$1/n$,那么投资组合的收益为 $$ -Y_n = \sum_{i=1}^n \frac{X_i}{n} = \frac{1}{n} \sum_{i=1}^n X_i. -$$ - -Try computing the mean and variance. - -You will find that +Y_n = \sum_{i=1}^n \frac{X_i}{n} = \frac{1}{n} \sum_{i=1}^n X_i. +$$ -* The mean is unchanged at $\mu$, while -* the variance of the portfolio has fallen to $\sigma^2 / n$. +试着计算均值和方差。 -Diversification reduces risk, as expected. +你会发现: -But there is a hidden assumption here: the variance of returns is finite. +* 均值保持不变,依旧是$\mu$, +* 投资组合的方差降低到了$\sigma^2 / n$。 -If the distribution is heavy-tailed and the variance is infinite, then this -logic is incorrect. +正如预期的那样,分散化减少了风险。 -For example, we saw above that if every $X_i$ is Cauchy, then so is $Y_n$. +但是这里有一个隐藏的假设:回报的方差是有限的。 -This means that diversification doesn't help at all! +如果分布是重尾的并且方差是无限的,那么这种逻辑就是不正确的。 +例如,我们上面看到,如果每个$X_i$都是柯西分布,那么$Y_n$也是。 -### Fiscal policy +这意味着分散化根本没有帮助! -The heaviness of the tail in the wealth distribution matters for taxation and redistribution policies. +### 财政政策 -The same is true for the income distribution. +财富分配中尾部的厚重程度对税收和再分配政策至关重要。 -For example, the heaviness of the tail of the income distribution helps -determine {doc}`how much revenue a given tax policy will raise `. +收入分配也是如此。 +例如,收入分配尾部的厚重程度有助于决定{doc}`how much revenue a given tax policy will raise `。 (cltail)= -## Classifying tail properties +## 分类尾部特性 -Up until now we have discussed light and heavy tails without any mathematical -definitions. +到目前为止,我们讨论了轻尾和重尾,但没有任何数学定义。 -Let's now rectify this. +现在让我们来纠正这一点。 -We will focus our attention on the right hand tails of -nonnegative random variables and their distributions. +我们将关注非负随机变量及其分布的右侧尾部。 -The definitions for -left hand tails are very similar and we omit them to simplify the exposition. +左侧尾部的定义非常相似,为了简化论述,我们省略它们。 (heavy-tail:formal-definition)= -### Light and heavy tails +### 轻尾和重尾 -A distribution $F$ with density $f$ on $\mathbb R_+$ is called [heavy-tailed](https://en.wikipedia.org/wiki/Heavy-tailed_distribution) if +一个在 $\mathbb R_+$ 上有密度 $f$ 的分布 $F$ 被称为[重尾](https://en.wikipedia.org/wiki/Heavy-tailed_distribution)的,如果 ```{math} :label: defht -\int_0^\infty \exp(tx) f(x) dx = \infty \; \text{ for all } t > 0. +\int_0^\infty \exp(tx) f(x) dx = \infty \; \text{ 对于所有 } t > 0. ``` -We say that a nonnegative random variable $X$ is **heavy-tailed** if its density is heavy-tailed. +我们说一个非负随机变量 $X$ 是**重尾**的,如果它的密度是重尾的。 -This is equivalent to stating that its **moment generating function** $m(t) := -\mathbb E \exp(t X)$ is infinite for all $t > 0$. +这等同于说它的**矩生成函数** $m(t) := \mathbb E \exp(t X)$ 对于所有 $t > 0$ 都是无限的。 -For example, the [log-normal -distribution](https://en.wikipedia.org/wiki/Log-normal_distribution) is -heavy-tailed because its moment generating function is infinite everywhere on -$(0, \infty)$. +例如,[对数正态分布](https://en.wikipedia.org/wiki/Log-normal_distribution)是重尾的,因为它的矩生成函数在 $(0, \infty)$ 上无限。 -The Pareto distribution is also heavy-tailed. -Less formally, a heavy-tailed distribution is one that is not exponentially bounded (i.e. the tails are heavier than the exponential distribution). +帕累托分布也是重尾分布。 -A distribution $F$ on $\mathbb R_+$ is called **light-tailed** if it is not heavy-tailed. +非正式地说,重尾分布是指不受指数型界限约束的分布(即尾部比指数分布更重)。 -A nonnegative random variable $X$ is **light-tailed** if its distribution $F$ is light-tailed. +如果一个在 $\mathbb R_+$ 上的分布 $F$ 不是重尾的,则称它为**轻尾**。 -For example, every random variable with bounded support is light-tailed. (Why?) +一个非负随机变量 $X$ 是**轻尾的**,如果它的分布 $F$ 是轻尾的。 -As another example, if $X$ has the [exponential distribution](https://en.wikipedia.org/wiki/Exponential_distribution), with cdf $F(x) = 1 - \exp(-\lambda x)$ for some $\lambda > 0$, then its moment generating function is +例如,所有有界支撑的随机变量都是轻尾的。(为什么?) + +再举一个例子,如果 $X$ 有[指数分布](https://en.wikipedia.org/wiki/Exponential_distribution),累积分布函数 $F(x) = 1 - \exp(-\lambda x)$ 对某个 $\lambda > 0$,则其矩生成函数为 $$ -m(t) = \frac{\lambda}{\lambda - t} \quad \text{when } t < \lambda +m(t) = \frac{\lambda}{\lambda - t} \quad \text{当 } t < \lambda $$ -In particular, $m(t)$ is finite whenever $t < \lambda$, so $X$ is light-tailed. - -One can show that if $X$ is light-tailed, then all of its -[moments](https://en.wikipedia.org/wiki/Moment_(mathematics)) are finite. +特别地,只要 $t < \lambda$,$m(t)$ 就是有限的,因此 $X$ 是轻尾的。 -Conversely, if some moment is infinite, then $X$ is heavy-tailed. +可以证明,如果 $X$ 是轻尾的,则它的所有 +[矩](https://en.wikipedia.org/wiki/Moment_(mathematics))都是有限的。 -The latter condition is not necessary, however. +反之,如果某个矩是无限的,则 $X$ 是重尾的。 -For example, the lognormal distribution is heavy-tailed but every moment is finite. +但后者条件不是必需的。 +例如,对数正态分布是重尾的,但每个矩都是有限的。 -## Further reading -For more on heavy tails in the wealth distribution, see e.g., {cite}`pareto1896cours` and {cite}`benhabib2018skewed`. +## 延伸阅读 -For more on heavy tails in the firm size distribution, see e.g., {cite}`axtell2001zipf`, {cite}`gabaix2016power`. +想了解更多关于财富分布中的重尾,可以参考文献 {cite}`pareto1896cours` 和 {cite}`benhabib2018skewed`。 -For more on heavy tails in the city size distribution, see e.g., {cite}`rozenfeld2011area`, {cite}`gabaix2016power`. +想了解更多关于公司规模分布中的重尾,可以参考文献 {cite}`axtell2001zipf`, {cite}`gabaix2016power`。 -There are other important implications of heavy tails, aside from those -discussed above. +想了解更多关于城市规模分布中的重尾,可以参考文献 {cite}`rozenfeld2011area`, {cite}`gabaix2016power`。 -For example, heavy tails in income and wealth affect productivity growth, business cycles, and political economy. +重尾的其他重要影响,除了上述讨论之外,还有不少。 -For further reading, see, for example, {cite}`acemoglu2002political`, {cite}`glaeser2003injustice`, {cite}`bhandari2018inequality` or {cite}`ahn2018inequality`. +例如,收入和财富中的重尾会影响生产力增长、商业周期和政治经济学。 +欲了解更多,请参阅 {cite}`acemoglu2002political`, {cite}`glaeser2003injustice`, {cite}`bhandari2018inequality` 或 {cite}`ahn2018inequality`。 -## Exercises +## 练习 ```{exercise} :label: ht_ex2 -Prove: If $X$ has a Pareto tail with tail index $\alpha$, then -$\mathbb E[X^r] = \infty$ for all $r \geq \alpha$. +证明:如果 $X$ 拥有尾指数为 $\alpha$ 的帕累托尾,则 +$\mathbb E[X^r] = \infty$ 对所有的 $r \geq \alpha$ 都成立。 ``` ```{solution-start} ht_ex2 :class: dropdown ``` -Let $X$ have a Pareto tail with tail index $\alpha$ and let $F$ be its cdf. +设 $X$ 拥有尾指数为 $\alpha$ 的帕累托尾,并且设 $F$ 为其累积分布函数。 -Fix $r \geq \alpha$. +固定 $r \geq \alpha$。 -In view of {eq}`plrt`, we can take positive constants $b$ and $\bar x$ such that +根据公式 {eq}`plrt`,我们可以取正常数 $b$ 和 $\bar x$,使得 $$ -\mathbb P\{X > x\} \geq b x^{- \alpha} \text{ whenever } x \geq \bar x +\mathbb P\{X > x\} \geq b x^{-\alpha} \text{ 当 } x \geq \bar x $$ -But then +但是 $$ \mathbb E X^r = r \int_0^\infty x^{r-1} \mathbb P\{ X > x \} dx @@ -1186,24 +1112,21 @@ r \int_0^{\bar x} x^{r-1} \mathbb P\{ X > x \} dx + r \int_{\bar x}^\infty x^{r-1} b x^{-\alpha} dx. $$ -We know that $\int_{\bar x}^\infty x^{r-\alpha-1} dx = \infty$ whenever $r - \alpha - 1 \geq -1$. +我们知道 $\int_{\bar x}^\infty x^{r-\alpha-1} dx = \infty$ 当 $r - \alpha - 1 \geq -1$ 时。 -Since $r \geq \alpha$, we have $\mathbb E X^r = \infty$. +由于 $r \geq \alpha$,我们得到 $\mathbb E X^r = \infty$。 ```{solution-end} ``` - ```{exercise} :label: ht_ex3 -Repeat exercise 1, but replace the three distributions (two normal, one -Cauchy) with three Pareto distributions using different choices of -$\alpha$. +重复练习1,但将三个分布(两个正态,一个柯西)替换为三个帕累托分布,并使用不同的 $lpha$ 值。 -For $\alpha$, try 1.15, 1.5 and 1.75. +对于 $\alpha$,尝试1.15、1.5和1.75。 -Use `np.random.seed(11)` to set the seed. +使用 `np.random.seed(11)` 来设置种子。 ``` @@ -1236,95 +1159,77 @@ plt.show() ```{solution-end} ``` - ```{exercise} :label: ht_ex5 -There is an ongoing argument about whether the firm size distribution should -be modeled as a Pareto distribution or a lognormal distribution (see, e.g., -{cite}`fujiwara2004pareto`, {cite}`kondo2018us` or {cite}`schluter2019size`). +关于企业规模分布应该用帕累托分布还是对数正态分布进行建模的争论一直持续不断(参见例如 {cite}`fujiwara2004pareto`、{cite}`kondo2018us` 或 {cite}`schluter2019size`)。 -This sounds esoteric but has real implications for a variety of economic -phenomena. +这听起来很深奥,但对各种经济现象都有实际影响。 -To illustrate this fact in a simple way, let us consider an economy with -100,000 firms, an interest rate of `r = 0.05` and a corporate tax rate of -15%. +为了简单说明这个事实,我们考虑一个拥有100,000家企业的经济体,利率为 `r = 0.05` 并且企业税率为15%。 -Your task is to estimate the present discounted value of projected corporate -tax revenue over the next 10 years. +你的任务是估计未来10年企业税收的现值折现。 -Because we are forecasting, we need a model. +因为我们在进行预测,我们需要一个模型。 -We will suppose that +我们将假设: -1. the number of firms and the firm size distribution (measured in profits) remain fixed and -1. the firm size distribution is either lognormal or Pareto. +1. 企业数量和企业规模分布(以利润计)保持不变,并且 +1. 企业规模分布要么是对数正态分布,要么是帕累托分布。 -Present discounted value of tax revenue will be estimated by +税收的现值折现将通过以下步骤估算: -1. generating 100,000 draws of firm profit from the firm size distribution, -1. multiplying by the tax rate, and -1. summing the results with discounting to obtain present value. +1. 从企业规模分布生成100,000次企业利润的抽样, +1. 将其乘以税率,并且 +1. 通过折现将结果求和以获得现值。 -The Pareto distribution is assumed to take the form {eq}`pareto` with $\bar x = 1$ and $\alpha = 1.05$. +帕累托分布假设采用 {eq}`pareto` 形式,其中 $\bar x = 1$ 且 $\alpha = 1.05$。 -(The value of the tail index $\alpha$ is plausible given the data {cite}`gabaix2016power`.) +(尾指数 $\alpha$ 的值是合理的,鉴于数据 {cite}`gabaix2016power`。) -To make the lognormal option as similar as possible to the Pareto option, choose -its parameters such that the mean and median of both distributions are the same. +为了使对数正态选项尽可能类似于帕累托选项,请选择其参数使得两个分布的均值和中位数相同。 -Note that, for each distribution, your estimate of tax revenue will be random -because it is based on a finite number of draws. +注意,对于每个分布,你的税收估算都是随机的,因为它基于有限的抽样。 -To take this into account, generate 100 replications (evaluations of tax revenue) -for each of the two distributions and compare the two samples by +为了考虑到这一点,对两种分布各产生100次重复(税收收入的估计),并通过以下方式比较两个样本: -* producing a [violin plot](https://en.wikipedia.org/wiki/Violin_plot) visualizing the two samples side-by-side and -* printing the mean and standard deviation of both samples. +* 制作一张将两个样本并排显示的[小提琴图](https://en.wikipedia.org/wiki/Violin_plot),并且 +* 输出两个样本的均值和标准差。 -For the seed use `np.random.seed(1234)`. +对种子使用 `np.random.seed(1234)`。 -What differences do you observe? +你观察到了哪些差异? -(Note: a better approach to this problem would be to model firm dynamics and -try to track individual firms given the current distribution. We will discuss -firm dynamics in later lectures.) +(注:解决这个问题的更好方法将是建模企业动态并尝试给定当前分布追踪个别企业。我们将在后续讲座中讨论企业动态。) ``` ```{solution-start} ht_ex5 :class: dropdown ``` -To do the exercise, we need to choose the parameters $\mu$ -and $\sigma$ of the lognormal distribution to match the mean and median -of the Pareto distribution. +为了完成这个练习,我们需要选择对数正态分布的参数 $\mu$ 和 $\sigma$,使其匹配Pareto分布的均值和中位数。 -Here we understand the lognormal distribution as that of the random variable -$\exp(\mu + \sigma Z)$ when $Z$ is standard normal. +这里我们将对数正态分布理解为随机变量 $\exp(\mu + \sigma Z)$,当 $Z$ 是标准正态分布时。 -The mean and median of the Pareto distribution {eq}`pareto` with -$\bar x = 1$ are +Pareto分布 {eq}`pareto` 的均值和中位数,其中 $\bar x = 1$,分别是 $$ -\text{mean } = \frac{\alpha}{\alpha - 1} -\quad \text{and} \quad -\text{median } = 2^{1/\alpha} +\text{均值} = \frac{\alpha}{\alpha - 1} +\quad \text{和} \quad +\text{中位数} = 2^{1/\alpha} $$ -Using the corresponding expressions for the lognormal distribution leads us to -the equations +使用对应的对数正态分布表达式,我们得到以下方程组 $$ \frac{\alpha}{\alpha - 1} = \exp(\mu + \sigma^2/2) -\quad \text{and} \quad +\quad \text{和} \quad 2^{1/\alpha} = \exp(\mu) $$ -which we solve for $\mu$ and $\sigma$ given $\alpha = 1.05$. +我们用 $\alpha = 1.05$ 来解这些方程得到 $\mu$ 和 $\sigma$。 -Here is the code that generates the two samples, produces the violin plot and -prints the mean and standard deviation of the two samples. +以下是生成两个样本、制作小提琴图并打印两个样本的均值和标准差的代码。 ```{code-cell} ipython3 num_firms = 100_000 @@ -1332,19 +1237,19 @@ num_years = 10 tax_rate = 0.15 r = 0.05 -β = 1 / (1 + r) # discount factor +β = 1 / (1 + r) # 折现因子 x_bar = 1.0 α = 1.05 def pareto_rvs(n): - "Uses a standard method to generate Pareto draws." + "使用标准方法生成Pareto抽样。" u = np.random.uniform(size=n) y = x_bar / (u**(1/α)) return y ``` -Let's compute the lognormal parameters: +我们来计算对数正态分布的参数: ```{code-cell} ipython3 μ = np.log(2) / α @@ -1352,8 +1257,7 @@ Let's compute the lognormal parameters: σ = np.sqrt(σ_sq) ``` -Here's a function to compute a single estimate of tax revenue for a particular -choice of distribution `dist`. +这是一个计算特定分布 `dist` 的单一税收估计的函数。 ```{code-cell} ipython3 def tax_rev(dist): @@ -1367,7 +1271,7 @@ def tax_rev(dist): return tax_raised ``` -Now let's generate the violin plot. +现在让我们生成小提琴图。 ```{code-cell} ipython3 num_reps = 100 @@ -1388,8 +1292,7 @@ ax.violinplot(data) plt.show() ``` - -Finally, let's print the means and standard deviations. +最后,我们来生成均值和标准差。 ```{code-cell} ipython3 tax_rev_pareto.mean(), tax_rev_pareto.std() @@ -1399,8 +1302,7 @@ tax_rev_pareto.mean(), tax_rev_pareto.std() tax_rev_lognorm.mean(), tax_rev_lognorm.std() ``` -Looking at the output of the code, our main conclusion is that the Pareto -assumption leads to a lower mean and greater dispersion. +通过查看代码的输出,我们的主要结论是,帕累托分布假设会导致更低的均值和更大的离散度。 ```{solution-end} ``` @@ -1408,17 +1310,15 @@ assumption leads to a lower mean and greater dispersion. ```{exercise} :label: ht_ex_cauchy -The [characteristic function](https://en.wikipedia.org/wiki/Characteristic_function_%28probability_theory%29) of the Cauchy distribution is +柯西分布的[特征函数](https://en.wikipedia.org/wiki/Characteristic_function_%28probability_theory%29)为 $$ \phi(t) = \mathbb E e^{itX} = \int e^{i t x} f(x) dx = e^{-|t|} $$ (lln_cch) -Prove that the sample mean $\bar X_n$ of $n$ independent draws $X_1, \ldots, -X_n$ from the Cauchy distribution has the same characteristic function as -$X_1$. +证明 $n$ 次独立抽样 $X_1, \ldots, X_n$ 从柯西分布得来的样本均值 $\bar X_n$ 具有与 $X_1$ 相同的特征函数。 -(This means that the sample mean never converges.) +(这意味着样本均值永远不会收敛。) ``` @@ -1426,7 +1326,7 @@ $X_1$. :class: dropdown ``` -By independence, the characteristic function of the sample mean becomes +由独立性,样本均值的特征函数变为 $$ \begin{aligned} @@ -1440,9 +1340,9 @@ $$ \end{aligned} $$ -In view of {eq}`lln_cch`, this is just $e^{-|t|}$. +根据 {eq}`lln_cch`,这就是 $e^{-|t|}$。 -Thus, in the case of the Cauchy distribution, the sample mean itself has the very same Cauchy distribution, regardless of $n$! +因此,在柯西分布的情况下,样本均值本身具有完全相同的柯西分布,无论 $n$ 是多少! ```{solution-end} ``` diff --git a/lectures/inequality.md b/lectures/inequality.md index b1ec0e1..087bc39 100644 --- a/lectures/inequality.md +++ b/lectures/inequality.md @@ -4,79 +4,71 @@ jupytext: extension: .md format_name: myst format_version: 0.13 - jupytext_version: 1.15.1 + jupytext_version: 1.16.1 kernelspec: display_name: Python 3 (ipykernel) language: python name: python3 --- -# Income and Wealth Inequality +# 收入与财富不平等 -## Overview +## 概览 -In the lecture {doc}`long_run_growth` we studied how GDP per capita has changed -for certain countries and regions. +在{doc}`long_run_growth` 中,我们研究了某些国家和地区的人均国内生产总值是如何变化的。 +的变化。 -Per capita GDP is important because it gives us an idea of average income for -households in a given country. +人均 GDP 很重要,因为它能让我们了解某个国家的家庭平均收入。 -However, when we study income and wealth, averages are only part of the story. +然而,当我们研究收入和财富时,平均数只是其中的一部分。 ```{prf:example} :label: ie_ex_av -For example, imagine two societies, each with one million people, where +例如,假设有两个社会,每个社会都有 100 万人,其中 -* in the first society, the yearly income of one man is $100,000,000 and the income of the - others are zero -* in the second society, the yearly income of everyone is $100 +* 在第一个社会中,一个人的年收入是 $100,000,000,其他人的年收入为零。 + 其他人的收入为零 +* 在第二个社会中,每个人的年收入都是 100 美元 -These countries have the same income per capita (average income is $100) but the lives of the people will be very different (e.g., almost everyone in the first society is -starving, even though one person is fabulously rich). +这些国家的人均收入相同(平均收入为 100 美元),但人民的生活却大不相同(例如,在第一个社会中,几乎每个人都在挨饿,尽管有一个人非常富有)。 ``` -The example above suggests that we should go beyond simple averages when we study income and wealth. +上面的例子表明,我们在研究收入和财富时,不应该仅仅局限于简单的平均数。 -This leads us to the topic of economic inequality, which examines how income and wealth (and other quantities) are distributed across a population. +这就引出了经济不平等的话题,即研究收入和财富(以及其他数量)如何在人口中分配。 -In this lecture we study inequality, beginning with measures of inequality and -then applying them to wealth and income data from the US and other countries. +在本讲座中,我们将研究不平等问题,首先探讨不平等的衡量标准,然后将其应用于美国和其他国家的财富和收入数据。 +### 一些历史 -### Some history +许多历史学家认为,不平等在罗马共和国的衰落中扮演了重要角色(参见{cite}`levitt2019did`等)。 -Many historians argue that inequality played a role in the fall of the Roman Republic (see, e.g., {cite}`levitt2019did`). +在打败迦太基和入侵西班牙之后,资金从帝国各地流入罗马,极大地富裕了掌权者。 -Following the defeat of Carthage and the invasion of Spain, money flowed into -Rome from across the empire, greatly enriched those in power. +与此同时,普通公民却被从农田中抽调出来长期作战,使他们的财富不断减少。 -Meanwhile, ordinary citizens were taken from their farms to fight for long -periods, diminishing their wealth. +由此导致的不平等现象加剧,成为动摇共和国根基的政治动荡的驱动因素。 -The resulting growth in inequality was a driving factor behind political turmoil that shook the foundations of the republic. +最终,罗马共和国让位于一系列独裁政权 -- 从公元前 27 年的[屋大维](https://en.wikipedia.org/wiki/Augustus)(奥古斯都)开始。 -Eventually, the Roman Republic gave way to a series of dictatorships, starting with [Octavian](https://en.wikipedia.org/wiki/Augustus) (Augustus) in 27 BCE. +这段历史告诉我们,不平等很重要,因为它可以推动世界重大事件的发生。 -This history tells us that inequality matters, in the sense that it can drive major world events. +不平等之所以重要,还有其他原因,比如它如何影响人类福祉。 -There are other reasons that inequality might matter, such as how it affects -human welfare. +有了这些动机,让我们开始思考什么是不平等,以及如何量化和分析不平等。 -With this motivation, let us start to think about what inequality is and how we -can quantify and analyze it. +### 测量 -### Measurement +在政界和大众媒体中,“不平等 ”一词的使用往往相当宽泛,没有任何确切的定义。 -In politics and popular media, the word "inequality" is often used quite loosely, without any firm definition. +要从科学的角度来看待不平等这一话题,我们必须从谨慎的定义开始。 -To bring a scientific perspective to the topic of inequality we must start with careful definitions. +因此,我们首先讨论经济研究中衡量不平等的方法。 -Hence we begin by discussing ways that inequality can be measured in economic research. - -We will need to install the following packages +我们需要安装以下的Python包 ```{code-cell} ipython3 :tags: [hide-output] @@ -84,7 +76,7 @@ We will need to install the following packages !pip install wbgapi plotly ``` -We will also use the following imports. +我们还将使用以下导入。 ```{code-cell} ipython3 import pandas as pd @@ -93,31 +85,34 @@ import matplotlib.pyplot as plt import random as rd import wbgapi as wb import plotly.express as px -``` - +import matplotlib as mpl +FONTPATH = "fonts/SourceHanSerifSC-SemiBold.otf" +mpl.font_manager.fontManager.addfont(FONTPATH) +plt.rcParams['font.family'] = ['Source Han Serif SC'] +``` -## The Lorenz curve +## 洛伦兹曲线 -One popular measure of inequality is the Lorenz curve. +洛伦兹曲线是衡量不平等的一个常用指标。 -In this section we define the Lorenz curve and examine its properties. +在本节中,我们将定义洛伦兹曲线并研究其特性。 -### Definition +### 定义 -The Lorenz curve takes a sample $w_1, \ldots, w_n$ and produces a curve $L$. +洛伦兹曲线取一个样本 $w_1,\ldots,w_n$,并生成一条曲线 $L$。 -We suppose that the sample has been sorted from smallest to largest. +我们假设样本从小到大排序。 -To aid our interpretation, suppose that we are measuring wealth +为了便于解释,假设我们测量的是财富 -* $w_1$ is the wealth of the poorest member of the population, and -* $w_n$ is the wealth of the richest member of the population. +* $w_1$ 是人口中最贫穷成员的财富,而 +* $w_n$ 是人口中最富有成员的财富。 -The curve $L$ is just a function $y = L(x)$ that we can plot and interpret. +曲线 $L$ 就是我们可以绘制和解释的函数 $y = L(x)$。 -To create it we first generate data points $(x_i, y_i)$ according to +要绘制这条曲线,我们首先要根据以下公式生成数据点 $(x_i,y_i)$ ```{prf:definition} :label: define-lorenz @@ -130,59 +125,52 @@ y_i = \frac{\sum_{j \leq i} w_j}{\sum_{j \leq n} w_j}, $$ ``` -Now the Lorenz curve $L$ is formed from these data points using interpolation. +现在,洛伦兹曲线 $L$ 就是根据这些数据点用插值法绘制的。 -If we use a line plot in `matplotlib`, the interpolation will be done for us. +如果我们使用 `matplotlib` 中的线形图,它将自动帮我们完成插值。 -The meaning of the statement $y = L(x)$ is that the lowest $(100 -\times x)$\% of people have $(100 \times y)$\% of all wealth. +语句 $y = L(x)$ 的含义是,最低的 $(100 \times x$ \% 的人拥有$(100 \times y)$\% 的财富。 -* if $x=0.5$ and $y=0.1$, then the bottom 50% of the population - owns 10% of the wealth. +* 如果 $x=0.5$,$y=0.1$,那么最底层的 50%人口拥有 10%的财富。 + 拥有 10%的财富。 -In the discussion above we focused on wealth but the same ideas apply to -income, consumption, etc. +在上面的讨论中,我们重点讨论了财富,但同样的观点也适用于收入、消费等。 -### Lorenz curves of simulated data +### 洛伦兹曲线的模拟数据 -Let's look at some examples and try to build understanding. +让我们来看一些例子,并尝试构建理解。 -First let us construct a `lorenz_curve` function that we can -use in our simulations below. +首先,让我们构建一个可以在下面的模拟中使用的 `lorenz_curve` 函数。 -It is useful to construct a function that translates an array of -income or wealth data into the cumulative share -of individuals (or households) and the cumulative share of income (or wealth). +构造一个函数,它能将收入或财富数据的数组转换为个人(或家庭)的累积份额和收入(或财富)的累积份额。 ```{code-cell} ipython3 def lorenz_curve(y): """ - Calculates the Lorenz Curve, a graphical representation of - the distribution of income or wealth. + 计算洛伦兹曲线,这是收入或财富分配的图形表示。 - It returns the cumulative share of people (x-axis) and - the cumulative share of income earned. + 它返回人们的累积份额(x轴)和赚取的收入的累积份额。 - Parameters + 参数 ---------- y : array_like(float or int, ndim=1) - Array of income/wealth for each individual. - Unordered or ordered is fine. + 每个个体的收入/财富数组。 + 无序或有序皆可。 - Returns + 返回值 ------- cum_people : array_like(float, ndim=1) - Cumulative share of people for each person index (i/n) + 每个人指数的人们的累积份额(i/n) cum_income : array_like(float, ndim=1) - Cumulative share of income for each person index + 每个人指数的收入的累积份额 - References + 参考文献 ---------- .. [1] https://en.wikipedia.org/wiki/Lorenz_curve - Examples + 示例 -------- >>> a_val, n = 3, 10_000 >>> y = np.random.pareto(a_val, size=n) @@ -202,22 +190,19 @@ def lorenz_curve(y): return cum_people, cum_income ``` -In the next figure, we generate $n=2000$ draws from a lognormal -distribution and treat these draws as our population. +在下图中,我们从对数正态分布中生成了 $n=2000$ 个样本,并将这些样本视为我们的总体。 -The straight 45-degree line ($x=L(x)$ for all $x$) corresponds to perfect equality. +直的 45 度线($x=L(x)$ 对于所有 $x$)对应于完全平等的情况。 -The log-normal draws produce a less equal distribution. +对数正态分布的样本产生了一个不那么平等的分布。 -For example, if we imagine these draws as being observations of wealth across -a sample of households, then the dashed lines show that the bottom 80\% of -households own just over 40\% of total wealth. +例如,如果我们将这些样本看作是家庭财富的观测值,那么虚线表明,底部 80\% 的家庭仅拥有总财富的 40\% 多一点。 ```{code-cell} ipython3 --- mystnb: figure: - caption: Lorenz curve of simulated wealth data + caption: "\u6A21\u62DF\u8D22\u5BCC\u6570\u636E\u7684\u6D1B\u4F26\u5179\u66F2\u7EBF" name: lorenz_simulated --- n = 2000 @@ -226,29 +211,26 @@ sample = np.exp(np.random.randn(n)) fig, ax = plt.subplots() f_vals, l_vals = lorenz_curve(sample) -ax.plot(f_vals, l_vals, label=f'lognormal sample', lw=2) -ax.plot(f_vals, f_vals, label='equality', lw=2) +ax.plot(f_vals, l_vals, label='对数正态样本', lw=2) +ax.plot(f_vals, f_vals, label='平等', lw=2) ax.vlines([0.8], [0.0], [0.43], alpha=0.5, colors='k', ls='--') ax.hlines([0.43], [0], [0.8], alpha=0.5, colors='k', ls='--') ax.set_xlim((0, 1)) -ax.set_xlabel("share of households") +ax.set_xlabel("家庭份额") ax.set_ylim((0, 1)) -ax.set_ylabel("share of wealth") +ax.set_ylabel("财富份额") ax.legend() plt.show() ``` +### 洛伦兹曲线(美国数据) - - -### Lorenz curves for US data - -Next let's look at US data for both income and wealth. +接下来让我们查看美国的收入和财富数据。 (data:survey-consumer-finance)= -The following code block imports a subset of the dataset `SCF_plus` for 2016, -which is derived from the [Survey of Consumer Finances](https://en.wikipedia.org/wiki/Survey_of_Consumer_Finances) (SCF). +下面的代码块导入了2016年的`SCF_plus`数据集的一个子集, +该数据集来源于[消费者财务调查](https://en.wikipedia.org/wiki/Survey_of_Consumer_Finances)(SCF)。 ```{code-cell} ipython3 url = 'https://github.com/QuantEcon/high_dim_data/raw/main/SCF_plus/SCF_plus_mini.csv' @@ -260,41 +242,40 @@ df_income_wealth = df.dropna() df_income_wealth.head(n=5) ``` -The next code block uses data stored in dataframe `df_income_wealth` to generate the Lorenz curves. +下一个代码块使用存储在数据框 `df_income_wealth` 中的数据来生成洛伦兹曲线。 -(The code is somewhat complex because we need to adjust the data according to -population weights supplied by the SCF.) +(代码稍微复杂一些,因为我们需要根据 SCF 提供的人口权重来调整数据。) ```{code-cell} ipython3 :tags: [hide-input] df = df_income_wealth -varlist = ['n_wealth', # net wealth - 't_income', # total income - 'l_income'] # labor income +varlist = ['n_wealth', # 净财富 + 't_income', # 总收入 + 'l_income'] # 劳动收入 years = df.year.unique() -# Create lists to store Lorenz data +# 创建列表储存洛伦兹曲线数据 F_vals, L_vals = [], [] for var in varlist: - # create lists to store Lorenz curve data + # 创建列表储存洛伦兹曲线数据 f_vals = [] l_vals = [] for year in years: - # Repeat the observations according to their weights + # 根据他们的权重重复观测 counts = list(round(df[df['year'] == year]['weights'] )) y = df[df['year'] == year][var].repeat(counts) y = np.asarray(y) - # Shuffle the sequence to improve the plot + # 洗牌序列以改善图形 rd.shuffle(y) - # calculate and store Lorenz curve data + # 计算并储存洛伦兹曲线数据 f_val, l_val = lorenz_curve(y) f_vals.append(f_val) l_vals.append(l_val) @@ -306,56 +287,47 @@ f_vals_nw, f_vals_ti, f_vals_li = F_vals l_vals_nw, l_vals_ti, l_vals_li = L_vals ``` -Now we plot Lorenz curves for net wealth, total income and labor income in the -US in 2016. +现在我们绘制2016年美国的净财富、总收入和劳动收入的洛伦兹曲线。 -Total income is the sum of households' all income sources, including labor income but excluding capital gains. +总收入是家庭所有收入来源的总和,包括劳动收入但不包括资本收益。 -(All income measures are pre-tax.) +(所有收入计量均为税前。) ```{code-cell} ipython3 --- mystnb: figure: - caption: 2016 US Lorenz curves + caption: "2016\u5E74\u7F8E\u56FD\u6D1B\u4F26\u5179\u66F2\u7EBF" name: lorenz_us image: alt: lorenz_us --- fig, ax = plt.subplots() -ax.plot(f_vals_nw[-1], l_vals_nw[-1], label=f'net wealth') -ax.plot(f_vals_ti[-1], l_vals_ti[-1], label=f'total income') -ax.plot(f_vals_li[-1], l_vals_li[-1], label=f'labor income') -ax.plot(f_vals_nw[-1], f_vals_nw[-1], label=f'equality') -ax.set_xlabel("share of households") -ax.set_ylabel("share of income/wealth") +ax.plot(f_vals_nw[-1], l_vals_nw[-1], label=f'净财富') +ax.plot(f_vals_ti[-1], l_vals_ti[-1], label=f'总收入') +ax.plot(f_vals_li[-1], l_vals_li[-1], label=f'劳动收入') +ax.plot(f_vals_nw[-1], f_vals_nw[-1], label=f'平等') +ax.set_xlabel("家庭份额") +ax.set_ylabel("收入/财富份额") ax.legend() plt.show() ``` +一个关键发现是,财富不平等比收入不平等更为极端。 -One key finding from this figure is that wealth inequality is more extreme than income inequality. - - - +## 基尼系数 +洛伦兹曲线提供了分布不平等的可视化表示。 +另一种研究收入和财富不平等的方法是通过基尼系数。 -## The Gini coefficient +在本节中,我们讨论基尼系数及其与洛伦兹曲线的关系。 -The Lorenz curve provides a visual representation of inequality in a distribution. +### 定义 -Another way to study income and wealth inequality is via the Gini coefficient. +如前所述,假设样本 $w_1, \ldots, w_n$ 已按从小到大的顺序排列。 -In this section we discuss the Gini coefficient and its relationship to the Lorenz curve. - - - -### Definition - -As before, suppose that the sample $w_1, \ldots, w_n$ has been sorted from smallest to largest. - -The Gini coefficient is defined for the sample above as +基尼系数的定义如下 ```{prf:definition} :label: define-gini @@ -367,98 +339,94 @@ G := $$ ``` -The Gini coefficient is closely related to the Lorenz curve. +基尼系数与洛伦兹曲线密切相关。 -In fact, it can be shown that its value is twice the area between the line of -equality and the Lorenz curve (e.g., the shaded area in {numref}`lorenz_gini`). +事实上,可以证明基尼系数的值是平等线与洛伦兹曲线之间面积的两倍(例如,{numref}`lorenz_gini` 中的阴影区域)。 -The idea is that $G=0$ indicates complete equality, while $G=1$ indicates complete inequality. +其思想是,$G=0$ 表示完全平等,而 $G=1$ 表示完全不平等。 ```{code-cell} ipython3 --- mystnb: figure: - caption: Gini coefficient (simulated wealth data) + caption: "\u57FA\u5C3C\u7CFB\u6570\uFF08\u6A21\u62DF\u8D22\u5BCC\u6570\u636E\uFF09" name: lorenz_gini --- fig, ax = plt.subplots() f_vals, l_vals = lorenz_curve(sample) -ax.plot(f_vals, l_vals, label=f'lognormal sample', lw=2) -ax.plot(f_vals, f_vals, label='equality', lw=2) +ax.plot(f_vals, l_vals, label=f'对数正态样本', lw=2) +ax.plot(f_vals, f_vals, label='平等线', lw=2) ax.fill_between(f_vals, l_vals, f_vals, alpha=0.06) ax.set_ylim((0, 1)) ax.set_xlim((0, 1)) -ax.text(0.04, 0.5, r'$G = 2 \times$ shaded area') -ax.set_xlabel("share of households (%)") -ax.set_ylabel("share of wealth (%)") +ax.text(0.04, 0.5, r'$G = 2 \times$ 阴影区域') +ax.set_xlabel("家庭比例 (%)") +ax.set_ylabel("财富比例 (%)") ax.legend() plt.show() ``` -In fact the Gini coefficient can also be expressed as +事实上,基尼系数还可以表示为 $$ G = \frac{A}{A+B} $$ -where $A$ is the area between the 45-degree line of -perfect equality and the Lorenz curve, while $B$ is the area below the Lorenze curve -- see {numref}`lorenz_gini2`. +其中 $A$ 是完美平等的45度线与洛伦兹曲线之间的区域面积,$B$ 是洛伦兹曲线以下的区域面积 —— 参见 {numref}`lorenz_gini2`。 ```{code-cell} ipython3 --- mystnb: figure: - caption: Lorenz curve and Gini coefficient + caption: "\u6D1B\u4F26\u5179\u66F2\u7EBF\u548C\u57FA\u5C3C\u7CFB\u6570" name: lorenz_gini2 --- fig, ax = plt.subplots() f_vals, l_vals = lorenz_curve(sample) -ax.plot(f_vals, l_vals, label='lognormal sample', lw=2) -ax.plot(f_vals, f_vals, label='equality', lw=2) +ax.plot(f_vals, l_vals, label='对数正态样本', lw=2) +ax.plot(f_vals, f_vals, label='平等', lw=2) ax.fill_between(f_vals, l_vals, f_vals, alpha=0.06) ax.fill_between(f_vals, l_vals, np.zeros_like(f_vals), alpha=0.06) ax.set_ylim((0, 1)) ax.set_xlim((0, 1)) ax.text(0.55, 0.4, 'A') ax.text(0.75, 0.15, 'B') -ax.set_xlabel("share of households") -ax.set_ylabel("share of wealth") +ax.set_xlabel("家庭份额") +ax.set_ylabel("财富份额") ax.legend() plt.show() ``` - - ```{seealso} -The World in Data project has a [graphical exploration of the Lorenz curve and the Gini coefficient](https://ourworldindata.org/what-is-the-gini-coefficient) +在全球数据网站上有一个很有趣的用图表阐述[洛伦兹曲线的网页](https://ourworldindata.org/what-is-the-gini-coefficient) ``` -### Gini coefficient of simulated data -Let's examine the Gini coefficient in some simulations. +### 模拟数据的基尼系数 + +让我们通过一些模拟来研究基尼系数。 -The code below computes the Gini coefficient from a sample. +下面的代码从样本中计算基尼系数。 (code:gini-coefficient)= ```{code-cell} ipython3 - def gini_coefficient(y): r""" - Implements the Gini inequality index + 实现基尼不平等指数 - Parameters + 参数 ---------- y : array_like(float) - Array of income/wealth for each individual. - Ordered or unordered is fine + 每个个体的收入/财富数组。 + 排序与否均可 - Returns + 返回值 ------- - Gini index: float - The gini index describing the inequality of the array of income/wealth + 基尼指数: float + 描述收入/财富数组不平等的基尼指数 - References + 参考资料 ---------- https://en.wikipedia.org/wiki/Gini_coefficient @@ -471,20 +439,17 @@ def gini_coefficient(y): return np.sum(i_sum) / (2 * n * np.sum(y)) ``` -Now we can compute the Gini coefficients for five different populations. +现在我们可以计算五个不同总体的基尼系数。 -Each of these populations is generated by drawing from a -lognormal distribution with parameters $\mu$ (mean) and $\sigma$ (standard deviation). +这些总体中的每一个都是从参数为 $\mu$(均值)和 $\sigma$(标准差)的对数正态分布中生成的。 -To create the five populations, we vary $\sigma$ over a grid of length $5$ -between $0.2$ and $4$. +为了创建这五个总体,我们在 $0.2$ 到 $4$ 的网格上变化 $\sigma$,网格长度为 $5$。 -In each case we set $\mu = - \sigma^2 / 2$. +在每种情况下,我们都设置 $\mu = - \sigma^2 / 2$。 -This implies that the mean of the distribution does not change with $\sigma$. +这意味着分布的均值不会随 $\sigma$ 改变。 -You can check this by looking up the expression for the mean of a lognormal -distribution. +你可以通过查找对数正态分布均值的表达式来验证这一点。 ```{code-cell} ipython3 %%time @@ -500,7 +465,7 @@ for σ in σ_vals: ginis.append(gini_coefficient(y)) ``` -Let's build a function that returns a figure (so that we can use it later in the lecture). +让我们构建一个返回图形的函数(这样我们可以在后面的讲义中使用它)。 ```{code-cell} ipython3 def plot_inequality_measures(x, y, legend, xlabel, ylabel): @@ -516,114 +481,109 @@ def plot_inequality_measures(x, y, legend, xlabel, ylabel): --- mystnb: figure: - caption: Gini coefficients of simulated data + caption: "\u6A21\u62DF\u6570\u636E\u7684\u57FA\u5C3C\u7CFB\u6570" name: gini_simulated --- fix, ax = plot_inequality_measures(σ_vals, ginis, - 'simulated', + '模拟', '$\sigma$', - 'Gini coefficients') + '基尼系数') plt.show() ``` -The plots show that inequality rises with $\sigma$, according to the Gini -coefficient. +根据基尼系数,图表显示不平等随着 $\sigma$ 增加而上升。 -### Gini coefficient for income (US data) +### 美国收入的基尼系数 -Let's look at the Gini coefficient for the distribution of income in the US. +让我们来看一下美国收入分布的基尼系数。 -We will get pre-computed Gini coefficients (based on income) from the World Bank using the [wbgapi](https://blogs.worldbank.org/opendata/introducing-wbgapi-new-python-package-accessing-world-bank-data). +我们将从世界银行预先计算的基尼系数(基于收入)中获取数据,使用 [wbgapi](https://blogs.worldbank.org/opendata/introducing-wbgapi-new-python-package-accessing-world-bank-data)。 -Let's use the `wbgapi` package we imported earlier to search the World Bank data for Gini to find the Series ID. +让我们使用前面导入的 `wbgapi` 包,搜索世界银行数据中的基尼系数以找到系列 ID。 ```{code-cell} ipython3 wb.search("gini") ``` -We now know the series ID is `SI.POV.GINI`. +我们现在知道系列 ID 是 `SI.POV.GINI`。 -(Another way to find the series ID is to use the [World Bank data portal](https://data.worldbank.org) and then use `wbgapi` to fetch the data.) +(另一种找到系列 ID 的方法是使用 [世界银行数据门户](https://data.worldbank.org) 并使用 `wbgapi` 提取数据。) -To get a quick overview, let's histogram Gini coefficients across all countries and all years in the World Bank dataset. +为了快速概览,让我们绘制世界银行数据集中所有国家和所有年份的基尼系数直方图。 ```{code-cell} ipython3 --- mystnb: figure: - caption: Histogram of Gini coefficients across countries + caption: "\u5404\u56FD\u57FA\u5C3C\u7CFB\u6570\u76F4\u65B9\u56FE" name: gini_histogram --- -# Fetch gini data for all countries +# 获取所有国家的基尼数据 gini_all = wb.data.DataFrame("SI.POV.GINI") -# remove 'YR' in index and convert to integer +# 移除索引中的 'YR' 并转为整数 gini_all.columns = gini_all.columns.map(lambda x: int(x.replace('YR',''))) -# Create a long series with a multi-index of the data to get global min and max values +# 创建带有多重索引的长系列数据,以获取全球最大和最小值 gini_all = gini_all.unstack(level='economy').dropna() -# Build a histogram +# 构建直方图 ax = gini_all.plot(kind="hist", bins=20) -ax.set_xlabel("Gini coefficient") -ax.set_ylabel("frequency") +ax.set_xlabel("基尼系数") +ax.set_ylabel("频率") plt.show() ``` -We can see in {numref}`gini_histogram` that across 50 years of data and all countries the measure varies between 20 and 65. - -Let us fetch the data `DataFrame` for the USA. +我们可以在 {numref}`gini_histogram` 中看到,根据50年的数据和所有国家的数据,该指标在20到65之间变化。 +现在,我们来看看美国在过去几十年的基尼系数变化。 ```{code-cell} ipython3 data = wb.data.DataFrame("SI.POV.GINI", "USA") data.head(n=5) -# remove 'YR' in index and convert to integer +# 移除索引中的 'YR' 并将其转换为整数 data.columns = data.columns.map(lambda x: int(x.replace('YR',''))) ``` -(This package often returns data with year information contained in the columns. This is not always convenient for simple plotting with pandas so it can be useful to transpose the results before plotting.) - +(此包通常会返回包含年份信息的列。这在使用 pandas 简单绘图时并不总是方便,因此在绘图之前将结果转置可能会有帮助。) ```{code-cell} ipython3 -data = data.T # Obtain years as rows -data_usa = data['USA'] # pd.Series of US data +data = data.T # 将年份作为行 +data_usa = data['USA'] # 获取美国数据的 pd.Series ``` -Let us take a look at the data for the US. +让我们来看一下美国的数据。 ```{code-cell} ipython3 --- mystnb: figure: - caption: Gini coefficients for income distribution (USA) + caption: "\u7F8E\u56FD\u6536\u5165\u5206\u5E03\u7684\u57FA\u5C3C\u7CFB\u6570" name: gini_usa1 --- fig, ax = plt.subplots() ax = data_usa.plot(ax=ax) ax.set_ylim(data_usa.min()-1, data_usa.max()+1) -ax.set_ylabel("Gini coefficient (income)") -ax.set_xlabel("year") +ax.set_ylabel("基尼系数(收入)") +ax.set_xlabel("年份") plt.show() ``` -As can be seen in {numref}`gini_usa1`, the income Gini -trended upward from 1980 to 2020 and then dropped following at the start of the COVID pandemic. +如 {numref}`gini_usa1` 所示,从1980年到2020年,美国的收入基尼系数呈上升趋势,然后在COVID疫情初期下降。 (compare-income-wealth-usa-over-time)= -### Gini coefficient for wealth - -In the previous section we looked at the Gini coefficient for income, focusing on using US data. +### 财富的基尼系数 -Now let's look at the Gini coefficient for the distribution of wealth. +在上一节中,我们研究了收入的基尼系数,重点是使用美国的数据。 -We will use US data from the {ref}`Survey of Consumer Finances` +现在让我们来看一下财富分布的基尼系数。 +我们将使用 {ref}`消费者金融调查` 的美国数据。 ```{code-cell} ipython3 df_income_wealth.year.describe() ``` -[This notebook](https://github.com/QuantEcon/lecture-python-intro/tree/main/lectures/_static/lecture_specific/inequality/data.ipynb) can be used to compute this information over the full dataset. +[此笔记本](https://github.com/QuantEcon/lecture-python-intro/tree/main/lectures/_static/lecture_specific/inequality/data.ipynb) 可以用于计算整个数据集中的此信息。 ```{code-cell} ipython3 data_url = 'https://github.com/QuantEcon/lecture-python-intro/raw/main/lectures/_static/lecture_specific/inequality/usa-gini-nwealth-tincome-lincome.csv' @@ -631,110 +591,105 @@ ginis = pd.read_csv(data_url, index_col='year') ginis.head(n=5) ``` -Let's plot the Gini coefficients for net wealth. +让我们绘制净财富的基尼系数图表。 ```{code-cell} ipython3 --- mystnb: figure: - caption: Gini coefficients of US net wealth + caption: "\u7F8E\u56FD\u51C0\u8D22\u5BCC\u7684\u57FA\u5C3C\u7CFB\u6570" name: gini_wealth_us --- fig, ax = plt.subplots() ax.plot(years, ginis["n_wealth"], marker='o') -ax.set_xlabel("year") -ax.set_ylabel("Gini coefficient") +ax.set_xlabel("年份") +ax.set_ylabel("基尼系数") plt.show() ``` -The time series for the wealth Gini exhibits a U-shape, falling until the early -1980s and then increasing rapidly. +财富基尼系数的时间序列呈现出 U 形走势,在20世纪80年代初之前下降,然后迅速上升。 -One possibility is that this change is mainly driven by technology. +一个可能的原因是这种变化主要由技术驱动的。 -However, we will see below that not all advanced economies experienced similar growth of inequality. +然而,我们将在下文中看到,并非所有发达经济体都经历了类似的不平等增长。 -### Cross-country comparisons of income inequality +### 跨国收入不平等的比较 -Earlier in this lecture we used `wbgapi` to get Gini data across many countries -and saved it in a variable called `gini_all` +在本讲义的前面部分,我们使用 `wbgapi` 获取了多个国家的基尼数据,并将其保存在名为 `gini_all` 的变量中。 -In this section we will use this data to compare several advanced economies, and -to look at the evolution in their respective income Ginis. +在本节中,我们将使用这些数据来比较几个发达经济体,并查看它们各自的收入基尼系数的变化。 ```{code-cell} ipython3 data = gini_all.unstack() data.columns ``` -There are 167 countries represented in this dataset. +此数据集中包含167个国家的数据。 -Let us compare three advanced economies: the US, the UK, and Norway +让我们比较三个发达经济体:美国、英国和挪威。 ```{code-cell} ipython3 --- mystnb: figure: - caption: Gini coefficients for income (USA, United Kingdom, and Norway) + caption: "\u6536\u5165\u57FA\u5C3C\u7CFB\u6570\uFF08\u7F8E\u56FD\u3001\u82F1\u56FD\ + \u548C\u632A\u5A01\uFF09" name: gini_usa_gbr_nor1 --- ax = data[['USA','GBR', 'NOR']].plot() -ax.set_xlabel('year') -ax.set_ylabel('Gini coefficient') -ax.legend(title="") +ax.set_xlabel('年份') +ax.set_ylabel('基尼系数') +ax.legend(labels=["美国", "英国", "挪威"], title="") plt.show() ``` -We see that Norway has a shorter time series. +我们看到挪威的数据时间序列较短。 -Let us take a closer look at the underlying data and see if we can rectify this. +让我们仔细查看底层数据,看看是否可以修正这个问题。 ```{code-cell} ipython3 data[['NOR']].dropna().head(n=5) ``` -The data for Norway in this dataset goes back to 1979 but there are gaps in the time series and matplotlib is not showing those data points. +此数据集中挪威的数据可以追溯到1979年,但时间序列中存在空缺,matplotlib 没有显示这些数据点。 -We can use the `.ffill()` method to copy and bring forward the last known value in a series to fill in these gaps +我们可以使用 `.ffill()` 方法来复制并前移序列中的最后已知值,以填补这些空缺。 ```{code-cell} ipython3 --- mystnb: figure: - caption: Gini coefficients for income (USA, United Kingdom, and Norway) + caption: "\u6536\u5165\u57FA\u5C3C\u7CFB\u6570\uFF08\u7F8E\u56FD\u3001\u82F1\u56FD\ + \u548C\u632A\u5A01\uFF09" name: gini_usa_gbr_nor2 --- data['NOR'] = data['NOR'].ffill() ax = data[['USA','GBR', 'NOR']].plot() -ax.set_xlabel('year') -ax.set_ylabel('Gini coefficient') -ax.legend(title="") +ax.set_xlabel('年份') +ax.set_ylabel('基尼系数') +ax.legend(labels=["美国", "英国", "挪威"], title="") plt.show() ``` -From this plot we can observe that the US has a higher Gini coefficient (i.e. -higher income inequality) when compared to the UK and Norway. - -Norway has the lowest Gini coefficient over the three economies and, moreover, -the Gini coefficient shows no upward trend. +从该图中我们可以观察到,与英国和挪威相比,美国的基尼系数更高(即收入不平等程度更高)。 +挪威在这三个经济体中基尼系数最低,而且基尼系数没有上升的趋势。 +### 基尼系数与人均GDP(随时间变化) -### Gini Coefficient and GDP per capita (over time) +我们还可以查看基尼系数与人均GDP的比较(随时间变化)。 -We can also look at how the Gini coefficient compares with GDP per capita (over time). - -Let's take another look at the US, Norway, and the UK. +让我们再次关注美国、挪威和英国。 ```{code-cell} ipython3 countries = ['USA', 'NOR', 'GBR'] gdppc = wb.data.DataFrame("NY.GDP.PCAP.KD", countries) -# remove 'YR' in index and convert to integer +# 移除索引中的 'YR' 并将其转换为整数 gdppc.columns = gdppc.columns.map(lambda x: int(x.replace('YR',''))) gdppc = gdppc.T ``` -We can rearrange the data so that we can plot GDP per capita and the Gini coefficient across years +我们可以重新整理数据,以便绘制不同时期的基尼系数和人均GDP。 ```{code-cell} ipython3 plot_data = pd.DataFrame(data[countries].unstack()) @@ -742,7 +697,7 @@ plot_data.index.names = ['country', 'year'] plot_data.columns = ['gini'] ``` -Now we can get the GDP per capita data into a shape that can be merged with `plot_data` +现在我们将人均GDP数据整理成可以与 `plot_data` 合并的格式。 ```{code-cell} ipython3 pgdppc = pd.DataFrame(gdppc.unstack()) @@ -752,16 +707,16 @@ plot_data = plot_data.merge(pgdppc, left_index=True, right_index=True) plot_data.reset_index(inplace=True) ``` -Now we use Plotly to build a plot with GDP per capita on the y-axis and the Gini coefficient on the x-axis. +现在我们使用 Plotly 绘制一张图表,其中 y 轴表示人均GDP,x 轴表示基尼系数。 ```{code-cell} ipython3 min_year = plot_data.year.min() max_year = plot_data.year.max() ``` -The time series for all three countries start and stop in different years. +这三个国家的时间序列开始和结束的年份不同。 -We will add a year mask to the data to improve clarity in the chart including the different end years associated with each country's time series. +我们将在数据中添加一个年份掩码,以提高图表的清晰度,包含每个国家时间序列的不同结束年份。 ```{code-cell} ipython3 labels = [1979, 1986, 1991, 1995, 2000, 2020, 2021, 2022] + \ @@ -778,39 +733,35 @@ fig = px.line(plot_data, color = "country", text = "year", height = 800, - labels = {"gini" : "Gini coefficient", "gdppc" : "GDP per capita"} + labels = {"gini" : "基尼系数", "gdppc" : "人均GDP"} ) +fig.for_each_trace(lambda t: t.update(name={'USA': '美国', 'GBR': '英国', 'NOR': '挪威'}[t.name])) + fig.update_traces(textposition="bottom right") fig.show() ``` ```{only} latex -This figure is built using `plotly` and is {ref}` available on the website ` +此图表是使用 `plotly` 创建的,详见 {ref}`网站上的图表`。 ``` -This plot shows that all three Western economies' GDP per capita has grown over -time with some fluctuations in the Gini coefficient. +此图表显示,所有三个西方经济体的人均GDP随着时间增长,而基尼系数则有所波动。 -From the early 80's the United Kingdom and the US economies both saw increases -in income inequality. +从80年代初开始,英国和美国的经济都经历了收入不平等的增加。 -Interestingly, since the year 2000, the United Kingdom saw a decline in income inequality while -the US exhibits persistent but stable levels around a Gini coefficient of 40. +有趣的是,自2000年以来,英国的收入不平等有所下降,而美国则表现出基尼系数约为40的持续但稳定的水平。 +## 前10%比例 -## Top shares +另一个受欢迎的不平等衡量指标是前10%比例。 -Another popular measure of inequality is the top shares. +在本节中,我们展示如何计算前10%比例。 -In this section we show how to compute top shares. +### 定义 +如前所述,假设样本 $w_1, \ldots, w_n$ 已按从小到大的顺序排列。 -### Definition - -As before, suppose that the sample $w_1, \ldots, w_n$ has been sorted from smallest to largest. - -Given the Lorenz curve $y = L(x)$ defined above, the top $100 \times p \%$ -share is defined as +给定上面定义的洛伦兹曲线 $y = L(x)$,前 $100 \times p \%$ 的比例定义为 ```{prf:definition} :label: top-shares @@ -821,17 +772,16 @@ T(p) = 1 - L (1-p) $$ (topshares) ``` -Here $\lfloor \cdot \rfloor$ is the floor function, which rounds any -number down to the integer less than or equal to that number. +这里 $\lfloor \cdot \rfloor$ 是取整函数,它将任何数字向下取整为小于或等于该数的整数。 -The following code uses the data from dataframe `df_income_wealth` to generate another dataframe `df_topshares`. +以下代码使用数据框 `df_income_wealth` 中的数据生成另一个数据框 `df_topshares`。 -`df_topshares` stores the top 10 percent shares for the total income, the labor income and net wealth from 1950 to 2016 in US. +`df_topshares` 存储了1950年至2016年美国总收入、劳动收入和净财富的前10%比例。 ```{code-cell} ipython3 :tags: [hide-input] -# transfer the survey weights from absolute into relative values +# 将调查权重从绝对值转换为相对值 df1 = df_income_wealth df2 = df1.groupby('year').sum(numeric_only=True).reset_index() df3 = df2[['year', 'weights']] @@ -839,17 +789,16 @@ df3.columns = 'year', 'r_weights' df4 = pd.merge(df3, df1, how="left", on=["year"]) df4['r_weights'] = df4['weights'] / df4['r_weights'] -# create weighted nw, ti, li +# 创建加权的净财富、总收入和劳动收入 df4['weighted_n_wealth'] = df4['n_wealth'] * df4['r_weights'] df4['weighted_t_income'] = df4['t_income'] * df4['r_weights'] df4['weighted_l_income'] = df4['l_income'] * df4['r_weights'] -# extract two top 10% groups by net wealth and total income. +# 提取按净财富和总收入分组的前10%群体 df6 = df4[df4['nw_groups'] == 'Top 10%'] df7 = df4[df4['ti_groups'] == 'Top 10%'] -# calculate the sum of weighted top 10% by net wealth, -# total income and labor income. +# 计算按净财富、总收入和劳动收入加权的前10% df5 = df4.groupby('year').sum(numeric_only=True).reset_index() df8 = df6.groupby('year').sum(numeric_only=True).reset_index() df9 = df7.groupby('year').sum(numeric_only=True).reset_index() @@ -858,7 +807,7 @@ df5['weighted_n_wealth_top10'] = df8['weighted_n_wealth'] df5['weighted_t_income_top10'] = df9['weighted_t_income'] df5['weighted_l_income_top10'] = df9['weighted_l_income'] -# calculate the top 10% shares of the three variables. +# 计算这三个变量的前10%比例 df5['topshare_n_wealth'] = df5['weighted_n_wealth_top10'] / \ df5['weighted_n_wealth'] df5['topshare_t_income'] = df5['weighted_t_income_top10'] / \ @@ -866,70 +815,65 @@ df5['topshare_t_income'] = df5['weighted_t_income_top10'] / \ df5['topshare_l_income'] = df5['weighted_l_income_top10'] / \ df5['weighted_l_income'] -# we only need these vars for top 10 percent shares +# 我们只需要这些变量来计算前10%的比例 df_topshares = df5[['year', 'topshare_n_wealth', 'topshare_t_income', 'topshare_l_income']] ``` -Then let's plot the top shares. +接下来让我们绘制前10%比例的图表。 ```{code-cell} ipython3 --- mystnb: figure: - caption: US top shares + caption: "\u7F8E\u56FD\u524D10%\u6BD4\u4F8B" name: top_shares_us --- fig, ax = plt.subplots() ax.plot(years, df_topshares["topshare_l_income"], - marker='o', label="labor income") + marker='o', label="劳动收入") ax.plot(years, df_topshares["topshare_n_wealth"], - marker='o', label="net wealth") + marker='o', label="净财富") ax.plot(years, df_topshares["topshare_t_income"], - marker='o', label="total income") -ax.set_xlabel("year") -ax.set_ylabel("top $10\%$ share") + marker='o', label="总收入") +ax.set_xlabel("年份") +ax.set_ylabel("前 $10\%$ 比例") ax.legend() plt.show() ``` -## Exercises - +## 练习 ```{exercise} :label: inequality_ex1 -Using simulation, compute the top 10 percent shares for the collection of -lognormal distributions associated with the random variables $w_\sigma = -\exp(\mu + \sigma Z)$, where $Z \sim N(0, 1)$ and $\sigma$ varies over a -finite grid between $0.2$ and $4$. +使用模拟计算系列对数正态分布的前10%份额,这些对数正态分布与随机变量 $w_\sigma = \exp(\mu + \sigma Z)$ 相关联,其中 $Z \sim N(0, 1)$ 且 $\sigma$ 在一个从 $0.2$ 到 $4$ 的有限网格上变化。 -As $\sigma$ increases, so does the variance of $w_\sigma$. +随着 $\sigma$ 的增加,$w_\sigma$ 的方差也在增加。 -To focus on volatility, adjust $\mu$ at each step to maintain the equality -$\mu=-\sigma^2/2$. +为了关注波动性,在每一步调整 $\mu$ 以保持等式 $\mu = -\sigma^2 / 2$ 为真。 -For each $\sigma$, generate 2,000 independent draws of $w_\sigma$ and -calculate the Lorenz curve and Gini coefficient. +对于每个 $\sigma$,生成2000个 $w_\sigma$ 的独立抽样,并计算洛伦兹曲线和基尼系数。 -Confirm that higher variance -generates more dispersion in the sample, and hence greater inequality. +确认更高的方差会在样本中产生更多的分散,从而导致更大的不平等。 ``` ```{solution-start} inequality_ex1 :class: dropdown ``` -Here is one solution: +这是一种解法: ```{code-cell} ipython3 def calculate_top_share(s, p=0.1): - + # 对 s 进行排序 s = np.sort(s) n = len(s) index = int(n * (1 - p)) return s[index:].sum() / s.sum() ``` +继续使用上面定义的 `calculate_top_share` 函数和之前定义的 `lorenz_curve` 以及 `gini_coefficient` 函数,我们可以为各个 σ 值生成统计数据,并绘制变化趋势。 + ```{code-cell} ipython3 k = 5 σ_vals = np.linspace(0.2, 4, k) @@ -954,16 +898,16 @@ for σ in σ_vals: --- mystnb: figure: - caption: Top shares of simulated data + caption: "\u6A21\u62DF\u6570\u636E\u7684\u524D10%\u6BD4\u4F8B" name: top_shares_simulated image: alt: top_shares_simulated --- fig, ax = plot_inequality_measures(σ_vals, topshares, - "simulated data", + "模拟数据", "$\sigma$", - "top $10\%$ share") + "前 $10\%$ 比例") plt.show() ``` @@ -971,16 +915,16 @@ plt.show() --- mystnb: figure: - caption: Gini coefficients of simulated data + caption: "\u6A21\u62DF\u6570\u636E\u7684\u57FA\u5C3C\u7CFB\u6570" name: gini_coef_simulated image: alt: gini_coef_simulated --- fig, ax = plot_inequality_measures(σ_vals, ginis, - "simulated data", + "模拟数据", "$\sigma$", - "gini coefficient") + "基尼系数") plt.show() ``` @@ -988,13 +932,13 @@ plt.show() --- mystnb: figure: - caption: Lorenz curves for simulated data + caption: "\u6A21\u62DF\u6570\u636E\u7684\u6D1B\u4F26\u5179\u66F2\u7EBF" name: lorenz_curve_simulated image: alt: lorenz_curve_simulated --- fig, ax = plt.subplots() -ax.plot([0,1],[0,1], label=f"equality") +ax.plot([0,1],[0,1], label=f"平等线") for i in range(len(f_vals)): ax.plot(f_vals[i], l_vals[i], label=f"$\sigma$ = {σ_vals[i]}") plt.legend() @@ -1008,19 +952,18 @@ plt.show() ```{exercise} :label: inequality_ex2 -According to the definition of the top shares {eq}`topshares` we can also calculate the top percentile shares using the Lorenz curve. - -Compute the top shares of US net wealth using the corresponding Lorenz curves data: ``f_vals_nw, l_vals_nw`` and linear interpolation. +根据前10%比例的定义 {eq}`topshares`,我们也可以使用洛伦兹曲线计算前百分位比例。 -Plot the top shares generated from Lorenz curve and the top shares approximated from data together. +使用洛伦兹曲线数据 ``f_vals_nw, l_vals_nw`` 和线性插值,计算美国净财富的前10%比例。 +绘制由洛伦兹曲线生成的前10%比例与从数据近似得出的前10%比例。 ``` ```{solution-start} inequality_ex2 :class: dropdown ``` -Here is one solution: +这是一个解决方案: ```{code-cell} ipython3 def lorenz2top(f_val, l_val, p=0.1): @@ -1038,19 +981,19 @@ for f_val, l_val in zip(f_vals_nw, l_vals_nw): --- mystnb: figure: - caption: 'US top shares: approximation vs Lorenz' + caption: "\u7F8E\u56FD\u524D10%\u6BD4\u4F8B\uFF1A\u8FD1\u4F3C\u503C vs \u6D1B\u4F26\ + \u5179\u66F2\u7EBF" name: top_shares_us_al image: alt: top_shares_us_al --- fig, ax = plt.subplots() -ax.plot(years, df_topshares["topshare_n_wealth"], marker='o',\ - label="net wealth-approx") -ax.plot(years, top_shares_nw, marker='o', label="net wealth-lorenz") +ax.plot(years, df_topshares["topshare_n_wealth"], marker='o', label="净财富-近似值") +ax.plot(years, top_shares_nw, marker='o', label="净财富-洛伦兹曲线") -ax.set_xlabel("year") -ax.set_ylabel("top $10\%$ share") +ax.set_xlabel("年份") +ax.set_ylabel("前 $10\%$ 比例") ax.legend() plt.show() ``` @@ -1061,22 +1004,22 @@ plt.show() ```{exercise} :label: inequality_ex3 -The {ref}`code to compute the Gini coefficient is listed in the lecture above `. +{ref}`计算基尼系数的代码在上述讲义中列出 `。 -This code uses loops to calculate the coefficient based on income or wealth data. +此代码使用循环计算基于收入或财富数据的基尼系数。 -This function can be re-written using vectorization which will greatly improve the computational efficiency when using `python`. +该函数可以通过向量化改写,从而大大提高 `python` 中的计算效率。 -Re-write the function `gini_coefficient` using `numpy` and vectorized code. +使用 `numpy` 和向量化代码重写 `gini_coefficient` 函数。 -You can compare the output of this new function with the one above, and note the speed differences. +你可以比较新函数与上面函数的输出,并注意速度差异。 ``` ```{solution-start} inequality_ex3 :class: dropdown ``` -Let's take a look at some raw data for the US that is stored in `df_income_wealth` +让我们先看看存储在 `df_income_wealth` 中的美国原始数据 ```{code-cell} ipython3 df_income_wealth.describe() @@ -1086,7 +1029,7 @@ df_income_wealth.describe() df_income_wealth.head(n=4) ``` -We will focus on wealth variable `n_wealth` to compute a Gini coefficient for the year 2016. +我们将重点关注财富变量 `n_wealth` 来计算2016年的基尼系数。 ```{code-cell} ipython3 data = df_income_wealth[df_income_wealth.year == 2016].sample(3000, random_state=1) @@ -1096,13 +1039,13 @@ data = df_income_wealth[df_income_wealth.year == 2016].sample(3000, random_state data.head(n=2) ``` -We can first compute the Gini coefficient using the function defined in the lecture above. +我们可以首先使用上述讲义中定义的函数计算基尼系数。 ```{code-cell} ipython3 gini_coefficient(data.n_wealth.values) ``` -Now we can write a vectorized version using `numpy` +现在我们可以使用 `numpy` 编写一个向量化版本。 ```{code-cell} ipython3 def gini(y): @@ -1112,10 +1055,12 @@ def gini(y): g_sum = np.sum(np.abs(y_1 - y_2)) return g_sum / (2 * n * np.sum(y)) ``` + ```{code-cell} ipython3 gini(data.n_wealth.values) ``` -Let's simulate five populations by drawing from a lognormal distribution as before + +让我们像之前一样通过从对数正态分布中抽取样本来模拟五个总体。 ```{code-cell} ipython3 k = 5 @@ -1125,7 +1070,8 @@ n = 2_000 μ_vals = -σ_vals**2/2 y_vals = np.exp(μ_vals + σ_vals*np.random.randn(n)) ``` -We can compute the Gini coefficient for these five populations using the vectorized function, the computation time is shown below: + +我们可以使用向量化函数计算这五个总体的基尼系数,计算时间如下所示: ```{code-cell} ipython3 %%time @@ -1133,14 +1079,14 @@ gini_coefficients =[] for i in range(k): gini_coefficients.append(gini(y_vals[i])) ``` -This shows the vectorized function is much faster. -This gives us the Gini coefficients for these five households. + +这表明向量化函数更快。 + +下面给出了这五个家庭的基尼系数。 ```{code-cell} ipython3 gini_coefficients ``` + ```{solution-end} ``` - - - diff --git a/lectures/input_output.md b/lectures/input_output.md index 7c8170c..c3fc08a 100644 --- a/lectures/input_output.md +++ b/lectures/input_output.md @@ -11,11 +11,9 @@ kernelspec: name: python3 --- -# Input-Output Models - -## Overview - -This lecture requires the following imports and installs before we proceed. +# 输入-输出模型 +## 概述 +在我们继续之前,本讲座需要以下导入和安装。 ```{code-cell} ipython3 :tags: [hide-output] @@ -40,9 +38,7 @@ quantecon_book_networks.config("matplotlib") mpl.rcParams.update(mpl.rcParamsDefault) ``` -The following figure illustrates a network of linkages among 15 sectors -obtained from the US Bureau of Economic Analysis’s 2021 Input-Output Accounts -Data. +下图展示了从美国经济分析局2021年输入-输出账户数据中获得的15个部门之间的联系网络。 ```{code-cell} ipython3 @@ -50,7 +46,7 @@ Data. def build_coefficient_matrices(Z, X): """ - Build coefficient matrices A and F from Z and X via + 通过以下方式从Z和X构建系数矩阵A和F: A[i, j] = Z[i, j] / X[j] F[i, j] = Z[i, j] / X[i] @@ -82,7 +78,7 @@ tags: [hide-input] --- centrality = qbn_io.eigenvector_centrality(A) -# Remove self-loops +# 去掉自循环 for i in range(A.shape[0]): A[i][i] = 0 @@ -99,52 +95,40 @@ qbn_plt.plot_graph(A, X, ax, codes, plt.show() ``` -|Label| Sector |Label| Sector |Label| Sector | +|标签| 行业 |标签| 行业 |标签| 行业 | |:---:|:-------------:|:---:|:--------------:|:---:|:-------------------------:| -| ag | Agriculture | wh | Wholesale | pr | Professional Services | -| mi | Mining | re | Retail | ed | Education & Health | -| ut | Utilities | tr | Transportation | ar | Arts & Entertainment | -| co | Construction | in | Information | ot | Other Services (exc govt) | -| ma | Manufacturing | fi | Finance | go | Government | - - -An arrow from $i$ to $j$ means that some of sector $i$'s output serves as an input to production of sector $j$. - -Economies are characterised by many such links. - -A basic framework for their analysis is -[Leontief's](https://en.wikipedia.org/wiki/Wassily_Leontief) input-output model. - +| ag | 农业 | wh | 批发 | pr | 专业服务 | +| mi | 采矿业 | re | 零售 | ed | 教育与健康 | +| ut | 公用事业 | tr | 运输 | ar | 艺术与娱乐 | +| co | 建筑 | in | 信息 | ot | 其他服务(不包括政府) | +| ma | 制造业 | fi | 金融 | go | 政府 | +从$i$到$j$的箭头表示$i$行业的一些产出作为$j$行业生产的输入。 +经济的特征是存在许多这样的联系。 +分析这些联系的基本框架是[列昂惕夫](https://en.wikipedia.org/wiki/Wassily_Leontief)的投入产出模型。 +在介绍投入产出模型之后,我们将描述它与{doc}`线性规划讲座 `的一些联系。 -After introducing the input-output model, we describe some of its connections to {doc}`linear programming lecture `. +## 投入产出分析 +设 + * $x_0$为单一外生生产投入的数量,例如劳动力 + * $x_j, j = 1,\ldots n$为最终产品$j$的总产出 + * $d_j, j = 1,\ldots n$为可用于最终消费的最终产品$j$的净产出 + * $z_{ij}$为分配用作生产产品$j$的投入的产品$i$的数量,$i=1, \ldots n$,$j = 1, \ldots n$ + * $z_{0j}$为分配用于生产产品$j$的劳动力数量 + * $a_{ij}$为生产一单位产品$j$所需的产品$i$的单位数,$i=0, \ldots, n, j= 1, \ldots n$ + * $w >0$为外生劳动力工资,以每单位劳动力的美元计 + * $p$为$n \times 1$的生产品$i = 1, \ldots , n$价格向量 -## Input-output analysis - -Let - - * $x_0$ be the amount of a single exogenous input to production, say labor - * $x_j, j = 1,\ldots n$ be the gross output of final good $j$ - * $d_j, j = 1,\ldots n$ be the net output of final good $j$ that is available for final consumption - * $z_{ij} $ be the quantity of good $i$ allocated to be an input to producing good $j$ for $i=1, \ldots n$, $j = 1, \ldots n$ - * $z_{0j}$ be the quantity of labor allocated to producing good $j$. - * $a_{ij}$ be the number of units of good $i$ required to produce one unit of good $j$, $i=0, \ldots, n, j= 1, \ldots n$. - * $w >0$ be an exogenous wage of labor, denominated in dollars per unit of labor - * $p$ be an $n \times 1$ vector of prices of produced goods $i = 1, \ldots , n$. - - - -The technology for producing good $j \in \{1, \ldots , n\}$ is described by the **Leontief** function +生产产品$j \in \{1, \ldots , n\}$的技术由以下**列昂惕夫**函数描述 $$ x_j = \min_{i \in \{0, \ldots , n \}} \left( \frac{z_{ij}}{a_{ij}}\right) $$ -### Two goods +### 两种产品 -To illustrate, we begin by setting $n =2$ and formulating -the following network. +为说明起见,我们首先设$n =2$并制定以下网络。 ```{code-cell} ipython3 :tags: [hide-input] @@ -184,7 +168,7 @@ plt.text(1.6, -0.5, r'$d_{2}$') plt.show() ``` -*Feasible allocations must satisfy* +*可行分配必须满足* $$ \begin{aligned} @@ -194,7 +178,7 @@ a_{01} x_1 + a_{02} x_2 & \leq x_0 \end{aligned} $$ -This can be graphically represented as follows. +这可以用图形表示如下。 ```{code-cell} ipython3 :tags: [hide-input] @@ -202,7 +186,7 @@ This can be graphically represented as follows. fig, ax = plt.subplots() ax.grid() -# Draw constraint lines +# 绘制约束线 ax.hlines(0, -1, 400) ax.vlines(0, -1, 200) @@ -214,14 +198,14 @@ ax.text(130, 38, "$(1-a_{11})x_1 + a_{12}x_2 \geq d_1$", size=10) ax.text(10, 105, "$-a_{21}x_1 + (1-a_{22})x_2 \geq d_2$", size=10) ax.text(150, 150, "$a_{01}x_1 +a_{02}x_2 \leq x_0$", size=10) -# Draw the feasible region +# 绘制可行区域 feasible_set = Polygon(np.array([[301, 151], [368, 143], [250, 120]]), color="cyan") ax.add_patch(feasible_set) -# Draw the optimal solution +# 绘制最优解 ax.plot(250, 120, "*", color="black") ax.text(260, 115, "solution", size=10) @@ -230,7 +214,7 @@ plt.show() +++ {"user_expressions": []} -More generally, constraints on production are +更一般地说,生产的约束条件是 $$ \begin{aligned} @@ -239,34 +223,29 @@ a_0^\top x & \leq x_0 \end{aligned} $$ (eq:inout_1) -where $A$ is the $n \times n$ matrix with typical element $a_{ij}$ and $a_0^\top = \begin{bmatrix} a_{01} & \cdots & a_{0n} \end{bmatrix}$. - +其中 $A$ 是一个 $n \times n$ 的矩阵,其典型元素为 $a_{ij}$,而 $a_0^\top = \begin{bmatrix} a_{01} & \cdots & a_{0n} \end{bmatrix}$。 - -If we solve the first block of equations of {eq}`eq:inout_1` for gross output $x$ we get +如果我们解 {eq}`eq:inout_1` 的第一组方程,得到总产出 $x$,我们得到 $$ x = (I -A)^{-1} d \equiv L d $$ (eq:inout_2) -where the matrix $L = (I-A)^{-1}$ is sometimes called a **Leontief Inverse**. - - +其中矩阵 $L = (I-A)^{-1}$ 有时被称为 **列昂惕夫逆矩阵**。 -To assure that the solution $X$ of {eq}`eq:inout_2` is a positive vector, the following **Hawkins-Simon conditions** suffice: +为确保 {eq}`eq:inout_2` 的解 $X$ 是一个正向量,以下 **霍金斯-西蒙条件** 就足够了: $$ \begin{aligned} -\det (I - A) > 0 \text{ and} \;\;\; \\ -(I-A)_{ij} > 0 \text{ for all } i=j +\det (I - A) > 0 \text{ 且} \;\;\; \\ +(I-A)_{ij} > 0 \text{ 对所有 } i=j \end{aligned} $$ - ```{prf:example} :label: io_ex_tg -For example a two-good economy described by +例如,一个由以下描述的两种商品经济 $$ A = @@ -274,7 +253,7 @@ A = 0.1 & 40 \\ 0.01 & 0 \end{bmatrix} -\text{ and } +\text{ 和 } d = \begin{bmatrix} 50 \\ @@ -295,56 +274,55 @@ B = I - A B ``` -Let's check the **Hawkins-Simon conditions** +让我们检查一下**霍金斯-西蒙条件** ```{code-cell} ipython3 -np.linalg.det(B) > 0 # checking Hawkins-Simon conditions +np.linalg.det(B) > 0 # 检查霍金斯-西蒙条件 ``` -Now, let's compute the **Leontief inverse** matrix +现在我们计算列昂惕夫逆矩阵 ```{code-cell} ipython3 -L = np.linalg.inv(B) # obtaining Leontief inverse matrix +L = np.linalg.inv(B) # 得到列昂惕夫逆矩阵 L ``` ```{code-cell} ipython3 -x = L @ d # solving for gross output +x = L @ d # 求解毛产出 x ``` +++ {"user_expressions": []} -## Production possibility frontier +## 生产可能性边界 -The second equation of {eq}`eq:inout_1` can be written +{eq}`eq:inout_1`的第二个等式可以写成 $$ a_0^\top x = x_0 $$ -or +或 $$ A_0^\top d = x_0 $$ (eq:inout_frontier) -where +其中 $$ A_0^\top = a_0^\top (I - A)^{-1} $$ - For $i \in \{1, \ldots , n\}$, the $i$th component of $A_0$ is the amount of labor that is required to produce one unit of final output of good $i$. +对于 $i \in \{1, \ldots , n\}$,$A_0$ 的第 $i$ 个分量是生产一单位第 $i$ 种商品的最终产出所需的劳动量。 -Equation {eq}`eq:inout_frontier` sweeps out a **production possibility frontier** of final consumption bundles $d$ that can be produced with exogenous labor input $x_0$. +等式 {eq}`eq:inout_frontier` 描绘了一个**生产可能性边界**,表示在给定外生劳动投入 $x_0$ 的情况下,可以生产的最终消费束 $d$。 ```{prf:example} :label: io_ex_ppf -Consider the example in {eq}`eq:inout_ex`. - -Suppose we are now given +考虑{eq}`eq:inout_ex`中的例子。 +假设我们现在给出 $$ a_0^\top = \begin{bmatrix} @@ -353,7 +331,7 @@ a_0^\top = \begin{bmatrix} $$ ``` -Then we can find $A_0^\top$ by +然后我们可以通过以下方式找到 $A_0^\top$: ```{code-cell} ipython3 a0 = np.array([4, 100]) @@ -363,7 +341,7 @@ A0 +++ {"user_expressions": []} -Thus, the production possibility frontier for this economy is +因此,这个经济体的生产可能性边界是: $$ 10d_1 + 500d_2 = x_0 @@ -371,9 +349,8 @@ $$ +++ {"user_expressions": []} -## Prices - -{cite}`DoSSo` argue that relative prices of the $n$ produced goods must satisfy +## 价格 +{cite}`DoSSo` 认为,$n$ 种生产商品的相对价格必须满足 $$ \begin{aligned} @@ -382,76 +359,64 @@ p_2 = a_{12}p_1 + a_{22}p_2 + a_{02}w \end{aligned} $$ -More generally, +更一般地, $$ p = A^\top p + a_0 w $$ -which states that the price of each final good equals the total cost -of production, which consists of costs of intermediate inputs $A^\top p$ -plus costs of labor $a_0 w$. - -This equation can be written as +这表明每种最终商品的价格等于生产的总成本,包括中间投入品的成本 $A^\top p$ 和劳动力成本 $a_0 w$。 +这个方程可以写成 $$ (I - A^\top) p = a_0 w $$ (eq:inout_price) -which implies +这意味着 $$ p = (I - A^\top)^{-1} a_0 w $$ -Notice how {eq}`eq:inout_price` with {eq}`eq:inout_1` forms a -**conjugate pair** through the appearance of operators -that are transposes of one another. - -This connection surfaces again in a classic linear program and its dual. - +注意 {eq}`eq:inout_price` 与 {eq}`eq:inout_1` 通过相互转置的算子形成了一个**共轭对**。 +这种联系在经典线性规划及其对偶问题中再次出现。 -## Linear programs - -A **primal** problem is +## 线性规划 +**原始问题**是 $$ \min_{x} w a_0^\top x $$ -subject to +约束条件为 $$ (I - A) x \geq d $$ - -The associated **dual** problem is +相关的**对偶问题**是 $$ \max_{p} p^\top d $$ -subject to +约束条件为 $$ (I -A)^\top p \leq a_0 w $$ -The primal problem chooses a feasible production plan to minimize costs for delivering a pre-assigned vector of final goods consumption $d$. - -The dual problem chooses prices to maximize the value of a pre-assigned vector of final goods $d$ subject to prices covering costs of production. - -By the [strong duality theorem](https://en.wikipedia.org/wiki/Dual_linear_program#Strong_duality), -optimal value of the primal and dual problems coincide: +原始问题选择一个可行的生产计划,以最小化交付预先指定的最终商品消费向量 $d$ 的成本。 +对偶问题选择价格,以最大化预先指定的最终商品向量 $d$ 的价值,同时受制于价格覆盖生产成本。 +根据[强对偶定理](https://en.wikipedia.org/wiki/Dual_linear_program#Strong_duality), +原始问题和对偶问题的最优值相同: $$ w a_0^\top x^* = p^* d $$ -where $^*$'s denote optimal choices for the primal and dual problems. - -The dual problem can be graphically represented as follows. +其中 $^*$ 表示原始和对偶问题的最优选择。 +对偶问题可以用图形表示如下。 ```{code-cell} ipython3 :tags: [hide-input] @@ -459,7 +424,7 @@ The dual problem can be graphically represented as follows. fig, ax = plt.subplots() ax.grid() -# Draw constraint lines +# 绘制约束线 ax.hlines(0, -1, 50) ax.vlines(0, -1, 250) @@ -469,7 +434,7 @@ ax.plot(np.linspace(0, 50, 100), (33+1.46*np.linspace(0, 50, 100))/0.83, color=" ax.text(15, 175, "$(1-a_{11})p_1 - a_{21}p_2 \leq a_{01}w$", size=10) ax.text(30, 85, "$-a_{12}p_1 + (1-a_{22})p_2 \leq a_{02}w$", size=10) -# Draw the feasible region +# 绘制可行区域 feasible_set = Polygon(np.array([[17, 69], [4, 0], [0,0], @@ -477,7 +442,7 @@ feasible_set = Polygon(np.array([[17, 69], color="cyan") ax.add_patch(feasible_set) -# Draw the optimal solution +# 绘制最优解 ax.plot(17, 69, "*", color="black") ax.text(18, 60, "dual solution", size=10) @@ -486,51 +451,40 @@ plt.show() +++ {"user_expressions": []} -## Leontief inverse - -We have discussed that gross output $x$ is given by {eq}`eq:inout_2`, where $L$ is called the Leontief Inverse. - -Recall the {doc}`Neumann Series Lemma ` which states that $L$ exists if the spectral radius $r(A)<1$. - -In fact +## 列昂惕夫逆矩阵 +我们已经讨论过,总产出 $x$ 由公式 {eq}`eq:inout_2` 给出,其中 $L$ 被称为列昂惕夫逆矩阵。 +回顾 {doc}`诺伊曼级数引理 `,它指出如果谱半径 $r(A)<1$,则 $L$ 存在。 +事实上 $$ L = \sum_{i=0}^{\infty} A^i $$ -### Demand shocks - -Consider the impact of a demand shock $\Delta d$ which shifts demand from $d_0$ to $d_1 = d_0 + \Delta d$. - -Gross output shifts from $x_0 = Ld_0$ to $x_1 = Ld_1$. - -If $r(A) < 1$ then a solution exists and +### 需求冲击 +考虑需求冲击 $\Delta d$ 的影响,它将需求从 $d_0$ 转变为 $d_1 = d_0 + \Delta d$。 +总产出从 $x_0 = Ld_0$ 转变为 $x_1 = Ld_1$。 +如果 $r(A) < 1$,则存在解,且 $$ \Delta x = L \Delta d = \Delta d + A(\Delta d) + A^2 (\Delta d) + \cdots $$ -This illustrates that an element $l_{ij}$ of $L$ shows the total impact on sector $i$ of a unit change in demand of good $j$. - -## Applications of graph theory - -We can further study input-output networks through applications of {doc}`graph theory `. +这说明 $L$ 的一个元素 $l_{ij}$ 显示了对商品 $j$ 的需求单位变化对部门 $i$ 的总影响。 -An input-output network can be represented by a weighted directed graph induced by the adjacency matrix $A$. - -The set of nodes $V = [n]$ is the list of sectors and the set of edges is given by +## 图论的应用 +我们可以通过 {doc}`图论 ` 的应用进一步研究投入产出网络。 +投入产出网络可以通过邻接矩阵 $A$ 诱导的加权有向图来表示。 +节点集 $V = [n]$ 是部门列表,边集由以下给出: $$ E = \{(i,j) \in V \times V : a_{ij}>0\} $$ -In {numref}`us_15sectors` weights are indicated by the widths of the arrows, which are proportional to the corresponding input-output coefficients. - -We can now use centrality measures to rank sectors and discuss their importance relative to the other sectors. - -### Eigenvector centrality +在 {numref}`us_15sectors` 中,权重由箭头的宽度表示,与相应的投入产出系数成正比。 +现在我们可以使用中心性度量来对部门进行排序,并讨论它们相对于其他部门的重要性。 -Eigenvector centrality of a node $i$ is measured by +### 特征向量中心性 +节点 $i$ 的特征向量中心性由以下公式衡量: $$ \begin{aligned} @@ -538,54 +492,41 @@ $$ \end{aligned} $$ -We plot a bar graph of hub-based eigenvector centrality for the sectors represented in {numref}`us_15sectors`. +我们为 {numref}`us_15sectors` 中表示的部门绘制了基于枢纽的特征向量中心性条形图。 ```{code-cell} ipython3 :tags: [hide-input] fig, ax = plt.subplots() ax.bar(codes, centrality, color=color_list, alpha=0.6) -ax.set_ylabel("eigenvector centrality", fontsize=12) +ax.set_ylabel("特征向量中心性", fontsize=12) plt.show() ``` -A higher measure indicates higher importance as a supplier. +较高的指标表示作为供应商的重要性更高。 +因此,大多数行业的需求冲击将显著影响具有高特征向量中心性的行业的活动。 +上图表明制造业是美国经济中最主导的行业。 -As a result demand shocks in most sectors will significantly impact activity in sectors with high eigenvector centrality. - -The above figure indicates that manufacturing is the most dominant sector in the US economy. - -### Output multipliers - -Another way to rank sectors in input-output networks is via output multipliers. - -The **output multiplier** of sector $j$ denoted by $\mu_j$ is usually defined as the -total sector-wide impact of a unit change of demand in sector $j$. - -Earlier when disussing demand shocks we concluded that for $L = (l_{ij})$ the element -$l_{ij}$ represents the impact on sector $i$ of a unit change in demand in sector $j$. - -Thus, +### 产出乘数 +在投入产出网络中对行业进行排名的另一种方法是通过产出乘数。 +行业 $j$ 的**产出乘数**,记为 $\mu_j$,通常定义为行业 $j$ 需求单位变化所产生的整个行业范围内的总影响。 +早些时候在讨论需求冲击时,我们得出结论:对于 $L = (l_{ij})$,元素 $l_{ij}$ 表示行业 $j$ 需求单位变化对行业 $i$ 的影响。 +因此, $$ \mu_j = \sum_{j=1}^n l_{ij} $$ -This can be written as $\mu^\top = \mathbb{1}^\top L$ or - +这可以写成 $\mu^\top = \mathbb{1}^\top L$ 或 $$ \mu^\top = \mathbb{1}^\top (I-A)^{-1} $$ -Please note that here we use $\mathbb{1}$ to represent a vector of ones. - -High ranking sectors within this measure are important buyers of intermediate goods. - -A demand shock in such sectors will cause a large impact on the whole production network. - -The following figure displays the output multipliers for the sectors represented -in {numref}`us_15sectors`. +请注意,这里我们用 $\mathbb{1}$ 表示一个全为1的向量。 +在这个指标中排名较高的行业是中间品的重要购买者。 +这些行业的需求冲击将对整个生产网络造成巨大影响。 +下图显示了 {numref}`us_15sectors` 中表示的各行业的产出乘数。 ```{code-cell} ipython3 :tags: [hide-input] @@ -596,34 +537,32 @@ omult = qbn_io.katz_centrality(A, authority=True) fig, ax = plt.subplots() omult_color_list = qbn_io.colorise_weights(omult,beta=False) ax.bar(codes, omult, color=omult_color_list, alpha=0.6) -ax.set_ylabel("Output multipliers", fontsize=12) +ax.set_ylabel("产出乘数", fontsize=12) plt.show() ``` -We observe that manufacturing and agriculture are highest ranking sectors. - +我们观察到制造业和农业是排名最高的行业。 -## Exercises +## 练习 ```{exercise-start} :label: io_ex1 ``` -{cite}`DoSSo` Chapter 9 discusses an example with the following -parameter settings: +{cite}`DoSSo` 第9章讨论了一个具有以下参数设置的例子: $$ A = \begin{bmatrix} 0.1 & 1.46 \\ 0.16 & 0.17 \end{bmatrix} -\text{ and } +\text{ 和 } a_0 = \begin{bmatrix} .04 & .33 \end{bmatrix} $$ $$ x = \begin{bmatrix} 250 \\ 120 \end{bmatrix} -\text{ and } +\text{ 和 } x_0 = 50 $$ @@ -631,17 +570,16 @@ $$ d = \begin{bmatrix} 50 \\ 60 \end{bmatrix} $$ -Describe how they infer the input-output coefficients in $A$ and $a_0$ from the following hypothetical underlying "data" on agricultural and manufacturing industries: +描述他们如何从以下关于农业和制造业的假设"数据"中推断出A和a_0中的投入-产出系数: $$ z = \begin{bmatrix} 25 & 175 \\ 40 & 20 \end{bmatrix} -\text{ and } +\text{ 和 } z_0 = \begin{bmatrix} 10 & 40 \end{bmatrix} $$ -where $z_0$ is a vector of labor services used in each industry. - +其中z_0是每个行业使用的劳动服务的向量。 ```{exercise-end} ``` @@ -661,7 +599,7 @@ $$ :label: io_ex2 ``` -Derive the production possibility frontier for the economy characterized in the previous exercise. +推导上一练习中所描述经济的生产可能性边界。 ```{exercise-end} ``` @@ -689,7 +627,7 @@ A_0 +++ {"user_expressions": []} -Thus the production possibility frontier is given by +因此,生产可能性边界由以下给出 $$ 0.17 d_1 + 0.69 d_2 = 50 diff --git a/lectures/intro_supply_demand.md b/lectures/intro_supply_demand.md index d62d198..ec499c6 100644 --- a/lectures/intro_supply_demand.md +++ b/lectures/intro_supply_demand.md @@ -53,6 +53,12 @@ kernelspec: import numpy as np import matplotlib.pyplot as plt from collections import namedtuple +from matplotlib import font_manager +import matplotlib.font_manager + +# List all available font names +font_list = [f.name for f in matplotlib.font_manager.fontManager.ttflist] +print(font_list) ``` 在我们研究供给和需求模型之前,了解一些关于(a)消费者和生产者剩余以及(b)积分的背景知识会很有帮助。 (如果你对这两个主题都很熟悉,可以跳到{ref}`下一节 `。) diff --git a/lectures/laffer_adaptive.md b/lectures/laffer_adaptive.md index 684f2e6..1989d07 100644 --- a/lectures/laffer_adaptive.md +++ b/lectures/laffer_adaptive.md @@ -11,193 +11,190 @@ kernelspec: name: python3 --- -# Laffer Curves with Adaptive Expectations +# Laffer曲线与自适应预期 -## Overview +## 概览 -This lecture studies stationary and dynamic **Laffer curves** in the inflation tax rate in a non-linear version of the model studied in this lecture {doc}`money_inflation`. +本讲座研究了在通货膨胀税率下的静态和动态**Laffer曲线**,其采用的模型为此讲座{doc}`money_inflation`中研究的非线性版本。 -As in the lecture {doc}`money_inflation`, this lecture uses the log-linear version of the demand function for money that {cite}`Cagan` used in his classic paper in place of the linear demand function used in this lecture {doc}`money_inflation`. +与讲座{doc}`money_inflation`中一样,此讲座使用了{cite}`Cagan`在其经典论文中使用的对数线性货币需求函数版本,而不是此讲座{doc}`money_inflation`中使用的线性需求函数。 -But now, instead of assuming ''rational expectations'' in the form of ''perfect foresight'', -we'll adopt the ''adaptive expectations'' assumption used by {cite}`Cagan` and {cite}`Friedman1956`. +但现在,我们将不采用''理性预期''的''完全预见''形式,而是采用{cite}`Cagan`和{cite}`Friedman1956`使用的''自适应预期''假设。 -This means that instead of assuming that expected inflation $\pi_t^*$ is described by the "perfect foresight" or "rational expectations" hypothesis +这意味着,我们不再假设预期通货膨胀$\pi_t^*$是由"完全预见"或"理性预期"假设描述 $$ \pi_t^* = p_{t+1} - p_t -$$ +$$ -that we adopted in lectures {doc}`money_inflation` and lectures {doc}`money_inflation_nonlinear`, we'll now assume that $\pi_t^*$ is determined by the adaptive expectations hypothesis described in equation {eq}`eq:adaptex` reported below. +我们在讲座{doc}`money_inflation`和讲座{doc}`money_inflation_nonlinear`中采用的假设,现在我们假设$\pi_t^*$由下面报告的自适应预期假设{eq}`eq:adaptex`决定。 -We shall discover that changing our hypothesis about expectations formation in this way will change some our findings and leave others intact. In particular, we shall discover that +我们将发现,以这种方式改变关于预期形成的假设会改变我们的一些发现并保持其他发现不变。特别是,我们将发现 -* replacing rational expectations with adaptive expectations leaves the two stationary inflation rates unchanged, but that $\ldots$ -* it reverses the perverse dynamics by making the **lower** stationary inflation rate the one to which the system typically converges -* a more plausible comparative dynamic outcome emerges in which now inflation can be **reduced** by running **lower** government deficits +* 用自适应预期替换理性预期不改变两个静态通货膨胀率,但是$\ldots$ +* 它通过使系统通常收敛于**较低**的静态通货膨胀率来逆转不正常的动态 +* 出现了更加合理的比较动态结果,现在通过运行**更低**的政府赤字可以**降低**通货膨胀 -These more plausible comparative dynamics underlie the "old time religion" that states that -"inflation is always and everywhere caused by government deficits". +这些更合理的比较动态是建立在“老式信仰”上的,即“通货膨胀总是且处处由政府赤字引起”。 -These issues were studied by {cite}`bruno1990seigniorage`. +{cite}`bruno1990seigniorage`研究了这些问题。 -Their purpose was to reverse what they thought were counter intuitive -predictions of their model under rational expectations (i.e., perfect foresight in this context) -by dropping rational expectations and instead assuming that people form expectations about future inflation rates according to the "adaptive expectations" scheme {eq}`eq:adaptex` described below. +他们的目的是通过放弃理性预期,而假设人们根据下面描述的“自适应预期”方案{eq}`eq:adaptex`来形成对未来通货膨胀率的预期,从而逆转他们认为在理性预期(即在这种情况下的完全预见)下模型的违反直觉的预测。 ```{note} -{cite}`sargent1989least` had studied another way of selecting stationary equilibrium that involved replacing rational expectations with a model of learning via least squares regression. - {cite}`marcet2003recurrent` and {cite}`sargent2009conquest` extended that work and applied it to study recurrent high-inflation episodes in Latin America. -``` +{sargeant1989least} 研究了另一种选择静态均衡的方法,涉及用通过最小二乘回归学习的模型替换理性预期。 +{sargeant2009conquest} 和 {marcet2003recurrent} 扩展了这项工作,并将其应用于研究拉丁美洲反复出现的高通胀情节。 +``` -## The model +## 模型 -Let +设 -* $m_t$ be the log of the money supply at the beginning of time $t$ -* $p_t$ be the log of the price level at time $t$ -* $\pi_t^*$ be the public's expectation of the rate of inflation between $t$ and $t+1$ +* $m_t$ 为时间 $t$ 初始的货币供应量对数 +* $p_t$ 为时间 $t$ 的价格水平对数 +* $\pi_t^*$ 为公众对于时间 $t$ 到 $t+1$ 之间的通胀率的预期 -The law of motion of the money supply is +货币供应量的动态方程是 $$ \exp(m_{t+1}) - \exp(m_t) = g \exp(p_t) $$ (eq:ada_msupply) -where $g$ is the part of government expenditures financed by printing money. +其中 $g$ 是政府支出中通过印制货币来融资的部分。 -Notice that equation {eq}`eq:ada_msupply` implies that +注意方程 {eq}`eq:ada_msupply` 暗示 $$ m_{t+1} = \log[ \exp(m_t) + g \exp(p_t)] $$ (eq:ada_msupply2) -The demand function for money is +货币需求函数是 $$ m_{t+1} - p_t = -\alpha \pi_t^* $$ (eq:ada_mdemand) -where $\alpha \geq 0$. +其中 $\alpha \geq 0$。 -Expectations of inflation are governed by +通胀预期受控于 $$ \pi_{t}^* = (1-\delta) (p_t - p_{t-1}) + \delta \pi_{t-1}^* $$ (eq:adaptex) -where $\delta \in (0,1)$ +其中 $\delta \in (0,1)$ -## Computing an equilibrium sequence +## 计算平衡序列 -Equation the expressions for $m_{t+1}$ provided by {eq}`eq:ada_mdemand` and {eq}`eq:ada_msupply2` and use equation {eq}`eq:adaptex` to eliminate $\pi_t^*$ to obtain -the following equation for $p_t$: +通过将{eq}`eq:ada_mdemand` 和 {eq}`eq:ada_msupply2` 提供的 $m_{t+1}$ 表达式与使用方程 {eq}`eq:adaptex` 消除 $\pi_t^*$,得到下列 $p_t$ 的方程: $$ \log[ \exp(m_t) + g \exp(p_t)] - p_t = -\alpha [(1-\delta) (p_t - p_{t-1}) + \delta \pi_{t-1}^*] $$ (eq:pequation) -**Pseudo-code** +**伪代码** -Here is the pseudo-code for our algorithm. +这是我们算法的伪代码。 -Starting at time $0$ with initial conditions $(m_0, \pi_{-1}^*, p_{-1})$, for each $t \geq 0$ -deploy the following steps in order: +从时间 $0$ 开始,初始条件为 $(m_0, \pi_{-1}^*, p_{-1})$,对于每个 $t \geq 0$ 按顺序执行以下步骤: -* solve {eq}`eq:pequation` for $p_t$ -* solve equation {eq}`eq:adaptex` for $\pi_t^*$ -* solve equation {eq}`eq:ada_msupply2` for $m_{t+1}$ +* 解方程 {eq}`eq:pequation` 以求得 $p_t$ +* 解方程 {eq}`eq:adaptex` 以求得 $\pi_t^*$ +* 解方程 {eq}`eq:ada_msupply2` 以求得 $m_{t+1}$ -This completes the algorithm. +这完成了算法。 +## 声明或猜想 -## Claims or conjectures - - -It will turn out that +结果表明: -* if they exist, limiting values $\overline \pi$ and $\overline \mu$ will be equal +* 如果存在,限制值 $\overline \pi$ 和 $\overline \mu$ 将是相等的 -* if limiting values exist, there are two possible limiting values, one high, one low +* 如果存在限制值,有两种可能的限制值,一个高,一个低 -* unlike the outcome in lecture {doc}`money_inflation_nonlinear`, for almost all initial log price levels and expected inflation rates $p_0, \pi_{t}^*$, the limiting $\overline \pi = \overline \mu$ is the **lower** steady state value +* 与讲座{doc}`money_inflation_nonlinear`中的结果不同,对于几乎所有初始对数价格水平和预期通货膨胀率 $p_0, \pi_{t}^*$,限制 $\overline \pi = \overline \mu$ 是**较低**的稳态值 -* for each of the two possible limiting values $\bar \pi$ ,there is a unique initial log price level $p_0$ that implies that $\pi_t = \mu_t = \bar \mu$ for all $t \geq 0$ +* 对于这两个可能的限制值 $\overline \pi$,有一个唯一的初始对数价格水平 $p_0$,意味着 $\pi_t = \mu_t = \overline \mu$ 对所有 $t \geq 0$ - * this unique initial log price level solves $\log(\exp(m_0) + g \exp(p_0)) - p_0 = - \alpha \bar \pi $ + * 这个唯一的初始对数价格水平解决 $\log(\exp(m_0) + g \exp(p_0)) - p_0 = - \alpha \overline \pi$ - * the preceding equation for $p_0$ comes from $m_1 - p_0 = - \alpha \bar \pi$ - -## Limiting values of inflation rate + * 上述关于 $p_0$ 的方程来自于 $m_1 - p_0 = - \alpha \overline \pi$ + +## 通货膨胀率的限制值 -As in our earlier lecture {doc}`money_inflation_nonlinear`, we can compute the two prospective limiting values for $\bar \pi$ by studying the steady-state Laffer curve. +正如我们在早前的讲座 {doc}`money_inflation_nonlinear` 中讨论的,我们可以通过研究稳态劳动曲线来计算 $\bar \pi$ 的两个潜在的限制值。 -Thus, in a **steady state** +因此,在一个**稳态**中 $$ m_{t+1} - m_t = p_{t+1} - p_t = x \quad \forall t , $$ -where $x > 0 $ is a common rate of growth of logarithms of the money supply and price level. +其中 $x > 0$ 是货币供应量和价格水平的对数的共同增长率。 -A few lines of algebra yields the following equation that $x$ satisfies +几行代数可以得出满足 $x$ 的以下方程 $$ \exp(-\alpha x) - \exp(-(1 + \alpha) x) = g $$ (eq:ada_steadypi) -where we require that +我们要求 $$ g \leq \max_{x: x \geq 0} \exp(-\alpha x) - \exp(-(1 + \alpha) x) , $$ (eq:ada_revmax) -so that it is feasible to finance $g$ by printing money. +这样就可以通过印钞来财务支持 $g$。 -The left side of {eq}`eq:ada_steadypi` is steady state revenue raised by printing money. +{eq}`eq:ada_steadypi` 的左侧是通过印钞筹集的稳定状态收入。 -The right side of {eq}`eq:ada_steadypi` is the quantity of time $t$ goods that the government raises by printing money. +{eq}`eq:ada_steadypi` 的右侧是政府通过印钞筹集的 $t$ 时刻的商品数量。 -Soon we'll plot the left and right sides of equation {eq}`eq:ada_steadypi`. +很快我们将绘制方程 {eq}`eq:ada_steadypi` 的左右两侧。 -But first we'll write code that computes a steady-state -$\bar \pi$. +但首先,我们将编写代码来计算稳态的 +$\bar \pi$。 -Let's start by importing some libraries +让我们开始导入一些库 ```{code-cell} ipython3 from collections import namedtuple import numpy as np import matplotlib.pyplot as plt +import matplotlib as mpl from matplotlib.ticker import MaxNLocator from matplotlib.cm import get_cmap from matplotlib.colors import to_rgba import matplotlib from scipy.optimize import root, fsolve + +FONTPATH = "fonts/SourceHanSerifSC-SemiBold.otf" +mpl.font_manager.fontManager.addfont(FONTPATH) +plt.rcParams['font.family'] = ['Source Han Serif SC'] ``` +++ {"user_expressions": []} -Let's create a `namedtuple` to store the parameters of the model +让我们创建一个 `namedtuple` 来存储模型的参数 ```{code-cell} ipython3 LafferAdaptive = namedtuple('LafferAdaptive', - ["m0", # log of the money supply at t=0 - "α", # sensitivity of money demand - "g", # government expenditure + ["m0", # t=0时货币供应量的对数 + "α", # 货币需求的敏感性 + "g", # 政府支出 "δ"]) -# Create a Cagan Laffer model +# 创建一个 Cagan Laffer 模型 def create_model(α=0.5, m0=np.log(100), g=0.35, δ=0.9): return LafferAdaptive(α=α, m0=m0, g=g, δ=δ) model = create_model() ``` -Now we write code that computes steady-state $\bar \pi$s. +现在我们编写计算稳态 $\bar \pi$ 的代码。 ```{code-cell} ipython3 -# Define formula for π_bar +# 定义 π_bar 的计算公式 def solve_π(x, α, g): return np.exp(-α * x) - np.exp(-(1 + α) * x) - g @@ -205,24 +202,23 @@ def solve_π_bar(model, x0): π_bar = fsolve(solve_π, x0=x0, xtol=1e-10, args=(model.α, model.g))[0] return π_bar -# Solve for the two steady state of π +# 解两个稳态的 π π_l = solve_π_bar(model, x0=0.6) π_u = solve_π_bar(model, x0=3.0) -print(f'The two steady state of π are: {π_l, π_u}') +print(f'两个稳态的π是: {π_l, π_u}') ``` -We find two steady state $\bar \pi$ values +我们找到了两个稳态 $\bar \pi$ 值。 -## Steady-state Laffer curve +## 稳态拉弗曲线 -The following figure plots the steady-state Laffer curve together with the two stationary inflation rates. +下图绘制了稳态拉弗曲线以及两个稳定的通货膨胀率。 ```{code-cell} ipython3 --- mystnb: figure: - caption: Seigniorage as function of steady-state inflation. The dashed brown lines - indicate $\pi_l$ and $\pi_u$. + caption: 稳态通胀下的铸币税函数。虚线棕色线条代表$\pi_l$和$\pi_u$。 name: laffer_curve_adaptive width: 500px --- @@ -232,13 +228,13 @@ def compute_seign(x, α): def plot_laffer(model, πs): α, g = model.α, model.g - # Generate π values + # 生成π值 x_values = np.linspace(0, 5, 1000) - # Compute corresponding seigniorage values for the function + # 计算对应的铸币税值 y_values = compute_seign(x_values, α) - # Plot the function + # 绘制函数图形 plt.plot(x_values, y_values, label=f'$exp((-{α})x) - exp(- (1- {α}) x)$') for π, label in zip(πs, ['$\pi_l$', '$\pi_u$']): @@ -249,20 +245,20 @@ def plot_laffer(model, πs): plt.axhline(g, color='red', linewidth=0.5, linestyle='--', label='g') plt.xlabel('$\pi$') - plt.ylabel('seigniorage') + plt.ylabel('铸币税') plt.legend() plt.grid(True) plt.show() -# Steady state Laffer curve +# 稳态拉弗曲线 plot_laffer(model, (π_l, π_u)) ``` -## Associated initial price levels +## 相关的初始价格水平 -Now that we have our hands on the two possible steady states, we can compute two initial log price levels $p_{-1}$, which as initial conditions, imply that $\pi_t = \bar \pi $ for all $t \geq 0$. +现在我们掌握了两个可能的稳定状态,我们可以计算两个初始对数价格水平 $p_{-1}$,作为初始条件,这意味着对于所有 $t \geq 0$,$\pi_t = \bar \pi$。 -In particular, to initiate a fixed point of the dynamic Laffer curve dynamics, we set +特别是,为了启动动态劳弗曲线动态的固定点,我们设置 $$ p_{-1} = m_0 + \alpha \pi^* @@ -273,17 +269,16 @@ def solve_p_init(model, π_star): m0, α = model.m0, model.α return m0 + α*π_star - -# Compute two initial price levels associated with π_l and π_u +# 计算与 π_l 和 π_u 相关联的两个初始价格水平 p_l, p_u = map(lambda π: solve_p_init(model, π), (π_l, π_u)) -print('Associated initial p_{-1}s', f'are: {p_l, p_u}') +print('相关的初始 p_{-1}', f'为: {p_l, p_u}') ``` -### Verification +### 验证 -To start, let's write some code to verify that if we initial $\pi_{-1}^*,p_{-1}$ appropriately, the inflation rate $\pi_t$ will be constant for all $t \geq 0$ (at either $\pi_u$ or $\pi_l$ depending on the initial condition) +首先,我们编写一些代码来验证,如果我们适当初始化 $\pi_{-1}^*,p_{-1}$,则通货膨胀率 $\pi_t$ 对于所有 $t \geq 0$ 将保持恒定(取决于初始条件,可以是 $\pi_u$ 或 $\pi_l$) -The following code verifies this. +以下代码进行了验证。 ```{code-cell} ipython3 def solve_laffer_adapt(p_init, π_init, model, num_steps): @@ -299,65 +294,64 @@ def solve_laffer_adapt(p_init, π_init, model, num_steps): p_seq[0] = p_init for t in range(1, num_steps): - # Solve p_t + # 解决 p_t def p_t(pt): - return np.log(np.exp(m_seq[t]) + g * np.exp(pt)) \ - - pt + α * ((1-δ)*(pt - p_seq[t-1]) + δ*π_seq[t-1]) + return np.log(np.exp(m_seq[t]) + g * np.exp(pt)) - pt + α * ((1-δ)*(pt - p_seq[t-1]) + δ*π_seq[t-1]) p_seq[t] = root(fun=p_t, x0=p_seq[t-1]).x[0] - # Solve π_t + # 解决 π_t π_seq[t] = (1-δ) * (p_seq[t]-p_seq[t-1]) + δ*π_seq[t-1] - # Solve m_t + # 解决 m_t m_seq[t+1] = np.log(np.exp(m_seq[t]) + g*np.exp(p_seq[t])) - # Solve μ_t + # 解决 μ_t μ_seq[t] = m_seq[t+1] - m_seq[t] return π_seq, μ_seq, m_seq, p_seq ``` -Compute limiting values starting from $p_{-1}$ associated with $\pi_l$ +计算从 $p_{-1}$ 开始,与 $\pi_l$ 相关联的极限值 ```{code-cell} ipython3 π_seq, μ_seq, m_seq, p_seq = solve_laffer_adapt(p_l, π_l, model, 50) -# Check steady state m_{t+1} - m_t and p_{t+1} - p_t +# 检查稳态 m_{t+1} - m_t 和 p_{t+1} - p_t print('m_{t+1} - m_t:', m_seq[-1] - m_seq[-2]) print('p_{t+1} - p_t:', p_seq[-1] - p_seq[-2]) -# Check if exp(-αx) - exp(-(1 + α)x) = g +# 检查 exp(-αx) - exp(-(1 + α)x) 是否等于 g eq_g = lambda x: np.exp(-model.α * x) - np.exp(-(1 + model.α) * x) print('eq_g == g:', np.isclose(eq_g(m_seq[-1] - m_seq[-2]), model.g)) ``` -Compute limiting values starting from $p_{-1}$ associated with $\pi_u$ +计算从 $p_{-1}$ 开始,与 $\pi_u$ 相关联的极限值 ```{code-cell} ipython3 π_seq, μ_seq, m_seq, p_seq = solve_laffer_adapt(p_u, π_u, model, 50) -# Check steady state m_{t+1} - m_t and p_{t+1} - p_t +# 检查稳态 m_{t+1} - m_t 和 p_{t+1} - p_t print('m_{t+1} - m_t:', m_seq[-1] - m_seq[-2]) print('p_{t+1} - p_t:', p_seq[-1] - p_seq[-2]) -# Check if exp(-αx) - exp(-(1 + α)x) = g +# 检查 exp(-αx) - exp(-(1 + α)x) 是否等于 g eq_g = lambda x: np.exp(-model.α * x) - np.exp(-(1 + model.α) * x) print('eq_g == g:', np.isclose(eq_g(m_seq[-1] - m_seq[-2]), model.g)) ``` -## Slippery side of Laffer curve dynamics +## 动态拉弗曲线的滑动侧面 -We are now equipped to compute time series starting from different $p_{-1}, \pi_{-1}^*$ settings, analogous to those in this lecture {doc}`money_inflation` and this lecture {doc}`money_inflation_nonlinear`. +我们现在已经具备了从不同的 $p_{-1}, \pi_{-1}^*$ 设置开始计算时间序列的能力,类似于这个讲座中的设置 {doc}`money_inflation` 和这个讲座 {doc}`money_inflation_nonlinear`。 -Now we'll study how outcomes unfold when we start $p_{-1}, \pi_{-1}^*$ away from a stationary point of the dynamic Laffer curve, i.e., away from either $\pi_u$ or $ \pi_l$. +现在我们将研究当我们从动态拉弗曲线的一个静止点开始 $p_{-1}, \pi_{-1}^*$,即远离 $\pi_u$ 或 $ \pi_l$ 时,结果如何展开。 -To construct a perturbation pair $\check p_{-1}, \check \pi_{-1}^*$we'll implement the following pseudo code: +为了构建一个扰动对 $\check p_{-1}, \check \pi_{-1}^*$,我们将实现以下伪代码: -* set $\check \pi_{-1}^* $ not equal to one of the stationary points $\pi_u$ or $ \pi_l$. -* set $\check p_{-1} = m_0 + \alpha \check \pi_{-1}^*$ +* 设置 $\check \pi_{-1}^* $ 不等于静止点之一 $\pi_u$ 或 $ \pi_l$。 +* 设置 $\check p_{-1} = m_0 + \alpha \check \pi_{-1}^*$ ```{code-cell} ipython3 :tags: [hide-cell] @@ -388,31 +382,29 @@ def draw_iterations(π0s, model, line_params, π_bars, num_steps): axes[1].set_ylabel('$p_t$') axes[2].set_ylabel('$\pi_t$') axes[3].set_ylabel('$\mu_t$') - axes[3].set_xlabel('timestep') + axes[3].set_xlabel('时间步') axes[3].xaxis.set_major_locator(MaxNLocator(integer=True)) plt.tight_layout() plt.show() ``` -Let's simulate the result generated by varying the initial $\pi_{-1}$ and corresponding $p_{-1}$ +让我们模拟通过改变初始 $\pi_{-1}$ 和对应的 $p_{-1}$ 生成的结果 ```{code-cell} ipython3 --- mystnb: figure: - caption: Starting from different initial values of $\pi_0$, paths of $m_t$ (top - panel, log scale for $m$), $p_t$ (second panel, log scale for $p$), $\pi_t$ (third panel), and $\mu_t$ (bottom - panel) + caption: 从不同的 $\pi_0$ 初始值开始,$m_t$ 的路径(顶部面板,$m$ 的对数标度),$p_t$(第二面板,$p$ 的对数标度),$\pi_t$(第三面板),和 $\mu_t$(底部面板) name: pi0_path width: 500px --- πs = np.linspace(π_l, π_u, 10) -line_params = {'lw': 1.5, - 'marker': 'o', - 'markersize': 3} +line_params = {'lw': 1.5, + 'marker': 'o', + 'markersize': 3} π_bars = (π_l, π_u) draw_iterations(πs, model, line_params, π_bars, num_steps=80) -``` +``` \ No newline at end of file diff --git a/lectures/lake_model.md b/lectures/lake_model.md index f70da94..30a24d9 100644 --- a/lectures/lake_model.md +++ b/lectures/lake_model.md @@ -11,49 +11,42 @@ kernelspec: name: python3 --- -# A Lake Model of Employment - -## Outline - -In addition to what's in Anaconda, this lecture will need the following libraries: +# 就业的湖泊模型 +## 大纲 +除了 Anaconda 中包含的内容外,本讲座还需要以下库: ```{code-cell} ipython3 import numpy as np import matplotlib.pyplot as plt ``` -## The Lake model - -This model is sometimes called the **lake model** because there are two pools of workers: - -1. those who are currently employed. -2. those who are currently unemployed but are seeking employment. - -The "flows" between the two lakes are as follows: +## 湖泊模型 +这个模型有时被称为**湖泊模型**,因为存在两个工人群体: +1. 当前就业的工人。 +2. 当前失业但正在寻找工作的工人。 -1. workers exit the labor market at rate $d$. -2. new workers enter the labor market at rate $b$. -3. employed workers separate from their jobs at rate $\alpha$. -4. unemployed workers find jobs at rate $\lambda$. +两个"湖泊"之间的"流动"如下: +1. 工人以 $d$ 的比率退出劳动力市场。 +2. 新工人以 $b$ 的比率进入劳动力市场。 +3. 就业工人以 $\alpha$ 的比率与工作分离。 +4. 失业工人以 $\lambda$ 的比率找到工作。 -The graph below illustrates the lake model. +下图说明了湖泊模型。 ```{figure} /_static/lecture_specific/lake_model/lake_model_worker.png :name: lake_model_graphviz -An illustration of the lake model +湖泊模型的示意图 ``` -## Dynamics - -Let $e_t$ and $u_t$ be the number of employed and unemployed workers at time $t$ respectively. - -The total population of workers is $n_t = e_t + u_t$. - -The number of unemployed and employed workers thus evolves according to: +## 动态变化 +设 $e_t$ 和 $u_t$ 分别表示在时间 $t$ 时就业和失业工人的数量。 +工人总人口为 $n_t = e_t + u_t$。 +因此,失业和就业工人的数量按以下方式变化: ```{math} :label: lake_model + \begin{aligned} u_{t+1} &= (1-d)(1-\lambda)u_t + \alpha(1-d)e_t + bn_t \\ &= ((1-d)(1-\lambda) + b)u_t + (\alpha(1-d) + b)e_t \\ @@ -61,7 +54,7 @@ The number of unemployed and employed workers thus evolves according to: \end{aligned} ``` -We can arrange {eq}`lake_model` as a linear system of equations in matrix form $x_{t+1} = Ax_t$ where +我们可以将{eq}`lake_model`安排为矩阵形式的线性方程组 $x_{t+1} = Ax_t$,其中 $$ x_{t+1} = @@ -75,7 +68,7 @@ A = (1-d)(1-\lambda) + b & \alpha(1-d) + b \\ (1-d)\lambda & (1 - \alpha)(1-d) \end{bmatrix} -\quad \text{and} \quad +\quad \text{且} \quad x_t = \begin{bmatrix} u_t \\ @@ -83,39 +76,31 @@ x_t = \end{bmatrix}. $$ -Suppose at $t=0$ we have $x_0 = \begin{bmatrix} u_0 & e_0 \end{bmatrix}^\top$. +假设在 $t=0$ 时,我们有 $x_0 = \begin{bmatrix} u_0 & e_0 \end{bmatrix}^\top$。 +那么,$x_1=Ax_0$,$x_2=Ax_1=A^2x_0$,因此 $x_t = A^tx_0$。 +因此,该系统的长期结果可能取决于初始条件 $x_0$ 和矩阵 $A$。 +我们关心 $u_t$ 和 $e_t$ 随时间如何演变。 +我们应该预期什么样的长期失业率和就业率? +长期结果是否取决于初始值 $(u_0, e_o)$? -Then, $x_1=Ax_0$, $x_2=Ax_1=A^2x_0$ and thus $x_t = A^tx_0$. - -Thus the long-run outcomes of this system may depend on the initial condition $x_0$ and the matrix $A$. - -We are interested in how $u_t$ and $e_t$ evolve over time. - -What long-run unemployment rate and employment rate should we expect? - -Do long-run outcomes depend on the initial values $(u_0, e_o)$? - -### Visualising the long-run outcomes - -Let us first plot the time series of unemployment $u_t$, employment $e_t$, and labor force $n_t$. +### 可视化长期结果 +让我们首先绘制失业率 $u_t$、就业率 $e_t$ 和劳动力 $n_t$ 的时间序列图。 ```{code-cell} ipython3 class LakeModel: """ - Solves the lake model and computes dynamics of the unemployment stocks and - rates. - - Parameters: - ------------ - λ : scalar - The job finding rate for currently unemployed workers - α : scalar - The dismissal rate for currently employed workers - b : scalar - Entry rate into the labor force - d : scalar - Exit rate from the labor force - + 求解湖泊模型并计算失业存量和失业率的动态变化。 + + 参数: + ------------ + λ:标量 + 当前失业工人的找到工作的比率 + α:标量 + 当前就业工人的解雇率 + b:标量 + 进入劳动力市场的比率 + d:标量 + 退出劳动力市场的比率 """ def __init__(self, λ=0.1, α=0.013, b=0.0124, d=0.00822): self.λ, self.α, self.b, self.d = λ, α, b, d @@ -134,22 +119,21 @@ class LakeModel: def simulate_path(self, x0, T=1000): """ - Simulates the sequence of employment and unemployment + 模拟就业和失业的序列 - Parameters + 参数 ---------- - x0 : array - Contains initial values (u0,e0) - T : int - Number of periods to simulate + x0:数组 + 包含初始值 (u0,e0) + T:整数 + 模拟的周期数 - Returns + 返回值 ---------- - x : iterator - Contains sequence of employment and unemployment rates - + x:迭代器 + 包含就业率和失业率的序列 """ - x0 = np.atleast_1d(x0) # Recast as array just in case + x0 = np.atleast_1d(x0) # 以防万一,重新转换为数组 x_ts= np.zeros((2, T)) x_ts[:, 0] = x0 for t in range(1, T): @@ -159,11 +143,11 @@ class LakeModel: ```{code-cell} ipython3 lm = LakeModel() -e_0 = 0.92 # Initial employment -u_0 = 1 - e_0 # Initial unemployment, given initial n_0 = 1 +e_0 = 0.92 # 初始就业 +u_0 = 1 - e_0 # 给定初始 n_0 = 1 的情况下的初始失业率 lm = LakeModel() -T = 100 # Simulation length +T = 100 # 模拟时长 x_0 = (u_0, e_0) x_path = lm.simulate_path(x_0, T) @@ -172,13 +156,13 @@ fig, axes = plt.subplots(3, 1, figsize=(10, 8)) axes[0].plot(x_path[0, :], lw=2) -axes[0].set_title('Unemployment') +axes[0].set_title('失业') axes[1].plot(x_path[1, :], lw=2) -axes[1].set_title('Employment') +axes[1].set_title('就业') axes[2].plot(x_path.sum(0), lw=2) -axes[2].set_title('Labor force') +axes[2].set_title('劳动力') for ax in axes: ax.grid() @@ -186,17 +170,12 @@ for ax in axes: plt.tight_layout() plt.show() ``` - -Not surprisingly, we observe that labor force $n_t$ increases at a constant rate. - -This coincides with the fact there is only one inflow source (new entrants pool) to unemployment and employment pools. - -The inflow and outflow of labor market system -is determined by constant exit rate and entry rate of labor market in the long run. - -In detail, let $\mathbb{1}=[1, 1]^\top$ be a vector of ones. - -Observe that +将劳动力 $n_t$ 以恒定速率增长的观察结果并不令人惊讶。 +这与失业和就业池只有一个流入源(新进入者池)的事实相吻合。 +劳动力市场系统的流入和流出 +在长期内由劳动力市场的固定退出率和进入率决定。 +具体来说,令 $\mathbb{1}=[1, 1]^\top$ 为一个全1向量。 +观察到 $$ \begin{aligned} @@ -208,74 +187,59 @@ $$ \end{aligned} $$ -Hence, the growth rate of $n_t$ is fixed at $1 + b - d$. - -Moreover, the times series of unemployment and employment seems to grow at some stable rates in the long run. - -### The application of Perron-Frobenius theorem - -Since by intuition if we consider unemployment pool and employment pool as a closed system, the growth should be similar to the labor force. - -We next ask whether the long-run growth rates of $e_t$ and $u_t$ -also dominated by $1+b-d$ as labor force. - -The answer will be clearer if we appeal to {ref}`Perron-Frobenius theorem`. - -The importance of the Perron-Frobenius theorem stems from the fact that -firstly in the real world most matrices we encounter are nonnegative matrices. - -Secondly, many important models are simply linear iterative models that -begin with an initial condition $x_0$ and then evolve recursively by the rule -$x_{t+1} = Ax_t$ or in short $x_t = A^tx_0$. - -This theorem helps characterise the dominant eigenvalue $r(A)$ which -determines the behavior of this iterative process. - -#### Dominant eigenvector - -We now illustrate the power of the Perron-Frobenius theorem by showing how it -helps us to analyze the lake model. - -Since $A$ is a nonnegative and irreducible matrix, the Perron-Frobenius theorem implies that: - -- the spectral radius $r(A)$ is an eigenvalue of $A$, where +因此,$n_t$ 的增长率固定为 $1 + b - d$。 +此外,失业和就业的时间序列似乎在长期内以某些稳定的速率增长。 + +### 佩龙-弗罗贝尼乌斯定理的应用 +直观上,如果我们将失业池和就业池视为一个封闭系统,其增长应与劳动力相似。 +接下来我们询问 $e_t$ 和 $u_t$ 的长期增长率 +是否也由 $1+b-d$ 主导,就像劳动力一样。 +如果我们求助于{ref}`佩龙-弗罗贝尼乌斯定理`,答案将更加清晰。 +佩龙-弗罗贝尼乌斯定理的重要性源于以下事实: +首先,在现实世界中,我们遇到的大多数矩阵都是非负矩阵。 +其次,许多重要模型都是简单的线性迭代模型, +从初始条件 $x_0$ 开始,然后按规则 $x_{t+1} = Ax_t$ 或简写为 $x_t = A^tx_0$ 递归演变。 +这个定理有助于表征主导特征值 $r(A)$,它 +决定了这个迭代过程的行为。 + +#### 主导特征向量 +现在我们通过展示佩龙-弗罗贝尼乌斯定理如何帮助我们分析湖泊模型来说明它的力量。 +由于 $A$ 是非负且不可约的矩阵,佩龙-弗罗贝尼乌斯定理意味着: +- 谱半径 $r(A)$ 是 $A$ 的一个特征值,其中 $$ - r(A) := \max\{|\lambda|: \lambda \text{ is an eigenvalue of } A \} + r(A) := \max\{|\lambda|: \lambda \text{ 是 } A \text{ 的特征值 } \} $$ -- any other eigenvalue $\lambda$ in absolute value is strictly smaller than $r(A)$: $|\lambda|< r(A)$, - -- there exist unique and everywhere positive right eigenvector $\phi$ (column vector) and left eigenvector $\psi$ (row vector): +- 任何其他特征值 $\lambda$ 的绝对值都严格小于 $r(A)$:$|\lambda|< r(A)$, +- 存在唯一且处处正的右特征向量 $\phi$(列向量)和左特征向量 $\psi$(行向量): $$ A \phi = r(A) \phi, \quad \psi A = r(A) \psi $$ -- if further $A$ is positive, then with $<\psi, \phi> = \psi \phi=1$ we have +- 如果进一步 $A$ 是正的,那么当 $<\psi, \phi> = \psi \phi=1$ 时,我们有 $$ r(A)^{-t} A^t \to \phi \psi $$ -The last statement implies that the magnitude of $A^t$ is identical to the magnitude of $r(A)^t$ in the long run, where $r(A)$ can be considered as the dominant eigenvalue in this lecture. - -Therefore, the magnitude $x_t = A^t x_0$ is also dominated by $r(A)^t$ in the long run. - -Recall that the spectral radius is bounded by column sums: for $A \geq 0$, we have +最后一个陈述意味着长期来看,$A^t$ 的量级与 $r(A)^t$ 的量级相同,其中 $r(A)$ 在本讲座中可被视为主导特征值。 +因此,长期来看,$x_t = A^t x_0$ 的量级也由 $r(A)^t$ 主导。 +回想一下,谱半径受列和的约束:对于 $A \geq 0$,我们有 ```{math} :label: PF_bounds + \min_j \text{colsum}_j (A) \leq r(A) \leq \max_j \text{colsum}_j (A) ``` -Note that $\text{colsum}_j(A) = 1 + b - d$ for $j=1,2$ and by {eq}`PF_bounds` we can thus conclude that the dominant eigenvalue -is $r(A) = 1 + b - d$. +注意,对于 $j=1,2$,$\text{colsum}_j(A) = 1 + b - d$,根据{eq}`PF_bounds`,我们因此可以得出主特征值为 $r(A) = 1 + b - d$。 -Denote $g = b - d$ as the overall growth rate of the total labor force, so that $r(A) = 1 + g$. +令 $g = b - d$ 表示总劳动力的整体增长率,因此 $r(A) = 1 + g$。 -The Perron-Frobenius implies that there is a unique positive eigenvector $\bar{x} = \begin{bmatrix} \bar{u} \\ \bar{e} \end{bmatrix}$ -such that $A\bar{x} = r(A)\bar{x}$ and $\begin{bmatrix} 1 & 1 \end{bmatrix} \bar{x} = 1$: +佩龙-弗罗贝尼乌斯定理意味着存在唯一的正特征向量 $\bar{x} = \begin{bmatrix} \bar{u} \\ \bar{e} \end{bmatrix}$, +使得 $A\bar{x} = r(A)\bar{x}$ 且 $\begin{bmatrix} 1 & 1 \end{bmatrix} \bar{x} = 1$: ```{math} :label: steady_x @@ -286,23 +250,21 @@ such that $A\bar{x} = r(A)\bar{x}$ and $\begin{bmatrix} 1 & 1 \end{bmatrix} \bar \end{aligned} ``` -Since $\bar{x}$ is the eigenvector corresponding to the dominant eigenvalue $r(A)$, we call $\bar{x}$ the dominant eigenvector. - -This dominant eigenvector plays an important role in determining long-run outcomes as illustrated below. +由于 $\bar{x}$ 是对应于主特征值 $r(A)$ 的特征向量,我们称 $\bar{x}$ 为主特征向量。 +这个主特征向量在确定长期结果方面起着重要作用,如下所示。 ```{code-cell} ipython3 def plot_time_paths(lm, x0=None, T=1000, ax=None): """ - Plots the simulated time series. - - Parameters + 绘制模拟的时间序列图。 + 参数 ---------- - lm : class - Lake Model - x0 : array - Contains some different initial values. - T : int - Number of periods to simulate + lm : 类 + 湖泊模型 + x0 : 数组 + 包含一些不同的初始值。 + T : 整数 + 要模拟的周期数 """ @@ -336,7 +298,7 @@ def plot_time_paths(lm, x0=None, T=1000, ax=None): - # Plot time series + # 绘制时间序列 for x in x0: x_ts = lm.simulate_path(x0=x) @@ -369,52 +331,38 @@ x0 = ((5.0, 0.1), (0.1, 4.0), (2.0, 1.0)) plot_time_paths(lm, x0=x0) ``` -Since $\bar{x}$ is an eigenvector corresponding to the eigenvalue $r(A)$, all the vectors in the set -$D := \{ x \in \mathbb{R}^2 : x = \alpha \bar{x} \; \text{for some} \; \alpha >0 \}$ are also eigenvectors corresponding -to $r(A)$. - -This set $D$ is represented by a dashed line in the above figure. - -The graph illustrates that for two distinct initial conditions $x_0$ the sequences of iterates $(A^t x_0)_{t \geq 0}$ move towards $D$ over time. - -This suggests that all such sequences share strong similarities in the long run, determined by the dominant eigenvector $\bar{x}$. - -#### Negative growth rate - -In the example illustrated above we considered parameters such that overall growth rate of the labor force $g>0$. +由于 $\bar{x}$ 是对应于特征值 $r(A)$ 的特征向量,集合 +$D := \{ x \in \mathbb{R}^2 : x = \alpha \bar{x} \; \text{对某些} \; \alpha >0 \}$ 中的所有向量也都是对应于 $r(A)$ 的特征向量。 +这个集合 $D$ 在上图中由虚线表示。 +图中说明了对于两个不同的初始条件 $x_0$,迭代序列 $(A^t x_0)_{t \geq 0}$ 随时间向 $D$ 移动。 +这表明所有这样的序列在长期内都具有很强的相似性,由主特征向量 $\bar{x}$ 决定。 -Suppose now we are faced with a situation where the $g<0$, i.e., negative growth in the labor force. - -This means that $b-d<0$, i.e., workers exit the market faster than they enter. - -What would the behavior of the iterative sequence $x_{t+1} = Ax_t$ be now? - -This is visualised below. +#### 负增长率 +在上面说明的例子中,我们考虑的参数使得劳动力的总体增长率 $g>0$。 +现在假设我们面临 $g<0$ 的情况,即劳动力呈负增长。 +这意味着 $b-d<0$,即工人退出市场的速度快于进入市场的速度。 +现在迭代序列 $x_{t+1} = Ax_t$ 的行为会是什么样的? +这在下面进行了可视化。 ```{code-cell} ipython3 lm = LakeModel(α=0.01, λ=0.1, d=0.025, b=0.02) plot_time_paths(lm, x0=x0) ``` -Thus, while the sequence of iterates still moves towards the dominant eigenvector $\bar{x}$, in this case -they converge to the origin. - -This is a result of the fact that $r(A)<1$, which ensures that the iterative sequence $(A^t x_0)_{t \geq 0}$ will converge -to some point, in this case to $(0,0)$. - -This leads us to the next result. +因此,虽然迭代序列仍然朝着主特征向量 $\bar{x}$ 移动,但在这种情况下,它们收敛到原点。 +这是由于 $r(A)<1$ 的事实,这确保了迭代序列 $(A^t x_0)_{t \geq 0}$ 将收敛到某个点,在这种情况下是 $(0,0)$。 +这引导我们到下一个结果。 -### Properties +### 性质 -Since the column sums of $A$ are $r(A)=1$, the left eigenvector is $\mathbb{1}^\top=[1, 1]$. - -Perron-Frobenius theory implies that +由于 $A$ 的列和为 $r(A)=1$,左特征向量是 $\mathbb{1}^\top=[1, 1]$。 +佩龙-弗罗贝尼乌斯理论意味着 $$ r(A)^{-t} A^{t} \approx \bar{x} \mathbb{1}^\top = \begin{bmatrix} \bar{u} & \bar{u} \\ \bar{e} & \bar{e} \end{bmatrix}. $$ -As a result, for any $x_0 = (u_0, e_0)^\top$, we have +因此,对于任何 $x_0 = (u_0, e_0)^\top$,我们有 $$ \begin{aligned} @@ -425,44 +373,38 @@ x_t = A^t x_0 &\approx r(A)^t \begin{bmatrix} \bar{u} & \bar{u} \\ \bar{e} & \ba \end{aligned} $$ -as $t$ is large enough. - -We see that the growth of $u_t$ and $e_t$ also dominated by $r(A) = 1+g$ in the long run: $x_t$ grows along $D$ as $r(A) > 1$ and converges to $(0, 0)$ as $r(A) < 1$. - -Moreover, the long-run unemployment and employment are steady fractions of $n_t$. - -The latter implies that $\bar{u}$ and $\bar{e}$ are long-run unemployment rate and employment rate, respectively. +当 $t$ 足够大时。 -In detail, we have the unemployment rates and employment rates: $x_t / n_t = A^t n_0 / n_t \to \bar{x}$ as $t \to \infty$. +我们看到,在长期内,$u_t$ 和 $e_t$ 的增长也由 $r(A) = 1+g$ 主导:当 $r(A) > 1$ 时,$x_t$ 沿着 $D$ 增长,当 $r(A) < 1$ 时,收敛到 $(0, 0)$。 +此外,长期失业和就业是 $n_t$ 的稳定比例。 +后者意味着 $\bar{u}$ 和 $\bar{e}$ 分别是长期失业率和就业率。 +具体来说,我们有失业率和就业率:当 $t \to \infty$ 时,$x_t / n_t = A^t n_0 / n_t \to \bar{x}$。 -To illustrate the dynamics of the rates, let $\hat{A} := A / (1+g)$ be the transition matrix of $r_t := x_t/ n_t$. - -The dynamics of the rates follow +为了说明这些比率的动态,令 $\hat{A} := A / (1+g)$ 为 $r_t := x_t/ n_t$ 的转移矩阵。 +比率的动态遵循 $$ r_{t+1} = \frac{x_{t+1}}{n_{t+1}} = \frac{x_{t+1}}{(1+g) n_{t}} = \frac{A x_t}{(1+g)n_t} = \hat{A} \frac{x_t}{n_t} =\hat{A} r_t. $$ -Observe that the column sums of $\hat{A}$ are all one so that $r(\hat{A})=1$. - -One can check that $\bar{x}$ is also the right eigenvector of $\hat{A}$ corresponding to $r(\hat{A})$ that $\bar{x} = \hat{A} \bar{x}$. - -Moreover, $\hat{A}^t r_0 \to \bar{x}$ as $t \to \infty$ for any $r_0 = x_0 / n_0$, since the above discussion implies +注意到 $\hat{A}$ 的列和都为 1,因此 $r(\hat{A})=1$。 +可以验证 $\bar{x}$ 也是 $\hat{A}$ 对应于 $r(\hat{A})$ 的右特征向量,即 $\bar{x} = \hat{A} \bar{x}$。 +此外,对于任何 $r_0 = x_0 / n_0$,当 $t \to \infty$ 时,$\hat{A}^t r_0 \to \bar{x}$,因为上述讨论意味着 $$ r_t = \hat{A}^t r_0 = (1+g)^{-t} A^t r_0 = r(A)^{-t} A^t r_0 \to \begin{bmatrix} \bar{u} & \bar{u} \\ \bar{e} & \bar{e} \end{bmatrix} r_0 = \begin{bmatrix} \bar{u} \\ \bar{e} \end{bmatrix}. $$ -This is illustrated below. +这在下面有所说明。 ```{code-cell} ipython3 lm = LakeModel() -e_0 = 0.92 # Initial employment -u_0 = 1 - e_0 # Initial unemployment, given initial n_0 = 1 +e_0 = 0.92 # 初始就业 +u_0 = 1 - e_0 # 给定初始 n_0 = 1 的情况下的初始失业率 lm = LakeModel() -T = 100 # Simulation length +T = 100 # 模拟时长 x_0 = (u_0, e_0) @@ -472,14 +414,14 @@ rate_path = x_path / x_path.sum(0) fig, axes = plt.subplots(2, 1, figsize=(10, 8)) -# Plot steady ū and ē +# 绘制稳态 ū 和 ē axes[0].hlines(lm.ū, 0, T, 'r', '--', lw=2, label='ū') axes[1].hlines(lm.ē, 0, T, 'r', '--', lw=2, label='ē') -titles = ['Unemployment rate', 'Employment rate'] +titles = ['失业率', '就业率'] locations = ['lower right', 'upper right'] -# Plot unemployment rate and employment rate +# 绘制失业率和就业率 for i, ax in enumerate(axes): ax.plot(rate_path[i, :], lw=2, alpha=0.6) ax.set_title(titles[i]) @@ -491,48 +433,34 @@ plt.tight_layout() plt.show() ``` -To provide more intuition for convergence, we further explain the convergence below without the Perron-Frobenius theorem. - -Suppose that $\hat{A} = P D P^{-1}$ is diagonalizable, where $P = [v_1, v_2]$ consists of eigenvectors $v_1$ and $v_2$ of $\hat{A}$ -corresponding to eigenvalues $\gamma_1$ and $\gamma_2$ respectively, -and $D = \text{diag}(\gamma_1, \gamma_2)$. - -Let $\gamma_1 = r(\hat{A})=1$ and $|\gamma_2| < \gamma_1$, so that the spectral radius is a dominant eigenvalue. - -The dynamics of the rates follow $r_{t+1} = \hat{A} r_t$, where $r_0$ is a probability vector: $\sum_j r_{0,j}=1$. - -Consider $z_t = P^{-1} r_t $. - -Then, we have $z_{t+1} = P^{-1} r_{t+1} = P^{-1} \hat{A} r_t = P^{-1} \hat{A} P z_t = D z_t$. - -Hence, we obtain $z_t = D^t z_0$, and for some $z_0 = (c_1, c_2)^\top$ we have +为了更直观地理解收敛性,我们在下面不使用佩龙-弗罗贝尼乌斯定理进一步解释收敛过程。 +假设 $\hat{A} = P D P^{-1}$ 是可对角化的,其中 $P = [v_1, v_2]$ 由 $\hat{A}$ 的特征向量 $v_1$ 和 $v_2$ 组成, +分别对应于特征值 $\gamma_1$ 和 $\gamma_2$, +且 $D = \text{diag}(\gamma_1, \gamma_2)$。 +令 $\gamma_1 = r(\hat{A})=1$ 且 $|\gamma_2| < \gamma_1$,使得谱半径是一个主导特征值。 +比率的动态遵循 $r_{t+1} = \hat{A} r_t$,其中 $r_0$ 是一个概率向量:$\sum_j r_{0,j}=1$。 +考虑 $z_t = P^{-1} r_t $。 +那么,我们有 $z_{t+1} = P^{-1} r_{t+1} = P^{-1} \hat{A} r_t = P^{-1} \hat{A} P z_t = D z_t$。 +因此,我们得到 $z_t = D^t z_0$,对于某个 $z_0 = (c_1, c_2)^\top$,我们有 $$ r_t = P z_t = \begin{bmatrix} v_1 & v_2 \end{bmatrix} \begin{bmatrix} \gamma_1^t & 0 \\ 0 & \gamma_2^t \end{bmatrix} \begin{bmatrix} c_1 \\ c_2 \end{bmatrix} = c_1 \gamma_1^t v_1 + c_2 \gamma_2^t v_2. $$ +由于 $|\gamma_2| < |\gamma_1|=1$,右侧的第二项收敛到零。 +因此,收敛过程遵循 $r_t \to c_1 v_1$。 +由于 $\hat{A}$ 的列和为 1,且 $r_0$ 是一个概率向量,$r_t$ 必须是一个概率向量。 +在这种情况下,$c_1 v_1$ 必须是一个归一化的特征向量,所以 $c_1 v_1 = \bar{x}$,然后 $r_t \to \bar{x}$。 -Since $|\gamma_2| < |\gamma_1|=1$, the second term in the right hand side converges to zero. - -Therefore, the convergence follows $r_t \to c_1 v_1$. - -Since the column sums of $\hat{A}$ are one and $r_0$ is a probability vector, $r_t$ must be a probability vector. - -In this case, $c_1 v_1$ must be a normalized eigenvector, so $c_1 v_1 = \bar{x}$ and then $r_t \to \bar{x}$. - -## Exercise - -```{exercise-start} Evolution of unemployment and employment rate +## 练习 +```{exercise-start} 失业率和就业率的演化 :label: lake_model_ex1 ``` -How do the long-run unemployment rate and employment rate evolve if there is an increase in the separation rate $\alpha$ -or a decrease in job finding rate $\lambda$? - -Is the result compatible with your intuition? - -Plot the graph to illustrate how the line $D := \{ x \in \mathbb{R}^2 : x = \alpha \bar{x} \; \text{for some} \; \alpha >0 \}$ -shifts in the unemployment-employment space. +如果分离率 $\alpha$ 增加或求职率 $\lambda$ 下降,长期失业率和就业率会如何演变? +这个结果是否符合你的直觉? +绘制图表来说明直线 $D := \{ x \in \mathbb{R}^2 : x = \alpha \bar{x} \; \text{对某些} \; \alpha >0 \}$ +在失业-就业空间中如何移动。 ```{exercise-end} ``` @@ -541,14 +469,10 @@ shifts in the unemployment-employment space. :class: dropdown ``` -Eq. {eq}`steady_x` implies that the long-run unemployment rate will increase, and the employment rate will decrease -if $\alpha$ increases or $\lambda$ decreases. - -Suppose first that $\alpha=0.01, \lambda=0.1, d=0.02, b=0.025$. -Assume that $\alpha$ increases to $0.04$. - -The below graph illustrates that the line $D$ shifts clockwise downward, which indicates that -the fraction of unemployment rises as the separation rate increases. +方程 {eq}`steady_x` 表明,如果 $\alpha$ 增加或 $\lambda$ 减少,长期失业率将会上升,而就业率将会下降。 +首先假设 $\alpha=0.01, \lambda=0.1, d=0.02, b=0.025$。 +假设 $\alpha$ 增加到 $0.04$。 +下图说明了直线 $D$ 顺时针向下移动,这表明随着分离率的增加,失业人口的比例上升。 ```{code-cell} ipython3 fig, ax = plt.subplots(figsize=(10, 8)) diff --git a/lectures/linear_equations.md b/lectures/linear_equations.md index 4b9ccb0..fec1bf7 100644 --- a/lectures/linear_equations.md +++ b/lectures/linear_equations.md @@ -11,152 +11,144 @@ kernelspec: name: python3 --- -# Linear Equations and Matrix Algebra +# 线性方程和矩阵代数 ```{index} single: Linear Equations and Matrix Algebra ``` -## Overview +## 概述 -Many problems in economics and finance require solving linear equations. +经济学和金融学中的许多问题都需要解线性方程。 -In this lecture we discuss linear equations and their applications. +在本讲座中,我们将讨论线性方程及其应用。 -To illustrate the importance of linear equations, we begin with a two good -model of supply and demand. +为了说明线性方程的重要性,我们从一个两种商品的供需模型开始。 -The two good case is so simple that solutions can be calculated by hand. +两种商品的情况非常简单,可以手动计算解。 -But often we need to consider markets containing many goods. +但我们经常需要考虑包含多种商品的市场。 -In the multiple goods case we face large systems of linear equations, with many equations -and unknowns. +在多种商品的情况下,我们面对的是大型线性方程组,有许多方程和未知数。 -To handle such systems we need two things: +为了处理这样的系统,我们需要两样东西: -* matrix algebra (and the knowledge of how to use it) plus -* computer code to apply matrix algebra to the problems of interest. +* 矩阵代数(以及如何使用它的知识)以及 +* 将矩阵代数应用于感兴趣问题的计算机代码。 -This lecture covers these steps. +本讲座涵盖了这些步骤。 -We will use the following packages: +我们将使用以下的库: ```{code-cell} ipython3 import numpy as np import matplotlib.pyplot as plt ``` +## 两种商品的例子 -## A two good example +在本节中,我们将讨论一个简单的两种商品例子,并通过以下两种方法解决: -In this section we discuss a simple two good example and solve it by +1. 纸笔计算 +2. 矩阵代数 -1. pencil and paper -2. matrix algebra +正如我们将看到的,第二种方法更具普遍性。 -The second method is more general, as we will see. +### 纸笔计算方法 +假设我们有两种相关的商品,比如: -### Pencil and paper methods +* 丙烷和乙醇,或 +* 大米和小麦等。 -Suppose that we have two related goods, such as +为了简化问题,我们将它们标记为商品0和商品1。 -* propane and ethanol, and -* rice and wheat, etc. - -To keep things simple, we label them as good 0 and good 1. - -The demand for each good depends on the price of both goods: +每种商品的需求取决于两种商品的价格: ```{math} :label: two_eq_demand + \begin{aligned} q_0^d = 100 - 10 p_0 - 5 p_1 \\ q_1^d = 50 - p_0 - 10 p_1 \end{aligned} ``` -(We are assuming demand decreases when the price of either good goes up, but -other cases are also possible.) +(我们假设当任一商品的价格上涨时需求会下降,但其他情况也是可能的。) +让我们假设供给由以下方程给出: -Let's suppose that supply is given by ```{math} :label: two_eq_supply + \begin{aligned} q_0^s = 10 p_0 + 5 p_1 \\ q_1^s = 5 p_0 + 10 p_1 \end{aligned} ``` -Equilibrium holds when supply equals demand ($q_0^s = q_0^d$ and $q_1^s = q_1^d$). +当供给等于需求时($q_0^s = q_0^d$ 和 $q_1^s = q_1^d$),市场达到均衡。 -This yields the linear system +这产生了以下线性系统: ```{math} :label: two_equilibrium + \begin{aligned} 100 - 10 p_0 - 5 p_1 = 10 p_0 + 5 p_1 \\ 50 - p_0 - 10 p_1 = 5 p_0 + 10 p_1 \end{aligned} ``` -We can solve this with pencil and paper to get +我们可以用纸笔计算得到: $$ - p_0 = 4.41 \quad \text{and} \quad p_1 = 1.18. + p_0 = 4.41 \quad \text{和} \quad p_1 = 1.18. $$ -Inserting these results into either {eq}`two_eq_demand` or {eq}`two_eq_supply` yields the -equilibrium quantities +将这些结果代入{eq}`two_eq_demand`或{eq}`two_eq_supply`中,可得均衡数量: $$ - q_0 = 50 \quad \text{and} \quad q_1 = 33.82. + q_0 = 50 \quad \text{和} \quad q_1 = 33.82. $$ +### 展望未来 -### Looking forward - -Pencil and paper methods are easy in the two good case. - -But what if there are many goods? +在两种商品的情况下,纸笔计算方法很容易。 -For such problems we need matrix algebra. +但如果有很多种商品呢? -Before solving problems with matrix algebra, let's first recall the -basics of vectors and matrices, in both theory and computation. +对于这样的问题,我们需要矩阵代数。 +在用矩阵代数解决问题之前,让我们先回顾一下向量和矩阵的基础知识,包括理论和计算。 -## {index}`Vectors ` +## {index}`向量 ` ```{index} single: Linear Algebra; Vectors ``` -A **vector** of length $n$ is just a sequence (or array, or tuple) of $n$ numbers, which we write as $x = (x_1, \ldots, x_n)$ or $x = \begin{bmatrix}x_1, \ldots, x_n\end{bmatrix}$. +一个长度为$n$的**向量**就是一个由$n$个数字组成的序列(或数组,或元组),我们将其写作$x = (x_1, \ldots, x_n)$或$x = \begin{bmatrix}x_1, \ldots, x_n\end{bmatrix}$。 -We can write these sequences either horizontally or vertically. +我们可以将这些序列横向或纵向写出。 -But when we use matrix operations, our default assumption is that vectors are -column vectors. +但当我们使用矩阵运算时,我们默认假设向量是列向量。 -The set of all $n$-vectors is denoted by $\mathbb R^n$. +所有$n$维向量的集合用$\mathbb R^n$表示。 ```{prf:example} :label: le_ex_dim -* $\mathbb R^2$ is the plane --- the set of pairs $(x_1, x_2)$. -* $\mathbb R^3$ is 3 dimensional space --- the set of vectors $(x_1, x_2, x_3)$. +* $\mathbb R^2$是平面 --- 即所有$(x_1, x_2)$对的集合。 +* $\mathbb R^3$是三维空间 --- 即所有$(x_1, x_2, x_3)$向量的集合。 +向量通常在视觉上表示为从原点到某点的箭头。 ``` -Often vectors are represented visually as arrows from the origin to the point. - -Here's a visualization. +这里是一个可视化示例。 ```{code-cell} ipython3 :tags: [hide-input] fig, ax = plt.subplots() -# Set the axes through the origin +# 通过原点建立坐标轴 for spine in ['left', 'bottom']: ax.spines[spine].set_position('zero') for spine in ['right', 'top']: @@ -175,17 +167,16 @@ for v in vecs: plt.show() ``` -### Vector operations +### 向量运算 ```{index} single: Vectors; Operations ``` -Sometimes we want to modify vectors. +有时我们需要修改向量。 -The two most common operators on vectors are addition and scalar -multiplication, which we now describe. +对向量最常见的两种运算是加法和标量乘法,我们现在来描述这两种运算。 -When we add two vectors, we add them element-by-element. +当我们对两个向量进行加法运算时,我们是逐元素相加。 ```{prf:example} :label: le_ex_add @@ -213,7 +204,7 @@ $$ $$ ``` -In general, +一般来说, $$ x + y = @@ -237,13 +228,13 @@ x + y = \end{bmatrix}. $$ -We can visualise vector addition in $\mathbb{R}^2$ as follows. +我们可以在$\mathbb{R}^2$中将向量加法可视化如下。 ```{code-cell} ipython3 :tags: [hide-input] fig, ax = plt.subplots() -# Set the axes through the origin +# 通过原点建立坐标轴 for spine in ['left', 'bottom']: ax.spines[spine].set_position('zero') for spine in ['right', 'top']: @@ -275,7 +266,7 @@ for i, v in enumerate(vecs): plt.show() ``` -Scalar multiplication is an operation that multiplies a vector $x$ with a scalar elementwise. +标量乘法是一种将向量 $x$ 与一个标量进行元素级别相乘的运算。 ```{prf:example} :label: le_ex_mul @@ -299,7 +290,7 @@ $$ $$ ``` -More generally, it takes a number $\gamma$ and a vector $x$ and produces +更一般地,它取一个数 $\gamma$ 和一个向量 $x$,得到 $$ \gamma x := @@ -311,13 +302,13 @@ $$ \end{bmatrix}. $$ -Scalar multiplication is illustrated in the next figure. +标量乘法在下图中进行了说明。 ```{code-cell} ipython3 :tags: [hide-input] fig, ax = plt.subplots() -# Set the axes through the origin +# 通过原点建立坐标轴 for spine in ['left', 'bottom']: ax.spines[spine].set_position('zero') for spine in ['right', 'top']: @@ -346,26 +337,25 @@ for s in scalars: plt.show() ``` -In Python, a vector can be represented as a list or tuple, -such as `x = [2, 4, 6]` or `x = (2, 4, 6)`. +在Python中,向量可以用列表或元组表示, +例如 `x = [2, 4, 6]` 或 `x = (2, 4, 6]`。 -However, it is more common to represent vectors with -[NumPy arrays](https://python-programming.quantecon.org/numpy.html#numpy-arrays). +然而,更常见的是用 +[NumPy数组](https://python-programming.quantecon.org/numpy.html#numpy-arrays)来表示向量。 -One advantage of NumPy arrays is that scalar multiplication and addition have -very natural syntax. +NumPy数组的一个优点是标量乘法和加法具有非常自然的语法。 ```{code-cell} ipython3 -x = np.ones(3) # Vector of three ones -y = np.array((2, 4, 6)) # Converts tuple (2, 4, 6) into a NumPy array -x + y # Add (element-by-element) +x = np.ones(3) # 三个元素为一的向量 +y = np.array((2, 4, 6)) # 将 (2, 4, 6) 转换为 NumPy 数组 +x + y # 每个元素相加 ``` ```{code-cell} -4 * x # Scalar multiply +4 * x # 标量乘法 ``` -### Inner product and norm +### 内积和范数 ```{index} single: Vectors; Inner Product ``` @@ -373,7 +363,7 @@ x + y # Add (element-by-element) ```{index} single: Vectors; Norm ``` -The **inner product** of vectors $x,y \in \mathbb R^n$ is defined as +向量 $x,y \in \mathbb R^n$ 的**内积**定义为 $$ x^\top y = @@ -390,50 +380,48 @@ x^\top y = := \sum_{i=1}^n x_i y_i. $$ -The **norm** of a vector $x$ represents its "length" (i.e., its distance from -the zero vector) and is defined as +向量 $x$ 的**范数**表示其"长度"(即,其与零向量的距离),定义为 $$ \| x \| := \sqrt{x^\top x} := \left( \sum_{i=1}^n x_i^2 \right)^{1/2}. $$ -The expression $\| x - y\|$ can be thought of as the "distance" between $x$ and $y$. +表达式 $\| x - y\|$ 可以被理解为 $x$ 和 $y$ 之间的"距离"。 -The inner product and norm can be computed as follows +内积和范数可以按以下方式计算 ```{code-cell} ipython3 -np.sum(x*y) # Inner product of x and y +np.sum(x*y) # x和y的内积 ``` ```{code-cell} ipython3 -x @ y # Another way to compute the inner product +x @ y # 另外一种计算内积的办法 ``` ```{code-cell} ipython3 -np.sqrt(np.sum(x**2)) # Norm of x, method one +np.sqrt(np.sum(x**2)) # x的范数,方法一 ``` ```{code-cell} ipython3 -np.linalg.norm(x) # Norm of x, method two +np.linalg.norm(x) # x的范数,方法二 ``` -## Matrix operations +## 矩阵运算 ```{index} single: Matrix; Operations ``` -When we discussed linear price systems, we mentioned using matrix algebra. +当我们讨论线性价格系统时,我们提到了使用矩阵代数。 -Matrix algebra is similar to algebra for numbers. +矩阵代数类似于数字代数。 -Let's review some details. +让我们回顾一些细节。 -### Addition and scalar multiplication +### 加法和标量乘法 -Just as was the case for vectors, we can add, subtract and scalar multiply -matrices. +就像向量一样,我们可以对矩阵进行加法、减法和标量乘法。 -Scalar multiplication and addition are generalizations of the vector case: +标量乘法和加法是向量情况的推广: ```{prf:example} :label: le_ex_asm @@ -452,7 +440,7 @@ $$ $$ ``` -In general for a number $\gamma$ and any matrix $A$, +一般来说,对于任意数 $\gamma$ 和任意矩阵 $A$, $$ \gamma A = @@ -472,8 +460,7 @@ $$ ```{prf:example} :label: le_ex_ma -Consider this example of matrix addition, - +考虑这个矩阵加法的例子, $$ \begin{bmatrix} 1 & 5 \\ @@ -492,7 +479,7 @@ $$ $$ ``` -In general, +一般来说, $$ A + B = @@ -513,27 +500,22 @@ A + B = \end{bmatrix}. $$ -In the latter case, the matrices must have the same shape in order for the -definition to make sense. +在后一种情况下,矩阵必须具有相同的形状才能使定义有意义。 -### Matrix multiplication +### 矩阵乘法 -We also have a convention for *multiplying* two matrices. +我们还有一个*相乘*两个矩阵的约定。 -The rule for matrix multiplication generalizes the idea of inner products -discussed above. +矩阵乘法的规则推广了上面讨论的内积的概念。 -If $A$ and $B$ are two matrices, then their product $A B$ is formed by taking -as its $i,j$-th element the inner product of the $i$-th row of $A$ and the -$j$-th column of $B$. +如果 $A$ 和 $B$ 是两个矩阵,那么它们的乘积 $A B$ 的形成是通过取 $A$ 的第 $i$ 行和 $B$ 的第 $j$ 列的内积作为其第 $i,j$ 个元素。 -If $A$ is $n \times k$ and $B$ is $j \times m$, then to multiply $A$ and $B$ -we require $k = j$, and the resulting matrix $A B$ is $n \times m$. +如果 $A$ 是 $n \times k$ 的,$B$ 是 $j \times m$ 的,那么要相乘 $A$ 和 $B$,我们需要 $k = j$,而得到的矩阵 $A B$ 是 $n \times m$ 的。 ```{prf:example} :label: le_ex_2dmul -Here's an example of a $2 \times 2$ matrix multiplied by a $2 \times 1$ vector. +这里是一个 $2 \times 2$ 矩阵乘以 $2 \times 1$ 向量的例子。 $$ Ax = @@ -553,10 +535,9 @@ Ax = $$ ``` -As an important special case, consider multiplying $n \times k$ -matrix $A$ and $k \times 1$ column vector $x$. +作为一个重要的特殊情况,考虑将 $n \times k$ 矩阵 $A$ 和 $k \times 1$ 列向量 $x$ 相乘。 -According to the preceding rule, this gives us an $n \times 1$ column vector. +根据前面的规则,这给我们一个 $n \times 1$ 列向量。 ```{math} :label: la_atx @@ -585,7 +566,7 @@ A x = \end{bmatrix}}_{n \times 1} ``` -Here is a simple illustration of multiplication of two matrices. +下面展示了两个矩阵的乘法。 $$ AB = @@ -603,17 +584,16 @@ AB = \end{bmatrix} $$ -There are many tutorials to help you further visualize this operation, such as - -* [this one](http://www.mathsisfun.com/algebra/matrix-multiplying.html), or -* the discussion on the [Wikipedia page](https://en.wikipedia.org/wiki/Matrix_multiplication). +有许多教程可以帮助你进一步可视化这个操作,例如 +* [这个教程](http://www.mathsisfun.com/algebra/matrix-multiplying.html),或者 +* [维基百科页面](https://en.wikipedia.org/wiki/Matrix_multiplication)上的讨论。 ```{note} -Unlike number products, $A B$ and $B A$ are not generally the same thing. +与数字乘积不同,$A B$ 和 $B A$ 通常不是同一件事。 ``` -One important special case is the [identity matrix](https://en.wikipedia.org/wiki/Identity_matrix), which has ones on the principal diagonal and zero elsewhere: +一个重要的特殊情况是[单位矩阵](https://en.wikipedia.org/wiki/Identity_matrix),它在主对角线上有 1,其他地方都是 0: $$ I = @@ -624,21 +604,19 @@ $$ \end{bmatrix} $$ -It is a useful exercise to check the following: - -* if $A$ is $n \times k$ and $I$ is the $k \times k$ identity matrix, then $AI = A$, and -* if $I$ is the $n \times n$ identity matrix, then $IA = A$. +验证以下内容是一个有用的练习: +* 如果 $A$ 是 $n \times k$ 矩阵,$I$ 是 $k \times k$ 单位矩阵,那么 $AI = A$,并且 +* 如果 $I$ 是 $n \times n$ 单位矩阵,那么 $IA = A$。 -### Matrices in NumPy +### NumPy中的矩阵 ```{index} single: Matrix; Numpy ``` +NumPy 数组也被用作矩阵,并且对所有标准矩阵运算都有快速、高效的函数和方法。 -NumPy arrays are also used as matrices, and have fast, efficient functions and methods for all the standard matrix operations. - -You can create them manually from tuples of tuples (or lists of lists) as follows +你可以通过以下方式从元组的元组(或列表的列表)手动创建它们 ```{code-cell} ipython3 A = ((1, 2), @@ -657,20 +635,18 @@ type(A) A.shape ``` -The `shape` attribute is a tuple giving the number of rows and columns --- -see [here](https://python-programming.quantecon.org/numpy.html#shape-and-dimension) -for more discussion. +`shape` 属性是一个给出行数和列数的元组 --- +更多讨论请参见[这里](https://python-programming.quantecon.org/numpy.html#shape-and-dimension)。 -To get the transpose of `A`, use `A.transpose()` or, more simply, `A.T`. +要获得 `A` 的转置,使用 `A.transpose()` 或更简单地使用 `A.T`。 -There are many convenient functions for creating common matrices (matrices of zeros, -ones, etc.) --- see [here](https://python-programming.quantecon.org/numpy.html#creating-arrays). +有许多方便的函数用于创建常见矩阵(零矩阵、单位矩阵等) --- 请参见[这里](https://python-programming.quantecon.org/numpy.html#creating-arrays)。 -Since operations are performed elementwise by default, scalar multiplication and addition have very natural syntax. +由于默认情况下操作是按元素执行的,标量乘法和加法具有非常自然的语法。 ```{code-cell} ipython3 -A = np.identity(3) # 3 x 3 identity matrix -B = np.ones((3, 3)) # 3 x 3 matrix of ones +A = np.identity(3) # 3 x 3 单位矩阵 +B = np.ones((3, 3)) # 3 x 3 元素为一的矩阵 2 * A ``` @@ -678,25 +654,23 @@ B = np.ones((3, 3)) # 3 x 3 matrix of ones A + B ``` -To multiply matrices we use the `@` symbol. +我们用 `@` 来进行矩阵乘法。 ```{note} -In particular, `A @ B` is matrix multiplication, whereas `A * B` is element-by-element multiplication. +其中 `A @ B` 是矩阵乘法, 但是 `A * B`是每个元素之间的运算。 ``` -### Two good model in matrix form - -We can now revisit the two good model and solve {eq}`two_equilibrium` -numerically via matrix algebra. +### 矩阵形式的两种商品模型 -This involves some extra steps but the method is widely applicable --- as we -will see when we include more goods. +我们现在可以重新审视两种商品模型,并通过矩阵代数数值求解 {eq}`two_equilibrium` 方程。 -First we rewrite {eq}`two_eq_demand` as +这涉及一些额外的步骤,但这种方法广泛适用 --- 正如我们在包含更多商品时将看到的那样。 +首先,我们将 {eq}`two_eq_demand` 重写为 ```{math} :label: two_eq_demand_mat + q^d = D p + h \quad \text{where} \quad q^d = @@ -718,14 +692,15 @@ First we rewrite {eq}`two_eq_demand` as \end{bmatrix}. ``` -Recall that $p \in \mathbb{R}^{2}$ is the price of two goods. +回想一下,$p \in \mathbb{R}^{2}$ 是两种商品的价格。 -(Please check that $q^d = D p + h$ represents the same equations as {eq}`two_eq_demand`.) +(请检查 $q^d = D p + h$ 是否表示与 {eq}`two_eq_demand` 相同的方程。) -We rewrite {eq}`two_eq_supply` as +我们将 {eq}`two_eq_supply` 重写为 ```{math} :label: two_eq_supply_mat + q^s = C p \quad \text{where} \quad q^s = @@ -741,76 +716,71 @@ We rewrite {eq}`two_eq_supply` as \end{bmatrix}. ``` -Now equality of supply and demand can be expressed as $q^s = q^d$, or +现在供给和需求的相等可以表示为 $q^s = q^d$,或 $$ C p = D p + h. $$ -We can rearrange the terms to get +我们可以重新排列这些项得到 $$ (C - D) p = h. $$ -If all of the terms were numbers, we could solve for prices as $p = h / -(C-D)$. - -Matrix algebra allows us to do something similar: we can solve for equilibrium -prices using the inverse of $C - D$: +如果所有项都是数字,我们可以求解价格为 $p = h / (C-D)$。 +矩阵代数允许我们做类似的事情:我们可以使用 $C - D$ 的逆矩阵来求解均衡价格: ```{math} :label: two_matrix - p = (C - D)^{-1} h. -``` - -Before we implement the solution let us consider a more general setting. +p = (C - D)^{-1} h. +``` +在我们实施解决方案之前,让我们考虑一个更一般的设置。 -### More goods +### 更多商品 -It is natural to think about demand systems with more goods. +考虑有更多商品的需求系统是很自然的。 -For example, even within energy commodities there are many different goods, -including crude oil, gasoline, coal, natural gas, ethanol, and uranium. +例如,即使在能源商品中也有许多不同的商品, +包括原油、汽油、煤炭、天然气、乙醇和铀。 -The prices of these goods are related, so it makes sense to study them -together. +这些商品的价格是相关的,所以一起研究它们是有意义的。 -Pencil and paper methods become very time consuming with large systems. +对于大型系统,纸笔方法会变得非常耗时。 -But fortunately the matrix methods described above are essentially unchanged. +但幸运的是,上面描述的矩阵方法基本上保持不变。 -In general, we can write the demand equation as $q^d = Dp + h$, where +一般来说,我们可以将需求方程写为 $q^d = Dp + h$,其中 -* $q^d$ is an $n \times 1$ vector of demand quantities for $n$ different goods. -* $D$ is an $n \times n$ "coefficient" matrix. -* $h$ is an $n \times 1$ vector of constant values. +* $q^d$ 是一个 $n \times 1$ 的向量,表示 $n$ 种不同商品的需求量。 +* $D$ 是一个 $n \times n$ 的"系数"矩阵。 +* $h$ 是一个 $n \times 1$ 的常数值向量。 -Similarly, we can write the supply equation as $q^s = Cp + e$, where +类似地,我们可以将供给方程写为 $q^s = Cp + e$,其中 -* $q^s$ is an $n \times 1$ vector of supply quantities for the same goods. -* $C$ is an $n \times n$ "coefficient" matrix. -* $e$ is an $n \times 1$ vector of constant values. +* $q^s$ 是一个 $n \times 1$ 的向量,表示相同商品的供给量。 +* $C$ 是一个 $n \times n$ 的"系数"矩阵。 +* $e$ 是一个 $n \times 1$ 的常数值向量。 -To find an equilibrium, we solve $Dp + h = Cp + e$, or +为了找到均衡,我们求解 $Dp + h = Cp + e$,或 ```{math} :label: n_eq_sys_la + (D- C)p = e - h. ``` -Then the price vector of the n different goods is +那么,n 种不同商品的价格向量是 $$ p = (D- C)^{-1}(e - h). $$ +### 一般线性系统 -### General linear systems - -A more general version of the problem described above looks as follows. +上述问题的一个更一般版本看起来如下。 ```{math} :label: la_se @@ -822,22 +792,21 @@ A more general version of the problem described above looks as follows. \end{matrix} ``` -The objective here is to solve for the "unknowns" $x_1, \ldots, x_n$. +这里的目标是解出"未知数" $x_1, \ldots, x_n$。 -We take as given the coefficients $a_{11}, \ldots, a_{nn}$ and constants $b_1, \ldots, b_n$. +我们给定系数 $a_{11}, \ldots, a_{nn}$ 和常数 $b_1, \ldots, b_n$。 -Notice that we are treating a setting where the number of unknowns equals the -number of equations. +注意,我们处理的是未知数数量等于方程数量的情况。 -This is the case where we are most likely to find a well-defined solution. +这是我们最有可能找到明确定义解的情况。 -(The other cases are referred to as [overdetermined](https://en.wikipedia.org/wiki/Overdetermined_system) and [underdetermined](https://en.wikipedia.org/wiki/Underdetermined_system) systems -of equations --- we defer discussion of these cases until later lectures.) +(其他情况被称为[超定](https://en.wikipedia.org/wiki/Overdetermined_system)和[欠定](https://en.wikipedia.org/wiki/Underdetermined_system)方程组 --- 我们将在后续讲座中讨论这些情况。) -In matrix form, the system {eq}`la_se` becomes +用矩阵形式表示,方程组 {eq}`la_se` 变为 ```{math} :label: la_gf + A x = b \quad \text{where} \quad A = @@ -854,50 +823,43 @@ In matrix form, the system {eq}`la_se` becomes b_n \end{bmatrix}. ``` +例如,{eq}`n_eq_sys_la` 具有这种形式,其中 -```{prf:example} -:label: le_ex_gls -For example, {eq}`n_eq_sys_la` has this form with - -$$ - A = D - C, +$$ + A = D - C, \quad b = e - h - \quad \text{and} \quad + \quad \text{和} \quad x = p. $$ -``` - -When considering problems such as {eq}`la_gf`, we need to ask at least some of -the following questions -* Does a solution actually exist? -* If a solution exists, how should we compute it? +当考虑诸如 {eq}`la_gf` 这样的问题时,我们至少需要问以下一些问题: +* 解是否真的存在? +* 如果解存在,我们应该如何计算它? - -## Solving systems of equations +## 解方程组 ```{index} single: Matrix; Solving Systems of Equations ``` -Recall again the system of equations {eq}`la_se`, which we write here again as +再次回顾方程组 {eq}`la_se`,我们在此重新写为 ```{math} :label: la_se2 - A x = b. + + A x = b ``` -The problem we face is to find a vector $x \in \mathbb R^n$ that solves -{eq}`la_se2`, taking $b$ and $A$ as given. +我们面临的问题是找到一个向量 $x \in \mathbb R^n$,使其解决 {eq}`la_se2` ,其中 $b$ 和 $A$ 是给定的。 -We may not always find a unique vector $x$ that solves {eq}`la_se2`. +我们可能并不总能找到一个唯一的向量 $x$ 来解决 {eq}`la_se2` 。 -We illustrate two such cases below. +我们在下面举例说明两种这样的情况。 -### No solution +### 无解 -Consider the system of equations given by, +考虑由以下给出的方程组: $$ \begin{aligned} @@ -906,9 +868,9 @@ $$ \end{aligned} $$ -It can be verified manually that this system has no possible solution. +可以手动验证这个系统没有可能的解。 -To illustrate why this situation arises let's plot the two lines. +为了说明为什么会出现这种情况,让我们绘制这两条直线。 ```{code-cell} ipython3 fig, ax = plt.subplots() @@ -921,15 +883,16 @@ plt.show() +++ {"tags": []} -Clearly, these are parallel lines and hence we will never find a point $x \in \mathbb{R}^2$ -such that these lines intersect. -Thus, this system has no possible solution. +显然,这些是平行线,因此我们永远无法找到一个点 $x \in \mathbb{R}^2$ 使得这些线相交。 + +因此,这个系统没有可能的解。 -We can rewrite this system in matrix form as +我们可以将这个系统用矩阵形式重写为 ```{math} :label: no_soln + A x = b \quad \text{where} \quad A = @@ -945,25 +908,21 @@ We can rewrite this system in matrix form as \end{bmatrix}. ``` -It can be noted that the $2^{nd}$ row of matrix $A = (2, 6)$ is just a scalar multiple of the $1^{st}$ row of matrix $A = (1, 3)$. - -The rows of matrix $A$ in this case are called **linearly dependent.** - +可以注意到,矩阵 $A$ 的第 $2$ 行 $(2, 6)$ 只是第 $1$ 行 $(1, 3)$ 的标量倍数。 +在这种情况下,矩阵 $A$ 的行被称为**线性相关的。** ```{note} -Advanced readers can find a detailed explanation of linear dependence and -independence [here](https://python.quantecon.org/linear_algebra.html#linear-independence). +高级读者可以在[这里](https://python.quantecon.org/linear_algebra.html#linear-independence)找到关于线性相关和线性无关的详细解释。 -But these details are not needed in what follows. +但在接下来的内容中不需要这些细节。 ``` +### 多解 -### Many solutions - -Now consider, +现在考虑, $$ \begin{aligned} @@ -972,14 +931,15 @@ $$ \end{aligned} $$ -Any vector $v = (x,y)$ such that $x = 2y - 4$ will solve the above system. +任何满足 $x = 2y - 4$ 的向量 $v = (x,y)$ 都将解决上述系统。 -Since we can find infinite such vectors this system has infinitely many solutions. +由于我们可以找到无限多个这样的向量,这个系统有无穷多个解。 -This is because the rows of the corresponding matrix +这是因为对应矩阵的行 ```{math} :label: many_solns + A = \begin{bmatrix} 1 & -2 \\ @@ -987,17 +947,15 @@ This is because the rows of the corresponding matrix \end{bmatrix}. ``` -are linearly dependent --- can you see why? +是线性相关的 --- 你能看出为什么吗? -We now impose conditions on $A$ in {eq}`la_se2` that rule out these problems. +我们现在对 {eq}`la_se2` 中的 $A$ 施加条件,以排除这些问题。 +### 非奇异矩阵 -### Nonsingular matrices +对于每个方阵,我们都可以指定一个唯一的数,称为[行列式](https://en.wikipedia.org/wiki/Determinant)。 -To every square matrix we can assign a unique number called the -[determinant](https://en.wikipedia.org/wiki/Determinant). - -For $2 \times 2$ matrices, the determinant is given by, +对于 $2 \times 2$ 矩阵,行列式由以下公式给出: $$ \begin{bmatrix} @@ -1008,56 +966,49 @@ $$ {\color{red}{ad}} - {\color{blue}{bc}}. $$ -If the determinant of $A$ is not zero, then we say that $A$ is *nonsingular*. +如果 $A$ 的行列式不为零,我们就说 $A$ 是*非奇异的*。 -A square matrix $A$ is nonsingular if and only if the rows and columns of $A$ -are linearly independent. +当且仅当 $A$ 的行和列是线性无关的,方阵 $A$ 才是非奇异的。 -A more detailed explanation of matrix inverse can be found [here](https://www.mathsisfun.com/algebra/matrix-inverse.html). +关于矩阵逆的更详细解释可以在[这里](https://www.mathsisfun.com/algebra/matrix-inverse.html)找到。 -You can check yourself that the in {eq}`no_soln` and {eq}`many_solns` with -linearly dependent rows are singular matrices. +你可以自己检查 {eq}`no_soln` 和 {eq}`many_solns` 中具有线性相关行的矩阵是奇异矩阵。 -This gives us a useful one-number summary of whether or not a square matrix -can be inverted. +这为我们提供了一个有用的单数值概括,用来判断一个方阵是否可逆。 -In particular, a square matrix $A$ has a nonzero determinant, if and only if -it possesses an *inverse matrix* $A^{-1}$, with the property that $A A^{-1} = -A^{-1} A = I$. +特别地,方阵 $A$ 具有非零行列式,当且仅当它具有*逆矩阵* $A^{-1}$,满足 $A A^{-1} = A^{-1} A = I$。 -As a consequence, if we pre-multiply both sides of $Ax = b$ by $A^{-1}$, we -get +因此,如果我们用 $A^{-1}$ 左乘 $Ax = b$ 的两边,我们得到 ```{math} :label: la_se_inv + x = A^{-1} b. ``` -This is the solution to $Ax = b$ --- the solution we are looking for. - +这是对 $Ax = b$ 的解答 --- 这就是我们要寻找的解。 - -### Linear equations with NumPy +### 使用NumPy求解线性方程 ```{index} single: Linear Algebra; SciPy ``` -In the two good example we obtained the matrix equation, +在两个好的例子中,我们得到了矩阵方程: $$ p = (C-D)^{-1} h. $$ -where $C$, $D$ and $h$ are given by {eq}`two_eq_demand_mat` and {eq}`two_eq_supply_mat`. +其中 $C$、$D$ 和 $h$ 由 {eq}`two_eq_demand_mat` 和 {eq}`two_eq_supply_mat` 给出。 -This equation is analogous to {eq}`la_se_inv` with $A = (C-D)^{-1}$, $b = h$, and $x = p$. +这个方程类似于 {eq}`la_se_inv`,其中 $A = (C-D)^{-1}$,$b = h$,且 $x = p$。 -We can now solve for equilibrium prices with NumPy's `linalg` submodule. +我们现在可以使用NumPy的`linalg`子模块求解均衡价格。 -All of these routines are Python front ends to time-tested and highly optimized FORTRAN code. +所有这些程序都是经过时间检验和高度优化的FORTRAN代码的Python前端。 ```{code-cell} ipython3 -C = ((10, 5), # Matrix C +C = ((10, 5), # 矩阵 C (5, 10)) ``` @@ -1068,68 +1019,64 @@ C = np.array(C) ``` ```{code-cell} ipython3 -D = ((-10, -5), # Matrix D +D = ((-10, -5), # 矩阵 D (-1, -10)) D = np.array(D) ``` ```{code-cell} ipython3 -h = np.array((100, 50)) # Vector h -h.shape = 2,1 # Transforming h to a column vector +h = np.array((100, 50)) # 向量 h +h.shape = 2,1 # 将h转换为列向量 ``` ```{code-cell} ipython3 from numpy.linalg import det, inv A = C - D -# Check that A is nonsingular (non-zero determinant), and hence invertible +#检查A是否为奇异矩阵(行列式是否为零),是否可逆 det(A) ``` ```{code-cell} ipython3 -A_inv = inv(A) # compute the inverse +A_inv = inv(A) #计算逆矩阵 A_inv ``` ```{code-cell} ipython3 -p = A_inv @ h # equilibrium prices +p = A_inv @ h #均衡价格 p ``` ```{code-cell} ipython3 -q = C @ p # equilibrium quantities +q = C @ p # 均衡数量 q ``` -Notice that we get the same solutions as the pencil and paper case. +注意,我们得到的解与纸笔计算的情况相同。 -We can also solve for $p$ using `solve(A, h)` as follows. +我们还可以使用 `solve(A, h)` 来求解 $p$,如下所示。 ```{code-cell} ipython3 from numpy.linalg import solve -p = solve(A, h) # equilibrium prices +p = solve(A, h) # 均衡价格 p ``` ```{code-cell} ipython3 -q = C @ p # equilibrium quantities +q = C @ p # 均衡数量 q ``` +观察我们如何通过 `inv(A) @ y` 或使用 `solve(A, y)` 来求解 $x = A^{-1} y$。 -Observe how we can solve for $x = A^{-1} y$ by either via `inv(A) @ y`, or using `solve(A, y)`. - -The latter method uses a different algorithm that is numerically more stable and hence should be the default option. - - +后一种方法使用了一种不同的算法,在数值上更加稳定,因此应该是默认选项。 -## Exercises +## 练习 ```{exercise-start} :label: lin_eqs_ex1 ``` +让我们考虑一个有3种商品的市场 - 商品0、商品1和商品2。 -Let's consider a market with 3 commodities - good 0, good 1 and good 2. - -The demand for each good depends on the price of the other two goods and is given by: +每种商品的需求取决于其他两种商品的价格,由以下公式给出: $$ \begin{aligned} @@ -1139,9 +1086,9 @@ $$ \end{aligned} $$ -(Here demand decreases when own price increases but increases when prices of other goods increase.) +(这里,当自身价格上涨时需求下降,但当其他商品价格上涨时需求增加。) -The supply of each good is given by: +每种商品的供给由以下公式给出: $$ \begin{aligned} @@ -1151,19 +1098,17 @@ $$ \end{aligned} $$ -Equilibrium holds when supply equals demand, i.e, $q_0^d = q_0^s$, $q_1^d = q_1^s$ and $q_2^d = q_2^s$. +当供给等于需求时,市场达到均衡,即 $q_0^d = q_0^s$,$q_1^d = q_1^s$ 和 $q_2^d = q_2^s$。 -1. Set up the market as a system of linear equations. -2. Use matrix algebra to solve for equilibrium prices. Do this using both the `numpy.linalg.solve` - and `inv(A)` methods. Compare the solutions. +1. 将市场设置为线性方程组。 +2. 使用矩阵代数求解均衡价格。分别使用 `numpy.linalg.solve` 和 `inv(A)` 方法来做这个。比较这两种解法。 ```{exercise-end} ``` ```{solution-start} lin_eqs_ex1 :class: dropdown ``` - -The generated system would be: +生成的系统将是: $$ \begin{aligned} @@ -1173,11 +1118,11 @@ $$ \end{aligned} $$ -In matrix form we will write this as: +用矩阵形式,我们将其表示为: $$ Ap = b -\quad \text{where} \quad +\quad \text{其中} \quad A = \begin{bmatrix} 35 & -5 & -5 \\ @@ -1190,7 +1135,7 @@ A = p_1 \\ p_2 \end{bmatrix} -\quad \text{and} \quad +\quad \text{且} \quad b = \begin{bmatrix} 100 \\ @@ -1203,18 +1148,18 @@ $$ import numpy as np from numpy.linalg import det -A = np.array([[35, -5, -5], # matrix A +A = np.array([[35, -5, -5], # 矩阵 A [-5, 25, -10], [-5, -5, 15]]) -b = np.array((100, 75, 55)) # column vector b +b = np.array((100, 75, 55)) # 列向量 b b.shape = (3, 1) -det(A) # check if A is nonsingular +det(A) # 检查A是否为奇异矩阵 ``` ```{code-cell} -# Using inverse +# 使用inverse from numpy.linalg import det A_inv = inv(A) @@ -1224,13 +1169,14 @@ p ``` ```{code-cell} -# Using numpy.linalg.solve +# 使用 numpy.linalg.solve from numpy.linalg import solve p = solve(A, b) p ``` -The solution is given by: +答案为: + $$ p_0 = 4.6925, \; p_1 = 7.0625 \;\; \text{and} \;\; p_2 = 7.675 $$ @@ -1241,64 +1187,61 @@ $$ ```{exercise-start} :label: lin_eqs_ex2 ``` -Earlier in the lecture we discussed cases where the system of equations given by $Ax = b$ has no solution. +在讲座的早些时候,我们讨论了$Ax = b$这个方程组没有解的情况。 -In this case $Ax = b$ is called an _inconsistent_ system of equations. +在这种情况下,$Ax = b$被称为*不相容*方程组。 -When faced with an inconsistent system we try to find the best "approximate" solution. +面对不相容系统时,我们尝试找到最佳的"近似"解。 -There are various methods to do this, one such method is the **method of least squares.** +有多种方法可以做到这一点,其中一种是**最小二乘法**。 -Suppose we have an inconsistent system +假设我们有一个不相容系统 ```{math} :label: inconsistent + Ax = b ``` -where $A$ is an $m \times n$ matrix and $b$ is an $m \times 1$ column vector. -A **least squares solution** to {eq}`inconsistent` is an $n \times 1$ column vector $\hat{x}$ such that, for all other vectors $x \in \mathbb{R}^n$, the distance from $A\hat{x}$ to $b$ -is less than the distance from $Ax$ to $b$. +其中$A$是一个$m \times n$矩阵,$b$是一个$m \times 1$列向量。 + +对于{eq}`inconsistent`,**最小二乘解**是一个$n \times 1$列向量$\hat{x}$,使得对于所有其他向量$x \in \mathbb{R}^n$,$A\hat{x}$到$b$的距离小于$Ax$到$b$的距离。 -That is, +即, $$ \|A\hat{x} - b\| \leq \|Ax - b\| $$ -It can be shown that, for the system of equations $Ax = b$, the least squares -solution $\hat{x}$ is +可以证明,对于方程组$Ax = b$,最小二乘解$\hat{x}$是 ```{math} :label: least_squares + \hat{x} = (A^T A)^{-1} A^T b ``` -Now consider the general equation of a linear demand curve of a good given by: +现在考虑一种商品的线性需求曲线的一般方程: $$ p = m - nq $$ -where $p$ is the price of the good and $q$ is the quantity demanded. +其中$p$是商品的价格,$q$是需求量。 -Suppose we are trying to *estimate* the values of $m$ and $n$. +假设我们正试图*估计*$m$和$n$的值。 -We do this by repeatedly observing the price and quantity (for example, each -month) and then choosing $m$ and $n$ to fit the relationship between $p$ and -$q$. +我们通过重复观察价格和数量(例如,每个月)来做到这一点,然后选择$m$和$n$来拟合$p$和$q$之间的关系。 -We have the following observations: +我们有以下观察结果: -| Price | Quantity Demanded | +| 价格 | 需求量 | |:-----:|:-----------------:| | 1 | 9 | | 3 | 7 | | 8 | 3 | - -Requiring the demand curve $p = m - nq$ to pass through all these points leads to the -following three equations: +要求需求曲线$p = m - nq$通过所有这些点,得到以下三个方程: $$ \begin{aligned} @@ -1308,17 +1251,18 @@ $$ \end{aligned} $$ -Thus we obtain a system of equations $Ax = b$ where $A = \begin{bmatrix} 1 & -9 \\ 1 & -7 \\ 1 & -3 \end{bmatrix}$, -$x = \begin{bmatrix} m \\ n \end{bmatrix}$ and $b = \begin{bmatrix} 1 \\ 3 \\ 8 \end{bmatrix}$. +因此,我们得到一个方程组$Ax = b$,其中$A = \begin{bmatrix} 1 & -9 \\ 1 & -7 \\ 1 & -3 \end{bmatrix}$, +$x = \begin{bmatrix} m \\ n \end{bmatrix}$,$b = \begin{bmatrix} 1 \\ 3 \\ 8 \end{bmatrix}$。 +可以验证这个系统没有解。 + +(问题在于我们有三个方程但只有两个未知数。) -It can be verified that this system has no solutions. +因此,我们将尝试找到$x$的最佳近似解。 -(The problem is that we have three equations and only two unknowns.) +1. 使用{eq}`least_squares`和矩阵代数找到最小二乘解$\hat{x}$。 -We will thus try to find the best approximate solution for $x$. +2. 使用`numpy.linalg.lstsq`找到最小二乘解,并比较结果。 -1. Use {eq}`least_squares` and matrix algebra to find the least squares solution $\hat{x}$. -2. Find the least squares solution using `numpy.linalg.lstsq` and compare the results. ```{exercise-end} ``` @@ -1333,14 +1277,14 @@ from numpy.linalg import inv ``` ```{code-cell} ipython3 -# Using matrix algebra -A = np.array([[1, -9], # matrix A +# 运用线性代数 +A = np.array([[1, -9], # 矩阵 A [1, -7], [1, -3]]) -A_T = np.transpose(A) # transpose of matrix A +A_T = np.transpose(A) # 矩阵A的转置 -b = np.array((1, 3, 8)) # column vector b +b = np.array((1, 3, 8)) # 列向量 b b.shape = (3, 1) x = inv(A_T @ A) @ A_T @ b @@ -1348,7 +1292,7 @@ x ``` ```{code-cell} ipython3 -# Using numpy.linalg.lstsq +# 使用 numpy.linalg.lstsq x, res, _, _ = np.linalg.lstsq(A, b, rcond=None) ``` @@ -1358,10 +1302,10 @@ x, res, _, _ = np.linalg.lstsq(A, b, rcond=None) print(f"x\u0302 = {x}") print(f"\u2016Ax\u0302 - b\u2016\u00B2 = {res[0]}") ``` +这是一个可视化图,展示了最小二乘法如何近似一组点之间连线的方程。 -Here is a visualization of how the least squares method approximates the equation of a line connecting a set of points. +我们也可以将此描述为在一组点之间"拟合"一条直线。 -We can also describe this as "fitting" a line between a set of points. ```{code-cell} ipython3 fig, ax = plt.subplots() @@ -1370,10 +1314,10 @@ q = np.array((9, 7, 3)) a, b = x -ax.plot(q, p, 'o', label='observations', markersize=5) -ax.plot(q, a - b*q, 'r', label='Fitted line') -plt.xlabel('quantity demanded') -plt.ylabel('price') +ax.plot(q, p, 'o', label='观测点', markersize=5) +ax.plot(q, a - b*q, 'r', label='拟合线') +plt.xlabel('需求数量') +plt.ylabel('价格') plt.legend() plt.show() ``` @@ -1381,9 +1325,8 @@ plt.show() ```{solution-end} ``` +### 延伸阅读 -### Further reading - -The documentation of the `numpy.linalg` submodule can be found [here](https://numpy.org/devdocs/reference/routines.linalg.html). +`numpy.linalg` 子模块的文档可以在[这里](https://numpy.org/devdocs/reference/routines.linalg.html)找到。 -More advanced topics in linear algebra can be found [here](https://python.quantecon.org/linear_algebra.html#id5). +线性代数的更高级主题可以在[这里](https://python.quantecon.org/linear_algebra.html#id5)找到。 diff --git a/lectures/lln_clt.md b/lectures/lln_clt.md index 83f21d4..28b482f 100644 --- a/lectures/lln_clt.md +++ b/lectures/lln_clt.md @@ -11,61 +11,63 @@ kernelspec: name: python3 --- -# LLN and CLT +# 大数定律与中心极限定理 -## Overview +## 概述 -This lecture illustrates two of the most important results in probability and statistics: +本讲座展示了概率统计中两个最重要的结果: -1. the law of large numbers (LLN) and -2. the central limit theorem (CLT). +1. 大数定律(LLN)和 +2. 中心极限定理(CLT)。 -These beautiful theorems lie behind many of the most fundamental results in -econometrics and quantitative economic modeling. +这些美丽的定理是许多计量经济学和定量经济模型的基础。 -The lecture is based around simulations that show the LLN and CLT in action. +本讲座围绕模拟进行,展示了大数定律和中心极限定理的实际操作。 -We also demonstrate how the LLN and CLT break down when the assumptions they -are based on do not hold. +我们还将演示当所依赖的假设不成立时,大数定律和中心极限定理如何失效。 -This lecture will focus on the univariate case (the multivariate case is treated [in a more advanced lecture](https://python.quantecon.org/lln_clt.html#the-multivariate-case)). +本讲将关注单变量情况(多变量情况在[更高级的讲座中处理](https://python.quantecon.org/lln_clt.html#the-multivariate-case))。 -We'll need the following imports: +我们将需要以下导入: ```{code-cell} ipython3 import matplotlib.pyplot as plt +import matplotlib as mpl import numpy as np import scipy.stats as st + +FONTPATH = "fonts/SourceHanSerifSC-SemiBold.otf" +mpl.font_manager.fontManager.addfont(FONTPATH) +plt.rcParams['font.family'] = ['Source Han Serif SC'] ``` (lln_mr)= -## The law of large numbers +## 大数定律 ```{index} single: Law of Large Numbers ``` -We begin with the law of large numbers, which tells us when sample averages -will converge to their population means. +我们从大数定律开始讲起,该定律说明在什么条件下样本平均值会收敛到它们的总体平均值。 -### The LLN in action +### 大数定律的应用 -Let's see an example of the LLN in action before we go further. +在我们进一步讨论之前,让我们看一个大数定律的例子。 ```{prf:example} :label: lln_ex_ber -Consider a [Bernoulli random variable](https://en.wikipedia.org/wiki/Bernoulli_distribution) $X$ with parameter $p$. +考虑一个参数为 $p$ 的[伯努利随机变量](https://en.wikipedia.org/wiki/Bernoulli_distribution) $X$。 -This means that $X$ takes values in $\{0,1\}$ and $\mathbb P\{X=1\} = p$. +这意味着 $X$ 的取值在 $\{0,1\}$ 中,且 $\mathbb P\{X=1\} = p$。 -We can think of drawing $X$ as tossing a biased coin where +我们可以将抽取 $X$ 想象为抛掷一个有偏的硬币,其中 -* the coin falls on "heads" with probability $p$ and -* the coin falls on "tails" with probability $1-p$ +* 硬币落在“正面”的概率为 $p$, +* 硬币落在“反面”的概率为 $1-p$。 -We set $X=1$ if the coin is "heads" and zero otherwise. +如果硬币是“正面”,我们设 $X=1$,否则为零。 -The (population) mean of $X$ is +$X$ 的(总体)均值为 $$ \mathbb E X @@ -73,28 +75,26 @@ $$ $$ ``` -We can generate a draw of $X$ with `scipy.stats` (imported as `st`) as follows: +我们可以使用 `scipy.stats`(导入为 `st`)生成一个 $X$ 的抽样,如下: ```{code-cell} ipython3 p = 0.8 X = st.bernoulli.rvs(p) print(X) ``` +在这个场景中,大数定律告诉我们如果我们多次投掷硬币,我们看到的正面比例将接近均值 $p$。 -In this setting, the LLN tells us if we flip the coin many times, the fraction -of heads that we see will be close to the mean $p$. - -We use $n$ to represent the number of times the coin is flipped. +我们使用 $n$ 来表示投掷硬币的次数。 -Let's check this: +让我们检查一下: ```{code-cell} ipython3 n = 1_000_000 X_draws = st.bernoulli.rvs(p, size=n) -print(X_draws.mean()) # count the number of 1's and divide by n +print(X_draws.mean()) # 计算1的个数并除以n ``` -If we change $p$ the claim still holds: +如果我们改变 $p$,这个说法仍然成立: ```{code-cell} ipython3 p = 0.3 @@ -102,23 +102,21 @@ X_draws = st.bernoulli.rvs(p, size=n) print(X_draws.mean()) ``` -Let's connect this to the discussion above, where we said the sample average -converges to the "population mean". +让我们将这个讨论与上面的讨论联系起来,我们说的样本平均值收敛于“群体平均值”。 -Think of $X_1, \ldots, X_n$ as independent flips of the coin. +想象 $X_1, \ldots, X_n$ 是独立的投掷硬币行为。 -The population mean is the mean in an infinite sample, which equals the -expectation $\mathbb E X$. +群体平均值是在无限样本中的平均值,等于期望 $\mathbb E X$。 -The sample mean of the draws $X_1, \ldots, X_n$ is +抽样的平均值义 $X_1, \ldots, X_n$ 是 $$ \bar X_n := \frac{1}{n} \sum_{i=1}^n X_i $$ -In this case, it is the fraction of draws that equal one (the number of heads divided by $n$). +在这种情况下,它是等于一的抽样的比例(正面的数量除以 $n$)。 -Thus, the LLN tells us that for the Bernoulli trials above +因此,大数定律告诉我们对于上述伯努利试验 ```{math} :label: exp @@ -126,176 +124,161 @@ Thus, the LLN tells us that for the Bernoulli trials above \qquad (n \to \infty) ``` -This is exactly what we illustrated in the code. +这正是我们在代码中演示的。 (lln_ksl)= -### Statement of the LLN - -Let's state the LLN more carefully. +### 大数定律的陈述 -Let $X_1, \ldots, X_n$ be random variables, all of which have the same -distribution. +让我们更仔细地阐述大数定律。 -These random variables can be continuous or discrete. +设 $X_1, \ldots, X_n$ 是随机变量,它们都具有相同的分布。 -For simplicity we will +这些随机变量可以是连续的或离散的。 -* assume they are continuous and -* let $f$ denote their common density function +为简单起见,我们将: -The last statement means that for any $i$ in $\{1, \ldots, n\}$ and any -numbers $a, b$, +* 假设它们是连续的,并且 +* 让 $f$ 表示它们的共同密度函数 +最后这个说法意味着,对于 $\{1, \ldots, n\}$ 中的任何 $i$ 和任何数 $a, b$, $$ \mathbb P\{a \leq X_i \leq b\} = \int_a^b f(x) dx $$ -(For the discrete case, we need to replace densities with probability mass -functions and integrals with sums.) +(对于离散情况,我们需要用概率质量函数替换密度,并用求和替换积分。) -Let $\mu$ denote the common mean of this sample. +让 $\mu$ 表示这个样本的共同平均值。 -Thus, for each $i$, +因此,对于每个 $i$, $$ \mu := \mathbb E X_i = \int_{-\infty}^{\infty} x f(x) dx $$ -The sample mean is +样本均值是 $$ \bar X_n := \frac{1}{n} \sum_{i=1}^n X_i $$ -The next theorem is called Kolmogorov's strong law of large numbers. +接下来的定理称为柯尔莫哥洛夫强大数律。 (iid-theorem)= ````{prf:theorem} -If $X_1, \ldots, X_n$ are IID and $\mathbb E |X|$ is finite, then +如果 $X_1, \ldots, X_n$ 是独立同分布(IID)的,且 $\mathbb E |X|$ 是有限的,则 ```{math} :label: lln_as -\mathbb P \left\{ \bar X_n \to \mu \text{ as } n \to \infty \right\} = 1 +\mathbb P \left\{ \overline{X}_n \to \mu \text{ 当 } n \to \infty \right\} = 1 ``` ```` -Here +这里 -* IID means independent and identically distributed and +* IID 表示独立同分布 * $\mathbb E |X| = \int_{-\infty}^\infty |x| f(x) dx$ +### 关于定理的评论 +定理中的概率为一是什么意思? -### Comments on the theorem - -What does the probability one statement in the theorem mean? - -Let's think about it from a simulation perspective, imagining for a moment that -our computer can generate perfect random samples (although this [isn't strictly true](https://en.wikipedia.org/wiki/Pseudorandom_number_generator)). +我们尝试从模拟的角度来考虑,假设一下我们的计算机可以生成完美的随机样本(尽管事实上这[并非严格真实](https://en.wikipedia.org/wiki/Pseudorandom_number_generator))。 -Let's also imagine that we can generate infinite sequences so that the -statement $\bar X_n \to \mu$ can be evaluated. +同时假设我们可以生成无限序列,从而使得 $\bar X_n \to \mu$ 能够得到评估。 -In this setting, {eq}`lln_as` should be interpreted as meaning that the -probability of the computer producing a sequence where $\bar X_n \to \mu$ -fails to occur is zero. +在这种设置下,{eq}`lln_as` 应该被理解为计算机生成一个 $\bar X_n \to \mu$ 失败发生的概率是零。 -### Illustration +### 示例说明 ```{index} single: Law of Large Numbers; Illustration ``` -Let's illustrate the LLN using simulation. +让我们使用模拟来说明大数定律(LLN)。 -When we illustrate it, we will use a key idea: the sample mean $\bar X_n$ is -itself a random variable. +在说明它时,我们将使用一个关键思想:样本均值 $\bar X_n$ 本身是一个随机变量。 -The reason $\bar X_n$ is a random variable is that it's a function of the -random variables $X_1, \ldots, X_n$. +$\bar X_n$ 是随机变量的原因是它是随机变量 $X_1, \ldots, X_n$ 的函数。 -What we are going to do now is +我们现在要做的是: -1. pick some fixed distribution to draw each $X_i$ from -1. set $n$ to some large number +1. 选择一些固定的分布来抽取每个 $X_i$ +1. 将 $n$ 设置为一个较大的数字 -and then repeat the following three instructions. +然后重复以下三个步骤: -1. generate the draws $X_1, \ldots, X_n$ -1. calculate the sample mean $\bar X_n$ and record its value in an array `sample_means` -1. go to step 1. +1. 生成抽样 $X_1, \ldots, X_n$ +1. 计算样本均值 $\bar X_n$ 并在数组 `sample_means` 中记录其值 +1. 返回步骤 1。 -We will loop over these three steps $m$ times, where $m$ is some large integer. +我们将循环这三个步骤 $m$ 次,其中 $m$ 是一个较大的整数。 -The array `sample_means` will now contain $m$ draws of the random variable $\bar X_n$. +数组 `sample_means` 现在将包含 $m$ 次抽取的随机变量 $\bar X_n$。 -If we histogram these observations of $\bar X_n$, we should see that they are clustered around the population mean $\mathbb E X$. +如果我们对 $\bar X_n$ 的这些观测做直方图,我们应该看到它们聚集在总体平均值 $\mathbb E X$ 周围。 -Moreover, if we repeat the exercise with a larger value of $n$, we should see that the observations are even more tightly clustered around the population mean. +此外,如果我们在更大的 $n$ 值下重复这个练习,我们应该看到观测结果更紧密地聚集在总体平均值周围。 -This is, in essence, what the LLN is telling us. +这实质上就是 LLN 告诉我们的内容。 -To implement these steps, we will use functions. +为了实现这些步骤,我们将使用函数。 -Our first function generates a sample mean of size $n$ given a distribution. +我们的第一个函数生成给定分布的大小为 $n$ 的样本均值。 ```{code-cell} ipython3 -def draw_means(X_distribution, # The distribution of each X_i - n): # The size of the sample mean +def draw_means(X_distribution, # 各个 X_i 的分布 + n): # 样本均值的大小 - # Generate n draws: X_1, ..., X_n + # 生成 n 次抽样:X_1, ..., X_n X_samples = X_distribution.rvs(size=n) - # Return the sample mean + # 返回样本均值 return np.mean(X_samples) ``` -Now we write a function to generate $m$ sample means and histogram them. +现在我们写一个函数来生成 $m$ 个样本均值并绘制它们的直方图。 ```{code-cell} ipython3 def generate_histogram(X_distribution, n, m): - # Compute m sample means - + # 计算 m 个样本均值 sample_means = np.empty(m) for j in range(m): sample_means[j] = draw_means(X_distribution, n) - # Generate a histogram - + # 生成一个直方图 fig, ax = plt.subplots() ax.hist(sample_means, bins=30, alpha=0.5, density=True) - μ = X_distribution.mean() # Get the population mean - σ = X_distribution.std() # and the standard deviation + μ = X_distribution.mean() # 获取总体均值 + σ = X_distribution.std() # 及标准偏差 ax.axvline(x=μ, ls="--", c="k", label=fr"$\mu = {μ}$") ax.set_xlim(μ - σ, μ + σ) ax.set_xlabel(r'$\bar X_n$', size=12) - ax.set_ylabel('density', size=12) + ax.set_ylabel('密度', size=12) ax.legend() plt.show() ``` -Now we call the function. +现在我们调用这个函数。 ```{code-cell} ipython3 -# pick a distribution to draw each $X_i$ from +# 选择一个分布来绘制每个 $X_i$ X_distribution = st.norm(loc=5, scale=2) -# Call the function +# 调用函数 generate_histogram(X_distribution, n=1_000, m=1000) ``` -We can see that the distribution of $\bar X$ is clustered around $\mathbb E X$ -as expected. +我们可以看到 $\bar X$ 的分布像预期的那样围绕 $\mathbb E X$ 聚集。 -Let's vary `n` to see how the distribution of the sample mean changes. +让我们改变 `n` 来看样本均值的分布是如何变化的。 -We will use a [violin plot](https://intro.quantecon.org/prob_dist.html#violin-plots) to show the different distributions. +我们将使用[小提琴图](https://intro.quantecon.org/prob_dist.html#violin-plots)来显示不同的分布。 -Each distribution in the violin plot represents the distribution of $X_n$ for some $n$, calculated by simulation. +小提琴图中的每一个分布代表着某个 $n$ 的 $X_n$ 分布,通过模拟计算得到。 ```{code-cell} ipython3 def means_violin_plot(distribution, @@ -321,177 +304,164 @@ def means_violin_plot(distribution, plt.subplots_adjust(bottom=0.15, wspace=0.05) - ax.set_ylabel('density', size=12) + ax.set_ylabel('密度', size=12) ax.legend() plt.show() ``` -Let's try with a normal distribution. +我们来试试正态分布。 ```{code-cell} ipython3 means_violin_plot(st.norm(loc=5, scale=2)) ``` -As $n$ gets large, more probability mass clusters around the population mean $\mu$. +随着 $n$ 的增大,更多的概率质量聚集在总体均值 $\mu$ 附近。 -Now let's try with a Beta distribution. +现在我们试试 Beta 分布。 ```{code-cell} ipython3 means_violin_plot(st.beta(6, 6)) ``` -We get a similar result. +我们得到了类似的结果。 +++ -## Breaking the LLN +## 打破大数定律 -We have to pay attention to the assumptions in the statement of the LLN. +我们必须关注大数定律陈述中的假设。 -If these assumptions do not hold, then the LLN might fail. +如果这些假设不成立,那么大数定律可能会失败。 -### Infinite first moment +### 无限的第一矩 -As indicated by the theorem, the LLN can break when $\mathbb E |X|$ is not finite. +如定理所示,当 $\mathbb E |X|$ 不是有限的时候,大数定律可以失败。 -We can demonstrate this using the [Cauchy distribution](https://en.wikipedia.org/wiki/Cauchy_distribution). +我们可以使用[柯西分布](https://zh.wikipedia.org/wiki/%E6%9F%AF%E8%A5%BF%E5%88%86%E5%B8%83)来证明这一点。 -The Cauchy distribution has the following property: +柯西分布具有以下性质: -If $X_1, \ldots, X_n$ are IID and Cauchy, then so is $\bar X_n$. +如果 $X_1, \ldots, X_n$ 是独立同分布且符合柯西分布,那么 $\bar X_n$ 也符合柯西分布。 -This means that the distribution of $\bar X_n$ does not eventually concentrate on a single number. +这意味着 $\bar X_n$ 的分布最终不会集中在某一个数字上。 -Hence the LLN does not hold. +因此大数定律不成立。 -The LLN fails to hold here because the assumption $\mathbb E|X| < \infty$ is violated by the Cauchy distribution. +这里大数定律失败是因为柯西分布违反了假设 $\mathbb E|X| < \infty$。 +++ +### IID 条件的失败 -### Failure of the IID condition - -The LLN can also fail to hold when the IID assumption is violated. +LLN 可能因违反 IID 假设而不成立。 ```{prf:example} :label: lln_ex_fail $$ X_0 \sim N(0,1) - \quad \text{and} \quad - X_i = X_{i-1} \quad \text{for} \quad i = 1, ..., n + \quad \text{和} \quad + X_i = X_{i-1} \quad \text{对于} \quad i = 1, ..., n $$ -In this case, +在这种情况下, $$ - \bar X_n = \frac{1}{n} \sum_{i=1}^n X_i = X_0 \sim N(0,1) + \bar{X}_n = \frac{1}{n} \sum_{i=1}^n X_i = X_0 \sim N(0,1) $$ -Therefore, the distribution of $\bar X_n$ is $N(0,1)$ for all $n$! +因此,$\bar{X}_n$ 的分布对所有 $n$ 都是 $N(0,1)$! ``` -Does this contradict the LLN, which says that the distribution of $\bar X_n$ -collapses to the single point $\mu$? - -No, the LLN is correct --- the issue is that its assumptions are not -satisfied. +这是否与 LLN 相矛盾,LLN 表明 $\bar{X}_n$ 的分布将收敛至单点 $\mu$? -In particular, the sequence $X_1, \ldots, X_n$ is not independent. +不,LLN 是正确的——问题在于其假设未被满足。 +特别是,序列 $X_1, \ldots, X_n$ 不是独立的。 ```{note} :name: iid_violation -Although in this case the violation of IID breaks the LLN, there *are* situations -where IID fails but the LLN still holds. +尽管在这种情况下,IID 的违反破坏了 LLN,但*有*的情况下即使 IID 失败 LLN 仍然成立。 -We will show an example in the [exercise](lln_ex3). +我们将在[练习](lln_ex3)中展示一个例子。 ``` +++ -## Central limit theorem +## 中心极限定理 ```{index} single: Central Limit Theorem ``` -Next, we turn to the central limit theorem (CLT), which tells us about the -distribution of the deviation between sample averages and population means. +接下来,我们来讨论中心极限定理(CLT),它告诉我们样本均值与总体均值之间的偏差的分布情况。 +### 定理的陈述 -### Statement of the theorem - -The central limit theorem is one of the most remarkable results in all of mathematics. - -In the IID setting, it tells us the following: +中心极限定理是数学中最了不起的结果之一。 +在独立同分布(IID)的设定下,它告诉我们以下内容: ````{prf:theorem} :label: statement_clt -If $X_1, \ldots, X_n$ is IID with common mean $\mu$ and common variance -$\sigma^2 \in (0, \infty)$, then +如果 $X_1, \ldots, X_n$ 是 IID,具有共同的均值 $\mu$ 和共同的方差 $\sigma^2 \in (0, \infty)$,那么 ```{math} :label: lln_clt -\sqrt{n} ( \bar X_n - \mu ) \stackrel { d } {\to} N(0, \sigma^2) +\sqrt{n} (\bar X_n - \mu ) \stackrel { d } { \to } N(0, \sigma^2) \quad \text{as} \quad n \to \infty ``` ```` -Here $\stackrel { d } {\to} N(0, \sigma^2)$ indicates [convergence in distribution](https://en.wikipedia.org/wiki/Convergence_of_random_variables#Convergence_in_distribution) to a centered (i.e., zero mean) normal with standard deviation $\sigma$. +这里的 $\stackrel { d } { \to } N(0, \sigma^2)$ 表示[分布收敛](https://en.wikipedia.org/wiki/Convergence_of_random_variables#Convergence_in_distribution)到以 0 为均值且标准差为 $\sigma$ 的正态分布。 +CLT 的惊人含义是,对于任何具有有限[二阶矩](https://en.wikipedia.org/wiki/Moment_(mathematics))的分布,简单地添加独立副本总是会得到高斯(正态)曲线。 -The striking implication of the CLT is that for any distribution with -finite [second moment](https://en.wikipedia.org/wiki/Moment_(mathematics)), the simple operation of adding independent -copies always leads to a Gaussian(Normal) curve. +### 模拟 1 -### Simulation 1 +由于中心极限定理(CLT)几乎像魔法一样,运行验证其含义的模拟是构建理解的一种好方法。 -Since the CLT seems almost magical, running simulations that verify its implications is one good way to build understanding. +为此,我们现在进行以下模拟: -To this end, we now perform the following simulation +1. 为基本观察值 $X_i$ 选择一个任意分布 $F$。 +1. 生成独立的 $Y_n := \sqrt{n} (\bar{X}_n - \mu)$ 的抽取。 +1. 使用这些抽取来计算它们的分布的某些度量值——例如直方图。 +1. 将后者与 $N(0, \sigma^2)$ 进行比较。 -1. Choose an arbitrary distribution $F$ for the underlying observations $X_i$. -1. Generate independent draws of $Y_n := \sqrt{n} ( \bar X_n - \mu )$. -1. Use these draws to compute some measure of their distribution --- such as a histogram. -1. Compare the latter to $N(0, \sigma^2)$. +下面的代码正是为指数分布 $F(x) = 1 - e^{- \lambda x}$ 执行了这一操作。 -Here's some code that does exactly this for the exponential distribution -$F(x) = 1 - e^{- \lambda x}$. - -(Please experiment with other choices of $F$, but remember that, to conform with the conditions of the CLT, the distribution must have a finite second moment.) +(请尝试使用其他 $F$ 的选择,但请记住,为了符合CLT的条件,分布必须有有限的二阶矩。) (sim_one)= ```{code-cell} ipython3 -# Set parameters -n = 250 # Choice of n -k = 1_000_000 # Number of draws of Y_n -distribution = st.expon(2) # Exponential distribution, λ = 1/2 +# 设定参数 +n = 250 # n 的选择 +k = 1_000_000 # Y_n 的抽取次数 +distribution = st.expon(2) # 指数分布,λ = 1/2 μ, σ = distribution.mean(), distribution.std() -# Draw underlying RVs. Each row contains a draw of X_1,..,X_n +# 抽取底层随机变量。每行包含一次抽取的 X_1, ..., X_n data = distribution.rvs((k, n)) -# Compute mean of each row, producing k draws of \bar X_n +# 计算每行的均值,生成 k 次抽取的 \bar{X}_n sample_means = data.mean(axis=1) -# Generate observations of Y_n +# 生成 Y_n 的观察值 Y = np.sqrt(n) * (sample_means - μ) -# Plot +# 绘图 fig, ax = plt.subplots(figsize=(10, 6)) xmin, xmax = -3 * σ, 3 * σ ax.set_xlim(xmin, xmax) ax.hist(Y, bins=60, alpha=0.4, density=True) xgrid = np.linspace(xmin, xmax, 200) ax.plot(xgrid, st.norm.pdf(xgrid, scale=σ), - 'k-', lw=2, label='$N(0, \sigma^2)$') + 'k-', lw=2, label='N(0, \sigma^2)') ax.set_xlabel(r"$Y_n$", size=12) ax.set_ylabel(r"$density$", size=12) @@ -500,21 +470,21 @@ ax.legend() plt.show() ``` -(Notice the absence of for loops --- every operation is vectorized, meaning that the major calculations are all shifted to fast C code.) +(注意这里没有 for 循环——所有的操作都是矢量化的,意味着主要计算都转移到了快速的 C 代码上。) -The fit to the normal density is already tight and can be further improved by increasing `n`. +通过增加 `n`,拟合到正态密度可以进一步改进。 -## Exercises +## 练习 ```{exercise} :label: lln_ex1 -Repeat the simulation [above](sim_one) with the [Beta distribution](https://en.wikipedia.org/wiki/Beta_distribution). +用[贝塔分布](https://en.wikipedia.org/wiki/Beta_distribution)重复[上面](sim_one)的模拟。 -You can choose any $\alpha > 0$ and $\beta > 0$. +你可以选择任何 $\alpha > 0$ 和 $\beta > 0$。 ``` ```{solution-start} lln_ex1 @@ -522,20 +492,20 @@ You can choose any $\alpha > 0$ and $\beta > 0$. ``` ```{code-cell} ipython3 -# Set parameters -n = 250 # Choice of n -k = 1_000_000 # Number of draws of Y_n -distribution = st.beta(2,2) # We chose Beta(2, 2) as an example +# 设置参数 +n = 250 # 选择 n 的值 +k = 1_000_000 # Y_n 的抽样次数 +distribution = st.beta(2,2) # 这里选择 Beta(2, 2) 作为示例 μ, σ = distribution.mean(), distribution.std() -# Draw underlying RVs. Each row contains a draw of X_1,..,X_n +# 抽取底层随机变量。每行包含一次抽取的 X_1,..,X_n data = distribution.rvs((k, n)) -# Compute mean of each row, producing k draws of \bar X_n +# 计算每行的均值,生成 k 次 \bar X_n 的抽样 sample_means = data.mean(axis=1) -# Generate observations of Y_n +# 生成 Y_n 的观测值 Y = np.sqrt(n) * (sample_means - μ) -# Plot +# 绘图 fig, ax = plt.subplots(figsize=(10, 6)) xmin, xmax = -3 * σ, 3 * σ ax.set_xlim(xmin, xmax) @@ -555,11 +525,11 @@ plt.show() ````{exercise} :label: lln_ex2 -At the start of this lecture we discussed Bernoulli random variables. +在这次讲座开始时,我们讨论了伯努利随机变量。 -NumPy doesn't provide a `bernoulli` function that we can sample from. +NumPy没有提供我们可以从中采样的`bernoulli`函数。 -However, we can generate a draw of Bernoulli $X$ using NumPy via +但是,我们可以通过NumPy生成伯努利$X$的抽样,使用以下方式: ```python3 U = np.random.rand() @@ -567,60 +537,57 @@ X = 1 if U < p else 0 print(X) ``` -Explain why this provides a random variable $X$ with the right distribution. +解释为什么这能提供一个具有正确分布的随机变量$X$。 ```` ```{solution-start} lln_ex2 :class: dropdown ``` -We can write $X$ as $X = \mathbf 1\{U < p\}$ where $\mathbf 1$ is the -[indicator function](https://en.wikipedia.org/wiki/Indicator_function) (i.e., -1 if the statement is true and zero otherwise). +我们可以将$X$写为$X = \mathbf 1\{U < p\}$,其中$\mathbf 1$是 +[指示函数](https://en.wikipedia.org/wiki/Indicator_function)(即, +如果语句为真则为1,否则为0)。 -Here we generated a uniform draw $U$ on $[0,1]$ and then used the fact that +这里我们生成了一个在$[0,1]$上均匀分布的$U$,然后使用了以下事实: $$ \mathbb P\{0 \leq U < p\} = p - 0 = p $$ -This means that $X = \mathbf 1\{U < p\}$ has the right distribution. +这意味着$X = \mathbf 1\{U < p\}$具有正确的分布。 ```{solution-end} ``` - - -```{exercise} +```{exercise} :label: lln_ex3 -We mentioned above that LLN can still hold sometimes when IID is violated. +我们上面提到即使违反IID条件,LLN有时仍然成立。 -Let's investigate this claim further. +让我们进一步调查这个说法。 -Consider the AR(1) process +考虑AR(1)过程 $$ X_{t+1} = \alpha + \beta X_t + \sigma \epsilon _{t+1} $$ -where $\alpha, \beta, \sigma$ are constants and $\epsilon_1, \epsilon_2, -\ldots$ are IID and standard normal. +其中 $\alpha, \beta, \sigma$ 是常数,$\epsilon_1, \epsilon_2, +\ldots$ 是独立同分布且标准正态。 -Suppose that +假设 $$ X_0 \sim N \left(\frac{\alpha}{1-\beta}, \frac{\sigma^2}{1-\beta^2}\right) $$ -This process violates the independence assumption of the LLN -(since $X_{t+1}$ depends on the value of $X_t$). +这个过程违反了LLN的独立性假设 +(因为 $X_{t+1}$ 依赖于 $X_t$ 的值)。 -However, the next exercise teaches us that LLN type convergence of the sample -mean to the population mean still occurs. +然而,下一个练习告诉我们,样本均值向总体均值的LLN类型收敛仍然会发生。 -1. Prove that the sequence $X_1, X_2, \ldots$ is identically distributed. -2. Show that LLN convergence holds using simulations with $\alpha = 0.8$, $\beta = 0.2$. +1. 证明序列 $X_1, X_2, \ldots$ 是同分布的。 +2. 使用模拟证明LLN收敛成立,其中 $\alpha = 0.8$, $\beta = 0.2$。 ``` @@ -628,16 +595,15 @@ mean to the population mean still occurs. :class: dropdown ``` -**Q1 Solution** +**Q1 解答** -Regarding part 1, we claim that $X_t$ has the same distribution as $X_0$ for -all $t$. +关于第一部分,我们认为 $X_t$ 在所有 $t$ 时刻的分布与 $X_0$ 相同。 -To construct a proof, we suppose that the claim is true for $X_t$. +为了构建证明,我们假设这个命题对 $X_t$ 是正确的。 -Now we claim it is also true for $X_{t+1}$. +现在我们声称它对于 $X_{t+1}$ 也是正确的。 -Observe that we have the correct mean: +观察我们是否得到了正确的均值: $$ \begin{aligned} @@ -647,7 +613,7 @@ $$ \end{aligned} $$ -We also have the correct variance: +我们也得到了正确的方差: $$ \begin{aligned} @@ -657,21 +623,18 @@ $$ \end{aligned} $$ -Finally, since both $X_t$ and $\epsilon_0$ are normally distributed and -independent from each other, any linear combination of these two variables is -also normally distributed. +最后,由于 $X_t$ 和 $\epsilon_0$ 都是正态分布并且彼此独立,这两个变量的任何线性组合也是正态分布的。 -We have now shown that +我们现在已经展示了 $$ X_{t+1} \sim N \left(\frac{\alpha}{1-\beta}, \frac{\sigma^2}{1-\beta^2}\right) $$ -We can conclude this AR(1) process violates the independence assumption but is -identically distributed. +我们可以得出结论,这个AR(1)过程违反了独立性假设,但是分布相同。 -**Q2 Solution** +**Q2 解决方案** ```{code-cell} ipython3 σ = 10 @@ -693,7 +656,7 @@ for t in range(n-1): ax.scatter(range(100, n), means[100:n], s=10, alpha=0.5) ax.set_xlabel(r"$n$", size=12) -ax.set_ylabel(r"$\bar X_n$", size=12) +ax.set_ylabel(r"$\bar{X}_n$", size=12) yabs_max = max(ax.get_ylim(), key=abs) ax.axhline(y=α/(1-β), ls="--", lw=3, label=r"$\mu = \frac{\alpha}{1-\beta}$", @@ -703,8 +666,7 @@ plt.legend() plt.show() ``` -We see the convergence of $\bar x$ around $\mu$ even when the independence assumption is violated. - +我们看到在独立性假设被违反的情况下,$\bar{x}$ 仍然收敛于 $\mu$。 ```{solution-end} -``` +``` \ No newline at end of file diff --git a/lectures/lp_intro.md b/lectures/lp_intro.md index 102ad4f..683b63b 100644 --- a/lectures/lp_intro.md +++ b/lectures/lp_intro.md @@ -10,9 +10,10 @@ kernelspec: --- (lp_intro)= -# Linear Programming +# 线性规划 + +在本讲中,我们将需要以下库。使用 `pip` 安装 [ortools](https://developers.google.com/optimization)。 -In this lecture, we will need the following library. Install [ortools](https://developers.google.com/optimization) using `pip`. ```{code-cell} ipython3 --- @@ -21,31 +22,30 @@ tags: [hide-output] !pip install ortools ``` -## Overview +## 概述 + +**线性规划** 问题旨在在一组线性等式和/或不等式约束下,最大化或最小化一个线性目标函数。 -**Linear programming** problems either maximize or minimize -a linear objective function subject to a set of linear equality and/or inequality constraints. +线性程序成对出现: -Linear programs come in pairs: +* 一个原始的 **原始** 问题,以及 -* an original **primal** problem, and +* 一个相关的 **对偶** 问题。 -* an associated **dual** problem. +如果原始问题涉及 *最大化*,则对偶问题涉及 *最小化*。 -If a primal problem involves *maximization*, the dual problem involves *minimization*. +如果原始问题涉及 *最小化*,则对偶问题涉及 *最大化*。 -If a primal problem involves *minimization**, the dual problem involves **maximization*. +我们提供线性程序的标准形式,以及将其他形式的线性规划问题转化为标准形式的方法。 -We provide a standard form of a linear program and methods to transform other forms of linear programming problems into a standard form. +我们将说明如何使用 [SciPy](https://scipy.org/) 和 [Google OR-Tools](https://developers.google.com/optimization) 来解决线性规划问题。 -We tell how to solve a linear programming problem using [SciPy](https://scipy.org/) and [Google OR-Tools](https://developers.google.com/optimization). ```{seealso} -In another lecture, we will employ the linear programming method to solve the -{doc}`optimal transport problem `. +在另一讲中,我们将使用线性规划方法来解决 {doc}`最优运输问题 `。 ``` -Let's start with some standard imports. +让我们先进行一些标准的导入。 ```{code-cell} ipython3 import numpy as np @@ -55,55 +55,53 @@ import matplotlib.pyplot as plt from matplotlib.patches import Polygon ``` -Let's start with some examples of linear programming problem. - +让我们从一些线性规划问题的例子开始。 +## 示例 1:生产问题 -## Example 1: production problem +这个例子由 {cite}`bertsimas_tsitsiklis1997` 创建。 -This example was created by {cite}`bertsimas_tsitsiklis1997` +假设一个工厂可以生产两种商品,称为产品 $1$ 和产品 $2$。 -Suppose that a factory can produce two goods called Product $1$ and Product $2$. +生产每种产品都需要材料和劳动。 -To produce each product requires both material and labor. +销售每种产品会产生收入。 -Selling each product generates revenue. +每单位所需的材料和劳动投入及其收入如下表所示: -Required per unit material and labor inputs and revenues are shown in table below: +| | 产品 1 | 产品 2 | +| :------: | :----: | :----: | +| 材料 | 2 | 5 | +| 劳动 | 4 | 2 | +| 收入 | 3 | 4 | -| | Product 1 | Product 2 | -| :------: | :-------: | :-------: | -| Material | 2 | 5 | -| Labor | 4 | 2 | -| Revenue | 3 | 4 | +可用的材料为 30 单位,劳动为 20 单位。 -30 units of material and 20 units of labor available. +公司的问题是构建一个生产计划,利用其 30 单位的材料和 20 单位的劳动来最大化其收入。 -A firm's problem is to construct a production plan that uses its 30 units of materials and 20 units of labor to maximize its revenue. +令 $x_i$ 表示公司生产的产品 $i$ 的数量,$z$ 表示总收入。 -Let $x_i$ denote the quantity of Product $i$ that the firm produces and $z$ denote the total revenue. - -This problem can be formulated as: +该问题可以表述为: $$ \begin{aligned} \max_{x_1,x_2} \ & z = 3 x_1 + 4 x_2 \\ -\mbox{subject to } \ & 2 x_1 + 5 x_2 \le 30 \\ +\mbox{受以下约束 } \ & 2 x_1 + 5 x_2 \le 30 \\ & 4 x_1 + 2 x_2 \le 20 \\ & x_1, x_2 \ge 0 \\ \end{aligned} $$ -The following graph illustrates the firm's constraints and iso-revenue lines. +下图说明了公司的约束条件和等收入线。 -Iso-revenue lines show all the combinations of materials and labor that produce the same revenue. +等收入线显示了产生相同收入的材料和劳动的所有组合。 ```{code-cell} ipython3 --- tags: [hide-input] --- fig, ax = plt.subplots() -# Draw constraint lines +#绘制约束 ax.set_xlim(0,15) ax.set_ylim(0,10) x1 = np.linspace(0, 15) @@ -111,17 +109,17 @@ ax.plot(x1, 6-0.4*x1, label="$2x_1 + 5x_2=30$") ax.plot(x1, 10-2*x1, label="$4x_1 + 2x_2=20$") -# Draw the feasible region +# 绘制可行区域 feasible_set = Polygon(np.array([[0, 0],[0, 6],[2.5, 5],[5, 0]]), alpha=0.1) ax.add_patch(feasible_set) -# Draw the objective function -ax.plot(x1, 3.875-0.75*x1, label="iso-revenue lines",color='k',linewidth=0.75) +# 绘制目标函数 +ax.plot(x1, 3.875-0.75*x1, label="等收入线",color='k',linewidth=0.75) ax.plot(x1, 5.375-0.75*x1, color='k',linewidth=0.75) ax.plot(x1, 6.875-0.75*x1, color='k',linewidth=0.75) -# Draw the optimal solution -ax.plot(2.5, 5, ".", label="optimal solution") +# 绘制最优解 +ax.plot(2.5, 5, ".", label="最优解") ax.set_xlabel("$x_1$") ax.set_ylabel("$x_2$") ax.legend() @@ -129,138 +127,127 @@ ax.legend() plt.show() ``` -The blue region is the feasible set within which all constraints are satisfied. - -Parallel black lines are iso-revenue lines. +蓝色区域是可行集合,在该集合内所有约束都得到了满足。 -The firm's objective is to find the parallel black lines to the upper boundary of the feasible set. +平行的黑线是等收入线。 -The intersection of the feasible set and the highest black line delineates the optimal set. +公司的目标是找到与可行集合上边界平行的黑线。 -In this example, the optimal set is the point $(2.5, 5)$. +可行集合与最高黑线的交点界定了最优集合。 +在这个例子中,最优集合是点 $(2.5, 5)$。 +### 计算:使用 OR-Tools -### Computation: using OR-Tools +让我们尝试使用 `ortools.linear_solver` 包来解决同样的问题。 -Let's try to solve the same problem using the package `ortools.linear_solver`. - - - -The following cell instantiates a solver and creates two variables specifying the range of values that they can have. +下面的单元格实例化一个求解器,并创建两个变量,指定它们可以具有的值范围。 ```{code-cell} ipython3 -# Instantiate a GLOP(Google Linear Optimization Package) solver +# 实例化一个 GLOP(Google 线性优化包)求解器。 solver = pywraplp.Solver.CreateSolver('GLOP') ``` -Let's create two variables $x_1$ and $x_2$ such that they can only have nonnegative values. +让我们创建两个变量 $x_1$ 和 $x_2$,使它们只能取非负值。 ```{code-cell} ipython3 -# Create the two variables and let them take on any non-negative value. +# 创建这两个变量,并让它们可以取任何非负值。 x1 = solver.NumVar(0, solver.infinity(), 'x1') x2 = solver.NumVar(0, solver.infinity(), 'x2') ``` -Add the constraints to the problem. +向问题中添加约束条件。 ```{code-cell} ipython3 -# Constraint 1: 2x_1 + 5x_2 <= 30.0 +# 约束 1: 2x_1 + 5x_2 <= 30.0 solver.Add(2 * x1 + 5 * x2 <= 30.0) -# Constraint 2: 4x_1 + 2x_2 <= 20.0 +# 约束 2: 4x_1 + 2x_2 <= 20.0 solver.Add(4 * x1 + 2 * x2 <= 20.0) ``` -Let's specify the objective function. We use `solver.Maximize` method in the case when we want to maximize the objective function and in the case of minimization we can use `solver.Minimize`. +让我们指定目标函数。我们在希望最大化目标函数时使用 `solver.Maximize` 方法,而在希望最小化时可以使用 `solver.Minimize`。 ```{code-cell} ipython3 -# Objective function: 3x_1 + 4x_2 +# 目标函数: 3x_1 + 4x_2 solver.Maximize(3 * x1 + 4 * x2) ``` -Once we solve the problem, we can check whether the solver was successful in solving the problem using its status. If it's successful, then the status will be equal to `pywraplp.Solver.OPTIMAL`. +一旦我们解决了问题,就可以检查求解器是否成功解决了问题,方法是查看其状态。如果成功,则状态将等于 `pywraplp.Solver.OPTIMAL`。 ```{code-cell} ipython3 -# Solve the system. +# 求解系统 status = solver.Solve() if status == pywraplp.Solver.OPTIMAL: print('Objective value =', solver.Objective().Value()) print(f'(x1, x2): ({x1.solution_value():.2}, {x2.solution_value():.2})') else: - print('The problem does not have an optimal solution.') + print('该问题没有最优解。') ``` -## Example 2: investment problem +## 示例 2:投资问题 -We now consider a problem posed and solved by {cite}`hu_guo2018`. +我们现在考虑一个由 {cite}`hu_guo2018` 提出的并解决的问题。 -A mutual fund has $ \$ 100,000$ to be invested over a three-year horizon. +一个共同基金有 $100,000 美元$ 可在三年内投资。 -Three investment options are available: +有三种投资选择可供选择: -1. Annuity: the fund can pay a same amount of new capital at the beginning of each of three years and receive a payoff of 130\% of total capital invested at the end of the third year. Once the mutual fund decides to invest in this annuity, it has to keep investing in all subsequent years in the three year horizon. +1. 年金:基金可以在每年的开始支付相同金额的新资本,并在第三年末获得投资总资本的130\%的收益。一旦共同基金决定投资于此年金,它必须在三年内持续投资。 -2. Bank account: the fund can deposit any amount into a bank at the beginning of each year and receive its capital plus 6\% interest at the end of that year. In addition, the mutual fund is permitted to borrow no more than $20,000 at the beginning of each year and is asked to pay back the amount borrowed plus 6\% interest at the end of the year. The mutual fund can choose whether to deposit or borrow at the beginning of each year. +2. 银行账户:基金可以在每年的开始存入任何金额,并在该年末获得其资本加上6\%的利息。此外,共同基金被允许每年在开始时借款不超过 $20,000,并被要求在年末偿还借款金额加上6\%的利息。共同基金可以选择在每年的开始存款或借款。 -3. Corporate bond: At the beginning of the second year, a corporate bond becomes available. -The fund can buy an amount -that is no more than $ \$ $50,000 of this bond at the beginning of the second year and at the end of the third year receive a payout of 130\% of the amount invested in the bond. +3. 企业债券:在第二年的开始,企业债券变得可用。基金可以在第二年开始时购买不超过 $50,000 美元的债券,并在第三年末获得130\%的投资回报。 -The mutual fund's objective is to maximize total payout that it owns at the end of the third year. +共同基金的目标是在第三年末最大化其拥有的总回报。 -We can formulate this as a linear programming problem. +我们可以将此表述为一个线性规划问题。 -Let $x_1$ be the amount of put in the annuity, $x_2, x_3, x_4$ be bank deposit balances at the beginning of the three years, and $x_5$ be the amount invested in the corporate bond. +让 $x_1$ 表示投入年金的金额,$x_2, x_3, x_4$ 表示三年初的银行存款余额,$x_5$ 表示投资于企业债券的金额。 -When $x_2, x_3, x_4$ are negative, it means that the mutual fund has borrowed from bank. +当 $x_2, x_3, x_4$ 为负时,意味着共同基金从银行借款。 -The table below shows the mutual fund's decision variables together with the timing protocol described above: +下表展示了共同基金的决策变量以及上述时序协议: -| | Year 1 | Year 2 | Year 3 | +| | 第1年 | 第2年 | 第3年 | | :------------: | :----: | :----: | :----: | -| Annuity | $x_1$ | $x_1$ | $x_1$ | -| Bank account | $x_2$ | $x_3$ | $x_4$ | -| Corporate bond | 0 | $x_5$ | 0 | +| 年金 | $x_1$ | $x_1$ | $x_1$ | +| 银行账户 | $x_2$ | $x_3$ | $x_4$ | +| 企业债券 | 0 | $x_5$ | 0 | -The mutual fund's decision making proceeds according to the following timing protocol: +共同基金的决策过程遵循以下时序协议: -1. At the beginning of the first year, the mutual fund decides how much to invest in the annuity and - how much to deposit in the bank. This decision is subject to the constraint: +1. 在第一年的开始,共同基金决定投资多少在年金中,存入银行多少。该决策受以下约束: $$ x_1 + x_2 = 100,000 $$ -2. At the beginning of the second year, the mutual fund has a bank balance of $1.06 x_2$. - It must keep $x_1$ in the annuity. It can choose to put $x_5$ into the corporate bond, - and put $x_3$ in the bank. These decisions are restricted by +2. 在第二年的开始,共同基金的银行余额为 $1.06 x_2$。它必须在年金中保留 $x_1$。它可以选择将 $x_5$ 投入企业债券,并将 $x_3$ 存入银行。这些决策受以下约束: $$ x_1 + x_5 = 1.06 x_2 - x_3 $$ -3. At the beginning of the third year, the mutual fund has a bank account balance equal - to $1.06 x_3$. It must again invest $x_1$ in the annuity, - leaving it with a bank account balance equal to $x_4$. This situation is summarized by the restriction: +3. 在第三年的开始,共同基金的银行账户余额等于 $1.06 x_3$。它必须再次投资 $x_1$ 在年金中,留下银行账户余额为 $x_4$。这种情况可以用以下约束来总结: $$ x_1 = 1.06 x_3 - x_4 $$ -The mutual fund's objective function, i.e., its wealth at the end of the third year is: +共同基金的目标函数,即其在第三年末的财富为: $$ 1.30 \cdot 3x_1 + 1.06 x_4 + 1.30 x_5 $$ -Thus, the mutual fund confronts the linear program: +因此,共同基金面临的线性规划为: $$ \begin{aligned} \max_{x} \ & 1.30 \cdot 3x_1 + 1.06 x_4 + 1.30 x_5 \\ -\mbox{subject to } \ & x_1 + x_2 = 100,000\\ +\mbox{受以下约束 } \ & x_1 + x_2 = 100,000\\ & x_1 - 1.06 x_2 + x_3 + x_5 = 0\\ & x_1 - 1.06 x_3 + x_4 = 0\\ & x_2 \ge -20,000\\ @@ -268,27 +255,26 @@ $$ & x_4 \ge -20,000\\ & x_5 \le 50,000\\ & x_j \ge 0, \quad j = 1,5\\ - & x_j \ \text{unrestricted}, \quad j = 2,3,4\\ + & x_j \ \text{无界}, \quad j = 2,3,4\\ \end{aligned} $$ +### 计算:使用 OR-Tools -### Computation: using OR-Tools - -Let's try to solve the above problem using the package `ortools.linear_solver`. +让我们尝试使用 `ortools.linear_solver` 包来解决上述问题。 -The following cell instantiates a solver and creates two variables specifying the range of values that they can have. +以下代码实例化一个求解器,并创建两个变量,指定它们可以拥有的值范围。 ```{code-cell} ipython3 -# Instantiate a GLOP(Google Linear Optimization Package) solver +#实例化一个 GLOP(Google 线性优化包)求解器。 solver = pywraplp.Solver.CreateSolver('GLOP') ``` -Let's create five variables $x_1, x_2, x_3, x_4,$ and $x_5$ such that they can only have the values defined in the above constraints. +让我们创建五个变量 $x_1, x_2, x_3, x_4$ 和 $x_5$,使它们只能取上述约束中定义的值。 ```{code-cell} ipython3 -# Create the variables using the ranges available from constraints +# 使用约束中可用的范围创建变量。 x1 = solver.NumVar(0, solver.infinity(), 'x1') x2 = solver.NumVar(-20_000, solver.infinity(), 'x2') x3 = solver.NumVar(-20_000, solver.infinity(), 'x3') @@ -296,30 +282,31 @@ x4 = solver.NumVar(-20_000, solver.infinity(), 'x4') x5 = solver.NumVar(0, 50_000, 'x5') ``` -Add the constraints to the problem. +将约束添加到问题中。 ```{code-cell} ipython3 -# Constraint 1: x_1 + x_2 = 100,000 +# 约束 1: x_1 + x_2 = 100,000 solver.Add(x1 + x2 == 100_000.0) -# Constraint 2: x_1 - 1.06 * x_2 + x_3 + x_5 = 0 +# 约束 2: x_1 - 1.06 * x_2 + x_3 + x_5 = 0 solver.Add(x1 - 1.06 * x2 + x3 + x5 == 0.0) -# Constraint 3: x_1 - 1.06 * x_3 + x_4 = 0 +# 约束 3: x_1 - 1.06 * x_3 + x_4 = 0 solver.Add(x1 - 1.06 * x3 + x4 == 0.0) ``` -Let's specify the objective function. +让我们指定目标函数。 + ```{code-cell} ipython3 -# Objective function: 1.30 * 3 * x_1 + 1.06 * x_4 + 1.30 * x_5 +# 目标函数: 1.30 * 3 * x_1 + 1.06 * x_4 + 1.30 * x_5 solver.Maximize(1.30 * 3 * x1 + 1.06 * x4 + 1.30 * x5) ``` -Let's solve the problem and check the status using `pywraplp.Solver.OPTIMAL`. +让我们解决问题,并使用 `pywraplp.Solver.OPTIMAL` 检查状态。 ```{code-cell} ipython3 -# Solve the system. +# 求解系统 status = solver.Solve() if status == pywraplp.Solver.OPTIMAL: @@ -331,37 +318,35 @@ if status == pywraplp.Solver.OPTIMAL: x5_sol = round(x1.solution_value(), 3) print(f'(x1, x2, x3, x4, x5): ({x1_sol}, {x2_sol}, {x3_sol}, {x4_sol}, {x5_sol})') else: - print('The problem does not have an optimal solution.') + print('该问题没有最优解。') ``` -OR-Tools tells us that the best investment strategy is: - -1. At the beginning of the first year, the mutual fund should buy $ \$24,927.755$ of the annuity. Its bank account balance should be $ \$75,072.245$. - -2. At the beginning of the second year, the mutual fund should buy $ \$24,927.755$ of the corporate bond and keep invest in the annuity. Its bank balance should be $ \$24,927.755$. +OR-Tools 告诉我们,最佳投资策略是: -3. At the beginning of the third year, the bank balance should be $ \$75,072.245 $. +1. 在第一年的开始,互助基金应该购买 $ \$24,927.755$ 的年金。其银行账户余额应为 $ \$75,072.245$。 -4. At the end of the third year, the mutual fund will get payouts from the annuity and corporate bond and repay its loan from the bank. At the end it will own $ \$141,018.24 $, so that it's total net rate of return over the three periods is $ 41.02\%$. +2. 在第二年的开始,互助基金应该购买 $ \$24,927.755$ 的公司债券,并继续投资于年金。其银行余额应为 $ \$24,927.755$。 +3. 在第三年的开始,银行余额应为 $ \$75,072.245$。 +4. 在第三年结束时,互助基金将从年金和公司债券中获得收益,并偿还其银行贷款。最终,它将拥有 $ \$141,018.24 $,因此在这三个期间的总净收益率为 $ 41.02\%$。 -## Standard form +## 标准形式 -For purposes of +为了 -* unifying linear programs that are initially stated in superficially different forms, and +* 统一最初以表面不同形式表述的线性规划问题,以及 -* having a form that is convenient to put into black-box software packages, +* 拥有一种便于放入黑盒软件包的形式, -it is useful to devote some effort to describe a **standard form**. +花一些精力来描述 **标准形式** 是很有用的。 -Our standard form is: +我们的标准形式是: $$ \begin{aligned} \min_{x} \ & c_1 x_1 + c_2 x_2 + \dots + c_n x_n \\ -\mbox{subject to } \ & a_{11} x_1 + a_{12} x_2 + \dots + a_{1n} x_n = b_1 \\ +\mbox{受以下约束 } \ & a_{11} x_1 + a_{12} x_2 + \dots + a_{1n} x_n = b_1 \\ & a_{21} x_1 + a_{22} x_2 + \dots + a_{2n} x_n = b_2 \\ & \quad \vdots \\ & a_{m1} x_1 + a_{m2} x_2 + \dots + a_{mn} x_n = b_m \\ @@ -369,7 +354,7 @@ $$ \end{aligned} $$ -Let +让 $$ A = \begin{bmatrix} @@ -383,53 +368,53 @@ c = \begin{bmatrix} c_1 \\ c_2 \\ \vdots \\ c_n \\ \end{bmatrix}, \quad x = \begin{bmatrix} x_1 \\ x_2 \\ \vdots \\ x_n \\ \end{bmatrix}. \quad $$ -The standard form linear programming problem can be expressed concisely as: +标准形式的线性规划问题可以简洁地表达为: $$ \begin{aligned} \min_{x} \ & c'x \\ -\mbox{subject to } \ & Ax = b\\ +\mbox{受以下约束} \ & Ax = b\\ & x \geq 0\\ \end{aligned} $$ (lpproblem) -Here, $Ax = b$ means that the $i$-th entry of $Ax$ equals the $i$-th entry of $b$ for every $i$. +这里,$Ax = b$ 意味着 $Ax$ 的第 $i$ 个元素等于 $b$ 的第 $i$ 个元素,对于每个 $i$ 都成立。 -Similarly, $x \geq 0$ means that $x_j$ is greater than equal to $0$ for every $j$. +同样,$x \geq 0$ 意味着 $x_j$ 对于每个 $j$ 都大于等于 $0$。 -### Useful transformations +### 有用的变换 -It is useful to know how to transform a problem that initially is not stated in the standard form into one that is. +知道如何将一个最初未以标准形式表述的问题转换为标准形式是很有用的。 -By deploying the following steps, any linear programming problem can be transformed into an equivalent standard form linear programming problem. +通过以下步骤,任何线性规划问题都可以转化为一个等效的标准形式线性规划问题。 -1. Objective function: If a problem is originally a constrained *maximization* problem, we can construct a new objective function that is the additive inverse of the original objective function. The transformed problem is then a *minimization* problem. +1. 目标函数:如果一个问题最初是一个受限的 *最大化* 问题,我们可以构建一个新的目标函数,该函数是原始目标函数的加法逆。然后转换的问题是一个 *最小化* 问题。 -2. Decision variables: Given a variable $x_j$ satisfying $x_j \le 0$, we can introduce a new variable $x_j' = - x_j$ and substitute it into original problem. Given a free variable $x_i$ with no restriction on its sign, we can introduce two new variables $x_j^+$ and $x_j^-$ satisfying $x_j^+, x_j^- \ge 0$ and replace $x_j$ by $x_j^+ - x_j^-$. +2. 决策变量:对于一个变量 $x_j$ 满足 $x_j \le 0$,我们可以引入一个新变量 $x_j' = - x_j$ 并将其代入原始问题。对于一个对符号没有限制的自由变量 $x_i$,我们可以引入两个新变量 $x_j^+$ 和 $x_j^-$,使得 $x_j^+, x_j^- \ge 0$,并用 $x_j^+ - x_j^-$ 替换 $x_j$。 -3. Inequality constraints: Given an inequality constraint $\sum_{j=1}^n a_{ij}x_j \le 0$, we can introduce a new variable $s_i$, called a **slack variable** that satisfies $s_i \ge 0$ and replace the original constraint by $\sum_{j=1}^n a_{ij}x_j + s_i = 0$. +3. 不等式约束:对于一个不等式约束 $\sum_{j=1}^n a_{ij}x_j \le 0$,我们可以引入一个新变量 $s_i$,称为 **松弛变量**,使得 $s_i \ge 0$,并用 $\sum_{j=1}^n a_{ij}x_j + s_i = 0$ 替换原始约束。 -Let's apply the above steps to the two examples described above. +让我们将上述步骤应用于上面描述的两个示例。 -### Example 1: production problem +### 示例 1:生产问题 -The original problem is: +原始问题是: $$ \begin{aligned} \max_{x_1,x_2} \ & 3 x_1 + 4 x_2 \\ -\mbox{subject to } \ & 2 x_1 + 5 x_2 \le 30 \\ +\mbox{受以下约束 } \ & 2 x_1 + 5 x_2 \le 30 \\ & 4 x_1 + 2 x_2 \le 20 \\ & x_1, x_2 \ge 0 \\ \end{aligned} $$ -This problem is equivalent to the following problem with a standard form: +这个问题等同于以下标准形式的问题: $$ \begin{aligned} \min_{x_1,x_2} \ & -(3 x_1 + 4 x_2) \\ -\mbox{subject to } \ & 2 x_1 + 5 x_2 + s_1 = 30 \\ +\mbox{受以下约束 } \ & 2 x_1 + 5 x_2 + s_1 = 30 \\ & 4 x_1 + 2 x_2 + s_2 = 20 \\ & x_1, x_2, s_1, s_2 \ge 0 \\ \end{aligned} @@ -437,78 +422,79 @@ $$ -### Computation: using SciPy +### 计算:使用 SciPy + +包 `scipy.optimize` 提供了一个函数 `linprog` 用于求解以下形式的线性规划问题: -The package `scipy.optimize` provides a function `linprog` to solve linear programming problems with a form below: $$ \begin{aligned} \min_{x} \ & c' x \\ -\mbox{subject to } \ & A_{ub}x \le b_{ub} \\ +\mbox{受以下约束} \ & A_{ub}x \le b_{ub} \\ & A_{eq}x = b_{eq} \\ & l \le x \le u \\ \end{aligned} $$ -$A_{eq}, b_{eq}$ denote the equality constraint matrix and vector, and $A_{ub}, b_{ub}$ denote the inequality constraint matrix and vector. +$A_{eq}, b_{eq}$ 表示等式约束矩阵和向量,$A_{ub}, b_{ub}$ 表示不等式约束矩阵和向量。 ```{note} -By default $l = 0$ and $u = \text{None}$ unless explicitly specified with the argument `bounds`. +默认情况下,$l = 0$ 且 $u = \text{None}$,除非通过参数 `bounds` 明确指定。 ``` -Let's now try to solve the Problem 1 using SciPy. +现在让我们尝试使用 SciPy 解决问题 1。 ```{code-cell} ipython3 -# Construct parameters +# 构造参数 c_ex1 = np.array([3, 4]) -# Inequality constraints +# 不等式约束 A_ex1 = np.array([[2, 5], [4, 2]]) b_ex1 = np.array([30,20]) ``` -Once we solve the problem, we can check whether the solver was successful in solving the problem using the boolean attribute `success`. If it's successful, then the `success` attribute is set to `True`. +一旦我们解决了问题,就可以使用布尔属性 `success` 检查求解器是否成功解决了该问题。如果成功,则 `success` 属性被设置为 `True`。 ```{code-cell} ipython3 -# Solve the problem -# we put a negative sign on the objective as linprog does minimization +# 解决问题 +# 我们在目标上加上负号,因为 linprog 进行最小化 res_ex1 = linprog(-c_ex1, A_ub=A_ex1, b_ub=b_ex1) if res_ex1.success: - # We use negative sign to get the optimal value (maximized value) - print('Optimal Value:', -res_ex1.fun) + # 我们使用负号来获得最优值(最大化值) + print('最优值:', -res_ex1.fun) print(f'(x1, x2): {res_ex1.x[0], res_ex1.x[1]}') else: - print('The problem does not have an optimal solution.') + print('该问题没有最优解') ``` -The optimal plan tells the factory to produce $2.5$ units of Product 1 and $5$ units of Product 2; that generates a maximizing value of revenue of $27.5$. +最优计划告诉工厂生产 $2.5$ 单位的产品 1 和 $5$ 单位的产品 2;这将产生最大化的收入值为 $27.5$。 -We are using the `linprog` function as a *black box*. +我们将 `linprog` 函数视为一个 *黑箱*。 -Inside it, Python first transforms the problem into standard form. +在内部,Python 首先将问题转换为标准形式。 -To do that, for each inequality constraint it generates one slack variable. +为此,对于每个不等式约束,它生成一个松弛变量。 -Here the vector of slack variables is a two-dimensional NumPy array that equals $b_{ub} - A_{ub}x$. +这里,松弛变量的向量是一个二维 NumPy 数组,等于 $b_{ub} - A_{ub}x$。 -See the [official documentation](https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.linprog.html#scipy.optimize.linprog) for more details. +有关更多详细信息,请参见 [官方文档](https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.linprog.html#scipy.optimize.linprog)。 ```{note} -This problem is to maximize the objective, so that we need to put a minus sign in front of parameter vector $c$. +这个问题是为了最大化目标,因此我们需要在参数向量 $c$ 前面加上负号。 ``` -### Example 2: investment problem +### 示例 2:投资问题 -The original problem is: +原始问题是: $$ \begin{aligned} \max_{x} \ & 1.30 \cdot 3x_1 + 1.06 x_4 + 1.30 x_5 \\ -\mbox{subject to } \ & x_1 + x_2 = 100,000\\ +\mbox{受以下约束} \ & x_1 + x_2 = 100,000\\ & x_1 - 1.06 x_2 + x_3 + x_5 = 0\\ & x_1 - 1.06 x_3 + x_4 = 0\\ & x_2 \ge -20,000\\ @@ -516,16 +502,16 @@ $$ & x_4 \ge -20,000\\ & x_5 \le 50,000\\ & x_j \ge 0, \quad j = 1,5\\ - & x_j \ \text{unrestricted}, \quad j = 2,3,4\\ + & x_j \ \text{无约束}, \quad j = 2,3,4\\ \end{aligned} $$ -This problem is equivalent to the following problem with a standard form: +这个问题等同于以下标准形式的问题: $$ \begin{aligned} \min_{x} \ & -(1.30 \cdot 3x_1 + 1.06 x_4^+ - 1.06 x_4^- + 1.30 x_5) \\ -\mbox{subject to } \ & x_1 + x_2^+ - x_2^- = 100,000\\ +\mbox{受以下约束:} \ & x_1 + x_2^+ - x_2^- = 100,000\\ & x_1 - 1.06 (x_2^+ - x_2^-) + x_3^+ - x_3^- + x_5 = 0\\ & x_1 - 1.06 (x_3^+ - x_3^-) + x_4^+ - x_4^- = 0\\ & x_2^- - x_2^+ + s_1 = 20,000\\ @@ -539,19 +525,19 @@ $$ $$ ```{code-cell} ipython3 -# Construct parameters +# 构造参数 rate = 1.06 -# Objective function parameters +# 目标函数参数 c_ex2 = np.array([1.30*3, 0, 0, 1.06, 1.30]) -# Inequality constraints +# 不等式约束 A_ex2 = np.array([[1, 1, 0, 0, 0], [1, -rate, 1, 0, 1], [1, 0, -rate, 1, 0]]) b_ex2 = np.array([100_000, 0, 0]) -# Bounds on decision variables +# 决策变量的约束 bounds_ex2 = [( 0, None), (-20_000, None), (-20_000, None), @@ -559,17 +545,17 @@ bounds_ex2 = [( 0, None), ( 0, 50_000)] ``` -Let's solve the problem and check the status using `success` attribute. +让我们解决这个问题并检查 `success` 属性的状态。 ```{code-cell} ipython3 -# Solve the problem +# 求解问题 res_ex2 = linprog(-c_ex2, A_eq=A_ex2, b_eq=b_ex2, bounds=bounds_ex2) if res_ex2.success: - # We use negative sign to get the optimal value (maximized value) - print('Optimal Value:', -res_ex2.fun) + # 我们使用负号来获得最优值(最大化值) + print('最优值:', -res_ex2.fun) x1_sol = round(res_ex2.x[0], 3) x2_sol = round(res_ex2.x[1], 3) x3_sol = round(res_ex2.x[2], 3) @@ -577,34 +563,34 @@ if res_ex2.success: x5_sol = round(res_ex2.x[4], 3) print(f'(x1, x2, x3, x4, x5): {x1_sol, x2_sol, x3_sol, x4_sol, x5_sol}') else: - print('The problem does not have an optimal solution.') + print('该问题没有最优解') ``` -SciPy tells us that the best investment strategy is: +SciPy 告诉我们,最佳投资策略是: + +1. 在第一年的开始,互助基金应购买 $ \$24,927.75$ 的年金。其银行账户余额应为 $ \$75,072.25$。 -1. At the beginning of the first year, the mutual fund should buy $ \$24,927.75$ of the annuity. Its bank account balance should be $ \$75,072.25$. +2. 在第二年的开始,互助基金应购买 $ \$50,000 $ 的公司债券,并继续投资于年金。其银行账户余额应为 $ \$ 4,648.83$。 -2. At the beginning of the second year, the mutual fund should buy $ \$50,000 $ of the corporate bond and keep invest in the annuity. Its bank account balance should be $ \$ 4,648.83$. +3. 在第三年的开始,互助基金应从银行借款 $ \$20,000$ 并投资于年金。 -3. At the beginning of the third year, the mutual fund should borrow $ \$20,000$ from the bank and invest in the annuity. +4. 在第三年末,互助基金将从年金和公司债券中获得收益,并偿还银行贷款。最终将拥有 $ \$141,018.24 $,因此其在三个时期内的总净回报率为 $ 41.02\% $。 -4. At the end of the third year, the mutual fund will get payouts from the annuity and corporate bond and repay its loan from the bank. At the end it will own $ \$141,018.24 $, so that it's total net rate of return over the three periods is $ 41.02\% $. ```{note} -You might notice the difference in the values of optimal solution using OR-Tools and SciPy but the optimal value is the same. It is because there can be many optimal solutions for the same problem. +你可能会注意到使用 OR-Tools 和 SciPy 得到的最优解值有所不同,但最优值是相同的。这是因为同一个问题可能有多个最优解。 ``` -## Exercises +## 练习 ```{exercise-start} :label: lp_intro_ex1 ``` - -Implement a new extended solution for the Problem 1 where in the factory owner decides that number of units of Product 1 should not be less than the number of units of Product 2. +为问题 1 实现一个新的扩展解,其中工厂所有者决定产品 1 的单位数量不得少于产品 2 的单位数量。 ```{exercise-end} ``` @@ -614,55 +600,54 @@ Implement a new extended solution for the Problem 1 where in the factory owner d :class: dropdown ``` -So we can reformulate the problem as: +因此我们可以将问题重新表述为: $$ \begin{aligned} \max_{x_1,x_2} \ & z = 3 x_1 + 4 x_2 \\ -\mbox{subject to } \ & 2 x_1 + 5 x_2 \le 30 \\ +\mbox{受以下约束} \ & 2 x_1 + 5 x_2 \le 30 \\ & 4 x_1 + 2 x_2 \le 20 \\ & x_1 \ge x_2 \\ & x_1, x_2 \ge 0 \\ \end{aligned} $$ - ```{code-cell} ipython3 -# Instantiate a GLOP(Google Linear Optimization Package) solver +# 实例化一个 GLOP(Google 线性优化包)求解器 solver = pywraplp.Solver.CreateSolver('GLOP') -# Create the two variables and let them take on any non-negative value. +# 创建两个变量,并让它们取任何非负值。 x1 = solver.NumVar(0, solver.infinity(), 'x1') x2 = solver.NumVar(0, solver.infinity(), 'x2') ``` ```{code-cell} ipython3 -# Constraint 1: 2x_1 + 5x_2 <= 30.0 +# 约束 1: 2x_1 + 5x_2 <= 30.0 solver.Add(2 * x1 + 5 * x2 <= 30.0) -# Constraint 2: 4x_1 + 2x_2 <= 20.0 +# 约束 2: 4x_1 + 2x_2 <= 20.0 solver.Add(4 * x1 + 2 * x2 <= 20.0) -# Constraint 3: x_1 >= x_2 +# 约束 3: x_1 >= x_2 solver.Add(x1 >= x2) ``` ```{code-cell} ipython3 -# Objective function: 3x_1 + 4x_2 +# 目标函数: 3x_1 + 4x_2 solver.Maximize(3 * x1 + 4 * x2) ``` ```{code-cell} ipython3 -# Solve the system. +# 求解问题 status = solver.Solve() if status == pywraplp.Solver.OPTIMAL: - print('Objective value =', solver.Objective().Value()) + print('目标值 =', solver.Objective().Value()) x1_sol = round(x1.solution_value(), 2) x2_sol = round(x2.solution_value(), 2) print(f'(x1, x2): ({x1_sol}, {x2_sol})') else: - print('The problem does not have an optimal solution.') + print('该问题没有最优解。') ``` ```{solution-end} @@ -672,16 +657,15 @@ else: :label: lp_intro_ex2 ``` -A carpenter manufactures $2$ products - $A$ and $B$. - +一位木匠制造两种产品 - A 和 B。 -Product $A$ generates a profit of $23$ and product $B$ generates a profit of $10$. +产品 A 产生的利润为 23 美元,产品 B 产生的利润为 10 美元。 -It takes $2$ hours for the carpenter to produce $A$ and $0.8$ hours to produce $B$. +生产 A 需要 2 小时,生产 B 需要 0.8 小时。 -Moreover, he can't spend more than $25$ hours per week and the total number of units of $A$ and $B$ should not be greater than $20$. +此外,他每周不能花费超过 25 小时,并且 A 和 B 的总数量不能超过 20 个单位。 -Find the number of units of $A$ and product $B$ that he should manufacture in order to maximise his profit. +找出他应该制造的 A 和 B 的数量,以最大化他的利润。 ```{exercise-end} ``` @@ -691,54 +675,54 @@ Find the number of units of $A$ and product $B$ that he should manufacture in or :class: dropdown ``` -Let us assume the carpenter produces $x$ units of $A$ and $y$ units of $B$. +假设木匠生产 $x$ 单位的 $A$ 和 $y$ 单位的 $B$。 -So we can formulate the problem as: +我们可以将问题表述为: $$ \begin{aligned} \max_{x,y} \ & z = 23 x + 10 y \\ -\mbox{subject to } \ & x + y \le 20 \\ +\mbox{受以下约束} \ & x + y \le 20 \\ & 2 x + 0.8 y \le 25 \\ \end{aligned} $$ ```{code-cell} ipython3 -# Instantiate a GLOP(Google Linear Optimization Package) solver +# 实例化一个GLOP(谷歌线性优化包)求解器 solver = pywraplp.Solver.CreateSolver('GLOP') ``` -Let's create two variables $x_1$ and $x_2$ such that they can only have nonnegative values. +让我们创建两个变量 $x_1$ 和 $x_2$,使它们只能取非负值。 ```{code-cell} ipython3 -# Create the two variables and let them take on any non-negative value. +# 创建这两个变量,并让它们取任何非负值。 x = solver.NumVar(0, solver.infinity(), 'x') y = solver.NumVar(0, solver.infinity(), 'y') ``` ```{code-cell} ipython3 -# Constraint 1: x + y <= 20.0 +# 约束 1: x + y <= 20.0 solver.Add(x + y <= 20.0) -# Constraint 2: 2x + 0.8y <= 25.0 +# 约束 2: 2x + 0.8y <= 25.0 solver.Add(2 * x + 0.8 * y <= 25.0) ``` ```{code-cell} ipython3 -# Objective function: 23x + 10y +# 目标函数: 23x + 10y solver.Maximize(23 * x + 10 * y) ``` ```{code-cell} ipython3 -# Solve the system. +# 求解问题 status = solver.Solve() if status == pywraplp.Solver.OPTIMAL: - print('Maximum Profit =', solver.Objective().Value()) + print('最大利润 =', solver.Objective().Value()) x_sol = round(x.solution_value(), 3) y_sol = round(y.solution_value(), 3) print(f'(x, y): ({x_sol}, {y_sol})') else: - print('The problem does not have an optimal solution.') + print('该问题没有最优解。') ``` ```{solution-end} diff --git a/lectures/markov_chains_I.md b/lectures/markov_chains_I.md index 631dd70..1c66982 100644 --- a/lectures/markov_chains_I.md +++ b/lectures/markov_chains_I.md @@ -11,13 +11,12 @@ kernelspec: name: python3 --- -# Markov Chains: Basic Concepts +# 马尔科夫链:基本概念 - -```{index} single: Markov Chains: Basic Concepts and Stationarity +```{index} single: 马尔科夫链: 基本概念与平稳性 ``` -In addition to what's in Anaconda, this lecture will need the following libraries: +除了 Anaconda 中的库之外,本讲座还需要以下库: ```{code-cell} ipython3 :tags: [hide-output] @@ -25,31 +24,29 @@ In addition to what's in Anaconda, this lecture will need the following librarie !pip install quantecon ``` -## Overview - -Markov chains provide a way to model situations in which the past casts shadows on the future. +## 概述 -By this we mean that observing measurements about a present situation can help us forecast future situations. +马尔科夫链提供了一种对过去对未来产生影响的情况进行建模的方法。 -This can be possible when there are statistical dependencies among measurements of something taken at different points of time. +我们的意思是,观察当前情况的一些测量值可以帮助我们预测未来的情况。 -For example, +当在不同时间点对某事物的测量之间存在统计依赖时,这可能是可能的。 -* inflation next year might co-vary with inflation this year -* unemployment next month might co-vary with unemployment this month +例如, +* 明年的通货膨胀可能与今年的通货膨胀共同变化 +* 下个月的失业率可能与本月的失业率共同变化 -Markov chains are a workhorse for economics and finance. +马尔科夫链是经济学和金融学的一个重要工具。 -The theory of Markov chains is beautiful and provides many insights into -probability and dynamics. +马尔科夫链理论是美丽的,并为概率和动态提供了许多见解。 -In this lecture, we will +在本讲座中,我们将 -* review some of the key ideas from the theory of Markov chains and -* show how Markov chains appear in some economic applications. +* 回顾马尔科夫链理论中的一些关键思想,及 +* 展示马尔科夫链在一些经济应用中的出现方式。 -Let's start with some standard imports: +让我们从一些标准导入开始: ```{code-cell} ipython3 import matplotlib.pyplot as plt @@ -65,87 +62,77 @@ from matplotlib.patches import Polygon from mpl_toolkits.mplot3d.art3d import Poly3DCollection ``` -## Definitions and examples +## 定义与示例 -In this section we provide some definitions and elementary examples. +在本节中,我们将提供一些定义和基本示例。 (finite_dp_stoch_mat)= -### Stochastic matrices - -Recall that a **probability mass function** over $n$ possible outcomes is a -nonnegative $n$-vector $p$ that sums to one. - -For example, $p = (0.2, 0.2, 0.6)$ is a probability mass function over $3$ outcomes. +### 随机矩阵 -A **stochastic matrix** (or **Markov matrix**) is an $n \times n$ square matrix $P$ -such that each row of $P$ is a probability mass function over $n$ outcomes. +回想一下,**概率质量函数**是一个 $n$ 个可能结果上的非负 $n$-维向量 $p$,其总和为 1。 -In other words, +例如,$p = (0.2, 0.2, 0.6)$ 是一个三个结果上的概率质量函数。 -1. each element of $P$ is nonnegative, and -1. each row of $P$ sums to one +**随机矩阵**(或**马尔科夫矩阵**)是一个 $n \times n$ 的方阵 $P$,其中 $P$ 的每一行都是一个 $n$ 个结果上的概率质量函数。 -If $P$ is a stochastic matrix, then so is the $k$-th power $P^k$ for all $k \in \mathbb N$. +换句话说, -You are asked to check this in {ref}`an exercise ` below. +1. $P$ 的每个元素都是非负的,且 +1. $P$ 的每一行的和为 1 +如果 $P$ 是一个随机矩阵,则对于所有 $k \in \mathbb N$,$P^k$ 也是一个随机矩阵。 -### Markov chains +你将在{ref}`一个练习 `中验证这一点。 -Now we can introduce Markov chains. +### 马尔科夫链 -Before defining a Markov chain rigorously, we'll give some examples. +现在我们可以引入马尔科夫链。 +在严格定义马尔科夫链之前,我们先给出一些示例。 (mc_eg2)= -#### Example 1 +#### 示例 1 -From US unemployment data, Hamilton {cite}`Hamilton2005` estimated the following dynamics. +根据美国失业数据,Hamilton {cite}`Hamilton2005` 估计了以下动态。 ```{image} /_static/lecture_specific/markov_chains_I/Hamilton.png :name: mc_hamilton :align: center - ``` -Here there are three **states** +这里有三个**状态**: -* "ng" represents normal growth -* "mr" represents mild recession -* "sr" represents severe recession +* "ng" 表示正常增长 +* "mr" 表示轻度衰退 +* "sr" 表示严重衰退 -The arrows represent transition probabilities over one month. +箭头代表一个月内的转移概率。 -For example, the arrow from mild recession to normal growth has 0.145 next to it. +例如,从轻度衰退到正常增长的箭头旁边有 0.145。 -This tells us that, according to past data, there is a 14.5% probability of transitioning from mild recession to normal growth in one month. +这告诉我们,根据过去的数据,从轻度衰退转移到正常增长的概率为 14.5%。 -The arrow from normal growth back to normal growth tells us that there is a -97% probability of transitioning from normal growth to normal growth (staying -in the same state). +从正常增长回到正常增长的箭头告诉我们,从正常增长转移到正常增长(保持在同一状态)的概率为 97%。 -Note that these are conditional probabilities --- the probability of -transitioning from one state to another (or staying at the same one) conditional on the -current state. +请注意,这些是条件概率——从一个状态转移到另一个状态(或保持在同一状态)的概率是以当前状态为条件的。 -To make the problem easier to work with numerically, let's convert states to -numbers. +为了便于数值处理,让我们将状态转换为数字。 -In particular, we agree that +具体来说,我们约定 -* state 0 represents normal growth -* state 1 represents mild recession -* state 2 represents severe recession +* 状态 0 代表正常增长 +* 状态 1 代表轻度衰退 +* 状态 2 代表严重衰退 -Let $X_t$ record the value of the state at time $t$. +令 $X_t$ 记录时间 $t$ 时的状态值。 -Now we can write the statement "there is a 14.5% probability of transitioning from mild recession to normal growth in one month" as +现在我们可以将“轻度衰退转移到正常增长的概率为 14.5%”的陈述写为 $$ \mathbb P\{X_{t+1} = 0 \,|\, X_t = 1\} = 0.145 $$ -We can collect all of these conditional probabilities into a matrix, as follows +我们可以将所有这些条件概率收集到一个矩阵中,如下所示: $$ P = @@ -156,35 +143,30 @@ P = \end{bmatrix} $$ -Notice that $P$ is a stochastic matrix. +注意,$P$ 是一个随机矩阵。 -Now we have the following relationship +现在我们有以下关系: $$ P(i,j) = \mathbb P\{X_{t+1} = j \,|\, X_t = i\} $$ -This holds for any $i,j$ between 0 and 2. - -In particular, $P(i,j)$ is the - probability of transitioning from state $i$ to state $j$ in one month. - - +这对于任何 $i,j$ 在 0 到 2 之间都成立。 +在此,$P(i,j)$ 是从状态 $i$ 转移到状态 $j$ 在一个月内的概率。 (mc_eg1)= -#### Example 2 +#### 示例 2 -Consider a worker who, at any given time $t$, is either unemployed (state 0) -or employed (state 1). +考虑一个工人,在任何给定时间 $t$,他要么失业(状态 0),要么就业(状态 1)。 -Suppose that, over a one-month period, +假设在一个月内, -1. the unemployed worker finds a job with probability $\alpha \in (0, 1)$. -1. the employed worker loses her job and becomes unemployed with probability $\beta \in (0, 1)$. +1. 失业的工人以概率 $\alpha \in (0, 1)$ 找到工作。 +1. 就业的工人以概率 $\beta \in (0, 1)$ 失去工作并变得失业。 -Given the above information, we can write out the transition probabilities in matrix form as +根据上述信息,我们可以将转移概率写成矩阵形式 ```{math} :label: p_unempemp @@ -196,43 +178,39 @@ P = \end{bmatrix} ``` -For example, +例如, $$ \begin{aligned} P(0,1) & = - \text{ probability of transitioning from state $0$ to state $1$ in one month} + \text{ 从状态 $0$ 转移到状态 $1$ 的概率(一个月内)} \\ & = - \text{ probability finding a job next month} + \text{ 找到下个月工作的概率} \\ & = \alpha \end{aligned} $$ -Suppose we can estimate the values $\alpha$ and $\beta$. +假设我们可以估计 $\alpha$ 和 $\beta$ 的值。 -Then we can address a range of questions, such as +那么我们可以解决一系列问题,例如 -* What is the average duration of unemployment? -* Over the long-run, what fraction of the time does a worker find herself unemployed? -* Conditional on employment, what is the probability of becoming unemployed at least once over the next 12 months? +* 失业的平均持续时间是多少? +* 从长期来看,工人失业的时间占总时间的多少? +* 在就业的条件下,工人在接下来的 12 个月内至少失业一次的概率是多少? -We'll cover some of these applications below. +我们将在下面讨论其中一些应用。 (mc_eg3)= -#### Example 3 +#### 示例 3 -Imam and Temple {cite}`imampolitical` categorize political institutions into -three types: democracy $\text{(D)}$, autocracy $\text{(A)}$, and an intermediate -state called anocracy $\text{(N)}$. +Imam 和 Temple {cite}`imampolitical` 将政治制度分类为三种类型:民主 $\text{(D)}$,专制 $\text{(A)}$ 和一个称为无政府状态的中间状态 $\text{(N)}$。 -Each institution can have two potential development regimes: collapse $\text{(C)}$ and growth $\text{(G)}$. This results in six possible states: $\text{DG, DC, NG, NC, AG}$ and $\text{AC}$. - -Imam and Temple {cite}`imampolitical` estimate the following transition -probabilities: +每种制度都可以有两种可能的发展模式:崩溃 $\text{(C)}$ 和增长 $\text{(G)}$。这导致了六种可能的状态:$\text{DG, DC, NG, NC, AG}$ 和 $\text{AC}$。 +Imam 和 Temple {cite}`imampolitical` 估计了以下转移概率: $$ P := @@ -256,7 +234,7 @@ P = [[0.86, 0.11, 0.03, 0.00, 0.00, 0.00], [0.00, 0.00, 0.09, 0.15, 0.26, 0.50]] ``` -Here is a visualization, with darker colors indicating higher probability. +下面是可视化图,颜色越深表示概率越高。 ```{code-cell} ipython3 :tags: [hide-input] @@ -287,30 +265,25 @@ plt.colorbar(pc, ax=ax) plt.show() ``` -Looking at the data, we see that democracies tend to have longer-lasting growth -regimes compared to autocracies (as indicated by the lower probability of -transitioning from growth to growth in autocracies). - -We can also find a higher probability from collapse to growth in democratic regimes. +查看数据后,我们发现民主政体的增长期通常比专制政体更长(这体现在专制政体中从增长到增长的转移概率较低)。 +我们还可以发现,在民主政体中,从崩溃到增长的概率较高。 -### Defining Markov chains +### 定义马尔科夫链 +到目前为止,我们已经给出了马尔科夫链的示例,但还没有对其进行定义。 -So far we've given examples of Markov chains but we haven't defined them. +现在让我们进行定义。 -Let's do that now. +首先,设 $S$ 是一个有限集合 $\{x_1, \ldots, x_n\}$,其中包含 $n$ 个元素。 -To begin, let $S$ be a finite set $\{x_1, \ldots, x_n\}$ with $n$ elements. +集合 $S$ 被称为**状态空间**,$x_1, \ldots, x_n$ 被称为**状态值**。 -The set $S$ is called the **state space** and $x_1, \ldots, x_n$ are the **state values**. +一个分布 $\psi$ 在 $S$ 上是一个长度为 $n$ 的概率质量函数,其中 $\psi(i)$ 是分配给状态 $x_i$ 的概率。 -A **distribution** $\psi$ on $S$ is a probability mass function of length $n$, where $\psi(i)$ is the amount of probability allocated to state $x_i$. +在 $S$ 上的**马尔科夫链** $\{X_t\}$ 是一个取值于 $S$ 的随机变量序列,且具有**马尔科夫性质**。 -A **Markov chain** $\{X_t\}$ on $S$ is a sequence of random variables taking values in $S$ -that have the **Markov property**. - -This means that, for any date $t$ and any state $y \in S$, +这意味着,对于任何时间 $t$ 和任何状态 $y \in S$, ```{math} :label: fin_markov_mp @@ -319,9 +292,9 @@ This means that, for any date $t$ and any state $y \in S$, = \mathbb P \{ X_{t+1} = y \,|\, X_t, X_{t-1}, \ldots \} ``` -This means that once we know the current state $X_t$, adding knowledge of earlier states $X_{t-1}, X_{t-2}$ provides no additional information about probabilities of *future* states. +这意味着一旦我们知道当前状态 $X_t$,添加之前状态 $X_{t-1}, X_{t-2}$ 的知识不会对未来状态的概率提供额外信息。 -Thus, the dynamics of a Markov chain are fully determined by the set of **conditional probabilities** +因此,马尔科夫链的动态完全由**条件概率**集合决定: ```{math} :label: mpp @@ -330,95 +303,89 @@ P(x, y) := \mathbb P \{ X_{t+1} = y \,|\, X_t = x \} \qquad (x, y \in S) ``` -By construction, +根据构造, -* $P(x, y)$ is the probability of going from $x$ to $y$ in one unit of time (one step) -* $P(x, \cdot)$ is the conditional distribution of $X_{t+1}$ given $X_t = x$ +* $P(x, y)$ 是从 $x$ 到 $y$ 在一个时间单位(一步)内的转移概率 +* $P(x, \cdot)$ 是给定 $X_t = x$ 时,$X_{t+1}$ 的条件分布 -We can view $P$ as a stochastic matrix where +我们可以将 $P$ 视为一个随机矩阵,其中 $$ P_{ij} = P(x_i, x_j) \qquad 1 \leq i, j \leq n $$ -Going the other way, if we take a stochastic matrix $P$, we can generate a Markov -chain $\{X_t\}$ as follows: - -* draw $X_0$ from a distribution $\psi_0$ on $S$ -* for each $t = 0, 1, \ldots$, draw $X_{t+1}$ from $P(X_t,\cdot)$ +反过来,如果我们取一个随机矩阵 $P$,我们可以按如下方式生成一个马尔科夫链 $\{X_t\}$: -By construction, the resulting process satisfies {eq}`mpp`. +* 从 $S$ 上的分布 $\psi_0$ 中抽取 $X_0$ +* 对于每个 $t = 0, 1, \ldots$,从 $P(X_t, \cdot)$ 中抽取 $X_{t+1}$ +通过构造,所得的过程满足 {eq}`mpp`。 +## 模拟 -## Simulation - -```{index} single: Markov Chains; Simulation +```{index} single: 马尔科夫链; 模拟 ``` -A good way to study Markov chains is to simulate them. +研究马尔科夫链的一个好方法是模拟它们。 -Let's start by doing this ourselves and then look at libraries that can help -us. +让我们先自己做这个,然后再看看可以帮助我们的库。 -In these exercises, we'll take the state space to be $S = 0,\ldots, n-1$. +在这些练习中,我们将状态空间设为 $S = 0,\ldots, n-1$。 -(We start at $0$ because Python arrays are indexed from $0$.) +(我们从 $0$ 开始,因为 Python 数组是从 $0$ 索引的。) -### Writing our own simulation code +### 编写我们自己的模拟代码 -To simulate a Markov chain, we need +要模拟一个马尔科夫链,我们需要 -1. a stochastic matrix $P$ and -1. a probability mass function $\psi_0$ of length $n$ from which to draw an initial realization of $X_0$. +1. 一个随机矩阵 $P$ 和 +1. 一个长度为 $n$ 的概率质量函数 $\psi_0$,从中抽取 $X_0$ 的初始实现。 -The Markov chain is then constructed as follows: +然后马尔科夫链按照如下方式构建: -1. At time $t=0$, draw a realization of $X_0$ from the distribution $\psi_0$. -1. At each subsequent time $t$, draw a realization of the new state $X_{t+1}$ from $P(X_t, \cdot)$. +1. 在时间 $t=0$,从分布 $\psi_0$ 中抽取 $X_0$ 的一个实现。 +1. 在每个后续时间 $t$,从 $P(X_t, \cdot)$ 中抽取一个新状态 $X_{t+1}$ 的实现。 -(That is, draw from row $X_t$ of $P$.) +(也就是说,从 $P$ 的第 $X_t$ 行中抽取。) -To implement this simulation procedure, we need a method for generating draws -from a discrete distribution. +要实现这个模拟过程,我们需要一种方法从离散分布中生成抽取结果。 -For this task, we'll use `random.draw` from [QuantEcon.py](http://quantecon.org/quantecon-py). +对于这个任务,我们将使用 [QuantEcon.py](http://quantecon.org/quantecon-py) 中的 `random.draw`。 -To use `random.draw`, we first need to convert the probability mass function -to a cumulative distribution +要使用 `random.draw`,我们首先需要将概率质量函数转换为累积分布。 ```{code-cell} ipython3 -ψ_0 = (0.3, 0.7) # probabilities over {0, 1} -cdf = np.cumsum(ψ_0) # convert into cumulative distribution -qe.random.draw(cdf, 5) # generate 5 independent draws from ψ +ψ_0 = (0.3, 0.7) # {0, 1} 上的概率分布 +cdf = np.cumsum(ψ_0) # 转换为累积分布 +qe.random.draw(cdf, 5) # 从 ψ 中生成 5 个独立抽取 ``` -We'll write our code as a function that accepts the following three arguments +我们将编写一个函数,该函数接受以下三个参数: -* A stochastic matrix `P`. -* An initial distribution `ψ_0`. -* A positive integer `ts_length` representing the length of the time series the function should return. +* 随机矩阵 `P`。 +* 初始分布 `ψ_0`。 +* 正整数 `ts_length`,表示函数应返回的时间序列的长度。 ```{code-cell} ipython3 def mc_sample_path(P, ψ_0=None, ts_length=1_000): - # set up + # 设置 P = np.asarray(P) X = np.empty(ts_length, dtype=int) - # Convert each row of P into a cdf - P_dist = np.cumsum(P, axis=1) # Convert rows into cdfs + # 将 P 的每一行转换为累积分布函数(cdf) + P_dist = np.cumsum(P, axis=1) # 将行转换为 cdf - # draw initial state, defaulting to 0 + # 抽取初始状态,默认为 0 if ψ_0 is not None: X_0 = qe.random.draw(np.cumsum(ψ_0)) else: X_0 = 0 - # simulate + # 模拟 X[0] = X_0 for t in range(ts_length - 1): X[t+1] = qe.random.draw(P_dist[X[t], :]) @@ -426,43 +393,40 @@ def mc_sample_path(P, ψ_0=None, ts_length=1_000): return X ``` -Let's see how it works using the small matrix +让我们看看它是如何工作的,使用一个小的矩阵 ```{code-cell} ipython3 P = [[0.4, 0.6], [0.2, 0.8]] ``` -Here's a short time series. +以下是一个短的时间序列。 ```{code-cell} ipython3 mc_sample_path(P, ψ_0=(1.0, 0.0), ts_length=10) ``` -It can be shown that for a long series drawn from `P`, the fraction of the -sample that takes value 0 will be about 0.25. +可以证明,从矩阵 `P` 中生成的长序列中,取值为 0 的样本占比将约为 0.25。 -(We will explain why {ref}`later `.) +(我们将在{ref}`稍后 `解释为什么。) -Moreover, this is true regardless of the initial distribution from which -$X_0$ is drawn. +而且,这与 $X_0$ 是从哪个初始分布中抽取的无关。 -The following code illustrates this +下面的代码演示了这一点 ```{code-cell} ipython3 X = mc_sample_path(P, ψ_0=(0.1, 0.9), ts_length=1_000_000) np.mean(X == 0) ``` -You can try changing the initial distribution to confirm that the output is -always close to 0.25 (for the `P` matrix above). +您可以尝试更改初始分布,以确认输出总是接近 0.25(对于上述矩阵 `P`)。 -### Using QuantEcon's routines +### 使用 QuantEcon 的例程 -[QuantEcon.py](http://quantecon.org/quantecon-py) has routines for handling Markov chains, including simulation. +[QuantEcon.py](http://quantecon.org/quantecon-py) 提供了一些处理马尔科夫链的例程,包括模拟。 -Here's an illustration using the same $P$ as the preceding example +以下是使用与前例相同的 $P$ 的说明 ```{code-cell} ipython3 mc = qe.MarkovChain(P) @@ -470,63 +434,62 @@ X = mc.simulate(ts_length=1_000_000) np.mean(X == 0) ``` -The `simulate` routine is faster (because it is [JIT compiled](https://python-programming.quantecon.org/numba.html#numba-link)). +`simulate` 例程速度更快(因为它是 [JIT 编译](https://python-programming.quantecon.org/numba.html#numba-link) 的)。 ```{code-cell} ipython3 -%time mc_sample_path(P, ts_length=1_000_000) # Our homemade code version +%time mc_sample_path(P, ts_length=1_000_000) # 我们自制的代码版本 ``` ```{code-cell} ipython3 -%time mc.simulate(ts_length=1_000_000) # qe code version +%time mc.simulate(ts_length=1_000_000) # qe 代码版本 ``` -#### Adding state values and initial conditions +#### 添加状态值和初始条件 -If we wish to, we can provide a specification of state values to `MarkovChain`. +如果需要,我们可以向 `MarkovChain` 提供状态值的规范。 -These state values can be integers, floats, or even strings. +这些状态值可以是整数、浮点数,甚至是字符串。 -The following code illustrates +以下代码说明了这一点 ```{code-cell} ipython3 -mc = qe.MarkovChain(P, state_values=('unemployed', 'employed')) -mc.simulate(ts_length=4, init='employed') # Start at employed initial state +mc = qe.MarkovChain(P, state_values=('失业', '就业')) +mc.simulate(ts_length=4, init='就业') # 从就业初始状态开始 ``` ```{code-cell} ipython3 -mc.simulate(ts_length=4, init='unemployed') # Start at unemployed initial state +mc.simulate(ts_length=4, init='失业') # 从失业初始状态开始 ``` ```{code-cell} ipython3 -mc.simulate(ts_length=4) # Start at randomly chosen initial state +mc.simulate(ts_length=4) # 从随机选择的初始状态开始 ``` -If we want to see indices rather than state values as outputs as we can use +如果我们希望看到索引而不是状态值作为输出,我们可以使用 ```{code-cell} ipython3 mc.simulate_indices(ts_length=4) ``` (mc_md)= -## Distributions over time +## 随时间分布 -We learned that +我们了解到 -1. $\{X_t\}$ is a Markov chain with stochastic matrix $P$ -1. the distribution of $X_t$ is known to be $\psi_t$ +1. $\{X_t\}$ 是一个具有随机矩阵 $P$ 的马尔科夫链 +1. $X_t$ 的分布已知为 $\psi_t$ -What then is the distribution of $X_{t+1}$, or, more generally, of $X_{t+m}$? +那么,$X_{t+1}$ 的分布是什么?更一般地,$X_{t+m}$ 的分布是什么? -To answer this, we let $\psi_t$ be the distribution of $X_t$ for $t = 0, 1, 2, \ldots$. +为了回答这个问题,令 $\psi_t$ 为 $X_t$ 的分布,$t = 0, 1, 2, \ldots$。 -Our first aim is to find $\psi_{t + 1}$ given $\psi_t$ and $P$. +我们的第一个目标是找到给定 $\psi_t$ 和 $P$ 时的 $\psi_{t + 1}$。 -To begin, pick any $y \in S$. +首先,选择任意 $y \in S$。 -To get the probability of being at $y$ tomorrow (at $t+1$), we account for -all ways this can happen and sum their probabilities. +为了得到明天($t+1$ 时)在 $y$ 的概率,我们计算所有可能发生的方式,并对它们的概率求和。 -This leads to +这导致了 $$ \mathbb P \{X_{t+1} = y \} @@ -536,17 +499,17 @@ $$ -(We are using the [law of total probability](https://en.wikipedia.org/wiki/Law_of_total_probability).) +(我们正在使用[全概率公式](https://en.wikipedia.org/wiki/Law_of_total_probability)。) -Rewriting this statement in terms of marginal and conditional probabilities gives +将这一陈述重新写为边际概率和条件概率的形式: $$ \psi_{t+1}(y) = \sum_{x \in S} P(x,y) \psi_t(x) $$ -There are $n$ such equations, one for each $y \in S$. +有 $n$ 个这样的方程,每个 $y \in S$ 对应一个方程。 -If we think of $\psi_{t+1}$ and $\psi_t$ as row vectors, these $n$ equations are summarized by the matrix expression +如果我们将 $\psi_{t+1}$ 和 $\psi_t$ 视为行向量,则这 $n$ 个方程可通过矩阵表达式总结为 ```{math} :label: fin_mc_fr @@ -554,17 +517,15 @@ If we think of $\psi_{t+1}$ and $\psi_t$ as row vectors, these $n$ equations are \psi_{t+1} = \psi_t P ``` -Thus, we postmultiply by $P$ to move a distribution forward one unit of time. +因此,我们通过右乘 $P$ 将分布向前移动一个时间单位。 -By postmultiplying $m$ times, we move a distribution forward $m$ steps into the future. +通过右乘 $m$ 次,我们将分布向前移动 $m$ 步进入未来。 -Hence, iterating on {eq}`fin_mc_fr`, the expression $\psi_{t+m} = \psi_t P^m$ is also valid --- here $P^m$ is the $m$-th power of $P$. +因此,迭代 {eq}`fin_mc_fr`,表达式 $\psi_{t+m} = \psi_t P^m$ 也是有效的——这里 $P^m$ 是 $P$ 的第 $m$ 次幂。 -As a special case, we see that if $\psi_0$ is the initial distribution from -which $X_0$ is drawn, then $\psi_0 P^m$ is the distribution of -$X_m$. +作为一个特例,我们看到,如果 $\psi_0$ 是从中抽取 $X_0$ 的初始分布,则 $\psi_0 P^m$ 是 $X_m$ 的分布。 -This is very important, so let's repeat it +这非常重要,所以我们重复一下 ```{math} :label: mdfmc @@ -572,9 +533,9 @@ This is very important, so let's repeat it X_0 \sim \psi_0 \quad \implies \quad X_m \sim \psi_0 P^m ``` -The general rule is that postmultiplying a distribution by $P^m$ shifts it forward $m$ units of time. +一般规则是通过右乘 $P^m$ 将分布向前移动 $m$ 个时间单位。 -Hence the following is also valid. +因此,以下也是有效的。 ```{math} :label: mdfmc2 @@ -582,43 +543,38 @@ Hence the following is also valid. X_t \sim \psi_t \quad \implies \quad X_{t+m} \sim \psi_t P^m ``` - - (finite_mc_mstp)= -### Multiple step transition probabilities +### 多步转移概率 -We know that the probability of transitioning from $x$ to $y$ in -one step is $P(x,y)$. +我们知道,从 $x$ 到 $y$ 的一步转移概率是 $P(x,y)$。 -It turns out that the probability of transitioning from $x$ to $y$ in -$m$ steps is $P^m(x,y)$, the $(x,y)$-th element of the -$m$-th power of $P$. +实际上,从 $x$ 到 $y$ 的 $m$ 步转移概率是 $P^m(x,y)$,即 $P$ 的 $m$ 次方的 $(x,y)$ 元素。 -To see why, consider again {eq}`mdfmc2`, but now with a $\psi_t$ that puts all probability on state $x$. +要理解为什么是这样,请再次考虑 {eq}`mdfmc2`,但现在设 $\psi_t$ 将所有概率都放在状态 $x$ 上。 -Then $\psi_t$ is a vector with $1$ in position $x$ and zero elsewhere. +此时,$\psi_t$ 是一个在 $x$ 位置为 $1$,在其他地方为 $0$ 的向量。 -Inserting this into {eq}`mdfmc2`, we see that, conditional on $X_t = x$, the distribution of $X_{t+m}$ is the $x$-th row of $P^m$. +将其代入 {eq}`mdfmc2`,我们看到,条件 $X_t = x$ 的情况下,$X_{t+m}$ 的分布是 $P^m$ 的第 $x$ 行。 -In particular +特别地, $$ -\mathbb P \{X_{t+m} = y \,|\, X_t = x \} = P^m(x, y) = (x, y) \text{-th element of } P^m +\mathbb P \{X_{t+m} = y \,|\, X_t = x \} = P^m(x, y) = P^m 的第 (x, y) 元素 $$ -### Example: probability of recession +### 示例:衰退概率 -```{index} single: Markov Chains; Future Probabilities +```{index} single: 马尔科夫链; 未来概率 ``` -Recall the stochastic matrix $P$ for recession and growth {ref}`considered above `. +回顾我们{ref}`之前讨论的`关于衰退和增长的随机矩阵 $P$ 。 -Suppose that the current state is unknown --- perhaps statistics are available only at the *end* of the current month. +假设当前状态未知——也许统计数据只能在当前月份*结束*时获得。 -We guess that the probability that the economy is in state $x$ is $\psi_t(x)$ at time t. +我们猜测,在时间 $t$ 时经济处于状态 $x$ 的概率是 $\psi_t(x)$。 -The probability of being in recession (either mild or severe) in 6 months time is given by +那么,6 个月后处于衰退(无论是轻度还是严重衰退)的概率为 $$ (\psi_t P^6)(1) + (\psi_t P^6)(2) @@ -627,52 +583,41 @@ $$ (mc_eg1-1)= -### Example 2: cross-sectional distributions +### 示例 2:横截面分布 -The distributions we have been studying can be viewed either +我们研究的分布可以视为 -1. as probabilities or -1. as cross-sectional frequencies that the law of large numbers leads us to anticipate for large samples. +1. 概率,或 +1. 横截面频率,即根据大数法则我们预期的大样本中的结果。 -To illustrate, recall our model of employment/unemployment dynamics for a given worker {ref}`discussed above `. +为了解释这一点,请回顾我们之前讨论的关于单个工人就业/失业动态的模型 {ref}`上面讨论过的 `。 -Consider a large population of workers, each of whose lifetime experience is -described by the specified dynamics, with each worker's outcomes being -realizations of processes that are statistically independent of all other -workers' processes. +现在考虑一个大的工人群体,每个工人的一生经历都符合指定的动态,每个工人的结果都是与其他工人独立的过程的实现。 -Let $\psi_t$ be the current *cross-sectional* distribution over $\{ 0, 1 \}$. +令 $\psi_t$ 为 $\{0, 1\}$ 上的*横截面*分布。 -The cross-sectional distribution records fractions of workers employed and unemployed at a given moment $t$. +横截面分布记录了某一时刻 $t$ 工人的就业和失业比例。 -* For example, $\psi_t(0)$ is the unemployment rate at time $t$. +* 例如,$\psi_t(0)$ 是时间 $t$ 的失业率。 -What will the cross-sectional distribution be in 10 periods hence? +10 个周期之后,横截面分布会是什么样子? -The answer is $\psi_t P^{10}$, where $P$ is the stochastic matrix in -{eq}`p_unempemp`. +答案是 $\psi_t P^{10}$,其中 $P$ 是 {eq}`p_unempemp` 中的随机矩阵。 -This is because each worker's state evolves according to $P$, so -$\psi_t P^{10}$ is a [marginal distribution](https://en.wikipedia.org/wiki/Marginal_distribution) for a single randomly selected -worker. +这是因为每个工人的状态都根据 $P$ 变化,因此 $\psi_t P^{10}$ 是单个随机选择的工人的[边际分布](https://en.wikipedia.org/wiki/Marginal_distribution)。 -But when the sample is large, outcomes and probabilities are roughly equal (by an application of the law -of large numbers). +但当样本很大时,结果和概率大致相等(通过应用大数法则)。 -So for a very large (tending to infinite) population, -$\psi_t P^{10}$ also represents fractions of workers in -each state. +因此,对于一个非常大的(趋向于无限)群体,$\psi_t P^{10}$ 也代表每个状态的工人比例。 -This is exactly the cross-sectional distribution. +这正是横截面分布。 (stationary)= -## Stationary distributions +## 平稳分布 +如 {eq}`fin_mc_fr` 所示,我们可以通过右乘 $P$ 将分布向前移动一个时间单位。 -As seen in {eq}`fin_mc_fr`, we can shift a distribution forward one -unit of time via postmultiplication by $P$. - -Some distributions are invariant under this updating process --- for example, +一些分布在此更新过程中是不变的——例如, ```{code-cell} ipython3 P = np.array([[0.4, 0.6], @@ -681,129 +626,113 @@ P = np.array([[0.4, 0.6], ψ @ P ``` -Notice that `ψ @ P` is the same as `ψ`. - +注意,`ψ @ P` 与 `ψ` 相同。 - -Such distributions are called **stationary** or **invariant**. +这样的分布被称为**平稳**或**不变**分布。 (mc_stat_dd)= -Formally, a distribution $\psi^*$ on $S$ is called **stationary** for $P$ if $\psi^* P = \psi^* $. +正式地,如果分布 $\psi^*$ 满足 $\psi^* P = \psi^*$,则称其为 $P$ 的**平稳**分布。 -Notice that, postmultiplying by $P$, we have $\psi^* P^2 = \psi^* P = \psi^*$. +注意,通过右乘 $P$,我们有 $\psi^* P^2 = \psi^* P = \psi^*$。 -Continuing in the same way leads to $\psi^* = \psi^* P^t$ for all $t \ge 0$. +继续以同样的方式推导得到 $\psi^* = \psi^* P^t$ 对于所有 $t \ge 0$ 成立。 -This tells us an important fact: If the distribution of $\psi_0$ is a stationary distribution, then $\psi_t$ will have this same distribution for all $t \ge 0$. +这告诉我们一个重要的事实:如果分布 $\psi_0$ 是平稳分布,那么对于所有 $t \ge 0$,$\psi_t$ 将具有相同的分布。 -The following theorem is proved in Chapter 4 of {cite}`sargent2023economic` and numerous other sources. +以下定理在 {cite}`sargent2023economic` 的第 4 章及其他许多来源中得到了证明。 ```{prf:theorem} :label: unique_stat -Every stochastic matrix $P$ has at least one stationary distribution. +每个随机矩阵 $P$ 至少有一个平稳分布。 ``` -Note that there can be many stationary distributions corresponding to a given -stochastic matrix $P$. +请注意,对于给定的随机矩阵 $P$,可能存在多个平稳分布。 -* For example, if $P$ is the identity matrix, then all distributions on $S$ are stationary. +* 例如,如果 $P$ 是单位矩阵,那么 $S$ 上的所有分布都是平稳的。 -To get uniqueness, we need the Markov chain to "mix around," so that the state -doesn't get stuck in some part of the state space. +为了获得唯一性,我们需要马尔科夫链“混合”,以便状态不会卡在状态空间的某一部分。 -This gives some intuition for the following theorem. +这为以下定理提供了一些直觉。 ```{prf:theorem} :label: mc_po_conv_thm -If $P$ is everywhere positive, then $P$ has exactly one stationary -distribution. +如果 $P$ 是各处正的,那么 $P$ 恰好有一个平稳分布。 ``` -We will come back to this when we introduce irreducibility in the {doc}`next lecture ` on Markov chains. +我们将在 {doc}`下一讲 ` 中引入不可约性时回到这一点。 -### Example +### 示例 -Recall our model of the employment/unemployment dynamics of a particular worker {ref}`discussed above `. +回顾我们之前讨论的关于特定工人的就业/失业动态的模型 {ref}`在上面讨论过的 `。 -If $\alpha \in (0,1)$ and $\beta \in (0,1)$, then the transition matrix is everywhere positive. +如果 $\alpha \in (0,1)$ 且 $\beta \in (0,1)$,则转移矩阵在各处为正。 -Let $\psi^* = (p, 1-p)$ be the stationary distribution, so that $p$ -corresponds to unemployment (state 0). +设 $\psi^* = (p, 1-p)$ 为平稳分布,其中 $p$ 对应失业(状态 0)。 -Using $\psi^* = \psi^* P$ and a bit of algebra yields +使用 $\psi^* = \psi^* P$ 和一些代数推导得到 $$ p = \frac{\beta}{\alpha + \beta} $$ -This is, in some sense, a steady state probability of unemployment. - -Not surprisingly it tends to zero as $\beta \to 0$, and to one as $\alpha \to 0$. +从某种意义上说,这是失业的稳态概率。 +不出所料,当 $\beta \to 0$ 时它趋于 0,当 $\alpha \to 0$ 时它趋于 1。 +### 计算平稳分布 +[QuantEcon.py](http://quantecon.org/quantecon-py) 实现了计算平稳分布的稳定算法。 -### Calculating stationary distributions - -A stable algorithm for computing stationary distributions is implemented in [QuantEcon.py](http://quantecon.org/quantecon-py). - -Here's an example +这里是一个示例 ```{code-cell} ipython3 P = [[0.4, 0.6], [0.2, 0.8]] mc = qe.MarkovChain(P) -mc.stationary_distributions # Show all stationary distributions +mc.stationary_distributions # 显示所有平稳分布 ``` +### 渐进平稳性 +考虑一个处处为正的随机矩阵,具有唯一的平稳分布 $\psi^*$。 +有时,无论初始分布 $\psi_0$ 如何,$\psi_t = \psi_0 P^t$ 都会收敛到 $\psi^*$。 - - -### Asymptotic stationarity - -Consider an everywhere positive stochastic matrix with unique stationary distribution $\psi^*$. - -Sometimes the distribution $\psi_t = \psi_0 P^t$ of $X_t$ converges to $\psi^*$ regardless of $\psi_0$. - -For example, we have the following result +例如,我们有以下结果 (strict_stationary)= ```{prf:theorem} :label: mc_gs_thm -If there exists an integer $m$ such that all entries of $P^m$ are -strictly positive, then +如果存在一个整数 $m$,使得 $P^m$ 的所有元素都为严格正数,则 $$ \psi_0 P^t \to \psi^* - \quad \text{ as } t \to \infty + \quad \text{ 当 } t \to \infty 时 $$ -where $\psi^*$ is the unique stationary distribution. +其中 $\psi^*$ 是唯一的平稳分布。 ``` -This situation is often referred to as **asymptotic stationarity** or **global stability**. - -A proof of the theorem can be found in Chapter 4 of {cite}`sargent2023economic`, as well as many other sources. +这种情况通常称为**渐进平稳性**或**全局稳定性**。 +该定理的证明可以在 {cite}`sargent2023economic` 的第4章及许多其他来源中找到。 (hamilton)= -#### Example: Hamilton's chain +#### 示例:汉密尔顿链 -Hamilton's chain satisfies the conditions of the theorem because $P^2$ is everywhere positive: +汉密尔顿链满足定理的条件,因为 $P^2$ 处处为正: ```{code-cell} ipython3 P = np.array([[0.971, 0.029, 0.000], @@ -812,9 +741,9 @@ P = np.array([[0.971, 0.029, 0.000], P @ P ``` -Let's pick an initial distribution $\psi_1, \psi_2, \psi_3$ and trace out the sequence of distributions $\psi_i P^t$ for $t = 0, 1, 2, \ldots$, for $i=1, 2, 3$. +我们选择初始分布 $\psi_1, \psi_2, \psi_3$,并绘制出分布序列 $\psi_i P^t$,对于 $t = 0, 1, 2, \ldots$,以及 $i=1, 2, 3$。 -First, we write a function to iterate the sequence of distributions for `ts_length` period +首先,我们编写一个函数,用于迭代分布序列,持续 `ts_length` 个时间段 ```{code-cell} ipython3 def iterate_ψ(ψ_0, P, ts_length): @@ -826,20 +755,20 @@ def iterate_ψ(ψ_0, P, ts_length): return ψ_t ``` -Now we plot the sequence +现在我们绘制序列 ```{code-cell} ipython3 :tags: [hide-input] ψ_1 = (0.0, 0.0, 1.0) ψ_2 = (1.0, 0.0, 0.0) -ψ_3 = (0.0, 1.0, 0.0) # Three initial conditions -colors = ['blue','red', 'green'] # Different colors for each initial point +ψ_3 = (0.0, 1.0, 0.0) # 三个初始条件 +colors = ['blue','red', 'green'] # 不同颜色表示不同的初始点 -# Define the vertices of the unit simplex +# 定义单位单纯形的顶点 v = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0]]) -# Define the faces of the unit simplex +# 定义单位单纯形的面 faces = [ [v[0], v[1], v[2]], [v[0], v[1], v[3]], @@ -877,22 +806,22 @@ plt.close() HTML(anim.to_jshtml()) ``` -Here +在这里 -* $P$ is the stochastic matrix for recession and growth {ref}`considered above `. -* The red, blue and green dots are initial marginal probability distributions $\psi_1, \psi_2, \psi_3$, each of which is represented as a vector in $\mathbb R^3$. -* The transparent dots are the marginal distributions $\psi_i P^t$ for $t = 1, 2, \ldots$, for $i=1,2,3.$. -* The yellow dot is $\psi^*$. +* $P$ 是 {ref}`之前讨论的` 衰退和增长的随机矩阵。 +* 红色、蓝色和绿色的点是初始边际概率分布 $\psi_1, \psi_2, \psi_3$,它们分别表示为 $\mathbb R^3$ 中的向量。 +* 透明点是边际分布 $\psi_i P^t$ 对于 $t = 1, 2, \ldots$,$i=1,2,3.$。 +* 黄色点是 $\psi^*$。 -You might like to try experimenting with different initial conditions. +你可以尝试不同的初始条件来进行实验。 -#### Example: failure of convergence +#### 示例:收敛失败 -Consider the periodic chain with stochastic matrix +考虑一个具有以下随机矩阵的周期链 $$ P = @@ -902,21 +831,20 @@ P = \end{bmatrix} $$ -This matrix does not satisfy the conditions of -{ref}`strict_stationary` because, as you can readily check, +该矩阵不满足 {ref}`strict_stationary` 的条件,因为很容易检查到: -* $P^m = P$ when $m$ is odd and -* $P^m = I$, the identity matrix, when $m$ is even. +* 当 $m$ 为奇数时,$P^m = P$ +* 当 $m$ 为偶数时,$P^m = I$,即单位矩阵。 -Hence there is no $m$ such that all elements of $P^m$ are strictly positive. +因此,没有 $m$ 使得 $P^m$ 的所有元素都为严格正数。 -Moreover, we can see that global stability does not hold. +此外,我们可以看到,全局稳定性并不成立。 -For instance, if we start at $\psi_0 = (1,0)$, then $\psi_m = \psi_0 P^m$ is $(1, 0)$ when $m$ is even and $(0,1)$ when $m$ is odd. +例如,如果我们从 $\psi_0 = (1,0)$ 开始,则 $\psi_m = \psi_0 P^m$ 当 $m$ 为偶数时是 $(1, 0)$,当 $m$ 为奇数时是 $(0,1)$。 -We can see similar phenomena in higher dimensions. +我们可以在更高维度中看到类似的现象。 -The next figure illustrates this for a periodic Markov chain with three states. +下图展示了具有三个状态的周期性马尔科夫链。 ```{code-cell} ipython3 :tags: [hide-input] @@ -932,12 +860,12 @@ P = np.array([[0.0, 1.0, 0.0], fig = plt.figure() ax = fig.add_subplot(projection='3d') -colors = ['red','yellow', 'green', 'blue'] # Different colors for each initial point +colors = ['red','yellow', 'green', 'blue'] # 不同颜色表示不同的初始点 -# Define the vertices of the unit simplex +# 定义单位单纯形的顶点 v = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0]]) -# Define the faces of the unit simplex +# 定义单位单纯形的面 faces = [ [v[0], v[1], v[2]], [v[0], v[1], v[3]], @@ -952,7 +880,7 @@ def update(n): ax.set_zlim([0, 1]) ax.view_init(45, 45) - # Plot the 3D unit simplex as planes + # 绘制 3D 单纯形作为平面 simplex = Poly3DCollection(faces,alpha=0.05) ax.add_collection3d(simplex) @@ -970,24 +898,24 @@ anim = FuncAnimation(fig, update, frames=range(20), blit=False, repeat=False) plt.close() HTML(anim.to_jshtml()) ``` -This animation demonstrates the behavior of an irreducible and periodic stochastic matrix. -The red, yellow, and green dots represent different initial probability distributions. +该动画展示了一个不可约但具有周期性的随机矩阵的行为。 -The blue dot represents the unique stationary distribution. +红色、黄色和绿色的点表示不同的初始概率分布。 -Unlike Hamilton’s Markov chain, these initial distributions do not converge to the unique stationary distribution. +蓝色点表示唯一的平稳分布。 -Instead, they cycle periodically around the probability simplex, illustrating that asymptotic stability fails. +与汉密尔顿的马尔科夫链不同,这些初始分布不会收敛到唯一的平稳分布。 +相反,它们周期性地在概率单纯形上循环,说明了渐进稳定性失败的情况。 (finite_mc_expec)= -## Computing expectations +## 计算期望 -```{index} single: Markov Chains; Forecasting Future Values +```{index} single: 马尔科夫链; 预测未来值 ``` -We sometimes want to compute mathematical expectations of functions of $X_t$ of the form +我们有时想要计算形式为 $X_t$ 的函数的数学期望 ```{math} :label: mc_une @@ -995,7 +923,7 @@ We sometimes want to compute mathematical expectations of functions of $X_t$ o \mathbb E [ h(X_t) ] ``` -and conditional expectations such as +以及条件期望,例如 ```{math} :label: mc_cce @@ -1003,11 +931,10 @@ and conditional expectations such as \mathbb E [ h(X_{t + k}) \mid X_t = x] ``` -where +其中 -* $\{X_t\}$ is a Markov chain generated by $n \times n$ stochastic matrix $P$. -* $h$ is a given function, which, in terms of matrix - algebra, we'll think of as the column vector +* $\{X_t\}$ 是由 $n \times n$ 随机矩阵 $P$ 生成的马尔科夫链。 +* $h$ 是给定的函数,我们在矩阵代数的意义上将其视为列向量 $$ h = @@ -1018,30 +945,27 @@ h = \end{bmatrix}. $$ -Computing the unconditional expectation {eq}`mc_une` is easy. +计算无条件期望 {eq}`mc_une` 非常简单。 - -We just sum over the marginal distribution of $X_t$ to get +我们只需对 $X_t$ 的边际分布进行求和,得到 $$ \mathbb E [ h(X_t) ] = \sum_{x \in S} (\psi P^t)(x) h(x) $$ -Here $\psi$ is the distribution of $X_0$. +这里 $\psi$ 是 $X_0$ 的分布。 -Since $\psi$ and hence $\psi P^t$ are row vectors, we can also -write this as +由于 $\psi$ 和 $\psi P^t$ 都是行向量,我们也可以将其写成 $$ \mathbb E [ h(X_t) ] = \psi P^t h $$ -For the conditional expectation {eq}`mc_cce`, we need to sum over -the conditional distribution of $X_{t + k}$ given $X_t = x$. +对于条件期望 {eq}`mc_cce`,我们需要对 $X_{t + k}$ 给定 $X_t = x$ 的条件分布进行求和。 -We already know that this is $P^k(x, \cdot)$, so +我们已经知道这是 $P^k(x, \cdot)$,因此 ```{math} :label: mc_cce2 @@ -1050,12 +974,11 @@ We already know that this is $P^k(x, \cdot)$, so = (P^k h)(x) ``` -### Expectations of geometric sums +### 几何和的期望 -Sometimes we want to compute the mathematical expectation of a geometric sum, such as -$\sum_t \beta^t h(X_t)$. +有时我们想要计算几何和的数学期望,例如 $\sum_t \beta^t h(X_t)$。 -In view of the preceding discussion, this is +根据前面的讨论,这是 $$ \mathbb{E} @@ -1066,19 +989,19 @@ $$ = x + \beta (Ph)(x) + \beta^2 (P^2 h)(x) + \cdots $$ -By the {ref}`Neumann series lemma `, this sum can be calculated using +根据 {ref}`Neumann 级数引理 `,该和可以使用以下公式计算 $$ I + \beta P + \beta^2 P^2 + \cdots = (I - \beta P)^{-1} $$ -The vector $P^k h$ stores the conditional expectation $\mathbb E [ h(X_{t + k}) \mid X_t = x]$ over all $x$. +向量 $P^k h$ 储存了所有 $x$ 的条件期望 $\mathbb E [ h(X_{t + k}) \mid X_t = x]$。 ```{exercise} :label: mc1_ex_1 -Imam and Temple {cite}`imampolitical` used a three-state transition matrix to describe the transition of three states of a regime: growth, stagnation, and collapse +Imam 和 Temple {cite}`imampolitical` 使用了一个三状态转移矩阵来描述政权的三种状态:增长、停滞和崩溃 $$ P := @@ -1089,22 +1012,22 @@ P := \end{bmatrix} $$ -where rows, from top to down, correspond to growth, stagnation, and collapse. +其中从上到下的行分别对应增长、停滞和崩溃。 -In this exercise, +在本练习中, -1. visualize the transition matrix and show this process is asymptotically stationary -1. calculate the stationary distribution using simulations -1. visualize the dynamics of $(\psi_0 P^t)(i)$ where $t \in 0, ..., 25$ and compare the convergent path with the previous transition matrix +1. 可视化转移矩阵,并证明该过程是渐进平稳的 +1. 使用模拟计算平稳分布 +1. 可视化 $(\psi_0 P^t)(i)$ 的动态过程,其中 $t \in 0, ..., 25$,并将收敛路径与之前的转移矩阵进行比较 -Compare your solution to the paper. +将您的解答与论文进行比较。 ``` ```{solution-start} mc1_ex_1 :class: dropdown ``` -Solution 1: +解答 1: ```{image} /_static/lecture_specific/markov_chains_I/Temple.png :name: mc_temple @@ -1112,11 +1035,11 @@ Solution 1: ``` -Since the matrix is everywhere positive, there is a unique stationary distribution $\psi^*$ such that $\psi_t\to \psi^*$ as $t\to \infty$. +由于矩阵处处为正,因此存在唯一的平稳分布 $\psi^*$,使得 $\psi_t\to \psi^*$ 随 $t\to \infty$。 -Solution 2: +解答 2: -One simple way to calculate the stationary distribution is to take the power of the transition matrix as we have shown before +一种简单的方法是计算转移矩阵的幂,如我们之前所示 ```{code-cell} ipython3 P = np.array([[0.68, 0.12, 0.20], @@ -1126,7 +1049,7 @@ P_power = np.linalg.matrix_power(P, 20) P_power ``` -Note that rows of the transition matrix converge to the stationary distribution. +注意,转移矩阵的行收敛到平稳分布。 ```{code-cell} ipython3 ψ_star_p = P_power[0] @@ -1146,7 +1069,7 @@ mc = qe.MarkovChain(P) ````{exercise} :label: mc1_ex_2 -We discussed the six-state transition matrix estimated by Imam & Temple {cite}`imampolitical` [before](mc_eg3). +我们之前 [讨论过](mc_eg3) 的 Imam & Temple {cite}`imampolitical` 估计的六状态转移矩阵。 ```python nodes = ['DG', 'DC', 'NG', 'NC', 'AG', 'AC'] @@ -1158,20 +1081,20 @@ P = [[0.86, 0.11, 0.03, 0.00, 0.00, 0.00], [0.00, 0.00, 0.09, 0.15, 0.26, 0.50]] ``` -In this exercise, +在本练习中, -1. show this process is asymptotically stationary without simulation -2. simulate and visualize the dynamics starting with a uniform distribution across states (each state will have a probability of 1/6) -3. change the initial distribution to P(DG) = 1, while all other states have a probability of 0 +1. 不使用模拟,证明该过程是渐进平稳的 +2. 模拟并可视化从各状态均匀分布开始的动态(每个状态的概率为 1/6) +3. 将初始分布更改为 P(DG) = 1,其他所有状态的概率为 0 ```` ```{solution-start} mc1_ex_2 :class: dropdown ``` -Solution 1: +解答 1: -Although $P$ is not every positive, $P^m$ when $m=3$ is everywhere positive. +虽然 $P$ 不是处处为正的,但当 $m=3$ 时,$P^m$ 是处处为正的。 ```{code-cell} ipython3 P = np.array([[0.86, 0.11, 0.03, 0.00, 0.00, 0.00], @@ -1184,24 +1107,24 @@ P = np.array([[0.86, 0.11, 0.03, 0.00, 0.00, 0.00], np.linalg.matrix_power(P,3) ``` -So it satisfies the requirement. +因此它满足要求。 -Solution 2: +解答 2: -We find the distribution $\psi$ converges to the stationary distribution quickly regardless of the initial distributions +无论初始分布如何,我们发现分布 $\psi$ 很快收敛到平稳分布。 ```{code-cell} ipython3 ts_length = 30 num_distributions = 20 nodes = ['DG', 'DC', 'NG', 'NC', 'AG', 'AC'] -# Get parameters of transition matrix +# 获取转移矩阵的参数 n = len(P) mc = qe.MarkovChain(P) ψ_star = mc.stationary_distributions[0] ψ_0 = np.array([[1/6 for i in range(6)], [0 if i != 0 else 1 for i in range(6)]]) -## Draw the plot +## 绘制图像 fig, axes = plt.subplots(ncols=2) plt.subplots_adjust(wspace=0.35) for idx in range(2): @@ -1222,32 +1145,27 @@ plt.show() ```{exercise} :label: mc1_ex_3 -Prove the following: If $P$ is a stochastic matrix, then so is the $k$-th -power $P^k$ for all $k \in \mathbb N$. +证明以下命题:如果 $P$ 是一个随机矩阵,那么对于所有 $k \in \mathbb N$,$P^k$ 也是随机矩阵。 ``` - ```{solution-start} mc1_ex_3 :class: dropdown ``` -Suppose that $P$ is stochastic and, moreover, that $P^k$ is -stochastic for some integer $k$. +假设 $P$ 是随机矩阵,且 $P^k$ 是某个整数 $k$ 的随机矩阵。 -We will prove that $P^{k+1} = P P^k$ is also stochastic. +我们将证明 $P^{k+1} = P P^k$ 也是随机矩阵。 -(We are doing proof by induction --- we assume the claim is true at $k$ and -now prove it is true at $k+1$.) +(我们正在进行归纳证明——假设在 $k$ 时命题为真,并证明在 $k+1$ 时命题也为真。) -To see this, observe that, since $P^k$ is stochastic and the product of -nonnegative matrices is nonnegative, $P^{k+1} = P P^k$ is nonnegative. +要看到这一点,注意到,由于 $P^k$ 是随机矩阵且非负矩阵的乘积是非负的,因此 $P^{k+1} = P P^k$ 是非负的。 -Also, if $\mathbf 1$ is a column vector of ones, then, since $P^k$ is stochastic we -have $P^k \mathbf 1 = \mathbf 1$ (rows sum to one). +此外,如果 $\mathbf 1$ 是全 1 列向量,那么由于 $P^k$ 是随机矩阵,我们有 $P^k \mathbf 1 = \mathbf 1$ (行和为一)。 -Therefore $P^{k+1} \mathbf 1 = P P^k \mathbf 1 = P \mathbf 1 = \mathbf 1$ +因此 $P^{k+1} \mathbf 1 = P P^k \mathbf 1 = P \mathbf 1 = \mathbf 1$ -The proof is done. +证明完毕。 ```{solution-end} ``` + diff --git a/lectures/markov_chains_II.md b/lectures/markov_chains_II.md index fe2ad9b..f29a02b 100644 --- a/lectures/markov_chains_II.md +++ b/lectures/markov_chains_II.md @@ -11,12 +11,12 @@ kernelspec: name: python3 --- -# Markov Chains: Irreducibility and Ergodicity +# 马尔科夫链:不可约性与遍历性 -```{index} single: Markov Chains: Irreducibility and Ergodicity +```{index} single: 马尔科夫链: 不可约性与遍历性 ``` -In addition to what's in Anaconda, this lecture will need the following libraries: +除了 Anaconda 中的库外,这个讲座还需要以下库: ```{code-cell} ipython3 :tags: [hide-output] @@ -24,27 +24,24 @@ In addition to what's in Anaconda, this lecture will need the following librarie !pip install quantecon ``` -## Overview +## 概述 -This lecture continues on from our {doc}`earlier lecture on Markov chains -`. +本讲座是我们{doc}`早期关于马尔科夫链的讲座 `的延续。 +具体来说,我们将介绍不可约性和遍历性的概念,并了解它们与平稳性的联系。 -Specifically, we will introduce the concepts of irreducibility and ergodicity, and see how they connect to stationarity. +不可约性描述了马尔科夫链在系统中移动任意两个状态之间的能力。 -Irreducibility describes the ability of a Markov chain to move between any two states in the system. +遍历性是一种样本路径属性,描述了系统在长时间内的行为。 -Ergodicity is a sample path property that describes the behavior of the system over long periods of time. +正如我们将看到的: -As we will see, +* 不可约的马尔科夫链保证存在唯一的平稳分布,而 +* 遍历的马尔科夫链生成满足大数定律版本的时间序列。 -* an irreducible Markov chain guarantees the existence of a unique stationary distribution, while -* an ergodic Markov chain generates time series that satisfy a version of the - law of large numbers. +这些概念一起为理解马尔科夫链的长期行为提供了基础。 -Together, these concepts provide a foundation for understanding the long-term behavior of Markov chains. - -Let's start with some standard imports: +让我们从一些标准导入开始: ```{code-cell} ipython3 import matplotlib.pyplot as plt @@ -53,38 +50,31 @@ import numpy as np ``` (mc_irreducible)= -## Irreducibility - +## 不可约性 -To explain irreducibility, let's take $P$ to be a fixed stochastic matrix. +为了解释不可约性,让我们设 $P$ 为一个固定的随机矩阵。 -State $x$ is called **accessible** (or **reachable**) from state $y$ if $P^t(x,y)>0$ for some integer $t\ge 0$. +如果对于某个整数 $t\ge 0$,$P^t(x,y)>0$,则称状态 $x$ 对状态 $y$ 是**可达的**。 -Two states, $x$ and $y$, are said to **communicate** if $x$ and $y$ are accessible from each other. +当状态 $x$ 和 $y$ 彼此可达时,称它们**相互沟通**。 -In view of our discussion {ref}`above `, this means precisely -that +根据我们{ref}`前面讨论的 `,这正意味着 -* state $x$ can eventually be reached from state $y$, and -* state $y$ can eventually be reached from state $x$ +* 状态 $x$ 最终可以从状态 $y$ 到达,且 +* 状态 $y$ 最终可以从状态 $x$ 到达。 -The stochastic matrix $P$ is called **irreducible** if all states communicate; -that is, if $x$ and $y$ communicate for all $(x, y)$ in $S \times S$. +如果所有状态都相互沟通,则称随机矩阵 $P$ 是**不可约的**,即对于所有 $(x, y)$ 在 $S \times S$ 中,$x$ 和 $y$ 相互沟通。 ```{prf:example} :label: mc2_ex_ir -For example, consider the following transition probabilities for wealth of a -fictitious set of households +例如,考虑以下一组虚构家庭的财富转移概率。 ```{image} /_static/lecture_specific/markov_chains_II/Irre_1.png :name: mc_irre1 :align: center ``` - - -We can translate this into a stochastic matrix, putting zeros where -there's no edge between nodes +我们可以将其转化为一个随机矩阵,在没有节点之间边的地方填充零。 $$ P := @@ -95,11 +85,10 @@ P := \end{bmatrix} $$ -It's clear from the graph that this stochastic matrix is irreducible: we can eventually -reach any state from any other state. +从图中可以看出,这个随机矩阵是不可约的:我们可以最终从任何状态到达任何其他状态。 ``` -We can also test this using [QuantEcon.py](http://quantecon.org/quantecon-py)'s MarkovChain class +我们还可以使用 [QuantEcon.py](http://quantecon.org/quantecon-py) 的 `MarkovChain` 类来测试这一点: ```{code-cell} ipython3 P = [[0.9, 0.1, 0.0], @@ -113,18 +102,17 @@ mc.is_irreducible ```{prf:example} :label: mc2_ex_pf -Here's a more pessimistic scenario in which poor people remain poor forever +这是一个更加悲观的情景,贫困者永远保持贫困。 ```{image} /_static/lecture_specific/markov_chains_II/Irre_2.png :name: mc_irre2 :align: center ``` -This stochastic matrix is not irreducible since, for example, rich is not -accessible from poor. +这个随机矩阵不是不可约的,因为例如,富有状态无法从贫困状态到达。 ``` -Let's confirm this +让我们确认这一点: ```{code-cell} ipython3 P = [[1.0, 0.0, 0.0], @@ -135,120 +123,106 @@ mc = qe.MarkovChain(P, ('poor', 'middle', 'rich')) mc.is_irreducible ``` -It might be clear to you already that irreducibility is going to be important -in terms of long-run outcomes. +你可能已经明白,不可约性在长期结果中非常重要。 -For example, poverty is a life sentence in the second graph but not the first. +例如,在第二个图中,贫困是一种终身的困境,但在第一个图中不是。 -We'll come back to this a bit later. +我们稍后会回到这个问题。 -### Irreducibility and stationarity +### 不可约性与平稳性 -We discussed uniqueness of stationary distributions in our earlier lecture {doc}`markov_chains_I`. +我们在之前的讲座 {doc}`markov_chains_I` 中讨论了平稳分布的唯一性。 -There we {prf:ref}`stated ` that uniqueness holds when the transition matrix is everywhere positive. +我们{prf:ref}`指出 `,当转移矩阵处处为正时,唯一性成立。 -In fact irreducibility is sufficient: +事实上,不可约性就足够了: ```{prf:theorem} :label: mc_conv_thm -If $P$ is irreducible, then $P$ has exactly one stationary -distribution. +如果 $P$ 是不可约的,那么 $P$ 只有一个平稳分布。 ``` -For proof, see Chapter 4 of {cite}`sargent2023economic` or -Theorem 5.2 of {cite}`haggstrom2002finite`. - +有关证明,请参见 {cite}`sargent2023economic` 的第4章或 {cite}`haggstrom2002finite` 的定理5.2。 (ergodicity)= -## Ergodicity - +## 遍历性 -Under irreducibility, yet another important result obtains: +在不可约性下,还可以得到另一个重要结果: ````{prf:theorem} :label: stationary -If $P$ is irreducible and $\psi^*$ is the unique stationary -distribution, then, for all $x \in S$, +如果 $P$ 是不可约的,并且 $\psi^*$ 是唯一的平稳分布,那么对于所有 $x \in S$, ```{math} :label: llnfmc0 \frac{1}{m} \sum_{t = 1}^m \mathbb{1}\{X_t = x\} \to \psi^*(x) - \quad \text{as } m \to \infty + \quad \text{当 } m \to \infty ``` ```` -Here +这里 -* $\{X_t\}$ is a Markov chain with stochastic matrix $P$ and initial distribution $\psi_0$ +* $\{X_t\}$ 是具有随机矩阵 $P$ 和初始分布 $\psi_0$ 的马尔科夫链。 -* $\mathbb{1} \{X_t = x\} = 1$ if $X_t = x$ and zero otherwise. +* $\mathbb{1} \{X_t = x\} = 1$ 当且仅当 $X_t = x$,否则为0。 -The result in [theorem 4.3](llnfmc0) is sometimes called **ergodicity**. +定理[4.3](llnfmc0)中的结果有时称为**遍历性**。 -The theorem tells us that the fraction of time the chain spends at state $x$ -converges to $\psi^*(x)$ as time goes to infinity. +该定理告诉我们,随着时间趋于无穷大,链花费在状态 $x$ 的时间比例收敛到 $\psi^*(x)$。 (new_interp_sd)= -This gives us another way to interpret the stationary distribution (provided irreducibility holds). +这为我们提供了另一种解释平稳分布的方法(假设不可约性成立)。 -Importantly, the result is valid for any choice of $\psi_0$. +重要的是,这一结果对于任何 $\psi_0$ 都有效。 -The theorem is related to {doc}`the law of large numbers `. - -It tells us that, in some settings, the law of large numbers sometimes holds even when the -sequence of random variables is [not IID](iid_violation). +该定理与{doc}`大数定律 `相关。 +它告诉我们,在某些设置中,即使随机变量序列[不是独立同分布](iid_violation),大数定律有时也成立。 (mc_eg1-2)= -### Example: ergodicity and unemployment +### 示例:遍历性与失业 -Recall our cross-sectional interpretation of the employment/unemployment model {ref}`discussed before `. +回顾我们关于就业/失业模型的截面解释{ref}`之前讨论过 `。 -Assume that $\alpha \in (0,1)$ and $\beta \in (0,1)$, so that irreducibility holds. +假设 $\alpha \in (0,1)$ 且 $\beta \in (0,1)$,因此不可约性成立。 -We saw that the stationary distribution is $(p, 1-p)$, where +我们看到平稳分布是 $(p, 1-p)$,其中 $$ p = \frac{\beta}{\alpha + \beta} $$ -In the cross-sectional interpretation, this is the fraction of people unemployed. - -In view of our latest (ergodicity) result, it is also the fraction of time that a single worker can expect to spend unemployed. +在截面解释中,这是失业人员的比例。 -Thus, in the long run, cross-sectional averages for a population and time-series averages for a given person coincide. +根据我们最新的遍历性结果,这也是单个工人预期花费在失业状态的时间比例。 -This is one aspect of the concept of ergodicity. +因此,从长远来看,人口的截面平均值和单个个体的时间序列平均值是一致的。 +这是遍历性概念的一个方面。 (ergo)= -### Example: Hamilton dynamics +### 示例:汉密尔顿动力学 -Another example is the Hamilton dynamics we {ref}`discussed before `. +另一个示例是我们{ref}`之前讨论过的 `汉密尔顿动力学。 -Let $\{X_t\}$ be a sample path generated by these dynamics. +让 $\{X_t\}$ 是由这些动力学生成的样本路径。 -Let's denote the fraction of time spent in state $x$ over the period $t=1, -\ldots, n$ by $\hat p_n(x)$, so that +令在时间段 $t=1, \ldots, n$ 内花费在状态 $x$ 上的时间比例为 $\hat p_n(x)$,则有 $$ \hat p_n(x) := \frac{1}{n} \sum_{t = 1}^n \mathbb{1}\{X_t = x\} \qquad (x \in \{0, 1, 2\}) $$ +马尔科夫链的{ref}`图 `表明它是不可约的,因此遍历性成立。 -The {ref}`graph ` of the Markov chain shows it is irreducible, so -ergodicity holds. - -Hence we expect that $\hat p_n(x) \approx \psi^*(x)$ when $n$ is large. +因此,我们期望当 $n$ 较大时,$\hat p_n(x) \approx \psi^*(x)$。 -The next figure shows convergence of $\hat p_n(x)$ to $\psi^*(x)$ when $x=1$ and -$X_0$ is either $0, 1$ or $2$. +下图显示了当 $x=1$ 并且 $X_0$ 分别为 $0, 1$ 或 $2$ 时,$\hat p_n(x)$ 向 $\psi^*(x)$ 的收敛情况。 ```{code-cell} ipython3 P = np.array([[0.971, 0.029, 0.000], @@ -257,32 +231,32 @@ P = np.array([[0.971, 0.029, 0.000], ts_length = 10_000 mc = qe.MarkovChain(P) ψ_star = mc.stationary_distributions[0] -x = 1 # We study convergence to psi^*(x) +x = 1 # 我们研究 psi^*(x) 的收敛情况 fig, ax = plt.subplots() ax.axhline(ψ_star[x], linestyle='dashed', color='black', label = fr'$\psi^*({x})$') -# Compute the fraction of time spent in state 0, starting from different x_0s +# 计算花费在状态0的时间比例,从不同的x_0开始 for x0 in range(len(P)): X = mc.simulate(ts_length, init=x0) p_hat = (X == x).cumsum() / np.arange(1, ts_length+1) - ax.plot(p_hat, label=fr'$\hat p_n({x})$ when $X_0 = \, {x0}$') + ax.plot(p_hat, label=fr'$\hat p_n({x})$ 当 $X_0 = \, {x0}$') ax.set_xlabel('t') ax.set_ylabel(fr'$\hat p_n({x})$') ax.legend() plt.show() ``` -You might like to try changing $x=1$ to either $x=0$ or $x=2$. +你可能想尝试将 $x=1$ 改为 $x=0$ 或 $x=2$。 -In any of these cases, ergodicity will hold. +在这些情况下,遍历性都会成立。 -### Example: a periodic chain +### 示例:一个周期链 -```{prf:example} +````{prf:example} :label: mc2_ex_pc -Let's look at the following example with states 0 and 1: +让我们来看以下状态0和1的例子: $$ P := @@ -292,22 +266,21 @@ P := \end{bmatrix} $$ - -The transition graph shows that this model is irreducible. +转移图表明该模型是不可约的。 ```{image} /_static/lecture_specific/markov_chains_II/example4.png :name: mc_example4 :align: center ``` -Notice that there is a periodic cycle --- the state cycles between the two states in a regular way. -``` -Not surprisingly, this property -is called [periodicity](https://stats.libretexts.org/Bookshelves/Probability_Theory/Probability_Mathematical_Statistics_and_Stochastic_Processes_(Siegrist)/16%3A_Markov_Processes/16.05%3A_Periodicity_of_Discrete-Time_Chains). +请注意,这里有一个周期循环——状态以规则的方式在两个状态之间循环。 +```` + +毫不奇怪,这种属性被称为[周期性](https://stats.libretexts.org/Bookshelves/Probability_Theory/Probability_Mathematical_Statistics_and_Stochastic_Processes_(Siegrist)/16%3A_Markov_Processes/16.05%3A_Periodicity_of_Discrete-Time_Chains)。 -Nonetheless, the model is irreducible, so ergodicity holds. +尽管如此,该模型是不可约的,因此遍历性成立。 -The following figure illustrates +以下图表进行了说明: ```{code-cell} ipython3 P = np.array([[0, 1], @@ -324,9 +297,9 @@ for i in range(n): axes[i].set_xlabel('t') axes[i].set_ylabel(fr'$\hat p_n({i})$') - # Compute the fraction of time spent, for each x + # 计算每个 x 花费的时间比例 for x0 in range(n): - # Generate time series starting at different x_0 + # 从不同的 x_0 生成时间序列 X = mc.simulate(ts_length, init=x0) p_hat = (X == i).cumsum() / np.arange(1, ts_length+1) axes[i].plot(p_hat, label=f'$x_0 = \, {x0} $') @@ -336,18 +309,15 @@ plt.tight_layout() plt.show() ``` -This example helps to emphasize that asymptotic stationarity is about the distribution, while ergodicity is about the sample path. +该示例帮助强调了渐近平稳性是关于分布的,而遍历性是关于样本路径的。 -The proportion of time spent in a state can converge to the stationary distribution with periodic chains. +在周期性链中,花费在某个状态的时间比例可以收敛到平稳分布。然而,每个状态的分布却不会收敛。 -However, the distribution at each state does not. +### 示例:政治制度 -### Example: political institutions +让我们回到{ref}`前一讲中讨论的 `具有六个状态的政治制度模型,并研究其遍历性。 -Let's go back to the political institutions model with six states discussed {ref}`in a previous lecture ` and study ergodicity. - - -Here's the transition matrix. +以下是转移矩阵: $$ P := @@ -361,13 +331,11 @@ $$ \end{bmatrix} $$ +链的{ref}`图 `显示所有状态都是可达的,表明该链是不可约的。 -The {ref}`graph ` for the chain shows all states are reachable, -indicating that this chain is irreducible. - -In the next figure, we visualize the difference $\hat p_n(x) - \psi^* (x)$ for each state $x$. +在下图中,我们可视化了每个状态 $x$ 的 $\hat p_n(x) - \psi^* (x)$ 差异。 -Unlike the previous figure, $X_0$ is held fixed. +与前一个图不同,$X_0$ 是固定的。 ```{code-cell} ipython3 P = [[0.86, 0.11, 0.03, 0.00, 0.00, 0.00], @@ -382,12 +350,11 @@ mc = qe.MarkovChain(P) ψ_star = mc.stationary_distributions[0] fig, ax = plt.subplots() X = mc.simulate(ts_length, random_state=1) -# Center the plot at 0 +# 将图中心对准0 ax.axhline(linestyle='dashed', lw=2, color='black') - for x0 in range(len(P)): - # Calculate the fraction of time for each state + # 计算每个状态的时间比例 p_hat = (X == x0).cumsum() / np.arange(1, ts_length+1) ax.plot(p_hat - ψ_star[x0], label=f'$x = {x0+1} $') ax.set_xlabel('t') @@ -397,12 +364,12 @@ ax.legend() plt.show() ``` -## Exercises +## 练习 ````{exercise} :label: mc_ex1 -Benhabib et al. {cite}`benhabib_wealth_2019` estimated that the transition matrix for social mobility as the following +Benhabib 等人 {cite}`benhabib_wealth_2019` 估计了如下的社会流动性转移矩阵: $$ P:= @@ -418,13 +385,13 @@ P:= \end{bmatrix} $$ -where each state 1 to 8 corresponds to a percentile of wealth shares +其中每个状态1到8对应于财富份额的百分位数: $$ 0-20 \%, 20-40 \%, 40-60 \%, 60-80 \%, 80-90 \%, 90-95 \%, 95-99 \%, 99-100 \% $$ -The matrix is recorded as `P` below +矩阵记录为 `P`,如下: ```python P = [ @@ -442,18 +409,18 @@ P = np.array(P) codes_B = ('1','2','3','4','5','6','7','8') ``` -1. Show this process is asymptotically stationary and calculate an approximation to the stationary distribution. +1. 展示该过程是渐近平稳的,并计算平稳分布的近似值。 -1. Use simulations to illustrate ergodicity. +2. 使用模拟来说明遍历性。 -```` +```` ```{solution-start} mc_ex1 :class: dropdown ``` -Part 1: +第1部分: -One option is to take the power of the transition matrix. +一种选择是对转移矩阵取幂。 ```{code-cell} ipython3 P = [[0.222, 0.222, 0.215, 0.187, 0.081, 0.038, 0.029, 0.006], @@ -471,8 +438,7 @@ codes_B = ('1','2','3','4','5','6','7','8') np.linalg.matrix_power(P, 10) ``` -For this model, rows of $P^n$ converge to the stationary distribution as $n \to -\infty$: +对于此模型,当 $n \to \infty$ 时,$P^n$ 的行收敛到平稳分布: ```{code-cell} ipython3 mc = qe.MarkovChain(P) @@ -480,7 +446,7 @@ mc = qe.MarkovChain(P) ψ_star ``` -Part 2: +第2部分: ```{code-cell} ipython3 ts_length = 1000 @@ -490,7 +456,7 @@ X = mc.simulate(ts_length, random_state=1) ax.axhline(linestyle='dashed', lw=2, color='black') for x0 in range(len(P)): - # Calculate the fraction of time for each worker + # 计算每个工人的时间比例 p_hat = (X == x0).cumsum() / np.arange(1, ts_length+1) ax.plot(p_hat - ψ_star[x0], label=f'$x = {x0+1} $') ax.set_xlabel('t') @@ -500,17 +466,15 @@ ax.legend() plt.show() ``` -Note that the fraction of time spent at each state converges to the probability -assigned to that state by the stationary distribution. +注意,花费在每个状态的时间比例收敛到平稳分布为该状态分配的概率。 ```{solution-end} ``` - ```{exercise} :label: mc_ex2 -According to the discussion {ref}`above `, if a worker's employment dynamics obey the stochastic matrix +根据{ref}`上述讨论 `,如果一个工人的就业动态遵循以下随机矩阵 $$ P := @@ -520,48 +484,41 @@ P := \end{bmatrix} $$ -with $\alpha \in (0,1)$ and $\beta \in (0,1)$, then, in the long run, the fraction -of time spent unemployed will be +其中 $\alpha \in (0,1)$ 且 $\beta \in (0,1)$,那么,从长远来看,失业的时间比例将为 $$ p := \frac{\beta}{\alpha + \beta} $$ -In other words, if $\{X_t\}$ represents the Markov chain for -employment, then $\bar X_m \to p$ as $m \to \infty$, where +换句话说,如果 $\{X_t\}$ 表示工人的马尔科夫链,那么 $\bar X_m \to p$ 当 $m \to \infty$,其中 $$ \bar X_m := \frac{1}{m} \sum_{t = 1}^m \mathbb{1}\{X_t = 0\} $$ -This exercise asks you to illustrate convergence by computing -$\bar X_m$ for large $m$ and checking that -it is close to $p$. +本练习要求您通过计算大 $m$ 时的 $\bar X_m$ 来说明收敛性,并检查其是否接近 $p$。 -You will see that this statement is true regardless of the choice of initial -condition or the values of $\alpha, \beta$, provided both lie in -$(0, 1)$. +您会看到无论初始条件或 $\alpha, \beta$ 的选择如何,只要它们都位于 $(0,1)$ 区间内,此结论都成立。 -The result should be similar to the plot we plotted [here](ergo) +结果应与我们在[这里](ergo)绘制的图类似。 ``` ```{solution-start} mc_ex2 :class: dropdown ``` -We will address this exercise graphically. +我们将以图形方式解决此练习。 -The plots show the time series of $\bar X_m - p$ for two initial -conditions. +图显示了 $\bar X_m - p$ 的时间序列,对于两个初始条件。 -As $m$ gets large, both series converge to zero. +当 $m$ 变大时,两个序列都收敛到零。 ```{code-cell} ipython3 α = β = 0.1 ts_length = 3000 p = β / (α + β) -P = ((1 - α, α), # Careful: P and p are distinct +P = ((1 - α, α), # 注意:P 和 p 是不同的 ( β, 1 - β)) mc = qe.MarkovChain(P) @@ -569,11 +526,11 @@ fig, ax = plt.subplots() ax.axhline(linestyle='dashed', lw=2, color='black') for x0 in range(len(P)): - # Generate time series for worker that starts at x0 + # 为每个从 x0 开始的工人生成时间序列 X = mc.simulate(ts_length, init=x0) - # Compute fraction of time spent unemployed, for each n + # 计算每个 n 的失业时间比例 X_bar = (X == 0).cumsum() / np.arange(1, ts_length+1) - # Plot + # 绘图 ax.plot(X_bar - p, label=f'$x_0 = \, {x0} $') ax.set_xlabel('t') ax.set_ylabel(r'$\bar X_m - \psi^* (x)$') @@ -588,18 +545,17 @@ plt.show() ```{exercise} :label: mc_ex3 -In `quantecon` library, irreducibility is tested by checking whether the chain forms a [strongly connected component](https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.components.is_strongly_connected.html). +在 `quantecon` 库中,通过检查链是否形成[强连通分量](https://networkx.org/documentation/stable/reference/algorithms/generated/networkx.algorithms.components.is_strongly_connected.html)来测试不可约性。 -Another way to test irreducibility is via the following statement: +另一种测试不可约性的方法是通过以下陈述: -The $n \times n$ matrix $A$ is irreducible if and only if $\sum_{k=0}^{n-1}A^k$ -is a strictly positive matrix. +当且仅当 $\sum_{k=0}^{n-1}A^k$ 是严格正矩阵时,$n \times n$ 矩阵 $A$ 是不可约的。 -(see, e.g., {cite}`zhao_power_2012` and [this StackExchange post](https://math.stackexchange.com/questions/3336616/how-to-prove-this-matrix-is-a-irreducible-matrix)) +(参见{cite}`zhao_power_2012` 和 [此 StackExchange 讨论](https://math.stackexchange.com/questions/3336616/how-to-prove-this-matrix-is-a-irreducible-matrix)) -Based on this claim, write a function to test irreducibility. +根据此断言,编写一个函数来测试不可约性。 -``` +``` ```{solution-start} mc_ex3 :class: dropdown @@ -614,7 +570,7 @@ def is_irreducible(P): return np.all(result > 0) ``` -Let's try it. +让我们尝试一下。 ```{code-cell} ipython3 P1 = np.array([[0, 1], @@ -627,9 +583,9 @@ P3 = np.array([[0.971, 0.029, 0.000], [0.000, 0.508, 0.492]]) for P in (P1, P2, P3): - result = lambda P: 'irreducible' if is_irreducible(P) else 'reducible' + result = lambda P: '不可约' if is_irreducible(P) else '可约' print(f'{P}: {result(P)}') ``` ```{solution-end} -``` +``` diff --git a/lectures/mle.md b/lectures/mle.md index 8a15d6a..4b85d8e 100644 --- a/lectures/mle.md +++ b/lectures/mle.md @@ -11,23 +11,27 @@ kernelspec: name: python3 --- -# Maximum Likelihood Estimation +# 最大似然估计 ```{code-cell} ipython3 from scipy.stats import lognorm, pareto, expon import numpy as np from scipy.integrate import quad import matplotlib.pyplot as plt +import matplotlib as mpl import pandas as pd from math import exp + +FONTPATH = "fonts/SourceHanSerifSC-SemiBold.otf" +mpl.font_manager.fontManager.addfont(FONTPATH) +plt.rcParams['font.family'] = ['Source Han Serif SC'] ``` -## Introduction +## 介绍 -Consider a situation where a policymaker is trying to estimate how much revenue -a proposed wealth tax will raise. +考虑一个政策制定者试图估算提议中的财富税将带来多少收入的情况。 -The proposed tax is +提议的税务公式为: $$ h(w) = @@ -35,21 +39,22 @@ $$ a w & \text{if } w \leq \bar w \\ a \bar{w} + b (w-\bar{w}) & \text{if } w > \bar w \end{cases} -$$ +$$ -where $w$ is wealth. +其中 $w$ 代表财富。 ```{prf:example} :label: mle_ex_wt -For example, if $a = 0.05$, $b = 0.1$, and $\bar w = 2.5$, this means +例如,如果 $a = 0.05$,$b = 0.1$,且 $\bar w = 2.5$,意味着: -* a 5% tax on wealth up to 2.5 and -* a 10% tax on wealth in excess of 2.5. +* 对于2.5及以下的财富征收5%的税, +* 对超过2.5的部分财富征收10%的税。 -The unit is 100,000, so $w= 2.5$ means 250,000 dollars. +单位是100,000,所以 $w= 2.5$ 的意思是 250,000 美元。 ``` -Let's go ahead and define $h$: + +让我们来定义函数 $h$: ```{code-cell} ipython3 def h(w, a=0.05, b=0.1, w_bar=2.5): @@ -59,33 +64,29 @@ def h(w, a=0.05, b=0.1, w_bar=2.5): return a * w_bar + b * (w - w_bar) ``` -For a population of size $N$, where individual $i$ has wealth $w_i$, total revenue raised by -the tax will be +对于一个人口数量为 $N$ 的社区,每个人 $i$ 拥有财富 $w_i$,通过税收收入总额为 $$ T = \sum_{i=1}^{N} h(w_i) $$ -We wish to calculate this quantity. +我们希望计算这个总量。 -The problem we face is that, in most countries, wealth is not observed for all individuals. +我们面对的问题是,在大多数国家中,并不是所有个人的财富都能被观测到。 -Collecting and maintaining accurate wealth data for all individuals or households in a country -is just too hard. +为一个国家的所有个人或家庭收集并维持准确的财富数据是非常困难的。 -So let's suppose instead that we obtain a sample $w_1, w_2, \cdots, w_n$ telling us the wealth of $n$ randomly selected individuals. +因此,假设我们得到了一个样本 $w_1, w_2, \cdots, w_n$,它告诉我们 $n$ 个随机选定个人的财富。 -For our exercise we are going to use a sample of $n = 10,000$ observations from wealth data in the US in 2016. +在这个练习中,我们将使用 $n = 10,000$ 来自2016年美国的财富数据样本。 ```{code-cell} ipython3 n = 10_000 ``` -The data is derived from the -[Survey of Consumer Finances](https://en.wikipedia.org/wiki/Survey_of_Consumer_Finances) (SCF). - +这些数据来源于[消费者财务调查](https://en.wikipedia.org/wiki/Survey_of_Consumer_Finances) (SCF)。 -The following code imports this data and reads it into an array called `sample`. +以下代码导入了数据并将其读入名为 `sample` 的数组。 ```{code-cell} ipython3 :tags: [hide-input] @@ -94,41 +95,39 @@ url = 'https://media.githubusercontent.com/media/QuantEcon/high_dim_data/update_ df = pd.read_csv(url) df = df.dropna() df = df[df['year'] == 2016] -df = df.loc[df['n_wealth'] > 1 ] #restrcting data to net worth > 1 +df = df.loc[df['n_wealth'] > 1 ] # 限制数据为净值大于 1 的数据 rv = df['n_wealth'].sample(n=n, random_state=1234) rv = rv.to_numpy() / 100_000 sample = rv ``` -Let's histogram this sample. +让我们来做一个这个样本的直方图。 ```{code-cell} ipython3 fig, ax = plt.subplots() ax.set_xlim(-1, 20) density, edges = np.histogram(sample, bins=5000, density=True) prob = density * np.diff(edges) -plt.stairs(prob, edges, fill=True, alpha=0.8, label=r"unit: $\$100,000$") -plt.ylabel("prob") -plt.xlabel("net wealth") +plt.stairs(prob, edges, fill=True, alpha=0.8, label=r"单位:$100,000$") +plt.ylabel("概率") +plt.xlabel("净资产") plt.legend() plt.show() ``` -The histogram shows that many people have very low wealth and a few people have -very high wealth. +直方图显示许多人的财富非常低,少数人的财富非常高。 - -We will take the full population size to be +我们将假定全体人口规模为 ```{code-cell} ipython3 N = 100_000_000 ``` -How can we estimate total revenue from the full population using only the sample data? +我们如何仅使用样本数据来估计全体人口的总收入呢? -Our plan is to assume that wealth of each individual is a draw from a distribution with density $f$. +我们的计划是假设每个人的财富是从密度为 $f$ 的分布中抽取的。 -If we obtain an estimate of $f$ we can then approximate $T$ as follows: +如果我们获得 $f$ 的估计,那么我们可以按照下面的方式近似 $T$: $$ T = \sum_{i=1}^{N} h(w_i) @@ -136,32 +135,24 @@ $$ \approx N \int_{0}^{\infty} h(w)f(w) dw $$ (eq:est_rev) -(The sample mean should be close to the mean by the law of large numbers.) - -The problem now is: how do we estimate $f$? +(样本均值应该靠近平均值,根据大数定律。) +现在的问题是:我们如何估计 $f$? -## Maximum likelihood estimation +## 最大似然估计 -[Maximum likelihood estimation](https://en.wikipedia.org/wiki/Maximum_likelihood_estimation) -is a method of estimating an unknown distribution. +[最大似然估计](https://en.wikipedia.org/wiki/Maximum_likelihood_estimation) 是一种估计未知分布的方法。 -Maximum likelihood estimation has two steps: +最大似然估计有两个步骤: -1. Guess what the underlying distribution is (e.g., normal with mean $\mu$ and - standard deviation $\sigma$). -2. Estimate the parameter values (e.g., estimate $\mu$ and $\sigma$ for the - normal distribution) +1. 猜测潜在分布是什么(例如,正态分布,均值为 $\mu$,标准差为 $\sigma$)。 +2. 估计参数值(例如,估计正态分布的 $\mu$ 和 $\sigma$)。 -One possible assumption for the wealth is that each -$w_i$ is [log-normally distributed](https://en.wikipedia.org/wiki/Log-normal_distribution), -with parameters $\mu \in (-\infty,\infty)$ and $\sigma \in (0,\infty)$. +对于财富,一个可能的假设是每个 $w_i$ 都是[对数正态分布](https://en.wikipedia.org/wiki/Log-normal_distribution)的,参数 $\mu$ 在 $(-\infty, \infty)$ 范围内,$\sigma$ 在 $(0, \infty)$ 范围内。 -(This means that $\ln w_i$ is normally distributed with mean $\mu$ and standard deviation $\sigma$.) +(这意味着 $\ln w_i$ 是以 $\mu$ 为均值,$\sigma$ 为标准差的正态分布。) -You can see that this assumption is not completely unreasonable because, if we -histogram log wealth instead of wealth, the picture starts to look something -like a bell-shaped curve. +你可以看到这个假设不是完全没有道理,因为如果我们对财富的对数进行直方图表示而不是财富本身,图片开始看起来像一个钟形曲线。 ```{code-cell} ipython3 ln_sample = np.log(sample) @@ -170,32 +161,30 @@ ax.hist(ln_sample, density=True, bins=200, histtype='stepfilled', alpha=0.8) plt.show() ``` -Now our job is to obtain the maximum likelihood estimates of $\mu$ and $\sigma$, which -we denote by $\hat{\mu}$ and $\hat{\sigma}$. +现在我们的任务是获取 $\mu$ 和 $\sigma$ 的最大似然估计,我们用 $\hat{\mu}$ 和 $\hat{\sigma}$ 表示。 -These estimates can be found by maximizing the likelihood function given the -data. +这些估计值可以通过最大化给定数据的似然函数找到。 -The pdf of a lognormally distributed random variable $X$ is given by: +对数正态分布随机变量 $X$ 的概率密度函数 (pdf) 如下: $$ f(x, \mu, \sigma) = \frac{1}{x}\frac{1}{\sigma \sqrt{2\pi}} - \exp\left(\frac{-1}{2}\left(\frac{\ln x-\mu}{\sigma}\right)\right)^2 + \exp\left(-\frac{1}{2}\left(\frac{\ln x-\mu}{\sigma}\right)^2\right) $$ -For our sample $w_1, w_2, \cdots, w_n$, the [likelihood function](https://en.wikipedia.org/wiki/Likelihood_function) is given by +对于我们的样本 $w_1, w_2, \cdots, w_n$,[似然函数](https://en.wikipedia.org/wiki/Likelihood_function)定义为: $$ L(\mu, \sigma | w_i) = \prod_{i=1}^{n} f(w_i, \mu, \sigma) $$ -The likelihood function can be viewed as both +似然函数可以被视为: -* the joint distribution of the sample (which is assumed to be IID) and -* the "likelihood" of parameters $(\mu, \sigma)$ given the data. +* 样本的联合分布(假设是独立同分布)和 +* 给定数据的参数 $(\mu, \sigma)$ 的“似然性”。 -Taking logs on both sides gives us the log likelihood function, which is +对两边取对数,我们得到对数似然函数,如下所示: $$ \begin{aligned} @@ -207,9 +196,9 @@ $$ \end{aligned} $$ -To find where this function is maximised we find its partial derivatives wrt $\mu$ and $\sigma ^2$ and equate them to $0$. +为了找到这个函数的最大值,我们计算关于 $\mu$ 和 $\sigma ^2$ 的偏导数,并将它们设为 $0$. -Let's first find the maximum likelihood estimate (MLE) of $\mu$ +让我们首先找到 $\mu$ 的最大似然估计(MLE) $$ \frac{\delta \ell}{\delta \mu} @@ -218,7 +207,7 @@ $$ \implies \hat{\mu} = \frac{\sum_{i=1}^n \ln w_i}{n} $$ -Now let's find the MLE of $\sigma$ +现在让我们找到 $\sigma$ 的MLE $$ \frac{\delta \ell}{\delta \sigma^2} @@ -230,79 +219,72 @@ $$ \left( \frac{\sum_{i=1}^{n}(\ln w_i - \hat{\mu})^2}{n} \right)^{1/2} $$ -Now that we have derived the expressions for $\hat{\mu}$ and $\hat{\sigma}$, -let's compute them for our wealth sample. +现在我们已经推导出 $\hat{\mu}$ 和 $\hat{\sigma}$ 的表达式, +让我们为我们的财富样本计算它们。 ```{code-cell} ipython3 -μ_hat = np.mean(ln_sample) +μ_hat = np.mean(ln_sample) # 计算 μ 的估计值 μ_hat ``` ```{code-cell} ipython3 -num = (ln_sample - μ_hat)**2 -σ_hat = (np.mean(num))**(1/2) +num = (ln_sample - μ_hat)**2 # 计算方差的分子部分 +σ_hat = (np.mean(num))**(1/2) # 计算 σ 的估计值 σ_hat ``` -Let's plot the lognormal pdf using the estimated parameters against our sample data. +我们来绘制使用估计参数的对数正态分布概率密度函数,并与我们的样本数据进行对比。 ```{code-cell} ipython3 -dist_lognorm = lognorm(σ_hat, scale = exp(μ_hat)) -x = np.linspace(0,50,10000) +dist_lognorm = lognorm(σ_hat, scale = exp(μ_hat)) # 初始化对数正态分布 +x = np.linspace(0,50,10000) # 产生数据点 -fig, ax = plt.subplots() -ax.set_xlim(-1,20) +fig, ax = plt.subplots() # 创建图形和轴 +ax.set_xlim(-1,20) # 设置x轴的范围 -ax.hist(sample, density=True, bins=5_000, histtype='stepfilled', alpha=0.5) -ax.plot(x, dist_lognorm.pdf(x), 'k-', lw=0.5, label='lognormal pdf') -ax.legend() -plt.show() +ax.hist(sample, density=True, bins=5_000, histtype='stepfilled', alpha=0.5) # 绘制样本的直方图 +ax.plot(x, dist_lognorm.pdf(x), 'k-', lw=0.5, label='lognormal pdf') # 绘制对数正态分布的PDF +ax.legend() # 显示图例 +plt.show() # 展示图形 ``` -Our estimated lognormal distribution appears to be a reasonable fit for the overall data. +我们的估计的对数正态分布看起来很适合整体数据。 -We now use {eq}`eq:est_rev` to calculate total revenue. +我们现在使用方程{eq}`eq:est_rev`来计算总收入。 -We will compute the integral using numerical integration via SciPy's -[quad](https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.quad.html) -function +我们将通过 **SciPy** 的 [quad](https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.quad.html) 函数使用数值积分计算 ```{code-cell} ipython3 def total_revenue(dist): - integral, _ = quad(lambda x: h(x) * dist.pdf(x), 0, 100_000) - T = N * integral - return T + integral, _ = quad(lambda x: h(x) * dist.pdf(x), 0, 100_000) # 计算积分 + T = N * integral # 总收入 = 人数 * 单个收入 + return T # 返回总收入 ``` ```{code-cell} ipython3 -tr_lognorm = total_revenue(dist_lognorm) -tr_lognorm +tr_lognorm = total_revenue(dist_lognorm) # 使用对数正态分布计算总收入 +tr_lognorm # 显示总收入 ``` -(Our unit was 100,000 dollars, so this means that actual revenue is 100,000 -times as large.) +(我们的单位是10万美元,所以这意味着实际收入是10万倍。) +## 帕累托分布 -## Pareto distribution +如上所述,使用最大似然估计时需要我们假定一个先验的底层分布。 -We mentioned above that using maximum likelihood estimation requires us to make -a prior assumption of the underlying distribution. +之前我们假定这个分布是对数正态分布。 -Previously we assumed that the distribution is lognormal. +假设我们改为假设 $w_i$ 来自具有参数 $b$ 和 $x_m$ 的[帕累托分布](https://en.wikipedia.org/wiki/Pareto_distribution)。 -Suppose instead we assume that $w_i$ are drawn from the -[Pareto Distribution](https://en.wikipedia.org/wiki/Pareto_distribution) -with parameters $b$ and $x_m$. - -In this case, the maximum likelihood estimates are known to be +在这种情况下,最大似然估计已知为 $$ \hat{b} = \frac{n}{\sum_{i=1}^{n} \ln (w_i/\hat{x_m})} - \quad \text{and} \quad + \quad \text{和} \quad \hat{x}_m = \min_{i} w_i $$ -Let's calculate them. +我们来计算它们。 ```{code-cell} ipython3 xm_hat = min(sample) @@ -315,7 +297,7 @@ b_hat = 1/np.mean(den) b_hat ``` -Now let's recompute total revenue. +现在让我们重新计算总收入。 ```{code-cell} ipython3 dist_pareto = pareto(b = b_hat, scale = xm_hat) @@ -323,17 +305,15 @@ tr_pareto = total_revenue(dist_pareto) tr_pareto ``` -The number is very different! +这个数字差距很大! ```{code-cell} ipython3 tr_pareto / tr_lognorm ``` -We see that choosing the right distribution is extremely important. - +我们看到选择正确的分布非常重要。 - -Let's compare the fitted Pareto distribution to the histogram: +让我们将拟合的帕累托分布与直方图进行比较: ```{code-cell} ipython3 fig, ax = plt.subplots() @@ -347,22 +327,21 @@ ax.legend() plt.show() ``` -We observe that in this case the fit for the Pareto distribution is not very -good, so we can probably reject it. +我们观察到在这种情况下,帕累托分布的拟合效果并不好,所以我们可能会拒绝它。 -## What is the best distribution? +## 什么是最好的分布? -There is no "best" distribution --- every choice we make is an assumption. +没有“最好”的分布——我们做出的每一个选择都是一种假设。 -All we can do is try to pick a distribution that fits the data well. +我们能做的就是尝试选择一个能很好地拟合数据的分布。 -The plots above suggested that the lognormal distribution is optimal. +上面的图表表明,对数正态分布是最佳的。 -However when we inspect the upper tail (the richest people), the Pareto distribution may be a better fit. +然而,当我们检查上尾部(最富有的人)时,帕累托分布可能是一个更好的选择。 -To see this, let's now set a minimum threshold of net worth in our dataset. +为了查看这一点,现在让我们设定一个数据集中的净资产最低阈值。 -We set an arbitrary threshold of $500,000 and read the data into `sample_tail`. +我们设定一个任意阈值为 $500,000,并将数据读入 `sample_tail`。 ```{code-cell} ipython3 :tags: [hide-input] @@ -374,7 +353,7 @@ rv_tail = rv_tail.to_numpy() sample_tail = rv_tail/500_000 ``` -Let's plot this data. +让我们绘制这些数据。 ```{code-cell} ipython3 fig, ax = plt.subplots() @@ -383,14 +362,13 @@ ax.hist(sample_tail, density=True, bins=500, histtype='stepfilled', alpha=0.8) plt.show() ``` -Now let's try fitting some distributions to this data. - +现在让我们尝试对这些数据拟合一些分布。 -### Lognormal distribution for the right hand tail +### 对数正态分布和右尾部 -Let's start with the lognormal distribution +让我们从对数正态分布开始 -We estimate the parameters again and plot the density against our data. +我们再次估计参数并将密度与我们的数据进行对比。 ```{code-cell} ipython3 ln_sample_tail = np.log(sample_tail) @@ -402,20 +380,19 @@ dist_lognorm_tail = lognorm(σ_hat_tail, scale = exp(μ_hat_tail)) fig, ax = plt.subplots() ax.set_xlim(0,50) ax.hist(sample_tail, density=True, bins=500, histtype='stepfilled', alpha=0.5) -ax.plot(x, dist_lognorm_tail.pdf(x), 'k-', lw=0.5, label='lognormal pdf') +ax.plot(x, dist_lognorm_tail.pdf(x), 'k-', lw=0.5, label='对数正态 pdf') ax.legend() plt.show() ``` -While the lognormal distribution was a good fit for the entire dataset, -it is not a good fit for the right hand tail. +虽然对数正态分布对整个数据集拟合良好, +但它并不适合右尾部。 +### 帕累托分布用于右尾部 -### Pareto distribution for the right hand tail +现在假设截断数据集符合帕累托分布。 -Let's now assume the truncated dataset has a Pareto distribution. - -We estimate the parameters again and plot the density against our data. +我们再次估计参数,并将密度与我们的数据进行对比。 ```{code-cell} ipython3 xm_hat_tail = min(sample_tail) @@ -431,38 +408,35 @@ ax.plot(x, dist_pareto_tail.pdf(x), 'k-', lw=0.5, label='pareto pdf') plt.show() ``` -The Pareto distribution is a better fit for the right hand tail of our dataset. +帕累托分布更适合我们数据集的右尾部。 -### So what is the best distribution? +### 那么什么是最佳分布? -As we said above, there is no "best" distribution --- each choice is an -assumption. +如上所述,没有“最佳”分布——每个选择都是一个假设。 -We just have to test what we think are reasonable distributions. +我们只需要测试我们认为合理的分布。 -One test is to plot the data against the fitted distribution, as we did. +一种测试方法是将数据与拟合分布进行绘图,正如我们所做的。 -There are other more rigorous tests, such as the [Kolmogorov-Smirnov test](https://en.wikipedia.org/wiki/Kolmogorov%E2%80%93Smirnov_test). +还有其他更严格的测试方法,比如[科尔莫哥洛夫-斯米尔诺夫检验](https://en.wikipedia.org/wiki/Kolmogorov%E2%80%93Smirnov_test)。 -We omit such advanced topics (but encourage readers to study them once -they have completed these lectures). +我们忽略了这些高级主题(但鼓励读者在完成这些讲座后研究它们)。 -## Exercises +## 练习 ```{exercise-start} :label: mle_ex1 ``` -Suppose we assume wealth is [exponentially](https://en.wikipedia.org/wiki/Exponential_distribution) -distributed with parameter $\lambda > 0$. +假设我们假设财富是以参数 $\lambda > 0$ 的[指数](https://en.wikipedia.org/wiki/Exponential_distribution)分布。 -The maximum likelihood estimate of $\lambda$ is given by +$\lambda$ 的最大似然估计为 $$ \hat{\lambda} = \frac{n}{\sum_{i=1}^n w_i} $$ -1. Compute $\hat{\lambda}$ for our initial sample. -2. Use $\hat{\lambda}$ to find the total revenue +1. 计算我们初始样本的 $\hat{\lambda}$。 +2. 使用 $\hat{\lambda}$ 来找到总收入 ```{exercise-end} ``` @@ -489,7 +463,7 @@ tr_expo :label: mle_ex2 ``` -Plot the exponential distribution against the sample and check if it is a good fit or not. +绘制指数分布与样本的比较,并检查它是否适合。 ```{exercise-end} ``` @@ -509,7 +483,7 @@ ax.legend() plt.show() ``` -Clearly, this distribution is not a good fit for our data. +很明显,这个分布不适合我们的数据。 ```{solution-end} -``` +``` \ No newline at end of file diff --git a/lectures/money_inflation.md b/lectures/money_inflation.md index c284eb0..f5a1fde 100644 --- a/lectures/money_inflation.md +++ b/lectures/money_inflation.md @@ -783,7 +783,7 @@ p_0 = - (Q^{22})^{-1} Q^{21} m_0. 为了得到这个公式,首先注意到因为 $(Q^{21}\ Q^{22})$ 是 $Q$ 的逆的第二行,并且因为 $Q^{-1} Q = I$,所以我们有 $$ -\begin{bmatrix} Q^{21} & Q^{22} \end{bmatrix} \begin{bmatrix} Q_{11}\cr Q_{21} \end{bmatrix} = 0 +\begin{bmatrix} Q^{21} & Q^{22} \end{bmatrix} egin{bmatrix} Q_{11}\cr Q_{21} \end{bmatrix} = 0 $$ 这意味着 diff --git a/lectures/money_inflation_nonlinear.md b/lectures/money_inflation_nonlinear.md index f716916..fa9abc3 100644 --- a/lectures/money_inflation_nonlinear.md +++ b/lectures/money_inflation_nonlinear.md @@ -11,124 +11,125 @@ kernelspec: name: python3 --- -# Inflation Rate Laffer Curves +# 通货膨胀税Laffer曲线 -## Overview +## 概览 -We study stationary and dynamic *Laffer curves* in the inflation tax rate in a non-linear version of the model studied in {doc}`money_inflation`. +我们在非线性模型版本中研究通货膨胀税率的静态和动态*Laffer曲线*,该模型在 {doc}`money_inflation`中进行了研究。 -We use the log-linear version of the demand function for money that {cite}`Cagan` -used in his classic paper in place of the linear demand function used in {doc}`money_inflation`. +我们使用了{cite}`Cagan`在他的经典论文中使用的货币需求函数的对数线性版本,而不是在 {doc}`money_inflation` 中使用的线性需求函数。 -That change requires that we modify parts of our analysis. +这一改变要求我们修改部分分析内容。 -In particular, our dynamic system is no longer linear in state variables. +特别是,我们的动态系统在状态变量上不再是线性的。 -Nevertheless, the economic logic underlying an analysis based on what we called ''method 2'' remains unchanged. +然而,基于我们所谓的“方法2”的经济逻辑分析仍然没有改变。 -We shall discover qualitatively similar outcomes to those that we studied in {doc}`money_inflation`. +我们将发现与我们在 {doc}`money_inflation` 中研究的结果类似的质量结果。 -That lecture presented a linear version of the model in this lecture. +该讲座展示了本讲座中模型的线性版本。 -As in that lecture, we discussed these topics: +就像在那次讲座中,我们讨论了以下主题: -* an **inflation tax** that a government gathers by printing paper or electronic money -* a dynamic **Laffer curve** in the inflation tax rate that has two stationary equilibria -* perverse dynamics under rational expectations in which the system converges to the higher stationary inflation tax rate -* a peculiar comparative stationary-state analysis connected with that stationary inflation rate that asserts that inflation can be *reduced* by running *higher* government deficits +* 政府通过印制纸币或电子货币收集的**通货膨胀税** +* 在通货膨胀税率中有两个静态均衡的动态**Laffer曲线** +* 在理性预期下的反常动态,系统趋向于较高的静态通货膨胀税率 +* 与该静态通货膨胀率相关的奇特的比较静态状态分析,它声称通货膨胀可以通过运行*更高*的政府赤字来*降低* -These outcomes will set the stage for the analysis of {doc}`laffer_adaptive` that studies a version of the present model that uses a version of "adaptive expectations" instead of rational expectations. +这些结果将为分析 {doc}`laffer_adaptive` 做准备,该分析研究了现有模型的一个版本,该版本使用了一种“适应性预期”而不是理性预期。 -That lecture will show that +该讲座将展示 -* replacing rational expectations with adaptive expectations leaves the two stationary inflation rates unchanged, but that $\ldots$ -* it reverses the perverse dynamics by making the *lower* stationary inflation rate the one to which the system typically converges -* a more plausible comparative dynamic outcome emerges in which now inflation can be *reduced* by running *lower* government deficits +* 用适应性预期替代理性预期保持了两个静态通货膨胀率不变,但是$\ldots$ +* 它通过使*较低*的静态通货膨胀率成为系统通常收敛的结果,逆转了反常动态 +* 现在通货膨胀可以通过运行*较低*的政府赤字来*降低*,从而出现了更为合理的比较动态结果) -## The Model +## 模型 -Let +设定 -* $m_t$ be the log of the money supply at the beginning of time $t$ -* $p_t$ be the log of the price level at time $t$ - -The demand function for money is +* $m_t$ 为时间 $t$ 开始时货币供应量的对数 +* $p_t$ 为时间 $t$ 时的价格水平对数 + +货币的需求函数为: $$ -m_{t+1} - p_t = -\alpha (p_{t+1} - p_t) +m_{t+1} - p_t = -\alpha (p_{t+1} - p_t) $$ (eq:mdemand) -where $\alpha \geq 0$. +其中 $\alpha \geq 0$。 -The law of motion of the money supply is +货币供应量的运动规律为: -$$ -\exp(m_{t+1}) - \exp(m_t) = g \exp(p_t) +$$ +\exp(m_{t+1}) - \exp(m_t) = g \exp(p_t) $$ (eq:msupply) -where $g$ is the part of government expenditures financed by printing money. - -**Remark:** Please notice that while equation {eq}`eq:mdemand` is linear in logs of the money supply and price level, equation {eq}`eq:msupply` is linear in levels. This will require adapting the equilibrium computation methods that we deployed in {doc}`money_inflation`. +其中 $g$ 是政府支出中通过印钞方式筹资的部分。 +**备注:** 请注意,虽然公式 {eq}`eq:mdemand` 在货币供应量和价格水平的对数上是线性的,但公式 {eq}`eq:msupply` 在水平上是线性的。这将需要适应我们在 {doc}`money_inflation` 中部署的均衡计算方法。 +## 通货膨胀率的限制值 -## Limiting Values of Inflation Rate +我们可以通过研究稳态拉弗曲线来计算 $\overline \pi$ 的两个潜在限制值。 -We can compute the two prospective limiting values for $\overline \pi$ by studying the steady-state Laffer curve. - -Thus, in a *steady state* +因此,在*稳态*中 $$ m_{t+1} - m_t = p_{t+1} - p_t = x \quad \forall t , $$ -where $x > 0 $ is a common rate of growth of logarithms of the money supply and price level. +其中 $x > 0$ 是货币供应量和价格水平的对数的公共增长率。 -A few lines of algebra yields the following equation that $x$ satisfies +几行代数运算可以得出 $x$ 满足的以下方程 $$ \exp(-\alpha x) - \exp(-(1 + \alpha) x) = g $$ (eq:steadypi) -where we require that +我们需要满足 $$ g \leq \max_{x \geq 0} \{\exp(-\alpha x) - \exp(-(1 + \alpha) x) \}, $$ (eq:revmax) -so that it is feasible to finance $g$ by printing money. +以便通过印钞来融资是可行的。 -The left side of {eq}`eq:steadypi` is steady state revenue raised by printing money. +{eq}`eq:steadypi`的左侧是通过印钞所筹集的稳态收入。 -The right side of {eq}`eq:steadypi` is the quantity of time $t$ goods that the government raises by printing money. +{eq}`eq:steadypi`的右侧是政府通过印钞筹集的时间 $t$ 商品的数量。 -Soon we'll plot the left and right sides of equation {eq}`eq:steadypi`. +不久我们将绘制方程{eq}`eq:steadypi`的左右两侧。 -But first we'll write code that computes a steady-state -$\overline \pi$. +但首先我们将编写计算稳态 $\overline \pi$ 的代码。 -Let's start by importing some libraries +让我们从导入一些库开始 ```{code-cell} ipython3 from collections import namedtuple import numpy as np +import matplotlib as mpl import matplotlib.pyplot as plt from matplotlib.ticker import MaxNLocator from scipy.optimize import fsolve + +FONTPATH = "fonts/SourceHanSerifSC-SemiBold.otf" +mpl.font_manager.fontManager.addfont(FONTPATH) +plt.rcParams['font.family'] = ['Source Han Serif SC'] ``` +++ {"user_expressions": []} -Let's create a `namedtuple` to store the parameters of the model +让我们创建一个`namedtuple`来存储模型的参数 ```{code-cell} ipython3 CaganLaffer = namedtuple('CaganLaffer', - ["m0", # log of the money supply at t=0 - "α", # sensitivity of money demand + ["m0", # t=0时货币供应量的对数 + "α", # 货币需求的灵敏度 "λ", "g" ]) -# Create a Cagan Laffer model +# 创建一个Cagan Laffer模型 def create_model(α=0.5, m0=np.log(100), g=0.35): return CaganLaffer(α=α, m0=m0, λ=α/(1+α), g=g) @@ -137,10 +138,10 @@ model = create_model() +++ {"user_expressions": []} -Now we write code that computes steady-state $\overline \pi$s. +现在我们编写计算稳态$\overline \pi$的代码。 ```{code-cell} ipython3 -# Define formula for π_bar +# 定义π_bar的公式 def solve_π(x, α, g): return np.exp(-α * x) - np.exp(-(1 + α) * x) - g @@ -148,42 +149,42 @@ def solve_π_bar(model, x0): π_bar = fsolve(solve_π, x0=x0, xtol=1e-10, args=(model.α, model.g))[0] return π_bar -# Solve for the two steady state of π +# 求解两个稳态的π π_l = solve_π_bar(model, x0=0.6) π_u = solve_π_bar(model, x0=3.0) -print(f'The two steady state of π are: {π_l, π_u}') +print(f'两个稳态的π是: {π_l, π_u}') ``` -We find two steady state $\overline \pi$ values. +我们找到两个稳态$\overline \pi$的值。 -## Steady State Laffer curve +## 稳态拉弗曲线 -The following figure plots the steady state Laffer curve together with the two stationary inflation rates. +以下图形展示了稳态拉弗曲线以及两个稳态通货膨胀率。 ```{code-cell} ipython3 --- mystnb: figure: - caption: Seigniorage as function of steady state inflation. The dashed brown lines indicate $\pi_l$ and $\pi_u$. + caption: 稳态通胀的铸币税功能。虚线棕色线条代表 $\pi_l$ 和 $\pi_u$。 name: laffer_curve_nonlinear width: 500px --- def compute_seign(x, α): - return np.exp(-α * x) - np.exp(-(1 + α) * x) + return np.exp(-α * x) - np.exp(-(1 + α) * x) def plot_laffer(model, πs): α, g = model.α, model.g - # Generate π values + # 生成 π 值 x_values = np.linspace(0, 5, 1000) - # Compute corresponding seigniorage values for the function + # 计算对应的铸币税值 y_values = compute_seign(x_values, α) - # Plot the function + # 绘制函数 plt.plot(x_values, y_values, - label=f'Laffer curve') + label=f'拉弗曲线') for π, label in zip(πs, ['$\pi_l$', '$\pi_u$']): plt.text(π, plt.gca().get_ylim()[0]*2, label, horizontalalignment='center', @@ -192,24 +193,22 @@ def plot_laffer(model, πs): plt.axhline(g, color='red', linewidth=0.5, linestyle='--', label='g') plt.xlabel('$\pi$') - plt.ylabel('seigniorage') + plt.ylabel('铸币税') plt.legend() plt.show() -# Steady state Laffer curve +# 稳态拉弗曲线 plot_laffer(model, (π_l, π_u)) ``` -## Initial Price Levels - -Now that we have our hands on the two possible steady states, we can compute two functions $\underline p(m_0)$ and -$\overline p(m_0)$, which as initial conditions for $p_t$ at time $t$, imply that $\pi_t = \overline \pi $ for all $t \geq 0$. +## 初始价格水平 -The function $\underline p(m_0)$ will be associated with $\pi_l$ the lower steady-state inflation rate. - -The function $\overline p(m_0)$ will be associated with $\pi_u$ the lower steady-state inflation rate. +现在我们已经掌握了两个可能的稳态,我们可以计算两个函数 $\underline p(m_0)$ 和 +$\overline p(m_0)$,作为时间 $t$ 时 $p_t$ 的初始条件,意味着对于所有 $t \geq 0$ 有 $\pi_t = \overline \pi$。 +函数 $\underline p(m_0)$ 将会与较低的稳态通货膨胀率 $\pi_l$ 关联。 +函数 $\overline p(m_0)$ 将会与较高的稳态通货膨胀率 $\pi_u$ 关联。 ```{code-cell} ipython3 def solve_p0(p0, m0, α, g, π): @@ -222,25 +221,24 @@ def solve_p0_bar(model, x0, π_bar): π_bar))[0] return p0_bar -# Compute two initial price levels associated with π_l and π_u +# 计算与 π_l 和 π_u 关联的两个初始价格水平 p0_l = solve_p0_bar(model, x0=np.log(220), π_bar=π_l) p0_u = solve_p0_bar(model, x0=np.log(220), π_bar=π_u) -print(f'Associated initial p_0s are: {p0_l, p0_u}') +print(f'关联的初始 p_0s 是: {p0_l, p0_u}') ``` -### Verification +### 验证 -To start, let's write some code to verify that if the initial log price level $p_0$ takes one -of the two values we just calculated, the inflation rate $\pi_t$ will be constant for all $t \geq 0$. +首先,让我们编写一些代码来验证,如果初始对数价格水平 $p_0$ 取我们刚刚计算的两个值之一,那么通货膨胀率 $\pi_t$ 将对所有的 $t \geq 0$ 保持恒定。 -The following code verifies this. +下面的代码进行了验证。 ```{code-cell} ipython3 -# Implement pseudo-code above +# 实现上述伪代码 def simulate_seq(p0, model, num_steps): λ, g = model.λ, model.g π_seq, μ_seq, m_seq, p_seq = [], [], [model.m0], [p0] @@ -259,83 +257,80 @@ def simulate_seq(p0, model, num_steps): ```{code-cell} ipython3 π_seq, μ_seq, m_seq, p_seq = simulate_seq(p0_l, model, 150) -# Check π and μ at steady state +# 在稳态下检查 π 和 μ print('π_bar == μ_bar:', π_seq[-1] == μ_seq[-1]) -# Check steady state m_{t+1} - m_t and p_{t+1} - p_t +# 检查稳态下的 m_{t+1} - m_t 和 p_{t+1} - p_t print('m_{t+1} - m_t:', m_seq[-1] - m_seq[-2]) print('p_{t+1} - p_t:', p_seq[-1] - p_seq[-2]) -# Check if exp(-αx) - exp(-(1 + α)x) = g +# 检验 exp(-αx) - exp(-(1 + α)x) = g eq_g = lambda x: np.exp(-model.α * x) - np.exp(-(1 + model.α) * x) print('eq_g == g:', np.isclose(eq_g(m_seq[-1] - m_seq[-2]), model.g)) ``` -## Computing an Equilibrium Sequence +## 计算均衡序列 -We'll deploy a method similar to *Method 2* used in {doc}`money_inflation`. +我们将采用类似于 {doc}`money_inflation` 中的 *方法2*。 -We'll take the time $t$ state vector to be the pair $(m_t, p_t)$. +我们将时间 $t$ 的状态向量视为对 $(m_t, p_t)$。 -We'll treat $m_t$ as a ``natural state variable`` and $p_t$ as a ``jump`` variable. - -Let +我们将 $m_t$ 视为一个 ``自然状态变量``,而 $p_t$ 视为一个 ``跳跃`` 变量。 + +定义 $$ -\lambda \equiv \frac{\alpha}{1+ \alpha} +\lambda \equiv \frac{\alpha}{1 + \alpha} $$ -Let's rewrite equation {eq}`eq:mdemand` as +让我们重写方程 {eq}`eq:mdemand` 成为 $$ -p_t = (1-\lambda) m_{t+1} + \lambda p_{t+1} +p_t = (1-\lambda) m_{t+1} + \lambda p_{t+1} $$ (eq:mdemand2) -We'll summarize our algorithm with the following pseudo-code. - -**Pseudo-code** +我们将用以下伪代码总结我们的算法。 -The heart of the pseudo-code iterates on the following mapping from state vector $(m_t, p_t)$ at time $t$ -to state vector $(m_{t+1}, p_{t+1})$ at time $t+1$. +**伪代码** +伪代码的核心在于迭代下面的映射:从时间 $t$ 的状态向量 $(m_t, p_t)$ +到时间 $t+1$ 的状态向量 $(m_{t+1}, p_{t+1})$。 -* starting from a given pair $(m_t, p_t)$ at time $t \geq 0$ +* 从给定的一对 $(m_t, p_t)$ 在时间 $t \geq 0$ 开始 - * solve {eq}`eq:msupply` for $m_{t+1}$ + * 解决 {eq}`eq:msupply` 得到 $m_{t+1}$ - * solve {eq}`eq:mdemand2` for $p_{t+1} = \lambda^{-1} p_t + (1 - \lambda^{-1}) m_{t+1}$ + * 解决 {eq}`eq:mdemand2` 得到 $p_{t+1} = \lambda^{-1} p_t + (1 - \lambda^{-1}) m_{t+1}$ - * compute the inflation rate $\pi_t = p_{t+1} - p_t$ and growth of money supply $\mu_t = m_{t+1} - m_t $ + * 计算通货膨胀率 $\pi_t = p_{t+1} - p_t$ 和货币供应增长率 $\mu_t = m_{t+1} - m_t$ -Next, compute the two functions $\underline p(m_0)$ and $\overline p(m_0)$ described above +接下来,计算上述描述的两个函数 $\underline p(m_0)$ 和 $\overline p(m_0)$ -Now initiate the algorithm as follows. +现在开始如下算法。 - * set $m_0 >0$ - * set a value of $p_0 \in [\underline p(m_0), \overline p(m_0)]$ and form the pair $(m_0, p_0)$ at time $t =0$ + * 设置 $m_0 >0$ + * 在区间 $[\underline p(m_0), \overline p(m_0)]$ 中选择一个 $p_0$ 的值,并在时间 $t=0$ 形成一对 $(m_0, p_0)$ -Starting from $(m_0, p_0)$ iterate on $t$ to convergence of $\pi_t \rightarrow \overline \pi$ and $\mu_t \rightarrow \overline \mu$ - -It will turn out that +从 $(m_0, p_0)$ 开始,迭代 $t$ 直到 $\pi_t \rightarrow \overline \pi$ 和 $\mu_t \rightarrow \overline \mu$ 收敛 -* if they exist, limiting values $\overline \pi$ and $\overline \mu$ will be equal +结果将表明: -* if limiting values exist, there are two possible limiting values, one high, one low +* 如果它们存在,极限值 $\overline \pi$ 和 $\overline \mu$ 将是相等的 -* for almost all initial log price levels $p_0$, the limiting $\overline \pi = \overline \mu$ is -the higher value +* 如果极限值存在,有两个可能的极限值,一个高,一个低 -* for each of the two possible limiting values $\overline \pi$ ,there is a unique initial log price level $p_0$ that implies that $\pi_t = \mu_t = \overline \mu$ for all $t \geq 0$ +* 对于几乎所有初始对数价格水平 $p_0$,极限 $\overline \pi = \overline \mu$ 是更高的值 - * this unique initial log price level solves $\log(\exp(m_0) + g \exp(p_0)) - p_0 = - \alpha \overline \pi $ - - * the preceding equation for $p_0$ comes from $m_1 - p_0 = - \alpha \overline \pi$ +* 对于两个可能的极限值 $\overline \pi$ 中的每一个,存在一个独特的初始对数价格水平 $p_0$,它意味着所有 $t \geq 0$ 的 $\pi_t = \mu_t = \overline \mu$ + * 这个独特的初始对数价格水平解决了 $\log(\exp(m_0) + g \exp(p_0)) - p_0 = - \alpha \overline \pi$ -## Slippery Side of Laffer Curve Dynamics + * 上述关于 $p_0$ 的方程源自 $m_1 - p_0 = - \alpha \overline \pi$ + +## 拉弗曲线动态的滑动面 -We are now equipped to compute time series starting from different $p_0$ settings, like those in {doc}`money_inflation`. +我们现在已装备好用不同的 $p_0$ 设置来计算时间序列,就像在{doc}`money_inflation`中的设置。 ```{code-cell} ipython3 :tags: [hide-cell] @@ -344,35 +339,35 @@ def draw_iterations(p0s, model, line_params, p0_bars, num_steps): fig, axes = plt.subplots(4, 1, figsize=(8, 10), sharex=True) - # Pre-compute time steps + # 预先计算时间步 time_steps = np.arange(num_steps) - # Plot the first two y-axes in log scale + # 在对数刻度上绘制前两个y轴 for ax in axes[:2]: ax.set_yscale('log') - # Iterate over p_0s and calculate a series of y_t + # 遍历 p_0s 并计算一系列的 y_t for p0 in p0s: π_seq, μ_seq, m_seq, p_seq = simulate_seq(p0, model, num_steps) - # Plot m_t + # 绘制 m_t axes[0].plot(time_steps, m_seq[1:], **line_params) - # Plot p_t + # 绘制 p_t axes[1].plot(time_steps, p_seq[1:], **line_params) - # Plot π_t + # 绘制 π_t axes[2].plot(time_steps, π_seq, **line_params) - # Plot μ_t + # 绘制 μ_t axes[3].plot(time_steps, μ_seq, **line_params) - # Draw labels + # 绘制标签 axes[0].set_ylabel('$m_t$') axes[1].set_ylabel('$p_t$') axes[2].set_ylabel('$\pi_t$') axes[3].set_ylabel('$\mu_t$') - axes[3].set_xlabel('timestep') + axes[3].set_xlabel('时间步') for p_0, label in [(p0_bars[0], '$p_0=p_l$'), (p0_bars[1], '$p_0=p_u$')]: y = simulate_seq(p_0, model, 1)[0] @@ -381,7 +376,7 @@ def draw_iterations(p0s, model, line_params, p0_bars, num_steps): ax.text(num_steps * 1.02, y[0], label, verticalalignment='center', color='grey', size=10) - # Enforce integar axis label + # 强制整数轴标签 axes[3].xaxis.set_major_locator(MaxNLocator(integer=True)) plt.tight_layout() @@ -392,14 +387,12 @@ def draw_iterations(p0s, model, line_params, p0_bars, num_steps): --- mystnb: figure: - caption: Starting from different initial values of $p_0$, paths of $m_t$ (top - panel, log scale for $m$), $p_t$ (second panel, log scale for $p$), $\pi_t$ (third panel), and $\mu_t$ (bottom - panel) + caption: 从不同的初始值 $p_0$ 开始,$m_t$(顶部面板,$m$ 使用对数刻度),$p_t$(第二面板,$p$ 使用对数刻度),$\pi_t$(第三面板)和 $\mu_t$(底部面板)的路径 name: p0_path_nonlin width: 500px --- -# Generate a sequence from p0_l to p0_u +# 从 p0_l 到 p0_u 生成一个序列 p0s = np.arange(p0_l, p0_u, 0.1) line_params = {'lw': 1.5, @@ -411,26 +404,27 @@ p0_bars = (p0_l, p0_u) draw_iterations(p0s, model, line_params, p0_bars, num_steps=20) ``` -Staring at the paths of price levels in {numref}`p0_path_nonlin` reveals that almost all paths converge to the *higher* inflation tax rate displayed in the stationary state Laffer curve. displayed in figure {numref}`laffer_curve_nonlinear`. +盯着 {numref}`p0_path_nonlin` 中的价格水平路径,发现几乎所有路径都趋向于在固定状态拉佛曲线中显示的*更高*的通货膨胀税率。如图 {numref}`laffer_curve_nonlinear` 中显示。 -Thus, we have reconfirmed what we have called the "perverse" dynamics under rational expectations in which the system converges to the higher of two possible stationary inflation tax rates. +因此,我们重新确认了我们所称的“反常”动态,在理性预期下,系统会趋向于两个可能的固定通货膨胀税率中较高的一个。 -Those dynamics are "perverse" not only in the sense that they imply that the monetary and fiscal authorities that have chosen to finance government expenditures eventually impose a higher inflation tax than required to finance government expenditures, but because of the following "counterintuitive" situation that we can deduce by staring at the stationary state Laffer curve displayed in figure {numref}`laffer_curve_nonlinear`: +这种动态之所以被称为“反常”,不仅仅是因为它们意味着货币和财政当局选择通过通货膨胀税来资助政府开支,而且是由于我们通过盯着图 {numref}`laffer_curve_nonlinear` 中的固定状态拉佛曲线可以推断出的以下“违反直觉”的情况: -* the figure indicates that inflation can be *reduced* by running *higher* government deficits, i.e., by raising more resources through printing money. +* 图表显示,通过运行*更高*的政府赤字,即通过增加印钞筹集更多资源,可以*降低*通货膨胀。 ```{note} -The same qualitative outcomes prevail in {doc}`money_inflation` that studies a linear version of the model in this lecture. +在 {doc}`money_inflation` 中研究的模型的线性版本中也普遍存在同样的定性结果。 ``` -We discovered that +我们发现: -* all but one of the equilibrium paths converge to limits in which the higher of two possible stationary inflation tax prevails -* there is a unique equilibrium path associated with "plausible" statements about how reductions in government deficits affect a stationary inflation rate +* 所有但一个的均衡路径趋向于较高的两个可能的固定通货膨胀税率 +* 存在一个与关于政府赤字减少如何影响固定通货膨胀率的“合理”声明相关的独特均衡路径 -As in {doc}`money_inflation`, -on grounds of plausibility, we again recommend selecting the unique equilibrium that converges to the lower stationary inflation tax rate. +正如 {doc}`money_inflation` 中所述, +出于合理性的考虑,我们再次推荐选择趋向于较低固定通货膨胀税率的独特均衡。 -As we shall see, we accepting this recommendation is a key ingredient of outcomes of the "unpleasant arithmetic" that we describe in {doc}`unpleasant`. +正如我们将看到的,接受这个建议是我们在 {doc}`unpleasant` 中描述的“不愉快算术”结果的关键成分。 -In {doc}`laffer_adaptive`, we shall explore how {cite}`bruno1990seigniorage` and others justified our equilibrium selection in other ways. +在 {doc}`laffer_adaptive` 中,我们将探讨 {cite}`bruno1990seigniorage` 和其他人是如何以其他方式为我们的均衡选择提供理由的。 +``` \ No newline at end of file diff --git a/lectures/monte_carlo.md b/lectures/monte_carlo.md index 9f66c22..b01b09d 100644 --- a/lectures/monte_carlo.md +++ b/lectures/monte_carlo.md @@ -11,138 +11,129 @@ kernelspec: name: python3 --- - (monte-carlo)= -# Monte Carlo and Option Pricing +# 蒙特卡罗方法与期权定价 -## Overview +## 概览 -Simple probability calculations can be done either +简单的概率计算可以通过以下方式完成: -* with pencil and paper, or -* by looking up facts about well known probability distributions, or -* in our heads. +* 用铅笔和纸, +* 查询关于著名概率分布的事实, +* 在我们的脑海中。 -For example, we can easily work out +例如,我们可以轻松地计算出: -* the probability of three heads in five flips of a fair coin -* the expected value of a random variable that equals $-10$ with probability - $1/2$ and $100$ with probability $1/2$. +* 五次公平硬币翻转中出现三次正面的概率 +* 一个随机变量的期望值,该变量以 $1/2$ 的概率等于 $-10$,以 $1/2$ 的概率等于 $100$。 -But some probability calculations are very complex. +但是,有些概率计算非常复杂。 -Complex calculations concerning probabilities and expectations occur in many -economic and financial problems. +经济和金融问题中经常出现复杂的概率和期望值计算。 -Perhaps the most important tool for handling complicated probability -calculations is [Monte Carlo methods](https://en.wikipedia.org/wiki/Monte_Carlo_method). +或许处理复杂概率计算的最重要工具是[蒙特卡罗方法](https://en.wikipedia.org/wiki/Monte_Carlo_method)。 -In this lecture we introduce Monte Carlo methods for computing expectations, -with some applications in finance. +在这个讲座中,我们将介绍用于计算期望值的蒙特卡罗方法,以及在金融中的一些应用。 -We will use the following imports. +我们将使用以下导入。 ```{code-cell} ipython3 import numpy as np import matplotlib.pyplot as plt +import matplotlib as mpl from numpy.random import randn -``` +FONTPATH = "fonts/SourceHanSerifSC-SemiBold.otf" +mpl.font_manager.fontManager.addfont(FONTPATH) +plt.rcParams['font.family'] = ['Source Han Serif SC'] +``` -## An introduction to Monte Carlo +## 蒙特卡罗简介 -In this section we describe how Monte Carlo can be used to compute -expectations. +在本节中,我们描述如何使用蒙特卡罗计算期望值。 -### Share price with known distribution +### 已知分布的股票价格 -Suppose that we are considering buying a share in some company. +假设我们正在考虑购买某公司的股票。 -Our plan is either to +我们的计划是: -1. buy the share now, hold it for one year and then sell it, or -2. do something else with our money. +1. 现在买入股票,持有一年后再卖出,或者 +2. 用我们的钱做其他事情。 -We start by thinking of the share price in one year as a random variable $S$. +我们首先将一年后的股票价格视为一个随机变量 $S$。 -Before deciding whether or not to buy the share, we need to know some features -of the distribution of $S$. +在决定是否购买股票之前,我们需要知道 $S$ 分布的一些特性。 -For example, suppose the mean of $S$ is high relative to the price of buying -the share. +例如,假设 $S$ 的均值相对于购买股票的价格很高。 -This suggests we have a good chance of selling at a relatively high price. +这表明我们有很好的机会以相对较高的价格卖出。 -Suppose, however, that the variance of $S$ is also high. +但是,假设 $S$ 的方差也很高。 -This suggests that buying the share is risky, so perhaps we should refrain. +这表明购买股票有风险,所以我们可能应该放弃。 -Either way, this discussion shows the importance of understanding the -distribution of $S$. +无论如何,这次讨论显示了理解 $S$ 的分布的重要性。 -Suppose that, after analyzing the data, we guess that $S$ is well -represented by a lognormal distribution with parameters $\mu, \sigma$ . +假设,在分析数据后,我们猜测 $S$ 很好地由参数为 $\mu, \sigma$ 的对数正态分布表示。 -* $S$ has the same distribution as $\exp(\mu + \sigma Z)$ where $Z$ is standard normal. -* We write this statement as $S \sim LN(\mu, \sigma)$. +* $S$ 与 $\exp(\mu + \sigma Z)$ 分布相同,其中 $Z$ 是标准正态。 +* 我们将这个声明写为 $S \sim LN(\mu, \sigma)$。 -Any good reference on statistics (such as -[Wikipedia](https://en.wikipedia.org/wiki/Log-normal_distribution)) will tell -us that the mean and variance are +任何关于统计的好参考资料(如 [维基百科](https://en.wikipedia.org/wiki/Log-normal_distribution))都会告诉我们,其均值和方差为 $$ \mathbb E S = \exp \left(\mu + \frac{\sigma^2}{2} \right) $$ -and +和 $$ \mathop{\mathrm{Var}} S = [\exp(\sigma^2) - 1] \exp(2\mu + \sigma^2) $$ -So far we have no need for a computer. +到目前为止,我们还不需要使用计算机。 -### Share price with unknown distribution +### 股票价格的未知分布 -But now suppose that we study the distribution of $S$ more carefully. +但现在假设我们更仔细地研究股票价格 $S$ 的分布。 -We decide that the share price depends on three variables, $X_1$, $X_2$, and -$X_3$ (e.g., sales, inflation, and interest rates). +我们决定股票价格取决于三个变量,$X_1$、$X_2$ 和 $X_3$(例如,销售额、通货膨胀和利率)。 -In particular, our study suggests that +具体来说,我们的研究表明: $$ S = (X_1 + X_2 + X_3)^p $$ -where +其中: -* $p$ is a positive number, which is known to us (i.e., has been estimated), -* $X_i \sim LN(\mu_i, \sigma_i)$ for $i=1,2,3$, -* the values $\mu_i, \sigma_i$ are also known, and -* the random variables $X_1$, $X_2$ and $X_3$ are independent. +* $p$ 是一个已知的正数(即已估计), +* $X_i \sim LN(\mu_i, \sigma_i)$ 对于 $i=1,2,3$, +* 值 $\mu_i, \sigma_i$ 也是已知的, +* 随机变量 $X_1$、$X_2$ 和 $X_3$ 是相互独立的。 -How should we compute the mean of $S$? +我们应该如何计算 $S$ 的均值? -To do this with pencil and paper is hard (unless, say, $p=1$). +仅用笔和纸来计算是困难的(除非比如说 $p=1$)。 -But fortunately there's an easy way to do this, at least approximately. +但幸运的是,至少有一种简便的方法可以近似地做到这一点。 -This is the Monte Carlo method, which runs as follows: +这就是蒙特卡洛方法,其运行步骤如下: -1. Generate $n$ independent draws of $X_1$, $X_2$ and $X_3$ on a computer, -1. use these draws to generate $n$ independent draws of $S$, and -1. take the average value of these draws of $S$. +1. 在计算机上生成 $n$ 次独立抽取的 $X_1$、$X_2$ 和 $X_3$, +1. 使用这些抽取值生成 $n$ 次独立抽取的 $S$, +1. 取这些 $S$ 的抽取值的平均值。 -This average will be close to the true mean when $n$ is large. +当 $n$ 很大时,这个平均值将接近真实的平均值。 -This is due to the law of large numbers, which we discussed in {doc}`lln_clt`. +这归因于大数定律,我们在 {doc}`lln_clt` 中讨论过。 -We use the following values for $p$ and each $\mu_i$ and $\sigma_i$. +我们使用以下值为 $p$ 以及每个 $\mu_i$ 和 $\sigma_i$。 ```{code-cell} ipython3 n = 1_000_000 @@ -151,12 +142,9 @@ p = 0.5 σ_1, σ_2, σ_3 = 0.1, 0.05, 0.2 ``` +#### 使用循环的Python例程 - -#### A routine using loops in python - - -Here's a routine using native Python loops to calculate the desired mean +这里是一个使用Python原生循环计算期望平均值的例程 $$ \frac{1}{n} \sum_{i=1}^n S_i @@ -176,8 +164,7 @@ S / n ``` - -We can also construct a function that contains these operations: +我们还可以构建一个包含这些操作的函数: ```{code-cell} ipython3 def compute_mean(n=1_000_000): @@ -191,22 +178,20 @@ def compute_mean(n=1_000_000): ``` - -Now let's call it. +现在调用它。 ```{code-cell} ipython3 compute_mean() ``` +### 向量化例程 -### A vectorized routine +如果我们想要更准确的估计,我们应该增加 $n$。 -If we want a more accurate estimate we should increase $n$. +但是上面的代码运行得相当慢。 -But the code above runs quite slowly. - -To make it faster, let's implement a vectorized routine using NumPy. +为了让它运行得更快,让我们使用 NumPy 实现一个向量化例程。 ```{code-cell} ipython3 def compute_mean_vectorized(n=1_000_000): @@ -223,11 +208,9 @@ def compute_mean_vectorized(n=1_000_000): compute_mean_vectorized() ``` +请注意,这个例程运行得更快。 - -Notice that this routine is much faster. - -We can increase $n$ to get more accuracy and still have reasonable speed: +我们可以增加 $n$ 以提高精度,同时仍然保持合理的速度: ```{code-cell} ipython3 %%time @@ -235,99 +218,83 @@ We can increase $n$ to get more accuracy and still have reasonable speed: compute_mean_vectorized(n=10_000_000) ``` +## 以风险中性定价欧式看涨期权 +接下来,我们将在风险中性假设下对欧式看涨期权进行定价。 -## Pricing a European call option under risk neutrality - -Next we are going to price a European call option under risk neutrality. - -Let's first discuss risk neutrality and then consider European options. +首先让我们讨论风险中性,然后考虑欧式期权。 -### Risk-neutral pricing +### 风险中性定价 -When we use risk-neutral pricing, we determine the price of a given asset -according to its expected payoff: +当我们使用风险中性定价时,我们根据给定资产的预期收益来决定其价格: $$ -\text{cost } = \text{ expected benefit} +\text{成本} = \text{预期收益} $$ -For example, suppose someone promises to pay you +例如,假设有人承诺在抛硬币的结果为正面时支付你 -- 1,000,000 dollars if "heads" is the outcome of a fair coin flip -- 0 dollars if "tails" is the outcome +- 100万美元 +- 如果结果为反面,则支付0美元 -Let's denote the payoff as $G$, so that +我们记赔付为 $G$, 使得 $$ \mathbb P\left\{G = 10^6 \right\} = \mathbb P\{G = 0\} = \frac{1}{2} $$ -Suppose in addition that you can sell this promise to anyone who wants it. +假设此外你可以将这个承诺卖给任何希望购买的人。 -- First they pay you $P$, the price at which you sell it -- Then they get $G$, which could be either 1,000,000 or 0. +- 首先他们支付给你 $P$,即你的销售价格 +- 然后他们获得 $G$,可能是100万或者0。 -What's a fair price for this asset (this promise)? +这个资产(这个承诺)的公平价格是多少? -The definition of "fair" is ambiguous, but we can say that the -**risk-neutral price** is 500,000 dollars. +“公平”的定义是含糊的,但我们可以说 +**风险中性价格**是50万美元。 -This is because the risk-neutral price is just the expected payoff of the -asset, which is +这是因为风险中性价格仅仅是资产的预期收益,即 $$ \mathbb E G = \frac{1}{2} \times 10^6 + \frac{1}{2} \times 0 = 5 \times 10^5 $$ +### 关于风险的评论 -### A comment on risk - -As suggested by the name, the risk-neutral price ignores risk. - -To understand this, consider whether you would pay 500,000 dollars for such a -promise. +正如名称所示,风险中性定价忽略了风险。 -Would you prefer to receive 500,000 for sure or 1,000,000 dollars with -50% probability and nothing with 50% probability? +为了理解这一点,请考虑你是否愿意为这样一个承诺支付500,000美元。 -At least some readers will strictly prefer the first option --- although some -might prefer the second. +你是更愿意肯定收到500,000美元,还是有50%的概率收到1,000,000美元,而另外50%的概率什么也不得? -Thinking about this makes us realize that 500,000 is not necessarily the -"right" price --- or the price that we would see if there was a market for -these promises. +至少一些读者会严格偏好第一种选择——尽管有些人可能更喜欢第二种。 -Nonetheless, the risk-neutral price is an important benchmark, which economists -and financial market participants try to calculate every day. +思考这个问题让我们意识到500,000美元并不一定是“正确”的价格——或者,如果市场上有这样的承诺,我们会见到的价格。 +尽管如此,风险中性价格是一个重要的基准,经济学家和金融市场参与者每天都试图计算它。 -### Discounting +### 贴现 -Another thing we ignored in the previous discussion was time. +在之前的讨论中,我们忽略了时间因素。 -In general, receiving $x$ dollars now is preferable to receiving $x$ dollars -in $n$ periods (e.g., 10 years). +一般来说,现在收到$x$美元比$n$个周期后(例如10年)收到$x$美元更可取。 -After all, if we receive $x$ dollars now, we could put it in the bank at -interest rate $r > 0$ and receive $ (1 + r)^n x $ in $n$ periods. +毕竟,如果我们现在就收到$x$美元,我们可以将其存入银行中,按照利率$r > 0$计算,$n$个周期后会收到$(1 + r)^n x$。 -Hence future payments need to be discounted when we consider their present -value. +因此,我们在考虑它们的现值时需要对未来的支付进行贴现。 -We will implement discounting by +我们将通过以下方式实现贴现: -* multiplying a payment in one period by $\beta < 1$ -* multiplying a payment in $n$ periods by $\beta^n$, etc. +* 将一期的支付乘以$\beta < 1$ +* 将$n$期的支付乘以$\beta^n$等。 -The same adjustment needs to be applied to our risk-neutral price for the -promise described above. +对于上面描述的承诺,我们的风险中性价格也需要进行同样的调整。 -Thus, if $G$ is realized in $n$ periods, then the risk-neutral price is +因此,如果$G$在$n$个周期后实现,那么风险中性价格为: $$ P = \beta^n \mathbb E G @@ -335,40 +302,35 @@ $$ $$ +### 欧式看涨期权 -### European call options +现在让我们来计算一个欧式看涨期权的价格。 -Now let's price a European call option. +期权由三个要素描述: -The option is described by three things: +1. $n$, **到期日**, +2. $K$, **行权价**, +3. $S_n$, 在日期 $n$ 的**标的资产**价格。 -2. $n$, the **expiry date**, -2. $K$, the **strike price**, and -3. $S_n$, the price of the **underlying** asset at date $n$. +例如,假设标的资产是亚马逊的一股股票。 -For example, suppose that the underlying is one share in Amazon. +该期权的持有者有权在 $n$ 天后以 $K$ 价格买入亚马逊的一股股票。 -The owner of this option has the right to buy one share in Amazon at price $K$ after $n$ days. +如果 $S_n > K$,那么持有者将会行使期权,以 $K$ 的价格买入,在 $S_n$ 的价格卖出,并且获得 $S_n - K$ 的利润。 -If $S_n > K$, then the owner will exercise the option, buy at $K$, sell at -$S_n$, and make profit $S_n - K$. +如果 $S_n \leq K$,那么持有者将不会行使期权,收益为零。 -If $S_n \leq K$, then the owner will not exercise the option and the payoff is zero. +因此,收益为 $\max\{ S_n - K, 0 \}$。 -Thus, the payoff is $\max\{ S_n - K, 0 \}$. - -Under the assumption of risk neutrality, the price of the option is -the expected discounted payoff: +在风险中性的假设下,期权的价格是期望的折现收益: $$ P = \beta^n \mathbb E \max\{ S_n - K, 0 \} $$ -Now all we need to do is specify the distribution of $S_n$, so the expectation -can be calculated. - +现在我们需要指定 $S_n$ 的分布,以便可以计算期望。 -Suppose we know that $S_n \sim LN(\mu, \sigma)$ and $\mu$ and $\sigma$ are known. +假设我们知道 $S_n \sim LN(\mu, \sigma)$ 且 $\mu$ 和 $\sigma$ 是已知的。 -If $S_n^1, \ldots, S_n^M$ are independent draws from this lognormal distribution then, by the law of large numbers, +如果 $S_n^1, \ldots, S_n^M$ 是从这个对数正态分布中独立抽取的,则根据大数定律, $$ \mathbb E \max\{ S_n - K, 0 \} @@ -376,7 +338,7 @@ $$ \frac{1}{M} \sum_{m=1}^M \max \{S_n^m - K, 0 \} $$ -We suppose that +我们假设 ```{code-cell} ipython3 μ = 1.0 @@ -386,52 +348,45 @@ n = 10 β = 0.95 ``` - - -We set the simulation size to +我们设置模拟次数为 ```{code-cell} ipython3 M = 10_000_000 ``` - - -Here is our code +这是我们的代码 ```{code-cell} ipython3 S = np.exp(μ + σ * np.random.randn(M)) return_draws = np.maximum(S - K, 0) P = β**n * np.mean(return_draws) -print(f"The Monte Carlo option price is approximately {P:3f}") +print(f"蒙特卡洛期权价格约为 {P:3f}") ``` +## 通过动态模型进行定价 +在这个练习中,我们将研究一个更现实的股票价格 $S_n$ 模型。 -## Pricing via a dynamic model - -In this exercise we investigate a more realistic model for the share price $S_n$. - -This comes from specifying the underlying dynamics of the share price. +这来自于指定股票价格的底层动力学。 -First we specify the dynamics. +首先我们指定动态。 -Then we'll compute the price of the option using Monte Carlo. +然后我们将使用蒙特卡洛方法计算期权的价格。 -### Simple dynamics +### 简单动力学 -One simple model for $\{S_t\}$ is +一个简单的 $\{S_t\}$ 模型是 $$ \ln \frac{S_{t+1}}{S_t} = \mu + \sigma \xi_{t+1} $$ -where +其中 -* $S_0$ is lognormally distributed and -* $\{ \xi_t \}$ is IID and standard normal. +* $S_0$ 是对数正态分布的, +* $\{ \xi_t \}$ 是独立同分布的标准正态分布。 +在所述假设下,$S_n$ 是对数正态分布的。 -Under the stated assumptions, $S_n$ is lognormally distributed. - -To see why, observe that, with $s_t := \ln S_t$, the price dynamics become +为了看清楚为什么,注意到,设 $s_t := \ln S_t$,价格动力学变为 ```{math} :label: s_mc_dyms @@ -439,35 +394,32 @@ To see why, observe that, with $s_t := \ln S_t$, the price dynamics become s_{t+1} = s_t + \mu + \sigma \xi_{t+1} ``` -Since $s_0$ is normal and $\xi_1$ is normal and IID, we see that $s_1$ is -normally distributed. - -Continuing in this way shows that $s_n$ is normally distributed. +由于 $s_0$ 是正态分布的且 $\xi_1$ 是独立同分布的正态分布,我们可以看到 $s_1$ 是 +正态分布的。 -Hence $S_n = \exp(s_n)$ is lognormal. +继续这种方式表明 $s_n$ 是正态分布的。 +因此 $S_n = \exp(s_n)$ 是对数正态的。 -### Problems with simple dynamics -The simple dynamic model we studied above is convenient, since we can work out -the distribution of $S_n$. +### 简单动态问题 +我们上面研究的简单动态模型很方侈,因为我们可以计算得出$S_n$的分布。 -However, its predictions are counterfactual because, in the real world, -volatility (measured by $\sigma$) is not stationary. +然而,它的预测是违反事实的,因为在现实世界中,波动率(由$\sigma$衡量)并不是稳定的。 -Instead it rather changes over time, sometimes high (like during the GFC) and sometimes low. +相反,它随时间而变化,有时高(如在全球金融危机期间),有时低。 -In terms of our model above, this means that $\sigma$ should not be constant. +根据我们上面的模型,这意味着$\sigma$不应该是常数。 -### More realistic dynamics +### 更现实的动态模型 -This leads us to study the improved version: +这使我们研究了改进的版本: $$ \ln \frac{S_{t+1}}{S_t} = \mu + \sigma_t \xi_{t+1} $$ -where +其中 $$ \sigma_t = \exp(h_t), @@ -475,13 +427,12 @@ $$ h_{t+1} = \rho h_t + \nu \eta_{t+1} $$ -Here $\{\eta_t\}$ is also IID and standard normal. +这里$\{\eta_t\}$也是独立同分布且标准正态的。 +### 默认参数 -### Default parameters - -For the dynamic model, we adopt the following parameter values. +对于动态模型,我们采取以下参数值。 ```{code-cell} ipython3 default_μ = 0.0001 @@ -491,11 +442,9 @@ default_S0 = 10 default_h0 = 0 ``` +(这里`default_S0` 是 $S_0$ ,`default_h0` 是 $h_0$。) - -(Here `default_S0` is $S_0$ and `default_h0` is $h_0$.) - -For the option we use the following defaults. +对于期权,我们使用以下默认值。 ```{code-cell} ipython3 default_K = 100 @@ -503,15 +452,13 @@ default_n = 10 default_β = 0.95 ``` +### 可视化 - -### Visualizations - -With $s_t := \ln S_t$, the price dynamics become +将 $s_t := \ln S_t$,价格动态变为 $$ s_{t+1} = s_t + \mu + \exp(h_t) \xi_{t+1} $$ -Here is a function to simulate a path using this equation: +以下是使用这个方程模拟路径的函数: ```{code-cell} ipython3 def simulate_asset_price_path(μ=default_μ, S0=default_S0, h0=default_h0, n=default_n, ρ=default_ρ, ν=default_ν): @@ -527,13 +474,12 @@ def simulate_asset_price_path(μ=default_μ, S0=default_S0, h0=default_h0, n=def ``` - -Here we plot the paths and the log of the paths. +这里我们绘制路径及其对数路径。 ```{code-cell} ipython3 fig, axes = plt.subplots(2, 1) -titles = 'log paths', 'paths' +titles = '对数路径', '路径' transforms = np.log, lambda x: x for ax, transform, title in zip(axes, transforms, titles): for i in range(50): @@ -545,17 +491,13 @@ fig.tight_layout() plt.show() ``` +### 计算价格 +现在我们的模型更加复杂,我们无法轻易确定 $S_n$ 的分布。 -### Computing the price - -Now that our model is more complicated, we cannot easily determine the -distribution of $S_n$. - -So to compute the price $P$ of the option, we use Monte Carlo. +所以为了计算期权的价格 $P$,我们使用蒙特卡洛方法。 -We average over realizations $S_n^1, \ldots, S_n^M$ of $S_n$ and appealing to -the law of large numbers: +我们对实现 $S_n^1, \ldots, S_n^M$ 的平均值 $S_n$,并呼吁大数法则: $$ \mathbb E \max\{ S_n - K, 0 \} @@ -563,8 +505,7 @@ $$ \frac{1}{M} \sum_{m=1}^M \max \{S_n^m - K, 0 \} $$ - -Here's a version using Python loops. +下面是一个使用 Python 循环的版本。 ```{code-cell} ipython3 def compute_call_price(β=default_β, @@ -577,15 +518,15 @@ def compute_call_price(β=default_β, ν=default_ν, M=10_000): current_sum = 0.0 - # For each sample path + # 对每一个样本路径 for m in range(M): s = np.log(S0) h = h0 - # Simulate forward in time + # 模拟时间前进 for t in range(n): s = s + μ + np.exp(h) * randn() h = ρ * h + ν * randn() - # And add the value max{S_n - K, 0} to current_sum + # 并将值 max{S_n - K, 0} 加到 current_sum current_sum += np.maximum(np.exp(s) - K, 0) return β**n * current_sum / M @@ -597,18 +538,16 @@ compute_call_price() ``` - -## Exercises +## 练习 ```{exercise} :label: monte_carlo_ex1 -We would like to increase $M$ in the code above to make the calculation more -accurate. +我们想要在上面的代码中增加 $M$ 以使计算更加精确。 -But this is problematic because Python loops are slow. +但是这存在一个问题,因为Python循环运行缓慢。 -Your task is to write a faster version of this code using NumPy. +你的任务是使用NumPy编写一个更快的代码版本。 ``` ```{solution-start} monte_carlo_ex1 @@ -643,10 +582,9 @@ compute_call_price_vector() ``` +请注意,这个版本的速度比使用Python循环的版本要快。 -Notice that this version is faster than the one using a Python loop. - -Now let's try with larger $M$ to get a more accurate calculation. +现在让我们尝试更大的$M$以获得更准确的计算。 ```{code-cell} ipython3 %%time @@ -661,13 +599,13 @@ compute_call_price(M=10_000_000) ```{exercise} :label: monte_carlo_ex2 -Consider that a European call option may be written on an underlying with spot price of \$100 and a knockout barrier of \$120. +设想一种欧式看涨期权,该期权的标的资产现价为100美元,有一个120美元的敲出障碍。 -This option behaves in every way like a vanilla European call, except if the spot price ever moves above \$120, the option "knocks out" and the contract is null and void. +这种期权在各方面都类似于普通的欧式看涨期权,但如果现价曾经超过120美元,期权便会被"敲出",合约即刻失效。 -Note that the option does not reactivate if the spot price falls below \$120 again. +注意,如果现价再次跌破120美元,期权不会重新激活。 -Use the dynamics defined in {eq}`s_mc_dyms` to price the European call option. +使用{eq}`s_mc_dyms`定义的动态来定价这个欧式看涨期权。 ``` ```{solution-start} monte_carlo_ex2 @@ -686,6 +624,7 @@ default_β = 0.95 default_bp = 120 ``` + ```{code-cell} ipython3 def compute_call_price_with_barrier(β=default_β, μ=default_μ, @@ -698,13 +637,13 @@ def compute_call_price_with_barrier(β=default_β, bp=default_bp, M=50_000): current_sum = 0.0 - # For each sample path + # 对每个样本路径进行模拟 for m in range(M): s = np.log(S0) h = h0 payoff = 0 option_is_null = False - # Simulate forward in time + # 模拟前向时间发展 for t in range(n): s = s + μ + np.exp(h) * randn() h = ρ * h + ν * randn() @@ -715,7 +654,7 @@ def compute_call_price_with_barrier(β=default_β, if not option_is_null: payoff = np.maximum(np.exp(s) - K, 0) - # And add the payoff to current_sum + # 将payoff加入到current_sum中 current_sum += payoff return β**n * current_sum / M @@ -725,9 +664,7 @@ def compute_call_price_with_barrier(β=default_β, %time compute_call_price_with_barrier() ``` - - -Let's look at the vectorized version which is faster than using Python loops. +来看向量化版本,这个版本比使用Python循环快。 ```{code-cell} ipython3 def compute_call_price_with_barrier_vector(β=default_β, @@ -747,10 +684,10 @@ def compute_call_price_with_barrier_vector(β=default_β, Z = np.random.randn(2, M) s = s + μ + np.exp(h) * Z[0, :] h = ρ * h + ν * Z[1, :] - # Mark all the options null where S_n > barrier price + # 标记所有股价高于屏障价格的期权为无效 option_is_null = np.where(np.exp(s) > bp, True, option_is_null) - # mark payoff as 0 in the indices where options are null + # 在option_is_null的索引处将payoff标记为0 payoff = np.where(option_is_null, 0, np.maximum(np.exp(s) - K, 0)) expectation = np.mean(payoff) return β**n * expectation @@ -761,4 +698,4 @@ def compute_call_price_with_barrier_vector(β=default_β, ``` ```{solution-end} -``` +``` \ No newline at end of file diff --git a/lectures/networks.md b/lectures/networks.md index e207204..f4d9546 100644 --- a/lectures/networks.md +++ b/lectures/networks.md @@ -11,7 +11,7 @@ kernelspec: name: python3 --- -# Networks +# 网络 ```{code-cell} ipython3 :tags: [hide-output] @@ -19,35 +19,18 @@ kernelspec: !pip install quantecon-book-networks pandas-datareader ``` -## Outline +## 概述 -In recent years there has been rapid growth in a field called [network science](https://en.wikipedia.org/wiki/Network_science). - -Network science studies relationships between groups of objects. - -One important example is the [world wide web](https://en.wikipedia.org/wiki/World_Wide_Web#Linking) -, where web pages are connected by hyperlinks. - -Another is the [human brain](https://en.wikipedia.org/wiki/Neural_circuit): studies of brain function emphasize the network of -connections between nerve cells (neurons). - -[Artificial neural networks](https://en.wikipedia.org/wiki/Artificial_neural_network) are based on this idea, using data to build -intricate connections between simple processing units. - -Epidemiologists studying [transmission of diseases](https://en.wikipedia.org/wiki/Network_medicine#Network_epidemics) -like COVID-19 analyze interactions between groups of human hosts. - -In operations research, network analysis is used to study fundamental problems -as on minimum cost flow, the traveling salesman, [shortest paths](https://en.wikipedia.org/wiki/Shortest_path_problem), -and assignment. - -This lecture gives an introduction to economic and financial networks. - -Some parts of this lecture are drawn from the text -https://networks.quantecon.org/ but the level of this lecture is more -introductory. - -We will need the following imports. +近年来,一个被称为[网络科学](https://en.wikipedia.org/wiki/Network_science)的领域迅速发展。 +网络科学研究对象群体之间的关系。 +一个重要的例子是[万维网](https://en.wikipedia.org/wiki/World_Wide_Web#Linking),其中网页通过超链接相互连接。 +另一个例子是[人脑](https://en.wikipedia.org/wiki/Neural_circuit):对大脑功能的研究强调神经细胞(神经元)之间的连接网络。 +[人工神经网络](https://en.wikipedia.org/wiki/Artificial_neural_network)基于这一理念,利用数据在简单处理单元之间建立复杂的连接。 +研究COVID-19等[疾病传播](https://en.wikipedia.org/wiki/Network_medicine#Network_epidemics)的流行病学家分析人类宿主群体之间的相互作用。 +在运筹学中,网络分析用于研究基本问题,如最小成本流、旅行商问题、[最短路径](https://en.wikipedia.org/wiki/Shortest_path_problem)和分配问题。 +本讲座介绍了经济和金融网络。 +本讲座的部分内容来自教材书[《经济网络》](https://networks.quantecon.org),但本讲座的水平更为入门。 +我们需要以下导入。 ```{code-cell} ipython3 import numpy as np @@ -63,34 +46,29 @@ import quantecon_book_networks.data as qbn_data import matplotlib.patches as mpatches ``` -## Economic and financial networks - -Within economics, important examples of networks include - -* financial networks -* production networks -* trade networks -* transport networks and -* social networks +## 经济和金融网络 -Social networks affect trends in market sentiment and consumer decisions. +在经济学中,网络的重要例子包括: +* 金融网络 +* 生产网络 +* 贸易网络 +* 运输网络 +* 社交网络 -The structure of financial networks helps to determine relative fragility of the financial system. +社交网络影响市场情绪和消费者决策的趋势。 +金融网络的结构有助于确定金融系统的相对脆弱性。 +生产网络的结构影响贸易、创新和局部冲击的传播。 +为了更好地理解这些网络,让我们深入看一些例子。 -The structure of production networks affects trade, innovation and the propagation of local shocks. +### 例子:飞机出口 -To better understand such networks, let's look at some examples in more depth. - - -### Example: Aircraft Exports - -The following figure shows international trade in large commercial aircraft in 2019 based on International Trade Data SITC Revision 2. +下图显示了基于国际贸易数据SITC第2修订版的2019年大型商用飞机国际贸易情况。 ```{code-cell} ipython3 --- mystnb: figure: - caption: "Commercial Aircraft Network \n" + caption: "商用飞机网络 \n" name: aircraft_network tags: [hide-input] --- @@ -144,96 +122,64 @@ nx.draw_networkx_edges(DG, plt.show() ``` -The circles in the figure are called **nodes** or **vertices** -- in this case they represent countries. - -The arrows in the figure are called **edges** or **links**. - -Node size is proportional to total exports and edge width is proportional to exports to the target country. - -(The data is for trade in commercial aircraft weighing at least 15,000kg and was sourced from CID Dataverse.) - -The figure shows that the US, France and Germany are major export hubs. - -In the discussion below, we learn to quantify such ideas. - - -### Example: A Markov Chain +图中的圆圈被称为**节点**或**顶点** -- 在这个例子中,它们代表国家。 +图中的箭头被称为**边**或**链接**。 +节点大小与总出口成正比,边的宽度与对目标国家的出口成正比。 +(数据针对重量至少15,000公斤的商用飞机贸易,数据来源于CID Dataverse。) +图表显示美国、法国和德国是主要的出口枢纽。 +在下面的讨论中,我们将学习如何量化这些概念。 -Recall that, in our lecture on {ref}`Markov chains ` we studied a dynamic model of business cycles -where the states are +### 例子:马尔可夫链 -* "ng" = "normal growth" -* "mr" = "mild recession" -* "sr" = "severe recession" +回想一下,在我们关于{ref}`马尔可夫链 `的讲座中,我们研究了一个商业周期的动态模型,其中的状态包括: +* "ng" = "正常增长" +* "mr" = "轻度衰退" +* "sr" = "严重衰退" -Let's examine the following figure +让我们来看看下面这个图表 ```{image} /_static/lecture_specific/networks/mc.png :name: mc_networks :align: center ``` -This is an example of a network, where the set of nodes $V$ equals the states: - +这是一个网络的例子,其中节点集 $V$ 等于状态集: $$ V = \{ \text{"ng", "mr", "sr"} \} $$ +节点之间的边表示一个月的转移概率。 -The edges between the nodes show the one month transition probabilities. - - -## An introduction to graph theory - -Now we've looked at some examples, let's move on to theory. - -This theory will allow us to better organize our thoughts. - -The theoretical part of network science is constructed using a major branch of -mathematics called [graph theory](https://en.wikipedia.org/wiki/Graph_theory). - -Graph theory can be complicated and we will cover only the basics. - -However, these concepts will already be enough for us to discuss interesting and -important ideas on economic and financial networks. - -We focus on "directed" graphs, where connections are, in general, asymmetric -(arrows typically point one way, not both ways). - -E.g., +## 图论简介 -* bank $A$ lends money to bank $B$ -* firm $A$ supplies goods to firm $B$ -* individual $A$ "follows" individual $B$ on a given social network +现在我们已经看过一些例子,让我们转向理论部分。 +这个理论将帮助我们更好地组织我们的思路。 +网络科学的理论部分是使用数学的一个主要分支——[图论](https://en.wikipedia.org/wiki/Graph_theory)构建的。 +图论可能很复杂,我们只会涉及基础部分。 +但是,这些概念已经足以让我们讨论有关经济和金融网络的有趣且重要的想法。 +我们关注"有向"图,其中连接通常是不对称的(箭头通常只指向一个方向,而不是双向)。 +例如, +* 银行 $A$ 向银行 $B$ 贷款 +* 公司 $A$ 向公司 $B$ 供应商品 +* 个人 $A$ 在特定社交网络上"关注"个人 $B$ +("无向"图,即连接是对称的,是有向图的一种特殊情况——我们只需要坚持每个从 $A$ 指向 $B$ 的箭头都配对一个从 $B$ 指向 $A$ 的箭头。) -("Undirected" graphs, where connections are symmetric, are a special -case of directed graphs --- we just need to insist that each arrow pointing -from $A$ to $B$ is paired with another arrow pointing from $B$ to $A$.) +### 关键定义 +一个**有向图**由两个部分组成: +1. 一个有限集 $V$ 和 +2. 一组元素为 $V$ 中的元素的对 $(u, v)$。 -### Key definitions +$V$ 的元素被称为图的**顶点**或**节点**。 +对 $(u,v)$ 被称为图的**边**,所有边的集合通常用 $E$ 表示。 +直观和视觉上,边 $(u,v)$ 被理解为从节点 $u$ 到节点 $v$ 的一个箭头。 +(表示箭头的一个巧妙方法是记录箭头的尾部和头部的位置,这正是边所做的。) -A **directed graph** consists of two things: +在 {numref}`aircraft_network` 所示的飞机出口例子中 +* $V$ 是数据集中包含的所有国家。 +* $E$ 是图中的所有箭头,每个箭头表示从一个国家到另一个国家的某个正数量的飞机出口。 -1. a finite set $V$ and -1. a collection of pairs $(u, v)$ where $u$ and $v$ are elements of $V$. - -The elements of $V$ are called the **vertices** or **nodes** of the graph. - -The pairs $(u,v)$ are called the **edges** of the graph and the set of all edges will usually be denoted by $E$ - -Intuitively and visually, an edge $(u,v)$ is understood as an arrow from node $u$ to node $v$. - -(A neat way to represent an arrow is to record the location of the tail and -head of the arrow, and that's exactly what an edge does.) - -In the aircraft export example shown in {numref}`aircraft_network` - -* $V$ is all countries included in the data set. -* $E$ is all the arrows in the figure, each indicating some positive amount of aircraft exports from one country to another. - -Let's look at more examples. - -Two graphs are shown below, each with three nodes. +让我们看更多的例子。 +下面显示了两个图,每个图都有三个节点。 ```{figure} /_static/lecture_specific/networks/poverty_trap_1.png :name: poverty_trap_1 @@ -243,46 +189,33 @@ Poverty Trap +++ -We now construct a graph with the same nodes but different edges. +现在,我们构造一个具有相同节点但具有不同边的图。 ```{figure} /_static/lecture_specific/networks/poverty_trap_2.png :name: poverty_trap_2 -Poverty Trap +贫困陷阱 ``` +++ -For these graphs, the arrows (edges) can be thought of as representing -positive transition probabilities over a given unit of time. - -In general, if an edge $(u, v)$ exists, then the node $u$ is called a -**direct predecessor** of $v$ and $v$ is called a **direct successor** of $u$. +对于这些图,箭头(边)可以被视为表示给定时间单位内的正向转移概率。 +通常,如果存在边 $(u, v)$,则称节点 $u$ 为 $v$ 的**直接前驱**,$v$ 为 $u$ 的**直接后继**。 +此外,对于 $v \in V$, +* **入度**是 $i_d(v) = $ $v$ 的直接前驱数,以及 +* **出度**是 $o_d(v) = $ $v$ 的直接后继数。 -Also, for $v \in V$, - -* the **in-degree** is $i_d(v) = $ the number of direct predecessors of $v$ and -* the **out-degree** is $o_d(v) = $ the number of direct successors of $v$. - - -### Digraphs in Networkx - -The Python package [Networkx](https://networkx.org/) provides a convenient -data structure for representing directed graphs and implements many common -routines for analyzing them. - -As an example, let us recreate {numref}`poverty_trap_2` using Networkx. - -To do so, we first create an empty `DiGraph` object: +### Networkx中的有向图 +Python包 [Networkx](https://networkx.org/) 为表示有向图提供了一个便捷的数据结构,并实现了许多用于分析它们的常见例程。 +作为示例,让我们使用Networkx重新创建 {numref}`poverty_trap_2`。 +为此,我们首先创建一个空的 `DiGraph` 对象: ```{code-cell} ipython3 G_p = nx.DiGraph() ``` -Next we populate it with nodes and edges. - -To do this we write down a list of -all edges, with *poor* represented by *p* and so on: +接下来,我们用节点和边来填充它。 +为此,我们列出所有边的列表,其中*贫穷*用*p*表示,以此类推: ```{code-cell} ipython3 edge_list = [('p', 'p'), @@ -290,7 +223,7 @@ edge_list = [('p', 'p'), ('r', 'p'), ('r', 'm'), ('r', 'r')] ``` -Finally, we add the edges to our `DiGraph` object: +最后,我们将边添加到我们的 `DiGraph` 对象中: ```{code-cell} ipython3 for e in edge_list: @@ -298,16 +231,14 @@ for e in edge_list: G_p.add_edge(u, v) ``` -Alternatively, we can use the method `add_edges_from`. +或者,我们可以使用 `add_edges_from` 方法。 ```{code-cell} ipython3 G_p.add_edges_from(edge_list) ``` -Adding the edges automatically adds the nodes, so `G_p` is now a -correct representation of our graph. - -We can verify this by plotting the graph via Networkx with the following code: +添加边会自动添加节点,所以 `G_p` 现在是我们图的正确表示。 +我们可以通过使用以下代码通过Networkx绘制图来验证这一点: ```{code-cell} ipython3 fig, ax = plt.subplots() @@ -317,45 +248,36 @@ nx.draw_spring(G_p, ax=ax, node_size=500, with_labels=True, plt.show() ``` -The figure obtained above matches the original directed graph in {numref}`poverty_trap_2`. - - -`DiGraph` objects have methods that calculate in-degree and out-degree -of nodes. - -For example, +上面得到的图形与{numref}`poverty_trap_2`中的原始有向图相匹配。 +`DiGraph`对象有计算节点入度和出度的方法。 +例如, ```{code-cell} ipython3 G_p.in_degree('p') ``` (strongly_connected)= -### Communication - -Next, we study communication and connectedness, which have important -implications for economic networks. - -Node $v$ is called **accessible** from node $u$ if either $u=v$ or there -exists a sequence of edges that lead from $u$ to $v$. +### 通信 -* in this case, we write $u \to v$ +接下来,我们研究通信和连通性,这对经济网络有重要影响。 -(Visually, there is a sequence of arrows leading from $u$ to $v$.) +如果$u=v$或存在一系列从$u$到$v$的边,则称节点$v$从节点$u$**可达**。 +* 在这种情况下,我们写作$u \to v$ +(从视觉上看,有一系列箭头从$u$指向$v$。) -For example, suppose we have a directed graph representing a production network, where +例如,假设我们有一个表示生产网络的有向图,其中 +* $V$的元素是工业部门 +* 边$(i, j)$的存在意味着$i$向$j$供应产品或服务。 -* elements of $V$ are industrial sectors and -* existence of an edge $(i, j)$ means that $i$ supplies products or services to $j$. +那么$m \to \ell$意味着部门$m$是部门$\ell$的上游供应商。 -Then $m \to \ell$ means that sector $m$ is an upstream supplier of sector $\ell$. +如果$u \to v$且$v \to u$,则称两个节点$u$和$v$**相通**。 -Two nodes $u$ and $v$ are said to **communicate** if both $u \to v$ and $v \to u$. +如果所有节点都相通,则称图是**强连通的**。 -A graph is called **strongly connected** if all nodes communicate. +例如,{numref}`poverty_trap_1`是强连通的, +然而在{numref}`poverty_trap_2`中,富人节点从穷人节点不可达,因此它不是强连通的。 -For example, {numref}`poverty_trap_1` is strongly connected -however in {numref}`poverty_trap_2` rich is not accessible from poor, thus it is not strongly connected. - -We can verify this by first constructing the graphs using Networkx and then using `nx.is_strongly_connected`. +我们可以通过首先使用Networkx构建图,然后使用`nx.is_strongly_connected`来验证这一点。 ```{code-cell} ipython3 fig, ax = plt.subplots() @@ -369,7 +291,7 @@ nx.draw_networkx(G1, with_labels = True) ``` ```{code-cell} ipython3 -nx.is_strongly_connected(G1) #checking if above graph is strongly connected +nx.is_strongly_connected(G1) #检查上面的图是否强关联 ``` ```{code-cell} ipython3 @@ -384,25 +306,20 @@ nx.draw_networkx(G2, with_labels = True) ``` ```{code-cell} ipython3 -nx.is_strongly_connected(G2) #checking if above graph is strongly connected +nx.is_strongly_connected(G2) #检查上面的图是否强关联 ``` -## Weighted graphs - -We now introduce weighted graphs, where weights (numbers) are attached to each -edge. +## 加权图 +我们现在介绍加权图,其中每条边都附有权重(数字)。 - -### International private credit flows by country - -To motivate the idea, consider the following figure which shows flows of funds (i.e., -loans) between private banks, grouped by country of origin. +### 按国家划分的国际私人信贷流动 +为了说明这个概念,请看下图,它展示了私人银行之间的资金流动(即贷款),按原始国家分组。 ```{code-cell} ipython3 --- mystnb: figure: - caption: "International Credit Network \n" + caption: "国际信贷网络 \n" name: financial_network tags: [hide-input] --- @@ -460,90 +377,76 @@ nx.draw_networkx_edges(G, plt.show() ``` -The country codes are given in the following table - -|Code| Country |Code| Country |Code| Country |Code| Country | -|:--:|:--------------|:--:|:-------:|:--:|:-----------:|:--:|:--------------:| -| AU | Australia | DE | Germany | CL | Chile | ES | Spain | -| PT | Portugal | FR | France | TR | Turkey | GB | United Kingdom | -| US | United States | IE | Ireland | AT | Austria | IT | Italy | -| BE | Belgium | JP | Japan | SW | Switzerland | SE | Sweden | +国家代码在下表中给出 +|代码| 国家 |代码| 国家 |代码| 国家 |代码| 国家 | +|:--:|:-----------|:--:|:----:|:--:|:--------:|:--:|:-----------:| +| AU | 澳大利亚 | DE | 德国 | CL | 智利 | ES | 西班牙 | +| PT | 葡萄牙 | FR | 法国 | TR | 土耳其 | GB | 英国 | +| US | 美国 | IE | 爱尔兰| AT | 奥地利 | IT | 意大利 | +| BE | 比利时 | JP | 日本 | SW | 瑞士 | SE | 瑞典 | -An arrow from Japan to the US indicates aggregate claims held by Japanese -banks on all US-registered banks, as collected by the Bank of International -Settlements (BIS). +从日本到美国的箭头表示日本银行对所有在美国注册的银行的总体债权,这些数据由国际清算银行(BIS)收集。 -The size of each node in the figure is increasing in the -total foreign claims of all other nodes on this node. +图中每个节点的大小与所有其他节点对该节点的外国债权总额成正比。 -The widths of the arrows are proportional to the foreign claims they represent. +箭头的宽度与它们所代表的外国债权成正比。 -Notice that, in this network, an edge $(u, v)$ exists for almost every choice -of $u$ and $v$ (i.e., almost every country in the network). +请注意,在这个网络中,几乎对于每一对节点$u$和$v$都存在一条边$(u, v)$(即网络中几乎每个国家之间都有联系)。 -(In fact, there are even more small arrows, which we have dropped for clarity.) +(事实上,还有更多的小箭头,为了清晰起见我们省略了。) -Hence the existence of an edge from one node to another is not particularly informative. +因此,仅仅一个节点到另一个节点边的存在与否并不特别有信息量。 -To understand the network, we need to record not just the existence or absence -of a credit flow, but also the size of the flow. +为了理解这个网络,我们需要记录的不仅是信贷流动的存在或缺失,还要记录流动的规模。 -The correct data structure for recording this information is a "weighted -directed graph". +记录这些信息的正确数据结构是"加权有向图"。 +++ -### Definitions +### 定义 -A **weighted directed graph** is a directed graph to which we have added a -**weight function** $w$ that assigns a positive number to each edge. +**加权有向图**是一种有向图,我们为其添加了一个**权重函数**$w$,该函数为每条边分配一个正数。 -The figure above shows one weighted directed graph, where the weights are the size of fund flows. - -The following figure shows a weighted directed graph, with arrows -representing edges of the induced directed graph. +上图显示了一个加权有向图,其中权重是资金流动的大小。 +下图显示了一个加权有向图,箭头表示诱导有向图的边。 ```{figure} /_static/lecture_specific/networks/weighted.png :name: poverty_trap_weighted -Weighted Poverty Trap +加权贫困陷阱 ``` -The numbers next to the edges are the weights. - -In this case, you can think of the numbers on the arrows as transition -probabilities for a household over, say, one year. +边旁边的数字是权重。 -We see that a rich household has a 10\% chance of becoming poor in one year. +在这个例子中,你可以把箭头上的数字看作是一个家庭在一年内的转移概率。 +我们可以看到,一个富裕家庭在一年内有10%的机会变成贫困。 -## Adjacency matrices +## 邻接矩阵 -Another way that we can represent weights, which turns out to be very -convenient for numerical work, is via a matrix. +另一种表示权重的方法是通过矩阵,这种方法在数值计算中非常方便。 -The **adjacency matrix** of a weighted directed graph with nodes $\{v_1, \ldots, v_n\}$, edges $E$ and weight function $w$ is the matrix +对于一个有节点集 $\{v_1, \ldots, v_n\}$、边集 $E$ 和权重函数 $w$ 的加权有向图,其**邻接矩阵**是一个矩阵 $$ A = (a_{ij})_{1 \leq i,j \leq n} -\quad \text{with} \quad +\quad \text{其中} \quad a_{ij} = % \begin{cases} - w(v_i, v_j) & \text{ if } (v_i, v_j) \in E + w(v_i, v_j) & \text{如果} (v_i, v_j) \in E \\ - 0 & \text{ otherwise}. + 0 & \text{其他情况} \end{cases} % $$ -Once the nodes in $V$ are enumerated, the weight function and -adjacency matrix provide essentially the same information. +一旦 $V$ 中的节点被列举出来,权重函数和邻接矩阵本质上提供相同的信息。 -For example, with $\{$poor, middle, rich$\}$ mapped to $\{1, 2, 3\}$ respectively, -the adjacency matrix corresponding to the weighted directed graph in {numref}`poverty_trap_weighted` is +例如,将 $\{$贫困, 中产, 富裕$\}$ 分别映射到 $\{1, 2, 3\}$, +{numref}`poverty_trap_weighted` 中加权有向图对应的邻接矩阵是 $$ \begin{pmatrix} @@ -553,24 +456,19 @@ $$ \end{pmatrix}. $$ -In QuantEcon's `DiGraph` implementation, weights are recorded via the -keyword `weighted`: +在 QuantEcon 的 `DiGraph` 实现中,权重通过关键字 `weighted` 记录: ```{code-cell} ipython3 A = ((0.9, 0.1, 0.0), (0.4, 0.4, 0.2), (0.1, 0.1, 0.8)) A = np.array(A) -G = qe.DiGraph(A, weighted=True) # store weights +G = qe.DiGraph(A, weighted=True) # 储存权重 ``` -One of the key points to remember about adjacency matrices is that taking the -transpose _reverses all the arrows_ in the associated directed graph. - - -For example, the following directed graph can be -interpreted as a stylized version of a financial network, with nodes as banks -and edges showing the flow of funds. +关于邻接矩阵的一个关键点是,对其进行转置操作会*反转相关有向图中的所有箭头*。 +例如,以下有向图可以被解释为一个金融网络的简化版本,其中节点代表银行, +边表示资金流动。 ```{code-cell} ipython3 G4 = nx.DiGraph() @@ -596,10 +494,8 @@ nx.draw_networkx_nodes(G4, pos, linewidths= 0.5, edgecolors = 'black', plt.show() ``` -We see that bank 2 extends a loan of size 200 to bank 3. - -The corresponding adjacency matrix is - +我们看到银行2向银行3发放了200的贷款。 +对应的邻接矩阵是 $$ A = \begin{pmatrix} @@ -610,9 +506,7 @@ A = 150 & 0 & 250 & 300 & 0 \end{pmatrix}. $$ - -The transpose is - +其转置是 $$ A^\top = \begin{pmatrix} @@ -623,10 +517,8 @@ A^\top = 0 & 0 & 0 & 50 & 0 \end{pmatrix}. $$ - -The corresponding network is visualized in the following figure which shows the network of liabilities after the loans have been granted. - -Both of these networks (original and transpose) are useful for analyzing financial markets. +相应的网络在下图中可视化,显示了贷款发放后的负债网络。 +这两个网络(原始和转置)对于分析金融市场都很有用。 ```{code-cell} ipython3 G5 = nx.DiGraph() @@ -651,64 +543,47 @@ nx.draw_networkx_nodes(G5, pos, linewidths= 0.5, edgecolors = 'black', plt.show() ``` -In general, every nonnegative $n \times n$ matrix $A = (a_{ij})$ can be -viewed as the adjacency matrix of a weighted directed graph. - -To build the graph we set $V = 1, \ldots, n$ and take the edge set $E$ to be -all $(i,j)$ such that $a_{ij} > 0$. - -For the weight function we set $w(i, j) = a_{ij}$ for all edges $(i,j)$. +一般来说,每个非负的 $n \times n$ 矩阵 $A = (a_{ij})$ 都可以被视为加权有向图的邻接矩阵。 +要构建图,我们设 $V = 1, \ldots, n$,并将边集 $E$ 取为所有满足 $a_{ij} > 0$ 的 $(i,j)$。 +对于权重函数,我们对所有边 $(i,j)$ 设置 $w(i, j) = a_{ij}$。 +我们称这个图为 $A$ 诱导的加权有向图。 -We call this graph the weighted directed graph induced by $A$. - - -## Properties - -Consider a weighted directed graph with adjacency matrix $A$. - -Let $a^k_{ij}$ be element $i,j$ of $A^k$, the $k$-th power of $A$. - -The following result is useful in many applications: +## 性质 +考虑一个加权有向图,其邻接矩阵为 $A$。 +令 $a^k_{ij}$ 为 $A^k$($A$ 的 $k$ 次方)中的第 $i,j$ 个元素。 +以下结果在许多应用中很有用: ````{prf:theorem} :label: graph_theory_property1 -For distinct nodes $i, j$ in $V$ and any integer $k$, we have - +对于 $V$ 中的不同节点 $i, j$ 和任意整数 $k$,我们有 $$ a^k_{i j} > 0 -\quad \text{if and only if} \quad -\text{ $j$ is accessible from $i$}. +\quad \text{当且仅当} \quad +\text{$j$ 可从 $i$ 到达}。 $$ ```` +++ -The above result is obvious when $k=1$ and a proof of the general case can be -found in {cite}`sargent2022economic`. - -Now recall from the eigenvalues lecture that a -nonnegative matrix $A$ is called {ref}`irreducible` if for each $(i,j)$ there is an integer $k \geq 0$ such that $a^{k}_{ij} > 0$. - -From the preceding theorem, it is not too difficult (see -{cite}`sargent2022economic` for details) to get the next result. +当 $k=1$ 时,上述结果是显而易见的,对于一般情况的证明可以在 {cite}`sargent2022economic` 中找到。 +现在回想特征值讲座中提到,一个非负矩阵 $A$ 被称为{ref}`不可约的`,如果对于每个 $(i,j)$,存在一个整数 $k \geq 0$,使得 $a^{k}_{ij} > 0$。 +根据前面的定理,不难得出下一个结果(详见 {cite}`sargent2022economic`)。 ````{prf:theorem} :label: graph_theory_property2 -For a weighted directed graph the following statements are equivalent: - -1. The directed graph is strongly connected. -2. The adjacency matrix of the graph is irreducible. +对于一个加权有向图,以下陈述是等价的: +1. 该有向图是强连通的。 +2. 该图的邻接矩阵是不可约的。 ```` +++ -We illustrate the above theorem with a simple example. - -Consider the following weighted directed graph. +我们用一个简单的例子来说明上述定理。 +考虑以下加权有向图。 ```{image} /_static/lecture_specific/networks/properties.png @@ -718,7 +593,7 @@ Consider the following weighted directed graph. +++ -We first create the above network as a Networkx `DiGraph` object. +我们首先将上述网络创建为 Networkx `DiGraph` 对象。 ```{code-cell} ipython3 G6 = nx.DiGraph() @@ -728,10 +603,10 @@ G6.add_edges_from([('1','2'),('1','3'), ('3','1'),('3','2')]) ``` -Then we construct the associated adjacency matrix A. +然后我们构建相关的邻接矩阵A。 ```{code-cell} ipython3 -A = np.array([[0,0.7,0.3], # adjacency matrix A +A = np.array([[0,0.7,0.3], # 邻接矩阵A。 [1,0,0], [0.4,0.6,0]]) ``` @@ -748,46 +623,35 @@ def is_irreducible(P): ``` ```{code-cell} ipython3 -is_irreducible(A) # check irreducibility of A +is_irreducible(A) #检查A的不可约性 ``` ```{code-cell} ipython3 -nx.is_strongly_connected(G6) # check connectedness of graph +nx.is_strongly_connected(G6) # 检查图的连接性 ``` -## Network centrality +## 网络中心性 -When studying networks of all varieties, a recurring topic is the relative -"centrality" or "importance" of different nodes. +在研究各种网络时,一个反复出现的话题是不同节点的相对"中心性"或"重要性"。 +例子包括: +* 搜索引擎对网页的排名 +* 确定金融网络中最重要的银行(在金融危机时中央银行应该救助哪一家) +* 确定经济中最重要的工业部门 -Examples include +在接下来的内容中,**中心性度量**将每个加权有向图与一个向量$m$关联起来,其中$m_i$被解释为节点$v_i$的中心性(或排名)。 -* ranking of web pages by search engines -* determining the most important bank in a financial network (which one a - central bank should rescue if there is a financial crisis) -* determining the most important industrial sector in an economy. +### 度中心性 -In what follows, a **centrality measure** associates to each weighted directed -graph a vector $m$ where the $m_i$ is interpreted as the centrality (or rank) -of node $v_i$. - -### Degree centrality - -Two elementary measures of "importance" of a node in a given directed -graph are its in-degree and out-degree. - -Both of these provide a centrality measure. - -In-degree centrality is a vector containing the in-degree of each node in -the graph. - -Consider the following simple example. +在给定的有向图中,衡量节点"重要性"的两个基本指标是其入度和出度。 +这两者都提供了一种中心性度量。 +入度中心性是一个包含图中每个节点入度的向量。 +考虑以下简单例子。 ```{code-cell} ipython3 --- mystnb: figure: - caption: Sample Graph + caption: 样本图 name: sample_gph_1 --- G7 = nx.DiGraph() @@ -810,18 +674,16 @@ nx.draw_networkx_nodes(G7, pos, linewidths=0.5, edgecolors='black', plt.show() ``` -The following code displays the in-degree centrality of all nodes. +以下代码显示了所有节点的入度中心性。 ```{code-cell} ipython3 -iG7 = [G7.in_degree(v) for v in G7.nodes()] # computing in-degree centrality - +iG7 = [G7.in_degree(v) for v in G7.nodes()] #计算入度中心性 for i, d in enumerate(iG7): print(i+1, d) ``` -Consider the international credit network displayed in {numref}`financial_network`. - -The following plot displays the in-degree centrality of each country. +考虑{numref}`financial_network`中显示的国际信贷网络。 +以下图表显示了每个国家的入度中心性。 ```{code-cell} ipython3 D = qbn_io.build_unweighted_matrix(Z) @@ -852,98 +714,64 @@ ax.set_ylim((0,20)) plt.show() ``` -Unfortunately, while in-degree and out-degree centrality are simple to -calculate, they are not always informative. - -In {numref}`financial_network`, an edge exists between almost every node, -so the in- or out-degree based centrality ranking fails to effectively separate the countries. - -This can be seen in the above graph as well. - -Another example is the task of a web search engine, which ranks pages -by relevance whenever a user enters a search. - -Suppose web page A has twice as many inbound links as page B. - -In-degree centrality tells us that page A deserves a higher rank. - -But in fact, page A might be less important than page B. - -To see why, suppose that the links to A are from pages that receive almost no traffic, -while the links to B are from pages that receive very heavy traffic. - -In this case, page B probably receives more visitors, which in turn suggests -that page B contains more valuable (or entertaining) content. - -Thinking about this point suggests that importance might be *recursive*. - -This means that the importance of a given node depends on the importance of -other nodes that link to it. - -As another example, we can imagine a production network where the importance of a -given sector depends on the importance of the sectors that it supplies. - -This reverses the order of the previous example: now the importance of a given -node depends on the importance of other nodes that *it links to*. - -The next centrality measures will have these recursive features. - - -### Eigenvector centrality - -Suppose we have a weighted directed graph with adjacency matrix $A$. - -For simplicity, we will suppose that the nodes $V$ of the graph are just the -integers $1, \ldots, n$. - -Let $r(A)$ denote the {ref}`spectral radius` of $A$. - -The **eigenvector centrality** of the graph is defined as the $n$-vector $e$ that solves - +虽然入度和出度中心性计算简单,但它们并不总是有用。 +在{numref}`financial_network`中,几乎每个节点之间都存在边, +所以基于入度或出度的中心性排名无法有效区分国家。 +这在上图中也可以看到。 +另一个例子是网络搜索引擎的任务,它在用户输入搜索时按相关性对页面进行排名。 +假设网页A的入站链接是网页B的两倍。 +入度中心性告诉我们页面A应该获得更高的排名。 +但实际上,页面A可能不如页面B重要。 +原因是,假设指向A的链接来自几乎没有流量的页面, +而指向B的链接来自流量非常大的页面。 +在这种情况下,页面B可能会收到更多访问者,这反过来表明 +页面B包含更有价值(或更有趣)的内容。 +思考这一点suggests重要性可能是*递归的*。 +这意味着给定节点的重要性取决于 +链接到它的其他节点的重要性。 +作为另一个例子,我们可以想象一个生产网络,其中一个 +给定部门的重要性取决于它供应的部门的重要性。 +这reverses了前面例子的顺序:现在给定节点的重要性 +取决于它*链接到的*其他节点的重要性。 +下面的中心性度量将具有这些递归特征。 + +## 特征向量中心性 + +假设我们有一个带有邻接矩阵$A$的加权有向图。 +为简单起见,我们假设图的节点$V$就是 +整数$1, \ldots, n$。 +让$r(A)$表示$A$的{ref}`谱半径`。 +图的**特征向量中心性**被定义为解决以下方程的$n$维向量$e$ $$ \begin{aligned} e = \frac{1}{r(A)} A e. \end{aligned} $$ (ev_central) - -In other words, $e$ is the dominant eigenvector of $A$ (the eigenvector of the -largest eigenvalue --- see the discussion of the {ref}`Perron-Frobenius theorem` in the eigenvalue lecture. - -To better understand {eq}`ev_central`, we write out the full expression -for some element $e_i$ - +换句话说,$e$是$A$的主特征向量(最大特征值的特征向量 --- +请看特征值lecture中关于{ref}`Perron-Frobenius定理`的讨论。 +为了更好地理解{eq}`ev_central`,我们写出某个元素$e_i$的完整表达式 $$ \begin{aligned} e_i = \frac{1}{r(A)} \sum_{1 \leq j \leq n} a_{ij} e_j \end{aligned} $$ (eq_eicen) - - -Note the recursive nature of the definition: the centrality obtained by node -$i$ is proportional to a sum of the centrality of all nodes, weighted by -the *rates of flow* from $i$ into these nodes. - -A node $i$ is highly ranked if -1. there are many edges leaving $i$, -2. these edges have large weights, and -3. the edges point to other highly ranked nodes. - -Later, when we study demand shocks in production networks, there will be a more -concrete interpretation of eigenvector centrality. - -We will see that, in production networks, sectors with high eigenvector -centrality are important *suppliers*. - -In particular, they are activated by a wide array of demand shocks once orders -flow backwards through the network. - -To compute eigenvector centrality we can use the following function. +注意定义的递归性质:节点$i$获得的中心性 +与所有节点中心性的加权和成正比,权重为 +从$i$到这些节点的*流量率*。 +如果节点$i$满足以下条件,则它的排名很高: +1. 从$i$出发的边很多, +2. 这些边的权重很大,以及 +3. 这些边指向其他排名很高的节点。 +稍后,当我们研究生产网络中的需求冲击时,特征向量中心性将有 +更具体的解释。 +我们将看到,在生产网络中,具有高特征向量中心性的部门是重要的*供应商*。 +特别是,一旦订单通过网络向后流动,它们就会被各种需求冲击激活。 +要计算特征向量中心性,我们可以使用以下函数。 ```{code-cell} ipython3 def eigenvector_centrality(A, k=40, authority=False): """ - Computes the dominant eigenvector of A. Assumes A is - primitive and uses the power method. + 计算矩阵A的主特征向量。假设A是本原矩阵,并使用幂法。 """ A_temp = A.T if authority else A @@ -953,10 +781,10 @@ def eigenvector_centrality(A, k=40, authority=False): return e / np.sum(e) ``` -Let's compute eigenvector centrality for the graph generated in {numref}`sample_gph_1`. +让我们为{numref}`sample_gph_1`中生成的图计算特征向量中心性。 ```{code-cell} ipython3 -A = nx.to_numpy_array(G7) # compute adjacency matrix of graph +A = nx.to_numpy_array(G7) #计算图的邻接矩阵 ``` ```{code-cell} ipython3 @@ -967,10 +795,8 @@ for i in range(n): print(i+1,e[i]) ``` -While nodes $2$ and $4$ had the highest in-degree centrality, we can see that nodes $1$ and $2$ have the -highest eigenvector centrality. - -Let's revisit the international credit network in {numref}`financial_network`. +虽然节点 $2$ 和 $4$ 具有最高的入度中心性,但我们可以看到节点 $1$ 和 $2$ 具有最高的特征向量中心性。 +让我们重新审视{numref}`financial_network`中的国际信贷网络。 ```{code-cell} ipython3 eig_central = eigenvector_centrality(Z) @@ -980,7 +806,7 @@ eig_central = eigenvector_centrality(Z) --- mystnb: figure: - caption: Eigenvector centrality + caption: 特征向量中心性 name: eigenvctr_centrality --- fig, ax = plt.subplots() @@ -995,118 +821,78 @@ ax.legend(handles=[patch], fontsize=12, loc="upper left", handlelength=0, frameo plt.show() ``` -Countries that are rated highly according to this rank tend to be important -players in terms of supply of credit. - -Japan takes the highest rank according to this measure, although -countries with large financial sectors such as Great Britain and France are -not far behind. - -The advantage of eigenvector centrality is that it measures a node's importance while considering the importance of its neighbours. +根据这个排名评分较高的国家往往在信贷供应方面扮演重要角色。 +日本在这项指标中排名最高,尽管像英国和法国这样拥有大型金融部门的国家也紧随其后。 +特征向量中心性的优势在于它在衡量节点重要性的同时考虑了其邻居的重要性。 +谷歌的PageRank算法的核心就是特征向量中心性的一个变体,用于对网页进行排名。 +其主要原理是来自重要节点(以度中心性衡量)的链接比来自不重要节点的链接更有价值。 -A variant of eigenvector centrality is at the core of Google's PageRank algorithm, which is used to rank web pages. +### 卡茨中心性 -The main principle is that links from important nodes (as measured by degree centrality) are worth more than links from unimportant nodes. +特征向量中心性的一个问题是$r(A)$可能为零,这种情况下$1/r(A)$是未定义的。 +出于这个和其他原因,一些研究者更倾向于使用另一种称为卡茨中心性的网络中心性度量。 +固定$\beta$在$(0, 1/r(A))$区间内,带权有向图的**卡茨中心性**定义为解决以下方程的向量$\kappa$: - -### Katz centrality - -One problem with eigenvector centrality is that $r(A)$ might be zero, in which -case $1/r(A)$ is not defined. - -For this and other reasons, some researchers prefer another measure of -centrality for networks called Katz centrality. - -Fixing $\beta$ in $(0, 1/r(A))$, the **Katz centrality** of a weighted -directed graph with adjacency matrix $A$ is defined as the vector $\kappa$ -that solves - $$ \kappa_i = \beta \sum_{1 \leq j 1} a_{ij} \kappa_j + 1 -\qquad \text{for all } i \in \{0, \ldots, n-1\}. +\qquad \text{对所有 } i \in \{0, \ldots, n-1\}。 $$ (katz_central) -Here $\beta$ is a parameter that we can choose. - -In vector form we can write +这里$\beta$是我们可以选择的参数。 +用向量形式我们可以写成: $$ \kappa = \mathbf 1 + \beta A \kappa $$ (katz_central_vec) -where $\mathbf 1$ is a column vector of ones. - -The intuition behind this centrality measure is similar to that provided for -eigenvector centrality: high centrality is conferred on $i$ when it is linked -to by nodes that themselves have high centrality. - -Provided that $0 < \beta < 1/r(A)$, Katz centrality is always finite and well-defined -because then $r(\beta A) < 1$. - -This means that {eq}`katz_central_vec` has the unique solution +其中$\mathbf 1$是一个全为1的列向量。 +这种中心性度量背后的直觉与特征向量中心性提供的类似:当节点$i$被本身具有高中心性的节点链接时,它就会获得高中心性。 +只要$0 < \beta < 1/r(A)$,卡茨中心性总是有限且定义明确的,因为这时$r(\beta A) < 1$。 +这意味着方程{eq}`katz_central_vec`有唯一解: $$ \kappa = (I - \beta A)^{-1} \mathbf{1} $$ +这是由{ref}`诺伊曼级数定理`得出的。 +参数$\beta$用于确保$\kappa$是有限的。 +当$r(A)<1$时,我们使用$\beta=1$作为卡茨中心性计算的默认值。 -This follows from the {ref}`Neumann series theorem`. - -The parameter $\beta$ is used to ensure that $\kappa$ is finite - -When $r(A)<1$, we use $\beta=1$ as the default for Katz centrality computations. +### 权威与枢纽 +搜索引擎设计者认识到网页可以通过两种不同的方式变得重要。 -### Authorities vs hubs +一些页面具有高**枢纽中心性**,意味着它们链接到有价值的信息来源(例如,新闻聚合网站)。 -Search engine designers recognize that web pages can be important in two -different ways. +其他页面具有高**权威中心性**,意味着它们包含有价值的信息,这一点通过指向它们的链接的数量和重要性来体现(例如,受人尊敬的新闻机构的网站)。 -Some pages have high **hub centrality**, meaning that they link to valuable -sources of information (e.g., news aggregation sites). +类似的概念也已经应用于经济网络(通常使用不同的术语)。 -Other pages have high **authority centrality**, meaning that they contain -valuable information, as indicated by the number and significance of incoming -links (e.g., websites of respected news organizations). +我们之前讨论的特征向量中心性和Katz中心性衡量的是枢纽中心性。 +(如果节点指向其他具有高中心性的节点,则它们具有高中心性。) -Similar ideas can and have been applied to economic networks (often using -different terminology). +如果我们更关心权威中心性,我们可以使用相同的定义,只是取邻接矩阵的转置。 +这之所以有效,是因为取转置会反转箭头的方向。 +(现在,如果节点接收来自其他具有高中心性的节点的链接,它们将具有高中心性。) -The eigenvector centrality and Katz centrality measures we discussed above -measure hub centrality. - -(Nodes have high centrality if they point to other nodes with high centrality.) - -If we care more about authority centrality, we can use the same definitions -except that we take the transpose of the adjacency matrix. - -This works because taking the transpose reverses the direction of the arrows. - -(Now nodes will have high centrality if they receive links from other nodes -with high centrality.) - -For example, the **authority-based eigenvector centrality** of a weighted -directed graph with adjacency matrix $A$ is the vector $e$ solving +例如,具有邻接矩阵$A$的加权有向图的**基于权威的特征向量中心性**是解决以下方程的向量$e$: $$ e = \frac{1}{r(A)} A^\top e. $$ (eicena0) -The only difference from the original definition is that $A$ is replaced by -its transpose. +与原始定义的唯一区别是$A$被其转置替代。 +(转置不影响矩阵的谱半径,所以我们写成$r(A)$而不是$r(A^\top)$。) -(Transposes do not affect the spectral radius of a matrix so we wrote $r(A)$ instead of $r(A^\top)$.) - -Element-by-element, this is given by +按元素逐个表示,这可以写成: $$ e_j = \frac{1}{r(A)} \sum_{1 \leq i \leq n} a_{ij} e_i $$ (eicena) -We see $e_j$ will be high if many nodes with high authority rankings link to $j$. +我们可以看到,如果许多具有高权威排名的节点链接到$j$,则$e_j$将会很高。 -The following figurenshows the authority-based eigenvector centrality ranking for the international -credit network shown in {numref}`financial_network`. +下图显示了{numref}`financial_network`中所示的国际信贷网络的基于权威的特征向量中心性排名。 ```{code-cell} ipython3 ecentral_authority = eigenvector_centrality(Z, authority=True) @@ -1116,7 +902,7 @@ ecentral_authority = eigenvector_centrality(Z, authority=True) --- mystnb: figure: - caption: Eigenvector authority + caption: 特征向量权威度 name: eigenvector_centrality --- fig, ax = plt.subplots() @@ -1131,37 +917,29 @@ ax.legend(handles=[patch], fontsize=12, loc="upper left", handlelength=0, frameo plt.show() ``` -Highly ranked countries are those that attract large inflows of credit, or -credit inflows from other major players. - -In this case the US clearly dominates the rankings as a target of interbank credit. - - -## Further reading - -We apply the ideas discussed in this lecture to: +排名靠前的国家是那些吸引大量信贷流入或从其他主要参与者获得信贷流入的国家。 +在这种情况下,美国作为银行间信贷的目标显然主导了排名。 -Textbooks on economic and social networks include {cite}`jackson2010social`, -{cite}`easley2010networks`, {cite}`borgatti2018analyzing`, -{cite}`sargent2022economic` and {cite}`goyal2023networks`. +## 进一步阅读 +我们将本讲座中讨论的观点应用于: -Within the realm of network science, the texts -by {cite}`newman2018networks`, {cite}`menczer2020first` and -{cite}`coscia2021atlas` are excellent. +关于经济和社会网络的教科书包括 {cite}`jackson2010social`、 +{cite}`easley2010networks`、{cite}`borgatti2018analyzing`、 +{cite}`sargent2022economic` 和 {cite}`goyal2023networks`。 +在网络科学领域,{cite}`newman2018networks`、{cite}`menczer2020first` 和 +{cite}`coscia2021atlas` 的著作都非常出色。 -## Exercises +## 练习 ```{exercise-start} :label: networks_ex1 ``` -Here is a mathematical exercise for those who like proofs. - -Let $(V, E)$ be a directed graph and write $u \sim v$ if $u$ and $v$ communicate. - -Show that $\sim$ is an [equivalence relation](https://en.wikipedia.org/wiki/Equivalence_relation) on $V$. +这是一个适合喜欢证明的人的数学练习。 +设 $(V, E)$ 是一个有向图,若 $u$ 和 $v$ 互通,则记作 $u \sim v$。 +证明 $\sim$ 是 $V$ 上的[等价关系](https://en.wikipedia.org/wiki/Equivalence_relation)。 ```{exercise-end} ``` @@ -1170,28 +948,20 @@ Show that $\sim$ is an [equivalence relation](https://en.wikipedia.org/wiki/Equi :class: dropdown ``` -**Reflexivity:** - -Trivially, $u = v \Rightarrow u \rightarrow v$. - -Thus, $u \sim u$. - -**Symmetry:** -Suppose, $u \sim v$ - -$\Rightarrow u \rightarrow v$ and $v \rightarrow u$. - -By definition, this implies $v \sim u$. - -**Transitivity:** - -Suppose, $u \sim v$ and $v \sim w$ - -This implies, $u \rightarrow v$ and $v \rightarrow u$ and also $v \rightarrow w$ and $w \rightarrow v$. +**自反性:** +显然,$u = v \Rightarrow u \rightarrow v$。 +因此,$u \sim u$。 -Thus, we can conclude $u \rightarrow v \rightarrow w$ and $w \rightarrow v \rightarrow u$. +**对称性:** +假设 $u \sim v$ +$\Rightarrow u \rightarrow v$ 且 $v \rightarrow u$。 +根据定义,这意味着 $v \sim u$。 -Which means $u \sim w$. +**传递性:** +假设 $u \sim v$ 且 $v \sim w$ +这意味着,$u \rightarrow v$ 且 $v \rightarrow u$,同时 $v \rightarrow w$ 且 $w \rightarrow v$。 +因此,我们可以得出 $u \rightarrow v \rightarrow w$ 且 $w \rightarrow v \rightarrow u$。 +这意味着 $u \sim w$。 ```{solution-end} ``` @@ -1200,24 +970,18 @@ Which means $u \sim w$. :label: networks_ex2 ``` -Consider a directed graph $G$ with the set of nodes - +考虑一个有向图 $G$,其节点集为 $$ V = \{0,1,2,3,4,5,6,7\} $$ - -and the set of edges - +边集为 $$ E = \{(0, 1), (0, 3), (1, 0), (2, 4), (3, 2), (3, 4), (3, 7), (4, 3), (5, 4), (5, 6), (6, 3), (6, 5), (7, 0)\} $$ -1. Use `Networkx` to draw graph $G$. - -2. Find the associated adjacency matrix $A$ for $G$. - -3. Use the functions defined above to compute in-degree centrality, out-degree centrality and eigenvector centrality - of G. +1. 使用 `Networkx` 绘制图 $G$。 +2. 找出 $G$ 的相关邻接矩阵 $A$。 +3. 使用上面定义的函数计算 $G$ 的入度中心性、出度中心性和特征向量中心性。 ```{exercise-end} ``` @@ -1227,13 +991,13 @@ $$ ``` ```{code-cell} ipython3 -# First, let's plot the given graph +# 首先,让我们绘制给定的图 G = nx.DiGraph() -G.add_nodes_from(np.arange(8)) # adding nodes +G.add_nodes_from(np.arange(8)) # 添加节点 -G.add_edges_from([(0,1),(0,3), # adding edges +G.add_edges_from([(0,1),(0,3), # 添加边 (1,0), (2,4), (3,2),(3,4),(3,7), @@ -1248,20 +1012,20 @@ plt.show() ``` ```{code-cell} ipython3 -A = nx.to_numpy_array(G) #find adjacency matrix associated with G +A = nx.to_numpy_array(G) #求G的邻接矩阵 A ``` ```{code-cell} ipython3 -oG = [G.out_degree(v) for v in G.nodes()] # computing in-degree centrality +oG = [G.out_degree(v) for v in G.nodes()] # 计算入度中心性 for i, d in enumerate(oG): print(i, d) ``` ```{code-cell} ipython3 -e = eigenvector_centrality(A) # computing eigenvector centrality +e = eigenvector_centrality(A) # 计算特征向量中心性 n = len(e) for i in range(n): @@ -1275,19 +1039,13 @@ for i in range(n): :label: networks_ex3 ``` -Consider a graph $G$ with $n$ nodes and $n \times n$ adjacency matrix $A$. - -Let $S = \sum_{k=0}^{n-1} A^k$ - -We can say for any two nodes $i$ and $j$, $j$ is accessible from $i$ if and only if -$S_{ij} > 0$. - -Devise a function `is_accessible` that checks if any two nodes of a given graph are accessible. - -Consider the graph in {ref}`networks_ex2` and use this function to check if - -1. $1$ is accessible from $2$ -2. $6$ is accessible from $3$ +考虑一个有 $n$ 个节点和 $n \times n$ 邻接矩阵 $A$ 的图 $G$。 +令 $S = \sum_{k=0}^{n-1} A^k$ +我们可以说对于任意两个节点 $i$ 和 $j$,当且仅当 $S_{ij} > 0$ 时,$j$ 可从 $i$ 到达。 +设计一个函数 `is_accessible`,用于检查给定图中的任意两个节点是否可达。 +考虑 {ref}`networks_ex2` 中的图,并使用此函数检查 +1. 从 $2$ 是否可以到达 $1$ +2. 从 $3$ 是否可以到达 $6$ ```{exercise-end} ``` @@ -1312,9 +1070,9 @@ def is_accessible(G,i,j): ```{code-cell} ipython3 G = nx.DiGraph() -G.add_nodes_from(np.arange(8)) # adding nodes +G.add_nodes_from(np.arange(8)) # 添加节点 -G.add_edges_from([(0,1),(0,3), # adding edges +G.add_edges_from([(0,1),(0,3), # 添加边 (1,0), (2,4), (3,2),(3,4),(3,7), diff --git a/lectures/olg.md b/lectures/olg.md index 6530e1b..e3b10d5 100644 --- a/lectures/olg.md +++ b/lectures/olg.md @@ -11,55 +11,42 @@ kernelspec: name: python3 --- -# The Overlapping Generations Model +# 世代交叠模型 -In this lecture we study the famous overlapping generations (OLG) model, which -is used by policy makers and researchers to examine +在这节课中,我们将学习著名的世代交叠(OLG)模型。政策制定者和研究人员使用这个模型来研究: +* 财政政策 +* 货币政策 +* 长期增长 +以及许多其他主题。 -* fiscal policy -* monetary policy -* long-run growth +OLG模型的第一个严谨版本是由Paul Samuelson开发的{cite}`samuelson1958exact`。 -and many other topics. +我们的目标是对OLG模型的一个简单版本有深入的理解。 -The first rigorous version of the OLG model was developed by Paul Samuelson -{cite}`samuelson1958exact`. +## 概述 -Our aim is to gain a good understanding of a simple version of the OLG -model. +OLG模型的动态与[索洛-斯旺增长模型](https://intro.quantecon.org/solow.html)非常相似。 -## Overview +同时,OLG模型增加了一个重要的新特征:储蓄量的选择是内生的。 -The dynamics of the OLG model are quite similar to those of the [Solow-Swan -growth model](https://intro.quantecon.org/solow.html). +为了理解这一点的重要性,假设我们想预测一项新税收对长期增长的影响。 -At the same time, the OLG model adds an important new feature: the choice of -how much to save is endogenous. +我们可以在索洛-斯旺模型中添加一项税收,然后观察稳态的变化。 -To see why this is important, suppose, for example, that we are interested in -predicting the effect of a new tax on long-run growth. +但这忽略了一个事实:当面对新的税率时,家庭会改变他们的储蓄和消费行为。 -We could add a tax to the Solow-Swan model and look at the change in the -steady state. +这些变化可能会大大改变模型的预测。 -But this ignores the fact that households will change their savings and -consumption behavior when they face the new tax rate. +因此,如果我们关心准确的预测,我们应该对代理人的决策问题进行建模。 -Such changes can substantially alter the predictions of the model. +特别是,模型中的家庭应该根据他们面临的环境(技术、税收、价格等)决定储蓄多少和消费多少。 -Hence, if we care about accurate predictions, we should model the decision -problems of the agents. +OLG模型应对了这一挑战。 -In particular, households in the model should decide how much to save and how -much to consume, given the environment that they face (technology, taxes, -prices, etc.) +我们将介绍OLG模型的一个简单版本,阐明家庭的决策问题,并研究其对长期增长的影响。 -The OLG model takes up this challenge. +让我们从一些导入开始。 -We will present a simple version of the OLG model that clarifies the decision -problem of households and studies the implications for long-run growth. - -Let's start with some imports. ```{code-cell} ipython3 import numpy as np @@ -68,45 +55,27 @@ from collections import namedtuple import matplotlib.pyplot as plt ``` -## Environment - -We assume that time is discrete, so that $t=0, 1, \ldots$. - -An individual born at time $t$ lives for two periods, $t$ and $t + 1$. - -We call an agent - -- "young" during the first period of their lives and -- "old" during the second period of their lives. - -Young agents work, supplying labor and earning labor income. - -They also decide how much to save. - -Old agents do not work, so all income is financial. - -Their financial income is from interest on their savings from wage income, -which is then combined with the labor of the new young generation at $t+1$. - -The wage and interest rates are determined in equilibrium by supply and -demand. - -To make the algebra slightly easier, we are going to assume a constant -population size. - -We normalize the constant population size in each period to 1. - -We also suppose that each agent supplies one "unit" of labor hours, so total -labor supply is 1. - - -## Supply of capital - -First let's consider the household side. - -### Consumer's problem - -Suppose that utility for individuals born at time $t$ takes the form +## 环境 +我们假设时间是离散的,因此 $t=0, 1, \ldots$。 +在时间 $t$ 出生的个体存活两个时期,$t$ 和 $t + 1$。 +我们称一个个体 +- 在生命的第一个时期为"年轻人" +- 在生命的第二个时期为"老年人"。 +年轻人工作,提供劳动并赚取劳动收入。 +他们还决定存多少钱。 +老年人不工作,所以所有收入都是金融收入。 +他们的金融收入来自工资收入的储蓄利息, +这些储蓄随后与 $t+1$ 时期新的年轻一代的劳动相结合。 +工资和利率在均衡中由供求决定。 +为了使代数计算稍微简单一些,我们假设人口规模恒定。 +我们将每个时期的恒定人口规模标准化为1。 +我们还假设每个个体提供一个"单位"的劳动时间,因此总劳动供给为1。 + +## 资本供给 +首先让我们考虑家庭方面。 + +### 消费者问题 +假设在时间 $t$ 出生的个体的效用函数形式为 ```{math} :label: eq_crra @@ -114,16 +83,14 @@ Suppose that utility for individuals born at time $t$ takes the form U_t = u(c_t) + \beta u(c_{t+1}) ``` -Here - -- $u: \mathbb R_+ \to \mathbb R$ is called the "flow" utility function -- $\beta \in (0, 1)$ is the discount factor -- $c_t$ is time $t$ consumption of the individual born at time $t$ -- $c_{t+1}$ is time $t+1$ consumption of the same individual - -We assume that $u$ is strictly increasing. +这里 -Savings behavior is determined by the optimization problem +- $u: \mathbb R_+ \to \mathbb R$ 被称为"流量"效用函数 +- $\beta \in (0, 1)$ 是贴现因子 +- $c_t$ 是在时间 $t$ 出生的个体在时间 $t$ 的消费 +- $c_{t+1}$ 是同一个体在时间 $t+1$ 的消费 +我们假设 $u$ 是严格递增的。 +储蓄行为由以下优化问题决定 ```{math} @@ -132,7 +99,7 @@ Savings behavior is determined by the optimization problem \, \left \{ u(c_t) + \beta u(c_{t+1}) \right \} ``` -subject to +受制于 $$ c_t + s_t \le w_t @@ -140,118 +107,92 @@ $$ c_{t+1} \le R_{t+1} s_t $$ -Here +这里 +- $s_t$ 是出生于时间 $t$ 的个人的储蓄 +- $w_t$ 是时间 $t$ 的工资率 +- $R_{t+1}$ 是在时间 $t$ 投资的储蓄在时间 $t+1$ 支付的总利率 -- $s_t$ is savings by an individual born at time $t$ -- $w_t$ is the wage rate at time $t$ -- $R_{t+1}$ is the gross interest rate on savings invested at time $t$, paid at time $t+1$ +由于 $u$ 是严格递增的,这两个约束在最大值时都将成为等式。 -Since $u$ is strictly increasing, both of these constraints will hold as equalities at the maximum. +利用这一事实,并将第一个约束中的 $s_t$ 代入第二个约束,我们得到 +$c_{t+1} = R_{t+1}(w_t - c_t)$。 -Using this fact and substituting $s_t$ from the first constraint into the second we get -$c_{t+1} = R_{t+1}(w_t - c_t)$. +最大值的一阶条件可以通过将 $c_{t+1}$ 代入目标函数,对 $c_t$ 求导, +并将其设为零来获得。 -The first-order condition for a maximum can be obtained -by plugging $c_{t+1}$ into the objective function, taking the derivative -with respect to $c_t$, and setting it to zero. - -This leads to the **Euler equation** of the OLG model, which describes the optimal intertemporal consumption dynamics: +这导致了OLG模型的**欧拉方程**,它描述了最优的跨期消费动态: ```{math} :label: euler_1_olg u'(c_t) = \beta R_{t+1} u'( R_{t+1} (w_t - c_t)) ``` -From the first constraint we get $c_t = w_t - s_t$, so the Euler equation -can also be expressed as +从第一个约束我们得到 $c_t = w_t - s_t$,所以欧拉方程 +也可以表示为 ```{math} :label: euler_2_olg u'(w_t - s_t) = \beta R_{t+1} u'( R_{t+1} s_t) ``` -Suppose that, for each $w_t$ and $R_{t+1}$, there is exactly one $s_t$ that -solves [](euler_2_olg). - -Then savings can be written as a fixed function of $w_t$ and $R_{t+1}$. - -We write this as +假设对于每个 $w_t$ 和 $R_{t+1}$,恰好有一个 $s_t$ 可以 +解决 [](euler_2_olg)。 +那么储蓄可以被写成 $w_t$ 和 $R_{t+1}$ 的固定函数。 +我们将其表示为 ```{math} :label: saving_1_olg s_t = s(w_t, R_{t+1}) ``` -The precise form of the function $s$ will depend on the choice of flow utility -function $u$. - -Together, $w_t$ and $R_{t+1}$ represent the *prices* in the economy (price of -labor and rental rate of capital). - -Thus, [](saving_1_olg) states the quantity of savings given prices. +$s$ 函数的具体形式将取决于流效用函数 $u$ 的选择。 +$w_t$ 和 $R_{t+1}$ 共同代表经济中的*价格*(劳动力价格和资本租赁率)。 +因此,[](saving_1_olg) 表示给定价格下的储蓄量。 +### 示例:对数偏好 -### Example: log preferences - -In the special case $u(c) = \log c$, the Euler equation simplifies to - $s_t= \beta (w_t - s_t)$. - -Solving for saving, we get +在特殊情况 $u(c) = \log c$ 下,欧拉方程简化为 + $s_t= \beta (w_t - s_t)$。 +求解储蓄,我们得到 ```{math} :label: saving_log_2_olg s_t = s(w_t, R_{t+1}) = \frac{\beta}{1+\beta} w_t ``` -In this special case, savings does not depend on the interest rate. - - - -### Savings and investment +在这种特殊情况下,储蓄不依赖于利率。 -Since the population size is normalized to 1, $s_t$ is also total savings in -the economy at time $t$. +### 储蓄和投资 -In our closed economy, there is no foreign investment, so net savings equals -total investment, which can be understood as supply of capital to firms. +由于人口规模被标准化为1,$s_t$也代表了t时期经济中的总储蓄。 +在我们的封闭经济中,没有外国投资,所以净储蓄等于总投资,这可以理解为对企业的资本供给。 -In the next section we investigate demand for capital. +在下一节中,我们将研究资本需求。 -Equating supply and demand will allow us to determine equilibrium in the OLG -economy. +通过使供给和需求相等,我们将能够确定OLG经济中的均衡。 +## 资本需求 +首先我们描述企业的问题,然后我们写出一个方程来描述给定价格下的资本需求。 -## Demand for capital +### 企业问题 -First we describe the firm's problem and then we write down an equation -describing demand for capital given prices. - - -### Firm's problem - -For each integer $t \geq 0$, output $y_t$ in period $t$ is given by the -**[Cobb-Douglas production function](https://en.wikipedia.org/wiki/Cobb%E2%80%93Douglas_production_function)** +对于每个整数 $t \geq 0$,t期的产出 $y_t$ 由**[柯布-道格拉斯生产函数](https://en.wikipedia.org/wiki/Cobb%E2%80%93Douglas_production_function)**给出 ```{math} :label: cobb_douglas y_t = k_t^{\alpha} \ell_t^{1-\alpha} ``` -Here $k_t$ is capital, $\ell_t$ is labor, and $\alpha$ is a parameter -(sometimes called the "output elasticity of capital"). - -The profit maximization problem of the firm is +在这里,$k_t$ 是资本,$\ell_t$ 是劳动,而 $\alpha$ 是一个参数(有时被称为"资本的产出弹性")。 +公司的利润最大化问题是 ```{math} :label: opt_profit_olg \max_{k_t, \ell_t} \{ k^{\alpha}_t \ell_t^{1-\alpha} - R_t k_t -w_t \ell_t \} ``` - -The first-order conditions are obtained by taking the derivative of the -objective function with respect to capital and labor respectively and setting -them to zero: +一阶条件是通过分别对资本和劳动求目标函数的导数,并将它们设为零来获得的: ```{math} (1-\alpha)(k_t / \ell_t)^{\alpha} = w_t @@ -260,16 +201,16 @@ them to zero: ``` -### Demand +### 需求 -Using our assumption $\ell_t = 1$ allows us to write +我们的假设$\ell_t = 1$ 让我们可以 ```{math} :label: wage_one w_t = (1-\alpha)k_t^\alpha ``` -and +并且 ```{math} :label: interest_rate_one @@ -277,8 +218,7 @@ and \alpha k_t^{\alpha - 1} ``` -Rearranging [](interest_rate_one) gives the aggregate demand for capital -at time $t+1$ +重新整理 [](interest_rate_one) 得出在时间 $t+1$ 的总资本需求 ```{math} :label: aggregate_demand_capital_olg @@ -286,7 +226,7 @@ at time $t+1$ := \left (\frac{\alpha}{R_{t+1}} \right )^{1/(1-\alpha)} ``` -In Python code this is +在Python中 ```{code-cell} ipython3 def capital_demand(R, α): @@ -299,22 +239,17 @@ def capital_supply(R, β, w): return R * (β / (1 + β)) * w ``` -The next figure plots the supply of capital, as in [](saving_log_2_olg), as well as the demand for capital, as in [](aggregate_demand_capital_olg), as functions of the interest rate $R_{t+1}$. +下图绘制了资本供给曲线(如[](saving_log_2_olg)所示)以及资本需求曲线(如[](aggregate_demand_capital_olg)所示),这两条曲线都是利率$R_{t+1}$的函数。 +(对于对数效用这一特殊情况,供给不依赖于利率,因此我们得到一个常数函数。) -(For the special case of log utility, supply does not depend on the interest rate, so we have a constant function.) +## 均衡 -## Equilibrium +在本节中,我们将推导均衡条件并研究一个例子。 -In this section we derive equilibrium conditions and investigate an example. - - -### Equilibrium conditions - -In equilibrium, savings at time $t$ equals investment at time $t$, which -equals capital supply at time $t+1$. - -Equilibrium is computed by equating these quantities, setting +### 均衡条件 +在均衡状态下,t时刻的储蓄等于t时刻的投资,也等于t+1时刻的资本供给。 +通过将这些数量设为相等来计算均衡,即 ```{math} :label: equilibrium_1 @@ -324,21 +259,15 @@ Equilibrium is computed by equating these quantities, setting ``` -In principle, we can now solve for the equilibrium price $R_{t+1}$ given $w_t$. - -(In practice, we first need to specify the function $u$ and hence $s$.) +原则上,我们现在可以给定$w_t$来求解均衡价格$R_{t+1}$。 +(实际上,我们首先需要指定函数$u$,从而得到$s$。) +当我们求解这个关于$t+1$时刻结果的方程时,$t$时刻的量已经确定,所以我们可以将$w_t$视为常数。 +从均衡$R_{t+1}$和[](aggregate_demand_capital_olg),我们可以得到 +均衡数量$k_{t+1}$。 +### 示例:对数效用 -When we solve this equation, which concerns time $t+1$ outcomes, time -$t$ quantities are already determined, so we can treat $w_t$ as a constant. - -From equilibrium $R_{t+1}$ and [](aggregate_demand_capital_olg), we can obtain -the equilibrium quantity $k_{t+1}$. - - -### Example: log utility - -In the case of log utility, we can use [](equilibrium_1) and [](saving_log_2_olg) to obtain +在对数效用的情况下,我们可以使用[](equilibrium_1)和[](saving_log_2_olg)得到 ```{math} :label: equilibrium_2 @@ -346,7 +275,7 @@ In the case of log utility, we can use [](equilibrium_1) and [](saving_log_2_olg = \left( \frac{\alpha}{R_{t+1}} \right)^{1/(1-\alpha)} ``` -Solving for the equilibrium interest rate gives +求解均衡利率得到 ```{math} :label: equilibrium_price @@ -357,7 +286,7 @@ Solving for the equilibrium interest rate gives \right)^{\alpha-1} ``` -In Python we can compute this via +在Python中,我们可以通过以下方式计算这个 ```{code-cell} ipython3 def equilibrium_R_log_utility(α, β, w): @@ -365,16 +294,15 @@ def equilibrium_R_log_utility(α, β, w): return R ``` -In the case of log utility, since capital supply does not depend on the interest rate, the equilibrium quantity is fixed by supply. - -That is, +在对数效用的情况下,由于资本供给不依赖于利率,均衡数量是由供给固定的。 +也就是说, ```{math} :label: equilibrium_quantity k_{t+1} = s(w_t, R_{t+1}) = \frac{\beta }{1+\beta} w_t ``` -Let's redo our plot above but now inserting the equilibrium quantity and price. +让我们重新绘制上面的图,但这次加入均衡数量和价格。 ```{code-cell} ipython3 R_vals = np.linspace(0.3, 1) @@ -384,14 +312,14 @@ w = 2.0 fig, ax = plt.subplots() ax.plot(R_vals, capital_demand(R_vals, α), - label="aggregate demand") + label="总需求") ax.plot(R_vals, capital_supply(R_vals, β, w), - label="aggregate supply") + label="总供给") R_e = equilibrium_R_log_utility(α, β, w) k_e = (β / (1 + β)) * w -ax.plot(R_e, k_e, 'o',label='equilibrium') +ax.plot(R_e, k_e, 'o',label='均衡') ax.set_xlabel("$R_{t+1}$") ax.set_ylabel("$k_{t+1}$") @@ -399,33 +327,27 @@ ax.legend() plt.show() ``` -## Dynamics +## 动力学 -In this section we discuss dynamics. +在本节中,我们讨论动力学。 +目前,我们将重点关注对数效用的情况,因此均衡由[](equilibrium_quantity)决定。 -For now we will focus on the case of log utility, so that the equilibrium is determined by [](equilibrium_quantity). +### 资本的演变 -### Evolution of capital - -The discussion above shows how equilibrium $k_{t+1}$ is obtained given $w_t$. - -From [](wage_one) we can translate this into $k_{t+1}$ as a function of $k_t$ - -In particular, since $w_t = (1-\alpha)k_t^\alpha$, we have +上述讨论展示了如何在给定$w_t$的情况下获得均衡$k_{t+1}$。 +从[](wage_one)中,我们可以将此转换为$k_{t+1}$作为$k_t$的函数。 +特别是,由于$w_t = (1-\alpha)k_t^\alpha$,我们有 ```{math} :label: law_of_motion_capital k_{t+1} = \frac{\beta}{1+\beta} (1-\alpha)(k_t)^{\alpha} ``` -If we iterate on this equation, we get a sequence for capital stock. - - -Let's plot the 45-degree diagram of these dynamics, which we write as - +如果我们对这个方程进行迭代,我们将得到一个资本存量序列。 +让我们绘制这些动态的45度图,我们将其表示为 $$ k_{t+1} = g(k_t) - \quad \text{where } + \quad \text{其中 } g(k) := \frac{\beta}{1+\beta} (1-\alpha)(k)^{\alpha} $$ @@ -456,44 +378,38 @@ ax.set_ylabel('$k_{t+1}$', fontsize=12) plt.show() ``` -### Steady state (log case) - -The diagram shows that the model has a unique positive steady state, which we -denote by $k^*$. +### 稳态(对数情况) -We can solve for $k^*$ by setting $k^* = g(k^*)$, or +图表显示,该模型具有唯一的正稳态,我们将其表示为$k^*$。 +我们可以通过设置$k^* = g(k^*)$来求解$k^*$,即 ```{math} :label: steady_state_1 k^* = \frac{\beta (1-\alpha) (k^*)^{\alpha}}{(1+\beta)} ``` -Solving this equation yields +求解方程得到 ```{math} :label: steady_state_2 k^* = \left (\frac{\beta (1-\alpha)}{1+\beta} \right )^{1/(1-\alpha)} ``` - -We can get the steady state interest rate from [](interest_rate_one), which yields - +我们可以从[](interest_rate_one)得到稳态利率,即 $$ R^* = \alpha (k^*)^{\alpha - 1} = \frac{\alpha}{1 - \alpha} \frac{1 + \beta}{\beta} $$ - -In Python we have - +在Python中,我们有 ```{code-cell} ipython3 k_star = ((β * (1 - α))/(1 + β))**(1/(1-α)) R_star = (α/(1 - α)) * ((1 + β) / β) ``` -### Time series +### 时间序列 -The 45-degree diagram above shows that time series of capital with positive initial conditions converge to this steady state. +上面的45度图显示,具有正初始条件的资本时间序列会收敛到这个稳态。 +让我们绘制一些时间序列来可视化这一点。 -Let's plot some time series that visualize this. ```{code-cell} ipython3 ts_length = 25 @@ -503,84 +419,71 @@ for t in range(ts_length - 1): k_series[t+1] = k_update(k_series[t], α, β) fig, ax = plt.subplots() -ax.plot(k_series, label="capital series") +ax.plot(k_series, label="资本序列") ax.plot(range(ts_length), np.full(ts_length, k_star), 'k--', label="$k^*$") ax.set_ylim(0, 0.1) -ax.set_ylabel("capital") +ax.set_ylabel("资本") ax.set_xlabel("$t$") ax.legend() plt.show() ``` - -If you experiment with different positive initial conditions, you will see that the series always converges to $k^*$. - +如果你尝试不同的正初始条件,你会发现这个序列总是收敛于$k^*$。 +++ - -Below we also plot the gross interest rate over time. +下面我们还绘制了随时间变化的总利率。 ```{code-cell} ipython3 R_series = α * k_series**(α - 1) fig, ax = plt.subplots() -ax.plot(R_series, label="gross interest rate") +ax.plot(R_series, label="毛利率") ax.plot(range(ts_length), np.full(ts_length, R_star), 'k--', label="$R^*$") ax.set_ylim(0, 4) -ax.set_ylabel("gross interest rate") +ax.set_ylabel("毛利率") ax.set_xlabel("$t$") ax.legend() plt.show() ``` -The interest rate reflects the marginal product of capital, which is high when capital stock is low. - +利率反映了资本的边际产出,当资本存量较低时,利率较高。 +++ - -## CRRA preferences - -Previously, in our examples, we looked at the case of log utility. - -Log utility is a rather special case of CRRA utility with $\gamma \to 1$. - -In this section, we are going to assume that $u(c) = \frac{ c^{1- -\gamma}-1}{1-\gamma}$, where $\gamma >0, \gamma\neq 1$. - -This function is called the CRRA utility function. - -In other respects, the model is the same. - -Below we define the utility function in Python and construct a `namedtuple` to store the parameters. +## CRRA偏好 +此前,在我们的例子中,我们研究了对数效用的情况。 +对数效用是CRRA效用在$\gamma \to 1$时的一个特殊情况。 +在本节中,我们假设$u(c) = \frac{ c^{1-\gamma}-1}{1-\gamma}$,其中$\gamma >0, \gamma\neq 1$。 +这个函数被称为CRRA效用函数。 +在其他方面,模型保持不变。 +下面我们用Python定义效用函数,并构造一个`namedtuple`来存储参数。 ```{code-cell} ipython3 def crra(c, γ): return c**(1 - γ) / (1 - γ) -Model = namedtuple('Model', ['α', # Cobb-Douglas parameter - 'β', # discount factor - 'γ'] # parameter in CRRA utility +Model = namedtuple('Model', ['α', # 柯布-道格拉斯参数 + 'β', # 折现因子 + 'γ'] # CRRA效用函数中的参数 ) def create_olg_model(α=0.4, β=0.9, γ=0.5): return Model(α=α, β=β, γ=γ) ``` -Let's also redefine the capital demand function to work with this `namedtuple`. +让我们也重新定义资本需求函数,使其能够与这个`namedtuple`一起工作。 ```{code-cell} ipython3 def capital_demand(R, model): return (α/R)**(1/(1-model.α)) ``` -### Supply - +### 供给 +对于家庭来说,欧拉方程变为 -For households, the Euler equation becomes ```{math} :label: euler_crra (w_t - s_t)^{-\gamma} = \beta R^{1-\gamma}_{t+1} (s_t)^{-\gamma} ``` -Solving for savings, we have +求解储蓄,我们得到 ```{math} :label: saving_crra @@ -592,7 +495,7 @@ Solving for savings, we have ``` -Notice how, unlike the log case, savings now depends on the interest rate. +请注意,与对数情况不同,储蓄现在取决于利率。 ```{code-cell} ipython3 def savings_crra(w, R, model): @@ -607,9 +510,9 @@ w = 2.0 fig, ax = plt.subplots() ax.plot(R_vals, capital_demand(R_vals, model), - label="aggregate demand") + label="总需求") ax.plot(R_vals, savings_crra(w, R_vals, model), - label="aggregate supply") + label="总供给") ax.set_xlabel("$R_{t+1}$") ax.set_ylabel("$k_{t+1}$") @@ -617,12 +520,10 @@ ax.legend() plt.show() ``` -### Equilibrium +### 均衡 -Equating aggregate demand for capital (see [](aggregate_demand_capital_olg)) -with our new aggregate supply function yields equilibrium capital. - -Thus, we set +将资本的总需求(参见[](aggregate_demand_capital_olg))与我们新的总供给函数相等,即可得出均衡资本。 +因此,我们设定 ```{math} @@ -631,32 +532,27 @@ Thus, we set = \left (\frac{R_{t+1}}{\alpha} \right )^{1/(\alpha - 1)} ``` -This expression is quite complex and we cannot solve for $R_{t+1}$ analytically. - - -Combining [](interest_rate_one) and [](equilibrium_crra_2) yields +这个表达式相当复杂,我们无法解析地求解 $R_{t+1}$。 +将[](interest_rate_one)和[](equilibrium_crra_2)结合起来,得到 ```{math} :label: law_of_motion_capital_crra k_{t+1} = \left [ 1 + \beta^{-1/\gamma} (\alpha k^{\alpha - 1}_{t+1})^{(\gamma-1)/\gamma} \right ]^{-1} (1-\alpha)(k_t)^{\alpha} ``` -Again, with this equation and $k_t$ as given, we cannot solve for $k_{t+1}$ by pencil and paper. - - -In the exercise below, you will be asked to solve these equations numerically. +同样,有了这个等式和给定的 $k_t$,我们无法用纸笔求解 $k_{t+1}$。 +在下面的练习中,你将被要求用数值方法解这些方程。 +++ -## Exercises +## 练习 ```{exercise} :label: olg_ex1 -Solve for the dynamics of equilibrium capital stock in the CRRA case numerically using [](law_of_motion_capital_crra). - -Visualize the dynamics using a 45-degree diagram. +使用[](law_of_motion_capital_crra)在 CRRA 情况下数值求解均衡资本存量的动态。 +使用 45 度图来可视化这个动态过程。 ``` @@ -666,9 +562,8 @@ Visualize the dynamics using a 45-degree diagram. ``` -To solve for $k_{t+1}$ given $k_t$ we use [Newton's method](https://python.quantecon.org/newton_method.html). - -Let +为了在给定 $k_t$ 的情况下求解 $k_{t+1}$,我们使用[牛顿法](https://python.quantecon.org/newton_method.html)。 +设 ```{math} :label: crra_newton_1 @@ -683,11 +578,9 @@ Let \right] - (1-\alpha) k^{\alpha}_t =0 ``` -If $k_t$ is given then $f$ is a function of unknown $k_{t+1}$. - -Then we can use `scipy.optimize.newton` to solve $f(k_{t+1}, k_t)=0$ for $k_{t+1}$. - -First let's define $f$. +如果给定了 $k_t$,那么 $f$ 就是未知数 $k_{t+1}$ 的函数。 +然后我们可以使用 `scipy.optimize.newton` 求解 $f(k_{t+1}, k_t)=0$ 来得到 $k_{t+1}$。 +首先让我们定义 $f$。 ```{code-cell} ipython3 def f(k_prime, k, model): @@ -699,14 +592,14 @@ def f(k_prime, k, model): return p - z ``` -Now let's define a function that finds the value of $k_{t+1}$. +现在让我们定义一个函数来找出 $k_{t+1}$ 的值。 ```{code-cell} ipython3 def k_update(k, model): return optimize.newton(lambda k_prime: f(k_prime, k, model), 0.1) ``` -Finally, here is the 45-degree diagram. +最后,这是 45 度图。 ```{code-cell} ipython3 kmin, kmax = 0, 0.5 @@ -739,10 +632,9 @@ plt.show() ```{exercise} :label: olg_ex2 -The 45-degree diagram from the last exercise shows that there is a unique -positive steady state. +上一个练习中的45度图表明存在唯一的正稳态。 -The positive steady state can be obtained by setting $k_{t+1} = k_t = k^*$ in [](law_of_motion_capital_crra), which yields +通过在[](law_of_motion_capital_crra)中设置 $k_{t+1} = k_t = k^*$,可以得到正稳态,即: $$ k^* = @@ -750,20 +642,17 @@ $$ {1 + \beta^{-1/\gamma} (\alpha (k^*)^{\alpha-1})^{(\gamma-1)/\gamma}} $$ -Unlike the log preference case, the CRRA utility steady state $k^*$ -cannot be obtained analytically. - -Instead, we solve for $k^*$ using Newton's method. +与对数效用情况不同,CRRA效用的稳态 $k^*$ 无法通过解析方法获得。 +相反,我们使用牛顿法求解 $k^*$。 ``` ```{solution-start} olg_ex2 :class: dropdown ``` - -We introduce a function $h$ such that -positive steady state is the root of $h$. +我们引入一个函数 $h$,使得 +正稳态是 $h$ 的根。 ```{math} :label: crra_newton_2 @@ -773,7 +662,7 @@ positive steady state is the root of $h$. \right ] - (1-\alpha)(k^*)^{\alpha} ``` -Here it is in Python +这是用Python写的 ```{code-cell} ipython3 def h(k_star, model): @@ -785,7 +674,7 @@ def h(k_star, model): return p - z ``` -Let's apply Newton's method to find the root: +让我们运用牛顿法来求根: ```{code-cell} ipython3 k_star = optimize.newton(h, 0.2, args=(model,)) @@ -801,10 +690,9 @@ print(f"k_star = {k_star}") ```{exercise} :label: olg_ex3 -Generate three time paths for capital, from -three distinct initial conditions, under the parameterization listed above. +根据上面列出的参数化,生成三个资本的时间路径,来自三个不同的初始条件。 -Use initial conditions for $k_0$ of $0.001, 1.2, 2.6$ and time series length 10. +使用 $k_0$ 的初始条件为 $0.001, 1.2, 2.6$,并且时间序列长度为 10。 ``` @@ -814,7 +702,7 @@ Use initial conditions for $k_0$ of $0.001, 1.2, 2.6$ and time series length 10. ``` -Let's define the constants and three distinct intital conditions +让我们定义常数和三个不同的初始条件。 ```{code-cell} ipython3 ts_length = 10 @@ -828,7 +716,7 @@ def simulate_ts(model, k0_values, ts_length): ts = np.zeros(ts_length) - # simulate and plot time series + # 模拟并且绘制时间序列 for k_init in k0_values: ts[0] = k_init for t in range(1, ts_length): diff --git a/lectures/prob_dist.md b/lectures/prob_dist.md index 22defb1..32efc3d 100644 --- a/lectures/prob_dist.md +++ b/lectures/prob_dist.md @@ -11,113 +11,114 @@ kernelspec: name: python3 --- -# Distributions and Probabilities +# 分布和概率 ```{index} single: Distributions and Probabilities ``` -## Outline +## 概述 -In this lecture we give a quick introduction to data and probability distributions using Python. +在本讲中,我们将使用 Python 快速介绍数据和概率分布。 ```{code-cell} ipython3 :tags: [hide-output] !pip install --upgrade yfinance ``` - ```{code-cell} ipython3 import matplotlib.pyplot as plt +import matplotlib as mpl import pandas as pd import numpy as np import yfinance as yf import scipy.stats import seaborn as sns -``` -## Common distributions +FONTPATH = "fonts/SourceHanSerifSC-SemiBold.otf" +mpl.font_manager.fontManager.addfont(FONTPATH) +plt.rcParams['font.family'] = ['Source Han Serif SC'] +``` +## 常见分布 -In this section we recall the definitions of some well-known distributions and explore how to manipulate them with SciPy. +在本节中,我们回顾一些众所周知的分布的定义,并探讨如何使用 SciPy 来操作它们。 -### Discrete distributions +### 离散分布 -Let's start with discrete distributions. +我们从离散分布开始。 -A discrete distribution is defined by a set of numbers $S = \{x_1, \ldots, x_n\}$ and a **probability mass function** (PMF) on $S$, which is a function $p$ from $S$ to $[0,1]$ with the property +离散分布由一组数值 $S = \{x_1, \ldots, x_n\}$ 定义,并在 $S$ 上有一个**概率质量函数**(PMF),它是一个从 $S$ 到 $[0,1]$ 的函数 $p$,具有属性 $$ \sum_{i=1}^n p(x_i) = 1 $$ -We say that a random variable $X$ **has distribution** $p$ if $X$ takes value $x_i$ with probability $p(x_i)$. +我们说一个随机变量 $X$ **具有分布** $p$,如果 $X$ 以概率 $p(x_i)$ 取值 $x_i$。 -That is, +即, $$ -\mathbb P\{X = x_i\} = p(x_i) \quad \text{for } i= 1, \ldots, n +\mathbb P\{X = x_i\} = p(x_i) \quad \text{对于 } i= 1, \ldots, n $$ -The **mean** or **expected value** of a random variable $X$ with distribution $p$ is +具有分布 $p$ 的随机变量 $X$ 的**均值**或**期望值**是 $$ \mathbb{E}[X] = \sum_{i=1}^n x_i p(x_i) $$ -Expectation is also called the *first moment* of the distribution. +期望也称为分布的*第一矩*。 -We also refer to this number as the mean of the distribution (represented by) $p$. +我们也将这个数字称为分布(由 $p$ 表示)的均值。 -The **variance** of $X$ is defined as +$X$ 的**方差**定义为 $$ \mathbb{V}[X] = \sum_{i=1}^n (x_i - \mathbb{E}[X])^2 p(x_i) $$ -Variance is also called the *second central moment* of the distribution. +方差也称为分布的*第二中心矩*。 -The **cumulative distribution function** (CDF) of $X$ is defined by +$X$ 的**累积分布函数**(CDF)定义为 $$ F(x) = \mathbb{P}\{X \leq x\} = \sum_{i=1}^n \mathbb 1\{x_i \leq x\} p(x_i) $$ -Here $\mathbb 1\{ \textrm{statement} \} = 1$ if "statement" is true and zero otherwise. +这里 $\mathbb 1\{\text{statement} \} = 1$ 如果 "statement" 为真,否则为零。 -Hence the second term takes all $x_i \leq x$ and sums their probabilities. +因此第二项取所有 $x_i \leq x$ 并求它们概率的和。 -#### Uniform distribution +#### 均匀分布 -One simple example is the **uniform distribution**, where $p(x_i) = 1/n$ for all $i$. +一个简单的例子是**均匀分布**,其中 $p(x_i) = 1/n$ 对于所有 $i$ 都成立。 -We can import the uniform distribution on $S = \{1, \ldots, n\}$ from SciPy like so: +我们可以这样从 SciPy 导入 $S = \{1, \ldots, n\}$ 上的均匀分布: ```{code-cell} ipython3 n = 10 u = scipy.stats.randint(1, n+1) ``` -Here's the mean and variance: +计算均值和方差: ```{code-cell} ipython3 u.mean(), u.var() ``` -The formula for the mean is $(n+1)/2$, and the formula for the variance is $(n^2 - 1)/12$. +均值的公式是 $(n+1)/2$,方差的公式是 $(n^2 - 1)/12$。 - -Now let's evaluate the PMF: +现在让我们评估 PMF: ```{code-cell} ipython3 u.pmf(1) ``` - ```{code-cell} ipython3 u.pmf(2) ``` -Here's a plot of the probability mass function: +以下是 PMF 的图: ```{code-cell} ipython3 fig, ax = plt.subplots() @@ -129,8 +130,7 @@ ax.set_xlabel('S') ax.set_ylabel('PMF') plt.show() ``` - -Here's a plot of the CDF: +这里是 CDF 的图: ```{code-cell} ipython3 fig, ax = plt.subplots() @@ -143,73 +143,67 @@ ax.set_ylabel('CDF') plt.show() ``` -The CDF jumps up by $p(x_i)$ at $x_i$. +CDF 在$x_i$处跳升$p(x_i)$。 ```{exercise} :label: prob_ex1 -Calculate the mean and variance for this parameterization (i.e., $n=10$) -directly from the PMF, using the expressions given above. +计算这个参数化(即 $n=10$)的均值和方差,直接从 PMF 使用上面给出的公式计算。 -Check that your answers agree with `u.mean()` and `u.var()`. +验证你的答案与 `u.mean()` 和 `u.var()` 是否一致。 ``` +#### 伯努利分布 -#### Bernoulli distribution - -Another useful distribution is the Bernoulli distribution on $S = \{0,1\}$, which has PMF: +另一个有用的分布是 $S = \{0,1\}$ 上的伯努利分布,其 PMF 是: $$ p(i) = \theta^i (1 - \theta)^{1-i} \qquad (i = 0, 1) $$ -Here $\theta \in [0,1]$ is a parameter. +这里 $\theta \in [0,1]$ 是一个参数。 -We can think of this distribution as modeling probabilities for a random trial with success probability $\theta$. +我们可以将这个分布视为对一个随机试验进行概率建模,其成功概率是 $\theta$。 -* $p(1) = \theta$ means that the trial succeeds (takes value 1) with probability $\theta$ -* $p(0) = 1 - \theta$ means that the trial fails (takes value 0) with - probability $1-\theta$ +* $p(1) = \theta$ 表示试验成功(取值1)的概率是 $\theta$ +* $p(0) = 1 - \theta$ 表示试验失败(取值0)的概率是 $1-\theta$ -The formula for the mean is $\theta$, and the formula for the variance is $\theta(1-\theta)$. +均值的公式是 $\theta$,方差的公式是 $\theta(1-\theta)$。 -We can import the Bernoulli distribution on $S = \{0,1\}$ from SciPy like so: +我们可以这样从 SciPy 导入 $S = \{0,1\}$ 上的伯努利分布: ```{code-cell} ipython3 θ = 0.4 u = scipy.stats.bernoulli(θ) ``` - -Here's the mean and variance at $\theta=0.4$ +这是 $\theta=0.4$ 时的均值和方差: ```{code-cell} ipython3 u.mean(), u.var() ``` - -We can evaluate the PMF as follows +我们可以评估 PMF 如下: ```{code-cell} ipython3 u.pmf(0), u.pmf(1) ``` +#### 二项分布 -#### Binomial distribution - -Another useful (and more interesting) distribution is the **binomial distribution** on $S=\{0, \ldots, n\}$, which has PMF: +另一个有用(而且更有趣)的分布是 $S=\{0, \ldots, n\}$ 上的**二项分布**,其 PMF 为: $$ p(i) = \binom{n}{i} \theta^i (1-\theta)^{n-i} $$ -Again, $\theta \in [0,1]$ is a parameter. +再次强调,$\theta \in [0,1]$ 是一个参数。 -The interpretation of $p(i)$ is: the probability of $i$ successes in $n$ independent trials with success probability $\theta$. +$p(i)$ 的解释是:$n$次独立试验中有$i$次成功的概率,每次试验成功的概率为$\theta$。 -For example, if $\theta=0.5$, then $p(i)$ is the probability of $i$ heads in $n$ flips of a fair coin. +例如,如果$\theta=0.5$,那么$p(i)$就是$n$次抛掷公平硬币得到$i$次正面的概率。 -The formula for the mean is $n \theta$ and the formula for the variance is $n \theta (1-\theta)$. +均值的公式是$n\theta$,方差的公式是$n\theta(1-\theta)$。 -Let's investigate an example +现在让我们来探讨一个例子 ```{code-cell} ipython3 n = 10 @@ -217,19 +211,18 @@ n = 10 u = scipy.stats.binom(n, θ) ``` -According to our formulas, the mean and variance are +根据我们的公式,均值和方差是 ```{code-cell} ipython3 n * θ, n * θ * (1 - θ) ``` - -Let's see if SciPy gives us the same results: +让我们看看SciPy是否给出了相同的结果: ```{code-cell} ipython3 u.mean(), u.var() ``` -Here's the PMF: +这是 PMF: ```{code-cell} ipython3 u.pmf(1) @@ -246,7 +239,7 @@ ax.set_ylabel('PMF') plt.show() ``` -Here's the CDF: +这是 CDF: ```{code-cell} ipython3 fig, ax = plt.subplots() @@ -262,14 +255,14 @@ plt.show() ```{exercise} :label: prob_ex3 -Using `u.pmf`, check that our definition of the CDF given above calculates the same function as `u.cdf`. +使用`u.pmf`,验证我们上面给出的CDF定义是否计算出与`u.cdf`相同的函数。 ``` ```{solution-start} prob_ex3 :class: dropdown ``` -Here is one solution: +这里是一个解决方案: ```{code-cell} ipython3 fig, ax = plt.subplots() @@ -283,30 +276,30 @@ ax.set_ylabel('CDF') plt.show() ``` -We can see that the output graph is the same as the one above. +我们可以看到输出图与上面的相同。 ```{solution-end} ``` -#### Geometric distribution +#### 几何分布 -The geometric distribution has infinite support $S = \{0, 1, 2, \ldots\}$ and its PMF is given by +几何分布具有无限支持集 $S = \{0, 1, 2, \ldots\}$,其概率质量函数(PMF)为 $$ p(i) = (1 - \theta)^i \theta $$ -where $\theta \in [0,1]$ is a parameter +其中 $\theta \in [0,1]$ 是一个参数 -(A discrete distribution has infinite support if the set of points to which it assigns positive probability is infinite.) +(如果一个离散分布赋予正概率的点集是无限的,则称其具有无限支持。) -To understand the distribution, think of repeated independent random trials, each with success probability $\theta$. +为了理解这个分布,可以想象重复的独立随机试验,每次试验的成功概率为 $\theta$。 -The interpretation of $p(i)$ is: the probability there are $i$ failures before the first success occurs. +$p(i)$ 的解释是:第一次成功之前发生了 $i$ 次失败的概率。 -It can be shown that the mean of the distribution is $1/\theta$ and the variance is $(1-\theta)/\theta$. +可以证明该分布的平均值是 $1/\theta$,方差是 $(1-\theta)/\theta$。 -Here's an example. +下面是一个例子。 ```{code-cell} ipython3 θ = 0.1 @@ -314,7 +307,7 @@ u = scipy.stats.geom(θ) u.mean(), u.var() ``` -Here's part of the PMF: +这里是部分PMF: ```{code-cell} ipython3 fig, ax = plt.subplots() @@ -328,19 +321,19 @@ ax.set_ylabel('PMF') plt.show() ``` -#### Poisson distribution +#### 泊松分布 -The Poisson distribution on $S = \{0, 1, \ldots\}$ with parameter $\lambda > 0$ has PMF +泊松分布在 $S = \{0, 1, \ldots\}$ 上,参数为 $\lambda > 0$,其概率质量函数(PMF)为 $$ p(i) = \frac{\lambda^i}{i!} e^{-\lambda} $$ -The interpretation of $p(i)$ is: the probability of $i$ events in a fixed time interval, where the events occur independently at a constant rate $\lambda$. +$p(i)$ 的解释是:在固定时间区间内事件发生 $i$ 次的概率,其中事件以常数率 $\lambda$ 独立发生。 -It can be shown that the mean is $\lambda$ and the variance is also $\lambda$. +可以证明,其均值为 $\lambda$,方差也为 $\lambda$。 -Here's an example. +这里有一个例子。 ```{code-cell} ipython3 λ = 2 @@ -348,7 +341,7 @@ u = scipy.stats.poisson(λ) u.mean(), u.var() ``` -Here's the PMF: +这是概率质量函数: ```{code-cell} ipython3 u.pmf(1) @@ -365,53 +358,52 @@ ax.set_ylabel('PMF') plt.show() ``` -### Continuous distributions - +### 连续分布 -A continuous distribution is represented by a **probability density function**, which is a function $p$ over $\mathbb R$ (the set of all real numbers) such that $p(x) \geq 0$ for all $x$ and +连续分布由一个**概率密度函数**表示,这是一个在全体实数集 $\mathbb R$ 上的函数 $p$,满足对所有的 $x$ 有 $p(x) \geq 0$,并且 $$ -\int_{-\infty}^\infty p(x) dx = 1 +\int_{-\infty}^\infty p(x) \, dx = 1 $$ -We say that random variable $X$ has distribution $p$ if +我们说随机变量 $X$ 若有如下性质则服从分布 $p$: $$ -\mathbb P\{a < X < b\} = \int_a^b p(x) dx +\mathbb P\{a < X < b\} = \int_a^b p(x) \, dx $$ -for all $a \leq b$. +对所有 $a \leq b$ 都成立。 -The definition of the mean and variance of a random variable $X$ with distribution $p$ are the same as the discrete case, after replacing the sum with an integral. +随机变量 $X$ 若服从分布 $p$,其均值和方差的定义与离散情况相同,只是将求和换成了积分。 -For example, the mean of $X$ is +例如,$X$ 的均值为 $$ -\mathbb{E}[X] = \int_{-\infty}^\infty x p(x) dx +\mathbb{E}[X] = \int_{-\infty}^\infty x p(x) \, dx $$ -The **cumulative distribution function** (CDF) of $X$ is defined by +$X$ 的**累积分布函数**(CDF)定义为 $$ F(x) = \mathbb P\{X \leq x\} - = \int_{-\infty}^x p(x) dx + = \int_{-\infty}^x p(x) \, dx $$ -#### Normal distribution +#### 正态分布 -Perhaps the most famous distribution is the **normal distribution**, which has density +也许最著名的分布是**正态分布**,其密度为 $$ p(x) = \frac{1}{\sqrt{2\pi}\sigma} \exp\left(-\frac{(x-\mu)^2}{2\sigma^2}\right) $$ -This distribution has two parameters, $\mu \in \mathbb R$ and $\sigma \in (0, \infty)$. +这个分布有两个参数,$\mu \in \mathbb R$ 和 $\sigma \in (0, \infty)$。 -Using calculus, it can be shown that, for this distribution, the mean is $\mu$ and the variance is $\sigma^2$. +使用微积分,可以证明对于这种分布,均值是 $\mu$,方差是 $\sigma^2$。 -We can obtain the moments, PDF and CDF of the normal density via SciPy as follows: +我们可以通过 SciPy 获取正态分布的矩、PDF 和 CDF: ```{code-cell} ipython3 μ, σ = 0.0, 1.0 @@ -422,7 +414,7 @@ u = scipy.stats.norm(μ, σ) u.mean(), u.var() ``` -Here's a plot of the density --- the famous "bell-shaped curve": +下面是密度的图像——著名的“钟形曲线”: ```{code-cell} ipython3 μ_vals = [-1, 0, 1] @@ -441,7 +433,7 @@ plt.legend() plt.show() ``` -Here's a plot of the CDF: +下面是 CDF 的图像: ```{code-cell} ipython3 fig, ax = plt.subplots() @@ -457,25 +449,25 @@ plt.legend() plt.show() ``` -#### Lognormal distribution +#### 对数正态分布 -The **lognormal distribution** is a distribution on $\left(0, \infty\right)$ with density +**对数正态分布**是定义在 $\left(0, \infty\right)$ 上的一个分布,其密度函数为 $$ p(x) = \frac{1}{\sigma x \sqrt{2\pi}} \exp \left(- \frac{\left(\log x - \mu\right)^2}{2 \sigma^2} \right) $$ -This distribution has two parameters, $\mu$ and $\sigma$. +这个分布有两个参数,$\mu$ 和 $\sigma$。 -It can be shown that, for this distribution, the mean is $\exp\left(\mu + \sigma^2/2\right)$ and the variance is $\left[\exp\left(\sigma^2\right) - 1\right] \exp\left(2\mu + \sigma^2\right)$. +可以证明,对于这个分布,平均值是 $\exp\left(\mu + \sigma^2/2\right)$,方差是 $\left[\exp\left(\sigma^2\right) - 1\right] \exp\left(2\mu + \sigma^2\right)$。 -It can be proved that +可以证明: -* if $X$ is lognormally distributed, then $\log X$ is normally distributed, and -* if $X$ is normally distributed, then $\exp X$ is lognormally distributed. +* 如果 $X$ 是对数正态分布的,则 $\log X$ 是正态分布的, +* 如果 $X$ 是正态分布的,则 $\exp X$ 是对数正态分布的。 -We can obtain the moments, PDF, and CDF of the lognormal density as follows: +我们可以按照下面的方式获取对数正态分布的矩、PDF 和 CDF: ```{code-cell} ipython3 μ, σ = 0.0, 1.0 @@ -519,22 +511,22 @@ plt.legend() plt.show() ``` -#### Exponential distribution +#### 指数分布 -The **exponential distribution** is a distribution supported on $\left(0, \infty\right)$ with density +**指数分布**是定义在 $\left(0, \infty\right)$ 上的分布,其密度函数为 $$ p(x) = \lambda \exp \left( - \lambda x \right) \qquad (x > 0) $$ -This distribution has one parameter $\lambda$. +这个分布有一个参数 $\lambda$。 -The exponential distribution can be thought of as the continuous analog of the geometric distribution. +指数分布可以被视为几何分布的连续等价物。 -It can be shown that, for this distribution, the mean is $1/\lambda$ and the variance is $1/\lambda^2$. +可以证明,对于这个分布,平均值是 $1/\lambda$,方差是 $1/\lambda^2$。 -We can obtain the moments, PDF, and CDF of the exponential density as follows: +我们可以按照下面的方式获取指数分布的矩、PDF 和 CDF: ```{code-cell} ipython3 λ = 1.0 @@ -575,26 +567,24 @@ plt.legend() plt.show() ``` -#### Beta distribution +#### 贝塔分布 -The **beta distribution** is a distribution on $(0, 1)$ with density +**贝塔分布**是定义在 $(0, 1)$ 上的分布,其密度为 $$ p(x) = \frac{\Gamma(\alpha + \beta)}{\Gamma(\alpha) \Gamma(\beta)} x^{\alpha - 1} (1 - x)^{\beta - 1} $$ -where $\Gamma$ is the [gamma function](https://en.wikipedia.org/wiki/Gamma_function). +其中 $\Gamma$ 是[伽马函数](https://en.wikipedia.org/wiki/Gamma_function)。 -(The role of the gamma function is just to normalize the density, so that it -integrates to one.) +(伽马函数的作用是使密度标准化,从而使其积分为一。) -This distribution has two parameters, $\alpha > 0$ and $\beta > 0$. +此分布有两个参数,$\alpha > 0$ 和 $\beta > 0$。 -It can be shown that, for this distribution, the mean is $\alpha / (\alpha + \beta)$ and -the variance is $\alpha \beta / (\alpha + \beta)^2 (\alpha + \beta + 1)$. +可以证明对于该分布,均值为 $\alpha / (\alpha + \beta)$,方差为 $\alpha \beta / (\alpha + \beta)^2 (\alpha + \beta + 1)$。 -We can obtain the moments, PDF, and CDF of the Beta density as follows: +我们可以如下获得贝塔密度的矩、PDF 和 CDF: ```{code-cell} ipython3 α, β = 3.0, 1.0 @@ -636,25 +626,22 @@ plt.legend() plt.show() ``` -#### Gamma distribution +#### 伽马分布 -The **gamma distribution** is a distribution on $\left(0, \infty\right)$ with density +**伽马分布**是定义在 $\left(0, \infty\right)$ 上的分布,其密度为 $$ p(x) = \frac{\beta^\alpha}{\Gamma(\alpha)} x^{\alpha - 1} \exp(-\beta x) $$ -This distribution has two parameters, $\alpha > 0$ and $\beta > 0$. +此分布有两个参数,$\alpha > 0$ 和 $\beta > 0$。 -It can be shown that, for this distribution, the mean is $\alpha / \beta$ and -the variance is $\alpha / \beta^2$. +可以证明对于该分布,均值为 $\alpha / \beta$,方差为 $\alpha / \beta^2$。 -One interpretation is that if $X$ is gamma distributed and $\alpha$ is an -integer, then $X$ is the sum of $\alpha$ independent exponentially distributed -random variables with mean $1/\beta$. +一种解释是,如果 $X$ 是伽马分布并且 $\alpha$ 是一个整数,那么 $X$ 是 $\alpha$ 个独立具有均值 $1/\beta$ 的指数分布随机变量的总和。 -We can obtain the moments, PDF, and CDF of the Gamma density as follows: +我们可以如下获得伽马密度的矩、PDF 和 CDF: ```{code-cell} ipython3 α, β = 3.0, 2.0 @@ -696,12 +683,11 @@ plt.legend() plt.show() ``` -## Observed distributions - +## 观察到的分布 -Sometimes we refer to observed data or measurements as "distributions". +有时候我们将观测到的数据或测量值称为“分布”。 -For example, let's say we observe the income of 10 people over a year: +例如,假设我们观察了10个人一年的收入: ```{code-cell} ipython3 data = [['Hiroshi', 1200], @@ -719,34 +705,32 @@ df = pd.DataFrame(data, columns=['name', 'income']) df ``` -In this situation, we might refer to the set of their incomes as the "income distribution." +在这种情况下,我们可能称他们的收入集合为“收入分布”。 -The terminology is confusing because this set is not a probability distribution ---- it's just a collection of numbers. +这个术语有些令人困惑,因为这个集合不是一个概率分布——它只是一个数字的集合。 -However, as we will see, there are connections between observed distributions (i.e., sets of -numbers like the income distribution above) and probability distributions. +然而,正如我们将看到的,观察到的分布(即,像上述收入分布那样的数字集合)和概率分布之间存在联系。 -Below we explore some observed distributions. +下面我们探索一些观察到的分布。 -### Summary statistics +### 概括统计 -Suppose we have an observed distribution with values $\{x_1, \ldots, x_n\}$ +假设我们有一个观察到的分布,其值为 $\{x_1, \ldots, x_n\}$ -The **sample mean** of this distribution is defined as +这个分布的**样本均值**定义为 $$ \bar x = \frac{1}{n} \sum_{i=1}^n x_i $$ -The **sample variance** is defined as +**样本方差**定义为 $$ \frac{1}{n} \sum_{i=1}^n (x_i - \bar x)^2 $$ -For the income distribution given above, we can calculate these numbers via +对于上面给出的收入分布,我们可以通过下面的方式计算这些数字: ```{code-cell} ipython3 x = df['income'] @@ -756,44 +740,40 @@ x.mean(), x.var() ```{exercise} :label: prob_ex4 -If you try to check that the formulas given above for the sample mean and sample -variance produce the same numbers, you will see that the variance isn't quite -right. This is because SciPy uses $1/(n-1)$ instead of $1/n$ as the term at the -front of the variance. (Some books define the sample variance this way.) -Confirm. +如果你尝试检查上述给出的样本均值和样本方差的公式是否能产生相同的数字,你会发现方差并不完全正确。这是因为SciPy使用的是 $1/(n-1)$ 而不是 $1/n$ 作为方差的前面的系数。(有些书籍就是这样定义样本方差的。) +确认。 ``` +### 可视化 -### Visualization +让我们来看看我们可以用哪些方式来可视化一个或多个观察到的分布。 -Let's look at different ways that we can visualize one or more observed distributions. +我们将讲解 -We will cover +- 直方图 +- 核密度估计和 +- 小提琴图 -- histograms -- kernel density estimates and -- violin plots +#### 直方图 -#### Histograms - -We can histogram the income distribution we just constructed as follows +我们可以如下制作我们刚刚建立的收入分布的直方图 ```{code-cell} ipython3 fig, ax = plt.subplots() ax.hist(x, bins=5, density=True, histtype='bar') -ax.set_xlabel('income') -ax.set_ylabel('density') +ax.set_xlabel('收入') +ax.set_ylabel('密度') plt.show() ``` -Let's look at a distribution from real data. +让我们来看一个真实数据的分布。 -In particular, we will look at the monthly return on Amazon shares between 2000/1/1 and 2024/1/1. +特别是,我们将看一下2000/1/1至2024/1/1之间亚马逊股票的月收益率。 -The monthly return is calculated as the percent change in the share price over each month. +月收益率是每个月股价变动的百分比。 -So we will have one observation for each month. +因此,我们将得到每个月的一个观测。 ```{code-cell} ipython3 :tags: [hide-output] @@ -804,73 +784,72 @@ x_amazon = prices.pct_change()[1:] * 100 x_amazon.head() ``` -The first observation is the monthly return (percent change) over January 2000, which was +第一个观察结果是2000年1月的月回报(百分比变化),这是 ```{code-cell} ipython3 x_amazon.iloc[0] ``` -Let's turn the return observations into an array and histogram it. +让我们将回报观测值转换成数组并制作直方图。 ```{code-cell} ipython3 fig, ax = plt.subplots() ax.hist(x_amazon, bins=20) -ax.set_xlabel('monthly return (percent change)') -ax.set_ylabel('density') +ax.set_xlabel('月收益率(百分比变化)') +ax.set_ylabel('密度') plt.show() ``` -#### Kernel density estimates +#### 核密度估计 -Kernel density estimates (KDE) provide a simple way to estimate and visualize the density of a distribution. +核密度估计(KDE)提供了一种简单的方式来估计和可视化分布的密度。 -If you are not familiar with KDEs, you can think of them as a smoothed -histogram. +如果你不熟悉核密度估计,可以将其视为平滑的直方图。 -Let's have a look at a KDE formed from the Amazon return data. +让我们看看从亚马逊退货数据中形成的KDE。 ```{code-cell} ipython3 fig, ax = plt.subplots() sns.kdeplot(x_amazon, ax=ax) -ax.set_xlabel('monthly return (percent change)') +ax.set_xlabel('月度回报率(百分比变化)') ax.set_ylabel('KDE') plt.show() ``` -The smoothness of the KDE is dependent on how we choose the bandwidth. +KDE的平滑程度取决于我们选择带宽的方式。 ```{code-cell} ipython3 fig, ax = plt.subplots() sns.kdeplot(x_amazon, ax=ax, bw_adjust=0.1, alpha=0.5, label="bw=0.1") sns.kdeplot(x_amazon, ax=ax, bw_adjust=0.5, alpha=0.5, label="bw=0.5") sns.kdeplot(x_amazon, ax=ax, bw_adjust=1, alpha=0.5, label="bw=1") -ax.set_xlabel('monthly return (percent change)') +ax.set_xlabel('月度回报率(百分比变化)') ax.set_ylabel('KDE') plt.legend() plt.show() ``` -When we use a larger bandwidth, the KDE is smoother. +当我们使用较大的带宽时,KDE更加平滑。 -A suitable bandwidth is not too smooth (underfitting) or too wiggly (overfitting). +一个合适的带宽既不应过于平滑(欠拟合),也不应过于曲折(过拟合)。 -#### Violin plots +#### 小提琴图 -Another way to display an observed distribution is via a violin plot. +通过小提琴图展示观察到的分布是另一种方式。 ```{code-cell} ipython3 fig, ax = plt.subplots() ax.violinplot(x_amazon) -ax.set_ylabel('monthly return (percent change)') +ax.set_ylabel('月度回报率(百分比变化)') ax.set_xlabel('KDE') plt.show() ``` -Violin plots are particularly useful when we want to compare different distributions. +小提琴图在我们想要比较不同分布时特别有用。 -For example, let's compare the monthly returns on Amazon shares with the monthly return on Costco shares. +例如,让我们比较亚马逊股份的月度回报与Costco股份的月度回报。 ```{code-cell} ipython3 :tags: [hide-output] @@ -883,72 +862,70 @@ x_costco = prices.pct_change()[1:] * 100 ```{code-cell} ipython3 fig, ax = plt.subplots() ax.violinplot([x_amazon, x_costco]) -ax.set_ylabel('monthly return (percent change)') -ax.set_xlabel('retailers') +ax.set_ylabel('月度回报率(百分比变化)') +ax.set_xlabel('零售商') ax.set_xticks([1, 2]) -ax.set_xticklabels(['Amazon', 'Costco']) +ax.set_xticklabels(['亚马逊', '开市客']) plt.show() ``` -### Connection to probability distributions +### 与概率分布的联系 -Let's discuss the connection between observed distributions and probability distributions. +让我们讨论一下观察到的分布与概率分布之间的联系。 -Sometimes it's helpful to imagine that an observed distribution is generated by a particular probability distribution. +有时候,想象一个观察到的分布是由特定的概率分布生成的会很有帮助。 -For example, we might look at the returns from Amazon above and imagine that they were generated by a normal distribution. +例如,我们可能会观察上面亚马逊的回报,并想象它们是由正态分布生成的。 -(Even though this is not true, it *might* be a helpful way to think about the data.) +(尽管这不是真的,但这*可能*是一种有帮助的思考数据的方式。) -Here we match a normal distribution to the Amazon monthly returns by setting the -sample mean to the mean of the normal distribution and the sample variance equal -to the variance. +这里我们通过将样本均值设为正态分布的均值,将样本方差设为方差,来匹配正态分布到亚马逊月度回报上。 -Then we plot the density and the histogram. +然后我们绘制密度和直方图。 ```{code-cell} ipython3 -μ = x_amazon.mean() -σ_squared = x_amazon.var() -σ = np.sqrt(σ_squared) -u = scipy.stats.norm(μ, σ) +μ = x_amazon.mean() +σ_squared = x_amazon.var() +σ = np.sqrt(σ_squared) +u = scipy.stats.norm(μ, σ) ``` ```{code-cell} ipython3 -x_grid = np.linspace(-50, 65, 200) -fig, ax = plt.subplots() -ax.plot(x_grid, u.pdf(x_grid)) -ax.hist(x_amazon, density=True, bins=40) -ax.set_xlabel('monthly return (percent change)') -ax.set_ylabel('density') +x_grid = np.linspace(-50, 65, 200) +fig, ax = plt.subplots() +ax.plot(x_grid, u.pdf(x_grid)) +ax.hist(x_amazon, density=True, bins=40) +ax.set_xlabel('月度回报(百分比变化)') +ax.set_ylabel('密度') plt.show() ``` -The match between the histogram and the density is not bad but also not very good. +直方图与密度的匹配不错,但也不是很好。 -One reason is that the normal distribution is not really a good fit for this observed data --- we will discuss this point again when we talk about {ref}`heavy tailed distributions`. +一个原因是正态分布实际上并不真正适合这个观察数据 --- 我们在讨论{ref}`重尾分布`时将再次提到这一点。 -Of course, if the data really *is* generated by the normal distribution, then the fit will be better. +当然,如果数据真的是由正态分布生成的,那么拟合效果会更好。 -Let's see this in action +让我们看到这一点在实际中的运用: -- first we generate random draws from the normal distribution -- then we histogram them and compare with the density. +- 首先我们从正态分布中生成随机抽样 +- 然后我们对它们进行直方图绘制,并与密度比较。 ```{code-cell} ipython3 -μ, σ = 0, 1 -u = scipy.stats.norm(μ, σ) -N = 2000 # Number of observations -x_draws = u.rvs(N) -x_grid = np.linspace(-4, 4, 200) -fig, ax = plt.subplots() -ax.plot(x_grid, u.pdf(x_grid)) -ax.hist(x_draws, density=True, bins=40) +μ, σ = 0, 1 +u = scipy.stats.norm(μ, σ) +N = 2000 +x_draws = u.rvs(N) +x_grid = np.linspace(-4, 4, 200) +fig, ax = plt.subplots() +ax.plot(x_grid, u.pdf(x_grid)) +ax.hist(x_draws, density=True, bins=40) ax.set_xlabel('x') -ax.set_ylabel('density') +ax.set_ylabel('密度') plt.show() ``` -Note that if you keep increasing $N$, which is the number of observations, the fit will get better and better. +请注意,如果你不断增加 $N$,即观测数量,拟合效果会越来越好。 -This convergence is a version of the "law of large numbers", which we will discuss {ref}`later`. +这种收敛是“大数定律”的一个版本,我们将在{ref}`以后`讨论。 \ No newline at end of file diff --git a/lectures/pv.md b/lectures/pv.md index 595f749..e898980 100644 --- a/lectures/pv.md +++ b/lectures/pv.md @@ -11,95 +11,90 @@ kernelspec: name: python3 --- -# Present Values +# 现值 -## Overview +## 概述 -This lecture describes the **present value model** that is a starting point -of much asset pricing theory. +本讲座描述了**现值模型**,这是许多资产定价理论的起点。 -Asset pricing theory is a component of theories about many economic decisions including +资产定价理论是关于许多经济决策理论的组成部分,包括 - * consumption - * labor supply - * education choice - * demand for money + * 消费 + * 劳动力供给 + * 教育选择 + * 货币需求 -In asset pricing theory, and in economic dynamics more generally, a basic topic is the relationship -among different **time series**. +在资产定价理论中,更广泛的经济动态中,一个基本主题是不同**时间序列**之间的关系。 -A **time series** is a **sequence** indexed by time. +**时间序列**是按时间索引的**序列**。 -In this lecture, we'll represent a sequence as a vector. +在本讲座中,我们将把序列表示为向量。 -So our analysis will typically boil down to studying relationships among vectors. +因此,我们的分析通常归结为研究向量之间的关系。 -Our main tools in this lecture will be +本讲座中的主要工具将是 - * matrix multiplication, and - * matrix inversion. + * 矩阵乘法,和 + * 矩阵求逆。 -We'll use the calculations described here in subsequent lectures, including {doc}`consumption smoothing `, {doc}`equalizing difference model `, and -{doc}`monetarist theory of price levels `. +我们将在后续讲座中使用这里描述的计算,包括{doc}`消费平滑 `,{doc}`均衡差异模型 `,和{doc}`货币主义价格水平理论 `。 -Let's dive in. +让我们开始吧。 -## Analysis +## 分析 +设 -Let + * $\{d_t\}_{t=0}^T $ 是一系列股息或“支付” + * $\{p_t\}_{t=0}^T $ 是从$t$日期开始的资产支付流的延续索赔价格序列,即$\{d_s\}_{s=t}^T $ + * $ \delta \in (0,1) $ 是一个周期的“折现因子” + * $p_{T+1}^*$ 是时间$T+1$时资产的终端价格 - * $\{d_t\}_{t=0}^T $ be a sequence of dividends or "payouts" - * $\{p_t\}_{t=0}^T $ be a sequence of prices of a claim on the continuation of - the asset's payout stream from date $t$ on, namely, $\{d_s\}_{s=t}^T $ - * $ \delta \in (0,1) $ be a one-period "discount factor" - * $p_{T+1}^*$ be a terminal price of the asset at time $T+1$ - -We assume that the dividend stream $\{d_t\}_{t=0}^T $ and the terminal price -$p_{T+1}^*$ are both exogenous. +我们假设股息流$\{d_t\}_{t=0}^T $和终端价格$p_{T+1}^*$都是外生的。 -This means that they are determined outside the model. +这意味着它们是在模型之外确定的。 -Assume the sequence of asset pricing equations +假设资产定价方程序列 $$ p_t = d_t + \delta p_{t+1}, \quad t = 0, 1, \ldots , T $$ (eq:Euler1) -We say equation**s**, plural, because there are $T+1$ equations, one for each $t =0, 1, \ldots, T$. +我们说方程**s**,复数,因为有$T+1$个方程,每个$t =0, 1, \ldots, T$都有一个。 +方程{eq}`eq:Euler1`断言在时间$t$购买资产所支付的价格等于支付$d_t$加上时间$t+1$的价格乘以时间折现因子$\delta$。 -Equations {eq}`eq:Euler1` assert that price paid to purchase the asset at time $t$ equals the payout $d_t$ plus the price at time $t+1$ multiplied by a time discount factor $\delta$. +通过将明天的价格乘以$\delta$来折现,考虑了“等待一个周期的价值”。 -Discounting tomorrow's price by multiplying it by $\delta$ accounts for the "value of waiting one period". +我们想要解决$T+1$个方程{eq}`eq:Euler1`的系统,以资产价格序列$\{p_t\}_{t=0}^T $作为股息序列$\{d_t\}_{t=0}^T $和外生终端价格$p_{T+1}^*$的函数。 -We want to solve the system of $T+1$ equations {eq}`eq:Euler1` for the asset price sequence $\{p_t\}_{t=0}^T $ as a function of the dividend sequence $\{d_t\}_{t=0}^T $ and the exogenous terminal -price $p_{T+1}^*$. +像{eq}`eq:Euler1`这样的方程系统是线性**差分方程**的一个例子。 -A system of equations like {eq}`eq:Euler1` is an example of a linear **difference equation**. +有强大的数学方法可以用来解决这样的系统,它们本身就值得研究,因为它们是分析许多有趣经济模型的基础。 -There are powerful mathematical methods available for solving such systems and they are well worth -studying in their own right, being the foundation for the analysis of many interesting economic models. +例如,参见{doc}`Samuelson乘数-加速器 ` -For an example, see {doc}`Samuelson multiplier-accelerator ` +在本讲座中,我们将使用矩阵乘法和矩阵求逆来解决系统{eq}`eq:Euler1`,这是线性代数中的基本工具,在{doc}`线性方程和矩阵代数 `中介绍。 -In this lecture, we'll solve system {eq}`eq:Euler1` using matrix multiplication and matrix inversion, basic tools from linear algebra introduced in {doc}`linear equations and matrix algebra `. - -We will use the following imports +我们将导入以下的库 +++ ```{code-cell} ipython3 import numpy as np import matplotlib.pyplot as plt +from matplotlib import font_manager +fontP = font_manager.FontProperties() +fontP.set_family('SimHei') +fontP.set_size(14) ``` +++ -## Representing sequences as vectors +## 将序列表示为向量 -The equations in system {eq}`eq:Euler1` can be arranged as follows: +系统{eq}`eq:Euler1`中的方程可以如下排列: $$ \begin{aligned} @@ -111,7 +106,7 @@ $$ \end{aligned} $$ (eq:Euler_stack) -Write the system {eq}`eq:Euler_stack` of $T+1$ asset pricing equations as the single matrix equation +将$T+1$个资产定价方程的系统{eq}`eq:Euler_stack`写成单个矩阵方程 $$ \begin{bmatrix} 1 & -\delta & 0 & 0 & \cdots & 0 & 0 \cr @@ -130,25 +125,25 @@ $$ \end{bmatrix} $$ (eq:pvpieq) + +++ ```{exercise-start} :label: pv_ex_1 ``` -Carry out the matrix multiplication in [](eq:pvpieq) by hand and confirm that you -recover the equations in [](eq:Euler_stack). +手动用矩阵乘法对[](eq:pvpieq)进行计算,然后用 [](eq:Euler_stack)确认。 ```{exercise-end} ``` -In vector-matrix notation, we can write system {eq}`eq:pvpieq` as +用向量-矩阵表示法,我们可以将系统{eq}`eq:pvpieq`写成 $$ A p = d + b $$ (eq:apdb) -Here $A$ is the matrix on the left side of equation {eq}`eq:pvpieq`, while +这里$A$是方程{eq}`eq:pvpieq`左侧的矩阵,而 $$ p = @@ -176,20 +171,19 @@ $$ \end{bmatrix} $$ -The solution for the vector of prices is +价格向量的解是 $$ p = A^{-1}(d + b) $$ (eq:apdb_sol) - -For example, suppose that the dividend stream is +例如,假设股息流是 $$ d_{t+1} = 1.05 d_t, \quad t = 0, 1, \ldots , T-1. $$ -Let's write Python code to compute and plot the dividend stream. +让我们编写Python代码来计算和绘制股息流。 ```{code-cell} ipython3 T = 6 @@ -200,21 +194,21 @@ for t in range(T+1): current_d = current_d * 1.05 fig, ax = plt.subplots() -ax.plot(d, 'o', label='dividends') +ax.plot(d, 'o', label='股息') ax.legend() -ax.set_xlabel('time') +ax.set_xlabel('时间') plt.show() ``` -Now let's compute and plot the asset price. +现在让我们来计算和绘制资产价格。 -We set $\delta$ and $p_{T+1}^*$ to +我们将 $\delta$ 和 $p_{T+1}^*$ 设定为 ```{code-cell} ipython3 δ = 0.99 p_star = 10.0 ``` -Let's build the matrix $A$ +让我们来建立矩阵 $A$ ```{code-cell} ipython3 A = np.zeros((T+1, T+1)) @@ -226,34 +220,29 @@ for i in range(T+1): A[i, j+1] = -δ ``` - -Let's inspect $A$ - +让我们来一起检视$A$ ```{code-cell} ipython3 A ``` - -Now let's solve for prices using {eq}`eq:apdb_sol`. +让我们用 {eq}`eq:apdb_sol`来求解价格。 ```{code-cell} ipython3 b = np.zeros(T+1) b[-1] = δ * p_star p = np.linalg.solve(A, d + b) fig, ax = plt.subplots() -ax.plot(p, 'o', label='asset price') +ax.plot(p, 'o', label='资产价格') ax.legend() -ax.set_xlabel('time') +ax.set_xlabel('时间') plt.show() ``` - -Now let's consider a cyclically growing dividend sequence: +现在让我们来看一个周期性增长的股息序列: $$ d_{t+1} = 1.01 d_t + 0.1 \sin t, \quad t = 0, 1, \ldots , T-1. $$ - ```{code-cell} ipython3 T = 100 current_d = 1.0 @@ -263,27 +252,23 @@ for t in range(T+1): current_d = current_d * 1.01 + 0.1 * np.sin(t) fig, ax = plt.subplots() -ax.plot(d, 'o-', ms=4, alpha=0.8, label='dividends') +ax.plot(d, 'o-', ms=4, alpha=0.8, label='股息') ax.legend() -ax.set_xlabel('time') +ax.set_xlabel('时间') plt.show() ``` ```{exercise-start} :label: pv_ex_cyc ``` - -Compute the corresponding asset price sequence when $p^*_{T+1} = 0$ and $\delta -= 0.98$. - +当$p^*_{T+1} = 0$ 和 $\delta = 0.98$ 时,计算相对应的价格序列。 ```{exercise-end} ``` ```{solution-start} pv_ex_cyc :class: dropdown ``` - -We proceed as above after modifying parameters and consequently the matrix $A$. +我们改变之前的参数和矩阵$A$。 ```{code-cell} ipython3 δ = 0.98 @@ -300,26 +285,22 @@ b = np.zeros(T+1) b[-1] = δ * p_star p = np.linalg.solve(A, d + b) fig, ax = plt.subplots() -ax.plot(p, 'o-', ms=4, alpha=0.8, label='asset price') +ax.plot(p, 'o-', ms=4, alpha=0.8, label='资产价格') ax.legend() -ax.set_xlabel('time') +ax.set_xlabel('时间') plt.show() ``` - -The weighted averaging associated with the present value calculation largely -eliminates the cycles. - +与现值计算相关的加权平均在很大程度上消除了周期。 ```{solution-end} ``` -## Analytical expressions - -By the [inverse matrix theorem](https://en.wikipedia.org/wiki/Invertible_matrix), a matrix $B$ is the inverse of $A$ whenever $A B$ is the identity. +## 解析表达式 -It can be verified that the inverse of the matrix $A$ in {eq}`eq:pvpieq` is +根据逆矩阵定理,当$A B$是单位矩阵时,矩阵$B$是$A$的逆矩阵。 +可以验证,{eq}`eq:pvpieq`中的矩阵$A$的逆矩阵是 $$ A^{-1} = \begin{bmatrix} @@ -331,56 +312,44 @@ $$ A^{-1} = \end{bmatrix} $$ (eq:Ainv) - - ```{exercise-start} :label: pv_ex_2 ``` -Check this by showing that $A A^{-1}$ is equal to the identity matrix. - +通过证明$AA^{-1}$为单位矩阵来检查。 ```{exercise-end} ``` - -If we use the expression {eq}`eq:Ainv` in {eq}`eq:apdb_sol` and perform the indicated matrix multiplication, we shall find that +如果我们在 {eq}`eq:apdb_sol` 中使用表达式 {eq}`eq:Ainv` 并执行所指示的矩阵乘法,我们将发现 $$ p_t = \sum_{s=t}^T \delta^{s-t} d_s + \delta^{T+1-t} p_{T+1}^* $$ (eq:ptpveq) -Pricing formula {eq}`eq:ptpveq` asserts that two components sum to the asset price -$p_t$: +定价公式 {eq}`eq:ptpveq` 断言两个组成部分相加得到资产价格 $p_t$: - * a **fundamental component** $\sum_{s=t}^T \delta^{s-t} d_s$ that equals the **discounted present value** of prospective dividends - - * a **bubble component** $\delta^{T+1-t} p_{T+1}^*$ +* 一个**基本组成部分** $\sum_{s=t}^T \delta^{s-t} d_s$,等于预期股息的**贴现现值** -The fundamental component is pinned down by the discount factor $\delta$ and the -payout of the asset (in this case, dividends). - -The bubble component is the part of the price that is not pinned down by -fundamentals. - -It is sometimes convenient to rewrite the bubble component as +* 一个**泡沫组成部分** $\delta^{T+1-t} p_{T+1}^*$ +基本组成部分由贴现因子 $\delta$ 和资产的支付(在这种情况下为股息)确定。 +泡沫组成部分是价格中不由基本面决定的部分。 +有时将泡沫组成部分重写为 $$ c \delta^{-t} $$ - -where - +更为方便,其中 $$ c \equiv \delta^{T+1}p_{T+1}^* $$ +++ -## More about bubbles +## 关于泡沫的更多内容 -For a few moments, let's focus on the special case of an asset that never pays dividends, in which case +让我们暂时关注一种特殊情况,即一种永不支付股息的资产,在这种情况下 $$ \begin{bmatrix} @@ -393,8 +362,7 @@ $$ +++ -In this case system {eq}`eq:Euler1` of our $T+1$ asset pricing equations takes the -form of the single matrix equation +在这种情况下,我们的 $T+1$ 资产定价方程系统 {eq}`eq:Euler1` 采用以下单一矩阵方程的形式: $$ \begin{bmatrix} 1 & -\delta & 0 & 0 & \cdots & 0 & 0 \cr @@ -410,58 +378,48 @@ $$ \end{bmatrix} $$ (eq:pieq2) -Evidently, if $p_{T+1}^* = 0$, a price vector $p$ of all entries zero -solves this equation and the only the **fundamental** component of our pricing -formula {eq}`eq:ptpveq` is present. +显然,如果 $p_{T+1}^* = 0$,一个所有元素为零的价格向量 $p$ 可以解这个方程,此时我们定价公式 {eq}`eq:ptpveq` 中只有**基本面**成分存在。 -But let's activate the **bubble** component by setting +但让我们通过设置以下条件来激活**泡沫**成分: $$ p_{T+1}^* = c \delta^{-(T+1)} $$ (eq:eqbubbleterm) -for some positive constant $c$. +其中 $c$ 为某个正常数。 -In this case, when we multiply both sides of {eq}`eq:pieq2` by -the matrix $A^{-1}$ presented in equation {eq}`eq:Ainv`, we - find that +在这种情况下,当我们用方程 {eq}`eq:Ainv` 中的矩阵 $A^{-1}$ 乘以 {eq}`eq:pieq2` 的两边时,我们发现: $$ p_t = c \delta^{-t} $$ (eq:bubble) +## 总回报率 -## Gross rate of return - -Define the gross rate of return on holding the asset from period $t$ to period $t+1$ -as +定义从 $t$ 期到 $t+1$ 期持有资产的总回报率为: $$ R_t = \frac{p_{t+1}}{p_t} $$ (eq:rateofreturn) -Substituting equation {eq}`eq:bubble` into equation {eq}`eq:rateofreturn` confirms that an asset whose sole source of value is a bubble earns a gross rate of return +将方程 {eq}`eq:bubble` 代入方程 {eq}`eq:rateofreturn` 可以确认,一个价值完全来源于泡沫的资产的总回报率为: $$ R_t = \delta^{-1} > 1 , t = 0, 1, \ldots, T $$ - -## Exercises +## 练习 ```{exercise-start} :label: pv_ex_a ``` +给出以下 $d$ 和 $p_{T+1}^*$ 设置下资产价格 $p_t$ 的分析表达式: -Give analytical expressions for an asset price $p_t$ under the -following settings for $d$ and $p_{T+1}^*$: - -1. $p_{T+1}^* = 0, d_t = g^t d_0$ (a modified version of the Gordon growth formula) -1. $p_{T+1}^* = g^{T+1} d_0, d_t = g^t d_0$ (the plain vanilla Gordon growth formula) -1. $p_{T+1}^* = 0, d_t = 0$ (price of a worthless stock) -1. $p_{T+1}^* = c \delta^{-(T+1)}, d_t = 0$ (price of a pure bubble stock) - +1. $p_{T+1}^* = 0, d_t = g^t d_0$(戈登增长公式的修改版) +2. $p_{T+1}^* = g^{T+1} d_0, d_t = g^t d_0$(普通的戈登增长公式) +3. $p_{T+1}^* = 0, d_t = 0$(一个无价值股票的价格) +4. $p_{T+1}^* = c \delta^{-(T+1)}, d_t = 0$(一个纯泡沫股票的价格) ```{exercise-end} ``` @@ -469,14 +427,14 @@ following settings for $d$ and $p_{T+1}^*$: ```{solution-start} pv_ex_a :class: dropdown ``` - -Plugging each pair of the above $p_{T+1}^*, d_t$ into Equation {eq}`eq:ptpveq` yields: +将上述每对 $p_{T+1}^*, d_t$ 代入方程 {eq}`eq:ptpveq` 得到: 1. $p_t = \sum^T_{s=t} \delta^{s-t} g^s d_0$ -1. $p_t = \sum^T_{s=t} \delta^{s-t} g^s d_0 + \delta^{T+1-t} g^{T+1} d_0$ -1. $p_t = 0$ -1. $p_t = c \delta^{-t}$ +2. $p_t = \sum^T_{s=t} \delta^{s-t} g^s d_0 + \delta^{T+1-t} g^{T+1} d_0$ +3. $p_t = 0$ +4. $p_t = c \delta^{-t}$ ```{solution-end} -``` \ No newline at end of file +``` + diff --git a/lectures/schelling.md b/lectures/schelling.md index 8032e10..cee219c 100644 --- a/lectures/schelling.md +++ b/lectures/schelling.md @@ -20,7 +20,7 @@ kernelspec: ``` -# Racial Segregation +# 种族隔离 ```{index} single: Schelling Segregation Model ``` @@ -28,147 +28,135 @@ kernelspec: ```{index} single: Models; Schelling's Segregation Model ``` -## Outline +## 大纲 -In 1969, Thomas C. Schelling developed a simple but striking model of racial -segregation {cite}`Schelling1969`. +1969年,托马斯·C·谢林开发了一个简单但引人注目的种族隔离模型 {cite}`Schelling1969`。 -His model studies the dynamics of racially mixed neighborhoods. +他的模型研究了种族混合邻里的动态变化。 -Like much of Schelling's work, the model shows how local interactions can lead -to surprising aggregate outcomes. +就像谢林的许多工作一样,该模型显示出局部互动如何导致令人惊讶的总体结果。 -It studies a setting where agents (think of households) have relatively mild -preference for neighbors of the same race. +它研究了一个环境,其中代理(可以认为是家庭)对同种族的邻居有相对温和的偏好。 -For example, these agents might be comfortable with a mixed race neighborhood -but uncomfortable when they feel "surrounded" by people from a different race. +例如,这些代理可能对混种种族的邻里感到舒适,但当他们感觉被不同种族的人“包围”时会感到不舒服。 -Schelling illustrated the follow surprising result: in such a setting, mixed -race neighborhoods are likely to be unstable, tending to collapse over time. +谢林展示了以下令人惊讶的结果:在这样的环境中,混合种族的社区很可能是不稳定的,倾向于随时间崩溃。 -In fact the model predicts strongly divided neighborhoods, with high levels of -segregation. +事实上,该模型预测了高度分裂的社区,具有高水平的隔离。 -In other words, extreme segregation outcomes arise even though people's -preferences are not particularly extreme. +换句话说,尽管人们的偏好并不特别极端,但仍会出现极端的隔离结果。 -These extreme outcomes happen because of *interactions* between agents in the -model (e.g., households in a city) that drive self-reinforcing dynamics in the -model. +这些极端结果是因为模型中代理之间的*互动*(例如,城市中的家庭)驱动模型中的自我加强动态。 -These ideas will become clearer as the lecture unfolds. +随着讲座的展开,这些想法将变得更加清晰。 -In recognition of his work on segregation and other research, Schelling was -awarded the 2005 Nobel Prize in Economic Sciences (joint with Robert Aumann). +为了表彰他在种族隔离和其他研究方面的工作,谢林获得了2005年诺贝尔经济科学奖(与罗伯特·奥曼共享)。 - -Let's start with some imports: +让我们从一些导入开始: ```{code-cell} ipython3 import matplotlib.pyplot as plt from random import uniform, seed from math import sqrt import numpy as np +import matplotlib as mpl +FONTPATH = "fonts/SourceHanSerifSC-SemiBold.otf" +mpl.font_manager.fontManager.addfont(FONTPATH) +plt.rcParams['font.family'] = ['Source Han Serif SC'] ``` -## The model +## 模型 -In this section we will build a version of Schelling's model. +在这一节中,我们将构建一个Schelling模型的版本。 -### Set-Up +### 设置 -We will cover a variation of Schelling's model that is different from the -original but also easy to program and, at the same time, captures his main -idea. +我们将介绍一个与原始Schelling模型不同的变种,但同样易于编程,并且同时捕捉到了他的主要思想。 -Suppose we have two types of people: orange people and green people. +假设我们有两种类型的人:橙色人和绿色人。 -Assume there are $n$ of each type. +假设每种类型都有$n$个人。 -These agents all live on a single unit square. +这些代理人都居住在一个单位正方形上。 -Thus, the location (e.g, address) of an agent is just a point $(x, y)$, where -$0 < x, y < 1$. +因此,一个代理人的位置(例如,地址)只是一个点$(x, y)$,其中$0 < x, y < 1$。 -* The set of all points $(x,y)$ satisfying $0 < x, y < 1$ is called the **unit square** -* Below we denote the unit square by $S$ +* 所有点$(x,y)$满足$0 < x, y < 1$ 的集合称为**单位正方形** +* 下面我们用$S$表示单位正方形 +++ -### Preferences +### 偏好 -We will say that an agent is *happy* if 5 or more of her 10 nearest neighbors are of the same type. +我们将说一个代理人是*快乐*的,如果她最近的10个邻居中有5个或以上是同类型的。 -An agent who is not happy is called *unhappy*. +一个不快乐的代理人被称为*不快乐*。 -For example, +例如, -* if an agent is orange and 5 of her 10 nearest neighbors are orange, then she is happy. -* if an agent is green and 8 of her 10 nearest neighbors are orange, then she is unhappy. +* 如果一个代理人是橙色的,她最近的10个邻居中有5个是橙色的,那么她是快乐的。 +* 如果一个代理人是绿色的,她最近的10个邻居中有8个是橙色的,那么她是不快乐的。 -'Nearest' is in terms of [Euclidean distance](https://en.wikipedia.org/wiki/Euclidean_distance). +“最近”的意思是根据[欧几里得距离](https://en.wikipedia.org/wiki/Euclidean_distance)。 -An important point to note is that agents are **not** averse to living in mixed areas. +一个重要的点是,代理人**不**反对居住在混合区域。 -They are perfectly happy if half of their neighbors are of the other color. +如果他们的一半邻居是另一种颜色,他们完全满意。 +++ -### Behavior +### 行为 -Initially, agents are mixed together (integrated). +最初,代理们混在一起(集成)。 -In particular, we assume that the initial location of each agent is an -independent draw from a bivariate uniform distribution on the unit square $S$. +特别地,我们假设每个代理的初始位置是从单位正方形 $S$ 上的一个双变量均匀分布中独立抽取的。 -* First their $x$ coordinate is drawn from a uniform distribution on $(0,1)$ -* Then, independently, their $y$ coordinate is drawn from the same distribution. +* 首先,他们的 $x$ 坐标从 $(0,1)$ 上的均匀分布中抽取 +* 然后,独立地,他们的 $y$ 坐标从同一分布中抽取。 -Now, cycling through the set of all agents, each agent is now given the chance to stay or move. +现在,轮流通过所有代理,每个代理现在有机会留下或移动。 -Each agent stays if they are happy and moves if they are unhappy. +每个代理如果满意就留下,不满意就移动。 -The algorithm for moving is as follows +移动的算法如下: -```{prf:algorithm} Jump Chain Algorithm +```{prf:algorithm} 跳转链算法 :label: move_algo -1. Draw a random location in $S$ -1. If happy at new location, move there -1. Otherwise, go to step 1 +1. 在 $S$ 中随机抽取一个位置 +1. 如果在新位置上感到满意,就移动到那里 +1. 否则,回到步骤 1 ``` -We cycle continuously through the agents, each time allowing an unhappy agent -to move. +我们不断地循环通过代理,每次允许不满意的代理移动。 -We continue to cycle until no one wishes to move. +我们继续循环,直到没有人希望移动。 +++ -## Results +## 结果 -Let's now implement and run this simulation. +让我们现在实现和运行这个模拟。 -In what follows, agents are modeled as [objects](https://python-programming.quantecon.org/python_oop.html). +接下来,代理被模型化为[对象](https://python-programming.quantecon.org/python_oop.html)。 -Here's an indication of their structure: +以下是它们的结构指示: ```{code-block} none -* Data: +* 数据: - * type (green or orange) - * location + * 类型(绿色或橙色) + * 位置 -* Methods: +* 方法: - * determine whether happy or not given locations of other agents - * If not happy, move - * find a new location where happy + * 根据其他代理的位置确定是否快乐 + * 如果不快乐,移动 + * 找到一个快乐的新位置 ``` -Let's build them. +让我们构建它们。 ```{code-cell} ipython3 class Agent: @@ -181,57 +169,48 @@ class Agent: self.location = uniform(0, 1), uniform(0, 1) def get_distance(self, other): - "Computes the euclidean distance between self and other agent." + "计算自己与另一代理之间的欧几里得距离。" a = (self.location[0] - other.location[0])**2 b = (self.location[1] - other.location[1])**2 return sqrt(a + b) def happy(self, - agents, # List of other agents - num_neighbors=10, # No. of agents viewed as neighbors - require_same_type=5): # How many neighbors must be same type + agents, # 其他代理的列表 + num_neighbors=10, # 视为邻居的代理数量 + require_same_type=5): # 必须是同一类型的邻居数量 """ - True if a sufficient number of nearest neighbors are of the same - type. + 如果足够多的最近邻居是同一类型,则返回True。 """ distances = [] - # Distances is a list of pairs (d, agent), where d is distance from - # agent to self + # distances是一组对(d, agent),其中d是代理到self的距离 for agent in agents: if self != agent: distance = self.get_distance(agent) distances.append((distance, agent)) - # Sort from smallest to largest, according to distance + # 根据距离从小到大排序 distances.sort() - # Extract the neighboring agents + # 提取邻居代理 neighbors = [agent for d, agent in distances[:num_neighbors]] - # Count how many neighbors have the same type as self + # 计算有多少邻居与自己类型相同 num_same_type = sum(self.type == agent.type for agent in neighbors) return num_same_type >= require_same_type def update(self, agents): - "If not happy, then randomly choose new locations until happy." + "如果不快乐,随机选择新位置直到快乐。" while not self.happy(agents): self.draw_location() ``` - -Here's some code that takes a list of agents and produces a plot showing their -locations on the unit square. - -Orange agents are represented by orange dots and green ones are represented by -green dots. - ```{code-cell} ipython3 def plot_distribution(agents, cycle_num): - "Plot the distribution of agents after cycle_num rounds of the loop." + "绘制经过cycle_num轮循环后,代理分布图。" x_values_0, y_values_0 = [], [] x_values_1, y_values_1 = [], [] - # == Obtain locations of each type == # + # == 获取每种类型的位置 == # for agent in agents: x, y = agent.location if agent.type == 0: @@ -247,149 +226,138 @@ def plot_distribution(agents, cycle_num): 'o', markerfacecolor='orange', **plot_args) ax.plot(x_values_1, y_values_1, 'o', markerfacecolor='green', **plot_args) - ax.set_title(f'Cycle {cycle_num-1}') + ax.set_title(f'周期 {cycle_num-1}') plt.show() ``` -And here's some pseudocode for the main loop, where we cycle through the -agents until no one wishes to move. +在这里有一段伪代码,它描述了主循环的过程,我们在这个过程中遍历每个代理,直到没有代理希望移动为止。 -The pseudocode is +伪代码如下 ```{code-block} none -plot the distribution -while agents are still moving - for agent in agents - give agent the opportunity to move -plot the distribution +绘制分布 +while 代理还在移动 + for 每个代理 in 代理们 + 给予代理机会移动 +绘制分布 ``` -The real code is below +真实的代码如下 ```{code-cell} ipython3 def run_simulation(num_of_type_0=600, num_of_type_1=600, - max_iter=100_000, # Maximum number of iterations + max_iter=100_000, # 最大迭代次数 set_seed=1234): - # Set the seed for reproducibility + # 设置种子以确保可重现性 seed(set_seed) - # Create a list of agents of type 0 + # 创建类型0代理列表 agents = [Agent(0) for i in range(num_of_type_0)] - # Append a list of agents of type 1 + # 添加类型1代理列表 agents.extend(Agent(1) for i in range(num_of_type_1)) - # Initialize a counter + # 初始化计数器 count = 1 - # Plot the initial distribution + # 绘制初始分布 plot_distribution(agents, count) - # Loop until no agent wishes to move + # 循环直到没有代理希望移动 while count < max_iter: - print('Entering loop ', count) + print('进入循环 ', count) count += 1 - no_one_moved = True + 没有人移动 = True for agent in agents: old_location = agent.location agent.update(agents) if agent.location != old_location: - no_one_moved = False - if no_one_moved: + 没有人移动 = False + if 没有人移动: break - # Plot final distribution + # 绘制最终分布 plot_distribution(agents, count) if count < max_iter: - print(f'Converged after {count} iterations.') + print(f'在 {count} 次迭代后收敛。') else: - print('Hit iteration bound and terminated.') + print('达到迭代上限并终止。') ``` -Let's have a look at the results. +让我们看一下结果。 ```{code-cell} ipython3 run_simulation() ``` -As discussed above, agents are initially mixed randomly together. - -But after several cycles, they become segregated into distinct regions. +如上所述,代理最初是随机混合在一起的。 -In this instance, the program terminated after a small number of cycles -through the set of agents, indicating that all agents had reached a state of -happiness. +但经过几轮循环后,它们变得按照不同区域分离。 -What is striking about the pictures is how rapidly racial integration breaks down. +在这个例子中,程序在经过一小部分代理集合的循环后终止,表明所有代理都已达到幸福状态。 -This is despite the fact that people in the model don't actually mind living mixed with the other type. +图片显示的令人吃惊的现象是种族融合的迅速崩溃。 -Even with these preferences, the outcome is a high degree of segregation. +尽管实际上模型中的人并不介意和其他类型的人混居。 +即使是在这些偏好下,结果依旧是高度的隔离。 - -## Exercises +## 练习 ```{exercise-start} :label: schelling_ex1 ``` -The object oriented style that we used for coding above is neat but harder to -optimize than procedural code (i.e., code based around functions rather than -objects and methods). +我们之前用到的面向对象式编程虽然整洁,但相比于过程式编程(即,围绕函数而非对象和方法的编码)更难优化。 -Try writing a new version of the model that stores +尝试编写一个新版本的模型,存储: -* the locations of all agents as a 2D NumPy array of floats. -* the types of all agents as a flat NumPy array of integers. +* 所有代理的位置,作为一个二维的NumPy浮点数数组。 +* 所有代理的类型,作为一个平面的NumPy整数数组。 -Write functions that act on this data to update the model using the logic -similar to that described above. +编写函数,根据上述逻辑对这些数据进行操作以更新模型。 -However, implement the following two changes: +但是,实现以下两个变化: -1. Agents are offered a move at random (i.e., selected randomly and given the - opportunity to move). -2. After an agent has moved, flip their type with probability 0.01 +1. 代理被随机提供移动机会(即,随机选中并给予移动的机会)。 +2. 代理移动后,以0.01的概率翻转其类型。 -The second change introduces extra randomness into the model. +第二个变化为模型引入了额外的随机性。 -(We can imagine that, every so often, an agent moves to a different city and, -with small probability, is replaced by an agent of the other type.) +(我们可以想象,代理偶尔搬到不同的城市,并且小概率会被其他类型的代理替换。) ```{exercise-end} ``` - ```{solution-start} schelling_ex1 :class: dropdown ``` -solution here +解决方案如下 ```{code-cell} ipython3 from numpy.random import uniform, randint -n = 1000 # number of agents (agents = 0, ..., n-1) -k = 10 # number of agents regarded as neighbors -require_same_type = 5 # want >= require_same_type neighbors of the same type +n = 1000 # 代理人数目(代理人编号从0到n-1) +k = 10 # 每个代理人视为邻居的代理人数 +require_same_type = 5 # 希望 >= require_same_type 的邻居是相同类型 def initialize_state(): locations = uniform(size=(n, 2)) - types = randint(0, high=2, size=n) # label zero or one + types = randint(0, high=2, size=n) # 标签为零或一 return locations, types def compute_distances_from_loc(loc, locations): - """ Compute distance from location loc to all other points. """ + """ 计算位置 loc 到所有其他点的距离。 """ return np.linalg.norm(loc - locations, axis=1) def get_neighbors(loc, locations): - " Get all neighbors of a given location. " + " 获取给定位置的所有邻居。 " all_distances = compute_distances_from_loc(loc, locations) - indices = np.argsort(all_distances) # sort agents by distance to loc - neighbors = indices[:k] # keep the k closest ones + indices = np.argsort(all_distances) # 将代理人按距离 loc 的远近排序 + neighbors = indices[:k] # 保留最近的 k 个 return neighbors def is_happy(i, locations, types): @@ -403,14 +371,14 @@ def is_happy(i, locations, types): return happy def count_happy(locations, types): - " Count the number of happy agents. " + " 计算快乐代理人的数量。 " happy_sum = 0 for i in range(n): happy_sum += is_happy(i, locations, types) return happy_sum def update_agent(i, locations, types): - " Move agent if unhappy. " + " 如果代理人不快乐,则移动代理人。 " moved = False while not is_happy(i, locations, types): moved = True @@ -418,7 +386,7 @@ def update_agent(i, locations, types): return moved def plot_distribution(locations, types, title, savepdf=False): - " Plot the distribution of agents after cycle_num rounds of the loop." + " 绘制经过多轮循环后的代理人分布情况。" fig, ax = plt.subplots() colors = 'orange', 'green' for agent_type, color in zip((0, 1), colors): @@ -434,9 +402,9 @@ def plot_distribution(locations, types, title, savepdf=False): def sim_random_select(max_iter=100_000, flip_prob=0.01, test_freq=10_000): """ - Simulate by randomly selecting one household at each update. + 通过随机选择一个家庭进行更新来进行模拟。 - Flip the color of the household with probability `flip_prob`. + 以概率 `flip_prob` 翻转家庭的颜色。 """ @@ -445,39 +413,38 @@ def sim_random_select(max_iter=100_000, flip_prob=0.01, test_freq=10_000): while current_iter <= max_iter: - # Choose a random agent and update them + # 选择一个随机代理人并更新其状态 i = randint(0, n) moved = update_agent(i, locations, types) if flip_prob > 0: - # flip agent i's type with probability epsilon + # 以概率 epsilon 翻转代理人 i 的类型 U = uniform() if U < flip_prob: current_type = types[i] types[i] = 0 if current_type == 1 else 1 - # Every so many updates, plot and test for convergence + # 每隔一定次数更新后,绘图并检查收敛情况 if current_iter % test_freq == 0: cycle = current_iter / n - plot_distribution(locations, types, f'iteration {current_iter}') + plot_distribution(locations, types, f'迭代 {current_iter}') if count_happy(locations, types) == n: - print(f"Converged at iteration {current_iter}") + print(f"在迭代 {current_iter} 时收敛") break current_iter += 1 if current_iter > max_iter: - print(f"Terminating at iteration {current_iter}") + print(f"在迭代 {current_iter} 时终止") ``` - ```{solution-end} ``` +++ -When we run this we again find that mixed neighborhoods break down and segregation emerges. +当我们运行这个程序时,我们再次发现混合社区会瓦解,隔离现象会出现。 -Here's a sample run. +这里是一个样例运行。 ```{code-cell} ipython3 sim_random_select(max_iter=50_000, flip_prob=0.01, test_freq=10_000) @@ -485,4 +452,4 @@ sim_random_select(max_iter=50_000, flip_prob=0.01, test_freq=10_000) ```{code-cell} ipython3 -``` +``` \ No newline at end of file diff --git a/lectures/short_path.md b/lectures/short_path.md index ce547ef..5929117 100644 --- a/lectures/short_path.md +++ b/lectures/short_path.md @@ -18,91 +18,78 @@ kernelspec: ``` -# Shortest Paths +# 最短路径 ```{index} single: Dynamic Programming; Shortest Paths ``` -## Overview +## 概述 -The shortest path problem is a [classic problem](https://en.wikipedia.org/wiki/Shortest_path) in mathematics and computer science with applications in +最短路径问题是数学和计算机科学中的一个经典问题,其应用包括: -* Economics (sequential decision making, analysis of social networks, etc.) -* Operations research and transportation -* Robotics and artificial intelligence -* Telecommunication network design and routing -* etc., etc. +* 经济学(顺序决策、社会网络分析等) +* 运筹学和交通运输 +* 机器人技术和人工智能 +* 电信网络设计和路由 +* 等等 -Variations of the methods we discuss in this lecture are used millions of times every day, in applications such as +我们在本讲座中讨论的方法的变体每天在许多应用中使用数百万次,例如: -* Google Maps -* routing packets on the internet +* 谷歌地图 +* 互联网数据包路由 -For us, the shortest path problem also provides a nice introduction to the logic of **dynamic programming**. +对于我们来说,最短路径问题也为**动态规划**的逻辑提供了一个很好的介绍。 -Dynamic programming is an extremely powerful optimization technique that we apply in many lectures on this site. +动态规划是一种非常强大的优化技术,我们在本网站的许多讲座中都会应用它。 -The only scientific library we'll need in what follows is NumPy: +接下来我们唯一需要的科学库是NumPy: ```{code-cell} python3 import numpy as np ``` -## Outline of the problem - -The shortest path problem is one of finding how to traverse a [graph](https://en.wikipedia.org/wiki/Graph_%28mathematics%29) from one specified node to another at minimum cost. - -Consider the following graph +## 问题概述 +最短路径问题是寻找如何以最小成本从[图](https://en.wikipedia.org/wiki/Graph_%28mathematics%29)中的一个指定节点遍历到另一个节点。 +考虑下面这个图 ```{figure} /_static/lecture_specific/short_path/graph.png ``` -We wish to travel from node (vertex) A to node G at minimum cost - -* Arrows (edges) indicate the movements we can take. -* Numbers on edges indicate the cost of traveling that edge. - -(Graphs such as the one above are called weighted [directed graphs](https://en.wikipedia.org/wiki/Directed_graph).) - -Possible interpretations of the graph include - -* Minimum cost for supplier to reach a destination. -* Routing of packets on the internet (minimize time). -* etc., etc. - -For this simple graph, a quick scan of the edges shows that the optimal paths are - -* A, C, F, G at cost 8 +我们希望以最小成本从节点(顶点)A到达节点G +* 箭头(边)表示我们可以采取的移动。 +* 边上的数字表示沿该边行进的成本。 +(像上面这样的图被称为加权[有向图](https://en.wikipedia.org/wiki/Directed_graph)。) +图的可能解释包括 +* 供应商到达目的地的最小成本。 +* 互联网上的数据包路由(最小化时间)。 +* 等等。 +对于这个简单的图,快速扫描边可以看出最优路径是 +* A, C, F, G,成本为8 ```{figure} /_static/lecture_specific/short_path/graph4.png ``` -* A, D, F, G at cost 8 +* A, D, F, G, 成本为8 ```{figure} /_static/lecture_specific/short_path/graph3.png ``` -## Finding least-cost paths - -For large graphs, we need a systematic solution. - -Let $J(v)$ denote the minimum cost-to-go from node $v$, understood as the total cost from $v$ if we take the best route. - -Suppose that we know $J(v)$ for each node $v$, as shown below for the graph from the preceding example. +## 寻找最低成本路径 +对于大型图,我们需要一个系统的解决方案。 +让 $J(v)$ 表示从节点 $v$ 出发的最小成本,理解为如果我们选择最佳路线,从 $v$ 出发的总成本。 +假设我们知道每个节点 $v$ 的 $J(v)$,如下图所示(基于前面示例中的图)。 ```{figure} /_static/lecture_specific/short_path/graph2.png ``` -Note that $J(G) = 0$. - -The best path can now be found as follows - -1. Start at node $v = A$ -1. From current node $v$, move to any node that solves +注意 $J(G) = 0$。 +现在可以通过以下步骤找到最佳路径: +1. 从节点 $v = A$ 开始 +1. 从当前节点 $v$,移动到解决以下问题的任何节点 ```{math} :label: spprebell @@ -110,17 +97,13 @@ The best path can now be found as follows \min_{w \in F_v} \{ c(v, w) + J(w) \} ``` -where - -* $F_v$ is the set of nodes that can be reached from $v$ in one step. -* $c(v, w)$ is the cost of traveling from $v$ to $w$. - -Hence, if we know the function $J$, then finding the best path is almost trivial. - -But how can we find the cost-to-go function $J$? - -Some thought will convince you that, for every node $v$, -the function $J$ satisfies +其中 +* $F_v$ 是可以从 $v$ 一步到达的节点集合。 +* $c(v, w)$ 是从 $v$ 到 $w$ 的旅行成本。 +因此,如果我们知道函数 $J$,那么找到最佳路径几乎就是微不足道的事。 +但是我们如何找到成本函数 $J$ 呢? +经过一些思考,你会确信对于每个节点 $v$, +函数 $J$ 满足 ```{math} :label: spbell @@ -128,26 +111,23 @@ the function $J$ satisfies J(v) = \min_{w \in F_v} \{ c(v, w) + J(w) \} ``` -This is known as the **Bellman equation**, after the mathematician [Richard Bellman](https://en.wikipedia.org/wiki/Richard_E._Bellman). +这被称为**贝尔曼方程**,以数学家[理查德·贝尔曼](https://en.wikipedia.org/wiki/Richard_E._Bellman)的名字命名。 -The Bellman equation can be thought of as a restriction that $J$ must -satisfy. +贝尔曼方程可以被理解为$J$必须满足的一个限制条件。 -What we want to do now is use this restriction to compute $J$. +我们现在想要做的是利用这个限制条件来计算$J$。 -## Solving for minimum cost-to-go +## 求解最小代价-到-目标 -Let's look at an algorithm for computing $J$ and then think about how to -implement it. +让我们来看一个计算$J$的算法,然后思考如何实现它。 -### The algorithm +### 算法 -The standard algorithm for finding $J$ is to start an initial guess and then iterate. +找到$J$的标准算法是从一个初始猜测开始,然后进行迭代。 -This is a standard approach to solving nonlinear equations, often called -the method of **successive approximations**. +这是解决非线性方程的标准方法,通常被称为**连续近似法**。 -Our initial guess will be +我们的初始猜测将是 ```{math} :label: spguess @@ -155,45 +135,34 @@ Our initial guess will be J_0(v) = 0 \text{ for all } v ``` -Now - -1. Set $n = 0$ -1. Set $J_{n+1} (v) = \min_{w \in F_v} \{ c(v, w) + J_n(w) \}$ for all $v$ -1. If $J_{n+1}$ and $J_n$ are not equal then increment $n$, go to 2 - -This sequence converges to $J$. +现在 +1. 设 $n = 0$ +2. 对所有 $v$,设 $J_{n+1} (v) = \min_{w \in F_v} \{ c(v, w) + J_n(w) \}$ +3. 如果 $J_{n+1}$ 和 $J_n$ 不相等,则将 $n$ 加 1,返回步骤 2 +这个序列收敛于 $J$。 +虽然我们在此省略了证明,但我们将在其他动态规划讲座中证明类似的结论。 -Although we omit the proof, we'll prove similar claims in our other lectures -on dynamic programming. - -### Implementation - -Having an algorithm is a good start, but we also need to think about how to -implement it on a computer. - -First, for the cost function $c$, we'll implement it as a matrix -$Q$, where a typical element is +### 实现 +有了算法是一个好的开始,但我们还需要考虑如何在计算机上实现它。 +首先,对于成本函数 $c$,我们将其实现为矩阵 $Q$,其中典型元素为 $$ Q(v, w) = \begin{cases} - & c(v, w) \text{ if } w \in F_v \\ - & +\infty \text{ otherwise } + & c(v, w) \text{ 如果 } w \in F_v \\ + & +\infty \text{ 否则 } \end{cases} $$ +在这种情况下,$Q$ 通常被称为**距离矩阵**。 -In this context $Q$ is usually called the **distance matrix**. - -We're also numbering the nodes now, with $A = 0$, so, for example - +我们现在也对节点进行编号,其中 $A = 0$,所以,例如 $$ Q(1, 2) = -\text{ the cost of traveling from B to C } +\text{ 从 B 到 C 的旅行成本 } $$ - -For example, for the simple graph above, we set +例如,对于上面的简单图,我们设置 ```{code-cell} python3 from numpy import inf @@ -207,65 +176,55 @@ Q = np.array([[inf, 1, 5, 3, inf, inf, inf], [inf, inf, inf, inf, inf, inf, 0]]) ``` -Notice that the cost of staying still (on the principle diagonal) is set to - -* `np.inf` for non-destination nodes --- moving on is required. -* 0 for the destination node --- here is where we stop. - -For the sequence of approximations $\{J_n\}$ of the cost-to-go functions, we can use NumPy arrays. - -Let's try with this example and see how we go: +请注意,保持不动(在主对角线上)的成本设置为: +* 对于非目的地节点,设为 `np.inf` --- 必须继续移动。 +* 对于目的地节点,设为 0 --- 这是我们停止的地方。 +对于到达成本函数的近似序列 $\{J_n\}$,我们可以使用 NumPy 数组。 +让我们尝试这个例子,看看效果如何: ```{code-cell} python3 -nodes = range(7) # Nodes = 0, 1, ..., 6 -J = np.zeros_like(nodes, dtype=int) # Initial guess -next_J = np.empty_like(nodes, dtype=int) # Stores updated guess +nodes = range(7) # 节点 = 0, 1, ..., 6 +J = np.zeros_like(nodes, dtype=int) # 初始猜测 +next_J = np.empty_like(nodes, dtype=int) # 储存更新的猜测 max_iter = 500 i = 0 while i < max_iter: for v in nodes: - # Minimize Q[v, w] + J[w] over all choices of w + # 最小化所有 w 选择中的 Q[v, w] + J[w] next_J[v] = np.min(Q[v, :] + J) if np.array_equal(next_J, J): break - J[:] = next_J # Copy contents of next_J to J + J[:] = next_J # 将 next_J 的内容复制到 J i += 1 -print("The cost-to-go function is", J) +print("到达成本函数是", J) ``` -This matches with the numbers we obtained by inspection above. - -But, importantly, we now have a methodology for tackling large graphs. - -## Exercises - +这与我们上面通过观察得到的数字相符。 +但更重要的是,我们现在有了一种处理大型图的方法。 +## 练习 ```{exercise-start} :label: short_path_ex1 ``` -The text below describes a weighted directed graph. - -The line `node0, node1 0.04, node8 11.11, node14 72.21` means that from node0 we can go to - -* node1 at cost 0.04 -* node8 at cost 11.11 -* node14 at cost 72.21 +以下文本描述了一个加权有向图。 -No other nodes can be reached directly from node0. +行 `node0, node1 0.04, node8 11.11, node14 72.21` 表示从node0我们可以到达: +* node1,代价为0.04 +* node8,代价为11.11 +* node14,代价为72.21 +从node0无法直接到达其他节点。 +其他行具有类似的解释。 +你的任务是使用上面给出的算法来找到最优路径及其代价。 -Other lines have a similar interpretation. - -Your task is to use the algorithm given above to find the optimal path and its cost. ```{note} -You will be dealing with floating point numbers now, rather than -integers, so consider replacing `np.equal()` with `np.allclose()`. +现在你将处理浮点数而不是整数,所以考虑用 `np.allclose()` 替换 `np.equal()`。 ``` ```{code-cell} python3 @@ -379,7 +338,7 @@ node99, :class: dropdown ``` -First let's write a function that reads in the graph data above and builds a distance matrix. +首先,让我们编写一个函数,读取上面的图数据并构建一个距离矩阵。 ```{code-cell} python3 num_nodes = 100 @@ -387,15 +346,15 @@ destination_node = 99 def map_graph_to_distance_matrix(in_file): - # First let's set of the distance matrix Q with inf everywhere + # 首先,让我们用无穷大初始化距离矩阵Q Q = np.full((num_nodes, num_nodes), np.inf) - # Now we read in the data and modify Q + # 现在我们读取数据并修改Q with open(in_file) as infile: for line in infile: elements = line.split(',') node = elements.pop(0) - node = int(node[4:]) # convert node description to integer + node = int(node[4:]) # 将节点描述转换为整数 if node != destination_node: for element in elements: destination, cost = element.split() @@ -405,14 +364,11 @@ def map_graph_to_distance_matrix(in_file): return Q ``` -In addition, let's write - -1. a "Bellman operator" function that takes a distance matrix and current guess of J and returns an updated guess of J, and -1. a function that takes a distance matrix and returns a cost-to-go function. - -We'll use the algorithm described above. - -The minimization step is vectorized to make it faster. +此外,让我们编写 +1. 一个"贝尔曼算子"函数,该函数接受距离矩阵和当前的J估计值,并返回更新后的J估计值,以及 +2. 一个函数,该函数接受距离矩阵并返回一个代价函数。 +我们将使用上述算法。 +最小化步骤被向量化以提高速度。 ```{code-cell} python3 def bellman(J, Q): @@ -421,7 +377,7 @@ def bellman(J, Q): def compute_cost_to_go(Q): num_nodes = Q.shape[0] - J = np.zeros(num_nodes) # Initial guess + J = np.zeros(num_nodes) # 初始猜测 max_iter = 500 i = 0 @@ -430,17 +386,15 @@ def compute_cost_to_go(Q): if np.allclose(next_J, J): break else: - J[:] = next_J # Copy contents of next_J to J + J[:] = next_J # 将 next_J 的内容复制到 J i += 1 return(J) ``` -We used np.allclose() rather than testing exact equality because we are -dealing with floating point numbers now. +我们使用了 np.allclose() 而不是测试精确相等,因为我们现在处理的是浮点数。 -Finally, here's a function that uses the cost-to-go function to obtain the -optimal path (and its cost). +最后,这里有一个函数,它使用代价函数来获取最优路径(及其成本)。 ```{code-cell} python3 def print_best_path(J, Q): @@ -448,16 +402,16 @@ def print_best_path(J, Q): current_node = 0 while current_node != destination_node: print(current_node) - # Move to the next node and increment costs + # 移动到下一个节点并增加成本 next_node = np.argmin(Q[current_node, :] + J) sum_costs += Q[current_node, next_node] current_node = next_node print(destination_node) - print('Cost: ', sum_costs) + print('成本: ', sum_costs) ``` -Okay, now we have the necessary functions, let's call them to do the job we were assigned. +好了,现在我们已经有了必要的函数,让我们调用它们来完成我们被分配的任务。 ```{code-cell} python3 Q = map_graph_to_distance_matrix('graph.txt') @@ -465,7 +419,7 @@ J = compute_cost_to_go(Q) print_best_path(J, Q) ``` -The total cost of the path should agree with $J[0]$ so let's check this. +路径的总成本应该与 $J[0]$ 一致,所以让我们来验证一下。 ```{code-cell} python3 J[0] diff --git a/lectures/simple_linear_regression.md b/lectures/simple_linear_regression.md index 137d453..83a8ec3 100644 --- a/lectures/simple_linear_regression.md +++ b/lectures/simple_linear_regression.md @@ -11,25 +11,30 @@ kernelspec: name: python3 --- -# Simple Linear Regression Model +# 简单线性回归模型 ```{code-cell} ipython3 import numpy as np import pandas as pd +import matplotlib as mpl import matplotlib.pyplot as plt + +FONTPATH = "fonts/SourceHanSerifSC-SemiBold.otf" +mpl.font_manager.fontManager.addfont(FONTPATH) +plt.rcParams['font.family'] = ['Source Han Serif SC'] ``` -The simple regression model estimates the relationship between two variables $x_i$ and $y_i$ +简单回归模型估计两个变量 $x_i$ 和 $y_i$ 之间的关系 $$ y_i = \alpha + \beta x_i + \epsilon_i, i = 1,2,...,N $$ -where $\epsilon_i$ represents the error between the line of best fit and the sample values for $y_i$ given $x_i$. +其中 $\epsilon_i$ 表示最佳拟合线与样本值 $y_i$ 与 $x_i$ 的误差。 -Our goal is to choose values for $\alpha$ and $\beta$ to build a line of "best" fit for some data that is available for variables $x_i$ and $y_i$. +我们的目标是为 $\alpha$ 和 $\beta$ 选择值来为一些可用的变量 $x_i$ 和 $y_i$ 的数据构建“最佳”拟合线。 -Let us consider a simple dataset of 10 observations for variables $x_i$ and $y_i$: +让我们考虑一个具有10个观察值的简单数据集,变量为 $x_i$ 和 $y_i$: | | $y_i$ | $x_i$ | |-|---|---| @@ -44,7 +49,7 @@ Let us consider a simple dataset of 10 observations for variables $x_i$ and $y_i |9| 1800 | 27 | |10 | 250 | 2 | -Let us think about $y_i$ as sales for an ice-cream cart, while $x_i$ is a variable that records the day's temperature in Celsius. +让我们把 $y_i$ 视为一个冰淇淋车的销售额,而 $x_i$ 是记录当天摄氏度温度的变量。 ```{code-cell} ipython3 x = [32, 21, 24, 35, 10, 11, 22, 21, 27, 2] @@ -54,33 +59,33 @@ df.columns = ['X', 'Y'] df ``` -We can use a scatter plot of the data to see the relationship between $y_i$ (ice-cream sales in dollars (\$\'s)) and $x_i$ (degrees Celsius). +我们可以通过数据的散点图来观察 $y_i$(冰淇淋销售额(美元(\$\'s))和 $x_i$(摄氏度)之间的关系。 ```{code-cell} ipython3 --- mystnb: figure: - caption: "Scatter plot" + caption: "散点图" name: sales-v-temp1 --- ax = df.plot( x='X', y='Y', kind='scatter', - ylabel='Ice-cream sales ($\'s)', - xlabel='Degrees celcius' + ylabel='冰淇淋销售额(\$)', + xlabel='摄氏度' ) ``` -as you can see the data suggests that more ice-cream is typically sold on hotter days. +如您所见,数据表明在更热的日子里通常会卖出更多的冰淇淋。 -To build a linear model of the data we need to choose values for $\alpha$ and $\beta$ that represents a line of "best" fit such that +为了建立数据的线性模型,我们需要选择代表“最佳”拟合线的 $\alpha$ 和 $\beta$ 值,使得 $$ \hat{y_i} = \hat{\alpha} + \hat{\beta} x_i $$ -Let's start with $\alpha = 5$ and $\beta = 10$ +让我们从 $\alpha = 5$ 和 $\beta = 10$ 开始 ```{code-cell} ipython3 α = 5 @@ -92,7 +97,7 @@ df['Y_hat'] = α + β * df['X'] --- mystnb: figure: - caption: "Scatter plot with a line of fit" + caption: "带有拟合线的散点图" name: sales-v-temp2 --- fig, ax = plt.subplots() @@ -101,9 +106,9 @@ ax = df.plot(x='X',y='Y_hat', kind='line', ax=ax) plt.show() ``` -We can see that this model does a poor job of estimating the relationship. +我们可以看到这个模型在估计关系上做得很差。 -We can continue to guess and iterate towards a line of "best" fit by adjusting the parameters +我们可以继续通过调整参数来试图迭代并逼近“最佳”拟合线。 ```{code-cell} ipython3 β = 100 @@ -114,7 +119,7 @@ df['Y_hat'] = α + β * df['X'] --- mystnb: figure: - caption: "Scatter plot with a line of fit #2" + caption: "带拟合线的散点图 #2" name: sales-v-temp3 --- fig, ax = plt.subplots() @@ -132,7 +137,7 @@ df['Y_hat'] = α + β * df['X'] --- mystnb: figure: - caption: "Scatter plot with a line of fit #3" + caption: "带拟合线的散点图 #3" name: sales-v-temp4 --- fig, ax = plt.subplots() @@ -141,9 +146,9 @@ ax = df.plot(x='X',y='Y_hat', kind='line', ax=ax, color='g') plt.show() ``` -However we need to think about formalizing this guessing process by thinking of this problem as an optimization problem. +但是我们需要考虑将这个猜测过程正式化,把这个问题看作是一个优化问题。 -Let's consider the error $\epsilon_i$ and define the difference between the observed values $y_i$ and the estimated values $\hat{y}_i$ which we will call the residuals +让我们考虑误差 $\epsilon_i$ 并定义观测值 $y_i$ 与估计值 $\hat{y}_i$ 之间的差异,我们将其称为残差 $$ \begin{aligned} @@ -164,7 +169,7 @@ df --- mystnb: figure: - caption: "Plot of the residuals" + caption: "残差图" name: plt-residuals --- fig, ax = plt.subplots() @@ -174,32 +179,32 @@ plt.vlines(df['X'], df['Y_hat'], df['Y'], color='r') plt.show() ``` -The Ordinary Least Squares (OLS) method chooses $\alpha$ and $\beta$ in such a way that **minimizes** the sum of the squared residuals (SSR). +普通最小二乘方法 (OLS) 选择 $\alpha$ 和 $\beta$,以使残差平方和 (SSR) **最小化**。 $$ \min_{\alpha,\beta} \sum_{i=1}^{N}{\hat{e}_i^2} = \min_{\alpha,\beta} \sum_{i=1}^{N}{(y_i - \alpha - \beta x_i)^2} $$ -Let's call this a cost function +我们称之为成本函数 $$ C = \sum_{i=1}^{N}{(y_i - \alpha - \beta x_i)^2} $$ -that we would like to minimize with parameters $\alpha$ and $\beta$. +我们希望通过参数 $\alpha$ 和 $\beta$ 来最小化这个成本函数。 -## How does error change with respect to $\alpha$ and $\beta$ +## 错误相对于 $\alpha$ 和 $\beta$ 的变化 -Let us first look at how the total error changes with respect to $\beta$ (holding the intercept $\alpha$ constant) +首先让我们看看总误差相对于 $\beta$ 的变化(保持截距 $\alpha$ 不变) -We know from [the next section](slr:optimal-values) the optimal values for $\alpha$ and $\beta$ are: +我们从[下一节](slr:optimal-values)知道 $\alpha$ 和 $\beta$ 的最优值是: ```{code-cell} ipython3 β_optimal = 64.38 α_optimal = -14.72 ``` -We can then calculate the error for a range of $\beta$ values +我们可以计算一个范围内的 $\beta$ 值的错误 ```{code-cell} ipython3 errors = {} @@ -207,20 +212,20 @@ for β in np.arange(20,100,0.5): errors[β] = abs((α_optimal + β * df['X']) - df['Y']).sum() ``` -Plotting the error +绘制错误图 ```{code-cell} ipython3 --- mystnb: figure: - caption: "Plotting the error" + caption: "绘制错误图" name: plt-errors --- ax = pd.Series(errors).plot(xlabel='β', ylabel='error') plt.axvline(β_optimal, color='r'); ``` -Now let us vary $\alpha$ (holding $\beta$ constant) +现在我们改变 $\alpha$ (保持 $\beta$ 不变) ```{code-cell} ipython3 errors = {} @@ -228,13 +233,13 @@ for α in np.arange(-500,500,5): errors[α] = abs((α + β_optimal * df['X']) - df['Y']).sum() ``` -Plotting the error +绘制错误图 ```{code-cell} ipython3 --- mystnb: figure: - caption: "Plotting the error (2)" + caption: "绘制错误图 (2)" name: plt-errors-2 --- ax = pd.Series(errors).plot(xlabel='α', ylabel='error') @@ -242,136 +247,136 @@ plt.axvline(α_optimal, color='r'); ``` (slr:optimal-values)= -## Calculating optimal values +## 计算最优值 -Now let us use calculus to solve the optimization problem and compute the optimal values for $\alpha$ and $\beta$ to find the ordinary least squares solution. +现在让我们使用微积分来解决优化问题,并计算出 $\alpha$ 和 $\beta$ 的最优值,以找到普通最小二乘解。 -First taking the partial derivative with respect to $\alpha$ +首先对 $\alpha$ 取偏导 $$ \frac{\partial C}{\partial \alpha}[\sum_{i=1}^{N}{(y_i - \alpha - \beta x_i)^2}] $$ -and setting it equal to $0$ +并将其设为 $0$ $$ 0 = \sum_{i=1}^{N}{-2(y_i - \alpha - \beta x_i)} $$ -we can remove the constant $-2$ from the summation by dividing both sides by $-2$ +我们可以通过两边除以 $-2$ 来移除求和中的常数 $-2$ $$ 0 = \sum_{i=1}^{N}{(y_i - \alpha - \beta x_i)} $$ -Now we can split this equation up into the components +现在我们可以将这个方程分解为各个组成部分 $$ 0 = \sum_{i=1}^{N}{y_i} - \sum_{i=1}^{N}{\alpha} - \beta \sum_{i=1}^{N}{x_i} $$ -The middle term is a straight forward sum from $i=1,...N$ by a constant $\alpha$ +中间项是从 $i=1,...N$ 对常数 $\alpha$ 进行简单求和 $$ 0 = \sum_{i=1}^{N}{y_i} - N*\alpha - \beta \sum_{i=1}^{N}{x_i} $$ -and rearranging terms +并重新排列各项 $$ \alpha = \frac{\sum_{i=1}^{N}{y_i} - \beta \sum_{i=1}^{N}{x_i}}{N} $$ -We observe that both fractions resolve to the means $\bar{y_i}$ and $\bar{x_i}$ +我们观察到两个分数分别归结为均值 $\bar{y_i}$ 和 $\bar{x_i}$ $$ \alpha = \bar{y_i} - \beta\bar{x_i} $$ (eq:optimal-alpha) -Now let's take the partial derivative of the cost function $C$ with respect to $\beta$ +现在让我们对成本函数 $C$ 关于 $\beta$ 取偏导 $$ \frac{\partial C}{\partial \beta}[\sum_{i=1}^{N}{(y_i - \alpha - \beta x_i)^2}] $$ -and setting it equal to $0$ +并将其设为 $0$ $$ 0 = \sum_{i=1}^{N}{-2 x_i (y_i - \alpha - \beta x_i)} $$ -we can again take the constant outside of the summation and divide both sides by $-2$ +我们可以再次将常数从求和中取出,并将两边除以 $-2$ $$ 0 = \sum_{i=1}^{N}{x_i (y_i - \alpha - \beta x_i)} $$ -which becomes +这变成了 $$ 0 = \sum_{i=1}^{N}{(x_i y_i - \alpha x_i - \beta x_i^2)} $$ -now substituting for $\alpha$ +现在代入 $\alpha$ $$ 0 = \sum_{i=1}^{N}{(x_i y_i - (\bar{y_i} - \beta \bar{x_i}) x_i - \beta x_i^2)} $$ -and rearranging terms +并重新排列各项 $$ 0 = \sum_{i=1}^{N}{(x_i y_i - \bar{y_i} x_i - \beta \bar{x_i} x_i - \beta x_i^2)} $$ -This can be split into two summations +这可以被分成两个求和 $$ 0 = \sum_{i=1}^{N}(x_i y_i - \bar{y_i} x_i) + \beta \sum_{i=1}^{N}(\bar{x_i} x_i - x_i^2) $$ -and solving for $\beta$ yields +解$\beta$得到 $$ \beta = \frac{\sum_{i=1}^{N}(x_i y_i - \bar{y_i} x_i)}{\sum_{i=1}^{N}(x_i^2 - \bar{x_i} x_i)} $$ (eq:optimal-beta) -We can now use {eq}`eq:optimal-alpha` and {eq}`eq:optimal-beta` to calculate the optimal values for $\alpha$ and $\beta$ +我们现在可以使用{eq}`eq:optimal-alpha` 和 {eq}`eq:optimal-beta` 来计算$\alpha$和$\beta$的最优值 -Calculating $\beta$ +计算$\beta$ ```{code-cell} ipython3 -df = df[['X','Y']].copy() # Original Data +df = df[['X','Y']].copy() # 原始数据 -# Calculate the sample means +# 计算样本均值 x_bar = df['X'].mean() y_bar = df['Y'].mean() ``` -Now computing across the 10 observations and then summing the numerator and denominator +现在计算10个观察值,然后求和分子和分母 ```{code-cell} ipython3 -# Compute the Sums +# 计算求和 df['num'] = df['X'] * df['Y'] - y_bar * df['X'] df['den'] = pow(df['X'],2) - x_bar * df['X'] β = df['num'].sum() / df['den'].sum() print(β) ``` -Calculating $\alpha$ +计算$\alpha$ ```{code-cell} ipython3 α = y_bar - β * x_bar print(α) ``` -Now we can plot the OLS solution +现在我们可以绘制OLS解决方案 ```{code-cell} ipython3 --- mystnb: figure: - caption: "OLS line of best fit" + caption: "OLS最佳拟合线" name: plt-ols --- df['Y_hat'] = α + β * df['X'] @@ -386,31 +391,31 @@ plt.vlines(df['X'], df['Y_hat'], df['Y'], color='r'); :::{exercise} :label: slr-ex1 -Now that you know the equations that solve the simple linear regression model using OLS you can now run your own regressions to build a model between $y$ and $x$. +现在您已经知道了使用OLS解决简单线性回归模型的方程,您可以开始运行自己的回归以构建$y$和$x$之间的模型了。 -Let's consider two economic variables GDP per capita and Life Expectancy. +让我们考虑两个经济变量,人均GDP和预期寿命。 -1. What do you think their relationship would be? -2. Gather some data [from our world in data](https://ourworldindata.org) -3. Use `pandas` to import the `csv` formatted data and plot a few different countries of interest -4. Use {eq}`eq:optimal-alpha` and {eq}`eq:optimal-beta` to compute optimal values for $\alpha$ and $\beta$ -5. Plot the line of best fit found using OLS -6. Interpret the coefficients and write a summary sentence of the relationship between GDP per capita and Life Expectancy +1. 你认为它们之间的关系会是怎样的? +2. 从[我们的世界数据中](https://ourworldindata.org)搜集一些数据 +3. 使用`pandas`导入`csv`格式的数据,并绘制几个不同国家的图表 +4. 使用{eq}`eq:optimal-alpha` 和 {eq}`eq:optimal-beta`计算$\alpha$和$\beta$的最优值 +5. 使用OLS绘制最佳拟合线 +6. 解释系数并写出人均GDP和预期寿命之间关系的总结句子 ::: :::{solution-start} slr-ex1 ::: -**Q2:** Gather some data [from our world in data](https://ourworldindata.org) +**Q2:** 搜集一些数据 [来自我们的世界数据](https://ourworldindata.org) :::{raw} html ::: -You can download {download}`a copy of the data here ` if you get stuck +如果你遇到困难,可以从这里下载{download}`数据副本 ` -**Q3:** Use `pandas` to import the `csv` formatted data and plot a few different countries of interest +**Q3:** 使用`pandas`导入`csv`格式的数据并绘制几个不同国家的兴趣图表 ```{code-cell} ipython3 data_url = "https://github.com/QuantEcon/lecture-python-intro/raw/main/lectures/_static/lecture_specific/simple_linear_regression/life-expectancy-vs-gdp-per-capita.csv" @@ -421,13 +426,13 @@ df = pd.read_csv(data_url, nrows=10) df ``` -You can see that the data downloaded from Our World in Data has provided a global set of countries with the GDP per capita and Life Expectancy Data. +您可以看到从我们的世界数据下载的数据为全球各国提供了人均GDP和预期寿命数据。 -It is often a good idea to at first import a few lines of data from a csv to understand its structure so that you can then choose the columns that you want to read into your DataFrame. +首先从csv文件中导入几行数据以了解其结构,以便您可以选择要读取到DataFrame中的列,这通常是一个好主意。 -You can observe that there are a bunch of columns we won't need to import such as `Continent` +您可以观察到有许多我们不需要导入的列,比如`Continent` -So let's built a list of the columns we want to import +那么我们来构建一个我们想要导入的列的列表 ```{code-cell} ipython3 cols = ['Code', 'Year', 'Life expectancy at birth (historical)', 'GDP per capita'] @@ -435,14 +440,14 @@ df = pd.read_csv(data_url, usecols=cols) df ``` -Sometimes it can be useful to rename your columns to make it easier to work with in the DataFrame +有时候重命名列名可以使得在DataFrame中更容易操作 ```{code-cell} ipython3 df.columns = ["cntry", "year", "life_expectancy", "gdppc"] df ``` -We can see there are `NaN` values which represents missing data so let us go ahead and drop those +我们可以看到存在`NaN`值,这表示缺失数据,所以让我们继续删除这些数据 ```{code-cell} ipython3 df.dropna(inplace=True) @@ -452,69 +457,69 @@ df.dropna(inplace=True) df ``` -We have now dropped the number of rows in our DataFrame from 62156 to 12445 removing a lot of empty data relationships. +我们现在已经将我们的DataFrame的行数从62156减少到12445,删除了很多空的数据关系。 -Now we have a dataset containing life expectancy and GDP per capita for a range of years. +现在我们有一个包含一系列年份的人均寿命和人均GDP的数据集。 -It is always a good idea to spend a bit of time understanding what data you actually have. +花点时间了解你实际拥有的数据总是一个好主意。 -For example, you may want to explore this data to see if there is consistent reporting for all countries across years +例如,您可能想要探索这些数据,看看是否所有国家在各年之间的报告都是一致的。 -Let's first look at the Life Expectancy Data +让我们首先看看寿命数据 ```{code-cell} ipython3 le_years = df[['cntry', 'year', 'life_expectancy']].set_index(['cntry', 'year']).unstack()['life_expectancy'] le_years ``` -As you can see there are a lot of countries where data is not available for the Year 1543! +如您所见,有很多国家在1543年的数据是不可用的! -Which country does report this data? +哪个国家报告了这些数据? ```{code-cell} ipython3 le_years[~le_years[1543].isna()] ``` -You can see that Great Britain (GBR) is the only one available +您可以看到,只有大不列颠(GBR)是可用的 -You can also take a closer look at the time series to find that it is also non-continuous, even for GBR. +您还可以更仔细地观察时间序列,发现即使对于GBR,它也是不连续的。 ```{code-cell} ipython3 le_years.loc['GBR'].plot() ``` -In fact we can use pandas to quickly check how many countries are captured in each year +实际上我们可以使用pandas快速检查每个年份涵盖了多少个国家 ```{code-cell} ipython3 le_years.stack().unstack(level=0).count(axis=1).plot(xlabel="Year", ylabel="Number of countries"); ``` -So it is clear that if you are doing cross-sectional comparisons then more recent data will include a wider set of countries +所以很明显,如果你进行横断面比较,那么最近的数据将包括更广泛的国家集合 -Now let us consider the most recent year in the dataset 2018 +现在让我们考虑数据集中最近的一年2018 ```{code-cell} ipython3 df = df[df.year == 2018].reset_index(drop=True).copy() ``` ```{code-cell} ipython3 -df.plot(x='gdppc', y='life_expectancy', kind='scatter', xlabel="GDP per capita", ylabel="Life expectancy (years)",); +df.plot(x='gdppc', y='life_expectancy', kind='scatter', xlabel="GDP per capita", ylabel="Life expectancy (years)",); ``` -This data shows a couple of interesting relationships. +这些数据显示了一些有趣的关系。 -1. there are a number of countries with similar GDP per capita levels but a wide range in Life Expectancy -2. there appears to be a positive relationship between GDP per capita and life expectancy. Countries with higher GDP per capita tend to have higher life expectancy outcomes +1. 许多国家的人均GDP相近,但寿命差别很大 +2. 人均GDP与预期寿命之间似乎存在正向关系。人均GDP较高的国家往往拥有更高的预期寿命 -Even though OLS is solving linear equations -- one option we have is to transform the variables, such as through a log transform, and then use OLS to estimate the transformed variables. +尽管普通最小二乘法(OLS)是用来解线性方程的,但我们可以通过对变量进行转换(例如对数变换),然后使用OLS来估计转换后的变量。 -By specifying `logx` you can plot the GDP per Capita data on a log scale +通过指定 `logx` 你可以在对数尺度上绘制人均GDP数据 ```{code-cell} ipython3 -df.plot(x='gdppc', y='life_expectancy', kind='scatter', xlabel="GDP per capita", ylabel="Life expectancy (years)", logx=True); +df.plot(x='gdppc', y='life_expectancy', kind='scatter', xlabel="人均GDP", ylabel="预期寿命(年)", logx=True); ``` -As you can see from this transformation -- a linear model fits the shape of the data more closely. +从这次转换可以看出,线性模型更贴近数据的形状。 ```{code-cell} ipython3 df['log_gdppc'] = df['gdppc'].apply(np.log10) @@ -524,12 +529,12 @@ df['log_gdppc'] = df['gdppc'].apply(np.log10) df ``` -**Q4:** Use {eq}`eq:optimal-alpha` and {eq}`eq:optimal-beta` to compute optimal values for $\alpha$ and $\beta$ +**Q4:** 使用 {eq}`eq:optimal-alpha` 和 {eq}`eq:optimal-beta` 来计算 $\alpha$ 和 $\beta$ 的最优值 ```{code-cell} ipython3 -data = df[['log_gdppc', 'life_expectancy']].copy() # Get Data from DataFrame +data = df[['log_gdppc', 'life_expectancy']].copy() # 从DataFrame中提取数据 -# Calculate the sample means +# 计算样本均值 x_bar = data['log_gdppc'].mean() y_bar = data['life_expectancy'].mean() ``` @@ -539,7 +544,7 @@ data ``` ```{code-cell} ipython3 -# Compute the Sums +# 计算求和 data['num'] = data['log_gdppc'] * data['life_expectancy'] - y_bar * data['log_gdppc'] data['den'] = pow(data['log_gdppc'],2) - x_bar * data['log_gdppc'] β = data['num'].sum() / data['den'].sum() @@ -551,7 +556,7 @@ print(β) print(α) ``` -**Q5:** Plot the line of best fit found using OLS +**Q5:** 绘制使用 OLS 找到的最佳拟合线 ```{code-cell} ipython3 data['life_expectancy_hat'] = α + β * df['log_gdppc'] @@ -569,9 +574,9 @@ plt.vlines(data['log_gdppc'], data['life_expectancy_hat'], data['life_expectancy :::{exercise} :label: slr-ex2 -Minimizing the sum of squares is not the **only** way to generate the line of best fit. +通过最小化平方和并不是生成最佳拟合线的**唯一**方法。 -For example, we could also consider minimizing the sum of the **absolute values**, that would give less weight to outliers. +例如,我们还可以考虑最小化**绝对值之和**,这样对异常值的权重会更小。 -Solve for $\alpha$ and $\beta$ using the least absolute values -::: +求解 $\alpha$ 和 $\beta$ 使用最小绝对值法 +::: \ No newline at end of file diff --git a/lectures/solow.md b/lectures/solow.md index 0a5160b..740048c 100644 --- a/lectures/solow.md +++ b/lectures/solow.md @@ -10,76 +10,55 @@ kernelspec: --- (solow)= -# The Solow-Swan Growth Model +# 索洛-斯旺增长模型 -In this lecture we review a famous model due -to [Robert Solow (1925--2023)](https://en.wikipedia.org/wiki/Robert_Solow) and [Trevor Swan (1918--1989)](https://en.wikipedia.org/wiki/Trevor_Swan). +在本讲座中,我们将回顾一个由[罗伯特·索洛(1925--2023)](https://en.wikipedia.org/wiki/Robert_Solow)和[特雷弗·斯旺(1918--1989)](https://en.wikipedia.org/wiki/Trevor_Swan)提出的著名模型。 -The model is used to study growth over the long run. +这个模型用于研究长期经济增长。 -Although the model is simple, it contains some interesting lessons. +尽管模型简单,但它包含一些有趣的教训。 - -We will use the following imports. +我们将使用以下导入。 ```{code-cell} ipython3 import matplotlib.pyplot as plt import numpy as np ``` -## The model - -In a Solow--Swan economy, agents save a fixed fraction of their current -incomes. - -Savings sustain or increase the stock of capital. +## 模型 -Capital is combined with labor to produce output, which in turn is paid out to -workers and owners of capital. +在索洛-斯旺经济中,经济主体将其当前收入的固定比例用于储蓄。 +储蓄维持或增加资本存量。 +资本与劳动力相结合生产产出,产出又支付给工人和资本所有者。 +为了简化问题,我们忽略人口和生产力增长。 -To keep things simple, we ignore population and productivity growth. +对于每个整数 $t \geq 0$,第 $t$ 期的产出 $Y_t$ 由 $Y_t = F(K_t, L_t)$ 给出,其中 $K_t$ 是资本,$L_t$ 是劳动力,$F$ 是总生产函数。 -For each integer $t \geq 0$, output $Y_t$ in period $t$ is given by $Y_t = -F(K_t, L_t)$, where $K_t$ is capital, $L_t$ is labor and $F$ is an aggregate -production function. - -The function $F$ is assumed to be nonnegative and -**homogeneous of degree one**, meaning -that +假设函数 $F$ 是非负的,且是**一阶齐次的**,意味着 $$ F(\lambda K, \lambda L) = \lambda F(K, L) - \quad \text{for all } \lambda \geq 0 + \quad \text{对于所有 } \lambda \geq 0 $$ -Production functions with this property include - -* the **Cobb-Douglas** function $F(K, L) = A K^{\alpha} - L^{1-\alpha}$ with $0 \leq \alpha \leq 1$. -* the **CES** function $F(K, L) = \left\{ a K^\rho + b L^\rho \right\}^{1/\rho}$ - with $a, b, \rho > 0$. - -Here, $\alpha$ is the output elasticity of capital and $\rho$ is a parameter that determines the elasticity of substitution between capital and labor. - -We assume a closed economy, so aggregate domestic investment equals aggregate domestic -saving. +具有这种特性的生产函数包括: +* **科布-道格拉斯**函数 $F(K, L) = A K^{\alpha} L^{1-\alpha}$,其中 $0 \leq \alpha \leq 1$。 +* **CES**函数 $F(K, L) = \left\{ a K^\rho + b L^\rho \right\}^{1/\rho}$,其中 $a, b, \rho > 0$。 -The saving rate is a constant $s$ satisfying $0 \leq s \leq 1$, so that aggregate -investment and saving both equal $s Y_t$. +这里,$\alpha$ 是资本的产出弹性,$\rho$ 是决定资本和劳动力之间替代弹性的参数。 -Capital depreciates: without replenishing through investment, one unit of capital today -becomes $1-\delta$ units tomorrow. +我们假设是封闭经济,因此总国内投资等于总国内储蓄。 +储蓄率是一个常数 $s$,满足 $0 \leq s \leq 1$,所以总投资和储蓄都等于 $s Y_t$。 -Thus, +资本会贬值:如果不通过投资补充,今天的一单位资本明天会变成 $1-\delta$ 单位。 +因此, $$ K_{t+1} = s F(K_t, L_t) + (1 - \delta) K_t $$ - -Without population growth, $L_t$ equals some constant $L$. - -Setting $k_t := K_t / L$ and using homogeneity of degree one now yields +没有人口增长,$L_t$ 等于某个常数 $L$。 +令 $k_t := K_t / L$,并使用一阶齐次性,现在得到 $$ k_{t+1} @@ -88,8 +67,7 @@ $$ = s F(k_t, 1) + (1 - \delta) k_t $$ - -With $f(k) := F(k, 1)$, the final expression for capital dynamics is +令 $f(k) := F(k, 1)$,资本动态的最终表达式为 ```{math} :label: solow @@ -97,24 +75,19 @@ With $f(k) := F(k, 1)$, the final expression for capital dynamics is \text{ where } g(k) := s f(k) + (1 - \delta) k ``` -Our aim is to learn about the evolution of $k_t$ over time, -given an exogenous initial capital stock $k_0$. - - -## A graphical perspective +我们的目标是了解随时间变化的 $k_t$ 的演变,给定一个外生的初始资本存量 $k_0$。 -To understand the dynamics of the sequence $(k_t)_{t \geq 0}$ we use a 45-degree diagram. +## 图形化视角 -To do so, we first -need to specify the functional form for $f$ and assign values to the parameters. +为了理解序列 $(k_t)_{t \geq 0}$ 的动态,我们使用45度图。 -We choose the Cobb--Douglas specification $f(k) = A k^\alpha$ and set $A=2.0$, -$\alpha=0.3$, $s=0.3$ and $\delta=0.4$. +为此,我们首先需要为 $f$ 指定函数形式并为参数赋值。 -The function $g$ from {eq}`solow` is then plotted, along with the 45-degree line. +我们选择科布-道格拉斯规格 $f(k) = A k^\alpha$,并设定 $A=2.0$,$\alpha=0.3$,$s=0.3$ 和 $\delta=0.4$。 +然后绘制方程 {eq}`solow` 中的函数 $g$,以及45度线。 -Let's define the constants. +让我们定义这些常数。 ```{code-cell} ipython3 A, s, alpha, delta = 2, 0.3, 0.3, 0.4 @@ -122,14 +95,14 @@ x0 = 0.25 xmin, xmax = 0, 3 ``` -Now, we define the function $g$. +现在我们来定义函数$g$。 ```{code-cell} ipython3 def g(A, s, alpha, delta, k): return A * s * k**alpha + (1 - delta) * k ``` -Let's plot the 45-degree diagram of $g$. +让我们来绘制函数$g$的45度图。 ```{code-cell} ipython3 def plot45(kstar=None): @@ -176,30 +149,23 @@ def plot45(kstar=None): plot45() ``` -Suppose, at some $k_t$, the value $g(k_t)$ lies strictly above the 45-degree line. +假设在某个 $k_t$ 处,$g(k_t)$ 的值严格高于45度线。 +那么我们有 $k_{t+1} = g(k_t) > k_t$,人均资本增加。 +如果 $g(k_t) < k_t$,则人均资本下降。 +如果 $g(k_t) = k_t$,那么我们就处于**稳态**,$k_t$ 保持不变。 +(模型的{ref}`稳态`是映射 $g$ 的[不动点](https://en.wikipedia.org/wiki/Fixed_point_(mathematics))。) -Then we have $k_{t+1} = g(k_t) > k_t$ and capital per worker rises. - -If $g(k_t) < k_t$ then capital per worker falls. - -If $g(k_t) = k_t$, then we are at a **steady state** and $k_t$ remains constant. - -(A {ref}`steady state ` of the model is a [fixed point](https://en.wikipedia.org/wiki/Fixed_point_(mathematics)) of the mapping $g$.) - -From the shape of the function $g$ in the figure, we see that -there is a unique steady state in $(0, \infty)$. - -It solves $k = s Ak^{\alpha} + (1-\delta)k$ and hence is given by +从图中 $g$ 函数的形状可以看出,在 $(0, \infty)$ 内存在唯一的稳态。 +它满足方程 $k = s Ak^{\alpha} + (1-\delta)k$,因此可以表示为 ```{math} :label: kstarss k^* := \left( \frac{s A}{\delta} \right)^{1/(1 - \alpha)} ``` -If initial capital is below $k^*$, then capital increases over time. - -If initial capital is above this level, then the reverse is true. -Let's plot the 45-degree diagram to show the $k^*$ in the plot. +如果初始资本低于 $k^*$,那么资本会随时间增加。 +如果初始资本高于这个水平,则相反。 +让我们绘制45度图来在图中显示 $k^*$。 ```{code-cell} ipython3 kstar = ((s * A) / delta)**(1/(1 - alpha)) @@ -207,18 +173,11 @@ plot45(kstar) ``` -From our graphical analysis, it appears that $(k_t)$ converges to $k^*$, regardless of initial capital -$k_0$. - -This is a form of {ref}`global stability `. - - -The next figure shows three time paths for capital, from -three distinct initial conditions, under the parameterization listed above. - -At this parameterization, $k^* \approx 1.78$. - -Let's define the constants and three distinct initial conditions +根据我们的图形分析,似乎无论初始资本 $k_0$ 如何,$(k_t)$ 都会收敛到 $k^*$。 +这是一种{ref}`全局稳定性 `的形式。 +下图显示了在上述参数化条件下,从三个不同的初始条件出发的资本时间路径。 +在这种参数化下,$k^* \approx 1.78$。 +让我们定义常数和三个不同的初始条件 ```{code-cell} ipython3 A, s, alpha, delta = 2, 0.3, 0.3, 0.4 @@ -239,7 +198,7 @@ def simulate_ts(x0_values, ts_length): ts = np.zeros(ts_length) - # simulate and plot time series + # 模拟和绘制时间序列 for x_init in x0_values: ts[0] = x_init for t in range(1, ts_length): @@ -260,29 +219,23 @@ def simulate_ts(x0_values, ts_length): simulate_ts(x0, ts_length) ``` -As expected, the time paths in the figure all converge to $k^*$. +# 如预期,图中的所有时间路径都收敛于 $k^*$。 -## Growth in continuous time +## 连续时间的增长 -In this section, we investigate a continuous time version of the Solow--Swan -growth model. +在本节中,我们将研究 Solow--Swan 增长模型的连续时间版本。我们将看到连续时间提供的平滑如何能简化我们的分析。 -We will see how the smoothing provided by continuous time can -simplify our analysis. +回顾一下,资本的离散时间动态由以下公式给出:$k_{t+1} = s f(k_t) + (1 - \delta) k_t$。 - -Recall that the discrete time dynamics for capital are -given by $k_{t+1} = s f(k_t) + (1 - \delta) k_t$. - -A simple rearrangement gives the rate of change per unit of time: +简单重新排列可得到单位时间的变化率: $$ \Delta k_t = s f(k_t) - \delta k_t - \quad \text{where} \quad + \quad \text{其中} \quad \Delta k_t := k_{t+1} - k_t $$ -Taking the time step to zero gives the continuous time limit +将时间步长趋近于零得到连续时间极限 ```{math} :label: solowc @@ -291,38 +244,27 @@ Taking the time step to zero gives the continuous time limit k'_t := \frac{d}{dt} k_t ``` -Our aim is to learn about the evolution of $k_t$ over time, -given an initial stock $k_0$. +我们的目标是了解 $k_t$ 随时间的演变,给定初始存量 $k_0$。 -A **steady state** for {eq}`solowc` is a value $k^*$ -at which capital is unchanging, meaning $k'_t = 0$ or, equivalently, -$s f(k^*) = \delta k^*$. +{eq}`solowc` 的**稳态**是一个值 $k^*$,在该值处资本保持不变,即 $k'_t = 0$ 或等价地,$s f(k^*) = \delta k^*$。 -We assume -$f(k) = Ak^\alpha$, so $k^*$ solves -$s A k^\alpha = \delta k$. +我们假设 $f(k) = Ak^\alpha$,所以 $k^*$ 满足 $s A k^\alpha = \delta k$。 -The solution is the same as the discrete time case---see {eq}`kstarss`. +解决方案与离散时间情况相同——参见 {eq}`kstarss`。 -The dynamics are represented in -the next figure, maintaining the parameterization we used -above. +动态在下一个图中表示,保持我们上面使用的参数化。 -Writing $k'_t = g(k_t)$ with $g(k) = -s Ak^\alpha - \delta k$, values of $k$ with $g(k) > 0$ imply $k'_t > 0$, so -capital is increasing. +写作 $k'_t = g(k_t)$,其中 $g(k) = s Ak^\alpha - \delta k$,$g(k) > 0$ 的 $k$ 值意味着 $k'_t > 0$,所以资本在增加。 -When $g(k) < 0$, the opposite occurs. Once again, high marginal returns to -savings at low levels of capital combined with low rates of return at high -levels of capital combine to yield global stability. +当 $g(k) < 0$ 时,相反的情况发生。再一次,低水平资本时储蓄的高边际回报与高水平资本时的低回报率相结合,产生了全局稳定性。 -To see this in a figure, let's define the constants +为了在图中看到这一点,让我们定义以下常数 ```{code-cell} ipython3 A, s, alpha, delta = 2, 0.3, 0.3, 0.4 ``` -Next we define the function $g$ for growth in continuous time +接下来,我们为连续时间的增长定义函数 $g$ ```{code-cell} ipython3 def g_con(A, s, alpha, delta, k): @@ -368,28 +310,19 @@ kstar = ((s * A) / delta)**(1/(1 - alpha)) plot_gcon(kstar) ``` -This shows global stability heuristically for a fixed parameterization, but -how would we show the same thing formally for a continuum of plausible parameters? - -In the discrete time case, a neat expression for $k_t$ is hard to obtain. - -In continuous time the process is easier: we can obtain a relatively simple -expression for $k_t$ that specifies the entire path. - -The first step is -to set $x_t := k_t^{1-\alpha}$, so that $x'_t = (1-\alpha) k_t^{-\alpha} -k'_t$. - -Substituting into $k'_t = sAk_t^\alpha - \delta k_t$ leads to the -linear differential equation +这从启发式的角度展示了固定参数化的全局稳定性,但我们如何为一系列合理的参数形式地证明同样的结论呢? +在离散时间的情况下,很难得到$k_t$的简洁表达式。 +在连续时间中,这个过程更加简单:我们可以得到一个相对简单的$k_t$表达式,它可以指定整个路径。 +第一步是 +设$x_t := k_t^{1-\alpha}$,这样$x'_t = (1-\alpha) k_t^{-\alpha} k'_t$。 +将其代入$k'_t = sAk_t^\alpha - \delta k_t$,得到以下线性微分方程 ```{math} :label: xsolow x'_t = (1-\alpha) (sA - \delta x_t) ``` -This equation, which is a [linear ordinary differential equation](https://math.libretexts.org/Bookshelves/Calculus/Calculus_(Guichard)/17%3A_Differential_Equations/17.01%3A_First_Order_Differential_Equations), has the solution - +这个方程是一个[线性常微分方程](https://math.libretexts.org/Bookshelves/Calculus/Calculus_(Guichard)/17%3A_Differential_Equations/17.01%3A_First_Order_Differential_Equations),其解为 $$ x_t = \left( @@ -398,11 +331,8 @@ $$ \mathrm{e}^{-\delta (1-\alpha) t} + \frac{sA}{\delta} $$ - -(You can confirm that this function $x_t$ satisfies {eq}`xsolow` by -differentiating it with respect to $t$.) - -Converting back to $k_t$ yields +(你可以通过对$t$求导来确认这个函数$x_t$满足{eq}`xsolow`方程。) +转换回$k_t$得到 ```{math} :label: ssivs @@ -417,23 +347,17 @@ Converting back to $k_t$ yields \right]^{1/(1-\alpha)} ``` -Since $\delta > 0$ and $\alpha \in (0, 1)$, we see immediately that $k_t \to -k^*$ as $t \to \infty$ independent of $k_0$. - -Thus, global stability holds. - -## Exercises +由于 $\delta > 0$ 且 $\alpha \in (0, 1)$,我们立即可以看出,当 $t \to \infty$ 时,$k_t \to k^*$,且与 $k_0$ 无关。 +因此,全局稳定性成立。 +## 练习 ```{exercise} :label: solow_ex1 -Plot per capita consumption $c$ at the steady state, as a function of the savings rate $s$, where $0 \leq s \leq 1$. - -Use the Cobb--Douglas specification $f(k) = A k^\alpha$. - -Set $A=2.0, \alpha=0.3,$ and $\delta=0.5$ - -Also, find the approximate value of $s$ that maximizes the $c^*(s)$ and show it in the plot. +绘制稳态下人均消费 $c$ 作为储蓄率 $s$ 的函数图,其中 $0 \leq s \leq 1$。 +使用柯布-道格拉斯生产函数 $f(k) = A k^\alpha$。 +设定 $A=2.0, \alpha=0.3,$ 且 $\delta=0.5$。 +此外,找出大约能使 $c^*(s)$ 最大化的 $s$ 值,并在图中显示。 ``` @@ -441,8 +365,7 @@ Also, find the approximate value of $s$ that maximizes the $c^*(s)$ and show it :class: dropdown ``` -Steady state consumption at savings rate $s$ is given by - +在储蓄率 $s$ 下的稳态消费由以下公式给出: $$ c^*(s) = (1-s)f(k^*) = (1-s)A(k^*)^\alpha @@ -460,8 +383,9 @@ k_star = ((s_grid * A) / delta)**(1/(1 - alpha)) c_star = (1 - s_grid) * A * k_star ** alpha ``` -Let's find the value of $s$ that maximizes $c^*$ using [scipy.optimize.minimize_scalar](https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize_scalar.html#scipy.optimize.minimize_scalar). -We will use $-c^*(s)$ since `minimize_scalar` finds the minimum value. +让我们使用 [scipy.optimize.minimize_scalar](https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize_scalar.html#scipy.optimize.minimize_scalar) 来找出使 $c^*$ 最大化的 $s$ 值。 +我们将使用 $-c^*(s)$,因为 `minimize_scalar` 函数是用来寻找最小值的。 + ```{code-cell} ipython3 from scipy.optimize import minimize_scalar @@ -507,7 +431,7 @@ ax.legend() plt.show() ``` -One can also try to solve this mathematically by differentiating $c^*(s)$ and solve for $\frac{d}{ds}c^*(s)=0$ using [sympy](https://www.sympy.org/en/index.html). +也可以尝试用数学方法解决这个问题,即对 $c^*(s)$ 进行微分,并使用 [sympy](https://www.sympy.org/en/index.html) 求解 $\frac{d}{ds}c^*(s)=0$。 ```{code-cell} ipython3 from sympy import solve, Symbol @@ -519,15 +443,15 @@ k = ((s_symbol * A) / delta)**(1/(1 - alpha)) c = (1 - s_symbol) * A * k ** alpha ``` -Let's differentiate $c$ and solve using [sympy.solve](https://docs.sympy.org/latest/modules/solvers/solvers.html#sympy.solvers.solvers.solve) +让我们对 $c$ 进行微分,并使用 [sympy.solve](https://docs.sympy.org/latest/modules/solvers/solvers.html#sympy.solvers.solvers.solve) 求解。 ```{code-cell} ipython3 -# Solve using sympy +# 使用 sympy 求解 s_star = solve(c.diff())[0] print(f"s_star = {s_star}") ``` -Incidentally, the rate of savings which maximizes steady state level of per capita consumption is called the [Golden Rule savings rate](https://en.wikipedia.org/wiki/Golden_Rule_savings_rate). +顺便说一下,使人均消费的稳态水平最大化的储蓄率被称为[黄金法则储蓄率](https://en.wikipedia.org/wiki/Golden_Rule_savings_rate)。 ```{solution-end} ``` @@ -535,35 +459,24 @@ Incidentally, the rate of savings which maximizes steady state level of per capi ```{exercise-start} :label: solow_ex2 ``` -**Stochastic Productivity** - -To bring the Solow--Swan model closer to data, we need to think about handling -random fluctuations in aggregate quantities. +**随机生产率** -Among other things, this will -eliminate the unrealistic prediction that per-capita output $y_t = A -k^\alpha_t$ converges to a constant $y^* := A (k^*)^\alpha$. +为了使索洛-斯旺模型更贴近实际数据,我们需要考虑如何处理总量中的随机波动。 +这样做会带来多方面的影响,其中之一是消除了人均产出 $y_t = A k^\alpha_t$ 收敛到常数 $y^* := A (k^*)^\alpha$ 这一不切实际的预测。 +在接下来的讨论中,我们将转向离散时间模型。 -We shift to discrete time for the following discussion. - -One approach is to replace constant productivity with some -stochastic sequence $(A_t)_{t \geq 1}$. - -Dynamics are now +一种方法是用某个随机序列 $(A_t)_{t \geq 1}$ 替代常数生产率。 +现在的动态方程变为 ```{math} :label: solowran k_{t+1} = s A_{t+1} f(k_t) + (1 - \delta) k_t ``` -We suppose $f$ is Cobb--Douglas and $(A_t)$ is IID and lognormal. - -Now the long run convergence obtained in the deterministic case breaks -down, since the system is hit with new shocks at each point in time. - -Consider $A=2.0, s=0.6, \alpha=0.3,$ and $\delta=0.5$ - -Generate and plot the time series $k_t$. +我们假设 $f$ 是柯布-道格拉斯生产函数,且 $(A_t)$ 是独立同分布的对数正态分布。 +现在,在确定性情况下获得的长期收敛性不再成立,因为系统在每个时间点都会受到新的冲击。 +考虑 $A=2.0, s=0.6, \alpha=0.3,$ 且 $\delta=0.5$。 +生成并绘制时间序列 $k_t$ 的图表。 ```{exercise-end} ``` @@ -572,20 +485,20 @@ Generate and plot the time series $k_t$. :class: dropdown ``` -Let's define the constants for lognormal distribution and initial values used for simulation +让我们定义用于模拟的对数正态分布的常数和初始值 ```{code-cell} ipython3 -# Define the constants +# 定义一下常数 sig = 0.2 mu = np.log(2) - sig**2 / 2 A = 2.0 s = 0.6 alpha = 0.3 delta = 0.5 -x0 = [.25, 3.25] # list of initial values used for simulation +x0 = [.25, 3.25] # 用于模拟的初始值列表 ``` -Let's define the function *k_next* to find the next value of $k$ +让我们定义函数 *k_next* 来找出 $k$ 的下一个值 ```{code-cell} ipython3 def lgnorm(): @@ -600,7 +513,7 @@ def ts_plot(x_values, ts_length): fig, ax = plt.subplots(figsize=[11, 5]) ts = np.zeros(ts_length) - # simulate and plot time series + # 模拟并且绘制时间序列 for x_init in x_values: ts[0] = x_init for t in range(1, ts_length): diff --git a/lectures/status.md b/lectures/status.md index 3ada25f..4bd2066 100644 --- a/lectures/status.md +++ b/lectures/status.md @@ -9,24 +9,24 @@ kernelspec: name: python3 --- -# Execution Statistics +# 执行统计数据 -This table contains the latest execution statistics. +此表包含最新的执行统计数据。 ```{nb-exec-table} ``` (status:machine-details)= -These lectures are built on `linux` instances through `github actions`. +这些讲座是通过 `github actions` 在 `linux` 实例上构建的。 -These lectures are using the following python version +这些讲座使用以下的python版本 ```{code-cell} ipython !python --version ``` -and the following package versions +和以下的包版本 ```{code-cell} ipython :tags: [hide-output] diff --git a/lectures/supply_demand_heterogeneity.md b/lectures/supply_demand_heterogeneity.md index 347294b..b2b3110 100644 --- a/lectures/supply_demand_heterogeneity.md +++ b/lectures/supply_demand_heterogeneity.md @@ -12,88 +12,87 @@ kernelspec: --- (supply_demand_heterogeneity)= -# Market Equilibrium with Heterogeneity +# 市场均衡与异质性 -## Overview +## 概述 -In the {doc}`previous lecture -`, we studied competitive equilibria in an economy with many goods. +在{doc}`前一讲座`中,我们研究了具有多种商品的经济环境中的竞争均衡。 -While the results of the study were informative, we used a strong simplifying assumption: all of the agents in the economy are identical. +虽然研究结果具有启发性,但我们使用了一个强化的简化假设:经济中的所有代理人都是相同的。 -In the real world, households, firms and other economic agents differ from one another along many dimensions. +实际上,家庭、公司及其他经济主体在许多方面都不相同。 -In this lecture, we introduce heterogeneity across consumers by allowing their preferences and endowments to differ. +在这次讲座中,我们通过允许消费者的偏好和禀赋不同,引入了消费者间的异质性。 -We will examine competitive equilibrium in this setting. +我们将在这种设定下检验竞争均衡。 -We will also show how a "representative consumer" can be constructed. +我们还将展示如何构建一个“代表性消费者”。 -Here are some imports: +以下是一些导入内容: ```{code-cell} ipython3 import numpy as np from scipy.linalg import inv ``` -## An simple example +## 一个简单的例子 -Let's study a simple example of **pure exchange** economy without production. +让我们学习一个没有生产的**纯交换**经济的简单例子。 -There are two consumers who differ in their endowment vectors $e_i$ and their bliss-point vectors $b_i$ for $i=1,2$. +有两个消费者,他们在禀赋向量 $e_i$ 和极乐点向量 $b_i$ 上有所不同,其中 $i=1,2$。 -The total endowment is $e_1 + e_2$. +总的禀赋是 $e_1 + e_2$。 -A competitive equilibrium requires that +竞争均衡要求 $$ c_1 + c_2 = e_1 + e_2 $$ -Assume the demand curves +假设需求曲线为 $$ - c_i = (\Pi^\top \Pi )^{-1}(\Pi^\top b_i - \mu_i p ) + c_i = (\Pi^{\top} \Pi)^{-1}(\Pi^{\top} b_i - \mu_i p ) $$ -Competitive equilibrium then requires that +那么竞争均衡还需要满足 $$ e_1 + e_2 = - (\Pi^\top \Pi)^{-1}(\Pi^\top (b_1 + b_2) - (\mu_1 + \mu_2) p ) + (\Pi^{\top} \Pi)^{-1}(\Pi^{\top} (b_1 + b_2) - (\mu_1 + \mu_2) p ) $$ -which, after a line or two of linear algebra, implies that +通过一两行线性代数计算,可以推出 $$ -(\mu_1 + \mu_2) p = \Pi^\top(b_1+ b_2) - \Pi^\top \Pi (e_1 + e_2) +(\mu_1 + \mu_2) p = \Pi^{\top}(b_1+ b_2) - \Pi^{\top} \Pi (e_1 + e_2) $$ (eq:old6) -We can normalize prices by setting $\mu_1 + \mu_2 =1$ and then solving +我们可以通过设定 $\mu_1 + \mu_2 =1$ 来规范价格,然后解方程 $$ -\mu_i(p,e) = \frac{p^\top (\Pi^{-1} b_i - e_i)}{p^\top (\Pi^\top \Pi )^{-1} p} +\mu_i(p,e) = \frac{p^{\top} (\Pi^{-1} b_i - e_i)}{p^{\top} (\Pi^{\top} \Pi)^{-1} p} $$ (eq:old7) -for $\mu_i, i = 1,2$. +求得 $\mu_i, i = 1,2$。 ```{exercise-start} :label: sdh_ex1 ``` -Show that, up to normalization by a positive scalar, the same competitive equilibrium price vector that you computed in the preceding two-consumer economy would prevail in a single-consumer economy in which a single **representative consumer** has utility function +证明,在正标量的归一化下,你在前面两消费者经济中计算出的同一竞争均衡价格向量,在只有一个代表性消费者的单一消费者经济中依旧适用,其中这个**代表性消费者**的效用函数为: $$ --.5 (\Pi c -b) ^\top (\Pi c -b ) +-.5 (\Pi c -b) ^2 (\Pi c -b ) $$ -and endowment vector $e$, where +其初始禀赋向量为 $e$,其中 $$ b = b_1 + b_2 $$ -and +以及 $$ e = e_1 + e_2 . @@ -102,68 +101,65 @@ $$ ```{exercise-end} ``` -## Pure exchange economy +## 纯交换经济 -Let's further explore a pure exchange economy with $n$ goods and $m$ people. +让我们进一步探讨一个有 $n$ 种商品和 $m$ 个人的纯交换经济。 -### Competitive equilibrium +### 竞争均衡 -We'll compute a competitive equilibrium. +我们将计算一个竞争均衡。 -To compute a competitive equilibrium of a pure exchange economy, we use the fact that +为了计算纯交换经济的竞争均衡,我们使用以下事实: -- Relative prices in a competitive equilibrium are the same as those in a special single person or representative consumer economy with preference $\Pi$ and $b=\sum_i b_i$, and endowment $e = \sum_i e_{i}$. +- 竞争均衡中的相对价格与在特定单一人或代表性消费者经济中的相对价格相同,其中偏好为 $\Pi$,$b=\sum_i b_i$,并且禀赋 $e = \sum_i e_{i}$。 -We can use the following steps to compute a competitive equilibrium: +我们可以使用以下步骤来计算竞争均衡: -- First we solve the single representative consumer economy by normalizing $\mu = 1$. Then, we renormalize the price vector by using the first consumption good as a numeraire. +- 首先我们通过规范化 $\mu = 1$ 来解决单一代表性消费者经济问题。然后,我们使用第一个消费品作为计价单位来重新规范化价格向量。 -- Next we use the competitive equilibrium prices to compute each consumer's marginal utility of wealth: +- 接下来我们使用竞争均衡价格来计算每个消费者的财富边际效用: $$ -\mu_{i}=\frac{-W_{i}+p^{\top}\left(\Pi^{-1}b_{i}-e_{i}\right)}{p^{\top}(\Pi^{\top}\Pi)^{-1}p}$$ - -- Finally we compute a competitive equilibrium allocation by using the demand curves: - -$$ -c_{i}=\Pi^{-1}b_{i}-(\Pi^{\top}\Pi)^{-1}\mu_{i}p +\mu_{i}=\frac{-W_{i}+p^{T}\left(\Pi^{-1}b_{i}-e_{i}\right)}{p^{T}(\Pi^{T}\Pi)^{-1}p} $$ +- 最后我们使用需求曲线来计算竞争均衡分配: -### Designing some Python code - - -Below we shall construct a Python class with the following attributes: - - * **Preferences** in the form of +$$ +c_{i}=\Pi^{-1}b_{i}-(\Pi^{T}\Pi)^{-1}\mu_{i}p +$$ - * an $n \times n$ positive definite matrix $\Pi$ - * an $n \times 1$ vector of bliss points $b$ +### 设计一些 Python 代码 - * **Endowments** in the form of +以下我们将构建一个 Python 类,具有以下属性: - * an $n \times 1$ vector $e$ - * a scalar "wealth" $W$ with default value $0$ + * **偏好** 以以下形式: + + * 一个 $n \times n$ 的正定矩阵 $\Pi$ + * 一个 $n \times 1$ 的幸福点向量 $b$ + * **禀赋** 以以下形式: + + * 一个 $n \times 1$ 的向量 $e$ + * 一个默认值为 $0$ 的标量 "财富" $W$ -The class will include a test to make sure that $b \gg \Pi e $ and raise an exception if it is violated -(at some threshold level we'd have to specify). +这个类将包括一个测试,确保 $b \gg \Pi e $,如果违反了这一规则(在我们需要指定的某一阈值水平上),则会引发一个异常。 - * **A Person** in the form of a pair that consists of + * **一个人** 以以下形式: - * **Preferences** and **Endowments** + * **偏好** 和 **禀赋** - * **A Pure Exchange Economy** will consist of + * **纯交换经济** 将包括: - * a collection of $m$ **persons** + * $m$ 个 **人** 的集合 - * $m=1$ for our single-agent economy - * $m=2$ for our illustrations of a pure exchange economy + * $m=1$ 对于我们的单代理经济 + * $m=2$ 对于我们描绘纯交换经济的示例 - * an equilibrium price vector $p$ (normalized somehow) - * an equilibrium allocation $c_1, c_2, \ldots, c_m$ -- a collection of $m$ vectors of dimension $n \times 1$ + * 一个平衡价格向量 $p$(以某种方式标准化) + * 一个平衡配置 $c_1, c_2, \ldots, c_m$ -- $m$ 个维度为 $n \times 1$ 的向量的集合 -Now let's proceed to code. +现在让我们开始编码。 ```{code-cell} ipython3 class ExchangeEconomy: @@ -174,48 +170,48 @@ class ExchangeEconomy: Ws=None, thres=1.5): """ - Set up the environment for an exchange economy - - Args: - Π (np.array): shared matrix of substitution - bs (list): all consumers' bliss points - es (list): all consumers' endowments - Ws (list): all consumers' wealth - thres (float): a threshold set to test b >> Pi e violated + 为交换经济设置环境 + + 参数: + Π (np.array): 共用的替代矩阵 + bs (list): 所有消费者的幸福点 + es (list): 所有消费者的初始物资 + Ws (list): 所有消费者的财富 + thres (float): 设定一个阈值来测试 b >> Pi e 是否被违反 """ n, m = Π.shape[0], len(bs) - # check non-satiation + # 检查不饱和条件 for b, e in zip(bs, es): if np.min(b / np.max(Π @ e)) <= thres: - raise Exception('set bliss points further away') + raise Exception('设置更远的幸福点') if Ws == None: Ws = np.zeros(m) else: if sum(Ws) != 0: - raise Exception('invalid wealth distribution') + raise Exception('无效的财富分配') self.Π, self.bs, self.es, self.Ws, self.n, self.m = Π, bs, es, Ws, n, m def competitive_equilibrium(self): """ - Compute the competitive equilibrium prices and allocation + 计算竞争均衡的价格和分配 """ Π, bs, es, Ws = self.Π, self.bs, self.es, self.Ws n, m = self.n, self.m slope_dc = inv(Π.T @ Π) Π_inv = inv(Π) - # aggregate + # 合计 b = sum(bs) e = sum(es) - # compute price vector with mu=1 and renormalize + # 以 mu=1 计算价格向量并重新标准化 p = Π.T @ b - Π.T @ Π @ e p = p / p[0] - # compute marginal utility of wealth + # 计算财富的边际效用 μ_s = [] c_s = [] A = p.T @ slope_dc @ p @@ -228,100 +224,100 @@ class ExchangeEconomy: for c_i in c_s: if any(c_i < 0): - print('allocation: ', c_s) - raise Exception('negative allocation: equilibrium does not exist') + print('分配: ', c_s) + raise Exception('负分配:不存在平衡') return p, c_s, μ_s ``` -## Implementation +## 实施 -Next we use the class ``ExchangeEconomy`` defined above to study +接下来我们使用上面定义的类 ``ExchangeEconomy`` 来研究 -* a two-person economy without production, -* a dynamic economy, and -* an economy with risk and arrow securities. +* 一个没有生产的两人经济体, +* 一个动态经济体, 和 +* 一个有风险和箭头证券的经济体。 -### Two-person economy without production +### 没有生产的两人经济体 -Here we study how competitive equilibrium $p, c_1, c_2$ respond to different $b_i$ and $e_i$, $i \in \{1, 2\}$. +在这里我们研究竞争均衡 $p, c_1, c_2$ 如何响应不同的 $b_i$ 和 $e_i$, $i \in \{1, 2\}$。 ```{code-cell} ipython3 Π = np.array([[1, 0], [0, 1]]) -bs = [np.array([5, 5]), # first consumer's bliss points - np.array([5, 5])] # second consumer's bliss points +bs = [np.array([5, 5]), # 第一个消费者的幸福点 + np.array([5, 5])] # 第二个消费者的幸福点 -es = [np.array([0, 2]), # first consumer's endowment - np.array([2, 0])] # second consumer's endowment +es = [np.array([0, 2]), # 第一个消费者的禀赋 + np.array([2, 0])] # 第二个消费者的禀赋 EE = ExchangeEconomy(Π, bs, es) p, c_s, μ_s = EE.competitive_equilibrium() -print('Competitive equilibrium price vector:', p) -print('Competitive equilibrium allocation:', c_s) +print('竞争均衡价格向量:', p) +print('竞争均衡配置:', c_s) ``` -What happens if the first consumer likes the first good more and the second consumer likes the second good more? +如果第一个消费者更喜欢第一种商品,而第二个消费者更喜欢第二种商品会怎样? ```{code-cell} ipython3 -EE.bs = [np.array([6, 5]), # first consumer's bliss points - np.array([5, 6])] # second consumer's bliss points +EE.bs = [np.array([6, 5]), # 第一个消费者的幸福点 + np.array([5, 6])] # 第二个消费者的幸福点 p, c_s, μ_s = EE.competitive_equilibrium() -print('Competitive equilibrium price vector:', p) -print('Competitive equilibrium allocation:', c_s) +print('竞争均衡价格向量:', p) +print('竞争均衡配置:', c_s) ``` -Let the first consumer be poorer. +让第一个消费者变得更穷。 ```{code-cell} ipython3 -EE.es = [np.array([0.5, 0.5]), # first consumer's endowment - np.array([1, 1])] # second consumer's endowment +EE.es = [np.array([0.5, 0.5]), # 第一个消费者的禀赋 + np.array([1, 1])] # 第二个消费者的禀赋 p, c_s, μ_s = EE.competitive_equilibrium() -print('Competitive equilibrium price vector:', p) -print('Competitive equilibrium allocation:', c_s) +print('竞争均衡价格向量:', p) +print('竞争均衡配置:', c_s) ``` -Now let's construct an autarky (i.e., no-trade) equilibrium. +现在让我们构建一个自给自足(即无贸易)均衡。 ```{code-cell} ipython3 -EE.bs = [np.array([4, 6]), # first consumer's bliss points - np.array([6, 4])] # second consumer's bliss points +EE.bs = [np.array([4, 6]), # 第一个消费者的幸福点 + np.array([6, 4])] # 第二个消费者的幸福点 -EE.es = [np.array([0, 2]), # first consumer's endowment - np.array([2, 0])] # second consumer's endowment +EE.es = [np.array([0, 2]), # 第一个消费者的禀赋 + np.array([2, 0])] # 第二个消费者的禀赋 p, c_s, μ_s = EE.competitive_equilibrium() -print('Competitive equilibrium price vector:', p) -print('Competitive equilibrium allocation:', c_s) +print('竞争均衡价格向量:', p) +print('竞争均衡配置:', c_s) ``` -Now let's redistribute endowments before trade. +现在让我们在贸易之前重新分配禀赋。 ```{code-cell} ipython3 -bs = [np.array([5, 5]), # first consumer's bliss points - np.array([5, 5])] # second consumer's bliss points +bs = [np.array([5, 5]), # 第一个消费者的幸福点 + np.array([5, 5])] # 第二个消费者的幸福点 -es = [np.array([1, 1]), # first consumer's endowment - np.array([1, 1])] # second consumer's endowment +es = [np.array([1, 1]), # 第一个消费者的禀赋 + np.array([1, 1])] # 第二个消费者的禀赋 Ws = [0.5, -0.5] EE_new = ExchangeEconomy(Π, bs, es, Ws) p, c_s, μ_s = EE_new.competitive_equilibrium() -print('Competitive equilibrium price vector:', p) -print('Competitive equilibrium allocation:', c_s) +print('竞争均衡价格向量:', p) +print('竞争均衡配置:', c_s) ``` -### A dynamic economy +### 动态经济 -Now let's use the tricks described above to study a dynamic economy, one with two periods. +现在我们利用上述技巧来研究一个动态经济,这里涉及两个时期。 ```{code-cell} ipython3 beta = 0.95 @@ -336,13 +332,13 @@ es = [np.array([1, 1])] EE_DE = ExchangeEconomy(Π, bs, es) p, c_s, μ_s = EE_DE.competitive_equilibrium() -print('Competitive equilibrium price vector:', p) -print('Competitive equilibrium allocation:', c_s) +print('竞争均衡的价格向量:', p) +print('竞争均衡配置:', c_s) ``` -### Risk economy with arrow securities +### 带箭头证券的风险经济 -We use the tricks described above to interpret $c_1, c_2$ as "Arrow securities" that are state-contingent claims to consumption goods. +我们使用上述技巧来解释 $c_1, c_2$ 为“箭头证券”,这些证券是对消费品的状态或情况下的索赔。 ```{code-cell} ipython3 prob = 0.7 @@ -359,80 +355,79 @@ es = [np.array([1, 0]), EE_AS = ExchangeEconomy(Π, bs, es) p, c_s, μ_s = EE_AS.competitive_equilibrium() -print('Competitive equilibrium price vector:', p) -print('Competitive equilibrium allocation:', c_s) +print('竞争均衡价格向量:', p) +print('竞争均衡配置:', c_s) ``` -## Deducing a representative consumer +## 推导代表性消费者 -In the class of multiple consumer economies that we are studying here, it turns out that there -exists a single **representative consumer** whose preferences and endowments can be deduced from lists of preferences and endowments for separate individual consumers. +在我们这里研究的多消费者经济类中,事实上存在一个单一的**代表性消费者**,其偏好和禀赋可以从单独个体消费者的偏好和禀赋列表中推断出来。 -Consider a multiple consumer economy with initial distribution of wealth $W_i$ satisfying $\sum_i W_{i}=0$ +考虑一个具有初始财富分配 $W_i$ 的多消费者经济,满足 $\sum_i W_{i}=0$ -We allow an initial redistribution of wealth. +我们允许初始财富的重新分配。 -We have the following objects +我们有以下对象 -- The demand curve: +- 需求曲线: $$ -c_{i}=\Pi^{-1}b_{i}-(\Pi^{\top}\Pi)^{-1}\mu_{i}p +c_{i}=\Pi^{-1}b_{i}-(\Pi^T\Pi)^{-1}\mu_{i}p $$ -- The marginal utility of wealth: +- 财富的边际效用: $$ -\mu_{i}=\frac{-W_{i}+p^{\top}\left(\Pi^{-1}b_{i}-e_{i}\right)}{p^{\top}(\Pi^{\top}\Pi)^{-1}p} +\mu_{i}=\frac{-W_{i}+p^T\left(\Pi^{-1}b_{i}-e_{i}\right)}{p^T(\Pi^T\Pi)^{-1}p} $$ -- Market clearing: +- 市场清算: $$ \sum c_{i}=\sum e_{i} $$ -Denote aggregate consumption $\sum_i c_{i}=c$ and $\sum_i \mu_i = \mu$. +表示总消费 $\sum_i c_{i}=c$ 和 $\sum_i \mu_i = \mu$. -Market clearing requires +市场清算需要 $$ -\Pi^{-1}\left(\sum_{i}b_{i}\right)-(\Pi^{\top}\Pi)^{-1}p\left(\sum_{i}\mu_{i}\right)=\sum_{i}e_{i} +\Pi^{-1}\left(\sum_{i}b_{i}\right)-(\Pi^{T}\Pi)^{-1}p\left(\sum_{i}\mu_{i}\right)=\sum_{i}e_{i} $$ -which, after a few steps, leads to + +这经过几步计算后得到 $$ -p=\mu^{-1}\left(\Pi^{\top}b-\Pi^{\top}\Pi e\right) +p=\mu^{-1}\left(\Pi^{T}b-\Pi^{T}\Pi e\right) $$ -where +其中 $$ -\mu = \sum_i\mu_{i}=\frac{0 + p^{\top}\left(\Pi^{-1}b-e\right)}{p^{\top}(\Pi^{\top}\Pi)^{-1}p}. +\mu = \sum_i\mu_{i}=\frac{0 + p^{T}\left(\Pi^{-1}b-e\right)}{p^{T}(\Pi^{T}\Pi)^{-1}p}. $$ -Now consider the representative consumer economy specified above. +现在考虑上述指定的代表性消费者经济。 -Denote the marginal utility of wealth of the representative consumer by $\tilde{\mu}$. +用 $\tilde{\mu}$ 表示代表性消费者的财富边际效用。 -The demand function is +需求函数是 $$ -c=\Pi^{-1}b-(\Pi^{\top}\Pi)^{-1}\tilde{\mu} p +c=\Pi^{-1}b-(\Pi^{T}\Pi)^{-1}\tilde{\mu} p $$ -Substituting this into the budget constraint gives +将这个代入预算约束得到 $$ -\tilde{\mu}=\frac{p^{\top}\left(\Pi^{-1}b-e\right)}{p^{\top}(\Pi^{\top}\Pi)^{-1}p} +\tilde{\mu}=\frac{p^{T}\left(\Pi^{-1}b-e\right)}{p^{T}(\Pi^{T}\Pi)^{-1}p} $$ -In an equilibrium $c=e$, so +在平衡状态下 $c=e$,所以 $$ -p=\tilde{\mu}^{-1}(\Pi^{\top}b-\Pi^{\top}\Pi e) +p=\tilde{\mu}^{-1}(\Pi^{T}b-\Pi^{T}\Pi e) $$ -Thus, we have verified that, up to the choice of a numeraire in which to express absolute prices, the price -vector in our representative consumer economy is the same as that in an underlying economy with multiple consumers. +因此,我们已经验证了,直到选择一个基准物来表达绝对价格为止,我们代表性消费者经济中的价格向量与具有多个消费者的基本经济中的价格向量相同。 \ No newline at end of file diff --git a/lectures/supply_demand_multiple_goods.md b/lectures/supply_demand_multiple_goods.md index 5a010ce..6b5d5d6 100644 --- a/lectures/supply_demand_multiple_goods.md +++ b/lectures/supply_demand_multiple_goods.md @@ -12,33 +12,31 @@ kernelspec: --- (supply_demand_multiple_goods)= -# Supply and Demand with Many Goods +# 多种商品的供需关系 -## Overview +## 概览 -In a {doc}`previous lecture ` we studied supply, demand -and welfare in a market with a single consumption good. +在{doc}`previous lecture `中我们研究了一个只有单一消费商品市场的供给、需求和福利。 -In this lecture, we study a setting with $n$ goods and $n$ corresponding prices. +在这个讲座中,我们将研究有$n$种商品和$n$对应价格的情景。 -Key infrastructure concepts that we'll encounter in this lecture are +我们将在这次讲座中遇到的关键基础概念包括: -* inverse demand curves -* marginal utilities of wealth -* inverse supply curves -* consumer surplus -* producer surplus -* social welfare as a sum of consumer and producer surpluses -* competitive equilibrium +* 反向需求曲线 +* 财富的边际效用 +* 反向供给曲线 +* 消费者剩余 +* 生产者剩余 +* 社会福利作为消费者剩余和生产者剩余之和 +* 竞争均衡 - -We will provide a version of the [first fundamental welfare theorem](https://en.wikipedia.org/wiki/Fundamental_theorems_of_welfare_economics), which was formulated by +我们将提供[第一福利基本定理](https://en.wikipedia.org/wiki/Fundamental_theorems_of_welfare_economics)的一个版本,这个定理是由下列经济学家提出的: * [Leon Walras](https://en.wikipedia.org/wiki/L%C3%A9on_Walras) * [Francis Ysidro Edgeworth](https://en.wikipedia.org/wiki/Francis_Ysidro_Edgeworth) * [Vilfredo Pareto](https://en.wikipedia.org/wiki/Vilfredo_Pareto) -Important extensions to the key ideas were obtained by +对这些关键想法的重要扩展是由以下学者获得的: * [Abba Lerner](https://en.wikipedia.org/wiki/Abba_P._Lerner) * [Harold Hotelling](https://en.wikipedia.org/wiki/Harold_Hotelling) @@ -46,31 +44,34 @@ Important extensions to the key ideas were obtained by * [Kenneth Arrow](https://en.wikipedia.org/wiki/Kenneth_Arrow) * [Gerard Debreu](https://en.wikipedia.org/wiki/G%C3%A9rard_Debreu) +我们将描述两个经典的福利定理: -We shall describe two classic welfare theorems: - -* **first welfare theorem:** for a given distribution of wealth among consumers, a competitive equilibrium allocation of goods solves a social planning problem. +* **第一福利定理:** 对于消费者之间给定的财富分配,竞争均衡下的商品分配方案解决了一个社会规划问题。 +* **第二福利定理:** 解决社会规划问题的商品分配可以通过适当初始财富分配的竞争均衡来支持。 -* **second welfare theorem:** An allocation of goods to consumers that solves a social planning problem can be supported by a competitive equilibrium with an appropriate initial distribution of wealth. - -As usual, we start by importing some Python modules. +如往常一样,我们首先导入一些Python模块。 ```{code-cell} ipython3 -# import some packages +# 导入一些包 import numpy as np +import matplotlib as mpl import matplotlib.pyplot as plt from scipy.linalg import inv + +FONTPATH = "fonts/SourceHanSerifSC-SemiBold.otf" +mpl.font_manager.fontManager.addfont(FONTPATH) +plt.rcParams['font.family'] = ['Source Han Serif SC'] ``` -## Formulas from linear algebra +## 来自线性代数的公式 -We shall apply formulas from linear algebra that +我们将应用线性代数中的公式,这些公式: -* differentiate an inner product with respect to each vector -* differentiate a product of a matrix and a vector with respect to the vector -* differentiate a quadratic form in a vector with respect to the vector +* 将内积关于每个向量进行区分 +* 将矩阵与向量的乘积关于向量进行区分 +* 将向量的二次型关于向量进行区分 -Where $a$ is an $n \times 1$ vector, $A$ is an $n \times n$ matrix, and $x$ is an $n \times 1$ vector: +这里 $a$ 为一个 $n \times 1$ 向量,$A$ 为一个 $n \times n$ 矩阵, 且 $x$ 为一个 $n \times 1$ 向量: $$ \frac{\partial a^\top x }{\partial x} = \frac{\partial x^\top a }{\partial x} = a @@ -84,166 +85,165 @@ $$ \frac{\partial x^\top A x}{\partial x} = (A + A^\top)x $$ -## From utility function to demand curve +## 从效用函数到需求曲线 -Our study of consumers will use the following primitives +我们对消费者的研究将使用以下基本元素: -* $\Pi$ be an $m \times n$ matrix, -* $b$ be an $m \times 1$ vector of bliss points, -* $e$ be an $n \times 1$ vector of endowments, and +* $\Pi$ 是一个 $m \times n$ 矩阵, +* $b$ 是一个 $m \times 1$ 的幸福点向量, +* $e$ 是一个 $n \times 1$ 的禀赋向量, +++ -We will analyze endogenous objects $c$ and $p$, where +我们将分析内生对象 $c$ 和 $p$,其中: -* $c$ is an $n \times 1$ vector of consumptions of various goods, -* $p$ is an $n \times 1$ vector of prices +* $c$ 是一个 $n \times 1$ 各种商品的消费向量, +* $p$ 是一个 $n \times 1$ 价格向量 +++ -The matrix $\Pi$ describes a consumer's willingness to substitute one good for every other good. +矩阵 $\Pi$ 描述了消费者愿意如何替换一个商品以换取其他每一个商品。 -We assume that $\Pi$ has linearly independent columns, which implies that $\Pi^\top \Pi$ is a positive definite matrix. +我们假设 $\Pi$ 的列线性独立,这意味着 $\Pi^\top \Pi$ 是一个正定矩阵。 -* it follows that $\Pi^\top \Pi$ has an inverse. +* 这意味着 $\Pi^\top \Pi$ 有一个逆矩阵。 -We shall see below that $(\Pi^\top \Pi)^{-1}$ is a matrix of slopes of (compensated) demand curves for $c$ with respect to a vector of prices: +我们将在下面看到,$(\Pi^\top \Pi)^{-1}$ 是一个对价格向量来说的 $c$ 的(补偿的)需求曲线斜率矩阵: $$ \frac{\partial c } {\partial p} = (\Pi^\top \Pi)^{-1} $$ -A consumer faces $p$ as a price taker and chooses $c$ to maximize the utility function +消费者面对价格 $p$ 作为价格接受者,并选择 $c$ 来最大化效用函数 $$ - - \frac{1}{2} (\Pi c -b) ^\top (\Pi c -b ) + -\frac{1}{2} (\Pi c -b)^\top (\Pi c -b) $$ (eq:old0) -subject to the budget constraint +受到预算约束的限制 $$ - p^\top (c -e ) = 0 + p^\top (c -e) = 0 $$ (eq:old2) -We shall specify examples in which $\Pi$ and $b$ are such that it typically happens that +我们将指定一些例子,其中 $\Pi$ 和 $b$ 是这样的,通常会发生 $$ \Pi c \ll b $$ (eq:bversusc) -This means that the consumer has much less of each good than he wants. +这意味着消费者得到的每种商品都远少于他希望的数量。 -The deviation in {eq}`eq:bversusc` will ultimately assure us that competitive equilibrium prices are positive. +在 {eq}`eq:bversusc` 中的偏差最终将保证我们竞争性均衡价格是正的。 +++ -### Demand curve implied by constrained utility maximization +### 被约束的效用最大化所暗示的需求曲线 -For now, we assume that the budget constraint is {eq}`eq:old2`. +目前,我们假设预算约束是 {eq}`eq:old2`。 -So we'll be deriving what is known as a **Marshallian** demand curve. +因此,我们将导出所谓的**马歇尔**需求曲线。 -Our aim is to maximize [](eq:old0) subject to [](eq:old2). +我们的目标是在约束 [](eq:old2) 下最大化 [](eq:old0)。 -Form a Lagrangian +构建一个拉格朗日方法 -$$ L = - \frac{1}{2} (\Pi c -b)^\top (\Pi c -b ) + \mu [p^\top (e-c)] $$ +$$ L = - \frac{1}{2} (\Pi c -b)^T (\Pi c -b ) + \mu [p^T (e-c)] $$ -where $\mu$ is a Lagrange multiplier that is often called a **marginal utility of wealth**. +其中 $\mu$ 是一个拉格朗日乘数,通常称为**财富的边际效用**。 -The consumer chooses $c$ to maximize $L$ and $\mu$ to minimize it. +消费者选择 $c$ 来最大化 $L$,选 $\mu$ 来最小化它。 -First-order conditions for $c$ are +$c$ 的一阶条件是 $$ \frac{\partial L} {\partial c} - = - \Pi^\top \Pi c + \Pi^\top b - \mu p = 0 + = - \Pi^T \Pi c + \Pi^T b - \mu p = 0 $$ -so that, given $\mu$, the consumer chooses +因此,给定 $\mu$,消费者选择 $$ - c = (\Pi^\top \Pi )^{-1}(\Pi^\top b - \mu p ) + c = (\Pi^T \Pi )^{-1}(\Pi^T b - \mu p ) $$ (eq:old3) -Substituting {eq}`eq:old3` into budget constraint {eq}`eq:old2` and solving for $\mu$ gives +将 {eq}`eq:old3` 代入预算约束 {eq}`eq:old2` 并解出 $\mu$ 得 $$ - \mu(p,e) = \frac{p^\top ( \Pi^\top \Pi )^{-1} \Pi^\top b - p^\top e}{p^\top (\Pi^\top \Pi )^{-1} p}. + \mu(p,e) = \frac{p^T ( \Pi^T \Pi )^{-1} \Pi^T b - p^T e}{p^T (\Pi^T \Pi )^{-1} p}. $$ (eq:old4) -Equation {eq}`eq:old4` tells how marginal utility of wealth depends on the endowment vector $e$ and the price vector $p$. +方程 {eq}`eq:old4` 描述了财富的边际效用如何依赖于禀赋向量 $e$ 和价格向量 $p$。 ```{note} -Equation {eq}`eq:old4` is a consequence of imposing that $p^\top (c - e) = 0$. +方程 {eq}`eq:old4` 是基于约束 $p^T (c - e) = 0$ 得到的结果。 -We could instead take $\mu$ as a parameter and use {eq}`eq:old3` and the budget constraint {eq}`eq:old2p` to solve for wealth. +我们也可以将 $\mu$ 视为一个参数,并使用 {eq}`eq:old3` 和预算约束 {eq}`eq:old2p` 来解算财富。 -Which way we proceed determines whether we are constructing a **Marshallian** or **Hicksian** demand curve. +我们如何进行取决于我们是构建**马歇尔**需求曲线还是**希克斯**需求曲线。 ``` -## Endowment economy +## 禀赋经济 -We now study a pure-exchange economy, or what is sometimes called an endowment economy. +我们现在研究一个纯交换经济,或者有时被称为禀赋经济。 -Consider a single-consumer, multiple-goods economy without production. +考虑一个单一消费者,多种商品的经济体,没有生产。 -The only source of goods is the single consumer's endowment vector $e$. +商品的唯一来源是单一消费者的禀赋向量 $e$。 -A competitive equilibrium price vector induces the consumer to choose $c=e$. +一个竞争性均衡价格向量促使消费者选择 $c=e$。 -This implies that the equilibrium price vector satisfies +这意味着均衡价格向量满足 $$ -p = \mu^{-1} (\Pi^\top b - \Pi^\top \Pi e) +p = \mu^{-1} (\Pi^ T b - \Pi^ T \Pi e) $$ -In the present case where we have imposed budget constraint in the form {eq}`eq:old2`, we are free to normalize the price vector by setting the marginal utility of wealth $\mu =1$ (or any other value for that matter). +在我们施加了形式为 {eq}`eq:old2` 的预算约束的当前情况下,我们可以通过设置财富边际效用 $\mu =1$(或其他任何值)来规范化价格向量。 -This amounts to choosing a common unit (or numeraire) in which prices of all goods are expressed. +这相当于选择一个通用单位(或计价单位),在该单位中表达所有货物的价格。 -(Doubling all prices will affect neither quantities nor relative prices.) +(价格翻倍既不会影响数量也不会影响相对价格。) -We'll set $\mu=1$. +我们将设置 $\mu=1$。 ```{exercise} :label: sdm_ex1 -Verify that setting $\mu=1$ in {eq}`eq:old3` implies that formula {eq}`eq:old4` is satisfied. +验证在 {eq}`eq:old3` 中设置 $\mu=1$ 意味着满足公式 {eq}`eq:old4`。 ``` ```{exercise} :label: sdm_ex2 -Verify that setting $\mu=2$ in {eq}`eq:old3` also implies that formula -{eq}`eq:old4` is satisfied. +验证在 {eq}`eq:old3` 中设置 $\mu=2` 也意味着满足公式 {eq}`eq:old4`。 ``` -Here is a class that computes competitive equilibria for our economy. +以下是一个计算我们经济体竞争性均衡的类。 ```{code-cell} ipython3 class ExchangeEconomy: - def __init__(self, - Π, + def __init__(self, + Π, b, e, thres=1.5): """ - Set up the environment for an exchange economy + 设置交换经济的环境 - Args: - Π (np.array): shared matrix of substitution - b (list): the consumer's bliss point - e (list): the consumer's endowment - thres (float): a threshold to check p >> Π e condition + 参数: + Π (np.array): 替代共享矩阵 + b (list): 消费者的极乐点 + e (list): 消费者的禀赋 + thres (float): 检查 p >> Π e 条件的阈值 """ - # check non-satiation + # 检查非饱和性 if np.min(b / np.max(Π @ e)) <= thres: - raise Exception('set bliss points further away') + raise Exception('将极乐点设置得更远一些') self.Π, self.b, self.e = Π, b, e @@ -251,80 +251,80 @@ class ExchangeEconomy: def competitive_equilibrium(self): """ - Compute the competitive equilibrium prices and allocation + 计算竞争性均衡价格和配置 """ Π, b, e = self.Π, self.b, self.e - # compute price vector with μ=1 + # 计算价格向量,μ=1 p = Π.T @ b - Π.T @ Π @ e - # compute consumption vector + # 计算消费向量 slope_dc = inv(Π.T @ Π) Π_inv = inv(Π) c = Π_inv @ b - slope_dc @ p if any(c < 0): print('allocation: ', c) - raise Exception('negative allocation: equilibrium does not exist') + raise Exception('负配给:均衡不存在') return p, c ``` -## Digression: Marshallian and Hicksian demand curves +## 题外话:马歇尔需求曲线与希克斯需求曲线 -Sometimes we'll use budget constraint {eq}`eq:old2` in situations in which a consumer's endowment vector $e$ is his **only** source of income. +有时我们会在消费者的禀赋向量 $e$ 是其**唯一**收入来源的情况下使用预算限制 {eq}`eq:old2`。 -Other times we'll instead assume that the consumer has another source of income (positive or negative) and write his budget constraint as +其他时候,我们假设消费者有另一来源的收入(正或负),并把他的预算限制写作 $$ -p ^\top (c -e ) = w +p ^\top (c -e) = w $$ (eq:old2p) -where $w$ is measured in "dollars" (or some other **numeraire**) and component $p_i$ of the price vector is measured in dollars per unit of good $i$. +其中 $w$ 以“美元”(或其他**计价单位**)计量,价格向量的组成 $p_i$ 则以每单位商品 $i$ 的美元计量。 -Whether the consumer's budget constraint is {eq}`eq:old2` or {eq}`eq:old2p` and whether we take $w$ as a free parameter or instead as an endogenous variable will affect the consumer's marginal utility of wealth. +无论消费者的预算限制是 {eq}`eq:old2` 还是 {eq}`eq:old2p`,以及我们将 $w$ 视为一个自由参数还是一个内生变量,都会影响消费者的财富边际效用。 -Consequently, how we set $\mu$ determines whether we are constructing +因此,我们如何设置 $\mu$ 决定我们是在构建 -* a **Marshallian** demand curve, as when we use {eq}`eq:old2` and solve for $\mu$ using equation {eq}`eq:old4` above, or -* a **Hicksian** demand curve, as when we treat $\mu$ as a fixed parameter and solve for $w$ from {eq}`eq:old2p`. +* **马歇尔** 需求曲线,如当我们使用 {eq}`eq:old2` 并使用上述方程 {eq}`eq:old4` 解 $\mu$ 时,或 +* **希克斯** 需求曲线,如当我们将 $\mu$ 视为一个固定参数并从 {eq}`eq:old2p` 解 $w$ 时。 -Marshallian and Hicksian demand curves contemplate different mental experiments: +马歇尔和希克斯需求曲线考虑的是不同的心理实验: -For a Marshallian demand curve, hypothetical changes in a price vector have both **substitution** and **income** effects +对于马歇尔需求曲线,假设的价格向量变化有**替代**效应和**收入**效应 -* income effects are consequences of changes in $p^\top e$ associated with the change in the price vector +* 收入效应是价格向量变化所引起的 $p^\top e$ 变动的后果 -For a Hicksian demand curve, hypothetical price vector changes have only **substitution** effects +对于希克斯需求曲线,假设的价格向量变化只有**替代**效应 -* changes in the price vector leave the $p^\top e + w$ unaltered because we freeze $\mu$ and solve for $w$ +* 价格向量的变化不会改变 $p^\top e + w$,因为我们固定 $\mu$ 并解出 $w$ -Sometimes a Hicksian demand curve is called a **compensated** demand curve in order to emphasize that, to disarm the income (or wealth) effect associated with a price change, the consumer's wealth $w$ is adjusted. +有时希克斯需求曲线被称为**补偿**需求曲线,以强调为了消除与价格变化相关的收入(或财富)效应,消费者的财富 $w$ 被调整。 -We'll discuss these distinct demand curves more below. +接下来我们将更多地讨论这些不同的需求曲线。 +++ -## Dynamics and risk as special cases +## 动态与风险作为特殊情况 -Special cases of our $n$-good pure exchange model can be created to represent +通过在不同商品上标注不同日期,或者将商品的交付解释为取决于状态的世界(这些状态的实现由*已知概率分布*描述),我们可以创建$n$物品纯交换模型的特殊案例来代表: -* **dynamics** --- by putting different dates on different commodities -* **risk** --- by interpreting delivery of goods as being contingent on states of the world whose realizations are described by a *known probability distribution* +* **动态** —— 通过将不同的商品放在不同的日期 +* **风险** —— 通过解释商品的交付依赖于世界的状态,这些状态的实现由一个已知的概率分布描述 -Let's illustrate how. +让我们来说明一下。 -### Dynamics +### 动态 -Suppose that we want to represent a utility function +假设我们想要表示一个效用函数 $$ - \frac{1}{2} [(c_1 - b_1)^2 + \beta (c_2 - b_2)^2] $$ -where $\beta \in (0,1)$ is a discount factor, $c_1$ is consumption at time $1$ and $c_2$ is consumption at time 2. +这里 $\beta \in (0,1)$ 是一个贴现因子,$c_1$ 是时间1的消费,$c_2$ 是时间2的消费。 -To capture this with our quadratic utility function {eq}`eq:old0`, set +要用我们的二次效用函数{eq}`eq:old0`来表示这一点,设置 $$ \Pi = \begin{bmatrix} 1 & 0 \cr @@ -335,34 +335,33 @@ $$ e = \begin{bmatrix} e_1 \cr e_2 \end{bmatrix} $$ -and +以及 $$ -b = \begin{bmatrix} b_1 \cr \sqrt{\beta} b_2 -\end{bmatrix} +b = \begin{bmatrix} b_1 \cr \sqrt{\beta} b_2 \end{bmatrix} $$ -The budget constraint {eq}`eq:old2` becomes +预算约束{eq}`eq:old2`变为 $$ p_1 c_1 + p_2 c_2 = p_1 e_1 + p_2 e_2 $$ -The left side is the **discounted present value** of consumption. +左侧是消费的**贴现现值**。 -The right side is the **discounted present value** of the consumer's endowment. +右侧是消费者禀赋的**贴现现值**。 -The relative price $\frac{p_1}{p_2}$ has units of time $2$ goods per unit of time $1$ goods. +相对价格 $\frac{p_1}{p_2}$ 的单位是时间2商品每时间1商品单位。 -Consequently, +因此, $$ (1+r) := R := \frac{p_1}{p_2} $$ -is the **gross interest rate** and $r$ is the **net interest rate**. +是**毛利率**,$r$ 是**净利率**。 -Here is an example. +这里是一个例子。 ```{code-cell} ipython3 beta = 0.95 @@ -381,32 +380,32 @@ print('Competitive equilibrium price vector:', p) print('Competitive equilibrium allocation:', c) ``` -### Risk and state-contingent claims +### 风险与状态相关权益 -We study risk in the context of a **static** environment, meaning that there is only one period. +我们在**静态**环境中研究风险,这意味着只有一个时期。 -By **risk** we mean that an outcome is not known in advance, but that it is governed by a known probability distribution. +所谓**风险**,是指结果事先未知,但受已知概率分布的制约。 -As an example, our consumer confronts **risk** means in particular that +例如,我们的消费者面对的**风险**特别体现在: - * there are two states of nature, $1$ and $2$. + * 有两种自然状态,$1$ 和 $2$。 - * the consumer knows that the probability that state $1$ occurs is $\lambda$. + * 消费者知道状态 $1$ 发生的概率是 $\lambda$。 - * the consumer knows that the probability that state $2$ occurs is $(1-\lambda)$. + * 消费者知道状态 $2$ 发生的概率是 $(1-\lambda)$。 -Before the outcome is realized, the consumer's **expected utility** is +在结果实现之前,消费者的**预期效用**是 $$ -- \frac{1}{2} [\lambda (c_1 - b_1)^2 + (1-\lambda)(c_2 - b_2)^2] +-\frac{1}{2} [\lambda (c_1 - b_1)^2 + (1-\lambda)(c_2 - b_2)^2] $$ -where +其中: -* $c_1$ is consumption in state $1$ -* $c_2$ is consumption in state $2$ +* $c_1$ 是状态 $1$ 中的消费 +* $c_2$ 是状态 $2$ 中的消费 -To capture these preferences we set +为了描述这些偏好,我们设置 $$ \Pi = \begin{bmatrix} \sqrt{\lambda} & 0 \cr @@ -423,33 +422,33 @@ $$ b = \begin{bmatrix} \sqrt{\lambda}b_1 \cr \sqrt{1-\lambda}b_2 \end{bmatrix} $$ -A consumer's endowment vector is +消费者的禀赋向量是 $$ c = \begin{bmatrix} c_1 \cr c_2 \end{bmatrix} $$ -A price vector is +一个价格向量是 $$ p = \begin{bmatrix} p_1 \cr p_2 \end{bmatrix} $$ -where $p_i$ is the price of one unit of consumption in state $i \in \{1, 2\}$. +其中 $p_i$ 是状态 $i \in \{1, 2\}$ 下的一单位消费的价格。 -The state-contingent goods being traded are often called **Arrow securities**. +交易中的状态相关商品常被称为**箭头证券**。 -Before the random state of the world $i$ is realized, the consumer sells his/her state-contingent endowment bundle and purchases a state-contingent consumption bundle. +在世界的随机状态 $i$ 实现之前,消费者卖掉他/她的状态相关的禀赋组合,并购买一个状态相关的消费组合。 -Trading such state-contingent goods is one way economists often model **insurance**. +交易此类状态相关商品是经济学家常用来模拟**保险**的方式之一。 +++ -We use the tricks described above to interpret $c_1, c_2$ as "Arrow securities" that are state-contingent claims to consumption goods. +我们使用上述技巧来解释 $c_1, c_2$ 作为“箭证券”,它们是对消费品的状态条件索赔。 +++ -Here is an instance of the risk economy: +这里是一个风险经济的实例: ```{code-cell} ipython3 prob = 0.2 @@ -464,22 +463,22 @@ e = np.array([1, 1]) risk = ExchangeEconomy(Π, b, e) p, c = risk.competitive_equilibrium() -print('Competitive equilibrium price vector:', p) -print('Competitive equilibrium allocation:', c) +print('竞争均衡价格向量:', p) +print('竞争均衡分配:', c) ``` ```{exercise} :label: sdm_ex3 -Consider the instance above. +考虑上面的实例。 -Please numerically study how each of the following cases affects the equilibrium prices and allocations: +请数值研究以下每种情况如何影响均衡价格和分配: -* the consumer gets poorer, -* they like the first good more, or -* the probability that state $1$ occurs is higher. +* 消费者变得更穷, +* 他们更喜欢第一种商品,或者 +* 状态 $1$ 发生的概率更高。 -Hints. For each case choose some parameter $e, b, \text{ or } \lambda$ different from the instance. +提示:对于每种情况,选择与实例不同的一些参数 $e, b, \text{或} \lambda$。 ``` @@ -489,30 +488,30 @@ Hints. For each case choose some parameter $e, b, \text{ or } \lambda$ different :class: dropdown ``` -First consider when the consumer is poorer. +首先考虑当消费者更穷时。 -Here we just decrease the endowment. +这里我们只是减少了禀赋。 ```{code-cell} ipython3 risk.e = np.array([0.5, 0.5]) p, c = risk.competitive_equilibrium() -print('Competitive equilibrium price vector:', p) -print('Competitive equilibrium allocation:', c) +print('竞争均衡价格向量:', p) +print('竞争均衡分配:', c) ``` -If the consumer likes the first (or second) good more, then we can set a larger bliss value for good 1. +如果消费者更喜欢第一(或第二)种商品,那么我们可以为商品1设置更大的极乐值。 ```{code-cell} ipython3 risk.b = np.array([np.sqrt(prob) * 6, np.sqrt(1 - prob) * 5]) p, c = risk.competitive_equilibrium() -print('Competitive equilibrium price vector:', p) -print('Competitive equilibrium allocation:', c) +print('竞争均衡价格向量:', p) +print('竞争均衡分配:', c) ``` -Increase the probability that state $1$ occurs. +增加状态 $1$ 发生的概率。 ```{code-cell} ipython3 prob = 0.8 @@ -527,8 +526,8 @@ e = np.array([1, 1]) risk = ExchangeEconomy(Π, b, e) p, c = risk.competitive_equilibrium() -print('Competitive equilibrium price vector:', p) -print('Competitive equilibrium allocation:', c) +print('竞争均衡价格向量:', p) +print('竞争均衡分配:', c) ``` ```{solution-end} @@ -536,120 +535,109 @@ print('Competitive equilibrium allocation:', c) +++ -## Economies with endogenous supplies of goods +## 具有内生供应的经济体 -Up to now we have described a pure exchange economy in which endowments of goods are exogenous, meaning that they are taken as given from outside the model. +迄今为止,我们描述的是一个纯粹的交换经济,其中商品的禀赋是外生的,意味着它们是从模型外部给定的。 -### Supply curve of a competitive firm +### 竞争性公司的供应曲线 -A competitive firm that can produce goods takes a price vector $p$ as given and chooses a quantity $q$ -to maximize total revenue minus total costs. +能够生产商品的竞争公司将价格向量 $p$ 视为既定,并选择数量 $q$ +以最大化总收入减去总成本。 -The firm's total revenue equals $p^\top q$ and its total cost equals $C(q)$ where $C(q)$ is a total cost function +公司的总收入等于 $p^T q$,其总成本等于 $C(q)$,其中 $C(q)$ 是总成本函数 $$ -C(q) = h ^\top q + \frac{1}{2} q^\top J q +C(q) = h^T q + \frac{1}{2} q^T J q $$ +其中 $J$ 是一个正定矩阵。 -and $J$ is a positive definite matrix. - - -So the firm's profits are +因此公司的利润是 $$ -p^\top q - C(q) +p^T q - C(q) $$ (eq:compprofits) - - -An $n\times 1$ vector of **marginal costs** is +一个 $n \times 1$ 向量的**边际成本**是 $$ \frac{\partial C(q)}{\partial q} = h + H q $$ -where +其中 $$ -H = \frac{1}{2} (J + J^\top) +H = \frac{1}{2} (J + J^T) $$ -The firm maximizes total profits by setting **marginal revenue to marginal costs**. +公司通过将**边际收入等于边际成本**来最大化总利润。 -An $n \times 1$ vector of marginal revenues for the price-taking firm is $\frac{\partial p^\top q} -{\partial q} = p $. +对于收取定价的公司,一个 $n \times 1$ 向量的边际收入是 $\frac{\partial p^T q} +{\partial q} = p $。 -So **price equals marginal revenue** for our price-taking competitive firm. - -This leads to the following **inverse supply curve** for the competitive firm: +因此对于我们这个收取定价的竞争公司 **价格等于边际收入**。 +这导致以下竞争公司的**反向供应曲线**: $$ p = h + H q $$ +### 竞争均衡 +为了计算一个以需求曲线由财富边际效用 $\mu$ 确定的生产经济的竞争均衡,我们首先通过解决规划问题来计算分配。 +然后,我们使用逆需求曲线或供应曲线计算均衡价格向量。 -### Competitive equilibrium - +#### $\mu=1$ 的热身 -To compute a competitive equilibrium for a production economy where demand curve is pinned down by the marginal utility of wealth $\mu$, we first compute an allocation by solving a planning problem. +作为一个特殊情况,通过设置财富的边际效用 $\mu =1$ 来确定一个需求曲线。 -Then we compute the equilibrium price vector using the inverse demand or supply curve. - -#### $\mu=1$ warmup - -As a special case, let's pin down a demand curve by setting the marginal utility of wealth $\mu =1$. - -Equating supply price to demand price and letting $q=c$ we get +使供应价格等于需求价格并让 $q=c$,我们得到 $$ -p = h + H c = \Pi^\top b - \Pi^\top \Pi c , +p = h + H c = \Pi^{\top} b - \Pi^{\top} \Pi c, $$ -which implies the equilibrium quantity vector +这意味着均衡数量向量 $$ -c = (\Pi^\top \Pi + H )^{-1} ( \Pi^\top b - h) +c = (\Pi^{\top} \Pi + H )^{-1} ( \Pi^{\top} b - h) $$ (eq:old5) -This equation is the counterpart of equilibrium quantity {eq}`eq:old1` for the scalar $n=1$ model with which we began. +这个方程是我们开始时的标量 $n=1$ 模型均衡数量 {eq}`eq:old1` 的对应物。 -#### General $\mu\neq 1$ case +#### 通用 $\mu \neq 1$ 情况 -Now let's extend the preceding analysis to a more -general case by allowing $\mu \neq 1$. +现在让我们通过允许 $\mu \neq 1$ 来扩展之前的分析。 -Then the inverse demand curve is +然后,逆需求曲线为 $$ -p = \mu^{-1} [\Pi^\top b - \Pi^\top \Pi c] +p = \mu^{-1} [\Pi^{\top} b - \Pi^{\top} \Pi c] $$ (eq:old5pa) -Equating this to the inverse supply curve, letting $q=c$ and solving -for $c$ gives +使这个等于逆供应曲线,让 $q=c$ 并解出 $c$ 得 $$ -c = [\Pi^\top \Pi + \mu H]^{-1} [ \Pi^\top b - \mu h] +c = [\Pi^{\top} \Pi + \mu H]^{-1} [ \Pi^{\top} b - \mu h] $$ (eq:old5p) +++ -### Implementation +### 实现 -A Production Economy will consist of +生产型经济将包括: -* a single **person** that we'll interpret as a representative consumer -* a single set of **production costs** -* a multiplier $\mu$ that weights "consumers" versus "producers" in a planner's welfare function, as described above in the main text -* an $n \times 1$ vector $p$ of competitive equilibrium prices -* an $n \times 1$ vector $c$ of competitive equilibrium quantities -* **consumer surplus** -* **producer surplus** +* 一个我们将解释为代表性消费者的**个人** +* 一组**生产成本** +* 一个加权“消费者”与“生产者”在规划者福利函数中的乘数 $\mu$,如上文所述 +* 一个 $n \times 1$ 向量 $p$,代表竞争均衡价格 +* 一个 $n \times 1$ 向量 $c$,代表竞争均衡数量 +* **消费者剩余** +* **生产者剩余** -Here we define a class ``ProductionEconomy``. +这里我们定义一个类 `ProductionEconomy`。 ```{code-cell} ipython3 class ProductionEconomy: @@ -661,82 +649,82 @@ class ProductionEconomy: J, μ): """ - Set up the environment for a production economy - - Args: - Π (np.ndarray): matrix of substitution - b (np.array): bliss points - h (np.array): h in cost func - J (np.ndarray): J in cost func - μ (float): welfare weight of the corresponding planning problem + 设置生产型经济环境 + + 参数: + Π (np.ndarray): 替代矩阵 + b (np.array): 极乐点 + h (np.array): 成本函数中的 h + J (np.ndarray): 成本函数中的 J + μ (float): 对应规划问题的福利权重 """ self.n = len(b) self.Π, self.b, self.h, self.J, self.μ = Π, b, h, J, μ def competitive_equilibrium(self): """ - Compute a competitive equilibrium of the production economy + 计算生产型经济的竞争均衡 """ Π, b, h, μ, J = self.Π, self.b, self.h, self.μ, self.J H = .5 * (J + J.T) - # allocation + # 配置分配 c = inv(Π.T @ Π + μ * H) @ (Π.T @ b - μ * h) - # price + # 计算价格 p = 1 / μ * (Π.T @ b - Π.T @ Π @ c) - # check non-satiation + # 检查非饱和 if any(Π @ c - b >= 0): - raise Exception('invalid result: set bliss points further away') + raise Exception('无效结果:将极乐点设置得更远一些') return c, p def compute_surplus(self): """ - Compute consumer and producer surplus for single good case + 计算单一商品情况下的消费者和生产者剩余 """ if self.n != 1: - raise Exception('not single good') + raise Exception('非单一商品') h, J, Π, b, μ = self.h.item(), self.J.item(), self.Π.item(), self.b.item(), self.μ H = J - # supply/demand curve coefficients + # 供需曲线系数 s0, s1 = h, H d0, d1 = 1 / μ * Π * b, 1 / μ * Π**2 - # competitive equilibrium + # 竞争均衡 c, p = self.competitive_equilibrium() - # calculate surplus + # 计算剩余 c_surplus = d0 * c - .5 * d1 * c**2 - p * c p_surplus = p * c - s0 * c - .5 * s1 * c**2 return c_surplus, p_surplus ``` -Then define a function that plots demand and supply curves and labels surpluses and equilibrium. +然后定义一个函数,用于绘制需求曲线和供给曲线,并标注剩余和均衡点。 ```{code-cell} ipython3 :tags: [hide-input] def plot_competitive_equilibrium(PE): """ - Plot demand and supply curves, producer/consumer surpluses, and equilibrium for - a single good production economy + 绘制需求和供应曲线,生产者/消费者剩余以及均衡点 + 单一商品生产型经济 - Args: - PE (class): A initialized production economy class + 参数: + PE (class): 初始化的生产经济类 """ - # get singleton value + # 获取单值 J, h, Π, b, μ = PE.J.item(), PE.h.item(), PE.Π.item(), PE.b.item(), PE.μ H = J - # compute competitive equilibrium + # 计算竞争均衡 c, p = PE.competitive_equilibrium() c, p = c.item(), p.item() - # inverse supply/demand curve + # 反转供需曲线 supply_inv = lambda x: h + H * x demand_inv = lambda x: 1 / μ * (Π * b - Π * Π * x) @@ -745,42 +733,42 @@ def plot_competitive_equilibrium(PE): supply_curve = supply_inv(xs) demand_curve = demand_inv(xs) - # plot + # 绘图 plt.figure() - plt.plot(xs, supply_curve, label='Supply', color='#020060') - plt.plot(xs, demand_curve, label='Demand', color='#600001') + plt.plot(xs, supply_curve, label='供给曲线', color='#020060') + plt.plot(xs, demand_curve, label='需求曲线', color='#600001') - plt.fill_between(xs[xs <= c], demand_curve[xs <= c], ps[xs <= c], label='Consumer surplus', color='#EED1CF') - plt.fill_between(xs[xs <= c], supply_curve[xs <= c], ps[xs <= c], label='Producer surplus', color='#E6E6F5') + plt.fill_between(xs[xs <= c], demand_curve[xs <= c], ps[xs <= c], label='消费者剩余', color='#EED1CF') + plt.fill_between(xs[xs <= c], supply_curve[xs <= c], ps[xs <= c], label='生产者剩余', color='#E6E6F5') plt.vlines(c, 0, p, linestyle="dashed", color='black', alpha=0.7) plt.hlines(p, 0, c, linestyle="dashed", color='black', alpha=0.7) - plt.scatter(c, p, zorder=10, label='Competitive equilibrium', color='#600001') + plt.scatter(c, p, zorder=10, label='竞争均衡', color='#600001') plt.legend(loc='upper right') plt.margins(x=0, y=0) plt.ylim(0) - plt.xlabel('Quantity') - plt.ylabel('Price') + plt.xlabel('数量') + plt.ylabel('价格') plt.show() ``` -#### Example: single agent with one good and production +#### 示例:单一经济体与单一商品和生产 -Now let's construct an example of a production economy with one good. +现在我们来构建一个包含单一商品的生产经济示例。 -To do this we +为此我们需要: - * specify a single **person** and a **cost curve** in a way that let's us replicate the simple single-good supply demand example with which we started + * 指定一个**个人**和一个**成本曲线**,以复现我们开始时用到的简单单商品供求示例 - * compute equilibrium $p$ and $c$ and consumer and producer surpluses + * 计算均衡 $p$ 和 $c$,以及消费者和生产者剩余 - * draw graphs of both surpluses + * 绘制这两种剩余的图表 - * do experiments in which we shift $b$ and watch what happens to $p, c$. + * 通过改变 $b$ 并观察 $p, c$ 的变化来进行实验 ```{code-cell} ipython3 -Π = np.array([[1]]) # the matrix now is a singleton +Π = np.array([[1]]) # 矩阵现在是一个单元素 b = np.array([10]) h = np.array([0.5]) J = np.array([[1]]) @@ -789,62 +777,58 @@ J = np.array([[1]]) PE = ProductionEconomy(Π, b, h, J, μ) c, p = PE.competitive_equilibrium() -print('Competitive equilibrium price:', p.item()) -print('Competitive equilibrium allocation:', c.item()) +print('竞争均衡价格:', p.item()) +print('竞争均衡分配:', c.item()) -# plot plot_competitive_equilibrium(PE) ``` ```{code-cell} ipython3 c_surplus, p_surplus = PE.compute_surplus() -print('Consumer surplus:', c_surplus.item()) -print('Producer surplus:', p_surplus.item()) +print('消费者剩余:', c_surplus.item()) +print('生产者剩余:', p_surplus.item()) ``` -Let's give the consumer a lower welfare weight by raising $\mu$. +通过提高 $\mu$ 降低消费者福利权重。 ```{code-cell} ipython3 PE.μ = 2 c, p = PE.competitive_equilibrium() -print('Competitive equilibrium price:', p.item()) -print('Competitive equilibrium allocation:', c.item()) +print('竞争均衡价格:', p.item()) +print('竞争均衡配置:', c.item()) -# plot plot_competitive_equilibrium(PE) ``` ```{code-cell} ipython3 c_surplus, p_surplus = PE.compute_surplus() -print('Consumer surplus:', c_surplus.item()) -print('Producer surplus:', p_surplus.item()) +print('消费者剩余:', c_surplus.item()) +print('生产者剩余:', p_surplus.item()) ``` -Now we change the bliss point so that the consumer derives more utility from consumption. +现在我们改变极乐点,使消费者从消费中获得更多的效用。 ```{code-cell} ipython3 PE.μ = 1 PE.b = PE.b * 1.5 c, p = PE.competitive_equilibrium() -print('Competitive equilibrium price:', p.item()) -print('Competitive equilibrium allocation:', c.item()) +print('竞争均衡价格:', p.item()) +print('竞争均衡配置:', c.item()) -# plot plot_competitive_equilibrium(PE) ``` -This raises both the equilibrium price and quantity. - +这将提高均衡价格和数量。 -#### Example: single agent two-good economy with production +#### 示例:单一代理双商品经济与生产 - * we'll do some experiments like those above + * 我们将进行一些类似上面的实验 - * we can do experiments with a **diagonal** $\Pi$ and also with a **non-diagonal** $\Pi$ matrices to study how cross-slopes affect responses of $p$ and $c$ to various shifts in $b$ (TODO) + * 我们可以使用**对角线** $\Pi$ 和**非对角线** $\Pi$ 矩阵来做实验,以研究交叉斜率如何影响 $p$ 和 $c$ 对 $b$ 的各种变化的反应(待办) ```{code-cell} ipython3 Π = np.array([[1, 0], @@ -861,8 +845,8 @@ J = np.array([[1, 0.5], PE = ProductionEconomy(Π, b, h, J, μ) c, p = PE.competitive_equilibrium() -print('Competitive equilibrium price:', p) -print('Competitive equilibrium allocation:', c) +print('竞争性均衡价格:', p) +print('竞争性均衡配置:', c) ``` ```{code-cell} ipython3 @@ -870,8 +854,8 @@ PE.b = np.array([12, 10]) c, p = PE.competitive_equilibrium() -print('Competitive equilibrium price:', p) -print('Competitive equilibrium allocation:', c) +print('竞争性均衡价格:', p) +print('竞争性均衡配置:', c) ``` ```{code-cell} ipython3 @@ -882,91 +866,89 @@ PE.b = np.array([10, 10]) c, p = PE.competitive_equilibrium() -print('Competitive equilibrium price:', p) -print('Competitive equilibrium allocation:', c) +print('竞争性均衡价格:', p) +print('竞争性均衡配置:', c) ``` ```{code-cell} ipython3 PE.b = np.array([12, 10]) c, p = PE.competitive_equilibrium() -print('Competitive equilibrium price:', p) -print('Competitive equilibrium allocation:', c) +print('竞争性均衡价格:', p) +print('竞争性均衡配置:', c) ``` -### Digression: a supplier who is a monopolist +### 题外话:垄断的供应商 -A competitive firm is a **price-taker** who regards the price and therefore its marginal revenue as being beyond its control. +一个有竞争的公司是一个**价格接受者**,它认为价格因此其边际收益是无法由自己控制的。 -A monopolist knows that it has no competition and can influence the price and its marginal revenue by -setting quantity. +垄断者知道他们没有竞争对手,可以通过设定产量来影响价格及其边际收益。 -A monopolist takes a **demand curve** and not the **price** as beyond its control. +垄断者将**需求曲线**而不是**价格**视为无法控制的。 -Thus, instead of being a price-taker, a monopolist sets prices to maximize profits subject to the inverse demand curve -{eq}`eq:old5pa`. +因此,垄断者不是价格接受者,而是设定价格以最大化利润,受逆需求曲线的约束 +{eq}`eq:old5pa`。 -So the monopolist's total profits as a function of its output $q$ is +所以垄断者的总利润作为其产量$q$的函数是 $$ -[\mu^{-1} \Pi^\top (b - \Pi q)]^\top q - h^\top q - \frac{1}{2} q^\top J q +[\mu^{-1} \Pi^{\text{top}} (b - \Pi q)]^{\text{top}} q - h^{\text{top}} q - \frac{1}{2} q^{\text{top}} J q $$ (eq:monopprof) -After finding -first-order necessary conditions for maximizing monopoly profits with respect to $q$ -and solving them for $q$, we find that the monopolist sets +在找到使垄断利润最大化关于$q$的 +一阶必要条件并求解它们后,我们发现垄断者设置 $$ -q = (H + 2 \mu^{-1} \Pi^\top \Pi)^{-1} (\mu^{-1} \Pi^\top b - h) +q = (H + 2 \mu^{-1} \Pi^{\text{top}} \Pi)^{-1} (\mu^{-1} \Pi^{\text{top}} b - h) $$ (eq:qmonop) -We'll soon see that a monopolist sets a **lower output** $q$ than does either a +我们很快会看到,一个垄断者设置的**产量**$q$比以下任何一个都要低: - * planner who chooses $q$ to maximize social welfare + * 计划者选择$q$来最大化社会福利 - * a competitive equilibrium + * 一个有竞争的均衡 ```{exercise} :label: sdm_ex4 -Please verify the monopolist's supply curve {eq}`eq:qmonop`. +请验证垄断者的供应曲线 {eq}`eq:qmonop`。 ``` +++ -### A monopolist +### 垄断者 -Let's consider a monopolist supplier. +让我们考虑一个垄断供应商。 -We have included a method in our `ProductionEconomy` class to compute an equilibrium price and allocation when the supplier is a monopolist. +我们在我们的 `ProductionEconomy` 类中加入了一个方法,以计算在供应商为垄断者时的均衡价格和配置。 -Since the supplier now has the price-setting power +由于供应商现在拥有设定价格的权力: -- we first compute the optimal quantity that solves the monopolist's profit maximization problem. -- Then we back out an equilibrium price from the consumer's inverse demand curve. +- 我们首先计算解决垄断者利润最大化问题的最佳产量。 +- 然后我们从消费者的逆需求曲线中推导出一个均衡价格。 -Next, we use a graph for the single good case to illustrate the difference between a competitive equilibrium and an equilibrium with a monopolist supplier. +接下来,我们用图表来展示单一商品情况下,竞争性均衡和垄断供应商均衡之间的区别。 -Recall that in a competitive equilibrium, a price-taking supplier equates marginal revenue $p$ to marginal cost $h + Hq$. +回忆一下,在竞争性均衡中,一个接受价格的供应商使得边际收入 $p$ 等于边际成本 $h + Hq$。 -This yields a competitive producer's inverse supply curve. +这给出了竞争生产者的逆供应曲线。 -A monopolist's marginal revenue is not constant but instead is a non-trivial function of the quantity it sets. +垄断者的边际收入不是常数,而是依赖于它设定的数量的一个非平凡函数。 -The monopolist's marginal revenue is +垄断者的边际收入为 $$ -MR(q) = -2\mu^{-1}\Pi^{\top}\Pi q+\mu^{-1}\Pi^{\top}b, +MR(q) = -2\mu^{-1}\Pi^{\text{top}}\Pi q+\mu^{-1}\Pi^{\text{top}}b, $$ -which the monopolist equates to its marginal cost. +垄断者将此等于其边际成本。 -The plot indicates that the monopolist's sets output lower than either the competitive equilibrium quantity. +该图表显示,垄断者设定的产量比竞争均衡量更低。 -In a single good case, this equilibrium is associated with a higher price of the good. +在单一商品情况下,这种均衡与商品的更高价格相关。 ```{code-cell} ipython3 class Monopoly(ProductionEconomy): @@ -978,58 +960,54 @@ class Monopoly(ProductionEconomy): J, μ): """ - Inherit all properties and methods from class ProductionEconomy + 继承ProductionEconomy类的所有属性和方法 """ super().__init__(Π, b, h, J, μ) def equilibrium_with_monopoly(self): """ - Compute the equilibrium price and allocation when there is a monopolist supplier + 在有垄断供应商的情况下计算均衡价格和配置 """ Π, b, h, μ, J = self.Π, self.b, self.h, self.μ, self.J H = .5 * (J + J.T) - # allocation + # 配置 q = inv(μ * H + 2 * Π.T @ Π) @ (Π.T @ b - μ * h) - # price + # 价格 p = 1 / μ * (Π.T @ b - Π.T @ Π @ q) if any(Π @ q - b >= 0): - raise Exception('invalid result: set bliss points further away') + raise Exception('无效结果:将幸福点设置得更远') return q, p ``` - -Define a function that plots the demand, marginal cost and marginal revenue curves with surpluses and equilibrium labelled. - ```{code-cell} ipython3 :tags: [hide-input] def plot_monopoly(M): """ - Plot demand curve, marginal production cost and revenue, surpluses and the - equilibrium in a monopolist supplier economy with a single good + 绘制需求曲线、边际生产成本和收益、剩余以及标记的均衡情况,在具有垄断供应商的单一商品经济情形下 - Args: - M (class): A class inherits class ProductionEconomy with monopoly + 参数: + M (class): 继承单一商品垄断经济类ProductionEconomy的类 """ - # get singleton value + # 获取单值 J, h, Π, b, μ = M.J.item(), M.h.item(), M.Π.item(), M.b.item(), M.μ H = J - # compute competitive equilibrium + # 计算竞争均衡 c, p = M.competitive_equilibrium() q, pm = M.equilibrium_with_monopoly() c, p, q, pm = c.item(), p.item(), q.item(), pm.item() - # compute + # 计算 - # inverse supply/demand curve - marg_cost = lambda x: h + H * x - marg_rev = lambda x: -2 * 1 / μ * Π * Π * x + 1 / μ * Π * b - demand_inv = lambda x: 1 / μ * (Π * b - Π * Π * x) + # 供需曲线反函数 + marg_cost = lambda x: h + H * x # 边际成本 + marg_rev = lambda x: -2 * 1 / μ * Π * Π * x + 1 / μ * Π * b # 边际收益 + demand_inv = lambda x: 1 / μ * (Π * b - Π * Π * x) # 需求曲线 xs = np.linspace(0, 2 * c, 100) pms = np.ones(100) * pm @@ -1037,34 +1015,34 @@ def plot_monopoly(M): marg_rev_curve = marg_rev(xs) demand_curve = demand_inv(xs) - # plot + # 绘图 plt.figure() - plt.plot(xs, marg_cost_curve, label='Marginal cost', color='#020060') - plt.plot(xs, marg_rev_curve, label='Marginal revenue', color='#E55B13') - plt.plot(xs, demand_curve, label='Demand', color='#600001') + plt.plot(xs, marg_cost_curve, label='边际成本', color='#020060') + plt.plot(xs, marg_rev_curve, label='边际收益', color='#E55B13') + plt.plot(xs, demand_curve, label='需求', color='#600001') - plt.fill_between(xs[xs <= q], demand_curve[xs <= q], pms[xs <= q], label='Consumer surplus', color='#EED1CF') - plt.fill_between(xs[xs <= q], marg_cost_curve[xs <= q], pms[xs <= q], label='Producer surplus', color='#E6E6F5') + plt.fill_between(xs[xs <= q], demand_curve[xs <= q], pms[xs <= q], label='消费者剩余', color='#EED1CF') + plt.fill_between(xs[xs <= q], marg_cost_curve[xs <= q], pms[xs <= q], label='生产者剩余', color='#E6E6F5') plt.vlines(c, 0, p, linestyle="dashed", color='black', alpha=0.7) plt.hlines(p, 0, c, linestyle="dashed", color='black', alpha=0.7) - plt.scatter(c, p, zorder=10, label='Competitive equilibrium', color='#600001') + plt.scatter(c, p, zorder=10, label='竞争均衡', color='#600001') plt.vlines(q, 0, pm, linestyle="dashed", color='black', alpha=0.7) plt.hlines(pm, 0, q, linestyle="dashed", color='black', alpha=0.7) - plt.scatter(q, pm, zorder=10, label='Equilibrium with monopoly', color='#E55B13') + plt.scatter(q, pm, zorder=10, label='垄断均衡', color='#E55B13') plt.legend(loc='upper right') plt.margins(x=0, y=0) plt.ylim(0) - plt.xlabel('Quantity') - plt.ylabel('Price') + plt.xlabel('数量') + plt.ylabel('价格') plt.show() ``` -#### A multiple good example +#### 一个多种商品的例子 -Let's compare competitive equilibrium and monopoly outcomes in a multiple goods economy. +让我们在一个多种商品的经济中比较竞争均衡和垄断结果。 ```{code-cell} ipython3 Π = np.array([[1, 0], @@ -1082,17 +1060,17 @@ M = Monopoly(Π, b, h, J, μ) c, p = M.competitive_equilibrium() q, pm = M.equilibrium_with_monopoly() -print('Competitive equilibrium price:', p) -print('Competitive equilibrium allocation:', c) +print('竞争均衡价格:', p) +print('竞争均衡分配:', c) -print('Equilibrium with monopolist supplier price:', pm) -print('Equilibrium with monopolist supplier allocation:', q) +print('具有垄断供应商的均衡价格:', pm) +print('具有垄断供应商的均衡分配:', q) ``` -#### A single-good example +#### 单一商品示例 ```{code-cell} ipython3 -Π = np.array([[1]]) # the matrix now is a singleton +Π = np.array([[1]]) # 现在矩阵是一个单元素 b = np.array([10]) h = np.array([0.5]) J = np.array([[1]]) @@ -1102,53 +1080,52 @@ M = Monopoly(Π, b, h, J, μ) c, p = M.competitive_equilibrium() q, pm = M.equilibrium_with_monopoly() -print('Competitive equilibrium price:', p.item()) -print('Competitive equilibrium allocation:', c.item()) +print('竞争均衡价格:', p.item()) +print('竞争均衡配置:', c.item()) -print('Equilibrium with monopolist supplier price:', pm.item()) -print('Equilibrium with monopolist supplier allocation:', q.item()) +print('垄断供应商均衡价格:', pm.item()) +print('垄断供应商均衡配置:', q.item()) -# plot plot_monopoly(M) ``` -## Multi-good welfare maximization problem +## 多商品福利最大化问题 -Our welfare maximization problem -- also sometimes called a social planning problem -- is to choose $c$ to maximize +我们的福利最大化问题 -- 有时也被称为社会规划问题 -- 是选择 $c$ 来最大化 $$ - - \frac{1}{2} \mu^{-1}(\Pi c -b) ^\top (\Pi c -b ) + - \frac{1}{2} \mu^{-1}(\Pi c -b)^\top (\Pi c -b) $$ -minus the area under the inverse supply curve, namely, +减去逆供给曲线下的面积,即, $$ - h c + \frac{1}{2} c^\top J c + h c + \frac{1}{2} c^\top J c $$ -So the welfare criterion is +所以福利标准是 $$ - - \frac{1}{2} \mu^{-1}(\Pi c -b)^\top (\Pi c -b ) -h c - - \frac{1}{2} c^\top J c + - \frac{1}{2} \mu^{-1}(\Pi c -b)^\top (\Pi c -b) -h c + - \frac{1}{2} c^\top J c $$ -In this formulation, $\mu$ is a parameter that describes how the planner weighs interests of outside suppliers and our representative consumer. +在这个公式中,$\mu$ 是一个参数,描述了规划者如何权衡外部供应商和我们代表性消费者的利益。 -The first-order condition with respect to $c$ is +对 $c$ 的一阶条件是 $$ -- \mu^{-1} \Pi^\top \Pi c + \mu^{-1}\Pi^\top b - h - H c = 0 +- \mu^{-1} \Pi^\top \Pi c + \mu^{-1}\Pi^\top b - h - J c = 0 $$ -which implies {eq}`eq:old5p`. +这意味着 {eq}`eq:old5p` 被满足。 -Thus, as for the single-good case, with multiple goods a competitive equilibrium quantity vector solves a planning problem. +因此,与单一商品情形一样,对于多商品情况,一个竞争均衡的数量向量解决了一个规划问题。 -(This is another version of the first welfare theorem.) +(这是第一福利定理的另一个版本。) -We can deduce a competitive equilibrium price vector from either +我们可以从以下两个地方推导出竞争均衡的价格向量: - * the inverse demand curve, or + * 逆需求曲线,或 - * the inverse supply curve + * 逆供给曲线 \ No newline at end of file diff --git a/lectures/time_series_with_matrices.md b/lectures/time_series_with_matrices.md index e73566c..0ad0562 100644 --- a/lectures/time_series_with_matrices.md +++ b/lectures/time_series_with_matrices.md @@ -20,48 +20,43 @@ kernelspec: ``` -# Univariate Time Series with Matrix Algebra +# 用矩阵代数表示的单变量时间序列 -## Overview +## 概述 -This lecture uses matrices to solve some linear difference equations. +本讲使用矩阵来解决一些线性差分方程。 -As a running example, we’ll study a **second-order linear difference -equation** that was the key technical tool in Paul Samuelson’s 1939 -article {cite}`Samuelson1939` that introduced the *multiplier-accelerator model*. +作为一个实际例子,我们将研究一个保罗·萨缪尔森 1939 年文章 {cite}`Samuelson1939` 中的**二阶线性差分方程**,该文章引入了**乘数加速器**模型。 -This model became the workhorse that powered early econometric versions of -Keynesian macroeconomic models in the United States. +该模型推动了早期美国凯恩斯主义宏观经济模型的计量经济版本。 -You can read about the details of that model in {doc}`intermediate:samuelson`. +你可以在{doc}`intermediate:samuelson`中阅读该模型的详细信息。 -(That lecture also describes some technicalities about second-order linear difference equations.) +(该讲座还描述了一些关于二阶线性差分方程的细节。) -In this lecture, we'll also learn about an **autoregressive** representation and a **moving average** representation of a non-stationary -univariate time series $\{y_t\}_{t=0}^T$. +在本讲座中,我们还将了解一个非平稳单变量时间序列 $\{y_t\}_{t=0}^T$ 的**自回归**表达和**移动平均**表达。 -We'll also study a "perfect foresight" model of stock prices that involves solving -a "forward-looking" linear difference equation. +我们还将研究一个涉及解“前瞻性”线性差分方程的“完美預期”股票价格模型。 -We will use the following imports: +我们将使用以下导入: ```{code-cell} ipython3 import numpy as np import matplotlib.pyplot as plt from matplotlib import cm -# Custom figsize for this lecture +# 设置默认图形大小 plt.rcParams["figure.figsize"] = (11, 5) -# Set decimal printing to 3 decimal places +# 设置打印的浮点数精度 np.set_printoptions(precision=3, suppress=True) ``` -## Samuelson's model +## 萨缪尔森的模型 -Let $t = 0, \pm 1, \pm 2, \ldots$ index time. +设 $t = 0, \pm 1, \pm 2, \ldots$ 为时间。 -For $t = 1, 2, 3, \ldots, T$ suppose that +对于 $t=1, 2, 3, \ldots, T$,假设 ```{math} :label: tswm_1 @@ -69,25 +64,19 @@ For $t = 1, 2, 3, \ldots, T$ suppose that y_{t} = \alpha_{0} + \alpha_{1} y_{t-1} + \alpha_{2} y_{t-2} ``` -where we assume that $y_0$ and $y_{-1}$ are given numbers -that we take as *initial conditions*. +我们假设 $y_0$ 和 $y_{-1}$ 是给定的数字,我们将其作为**初始条件**。 -In Samuelson's model, $y_t$ stood for **national income** or perhaps a different -measure of aggregate activity called **gross domestic product** (GDP) at time $t$. +在萨缪尔森的模型中,$y_t$ 表示 **国民收入** 或者 **国内生产总值**(GDP)在时间 $t$ 的测量值。 -Equation {eq}`tswm_1` is called a *second-order linear difference equation*. It is called second order because it depends on two lags. +方程 {eq}`tswm_1` 称为 **二阶线性差分方程**,因为它包含了两个滞后值。 -But actually, it is a collection of $T$ simultaneous linear -equations in the $T$ variables $y_1, y_2, \ldots, y_T$. +实际上,它是 $T$ 个关于 $T$ 个变量 $y_1, y_2, \ldots, y_T$ 的线性方程的集合。 ```{note} -To be able to solve a second-order linear difference -equation, we require two *boundary conditions* that can take the form -either of two *initial conditions*, two *terminal conditions* or -possibly one of each. +为了能够解决一个二阶线性差分方程,我们需要两个**边界条件**,它们可以采取两种**初始条件**或两种**终端条件**或可能是每种一种的形式。 ``` -Let’s write our equations as a stacked system +我们将方程写成堆叠系统 $$ \underset{\equiv A}{\underbrace{\left[\begin{array}{cccccccc} @@ -114,35 +103,34 @@ y_{T} \end{array}\right]}} $$ -or +或者 $$ A y = b $$ -where +其中 $$ y = \begin{bmatrix} y_1 \cr y_2 \cr \vdots \cr y_T \end{bmatrix} $$ -Evidently $y$ can be computed from +显然,$y$ 可以由以下公式计算得出 $$ y = A^{-1} b $$ -The vector $y$ is a complete time path $\{y_t\}_{t=1}^T$. +向量 $y$ 是完整的时间路径 $\{y_t\}_{t=1}^T$。 -Let’s put Python to work on an example that captures the flavor of -Samuelson’s multiplier-accelerator model. +我们用 Python 来实现一个例子来展现萨缪尔森乘数-加速器模型的基本思想。 -We'll set parameters equal to the same values we used in {doc}`intermediate:samuelson`. +我们将参数设置为与讲{doc}`intermediate:samuelson`中使用的值相同。 ```{code-cell} ipython3 T = 80 -# parameters +# 参数 α_0 = 10.0 α_1 = 1.53 α_2 = -.9 @@ -151,10 +139,10 @@ y_neg1 = 28.0 # y_{-1} y_0 = 24.0 ``` -Now we construct $A$ and $b$. +现在我们构造 $A$ 和 $b$。 ```{code-cell} ipython3 -A = np.identity(T) # The T x T identity matrix +A = np.identity(T) # T x T 的单位矩阵 for i in range(T): @@ -169,19 +157,18 @@ b[0] = α_0 + α_1 * y_0 + α_2 * y_neg1 b[1] = α_0 + α_2 * y_0 ``` -Let’s look at the matrix $A$ and the vector $b$ for our -example. +让我们来看看我们的例子中的矩阵 $A$ 和向量 $b$。 ```{code-cell} ipython3 A, b ``` -Now let’s solve for the path of $y$. +现在我们来求解 $y$ 的路径。 -If $y_t$ is GNP at time $t$, then we have a version of -Samuelson’s model of the dynamics for GNP. +如果 $y_t$ 是 $t$ 时间的国民生产总值,那么我们就有了一个版本的 +萨缪尔森的国民生产总值动态模型。 -To solve $y = A^{-1} b$ we can either invert $A$ directly, as in +要求解 $y = A^{-1} b$,我们可以直接倒置 $A$ ```{code-cell} ipython3 A_inv = np.linalg.inv(A) @@ -195,14 +182,13 @@ or we can use `np.linalg.solve`: y_second_method = np.linalg.solve(A, b) ``` -Here make sure the two methods give the same result, at least up to floating -point precision: +我们确保这两种方法在一定精度下给出相同的结果: ```{code-cell} ipython3 np.allclose(y, y_second_method) ``` -$A$ is invertible as it is lower triangular and [its diagonal entries are non-zero](https://www.statlect.com/matrix-algebra/triangular-matrix) +$A$ 是可逆的,因为它是下三角且[其对角线条目非零](https://www.statlect.com/matrix-algebra/triangular-matrix) ```{code-cell} ipython3 # Check if A is lower triangular @@ -210,11 +196,9 @@ np.allclose(A, np.tril(A)) ``` ```{note} -In general, `np.linalg.solve` is more numerically stable than using -`np.linalg.inv` directly. -However, stability is not an issue for this small example. Moreover, we will -repeatedly use `A_inv` in what follows, so there is added value in computing -it directly. + +一般来说,`np.linalg.solve`比使用`np.linalg.solve`在数值上更稳定。 +然而,对于这个小例子来说,稳定性不是问题。此外,我们将下面重复使用`A_inv`,直接计算出它来会有比较好。 ``` Now we can plot. @@ -227,15 +211,13 @@ plt.ylabel('y') plt.show() ``` -The {ref}`*steady state*` value $y^*$ of $y_t$ is obtained by setting $y_t = y_{t-1} = -y_{t-2} = y^*$ in {eq}`tswm_1`, which yields +通过在{eq}`tswm_1`中设定 $y_t = y_{t-1} = y_{t-2} = y^*$,可以得到 $y_t$ 的 {ref}`*稳态*` 值 $y^*$。 $$ y^* = \frac{\alpha_{0}}{1 - \alpha_{1} - \alpha_{2}} $$ -If we set the initial values to $y_{0} = y_{-1} = y^*$, then $y_{t}$ will be -constant: +如果我们将初始值设为 $y_{0} = y_{-1} = y^*$,那么 $y_{t}$ 将是恒定的: ```{code-cell} ipython3 y_star = α_0 / (1 - α_1 - α_2) @@ -259,12 +241,10 @@ plt.ylabel('y') plt.show() ``` -## Adding a random term +## 添加随机项 + +为了让这个例子有趣一些,我们将遵循经济学家[尤金·斯卢茨基](https://en.wikipedia.org/wiki/Eugen_Slutsky)和[拉格纳·弗里希](https://en.wikipedia.org/wiki/Ragnar_Frisch)的方法,用以下**二阶随机线性差分方程**替换我们原来的二阶差分方程: -To generate some excitement, we'll follow in the spirit of the great economists -[Eugen Slutsky](https://en.wikipedia.org/wiki/Eugen_Slutsky) and [Ragnar Frisch](https://en.wikipedia.org/wiki/Ragnar_Frisch) and replace our original second-order difference -equation with the following **second-order stochastic linear difference -equation**: ```{math} :label: tswm_2 @@ -272,13 +252,11 @@ equation**: y_{t} = \alpha_{0} + \alpha_{1} y_{t-1} + \alpha_{2} y_{t-2} + u_t ``` -where $u_{t} \sim N\left(0, \sigma_{u}^{2}\right)$ and is {ref}`IID `, -meaning independent and identically distributed. +其中 $u_{t} \sim N\left(0, \sigma_{u}^{2}\right)$ 并且是 {ref}`独立同分布` -- 相互独立且服从相同分布。 -We’ll stack these $T$ equations into a system cast in terms of -matrix algebra. +我们将把这些 $T$ 个方程堆叠成一个以矩阵代数表示的系统。 -Let’s define the random vector +让我们定义随机向量 $$ u=\left[\begin{array}{c} @@ -289,20 +267,21 @@ u_{T} \end{array}\right] $$ -Where $A, b, y$ are defined as above, now assume that $y$ is -governed by the system +其中 $A, b, y$ 定义如上,现在假设 $y$ 由系统 $$ A y = b + u $$ (eq:eqar) -The solution for $y$ becomes +所支配 + +$y$ 的解变为 $$ y = A^{-1} \left(b + u\right) $$ (eq:eqma) -Let’s try it out in Python. +让我们在Python中尝试一下。 ```{code-cell} ipython3 σ_u = 2. @@ -318,16 +297,15 @@ plt.ylabel('y') plt.show() ``` -The above time series looks a lot like (detrended) GDP series for a -number of advanced countries in recent decades. +上面的时间序列在最近几十年中与很多先进国家(去趋势后的)GDP系列非常相似。 -We can simulate $N$ paths. +我们可以模拟 $N$ 条路径。 ```{code-cell} ipython3 N = 100 for i in range(N): - col = cm.viridis(np.random.rand()) # Choose a random color from viridis + col = cm.viridis(np.random.rand()) # 从 viridis 色系中随机选择一种颜色 u = np.random.normal(0, σ_u, size=T) y = A_inv @ (b + u) plt.plot(np.arange(T)+1, y, lw=0.5, color=col) @@ -338,14 +316,13 @@ plt.ylabel('y') plt.show() ``` -Also consider the case when $y_{0}$ and $y_{-1}$ are at -steady state. +同样考虑 $y_{0}$ 和 $y_{-1}$ 处于稳态的情况。 ```{code-cell} ipython3 N = 100 for i in range(N): - col = cm.viridis(np.random.rand()) # Choose a random color from viridis + col = cm.viridis(np.random.rand()) # 从 viridis 色系中随机选择一种颜色 u = np.random.normal(0, σ_u, size=T) y_steady = A_inv @ (b_steady + u) plt.plot(np.arange(T)+1, y_steady, lw=0.5, color=col) @@ -356,53 +333,51 @@ plt.ylabel('y') plt.show() ``` -## Computing population moments - +## 计算总体矩 -We can apply standard formulas for multivariate normal distributions to compute the mean vector and covariance matrix -for our time series model +我们可以应用多元正态分布的标准公式来计算我们的时间序列模型 $$ y = A^{-1} (b + u) . $$ -You can read about multivariate normal distributions in this lecture [Multivariate Normal Distribution](https://python.quantecon.org/multivariate_normal.html). +你可以在这篇讲义中阅读关于多元正态分布的内容 [多元正态分布](https://python.quantecon.org/multivariate_normal.html)。 -Let's write our model as +让我们将我们的模型写为 $$ y = \tilde A (b + u) $$ -where $\tilde A = A^{-1}$. +其中 $\tilde A = A^{-1}$。 -Because linear combinations of normal random variables are normal, we know that +因为正态随机变量的线性组合依然是正态的,我们知道 $$ y \sim {\mathcal N}(\mu_y, \Sigma_y) $$ -where +其中 $$ \mu_y = \tilde A b $$ -and +以及 $$ -\Sigma_y = \tilde A (\sigma_u^2 I_{T \times T} ) \tilde A^T +\Sigma_y = \tilde A (\σ_u^2 I_{T \times T} ) \tilde A^T $$ -Let's write a Python class that computes the mean vector $\mu_y$ and covariance matrix $\Sigma_y$. +让我们编写一个Python类来计算均值向量 $\mu_y$ 和协方差矩阵 $\Sigma_y$。 ```{code-cell} ipython3 class population_moments: """ - Compute population moments μ_y, Σ_y. + 计算人群矩 mu_y, Sigma_y. --------- - Parameters: - α_0, α_1, α_2, T, y_neg1, y_0 + 参数: + alpha0, alpha1, alpha2, T, y_1, y0 """ def __init__(self, α_0=10.0, α_1=1.53, @@ -412,7 +387,7 @@ class population_moments: y_0=24.0, σ_u=1): - # compute A + # 计算 A A = np.identity(T) for i in range(T): @@ -422,19 +397,19 @@ class population_moments: if i-2 >= 0: A[i, i-2] = -α_2 - # compute b + # 计算 b b = np.full(T, α_0) b[0] = α_0 + α_1 * y_0 + α_2 * y_neg1 b[1] = α_0 + α_2 * y_0 - # compute A inverse + # 计算 A 的逆 A_inv = np.linalg.inv(A) self.A, self.b, self.A_inv, self.σ_u, self.T = A, b, A_inv, σ_u, T def sample_y(self, n): """ - Give a sample of size n of y. + 提供一个大小为 n 的 y 样本。 """ A_inv, σ_u, b, T = self.A_inv, self.σ_u, self.b, self.T us = np.random.normal(0, σ_u, size=[n, T]) @@ -444,11 +419,11 @@ class population_moments: def get_moments(self): """ - Compute the population moments of y. + 计算 y 的总体矩。 """ A_inv, σ_u, b = self.A_inv, self.σ_u, self.b - # compute μ_y + # 计算 μ_y self.μ_y = A_inv @ b self.Σ_y = σ_u**2 * (A_inv @ A_inv.T) @@ -461,18 +436,18 @@ series_process = population_moments() A_inv = series_process.A_inv ``` -It is enlightening to study the $\mu_y, \Sigma_y$'s implied by various parameter values. +研究由各种参数值隐含的 $\mu_y, \Sigma_y$ 是非常有意义的。 -Among other things, we can use the class to exhibit how **statistical stationarity** of $y$ prevails only for very special initial conditions. +此外,我们可以使用该类展示 $y$ 的**统计平稳性**仅在非常特殊的初始条件下成立。 -Let's begin by generating $N$ time realizations of $y$ plotting them together with population mean $\mu_y$ . +让我们首先生成 $y$ 的 $N$ 次时间实现,并将它们与总体均值 $\mu_y$ 一起绘制出来。 ```{code-cell} ipython3 -# Plot mean +# 绘制均值 N = 100 for i in range(N): - col = cm.viridis(np.random.rand()) # Choose a random color from viridis + col = cm.viridis(np.random.rand()) # 从 viridis 色系中随机选择一种颜色 ys = series_process.sample_y(N) plt.plot(ys[i,:], lw=0.5, color=col) plt.plot(μ_y, color='red') @@ -483,21 +458,19 @@ plt.ylabel('y') plt.show() ``` -Visually, notice how the variance across realizations of $y_t$ decreases as $t$ increases. +从视觉上看,注意到随着 $t$ 增加,$y_t$ 的各次实现之间的方差在减少。 -Let's plot the population variance of $y_t$ against $t$. +绘制总体方差 $\Sigma_y$ 对角线。 ```{code-cell} ipython3 -# Plot variance +# 绘制方差 plt.plot(Σ_y.diagonal()) plt.show() ``` -Notice how the population variance increases and asymptotes. +注意总体方差如何增加并接近渐近值。 -+++ - -Let's print out the covariance matrix $\Sigma_y$ for a time series $y$. +让我们从多个实现中计算样本方差并绘制出来。 ```{code-cell} ipython3 series_process = population_moments(α_0=0, @@ -513,15 +486,15 @@ print("μ_y = ", μ_y) print("Σ_y = \n", Σ_y) ``` -Notice that the covariance between $y_t$ and $y_{t-1}$ -- the elements on the superdiagonal -- are *not* identical. +注意 $y_t$ 和 $y_{t-1}$ 之间的协方差——即超对角线上的元素——并*不*相同。 -This is an indication that the time series represented by our $y$ vector is not **stationary**. +这表明由 $y$ 向量表示的时间序列并非**平稳**。 -To make it stationary, we'd have to alter our system so that our *initial conditions* $(y_0, y_{-1})$ are not fixed numbers but instead a jointly normally distributed random vector with a particular mean and covariance matrix. +为了使其平稳,我们必须改变系统,使得*初始条件* $(y_0, y_{-1})$ 不再是固定的数值,而是一个具有特定均值和协方差矩阵的联合正态分布随机向量。 -We describe how to do that in [Linear State Space Models](https://python.quantecon.org/linear_models.html). +我们在[线性状态空间模型](https://python.quantecon.org/linear_models.html)中描述了如何实现这一点。 -But just to set the stage for that analysis, let's print out the bottom right corner of $\Sigma_y$. +但为了为分析做好铺垫,接下来我们打印出 $\Sigma_y$ 的右下角。 ```{code-cell} ipython3 series_process = population_moments() @@ -530,84 +503,79 @@ series_process = population_moments() print("bottom right corner of Σ_y = \n", Σ_y[72:,72:]) ``` -Please notice how the subdiagonal and superdiagonal elements seem to have converged. +注意 $y_t$ 和 $y_{t-1}$ 之间的协方差——即超对角线上的元素——并*不*相同。 -This is an indication that our process is asymptotically stationary. +这表明由 $y$ 向量表示的时间序列并非**平稳**。 -You can read about stationarity of more general linear time series models in this lecture [Linear State Space Models](https://python.quantecon.org/linear_models.html). +为了使其平稳,我们必须改变系统,使得*初始条件* $(y_0, y_{-1})$ 不再是固定的数值,而是一个具有特定均值和协方差矩阵的联合正态分布随机向量。 -There is a lot to be learned about the process by staring at the off diagonal elements of $\Sigma_y$ corresponding to different time periods $t$, but we resist the temptation to do so here. +我们在[线性状态空间模型](https://python.quantecon.org/linear_models.html)中描述了如何实现这一点。 + +但为了为分析做好铺垫,接下来我们打印出 $\Sigma_y$ 的右下角。 +++ -## Moving average representation +## 移动平均表示 -Let's print out $A^{-1}$ and stare at its structure +让我们打印出 $A^{-1}$ 并观察其结构 - * is it triangular or almost triangular or $\ldots$ ? + * 它是三角形矩阵、接近三角形,还是其他形式 $\ldots$? -To study the structure of $A^{-1}$, we shall print just up to $3$ decimals. +为了研究 $A^{-1}$ 的结构,我们将只打印到小数点后三位。 -Let's begin by printing out just the upper left hand corner of $A^{-1}$. +首先让我们打印出 $A^{-1}$ 的左上角部分。 ```{code-cell} ipython3 print(A_inv[0:7,0:7]) ``` -Evidently, $A^{-1}$ is a lower triangular matrix. +显然,$A^{-1}$ 是一个下三角矩阵。 + +注意每一行的结尾都与前一行的前对角线元素相同。 -Notice how every row ends with the previous row's pre-diagonal entries. +由于 $A^{-1}$ 是下三角矩阵,每一行代表特定 $t$ 时的 $y_t$,作为以下两部分之和: -Since $A^{-1}$ is lower triangular, each row represents $ y_t$ for a particular $t$ as the sum of -- a time-dependent function $A^{-1} b$ of the initial conditions incorporated in $b$, and -- a weighted sum of current and past values of the IID shocks $\{u_t\}$. +- 与初始条件 $b$ 相关的时间依赖函数 $A^{-1} b$,以及 +- 当前和过去 IID 冲击 $\{u_t\}$ 的加权和。 -Thus, let $\tilde{A}=A^{-1}$. +因此,设 $\tilde{A}=A^{-1}$。 -Evidently, for $t\geq0$, +显然,对于 $t\geq0$, $$ y_{t+1}=\sum_{i=1}^{t+1}\tilde{A}_{t+1,i}b_{i}+\sum_{i=1}^{t}\tilde{A}_{t+1,i}u_{i}+u_{t+1} $$ -This is a **moving average** representation with time-varying coefficients. +这是一个具有时间变化系数的**移动平均**表示。 -Just as system {eq}`eq:eqma` constitutes a -**moving average** representation for $y$, system {eq}`eq:eqar` constitutes an **autoregressive** representation for $y$. +正如系统 {eq}`eq:eqma` 构成了 $y$ 的**移动平均**表示,系统 {eq}`eq:eqar` 构成了 $y$ 的**自回归**表示。 -## A forward looking model +## 一个前瞻性模型 -Samuelson’s model is *backward looking* in the sense that we give it *initial conditions* and let it -run. +萨缪尔森的模型是*后视*的,因为我们给它*初始条件*并让它运行。 -Let’s now turn to model that is *forward looking*. +现在我们转向一个*前瞻性*模型。 -We apply similar linear algebra machinery to study a *perfect -foresight* model widely used as a benchmark in macroeconomics and -finance. +我们应用类似的线性代数工具来研究一个广泛用作宏观经济学和金融学基准的*完美预见*模型。 -As an example, we suppose that $p_t$ is the price of a stock and -that $y_t$ is its dividend. +例如,假设 $p_t$ 是股票价格,$y_t$ 是其股息。 -We assume that $y_t$ is determined by second-order difference -equation that we analyzed just above, so that +我们假设 $y_t$ 由我们刚刚分析的二阶差分方程确定,因此 $$ y = A^{-1} \left(b + u\right) $$ -Our *perfect foresight* model of stock prices is +我们的*完美预见*股票价格模型是 $$ p_{t} = \sum_{j=0}^{T-t} \beta^{j} y_{t+j}, \quad \beta \in (0,1) $$ -where $\beta$ is a discount factor. +其中 $\beta$ 是折现因子。 -The model asserts that the price of the stock at $t$ equals the -discounted present values of the (perfectly foreseen) future dividends. +该模型断言,在 $t$ 时股票的价格等于(完美预见的)未来股息的折现现值之和 -Form $$ \underset{\equiv p}{\underbrace{\left[\begin{array}{c} @@ -668,10 +636,9 @@ plt.legend() plt.show() ``` -Can you explain why the trend of the price is downward over time? +你能解释一下为什么价格的趋势在随时间下降吗? -Also consider the case when $y_{0}$ and $y_{-1}$ are at the -steady state. +接下来还可以考虑当 $y_{0}$ 和 $y_{-1}$ 处于稳态时的情况。 ```{code-cell} ipython3 p_steady = B @ y_steady diff --git a/lectures/troubleshooting.md b/lectures/troubleshooting.md index 7bc907a..795753c 100644 --- a/lectures/troubleshooting.md +++ b/lectures/troubleshooting.md @@ -18,54 +18,51 @@ kernelspec: ``` -# Troubleshooting +# 故障排除 -This page is for readers experiencing errors when running the code from the lectures. +本页面旨在帮助读者解决执行讲座代码时遇到的错误。 -## Fixing your local environment +## 修复您的本地环境 -The basic assumption of the lectures is that code in a lecture should execute whenever +讲座的基本假设是:只要 -1. it is executed in a Jupyter notebook and -1. the notebook is running on a machine with the latest version of Anaconda Python. +1. 在Jupyter笔记本中执行代码,并且 +1. 笔记本在安装了最新版Anaconda Python的机器上运行。 -You have installed Anaconda, haven't you, following the instructions in [this lecture](https://python-programming.quantecon.org/getting_started.html)? +你已经安装了Anaconda,是吧,按照[这节课](https://python-programming.quantecon.org/getting_started.html)中的指引? -Assuming that you have, the most common source of problems for our readers is that their Anaconda distribution is not up to date. +假设你已经安装了,我们读者最常见的问题是他们的Anaconda发行版不是最新的。 -[Here's a useful article](https://www.anaconda.com/blog/keeping-anaconda-date) -on how to update Anaconda. +[这里有一篇有用的文章](https://www.anaconda.com/blog/keeping-anaconda-date) +讲述如何更新Anaconda。 -Another option is to simply remove Anaconda and reinstall. +另一个选项是简单地卸载Anaconda并重新安装。 -You also need to keep the external code libraries, such as [QuantEcon.py](https://quantecon.org/quantecon-py) up to date. +你还需要更新外部代码库,例如[QuantEcon.py](https://quantecon.org/quantecon-py)。 -For this task you can either +为此,你可以 -* use conda install -y quantecon on the command line, or -* execute !conda install -y quantecon within a Jupyter notebook. +* 在命令行中使用 conda install -y quantecon,或 +* 在Jupyter笔记本中执行 !conda install -y quantecon。 -If your local environment is still not working you can do two things. +如果您的本地环境依然无法工作,你可以做两件事。 -First, you can use a remote machine instead, by clicking on the Launch Notebook icon available for each lecture +首先,你可以通过点击每个教程提供的启动笔记本图标改用远程机器 ```{image} _static/lecture_specific/troubleshooting/launch.png ``` -Second, you can report an issue, so we can try to fix your local set up. +其次,你可以报告一个问题,这样我们可以尝试修复您的本地设置。 -We like getting feedback on the lectures so please don't hesitate to get in -touch. +我们喜欢收到课程的反馈,所以请随时与我们联系。 -## Reporting an issue +## 报告问题 -One way to give feedback is to raise an issue through our [issue tracker](https://github.com/QuantEcon/lecture-python/issues). +一种反馈方式是通过我们的[问题跟踪器](https://github.com/QuantEcon/lecture-python/issues)提出问题。 -Please be as specific as possible. Tell us where the problem is and as much -detail about your local set up as you can provide. +请尽可能具体。告诉我们问题所在,以及尽可能多的关于您本地设置的细节。 -Another feedback option is to use our [discourse forum](https://discourse.quantecon.org/). - -Finally, you can provide direct feedback to [contact@quantecon.org](mailto:contact@quantecon.org) +另一个反馈选项是使用我们的[论坛](https://discourse.quantecon.org/)。 +最后,您可以直接向[contact@quantecon.org](mailto:contact@quantecon.org)提供反馈。 \ No newline at end of file diff --git a/lectures/unpleasant.md b/lectures/unpleasant.md index 676edfa..d5a557e 100644 --- a/lectures/unpleasant.md +++ b/lectures/unpleasant.md @@ -11,170 +11,153 @@ kernelspec: name: python3 --- -# Some Unpleasant Monetarist Arithmetic +# 一些不愉快的货币主义算术 -## Overview +## 概述 +本讲座基于 {doc}`money_inflation` 中引入的概念和问题。 -This lecture builds on concepts and issues introduced in {doc}`money_inflation`. +那个讲座描述了揭示通货膨胀税率和关联的货币收益率的静止均衡 [*拉弗曲线*](https://en.wikipedia.org/wiki/Laffer_curve)。 -That lecture describes stationary equilibria that reveal a [*Laffer curve*](https://en.wikipedia.org/wiki/Laffer_curve) in the inflation tax rate and the associated stationary rate of return -on currency. +在这次讲座中,我们研究一个在日期 $T > 0$ 之后占优的静止均衡的情况,但在那之前并非如此。 -In this lecture we study a situation in which a stationary equilibrium prevails after date $T > 0$, but not before then. +对于 $t=0, \ldots, T-1$,货币供应、价格水平和计息政府债务沿着一个在 $t=T$ 结束的过渡路径变化。 -For $t=0, \ldots, T-1$, the money supply, price level, and interest-bearing government debt vary along a transition path that ends at $t=T$. +在这个过渡期间,实际余额 $\frac{m_{t+1}}{p_t}$ 与在时间 $t$ 到期的一期政府债券 $\tilde{R} B_{t-1}$ 的比率每个期间都在减少。 -During this transition, the ratio of the real balances $\frac{m_{t+1}}{{p_t}}$ to indexed one-period government bonds $\tilde R B_{t-1}$ maturing at time $t$ decreases each period. +这对于必须通过印制货币来融资的 **息前** 政府赤字在 $t \geq T$ 时期有影响。 -This has consequences for the **gross-of-interest** government deficit that must be financed by printing money for times $t \geq T$. +关键的 **货币与债券** 比率只在时间 $T$ 及之后稳定。 -The critical **money-to-bonds** ratio stabilizes only at time $T$ and afterwards. +并且 $T$ 越大,在 $t \geq T$ 时期必须通过印制货币来融资的息前政府赤字就越大。 -And the larger is $T$, the higher is the gross-of-interest government deficit that must be financed -by printing money at times $t \geq T$. +这些结果是 Sargent 和 Wallace 的“不愉快的货币主义算术” {cite}`sargent1981` 的基本发现。 -These outcomes are the essential finding of Sargent and Wallace's "unpleasant monetarist arithmetic" {cite}`sargent1981`. +那次讲座描述了在讲座中出现的货币供应和需求。 -That lecture described supplies and demands for money that appear in lecture. +它还描述了我们在本讲座中从中向后工作的稳态均衡。 -It also characterized the steady state equilibrium from which we work backwards in this lecture. +除了学习“不愉快的货币主义算术”,在这次讲座中,我们还将学习如何实施一个用于计算初始价格水平的 [*不动点*](https://en.wikipedia.org/wiki/Fixed_point_(mathematics)) 算法。 -In addition to learning about "unpleasant monetarist arithmetic", in this lecture we'll learn how to implement a [*fixed point*](https://en.wikipedia.org/wiki/Fixed_point_(mathematics)) algorithm for computing an initial price level. +## 设置 +让我们从快速回顾一下 {doc}`money_inflation` 中提到的模型组件开始。 -## Setup - -Let's start with quick reminders of the model's components set out in {doc}`money_inflation`. - -Please consult that lecture for more details and Python code that we'll also use in this lecture. - -For $t \geq 1$, **real balances** evolve according to +请查阅那篇讲义以获取更多详情和我们在此讲义中也将使用的Python代码。 +对于 $t \geq 1$,**实际余额** 按照以下方式变化 $$ \frac{m_{t+1}}{p_t} - \frac{m_{t}}{p_{t-1}} \frac{p_{t-1}}{p_t} = g $$ -or +或者 $$ b_t - b_{t-1} R_{t-1} = g $$ (eq:up_bmotion) -where +其中 -* $b_t = \frac{m_{t+1}}{p_t}$ is real balances at the end of period $t$ -* $R_{t-1} = \frac{p_{t-1}}{p_t}$ is the gross rate of return on real balances held from $t-1$ to $t$ +* $b_t = \frac{m_{t+1}}{p_t}$ 是第 $t$ 期末的实际余额 +* $R_{t-1} = \frac{p_{t-1}}{p_t}$ 是从 $t-1$ 到 $t$ 期间实际余额的毛收益率 -The demand for real balances is +对实际余额的需求是 $$ -b_t = \gamma_1 - \gamma_2 R_t^{-1} . +b_t = \gamma_1 - \gamma_2 R_t^{-1} . $$ (eq:up_bdemand) -where $\gamma_1 > \gamma_2 > 0$. +其中 $\gamma_1 > \gamma_2 > 0$. -## Monetary-Fiscal Policy +## 货币-财政政策 -To the basic model of {doc}`money_inflation`, we add inflation-indexed one-period government bonds as an additional way for the government to finance government expenditures. +在{doc}`money_inflation`的基本模型上,我们增加了通胀指数化的一期政府债券作为政府筹集财政支出的另一种方式。 -Let $\widetilde R > 1$ be a time-invariant gross real rate of return on government one-period inflation-indexed bonds. +设 $\widetilde R > 1$ 为政府一期通胀指数化债券的恒定名义回报率。 -With this additional source of funds, the government's budget constraint at time $t \geq 0$ is now +有了这个额外的资金来源,政府在时间 $t \geq 0$ 的预算约束现在是 $$ B_t + \frac{m_{t+1}}{p_t} = \widetilde R B_{t-1} + \frac{m_t}{p_t} + g $$ +在时间 $0$ 开始之前,公众拥有 $\check m_0$ 单位的货币(以美元计)和 $\widetilde R \check B_{-1}$ 单位的一期指数化债券(以时间 $0$ 的商品计算);这两个数量是模型外设定的初始条件。 -Just before the beginning of time $0$, the public owns $\check m_0$ units of currency (measured in dollars) -and $\widetilde R \check B_{-1}$ units of one-period indexed bonds (measured in time $0$ goods); these two quantities are initial conditions set outside the model. - -Notice that $\check m_0$ is a *nominal* quantity, being measured in dollars, while -$\widetilde R \check B_{-1}$ is a *real* quantity, being measured in time $0$ goods. +注意 $\check m_0$ 是一个 *名义* 数量,以美元计算,而 $\widetilde R \check B_{-1}$ 是一个 *实际* 数量,以时间 $0$ 的商品计算。 +### 公开市场操作 -### Open market operations - -At time $0$, government can rearrange its portfolio of debts subject to the following constraint (on open-market operations): +在时间 $0$,政府可以重新安排其债务投资组合,并受以下约束(关于公开市场操作): $$ \widetilde R B_{-1} + \frac{m_0}{p_0} = \widetilde R \check B_{-1} + \frac{\check m_0}{p_0} $$ -or +或 $$ B_{-1} - \check B_{-1} = \frac{1}{p_0 \widetilde R} \left( \check m_0 - m_0 \right) $$ (eq:openmarketconstraint) -This equation says that the government (e.g., the central bank) can *decrease* $m_0$ relative to -$\check m_0$ by *increasing* $B_{-1}$ relative to $\check B_{-1}$. +该方程表明,政府(例如中央银行)可以通过*增加* $B_{-1}$ 相对于 $\check B_{-1}$ 来*减少* $m_0$ 相对于 $\check m_0$。 -This is a version of a standard constraint on a central bank's [**open market operations**](https://www.federalreserve.gov/monetarypolicy/openmarket.htm) in which it expands the stock of money by buying government bonds from the public. +这是中央银行[**公开市场操作**](https://www.federalreserve.gov/monetarypolicy/openmarket.htm)的一个标准约束版本,在此操作中,它通过从公众那里购买政府债券来扩大货币供应量。 -## An open market operation at $t=0$ +## 在 $t=0$ 进行公开市场操作 -Following Sargent and Wallace {cite}`sargent1981`, we analyze consequences of a central bank policy that -uses an open market operation to lower the price level in the face of a persistent fiscal -deficit that takes the form of a positive $g$. +遵循 Sargent 和 Wallace {cite}`sargent1981` 的分析,我们研究央行利用公开市场操作在持续的财政赤字情况下降低物价水平的政策后果,这种财政赤字形式为正的 $g$。 -Just before time $0$, the government chooses $(m_0, B_{-1})$ subject to constraint -{eq}`eq:openmarketconstraint`. +在时间 $0$ 之前,政府选择 $(m_0, B_{-1})$,受约束 +{eq}`eq:openmarketconstraint`。 -For $t =0, 1, \ldots, T-1$, +对于 $t =0, 1, \ldots, T-1$, $$ \begin{aligned} -B_t & = \widetilde R B_{t-1} + g \cr +B_t & = \widetilde R B_{t-1} + g \\ m_{t+1} & = m_0 \end{aligned} $$ -while for $t \geq T$, +而对于 $t \geq T$, $$ \begin{aligned} -B_t & = B_{T-1} \cr +B_t & = B_{T-1} \\ m_{t+1} & = m_t + p_t \overline g \end{aligned} $$ -where +其中 $$ -\overline g = \left[(\tilde R -1) B_{T-1} + g \right] +\overline g = \left[(\widetilde R -1) B_{T-1} + g \right] $$ (eq:overlineg) -We want to compute an equilibrium $\{p_t,m_t,b_t, R_t\}_{t=0}$ sequence under this scheme for -running monetary and fiscal policies. - -Here, by **fiscal policy** we mean the collection of actions that determine a sequence of net-of-interest government deficits $\{g_t\}_{t=0}^\infty$ that must be financed by issuing to the public either money or interest bearing bonds. +我们想计算在这一方案下的一个均衡 $\{p_t,m_t,b_t, R_t\}_{t=0}$ 序列,用于执行货币和财政政策。 -By **monetary policy** or **debt-management policy**, we mean the collection of actions that determine how the government divides its portfolio of debts to the public between interest-bearing parts (government bonds) and non-interest-bearing parts (money). +这里,**财政政策** 我们指的是一系列行动,决定一系列净利息政府赤字 $\{g_t\}_{t=0}^\infty$,这必须通过向公众发行货币或有息债券来融资。 -By an **open market operation**, we mean a government monetary policy action in which the government -(or its delegate, say, a central bank) either buys government bonds from the public for newly issued money, or sells bonds to the public and withdraws the money it receives from public circulation. +通过 **货币政策** 或 **债务管理政策**,我们指的是一系列行动,决定政府如何在有息部分(政府债券)和无息部分(货币)之间分配对公众的债务组合。 -## Algorithm (basic idea) +通过一个 **公开市场操作**,我们指的是政府的货币政策行动,其中政府(或其代表,比如中央银行)要么用新发行的货币从公众购买政府债券,要么向公众出售债券并收回其从公众流通中得到的货币。 +## 算法(基本思想) -We work backwards from $t=T$ and first compute $p_T, R_u$ associated with the low-inflation, low-inflation-tax-rate stationary equilibrium in {doc}`money_inflation_nonlinear`. +从 $t=T$ 向后工作,首先计算与低通胀、低通胀税率平稳状态平衡相关的 $p_T, R_u$,如 {doc}`money_inflation_nonlinear` 中所述。 -To start our description of our algorithm, it is useful to recall that a stationary rate of return -on currency $\bar R$ solves the quadratic equation +首先开始我们的算法描述,回忆一下货币利率 $\bar R$ 解决的二次方程很有用 $$ -\gamma_2 + (\gamma_1 + \gamma_2 - \overline g) \bar R - \gamma_1 \bar R^2 = 0 $$ (eq:up_steadyquadratic) -Quadratic equation {eq}`eq:up_steadyquadratic` has two roots, $R_l < R_u < 1$. - -For reasons described at the end of {doc}`money_inflation`, we select the larger root $R_u$. +二次方程 {eq}`eq:up_steadyquadratic` 有两个根,$R_l < R_u < 1$。 +如 {doc}`money_inflation` 末尾描述,我们选择较大的根 $R_u$。 -Next, we compute +接下来,我们计算 $$ \begin{aligned} @@ -184,143 +167,134 @@ p_T & = \frac{m_0}{\gamma_1 - \overline g - \gamma_2 R_u^{-1}} \end{aligned} $$ (eq:LafferTstationary) - -We can compute continuation sequences $\{R_t, b_t\}_{t=T+1}^\infty$ of rates of return and real balances that are associated with an equilibrium by solving equation {eq}`eq:up_bmotion` and {eq}`eq:up_bdemand` sequentially for $t \geq 1$: +我们可以通过连续解方程 {eq}`eq:up_bmotion` 和 {eq}`eq:up_bdemand` 来计算持续序列 $\{R_t, b_t\}_{t=T+1}^\infty$ 的回报率和实际余额,这些回报率和实际余额与一个平衡状态相关,对于 $t \geq 1$: $$ \begin{aligned} b_t & = b_{t-1} R_{t-1} + \overline g \cr R_t^{-1} & = \frac{\gamma_1}{\gamma_2} - \gamma_2^{-1} b_t \cr p_t & = R_t p_{t-1} \cr - m_t & = b_{t-1} p_t +m_t & = b_{t-1} p_t \end{aligned} $$ - -## Before time $T$ +## 在时间 $T$ 之前 -Define +定义 $$ \lambda \equiv \frac{\gamma_2}{\gamma_1}. $$ -Our restrictions that $\gamma_1 > \gamma_2 > 0$ imply that $\lambda \in [0,1)$. +我们的限制 $\gamma_1 > \gamma_2 > 0$ 暗示 $\lambda \in [0,1)$。 -We want to compute +我们想要计算 $$ \begin{aligned} -p_0 & = \gamma_1^{-1} \left[ \sum_{j=0}^\infty \lambda^j m_{j} \right] \cr +p_0 & = \gamma_1^{-1} \left[ \sum_{j=0}^\infty \lambda^j m_{j} \right] \cr & = \gamma_1^{-1} \left[ \sum_{j=0}^{T-1} \lambda^j m_{0} + \sum_{j=T}^\infty \lambda^j m_{1+j} \right] \end{aligned} $$ -Thus, +因此, $$ \begin{aligned} -p_0 & = \gamma_1^{-1} m_0 \left\{ \frac{1 - \lambda^T}{1-\lambda} + \frac{\lambda^T}{R_u-\lambda} \right\} \cr -p_1 & = \gamma_1^{-1} m_0 \left\{ \frac{1 - \lambda^{T-1}}{1-\lambda} + \frac{\lambda^{T-1}}{R_u-\lambda} \right\} \cr -\quad \vdots & \quad \quad \vdots \cr -p_{T-1} & = \gamma_1^{-1} m_0 \left\{ \frac{1 - \lambda}{1-\lambda} + \frac{\lambda}{R_u-\lambda} \right\} \cr +p_0 & = \gamma_1^{-1} m_0 \left\{ \frac{1 - \lambda^T}{1-\lambda} + \frac{\lambda^T}{R_u-\lambda} \right\} \cr +p_1 & = \gamma_1^{-1} m_0 \left\{ \frac{1 - \lambda^{T-1}}{1-\lambda} + \frac{\lambda^{T-1}}{R_u-\lambda} \right\} \cr +\quad \dots & \quad \quad \dots \cr +p_{T-1} & = \gamma_1^{-1} m_0 \left\{ \frac{1 - \lambda}{1-\lambda} + \frac{\lambda}{R_u-\lambda} \right\} \cr p_T & = \gamma_1^{-1} m_0 \left\{\frac{1}{R_u-\lambda} \right\} \end{aligned} $$ (eq:allts) -We can implement the preceding formulas by iterating on +我们可以通过迭代以下公式来实现前述公式: $$ p_t = \gamma_1^{-1} m_0 + \lambda p_{t+1}, \quad t = T-1, T-2, \ldots, 0 $$ -starting from +从 $$ -p_T = \frac{m_0}{\gamma_1 - \overline g - \gamma_2 R_u^{-1}} = \gamma_1^{-1} m_0 \left\{\frac{1}{R_u-\lambda} \right\} +p_T = \frac{m_0}{\gamma_1 - \overline g - \gamma_2 R_u^{-1}} = \gamma_1^{-1} m_0 \left\{\frac{1}{R_u-\lambda} \right\} $$ (eq:pTformula) ```{prf:remark} -We can verify the equivalence of the two formulas on the right sides of {eq}`eq:pTformula` by recalling that -$R_u$ is a root of the quadratic equation {eq}`eq:up_steadyquadratic` that determines steady state rates of return on currency. +可以通过回想 $R_u$ 是二次方程 {eq}`eq:up_steadyquadratic` 的根,该方程确定了货币的稳定状态回报率,从而验证 {eq}`eq:pTformula` 右侧两个公式的等价性。 ``` - -## Algorithm (pseudo code) -Now let's describe a computational algorithm in more detail in the form of a description -that constitutes pseudo code because it approaches a set of instructions we could provide to a -Python coder. +## 算法(伪代码) + +现在我们更详细地以伪代码形式描述一个计算算法,因为它接近我们可以提供给Python编程者的一组指令。 -To compute an equilibrium, we deploy the following algorithm. +为了计算一个均衡,我们使用以下算法。 ```{prf:algorithm} -Given *parameters* include $g, \check m_0, \check B_{-1}, \widetilde R >1, T $. +给定 *参数* 包括 $g, \check m_0, \check B_{-1}, \widetilde R >1, T $。 -We define a mapping from $p_0$ to $\widehat p_0$ as follows. +我们定义一个从 $p_0$ 到 $\widehat p_0$ 的映射,如下。 -* Set $m_0$ and then compute $B_{-1}$ to satisfy the constraint on time $0$ **open market operations** +* 设置 $m_0$,然后计算 $B_{-1}$ 以满足时间 $0$ **公开市场操作的** 约束 $$ -B_{-1}- \check B_{-1} = \frac{\widetilde R}{p_0} \left( \check m_0 - m_0 \right) +B_{-1}- \check B_{-1} = \frac{\widetilde R}{p_0} \left( \check m_0 - m_0 \right) $$ -* Compute $B_{T-1}$ from +* 从以下公式计算 $B_{T-1}$ $$ B_{T-1} = \widetilde R^T B_{-1} + \left( \frac{1 - \widetilde R^T}{1-\widetilde R} \right) g $$ -* Compute +* 计算 $$ -\overline g = g + \left[ \tilde R - 1\right] B_{T-1} +\overline g = g + \left[ \widetilde R - 1 \right] B_{T-1} $$ +* 从公式 {eq}`eq:up_steadyquadratic` 和 {eq}`eq:LafferTstationary` 计算 $R_u, p_T$ +* 从公式 {eq}`eq:allts` 计算新的 $p_0$ 估计值,称为 $\widehat p_0$ -* Compute $R_u, p_T$ from formulas {eq}`eq:up_steadyquadratic` and {eq}`eq:LafferTstationary` above - -* Compute a new estimate of $p_0$, call it $\widehat p_0$, from equation {eq}`eq:allts` above - -* Note that the preceding steps define a mapping +* 注意前面的步骤定义了一个映射 $$ \widehat p_0 = {\mathcal S}(p_0) $$ -* We seek a fixed point of ${\mathcal S}$, i.e., a solution of $p_0 = {\mathcal S}(p_0)$. +* 我们寻找 ${\mathcal S}$ 的不动点,即解 $p_0 = {\mathcal S}(p_0)$。 -* Compute a fixed point by iterating to convergence on the relaxation algorithm +* 通过迭代收敛的松弛算法计算不动点 $$ p_{0,j+1} = (1-\theta) {\mathcal S}(p_{0,j}) + \theta p_{0,j}, $$ -where $\theta \in [0,1)$ is a relaxation parameter. +其中 $\theta \in [0,1)$ 是一个松弛参数。 ``` -## Example Calculations +## 示例计算 -We'll set parameters of the model so that the steady state after time $T$ is initially the same -as in {doc}`money_inflation_nonlinear` +我们将模型参数设置为,在时间 $T$ 后的稳态初始和 {doc}`money_inflation_nonlinear` 中的相同。 -In particular, we set $\gamma_1=100, \gamma_2 =50, g=3.0$. We set $m_0 = 100$ in that lecture, -but now the counterpart will be $M_T$, which is endogenous. +特别是设置 $\gamma_1=100, \gamma_2 =50, g=3.0$。在那次讲座中,我们设置 $m_0 = 100$, +但现在相应的将是 $M_T$,它是内生的。 -As for new parameters, we'll set $\tilde R = 1.01, \check B_{-1} = 0, \check m_0 = 105, T = 5$. +对于新参数,我们将设置 $\tilde R = 1.01, \check B_{-1} = 0, \check m_0 = 105, T = 5$。 -We'll study a "small" open market operation by setting $m_0 = 100$. +我们将通过设置 $m_0 = 100$ 来研究一个“小型”公开市场操作。 -These parameter settings mean that just before time $0$, the "central bank" sells the public bonds in exchange for $\check m_0 - m_0 = 5$ units of currency. +这些参数设置意味着,在时间 $0$ 之前,“中央银行”以 $\check m_0 - m_0 = 5$ 货币单位换取了公众的债券。 -That leaves the public with less currency but more government interest-bearing bonds. +这使得公众持有更少的货币但更多的政府有息债券。 -Since the public has less currency (its supply has diminished) it is plausible to anticipate that the price level at time $0$ will be driven downward. +由于公众持有的货币较少(供应减少),可以合理预见时间 $0$ 的价格水平将受到向下推动。 -But that is not the end of the story, because this **open market operation** at time $0$ has consequences for future settings of $m_{t+1}$ and the gross-of-interest government deficit $\bar g_t$. +但这还不是故事的终点,因为时间 $0$ 的这次**公开市场操作**对未来 $m_{t+1}$ 和名义政府赤字 $\bar g_t$ 的设置产生了影响。 - -Let's start with some imports: +让我们开始一些导入: ```{code-cell} ipython3 import numpy as np @@ -328,10 +302,10 @@ import matplotlib.pyplot as plt from collections import namedtuple ``` -Now let's dive in and implement our pseudo code in Python. +现在让我们开始实现我们的伪代码用 Python。 ```{code-cell} ipython3 -# Create a namedtuple that contains parameters +# 创建一个包含参数的命名元组 MoneySupplyModel = namedtuple("MoneySupplyModel", ["γ1", "γ2", "g", "R_tilde", "m0_check", "Bm1_check", @@ -355,25 +329,25 @@ msm = create_model() ```{code-cell} ipython3 def S(p0, m0, model): - # unpack parameters + # 解包参数 γ1, γ2, g = model.γ1, model.γ2, model.g R_tilde = model.R_tilde m0_check, Bm1_check = model.m0_check, model.Bm1_check T = model.T - # open market operation + # 开放市场操作 Bm1 = 1 / (p0 * R_tilde) * (m0_check - m0) + Bm1_check - # compute B_{T-1} + # 计算 B_{T-1} BTm1 = R_tilde ** T * Bm1 + ((1 - R_tilde ** T) / (1 - R_tilde)) * g - # compute g bar + # 计算 g bar g_bar = g + (R_tilde - 1) * BTm1 - # solve the quadratic equation + # 解二次方程 Ru = np.roots((-γ1, γ1 + γ2 - g_bar, -γ2)).max() - # compute p0 + # 计算 p0 λ = γ2 / γ1 p0_new = (1 / γ1) * m0 * ((1 - λ ** T) / (1 - λ) + λ ** T / (Ru - λ)) @@ -395,15 +369,12 @@ def compute_fixed_point(m0, p0_guess, model, θ=0.5, tol=1e-6): return p0 ``` -Let's look at how price level $p_0$ in the stationary $R_u$ equilibrium depends on the initial -money supply $m_0$. - -Notice that the slope of $p_0$ as a function of $m_0$ is constant. +让我们看看在静态$R_u$均衡中,价格水平$p_0$如何依赖于初始货币供应量$m_0$。 -This outcome indicates that our model verifies a quantity theory of money outcome, -something that Sargent and Wallace {cite}`sargent1981` purposefully built into their model to justify -the adjective *monetarist* in their title. +注意$p_0$作为$m_0$的函数的斜率是恒定的。 +这个结果表明,我们的模型验证了一种货币数量论的结果, +这是Sargent和Wallace {cite}`sargent1981`在其模型中特意构建的,以证明其标题中“货币主义者”这一形容词。 ```{code-cell} ipython3 m0_arr = np.arange(10, 110, 10) @@ -412,27 +383,26 @@ m0_arr = np.arange(10, 110, 10) ```{code-cell} ipython3 plt.plot(m0_arr, [compute_fixed_point(m0, 1, msm) for m0 in m0_arr]) -plt.ylabel('$p_0$') -plt.xlabel('$m_0$') +plt.ylabel('价格水平 $p_0$') +plt.xlabel('初始货币供应量 $m_0$') plt.show() ``` -Now let's write and implement code that lets us experiment with the time $0$ open market operation described earlier. +现在让我们编写并实现代码,以便我们可以试验前面描述的时刻 $0$ 的公开市场操作。 ```{code-cell} ipython3 def simulate(m0, model, length=15, p0_guess=1): - - # unpack parameters + # 解包参数 γ1, γ2, g = model.γ1, model.γ2, model.g R_tilde = model.R_tilde m0_check, Bm1_check = model.m0_check, model.Bm1_check T = model.T # (pt, mt, bt, Rt) - paths = np.empty((4, length)) + 路径 = np.empty((4, length)) - # open market operation + # 开市操作 p0 = compute_fixed_point(m0, 1, model) Bm1 = 1 / (p0 * R_tilde) * (m0_check - m0) + Bm1_check BTm1 = R_tilde ** T * Bm1 + ((1 - R_tilde ** T) / (1 - R_tilde)) * g @@ -442,29 +412,28 @@ def simulate(m0, model, length=15, p0_guess=1): λ = γ2 / γ1 # t = 0 - paths[0, 0] = p0 - paths[1, 0] = m0 + 路径[0, 0] = p0 + 路径[1, 0] = m0 # 1 <= t <= T for t in range(1, T+1, 1): - paths[0, t] = (1 / γ1) * m0 * \ - ((1 - λ ** (T - t)) / (1 - λ) + 路径[0, t] = (1 / γ1) * m0 * ((1 - λ ** (T - t)) / (1 - λ) + (λ ** (T - t) / (Ru - λ))) - paths[1, t] = m0 + 路径[1, t] = m0 # t > T for t in range(T+1, length): - paths[0, t] = paths[0, t-1] / Ru - paths[1, t] = paths[1, t-1] + paths[0, t] * g_bar + 路径[0, t] = 路径[0, t-1] / Ru + 路径[1, t] = 路径[1, t-1] + 路径[0, t] * g_bar # Rt = pt / pt+1 - paths[3, :T] = paths[0, :T] / paths[0, 1:T+1] - paths[3, T:] = Ru + 路径[3, :T] = 路径[0, :T] / 路径[0, 1:T+1] + 路径[3, T:] = Ru # bt = γ1 - γ2 / Rt - paths[2, :] = γ1 - γ2 / paths[3, :] + 路径[2, :] = γ1 - γ2 / 路径[3, :] - return paths + return 路径 ``` ```{code-cell} ipython3 @@ -474,9 +443,9 @@ def plot_path(m0_arr, model, length=15): titles = ['$p_t$', '$m_t$', '$b_t$', '$R_t$'] for m0 in m0_arr: - paths = simulate(m0, model, length=length) + 路径 = simulate(m0, model, length=length) for i, ax in enumerate(axs.flat): - ax.plot(paths[i]) + ax.plot(路径[i]) ax.set_title(titles[i]) axs[0, 1].hlines(model.m0_check, 0, length, color='r', linestyle='--') @@ -488,18 +457,18 @@ def plot_path(m0_arr, model, length=15): --- mystnb: figure: - caption: "Unpleasant Arithmetic" + caption: "不愉快的算术" name: fig:unpl1 --- plot_path([80, 100], msm) ``` -{numref}`fig:unpl1` summarizes outcomes of two experiments that convey messages of Sargent and Wallace {cite}`sargent1981`. +{numref}`fig:unpl1` 总结了两个实验结果,这些结果传达了 Sargent 和 Wallace {cite}`sargent1981` 的信息。 -* An open market operation that reduces the supply of money at time $t=0$ reduces the price level at time $t=0$ +* 在时间 $t=0$ 进行的公开市场操作减少了货币供应,导致当时的价格水平下降 -* The lower is the post-open-market-operation money supply at time $0$, lower is the price level at time $0$. +* 在时间 $0$ 进行的公开市场操作后货币供应量越低,价格水平越低。 -* An open market operation that reduces the post open market operation money supply at time $0$ also *lowers* the rate of return on money $R_u$ at times $t \geq T$ because it brings a higher gross of interest government deficit that must be financed by printing money (i.e., levying an inflation tax) at time $t \geq T$. +* 在时间 $0$ 减少公开市场操作后的货币供应量的公开市场操作,也会*降低*时间 $t \geq T$ 的货币回报率 $R_u$,因为它带来了更高的政府借贷需通过印钞(即征收通货膨胀税)在时间 $t \geq T$ 来融资。 -* $R$ is important in the context of maintaining monetary stability and addressing the consequences of increased inflation due to government deficits. Thus, a larger $R$ might be chosen to mitigate the negative impacts on the real rate of return caused by inflation. +* $R$ 在维持货币稳定和处理政府赤字引起的通货膨胀后果的背景下非常重要。因此,可能会选择较大的 $R$ 来减轻因通货膨胀造成的实际回报率的负面影响。 \ No newline at end of file diff --git a/lectures/zreferences.md b/lectures/zreferences.md index 5f17e89..6e99fb3 100644 --- a/lectures/zreferences.md +++ b/lectures/zreferences.md @@ -10,8 +10,7 @@ kernelspec: --- (references)= -# References +# 参考文献 ```{bibliography} _static/quant-econ.bib ``` -