# Linearization {#sec-linearization}
```{r}
#| label: setup-linearization
#| output: false
#| code-fold: true
library(dplyr)
library(ggplot2)
library(patchwork)
mypalette <- c("#0f6a81", "#c2676d", "#995041", "#5c363a", "#d5968c", "#45939c")
# Figures are shown at their natural size, so this is the size the reader
# sees: 10pt as in the PDF body and the CeTZ figures, 9pt for tick and
# legend labels. The font is set for the device in _quarto.yml.
theme_set(
theme_minimal(base_size = 10) +
theme(
panel.grid = element_blank(),
axis.line = element_line(linewidth = 0.3),
axis.ticks = element_line(linewidth = 0.3),
axis.text = element_text(size = rel(0.9)),
legend.text = element_text(size = rel(0.9)),
plot.title = element_text(size = rel(1)),
strip.text = element_text(size = rel(1)),
legend.title = element_blank(),
legend.background = element_blank()
)
)
```
## なぜ近似するのか
この講義で扱う RBC, New Keynesian, DMP のモデルは, いずれも期待を含む非線形の差分方程式の体系として表されます. 前の授業で学んだ Ramsey モデルを例にとりましょう. 代表的家計は
$$
\max_{\{c_t, k_{t+1}\}_{t=0}^{\infty}} \sum_{t=0}^{\infty} \beta^t \frac{c_t^{1-\sigma}}{1-\sigma} \quad \text{s.t.} \quad c_t + k_{t+1} = k_t^{\alpha} + (1-\delta) k_t, \quad k_0 \text{ given}
$$
を解きます. 一階条件 (Euler 方程式) と資源制約から, 均衡は次の2本の式で表されます.
$$
\begin{aligned}
c_t^{-\sigma} &= \beta \left(1 - \delta + \alpha k_{t+1}^{\alpha - 1}\right) c_{t+1}^{-\sigma} \\
k_{t+1} &= k_t^{\alpha} + (1 - \delta) k_t - c_t
\end{aligned}
$${#eq-ramsey}
Euler 方程式の資本の限界生産性は, $t$ 期に選ぶ $k_{t+1}$ で評価される点に注意してください. @eq-ramsey は $(c_t, k_t)$ についての非線形の連立差分方程式で, 一般には解 (資本 $k_t$ から消費 $c_t$ を決める関数, つまり saddle path) を式で書き表すことができません. RBC モデルなどでは, これに確率的なショックと期待が加わり, 変数も増えます.
一方, 定常状態 ($c_t = c_{t+1} = c$, $k_t = k_{t+1} = k$) は解析的に求まります. @eq-ramsey で時間の添字を外すと, Euler 方程式から $1 = \beta \left(1 - \delta + \alpha k^{\alpha - 1}\right)$, 資源制約から $c = k^{\alpha} - \delta k$ が得られるので,
$$
k = \left(\frac{1/\beta - (1-\delta)}{\alpha}\right)^{\frac{1}{\alpha-1}}, \quad c = k^{\alpha} - \delta k
$${#eq-ramsey-ss}
となります.
そこで, 定常状態のまわりでモデルを一次近似 (線形化) し, 線形のモデルとして解くのが標準的な方法です. この章では, まず対数線形化の方法を説明し, 次に Ramsey モデルを線形化して saddle path を求めます. 最後に, 線形化したモデルの解が元の非線形モデルの解とほとんど一致することを数値計算で確かめます.
この章の数値計算では, パラメータを標準的な値 $\alpha = 0.36, \beta = 0.96, \delta = 0.1, \sigma = 1.5$ とします. 定常状態は @eq-ramsey-ss から次のように計算できます.
```{r}
#| label: steady-state
pars <- list(alpha = 0.36, beta = 0.96, delta = 0.1, sigma = 1.5)
k_ss <- function(pars) {
with(pars, ((1 / beta - (1 - delta)) / alpha)^(1 / (alpha - 1)))
}
c_ss <- function(pars) with(pars, k_ss(pars)^alpha - delta * k_ss(pars))
c(k = k_ss(pars), c = c_ss(pars))
```
## 前提知識
対数線形化を理解するために必要な知識は, 対数変化率と1次近似です.
::: {#prp-fact-1 name="対数の近似"}
ある実数 $m$ が0に近い時, 以下の近似が成り立つ.
$$
\log(1 + m) \approx m.
$$
:::
この時, $\hat{x}_t$ を定常状態 $x$ からの対数変化率とすると:
$$
\begin{aligned}
\hat{x}_t &:= \log x_t - \log x \\
&= \log\left(\frac{x_t}{x}\right) \\
&= \log\left(1 + \frac{x_t - x}{x}\right) \\
&\approx \frac{x_t - x}{x} \\
\end{aligned}
$$
この, $\frac{x_t - x}{x}$ という量は, 定常状態 $x$ からの変化率を表す量です. したがって, $\hat{x}_t = 0.01$ であれば, 定常状態 $x$ から1%の変化があったことを意味します. 論文によって, $\hat{x}_t := \log x_t - \log x$ と定義するか, $\hat{x}_t := \frac{x_t - x}{x}$ と定義するかは異なりますが, 近似的にはどちらも同じ意味を持ちます.
```{r}
#| label: fig-hat
#| fig-cap: "定常状態からの乖離の対数近似"
#| fig-notes: "定常状態は $x = 1$."
#| code-fold: true
#| fig-width: 5
#| fig-height: 3.1
x <- 1
xt <- x + seq(-0.05, 0.05, by = 0.01)
bind_rows(
tibble(xt = xt, value = (xt - x) / x, series = "ratio"),
tibble(xt = xt, value = log(xt) - log(x), series = "logdiff")
) |>
ggplot(aes(xt, value, color = series, linetype = series)) +
geom_line(linewidth = 0.4) +
scale_color_manual(
values = c(ratio = mypalette[1], logdiff = mypalette[2]),
breaks = c("ratio", "logdiff"),
labels = expression(frac(x[t] - x, x), log(x[t]) - log(x))
) +
scale_linetype_manual(
values = c(ratio = "solid", logdiff = "dashed"),
breaks = c("ratio", "logdiff"),
labels = expression(frac(x[t] - x, x), log(x[t]) - log(x))
) +
labs(x = expression(x[t]), y = expression(hat(x)[t])) +
theme(
legend.position = "inside",
legend.position.inside = c(0.02, 0.98),
legend.justification.inside = c(0, 1),
legend.key.height = unit(20, "pt")
)
```
@fig-hat にあるように, $\hat{x}_t$ は定常状態 $x$ に十分近ければ, どちらの定義もほとんど同じ値をとることがわかります.
::: {#thm-taylor-approx}
## 1次近似
1変数関数 $f$ の点 $x$ において, 以下が成り立つ.
$$
f(x_t) = f(x) + f'(x)(x_t - x) + R(x_t).
$$
ここで, $R(x_t)$ は高次の剰余項であり, $\lim_{x_t \rightarrow x} \frac{R(x_t)}{ |x_t - x| } = 0$ が成り立つ. したがって, $x$ の近傍では剰余項が十分小さいため, 以下の近似が成り立つ.
$$
f(x_t) \approx f(x) + f'(x)(x_t - x).
$$
多変数関数 $f: \mathbb{R}^n \rightarrow \mathbb{R}$ の場合も同様に近似できる.
$$
f(\mathbf{x}_t) \approx f(\mathbf{x}) + \sum_{i=1}^n \frac{\partial f}{\partial x_i}(\mathbf{x})(x_{t,i} - x_i).
$$
:::
```{r}
#| label: fig-taylor
#| fig-cap: "1次近似の例"
#| fig-notes: "$f(x_t) = x_t^{0.36}$ を点 $x = 1$ のまわりで1次近似したもの. 点は $(x, f(x))$."
#| code-fold: true
#| fig-width: 5
#| fig-height: 3.1
alpha_taylor <- 0.36
f_taylor <- function(x) x^alpha_taylor
# First-order approximation around x = 1: f(1) + f'(1) (x_t - 1)
f_approx <- function(x) 1 + alpha_taylor * (x - 1)
# Relative error of the approximation (%)
err_taylor <- function(x) 100 * (f_approx(x) - f_taylor(x)) / f_taylor(x)
xt <- seq(0.25, 2, by = 0.01)
bind_rows(
tibble(xt = xt, value = f_taylor(xt), series = "f"),
tibble(xt = xt, value = f_approx(xt), series = "approx")
) |>
ggplot(aes(xt, value, color = series, linetype = series)) +
geom_line(linewidth = 0.4) +
annotate("point", x = 1, y = f_taylor(1), size = 1.5) +
scale_color_manual(
values = c(f = mypalette[1], approx = mypalette[2]),
breaks = c("f", "approx"),
labels = expression(f(x[t]), f(x) + f * minute(x) * (x[t] - x))
) +
scale_linetype_manual(
values = c(f = "solid", approx = "dashed"),
breaks = c("f", "approx"),
labels = expression(f(x[t]), f(x) + f * minute(x) * (x[t] - x))
) +
labs(x = expression(x[t]), y = NULL) +
theme(
legend.position = "inside",
legend.position.inside = c(0.02, 0.98),
legend.justification.inside = c(0, 1)
)
```
@fig-taylor は, $f(x_t) = x_t^{0.36}$ を点 $x = 1$ のまわりで1次近似した例です. $x_t$ が $x$ に近いところでは, 1次近似は元の関数とほとんど一致します. 例えば, $x$ から10%離れた $x_t = 1.1$ での近似の誤差は `{r} sprintf("%.2f", err_taylor(1.1))`% ですが, 50%離れた $x_t = 1.5$ では `{r} sprintf("%.2f", err_taylor(1.5))`% に広がります. 景気循環での変数の定常状態からの乖離は数%程度なので (@tbl-stats-data), 1次近似は良い近似になります.
## 対数線形化
::: {#prp-log-linearization}
### 対数線形化の一般形
$y_t = f(\mathbf{x}_t)$ の $\mathbf{x}$ 近傍での線形化は以下のように表される.
$$
\hat{y}_t = \sum_{i=1}^n \frac{\partial f}{\partial x_i}(\mathbf{x})\frac{x_i}{f(\mathbf{x})}\hat{x}_{i, t}.
$$
:::
::: {.proof}
Step 1: 両辺の対数をとる.
$$
\log y_t = \log f(\mathbf{x}_t)
$$
Step 2: 両辺をそれぞれ一次近似する.
$$
\begin{aligned}
LHS &\approx \log y + \underbrace{\frac{1}{y}(y_t - y)}_{\hat{y}_t} \\
RHS &\approx \log f(\mathbf{x}) + \sum_{i=1}^n \frac{\partial f}{\partial x_i}(\mathbf{x})\frac{1}{f(\mathbf{x})}(x_{i, t} - x_i) \\
&= \log f(\mathbf{x}) + \sum_{i=1}^n \frac{\partial f}{\partial x_i}(\mathbf{x})\frac{x_i}{f(\mathbf{x})}\underbrace{\frac{x_{i, t} - x_i}{x_i}}_{\hat{x}_{i, t}}.
\end{aligned}
$$
Step 3: 定常状態 $\log y = \log f(\mathbf{x})$ を両辺から引く.
$$
\hat{y}_t = \sum_{i=1}^n \frac{\partial f}{\partial x_i}(\mathbf{x})\frac{x_i}{f(\mathbf{x})}\hat{x}_{i, t}.
$$
:::
::: {#exm-lin-prod}
## Cobb-Douglas Production Function
$$
Y_t = A_t K_t^{\alpha} L_t^{1 - \alpha} \Rightarrow
\hat{Y}_t \approx \hat{A}_t + \alpha \hat{K}_t + (1 - \alpha) \hat{L}_t.
$$
:::
::: {.proof}
<br>
**Step 1**: Logをとる
$$
\log Y_t = \log A_t + \alpha \log K_t + (1-\alpha)\log L_t
$$
**Step 2**: 線形近似
$$
\begin{aligned}
LHS &= \log Y + \hat{Y_t} \\
RHS &= \log A + \alpha \log K + (1-\alpha)\log L + \hat{A}_t + \alpha \hat{K}_t + (1-\alpha) \hat{L}_t
\end{aligned}
$$
**Step 3**: 定常状態を引く
$$
\hat{Y}_t = \hat{A}_t + \alpha \hat{K}_t + (1 - \alpha) \hat{L}_t
$$
:::
::: {#exm-lin-euler-eq}
## Ramsey Model
$$
\begin{aligned}
&c_t^{-\sigma} = \beta(1-\delta+\alpha k_{t+1}^{\alpha-1})c_{t+1}^{-\sigma} \\
& k_t^\alpha + (1-\delta)k_t = c_t + k_{t+1}
\end{aligned}
\quad\Rightarrow\quad
\begin{aligned}
\hat{c}_{t+1} &= \hat{c}_t + \frac{\beta \alpha (\alpha-1)k^{\alpha-1}}{\sigma} \hat{k}_{t+1} \\
\hat{k}_{t+1} &= -\frac{c}{k} \hat{c}_t + \frac{1}{\beta} \hat{k}_t.
\end{aligned}
$$
:::
::: {.proof}
<br>
**Step 1**: Logをとる
$$
\begin{aligned}
-\sigma \log c_t &= \log \beta + \log(1-\delta+\alpha k_{t+1}^{\alpha-1}) - \sigma \log c_{t+1} \\
\log (k_t^{\alpha} + (1-\delta)k_t) &= \log (c_t + k_{t+1})
\end{aligned}
$$
**Step 2**: 線形近似
$$
\begin{aligned}
LHS_1 &\approx -\sigma \hat{c}_t + LHS_1^*\\
RHS_1 &\approx \frac{\alpha(\alpha-1)k^{\alpha-1}}{1-\delta + \alpha k^{\alpha-1}}\hat{k}_{t+1}
- \sigma \hat{c}_{t+1} + RHS_1^*\\
LHS_2 &\approx \frac{\alpha k^{\alpha-1} + 1 - \delta}{k^{\alpha} + (1-\delta)k} k\hat{k}_t + LHS_2^*\\
RHS_2 &\approx \frac{c}{c + k} \hat{c}_t + \frac{k}{c + k} \hat{k}_{t+1} + RHS_2^*
\end{aligned}
$$
なお, $LHS_1^*, RHS_1^*, LHS_2^*, RHS_2^*$ は定常状態における値 (次のステップで消去される).
**Step 3**: 定常状態を引く
$$
\begin{aligned}
-\sigma \hat{c}_t &= \frac{\alpha(\alpha-1)k^{\alpha-1}}{1-\delta + \alpha k^{\alpha-1}}\hat{k}_{t+1} - \sigma \hat{c}_{t+1} \\
\frac{\alpha k^{\alpha-1} + 1 - \delta}{k^{\alpha} + (1-\delta)k}k \hat{k}_t &= \frac{c}{c + k} \hat{c}_t + \frac{k}{c + k} \hat{k}_{t+1}
\end{aligned}
$$
なお, 定常状態では
$$
\begin{aligned}
c^{-\sigma} &= \beta(1-\delta+\alpha k^{\alpha-1})c^{-\sigma} \\
k^\alpha + (1-\delta)k &= c + k
\end{aligned}
$$
が成り立つため, 整理すると以下を得ます.
$$
\begin{aligned}
\hat{c}_{t+1} &= \hat{c}_t + \frac{\beta \alpha (\alpha-1)k^{\alpha-1}}{\sigma} \hat{k}_{t+1} \\
\hat{k}_{t+1} &= -\frac{c}{k} \hat{c}_t + \frac{1}{\beta} \hat{k}_t.
\end{aligned}
$$
:::
### もう一つの方法
$y_t = f(\mathbf{x}_t)$ の対数線形化は以下のように行うこともできます. この場合は, 各変数 $z_t$ について $\hat{z}_t := \log z_t - \log z$ と定義します.
**Step 1:** $z_t = \exp(\log z_t)$ と置き換える.
$$
\exp(\log y_t) = f(\exp(\log \mathbf{x}_t)).
$$
**Step 2:** $\log z_t$ に対しての線形近似を行う.
$$
\begin{aligned}
LHS &\approx \exp(\log y) + \exp(\log y)(\log y_t - \log y) = y + y \hat{y}_t \\
RHS &\approx f(\exp(\log \mathbf{x})) + \sum_{i=1}^n \frac{\partial f}{\partial \log x_i}(\exp(\log \mathbf{x}))(\log x_{i, t} - \log x_i) \\
&= f(\mathbf{x}) + \sum_{i=1}^n \frac{\partial f}{\partial x_i}(\mathbf{x})\frac{\partial x_i}{\partial \log x_i}(\log x_{i, t} - \log x_i) \\
&= f(\mathbf{x}) + \sum_{i=1}^n \frac{\partial f}{\partial x_i}(\mathbf{x})x_i\hat{x}_{i, t}.
\end{aligned}
$$
**Step 3:** 両辺から定常状態 $y = f(\mathbf{x})$ を引き, 定常状態で割る.
$$
\hat{y}_t = \sum_{i=1}^n \frac{\partial f}{\partial x_i}(\mathbf{x})\frac{x_i}{f(\mathbf{x})}\hat{x}_{i, t}.
$$
よって, この方法でも同じ結果が得られます.
::: {#exm-lin-ramsey-alt}
## Ramsey Model
$$
\begin{aligned}
&c_t^{-\sigma} = \beta(1-\delta+\alpha k_{t+1}^{\alpha-1})c_{t+1}^{-\sigma} \\
& k_t^\alpha + (1-\delta)k_t = c_t + k_{t+1}
\end{aligned}
\quad\Rightarrow\quad
\begin{aligned}
\hat{c}_{t+1} &= \hat{c}_t + \frac{\beta \alpha (\alpha-1)k^{\alpha-1}}{\sigma} \hat{k}_{t+1} \\
\hat{k}_{t+1} &= -\frac{c}{k} \hat{c}_t + \frac{1}{\beta} \hat{k}_t.
\end{aligned}
$$
:::
::: {.proof}
<br>
**Step 1**: 置き換え
$$
\begin{aligned}
&\exp(-\sigma \log c_t) = \beta(1-\delta+\alpha \exp((\alpha-1)\log k_{t+1}))\exp(-\sigma \log c_{t+1}) \\
& \exp(\alpha \log k_t) + (1-\delta)\exp(\log k_t) = \exp(\log c_t) + \exp(\log k_{t+1})
\end{aligned}
$$
**Step 2**: 線形近似
$$
\begin{aligned}
LHS_1 &\approx LHS_1^* + \exp(-\sigma \log c)(-\sigma)(\log c_t - \log c) \\
&= LHS_1^* - \sigma c^{-\sigma} \hat{c}_t \\
RHS_1 &\approx RHS_1^* + \beta \alpha(\alpha-1)\exp(-\sigma \log c)\exp((\alpha-1)\log k)(\log k_{t+1} - \log k) \\
&+\beta (1-\delta+\alpha \exp((\alpha-1)\log k))\exp(-\sigma \log c)(-\sigma)(\log c_{t+1} - \log c) \\
&= RHS_1^* + \beta \alpha (\alpha-1) k^{\alpha-1} c^{-\sigma} \hat{k}_{t+1} - \sigma \beta(1-\delta+\alpha k^{\alpha-1}) c^{-\sigma} \hat{c}_{t+1} \\
LHS_2 &\approx LHS_2^* + (\alpha\exp(\alpha \log k) + (1-\delta)\exp(\log k))(\log k_t - \log k) \\
&= LHS_2^* + \left(\alpha k^{\alpha-1} + 1 - \delta\right) k \hat{k}_t \\
RHS_2 &\approx RHS_2^* + \exp(\log c)(\log c_t - \log c) + \exp(\log k)(\log k_{t+1} - \log k) \\
&= RHS_2^* + c \hat{c}_t + k \hat{k}_{t+1}
\end{aligned}
$$
**Step 3**: 定常状態を引き, 定常状態で割る
$$
\begin{aligned}
\hat{c}_{t+1} &= \hat{c}_t + \frac{\beta \alpha (\alpha-1)k^{\alpha-1}}{\sigma} \hat{k}_{t+1} \\
\hat{k}_{t+1} &= -\frac{c}{k} \hat{c}_t + \frac{1}{\beta} \hat{k}_t.
\end{aligned}
$$
ここで, $\frac{1}{\beta} = 1 - \delta + \alpha k^{\alpha-1}$ を用いた.
:::
## 線形モデル
@exm-lin-euler-eq で求めたように, Ramsey モデルを線形化すると次の2本の式が得られます.
$$
\begin{aligned}
\hat{c}_{t+1} &= \hat{c}_t + \frac{\beta \alpha (\alpha-1)k^{\alpha-1}}{\sigma} \hat{k}_{t+1} \\
\hat{k}_{t+1} &= -\frac{c}{k} \hat{c}_t + \frac{1}{\beta} \hat{k}_t
\end{aligned}
$$
1本目の式の $\hat{k}_{t+1}$ に2本目の式を代入すると, モデルは以下のように書き換えることができます.
$$
\underbrace{\begin{pmatrix}
\hat{c}_{t+1} \\
\hat{k}_{t+1}
\end{pmatrix}}_{\mathbf{z}_{t+1}}
=
\underbrace{\begin{pmatrix}
1 - \frac{\beta \alpha (\alpha-1) k^{\alpha-1}}{\sigma} \frac{c}{k} & \frac{\alpha (\alpha-1) k^{\alpha-1}}{\sigma} \\
-\frac{c}{k} & \frac{1}{\beta}
\end{pmatrix}}_{A}
\underbrace{\begin{pmatrix}
\hat{c}_t \\
\hat{k}_t
\end{pmatrix}}_{\mathbf{z}_t}
$${#eq-ramsey-linear}
```{r}
#| label: matrix-A
A <- with(pars, {
# Coefficient on k_hat_{t+1} in the linearized Euler equation
a <- beta * alpha * (alpha - 1) * k_ss(pars)^(alpha - 1) / sigma
rbind(
c(1 - a * c_ss(pars) / k_ss(pars), a / beta),
c(-c_ss(pars) / k_ss(pars), 1 / beta)
)
})
A
```
### 位相図
線形モデルでは, 初期値 $\mathbf{z}_0 = (\hat{c}_0, \hat{k}_0)^{\top}$ を決めれば, @eq-ramsey-linear を繰り返し使って $\mathbf{z}_1 = A \mathbf{z}_0$, $\mathbf{z}_2 = A \mathbf{z}_1, \dots$ と経路を計算できます. この経路を $(\hat{k}_t, \hat{c}_t)$ 平面に描いたものが位相図 (phase diagram) です. 位相図には, 消費が変化しない点の集合 ($\Delta \hat{c}_t := \hat{c}_{t+1} - \hat{c}_t = 0$) と, 資本が変化しない点の集合 ($\Delta \hat{k}_t := \hat{k}_{t+1} - \hat{k}_t = 0$) もあわせて描きます. 線形化した Euler 方程式から $\Delta \hat{c}_t = 0$ は $\hat{k}_{t+1} = 0$ と同じなので, 線形化した資源制約とあわせると, 2つの集合はそれぞれ次の直線になります.
$$
\Delta \hat{c}_t = 0 \;\Leftrightarrow\; \hat{c}_t = \frac{k}{\beta c} \hat{k}_t, \qquad \Delta \hat{k}_t = 0 \;\Leftrightarrow\; \hat{c}_t = \frac{k}{c} \left(\frac{1}{\beta} - 1\right) \hat{k}_t
$$
ここでは, 定常状態の近くの4つの初期値 A-D から, 10期分の経路を計算します.
```{r}
#| label: phase-sequences
#| code-fold: true
#| output: false
# Paths z_{t+1} = A z_t from four initial values off the saddle path. Values
# are deviations from the steady state in %.
n_steps <- 10
inits <- tibble(
id = c("A", "B", "C", "D"),
c_hat = c(0, -1, 0, 1),
k_hat = c(-1, -1, 1, 1)
)
iterate_A <- function(z0, n_steps) {
path <- matrix(
NA_real_,
n_steps + 1,
2,
dimnames = list(NULL, c("c_hat", "k_hat"))
)
path[1, ] <- z0
for (t in seq_len(n_steps)) {
path[t + 1, ] <- A %*% path[t, ]
}
as_tibble(path) |> mutate(t = 0:n_steps, .before = 1)
}
seqs <- bind_rows(lapply(seq_len(nrow(inits)), \(i) {
iterate_A(c(inits$c_hat[i], inits$k_hat[i]), n_steps) |>
mutate(id = inits$id[i], .before = 1)
}))
# The two stationarity loci of the linear model over the plotted range of k
k_lim <- c(-3, 3)
c_lim <- c(-1.5, 1.5)
slope_dc <- with(pars, k_ss(pars) / (beta * c_ss(pars)))
slope_dk <- with(pars, k_ss(pars) / c_ss(pars) * (1 / beta - 1))
loci <- tibble(
locus = rep(c("dc", "dk"), each = 2),
k_hat = rep(k_lim, 2),
c_hat = c(slope_dc * k_lim, slope_dk * k_lim)
)
```
::: {.content-visible when-format="html"}
@fig-phase-ojs で初期値を選ぶと, そこからの経路が表示されます. スライダーで $t$ を動かすと, 経路を1期ずつたどることができます.
```{r}
#| label: phase-ojs-data
#| echo: false
ojs_define(seq_data = seqs, loci_data = loci)
```
```{ojs}
//| label: ojs-phase-inputs
//| echo: false
// Blank lines inside this cell break the Typst book build, so none here.
// `ojs_define` hands over column-oriented data, so transpose it into rows
seqs = transpose(seq_data)
loci = transpose(loci_data)
starts = seqs.filter(d => d.t === 0)
// Inputs: the initial value and the period shown
viewof init = Inputs.select(starts.map(d => d.id), {
label: tex`(\hat{k}_0, \hat{c}_0) =`,
value: "A",
format: id => {
const d = starts.find(e => e.id === id)
return `(${d.k_hat}%, ${d.c_hat}%)`
}
})
viewof t = Inputs.range([0, 10], {label: "t =", step: 1, value: 0})
// Slopes of the two loci, to place their labels inside the panel
slope_of = name => {
const [p, q] = loci.filter(d => d.locus === name)
return (q.c_hat - p.c_hat) / (q.k_hat - p.k_hat)
}
label_dc = [{k_hat: 1.35 / slope_of("dc"), c_hat: 1.35}]
label_dk = [{k_hat: 2.9, c_hat: 2.9 * slope_of("dk")}]
// The path up to period t, and one arrow per step
path = seqs.filter(d => d.id === init && d.t <= t)
steps = path.slice(1).map((d, i) => ({
x1: path[i].k_hat, y1: path[i].c_hat, x2: d.k_hat, y2: d.c_hat
}))
```
::: {#fig-phase-ojs fig-notes="線形化した Ramsey モデルを, 選んだ初期値から計算したもの. 単位は定常状態からの乖離 (%)."}
```{ojs}
//| label: ojs-phase-plot
//| echo: false
// Blank lines inside this cell break the Typst book build, so none here.
Plot.plot({
style: {fontSize: "16px"},
marginLeft: 60,
marginBottom: 45,
x: {label: "k̂ (%)", domain: [-3, 3], labelOffset: 35},
y: {label: "ĉ (%)", domain: [-1.5, 1.5]},
marks: [
Plot.line(loci, {filter: d => d.locus === "dc", x: "k_hat", y: "c_hat",
stroke: "#0f6a81", clip: true}),
Plot.line(loci, {filter: d => d.locus === "dk", x: "k_hat", y: "c_hat",
stroke: "#c2676d", clip: true}),
Plot.text(label_dc, {x: "k_hat", y: "c_hat",
text: ["Δĉ = 0"], textAnchor: "start", dx: 6, fill: "#0f6a81"}),
Plot.text(label_dk, {x: "k_hat", y: "c_hat",
text: ["Δk̂ = 0"], textAnchor: "end", dy: -10, fill: "#c2676d"}),
Plot.dot([{k_hat: 0, c_hat: 0}], {x: "k_hat", y: "c_hat",
fill: "white", stroke: "black", r: 5}),
Plot.text([{k_hat: 0, c_hat: 0}], {x: "k_hat", y: "c_hat",
text: ["SS"], dx: -14, dy: -10}),
Plot.arrow(steps, {x1: "x1", y1: "y1", x2: "x2", y2: "y2",
stroke: "#995041", headLength: 7, inset: 3}),
Plot.dot(path, {x: "k_hat", y: "c_hat", fill: "#995041", r: 3}),
Plot.text(path.filter(d => d.t === t), {x: "k_hat", y: "c_hat",
text: d => `t=${d.t}`, dy: -12})
]
})
```
4つの初期値からの経路
:::
茶色の矢印が選んだ初期値からの経路で, 点は各期の $(\hat{k}_t, \hat{c}_t)$ です. 青い線は $\Delta \hat{c}_t = 0$, 赤い線は $\Delta \hat{k}_t = 0$ を表します.
:::
::: {.content-visible unless-format="html"}
@fig-phase-sequences は, 4つの初期値 A-D から10期分の経路をたどったものです. 茶色の矢印は1期ごとの動きを表し, 青い線は $\Delta \hat{c}_t = 0$, 赤い線は $\Delta \hat{k}_t = 0$ です.
```{r}
#| label: fig-phase-sequences
#| fig-cap: "4つの初期値からの経路"
#| fig-notes: "線形化した Ramsey モデルを, 初期値 A-D から10期分計算したもの. 単位は定常状態からの乖離 (%)."
#| fig-width: 5.5
#| fig-height: 3.8
# One arrow per step of each path
steps_seq <- seqs |>
mutate(k_end = lead(k_hat), c_end = lead(c_hat), .by = id) |>
filter(!is.na(k_end))
ggplot(mapping = aes(k_hat, c_hat)) +
geom_line(aes(color = locus), data = loci, linewidth = 0.4) +
scale_color_manual(
values = c(dc = mypalette[1], dk = mypalette[2]),
guide = "none"
) +
geom_segment(
aes(xend = k_end, yend = c_end),
data = steps_seq,
color = mypalette[3],
linewidth = 0.4,
arrow = arrow(length = unit(3, "pt"), type = "closed")
) +
geom_text(
aes(label = id),
data = filter(seqs, t == 0),
nudge_y = 0.12,
size = 9,
size.unit = "pt"
) +
annotate(
"text",
x = c_lim[2] / slope_dc,
y = c_lim[2],
label = "Delta * hat(c) == 0",
parse = TRUE,
hjust = -0.1,
vjust = 1,
color = mypalette[1],
size = 9,
size.unit = "pt"
) +
annotate(
"text",
x = k_lim[2],
y = slope_dk * k_lim[2],
label = "Delta * hat(k) == 0",
parse = TRUE,
hjust = 1,
vjust = -0.6,
color = mypalette[2],
size = 9,
size.unit = "pt"
) +
annotate(
"point",
x = 0,
y = 0,
shape = 21,
fill = "white",
color = "black",
size = 2
) +
coord_cartesian(xlim = k_lim, ylim = c_lim) +
labs(x = expression(hat(k)[t] ~ "(%)"), y = expression(hat(c)[t] ~ "(%)"))
```
:::
どの初期値から始めても, 経路は定常状態 (原点) に向かい続けることはなく, やがて離れていきます. $(\hat{k}_0, \hat{c}_0) = (-1\%, -1\%)$ や $(1\%, 1\%)$ から始めた場合のように, いったん定常状態に近づいてから離れていく経路もあります (4つの経路は @fig-ramsey-phase にも A-D として描いています. この2つは B と D です). 定常状態に収束するのは, ある特定の直線の上から始めた場合だけです. この直線が saddle path です.
### Saddle Path と固有ベクトル
saddle path は, 行列 $A$ の固有ベクトルを使って求めることができます. $A$ の固有値を $\lambda_1, \lambda_2$, 対応する固有ベクトルを $\mathbf{v}_1, \mathbf{v}_2$ とします ($A \mathbf{v}_i = \lambda_i \mathbf{v}_i$). 2つの固有値が異なれば $\mathbf{v}_1, \mathbf{v}_2$ は線形独立なので, どんな初期値も $\mathbf{z}_0 = a_1 \mathbf{v}_1 + a_2 \mathbf{v}_2$ と分解できます. $A^t \mathbf{v}_i = \lambda_i^t \mathbf{v}_i$ を使うと,
$$
\mathbf{z}_t = A^t \mathbf{z}_0 = a_1 \lambda_1^t \mathbf{v}_1 + a_2 \lambda_2^t \mathbf{v}_2
$$
となります. ここで $|\lambda_1| > 1 > |\lambda_2|$ とすると, $t \to \infty$ で $\lambda_2^t \to 0$ である一方, $\lambda_1^t$ は発散します. したがって, $\mathbf{z}_t$ が定常状態 $\mathbf{0}$ に収束するのは $a_1 = 0$ のとき, つまり初期値が $\mathbf{v}_2$ の定数倍のときに限られます. $\mathbf{v}_2 = (v_{2,1}, v_{2,2})^{\top}$ と書くと, これは
$$
\hat{c}_0 = \frac{v_{2,1}}{v_{2,2}} \hat{k}_0
$${#eq-saddle-path}
を意味します. $a_1 \neq 0$ のときは, 時間がたつにつれて $\lambda_1^t$ の項が大きくなり, 経路は $\mathbf{v}_1$ の方向に発散していきます. A-D の経路が最後には $\mathbf{v}_1$ の方向の直線 (@fig-ramsey-phase の点線) に沿って離れていくのはこのためです.
資本 $\hat{k}_0$ は前期までの蓄積で決まっている (状態変数) のに対し, 消費 $\hat{c}_0$ は家計がその期に選ぶことができます. 発散する経路は均衡にならないので^[非線形モデルでは, saddle path より上から始めると資本を使い果たし, 下から始めると資本を過剰に蓄積し続けて横断性条件を満たしません.], 家計は @eq-saddle-path を満たすように $\hat{c}_0$ を選びます. つまり saddle path は, 安定な固有値 $\lambda_2$ に対応する固有ベクトル $\mathbf{v}_2$ が張る直線です. saddle path の上では $\mathbf{z}_t = \lambda_2^t \mathbf{z}_0$ なので, 消費と資本は毎期 $\lambda_2$ 倍になり, 同じ率で定常状態に近づきます.
R の `eigen()` で固有値と固有ベクトルを求めてみましょう.
```{r}
#| label: eigen-A
ev <- eigen(A)
ev
```
```{r}
#| label: saddle-path
# `eigen()` orders eigenvalues by decreasing modulus, so pick the stable one
# by its modulus rather than by position.
is_stable <- abs(ev$values) < 1
lambda_s <- ev$values[is_stable]
lambda_u <- ev$values[!is_stable]
saddle <- ev$vectors[, is_stable]
unstable <- ev$vectors[, !is_stable]
saddle_slope <- saddle[1] / saddle[2]
saddle_slope
```
固有値は $\lambda_1 =$ `{r} sprintf("%.4f", lambda_u)`, $\lambda_2 =$ `{r} sprintf("%.4f", lambda_s)` で, $|\lambda_1| > 1 > |\lambda_2|$ を満たしています. したがって saddle path の傾きは $v_{2,1} / v_{2,2} =$ `{r} sprintf("%.3f", saddle_slope)` です. 資本が定常状態より1%多ければ, 消費は定常状態より約 `{r} sprintf("%.2f", saddle_slope)`% 多い水準を選ぶことになります. @fig-ramsey-phase は, これらをまとめて描いた位相図です.
```{r}
#| label: fig-ramsey-phase
#| fig-cap: "線形化した Ramsey モデルの位相図"
#| fig-notes: "単位は定常状態からの乖離 (%). 灰色の線は初期値 A-D からの10期分の経路, 茶色の線は saddle path 上の初期値 ($\\hat{k}_0 = \\pm 1$%) からの経路. 点線は不安定な固有ベクトル $\\mathbf{v}_1$ の方向."
#| code-fold: true
#| fig-width: 5.5
#| fig-height: 3.8
# Two paths that start on the saddle path, with capital 1% above and below
saddle_seqs <- bind_rows(lapply(c(-1, 1), \(k0) {
iterate_A(c(saddle_slope * k0, k0), n_steps) |>
mutate(id = sprintf("%+d", k0), .before = 1)
}))
k_grid <- seq(k_lim[1], k_lim[2], length.out = 2)
lines_phase <- bind_rows(
tibble(k_hat = k_grid, c_hat = slope_dc * k_grid, line = "dc"),
tibble(k_hat = k_grid, c_hat = slope_dk * k_grid, line = "dk"),
tibble(k_hat = k_grid, c_hat = saddle_slope * k_grid, line = "saddle"),
tibble(
k_hat = k_grid,
c_hat = unstable[1] / unstable[2] * k_grid,
line = "unstable"
)
)
ggplot(mapping = aes(k_hat, c_hat)) +
geom_line(
data = filter(lines_phase, line == "unstable"),
color = "gray50",
linetype = "dotted",
linewidth = 0.4
) +
geom_line(
data = filter(lines_phase, line == "dc"),
color = mypalette[1],
linewidth = 0.4
) +
geom_line(
data = filter(lines_phase, line == "dk"),
color = mypalette[2],
linewidth = 0.4
) +
geom_line(
data = filter(lines_phase, line == "saddle"),
color = "black",
linewidth = 0.4
) +
geom_path(aes(group = id), data = seqs, color = "gray60", linewidth = 0.3) +
geom_point(data = seqs, color = "gray60", size = 0.8) +
geom_text(
aes(label = id),
data = filter(seqs, t == 0),
nudge_y = 0.12,
size = 9,
size.unit = "pt"
) +
geom_path(
aes(group = id),
data = saddle_seqs,
color = mypalette[3],
linewidth = 0.6
) +
geom_point(data = saddle_seqs, color = mypalette[3], size = 0.8) +
annotate(
"text",
x = c_lim[2] / slope_dc,
y = c_lim[2],
label = "Delta * hat(c) == 0",
parse = TRUE,
hjust = -0.1,
vjust = 1,
color = mypalette[1],
size = 9,
size.unit = "pt"
) +
annotate(
"text",
x = k_lim[2],
y = slope_dk * k_lim[2],
label = "Delta * hat(k) == 0",
parse = TRUE,
hjust = 1,
vjust = -0.6,
color = mypalette[2],
size = 9,
size.unit = "pt"
) +
annotate(
"text",
x = k_lim[2],
y = saddle_slope * k_lim[2],
label = "Saddle path",
hjust = 1,
vjust = -0.6,
size = 9,
size.unit = "pt"
) +
annotate(
"point",
x = 0,
y = 0,
shape = 21,
fill = "white",
color = "black",
size = 2
) +
coord_cartesian(xlim = k_lim, ylim = c_lim) +
labs(x = expression(hat(k)[t] ~ "(%)"), y = expression(hat(c)[t] ~ "(%)"))
```
### Impulse Response
位相図とは別の見方として, 時間 $t$ を横軸にとり, 各変数の定常状態からの乖離を描くこともできます. ここでは, 資本が定常状態より1%多い状態 ($\hat{k}_0 = 0.01$) から始め, saddle path 上の消費 $\hat{c}_0$ を選んだときの経路を計算します. このように, 定常状態からずれた状態 (あるいはショック) に対して変数がどう反応するかを時間を追って描いたものを, Impulse Response Function (IRF) と呼びます.
```{r}
#| label: model-ramsey-lin
#| code-fold: show
#| output: false
k0_hat <- 0.01
c0_hat <- saddle_slope * k0_hat
n_periods <- 25
lin <- matrix(
0,
n_periods,
2,
dimnames = list(NULL, c("c_hat", "k_hat"))
)
lin[1, ] <- c(c0_hat, k0_hat)
for (t in 2:n_periods) {
lin[t, ] <- A %*% lin[t - 1, ]
}
lin <- as_tibble(lin) |> mutate(t = seq_len(n_periods), .before = 1)
```
```{r}
#| label: fig-ramsey-irf
#| fig-cap: "線形化した Ramsey モデルのIRF"
#| fig-notes: "$\\hat{k}_0 = 0.01$ から始め, saddle path 上の $\\hat{c}_0$ を選んだときの経路."
#| code-fold: true
#| fig-width: 5
#| fig-height: 3.1
irf_long <- bind_rows(
tibble(t = lin$t, value = lin$c_hat, series = "c"),
tibble(t = lin$t, value = lin$k_hat, series = "k")
)
# Labels at the right end of each line instead of a legend. The two lines end
# close together with k above c, so k's label sits above its end and c's below.
irf_labels <- irf_long |>
filter(t == max(t)) |>
mutate(
label = c(c = "hat(c)[t]", k = "hat(k)[t]")[series],
vjust = c(c = 0.8, k = 0.2)[series]
)
ggplot(irf_long, aes(t, value, color = series, linetype = series)) +
geom_line(linewidth = 0.4) +
geom_text(
aes(label = label, vjust = vjust),
data = irf_labels,
parse = TRUE,
hjust = 0,
nudge_x = 0.4,
size = 9,
size.unit = "pt"
) +
scale_color_manual(
values = c(c = mypalette[1], k = mypalette[2]),
guide = "none"
) +
scale_linetype_manual(
values = c(c = "solid", k = "dashed"),
guide = "none"
) +
scale_x_continuous(expand = expansion(mult = c(0.05, 0.1))) +
scale_y_continuous(labels = scales::label_percent(accuracy = 0.01)) +
labs(x = expression(t), y = "Deviation from Steady State")
```
saddle path 上では $\mathbf{z}_t = \lambda_2^t \mathbf{z}_0$ なので, @fig-ramsey-irf の消費と資本はどちらも毎期 $\lambda_2 =$ `{r} sprintf("%.4f", lambda_s)` 倍になり, 同じ率で定常状態に戻ります. 定常状態からの乖離が半分になるまでには約 `{r} sprintf("%.1f", log(0.5) / log(lambda_s))` 期かかります.
## 非線形モデル
非線形モデルは以下のように書き換えることができます. $(c_t, k_t)$ から, まず資源制約で $k_{t+1}$ が決まり, 次に Euler 方程式で $c_{t+1}$ が決まります.
$$
\begin{aligned}
k_{t+1} &= k_t^\alpha + (1-\delta)k_t - c_t \\
c_{t+1} &= (\beta(1-\delta+\alpha k_{t+1}^{\alpha-1}))^{\frac{1}{\sigma}} c_t
\end{aligned}
$${#eq-ramsey-nl}
線形モデルでは固有ベクトルから saddle path が直接求まりましたが, 非線形モデルでは saddle path を式で表すことができません. そこで, 初期資本 $k_0$ に対して saddle path の上にある $c_0$ を数値的に探します. ここではシューティング法 (shooting method) を使います.
シューティング法の考え方は, 位相図 (@fig-ramsey-phase) から分かります. $k_0$ を固定して $c_0$ を選ぶと, @eq-ramsey-nl から $(c_t, k_t)$ の経路がすべて決まります. $c_0$ が saddle path の上の値より高いと, 消費しすぎて資本が減り続け, やがて資本を使い果たします ($k_t < 0$). 逆に低いと, 資本が積み上がり続けます. したがって, 十分長い期間 $T$ の後の資本 $k_T$ は, $c_0$ が saddle path の上の値より低ければ定常状態の資本 $k$ より大きく, 高ければ小さくなります. この大小関係を手がかりに, $c_0$ の区間を半分ずつ狭めていきます.
::: {#alg-shooting name="シューティング法"}
1. 初期資本 $k_0$, 十分長い期間 $T$, 停止条件として十分小さい $\epsilon > 0$ を選ぶ. また, $c_0$ の区間 $[c_L, c_H]$ を, $c_0 = c_L$ からは資本が積み上がり, $c_0 = c_H$ からは資本を使い果たすように選ぶ.
1. $c_M = \frac{c_L + c_H}{2}$ を計算し, $(c_M, k_0)$ から @eq-ramsey-nl を $T$ 期間繰り返して $k_T$ を求める. 途中で $k_t < 0$ となったら, そこで計算をやめる.
1. $k_T > k$ ならば ($c_M$ が低すぎる) $c_L \leftarrow c_M$ とし, そうでなければ ($c_M$ が高すぎる) $c_H \leftarrow c_M$ とする.
1. $c_H - c_L < \epsilon$ ならば, $\frac{c_L + c_H}{2}$ を $c_0$ として返す. そうでなければ2に戻る.
:::
ステップ2から4は, $c_0$ の関数 $k_T - k$ の根を二分法 (@alg-bisection) で求めることにあたります. ここでは $T = 100$, 最初の区間を定常状態の消費 $c$ の $\pm 1\%$, $\epsilon = 10^{-12}$ とします.
```{r}
#| label: model-ramsey-nl
#| code-fold: show
#| output: false
step_ramsey <- function(c_t, k_t, pars) {
with(pars, {
k_next <- k_t^alpha + (1 - delta) * k_t - c_t
c_next <- (beta * (1 - delta + alpha * k_next^(alpha - 1)))^(1 / sigma) *
c_t
c(c_next, k_next)
})
}
# Path from (c0, k0) for n_periods. Once capital is exhausted (k_t < 0), the
# rest of the path is left as NA.
shoot_path <- function(c0, k0, pars, n_periods = 100) {
out <- matrix(
NA_real_,
n_periods + 1,
2,
dimnames = list(NULL, c("c", "k"))
)
out[1, ] <- c(c0, k0)
for (t in seq_len(n_periods)) {
out[t + 1, ] <- step_ramsey(out[t, "c"], out[t, "k"], pars)
if (out[t + 1, "k"] < 0) {
break
}
}
as_tibble(out) |> mutate(t = 0:n_periods, .before = 1)
}
# Shooting method by bisection on c0, recording every iteration
shooting <- function(
k0,
pars,
n_periods = 100,
bracket = c(1 - 0.01, 1 + 0.01) * c_ss(pars),
tol = 1e-12
) {
c_lo <- bracket[1]
c_hi <- bracket[2]
iterations <- list()
while (c_hi - c_lo > tol) {
c_mid <- (c_lo + c_hi) / 2
k_T <- tail(shoot_path(c_mid, k0, pars, n_periods)$k, 1)
# NA means capital was exhausted, so c_mid is too high
too_low <- !is.na(k_T) && k_T > k_ss(pars)
iterations[[length(iterations) + 1]] <- tibble(
iter = length(iterations) + 1,
c_lo = c_lo,
c_hi = c_hi,
c_mid = c_mid,
too_low = too_low
)
if (too_low) {
c_lo <- c_mid
} else {
c_hi <- c_mid
}
}
list(c0 = (c_lo + c_hi) / 2, iterations = bind_rows(iterations))
}
nl_path <- function(k0, pars, n_periods = 100) {
c0 <- shooting(k0, pars, n_periods)$c0
out <- matrix(0, n_periods, 2, dimnames = list(NULL, c("c", "k")))
out[1, ] <- c(c0, k0)
for (t in 2:n_periods) {
out[t, ] <- step_ramsey(out[t - 1, "c"], out[t - 1, "k"], pars)
}
as_tibble(out) |> mutate(t = seq_len(n_periods), .before = 1)
}
nl_left <- nl_path((1 - 0.01) * k_ss(pars), pars)
nl_right <- nl_path((1 + 0.01) * k_ss(pars), pars)
nl <- nl_right |>
mutate(c_hat = c / c_ss(pars) - 1, k_hat = k / k_ss(pars) - 1)
```
```{r}
#| label: shooting-iterations
#| code-fold: true
#| output: false
# Every iteration of the shooting method from capital 1% above the steady
# state: the trial path of each iteration and the bracket, in % deviations
k0_shoot <- (1 + 0.01) * k_ss(pars)
sol_shoot <- shooting(k0_shoot, pars)
n_show <- nrow(sol_shoot$iterations)
to_pct <- function(x, ss) 100 * (x / ss - 1)
shoot_brackets <- sol_shoot$iterations |>
mutate(across(c(c_lo, c_hi, c_mid), \(x) to_pct(x, c_ss(pars))))
shoot_paths <- bind_rows(lapply(seq_len(n_show), \(i) {
shoot_path(sol_shoot$iterations$c_mid[i], k0_shoot, pars) |>
filter(!is.na(c)) |>
mutate(iter = i, .before = 1)
})) |>
mutate(k_hat = to_pct(k, k_ss(pars)), c_hat = to_pct(c, c_ss(pars)))
shoot_solution <- shoot_path(sol_shoot$c0, k0_shoot, pars) |>
mutate(k_hat = to_pct(k, k_ss(pars)), c_hat = to_pct(c, c_ss(pars)))
```
::: {.content-visible when-format="html"}
@fig-shooting-ojs は, 資本が定常状態より1%多い状態から, @alg-shooting が収束するまでの `{r} n_show` 回の反復を示したものです. スライダーで反復の回数を動かすと, その回に試した $c_M$ からの経路と, 区間 $[c_L, c_H]$ が表示されます.
```{r}
#| label: shooting-ojs-data
#| echo: false
ojs_define(
shoot_paths_data = shoot_paths,
shoot_brackets_data = shoot_brackets,
shoot_solution_data = shoot_solution
)
```
```{ojs}
//| label: ojs-shooting-inputs
//| echo: false
// Blank lines inside this cell break the Typst book build, so none here.
// `ojs_define` hands over column-oriented data, so transpose it into rows
trials = transpose(shoot_paths_data)
brackets = transpose(shoot_brackets_data)
solution = transpose(shoot_solution_data)
viewof iter = Inputs.range([1, brackets.length], {
label: "Iteration", step: 1, value: 1
})
current = brackets.find(d => d.iter === iter)
trial = trials.filter(d => d.iter === iter)
past = trials.filter(d => d.iter < iter)
// Red when c_M is too high (capital runs out), teal when too low
trial_color = current.too_low ? "#0f6a81" : "#c2676d"
// Status: c_M in the color of its path, and the bracket width to two
// significant digits, written as m × 10^e once it is below 0.01%
fmt_mid = x => (x >= 0 ? "+" : "") + x.toFixed(4) + "%"
fmt_width = w => {
if (w >= 0.01) return html`<span>${w.toPrecision(2)}%</span>`
const [m, e] = w.toExponential(1).split("e")
return html`<span>${m} × 10<sup>${Number(e)}</sup>%</span>`
}
status = html`<div style="display: flex; gap: 2.5em; margin: 0.5em 0;">
<span>${tex`c_M`}: <b style="color: ${trial_color};">
${fmt_mid(current.c_mid)}</b></span>
<span>${tex`c_H - c_L`}: <b>${fmt_width(current.c_hi - current.c_lo)}</b>
</span></div>`
plot_time = Plot.plot({
width: 340,
height: 280,
style: {fontSize: "13px"},
marginLeft: 45,
x: {label: "t", domain: [0, 100]},
y: {label: "k̂ (%)", domain: [-3, 3]},
marks: [
Plot.ruleY([0], {stroke: "#5c363a", strokeDasharray: "4,3"}),
Plot.line(past, {x: "t", y: "k_hat", z: "iter", stroke: "#bbb",
clip: true}),
Plot.line(solution, {x: "t", y: "k_hat", stroke: "black",
strokeDasharray: "2,2", clip: true}),
Plot.line(trial, {x: "t", y: "k_hat", stroke: trial_color,
strokeWidth: 2, clip: true})
]
})
plot_phase = Plot.plot({
width: 340,
height: 280,
style: {fontSize: "13px"},
marginLeft: 45,
x: {label: "k̂ (%)", domain: [-3, 3]},
y: {label: "ĉ (%)", domain: [-1.5, 1.5]},
marks: [
Plot.line(past, {x: "k_hat", y: "c_hat", z: "iter", stroke: "#bbb",
clip: true}),
Plot.line(solution, {x: "k_hat", y: "c_hat", stroke: "black",
strokeDasharray: "2,2", clip: true}),
Plot.line(trial, {x: "k_hat", y: "c_hat", stroke: trial_color,
strokeWidth: 2, clip: true}),
// The bracket [c_L, c_H] at k_hat_0 = 1% and the midpoint c_M
Plot.ruleX([1], {y1: current.c_lo, y2: current.c_hi, stroke: "#995041",
strokeWidth: 5, strokeOpacity: 0.5}),
Plot.dot([{k_hat: 1, c_hat: current.c_mid}], {x: "k_hat", y: "c_hat",
fill: trial_color, r: 4}),
Plot.dot([{k_hat: 0, c_hat: 0}], {x: "k_hat", y: "c_hat",
fill: "white", stroke: "black", r: 4}),
Plot.text([{k_hat: 0, c_hat: 0}], {x: "k_hat", y: "c_hat",
text: ["SS"], dx: -12, dy: -10})
]
})
```
::: {#fig-shooting-ojs fig-notes="資本が定常状態より1%多い状態から計算したもの. 単位は定常状態からの乖離 (%)."}
```{ojs}
//| label: ojs-shooting-plot
//| echo: false
// Blank lines inside this cell break the Typst book build, so none here.
html`<div>${status}
<div style="display: flex; flex-wrap: wrap; gap: 12px;">
${plot_time}${plot_phase}</div></div>`
```
シューティング法の反復
:::
左の図は資本 $\hat{k}_t$ の経路, 右の図は位相図です. 太い線がその回に試した $c_M$ からの経路で, 低すぎるときは青, 高すぎるときは赤で表しています. 灰色の線はそれまでの反復の経路, 黒い点線は収束した後の経路です. 右の図の $\hat{k}_0 = 1\%$ の位置にある縦の帯が区間 $[c_L, c_H]$ で, 反復のたびに半分に狭まります. 反復が進むと区間は図では見えないほど狭くなりますが, 試した経路が収束した後の経路の近くにとどまる期間がしだいに長くなり, やがて100期の間ずっとほとんど重なるようになります (左の図).
:::
::: {.content-visible unless-format="html"}
@fig-shooting-static は, 資本が定常状態より1%多い状態から, @alg-shooting が収束するまでの `{r} n_show` 回の反復で試した $c_M$ からの資本の経路です. $c_M$ が低すぎると資本が積み上がり (青), 高すぎると資本を使い果たします (赤). 反復が進むにつれて, 試した経路が収束した後の経路 (破線) から離れる時点がしだいに遅くなり, やがて100期の間ずっとほとんど重なるようになります.
```{r}
#| label: fig-shooting-static
#| fig-cap: "シューティング法の反復"
#| fig-notes: "資本が定常状態より1%多い状態から, 収束するまでのすべての反復で試した $c_M$ からの資本の経路. 破線は収束した後の経路. 単位は定常状態からの乖離 (%)."
#| fig-width: 5
#| fig-height: 3.1
shoot_paths |>
left_join(select(shoot_brackets, iter, too_low), by = "iter") |>
mutate(guess = if_else(too_low, "c_M too low", "c_M too high")) |>
ggplot(aes(t, k_hat)) +
geom_hline(yintercept = 0, color = mypalette[4], linetype = "dashed") +
geom_line(aes(group = iter, color = guess), linewidth = 0.4) +
geom_line(data = shoot_solution, linetype = "dashed", linewidth = 0.4) +
scale_color_manual(
values = c("c_M too low" = mypalette[1], "c_M too high" = mypalette[2]),
breaks = c("c_M too low", "c_M too high"),
labels = expression(c[M] ~ "too low", c[M] ~ "too high")
) +
coord_cartesian(ylim = c(-3, 3)) +
labs(x = expression(t), y = expression(hat(k)[t] ~ "(%)")) +
theme(
legend.position = "inside",
legend.position.inside = c(0.02, 0.02),
legend.justification.inside = c(0, 0)
)
```
:::
@alg-shooting は `{r} nrow(sol_shoot$iterations)` 回の反復で停止し, $c_0$ は定常状態の消費より `{r} sprintf("%.4f", to_pct(sol_shoot$c0, c_ss(pars)))`% 多い値になりました. 線形モデルの saddle path から求めた値 (`{r} sprintf("%.4f", 100 * c0_hat)`%) とほとんど同じです.
```{r}
#| label: fig-ramsey-nl
#| fig-cap: "非線形の Ramsey モデルと線形化したモデル"
#| fig-notes: "左は資本が定常状態より1%大きい状態からのインパルス応答. 右は定常状態より1%小さい状態と1%大きい状態からの経路."
#| code-fold: true
#| fig-width: 6.25
#| fig-height: 3
# Impulse Response. The two paths nearly coincide, so the nonlinear one is
# drawn wide and pale and the linear one thin on top of it.
irf_long <- bind_rows(
tibble(t = lin$t, value = lin$c_hat, var = "c", model = "Linear"),
tibble(t = lin$t, value = lin$k_hat, var = "k", model = "Linear"),
tibble(t = nl$t, value = nl$c_hat, var = "c", model = "Nonlinear"),
tibble(t = nl$t, value = nl$k_hat, var = "k", model = "Nonlinear")
) |>
filter(t <= n_periods)
p1 <- ggplot(irf_long, aes(t, value, color = var)) +
geom_line(
data = \(d) filter(d, model == "Nonlinear"),
aes(linetype = model),
linewidth = 1.2,
alpha = 0.4
) +
geom_line(
data = \(d) filter(d, model == "Linear"),
aes(linetype = model),
linewidth = 0.4
) +
scale_color_manual(
values = c(c = mypalette[1], k = mypalette[2]),
breaks = c("c", "k"),
labels = expression(hat(c)[t], hat(k)[t])
) +
scale_linetype_manual(
values = c(Linear = "dashed", Nonlinear = "solid"),
breaks = c("Linear", "Nonlinear")
) +
scale_y_continuous(labels = scales::label_percent(accuracy = 0.01)) +
labs(
title = "Impulse Response",
x = expression(t),
y = "Deviation from Steady State"
) +
theme(legend.position = "bottom")
# Phase Diagram: the saddle path drawn against the two stationarity loci
k_hat_grid <- seq(-0.01, 0.01, by = 0.0001)
phase <- tibble(
k = k_ss(pars) * (1 + k_hat_grid),
c_saddle = c_ss(pars) * (1 + k_hat_grid * saddle_slope),
c_kdot = k^pars$alpha - pars$delta * k,
# c_{t+1} = c_t iff k_{t+1} = k_ss, so c_t = k^alpha + (1 - delta) k - k_ss
c_cdot = k^pars$alpha + (1 - pars$delta) * k - k_ss(pars)
)
phase_base <- function(df) {
# Where the c-dot locus leaves the top of the panel, to place its label
k_top <- with(df, k[which.min(abs(c_cdot - max(c_saddle)))])
ggplot(df, aes(k)) +
geom_line(aes(y = c_cdot), color = mypalette[1], linewidth = 0.4) +
geom_line(aes(y = c_kdot), color = mypalette[2], linewidth = 0.4) +
annotate(
"text",
x = k_top * 1.0005,
y = max(df$c_saddle),
label = "dot(c) == 0",
parse = TRUE,
hjust = 0,
size = 9,
size.unit = "pt"
) +
annotate(
"text",
x = max(df$k) * 0.999,
y = max(df$c_kdot) * 0.9995,
label = "dot(k) == 0",
parse = TRUE,
hjust = 1,
size = 9,
size.unit = "pt"
) +
annotate(
"point",
x = k_ss(pars),
y = c_ss(pars),
shape = 21,
fill = "white",
color = "black",
size = 2
) +
annotate(
"text",
x = k_ss(pars) * 0.999,
y = c_ss(pars) * 1.0006,
label = "SS",
hjust = 1,
size = 9,
size.unit = "pt"
) +
coord_cartesian(ylim = range(df$c_saddle)) +
labs(title = "Phase Diagram", x = expression(k[t]), y = expression(c[t]))
}
# Phase Diagram: linear saddle path against the nonlinear one
nl_phase <- bind_rows(nl_left, arrange(nl_right, desc(t)))
p2 <- phase_base(phase) +
geom_line(
data = nl_phase,
aes(x = k, y = c, color = "Nonlinear"),
linewidth = 1.2,
alpha = 0.4
) +
geom_line(
aes(y = c_saddle, color = "Linear"),
linetype = "dashed",
linewidth = 0.4
) +
scale_color_manual(
values = c(Linear = "black", Nonlinear = mypalette[3]),
breaks = c("Linear", "Nonlinear")
) +
theme(legend.position = "bottom")
p1 | p2
```
@fig-ramsey-nl に示すように, 線形化モデルと非線形モデルのIRFやSaddle Pathはほとんど一致していることがわかります.