{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## QZ法の実装\n",
"\n",
"講義ノートの記法を踏襲し, $x^1_t$ を先決変数, $x^2_t$ を非先決変数として, システム変数の分解 $x_t = (x^1_t, x^2_t)$ ができているとする. \n",
"\n",
"システム方程式\n",
"\n",
"\\begin{align}\n",
" Ex_{t+1} = Ax_t + Bu_t\n",
"\\end{align}\n",
"\n",
"の解から爆発的に発散するものを取り除くことで次の方程式を得る.\n",
"\n",
"\\begin{align}\n",
" x^1_{t+1} &= \\Omega_x x_t^1 + \\Omega_u u_t + \\Omega_y y^u_{t+1}\\\\\n",
" x^2_t &= \\Psi_x x_t^1 + \\Psi_y y^u_t\n",
"\\end{align}\n",
"\n",
" これが Blanchard-Kahn (1980) にはじまる合理的期待モデルの分析の味噌である. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ただし, $y^u_t = y^u(u_t, u_{t+1}, \\dots)$ は未来のショックに対する期待・予見を反映して決まる. \n",
"\n",
"以下では Python を使って手を動かしながら, 理論の応用方法を習得してほしい. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 準備\n",
"\n",
"サードパーティライブラリの Numpy と Scipy をインポートしておく. 行列計算のための scipy サブパッケージ scipy.linalg を利用するので, こちらもインポートしておく. \n",
"\n",
"\n",
"import _[LIBRARY]_ as _[ALIAS]_\n",
"
\n",
"\n",
"という構文は頻繁に使うので, 手になじませておくべきだろう. "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import sys # Python のバージョン取得のため\n",
"\n",
"import numpy as np\n",
"import scipy as sp\n",
"import scipy.linalg as la\n",
"import matplotlib.pyplot as plt\n",
"\n",
"%matplotlib inline \n",
"# Jupyter Notebook で図をノート内に表示"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"筆者と読者の環境の違いを把握できるようにバージョンを確認しておこう. "
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.5.1 |Anaconda 4.0.0 (x86_64)| (default, Dec 7 2015, 11:24:55) \n",
"[GCC 4.2.1 (Apple Inc. build 5577)]\n"
]
}
],
"source": [
"print(sys.version)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.10.4\n"
]
}
],
"source": [
"print(np.__version__)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.17.0\n"
]
}
],
"source": [
"print(sp.__version__)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"## QZ 分解\n",
"\n",
"数値計算のための基本となる道具は, ペンシル $(E,A)$ のQZ分解である. 同じサイズをもつ任意の正方行列 $(E, A)$ に対してユニタリ行列 $Q$, $Z$ が存在して, \n",
"\n",
"\\begin{align}\n",
"Q^* E Z = S, \\qquad Q^* A Z = T\n",
"\\end{align}\n",
"\n",
"が上三角行列になる. 特に, $(E,A)$ がレギュラー ($\\exists s\\in\\mathbb{C}; \\det (sE-A) \\neq 0$) であれば, 対角成分 $(s_{ii}, t_{ii})$ が一般固有値に関する情報をもっている. この条件がなければ均衡経路の一意性はまったく期待できない. やや技術的ではあるが, レギュラー性の仮定は以下で $T_{uu}$ が可逆であることをいうのに使っている. 各自確認しほしい. \n",
"\n",
"\n",
"### 例1) QZ分解の例\n",
"\n",
"宿題3 [16EDH3], 問題2の行列ペア\n",
"\n",
"\\begin{align}\n",
"E = \\begin{bmatrix}\n",
" 0 & -1 & 3\\\\\n",
" 0 & 0 & -1\\\\\n",
" 0 & 0 & -1\n",
"\\end{bmatrix}, \n",
"\\qquad\n",
"A = \\begin{bmatrix}\n",
" 2 & 2 & -2\\\\\n",
" -1 & 0 & 0\\\\\n",
" -1 & 0 & -1\n",
"\\end{bmatrix}\n",
"\\end{align}\n",
"\n",
"に対するQZ分解を計算してみよう. \n",
"\n",
"まずは Numpy 配列の生成から. "
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"E = np.array([\n",
" [0, -1, 3],\n",
" [0, 0, -1],\n",
" [0, 0, -1]\n",
" ])\n",
"\n",
"A = np.array([\n",
" [ 2, 2, -2],\n",
" [-1, 0, 0],\n",
" [-1, 0, 1]\n",
" ])"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, -1, 3],\n",
" [ 0, 0, -1],\n",
" [ 0, 0, -1]])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"E"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 2, 2, -2],\n",
" [-1, 0, 0],\n",
" [-1, 0, 1]])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python で QZ分解を実行するには, scipy.linalg の `qz` あるいは `ordqz` を使えばよい. 一般化固有値の並べ替えが必要なので, `ordqz` を使うのがよいだろう. まずはドキュメントを見てみよう. ドキュメントやエラーメッセージの解読はプログラミングの大切な一部なので, 決して避けて通ろうとしないように. "
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"la.ordqz?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`ordqz` には必要な情報のすべては書かれていなかった (例えば `A` と `AA` の関係がこれだけでは分からない). See also の欄にある `qz` のドキュメントも見ておこう. "
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"la.qz?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"これらの内容を合わせると, `ordqz` は\n",
"\n",
" AA, BB, alpha, beta, Q, Z = ordqz(A, B)\n",
" \n",
"の形式で呼び出し, AA が A を上三角化したもの, BB が B を上三角化したものであることが分かる. 分解は `A=Q*AA*Z'`, `B=Q*BB*Z'` が成り立つように行列`Q`, `Z` が作られている. これは我々の記法とほとんど同じであるが, `ordqz` は実QZ分解を行なっているという点で異なっている. その事実は, Parameters の欄の\n",
"\n",
" output : str {'real','complex'}, optional\n",
" Construct the real or complex QZ decomposition for real matrices.\n",
" Default is 'real'.\n",
" \n",
"というオプション引数の説明から分かる. デフォルトは `'real'` であり,あえて複素QZ分解(普通のQZ分解)を出力するには, `ordqz(A, B, output='complex')` の形式で呼び出す必要がある. しかし, 実QZ分解でもまったく問題がないのでこの違いは気にしないでおこう. Klein (2000) は実QZ分解を使った計算の方が高速であると述べているので, わざわざ複素QZ分解を使う理由はない. \n",
"\n",
"さらに,`ordqz` のドキュメントには\n",
"\n",
" On exit, ``(ALPHAR(j) + ALPHAI(j)*i)/BETA(j), j=1,...,N``, will be the\n",
" generalized eigenvalues.\n",
" \n",
"と書かれているので, このQZ分解は\n",
"\n",
"\\begin{align}\n",
" Bx_{t+1} = Ax_t\n",
"\\end{align}\n",
"\n",
"という線形方程式に対応しているものと分かる. したがって, われわれの記号の下では, 次のように呼び出せばよい. "
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/kenjisato/.pyenv/versions/anaconda3-2.4.0/lib/python3.5/site-packages/scipy/linalg/_decomp_qz.py:23: RuntimeWarning: divide by zero encountered in true_divide\n",
" sfunction = lambda x, y: (np.real(x/y) < 0.0)\n",
"/Users/kenjisato/.pyenv/versions/anaconda3-2.4.0/lib/python3.5/site-packages/scipy/linalg/_decomp_qz.py:23: RuntimeWarning: invalid value encountered in true_divide\n",
" sfunction = lambda x, y: (np.real(x/y) < 0.0)\n"
]
}
],
"source": [
"T, S, tii, sii, Q, Z = la.ordqz(A, E)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"divide by zero (ゼロ割) の警告は非正則な E を入れるとでてしまうが, これは安全に無視できる. 煩わしい場合は消すこともできるが, さしあたって問題にならないのでそのままにしておこう. "
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[-2. , 2. , -2. ],\n",
" [ 0. , 1.41421356, -0.70710678],\n",
" [ 0. , 0. , -0.70710678]])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"T"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1. , 0. , 3. ],\n",
" [ 0. , 0. , 1.41421356],\n",
" [ 0. , 0. , 0. ]])"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"S"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([-2.00000000+0.j, 1.41421356+0.j, -0.70710678+0.j])"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tii"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1., 0., 0.])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sii"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$T = Q^* A Z$, $S = Q^* E Z$ が成り立つことを確認しておこう. 数値計算では「完全に一致すること」を期待してはいけない. 十分近くにあるかどうかを確かめる. これは numpy.allclose() で確認できる. \n",
"\n",
"ちなみに, Python では `Q` の共役転置行列は `Q.conj().T` などと書く. "
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.allclose(T, Q.conj().T.dot(A).dot(Z))"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.allclose(S, Q.conj().T.dot(E).dot(Z))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"これで間違いなく望む上三角化できていることが分かった. \n",
"\n",
"\n",
"### 例2) 固有値のソーティング\n",
"\n",
"Klein の方法では左上ブロックに一般化固有値の絶対値が$1$以下のもの, 右下ブロックには絶対値が$1$より大きいものが来るようにしなければならないことを思い出そう. 一般化固有値は `tii / sii` で取り出せるのでこれを見ればよい. "
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/kenjisato/.pyenv/versions/anaconda3-2.4.0/lib/python3.5/site-packages/ipykernel/__main__.py:1: RuntimeWarning: divide by zero encountered in true_divide\n",
" if __name__ == '__main__':\n",
"/Users/kenjisato/.pyenv/versions/anaconda3-2.4.0/lib/python3.5/site-packages/ipykernel/__main__.py:1: RuntimeWarning: invalid value encountered in true_divide\n",
" if __name__ == '__main__':\n"
]
},
{
"data": {
"text/plain": [
"array([ -2. +0.j, inf+nanj, -inf+nanj])"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tii / sii"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"したがって `-2` という有限固有値と, 代数的重複度$2$の無限大固有値が存在することが分かる. すべてが不安定固有値なので, 安定-不安定の分解は自明なものではあるが, うまく行っている. \n",
"\n",
"しかし, これはいつも成り立つ訳ではない. 次の例を見れば分かるように, `ordqz` のデフォルトでは固有値の並びは制御されていない. **以下では次の行列ペアを考えよう**. "
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/kenjisato/.pyenv/versions/anaconda3-2.4.0/lib/python3.5/site-packages/scipy/linalg/_decomp_qz.py:23: RuntimeWarning: divide by zero encountered in true_divide\n",
" sfunction = lambda x, y: (np.real(x/y) < 0.0)\n",
"/Users/kenjisato/.pyenv/versions/anaconda3-2.4.0/lib/python3.5/site-packages/scipy/linalg/_decomp_qz.py:23: RuntimeWarning: invalid value encountered in true_divide\n",
" sfunction = lambda x, y: (np.real(x/y) < 0.0)\n",
"/Users/kenjisato/.pyenv/versions/anaconda3-2.4.0/lib/python3.5/site-packages/ipykernel/__main__.py:11: RuntimeWarning: divide by zero encountered in true_divide\n",
"/Users/kenjisato/.pyenv/versions/anaconda3-2.4.0/lib/python3.5/site-packages/ipykernel/__main__.py:11: RuntimeWarning: invalid value encountered in true_divide\n"
]
},
{
"data": {
"text/plain": [
"array([ 1.16284231 +0.j, 0.90522297 +0.j, inf+nanj,\n",
" 0.95000000 +0.j])"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"E = np.array([[-0.04384211, 0.06263158, 0.04384211, -1.05263158],\n",
" [ 0. , 0. , 0. , 0. ],\n",
" [ 1. , 0. , 0. , 0. ],\n",
" [ 0. , 1. , 0. , 0. ]])\n",
"\n",
"A = np.array([[ 0. , 0. , 0. , -1.05263158],\n",
" [ 0.3 , 1. , -0.72012357, -1. ],\n",
" [ 1.05263158, 0.20877193, 0.14614035, -0.19877193],\n",
" [ 0. , 0.95 , 0. , 0. ]])\n",
"T, S, tii, sii, Q, Z = la.ordqz(A, E)\n",
"tii / sii"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"並び替えは上手くいっていないようだが, どのようにすれば Klein (2000) に準拠した並び替えができるだろうか?\n",
"\n",
"再びドキュメントに戻って Parameters の項目を見てみると, `ordqz` は `sort` パラメータを取れることが分かる."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n", " Parameters\n", " ----------\n", " ...\n", "\n", " sort : {callable, 'lhp', 'rhp', 'iuc', 'ouc'}, optional\n", " Specifies whether the upper eigenvalues should be sorted. A callable\n", " may be passed that, given a eigenvalue, returns a boolean denoting\n", " whether the eigenvalue should be sorted to the top-left (True). For\n", " real matrix pairs, the sort function takes three real arguments\n", " (alphar, alphai, beta). The eigenvalue\n", " ``x = (alphar + alphai*1j)/beta``. For complex matrix pairs or\n", " output='complex', the sort function takes two complex arguments\n", " (alpha, beta). The eigenvalue ``x = (alpha/beta)``.\n", " Alternatively, string parameters may be used:\n", "\n", " - 'lhp' Left-hand plane (x.real < 0.0)\n", " - 'rhp' Right-hand plane (x.real > 0.0)\n", " - 'iuc' Inside the unit circle (x*x.conjugate() < 1.0)\n", " - 'ouc' Outside the unit circle (x*x.conjugate() > 1.0)\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`callable` というのは荒く言えば関数のことで, `callable` をパラメータに取れるということは, 固有値の並び順を決めるソート関数を自由に定義できることを意味している. \n", "\n", "われわれの目的では, `'iuc'` という定義済み順序を使えば十分そうに見えるが, 絶対値が$1$の固有値があるときにはうまくいかないのでソート関数`iouc`を定義しておこう.\n", "\n", "#### 練習問題\n", "\n", "`np.ordqz(A, E, sort='iuc')` の形式で QZ 分解を行うとKleinが指定した固有値の並びと合わなくなる場合がある. 例を1つ探しなさい\n", "\n", "\n", "#### ソート関数 `iouc`" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def iouc(a, b):\n", " \"\"\"inside or on unit circle\"\"\"\n", " return a*a.conjugate() / (b*b) <= 1.0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "実際に確かめてみよう. " ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/kenjisato/.pyenv/versions/anaconda3-2.4.0/lib/python3.5/site-packages/ipykernel/__main__.py:3: RuntimeWarning: divide by zero encountered in true_divide\n", " app.launch_new_instance()\n", "/Users/kenjisato/.pyenv/versions/anaconda3-2.4.0/lib/python3.5/site-packages/ipykernel/__main__.py:3: RuntimeWarning: invalid value encountered in true_divide\n", " app.launch_new_instance()\n" ] }, { "data": { "text/plain": [ "array([ 9.05222973e-01+0.j, 9.50000000e-01+0.j, 1.16284231e+00+0.j,\n", " -8.38612679e+33+0.j])" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "T, S, tii, sii, Q, Z = la.ordqz(A, E, sort=iouc)\n", "eig = tii / sii\n", "eig" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "これで望ましい結果を得ることができた. \n", "\n", "なお, $1$ より小さい絶対値をもつ固有値の数 $n_s$ は次のようにして取得できる. " ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ns = sum(eig*eig.conjugate() <= 1)\n", "ns" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 行列ブロックの表現\n", "\n", "関連する行列のブロック表現を確認しておこう. \n", "\n", "\\begin{align}\n", "Z=\n", " \\begin{bmatrix}\n", " Z_{1s} & Z_{1u}\\\\\n", " Z_{2s} & Z_{2u}\n", " \\end{bmatrix}\n", " \\qquad \n", " Q^* B = C = \n", " \\begin{bmatrix}\n", " C_s \\\\\n", " C_u\n", " \\end{bmatrix}\n", "\\end{align}\n", "\n", "\\begin{align}\n", "T=\n", " \\begin{bmatrix}\n", " T_{ss} & T_{su}\\\\\n", " 0 & T_{uu}\n", " \\end{bmatrix}\n", "\\qquad\n", "S=\n", " \\begin{bmatrix}\n", " S_{ss} & S_{su}\\\\\n", " 0 & S_{uu}\n", " \\end{bmatrix}\n", "\\end{align}\n", "\n", "引き続き先ほどの例2で使った行列ペアを考えよう. $B$ および $C$ 以外はすべて明らかになっている. 例えば, 次のように設定しよう." ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": true }, "outputs": [], "source": [ "B = np.array([[ 0.],\n", " [ 0.],\n", " [ 0.],\n", " [ 1.]])" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[-0.04384211, 0.06263158, 0.04384211, -1.05263158],\n", " [ 0. , 0. , 0. , 0. ],\n", " [ 1. , 0. , 0. , 0. ],\n", " [ 0. , 1. , 0. , 0. ]])" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "E" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 0. , 0. , 0. , -1.05263158],\n", " [ 0.3 , 1. , -0.72012357, -1. ],\n", " [ 1.05263158, 0.20877193, 0.14614035, -0.19877193],\n", " [ 0. , 0.95 , 0. , 0. ]])" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 5.16348163e-01, -3.32227570e-01, 7.89309455e-01,\n", " 2.13689563e-17],\n", " [ -7.18084910e-18, 7.60484364e-18, -1.11023051e-17,\n", " 1.00000000e+00],\n", " [ -8.56378756e-01, -2.00314516e-01, 4.75909152e-01,\n", " 1.36953913e-18],\n", " [ -0.00000000e+00, 9.21682666e-01, 3.87944666e-01,\n", " -2.70217253e-18]])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Q" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[-0.85720237, -0.05533784, 0.47711661, -0.18574595],\n", " [-0. , 0.77048231, -0.15167857, -0.61915315],\n", " [ 0.26010265, 0.4957814 , 0.69839117, 0.44586678],\n", " [-0.44446676, 0.39685708, -0.51147153, 0.61915315]])" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Z" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 0.90609357, -0.29806316, -0.29945132, -0.00879298],\n", " [ 0. , 0.79415423, -0.44674819, -0.24894418],\n", " [-0. , -0. , 0.68995773, -0.92474003],\n", " [-0. , -0. , -0. , -1.61510927]])" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "T" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 1.00096174e+00, -1.30918061e-01, -1.30491351e-01,\n", " -1.83180864e-01],\n", " [ 0.00000000e+00, 8.35951824e-01, -4.14308119e-01,\n", " -3.13245561e-01],\n", " [ -0.00000000e+00, -0.00000000e+00, 5.93337311e-01,\n", " -8.51771348e-01],\n", " [ -0.00000000e+00, -0.00000000e+00, -0.00000000e+00,\n", " 1.92592994e-34]])" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "S" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 0.00000000e+00],\n", " [ 9.21682666e-01],\n", " [ 3.87944666e-01],\n", " [ -2.70217253e-18]])" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "C = (Q.T).dot(B)\n", "C" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "さて, ブロック表現はどのように扱えばよいだろうか. まず, \n", "\n", "$$\n", "n = 4\n", "$$\n", "\n", "および\n", "\n", "$$\n", "n_u = n - n_s = 2\n", "$$\n", "\n", "であることに注意しよう. さらに, \n", "\n", "$$\n", "n_1 = 2, \\qquad n_2 = 2\n", "$$\n", "\n", "であるとしておこう. すべてのブロック行列をいちいち定義する必要はなく, [Numpy のスライスオブジェクト](http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.s_.html)を使えばいい. " ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": true }, "outputs": [], "source": [ "n, n1, n2 = 4, 2, 4\n", "\n", "_1s = np.s_[:n1, :ns]\n", "_2s = np.s_[n1:, :ns]\n", "_1u = np.s_[:n1, ns:]\n", "_2u = np.s_[n1:, ns:]\n", "\n", "_ss = np.s_[:ns, :ns]\n", "_su = np.s_[:ns, ns:]\n", "_uu = np.s_[ns:, ns:]\n", "\n", "_s = np.s_[:ns]\n", "_u = np.s_[ns:]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "例えば $Z_{1s}$, $Z_{2u}$, $T_{ss}$, $T_{uu}$ を取得する場合には次のようにすればよい. " ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[-0.85720237, -0.05533784],\n", " [-0. , 0.77048231]])" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Z[_1s]" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 0.69839117, 0.44586678],\n", " [-0.51147153, 0.61915315]])" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Z[_2u]" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 0.90609357, -0.29806316],\n", " [ 0. , 0.79415423]])" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "T[_ss]" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 0.68995773, -0.92474003],\n", " [-0. , -1.61510927]])" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "T[_uu]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## $y^u$ の計算\n", "\n", "一般の $y^u$ は無限和\n", "\n", "\\begin{align}\n", "y_{t}^{u}=-\\sum_{k=0}^{\\infty}\\left(T_{uu}^{-1}S_{uu}\\right)^{k}T_{uu}^{-1}C_{u}u_{t+k}\n", "\\end{align}\n", "\n", "によって計算されるので, 通常, 正確な評価はできない. しかし,\n", "\n", "- ${u_t}$ が有限列であるケース\n", "- $u_{t+1} = \\Phi u_t$ が成り立つケース\n", "\n", "では計算可能である. 後者は, $t=0$ にショックが起きて, 徐々にショックの影響が小さくなっていく ($|\\Phi|<1$) ような変動のモデルとして扱いやすい. 具体的な計算方法は講義ノートに解説を譲ろう. \n", "\n", "ここでは前者を考える. すなわち, 有限期間でショックの影響が完全になくなるケースである. $t = 0 \\dots T$ のシミュレーションを行うとして, $(u_0, \\dots, u_T)$ から $(y_0^u, \\dots, y_T^u)$ を計算したい. ただし, $u_{T+1}$ 以降 $u$ はゼロであるとする. \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\\begin{align}\n", "y_{t}^{u}=-\\sum_{k=0}^{T-t}\\left(T_{uu}^{-1}S_{uu}\\right)^{k}T_{uu}^{-1}C_{u}u_{t+k}\n", "\\end{align}\n", "\n", "に注意すれば, " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\\begin{align}\n", " y_{T+1}^u = 0\n", "\\end{align}\n", "\n", "および\n", "\n", "\\begin{align}\n", " y_t^u = T_{uu}^{-1}\\left[\n", " S_{uu} y_{t+1}^u - C_u u_t\n", " \\right], \\quad t=0,1,\\dots ,T\n", "\\end{align}\n", "\n", "によって, $y_t^u$ の系列を決定できる. \n", "\n", "適切なサイズのベクトル列 `u` (今の設定では数列) を入力に取り, $y^u$ に相当するベクトル列を吐き出す関数を定義しよう. `u` " ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def yu(u):\n", " \"\"\"Calculate y_t^u\"\"\"\n", " iTuu = la.inv(T[_uu])\n", " y = []\n", " for i in range(len(u)):\n", " try:\n", " yplus = iTuu.dot(S[_uu]).dot(y[i])\n", " except IndexError:\n", " yplus = 0\n", " \n", " u0 = u[-1-i]\n", " y1 = yplus - iTuu.dot(C[_u]).dot(u0)\n", " y.append(y1)\n", " \n", " return np.array(list(reversed(y)))" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[[ 0.00000000e+00],\n", " [ 0.00000000e+00]],\n", "\n", " [[ -1.68681927e+00],\n", " [ -5.01917593e-18]],\n", "\n", " [[ -1.12454618e+00],\n", " [ -3.34611729e-18]],\n", "\n", " [[ -5.62273092e-01],\n", " [ -1.67305864e-18]],\n", "\n", " [[ 0.00000000e+00],\n", " [ 0.00000000e+00]],\n", "\n", " [[ 0.00000000e+00],\n", " [ 0.00000000e+00]],\n", "\n", " [[ 0.00000000e+00],\n", " [ 0.00000000e+00]],\n", "\n", " [[ 0.00000000e+00],\n", " [ 0.00000000e+00]]])" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "u = np.array([0, 3, 2, 1, 0, 0, 0, 0])\n", "y = yu(u)\n", "y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 係数行列の計算\n", "\n", "\\begin{align}\n", "\\Omega_{x} &= Z_{1s}S_{ss}^{-1}T_{ss}Z_{1s}^{-1} \\\\ \n", "%\n", "\\Omega_u &= Z_{1s}S_{ss}^{-1}\\left[C_{s}-(T_{su}-T_{ss}Z_{1s}^{-1}Z_{1u})T_{uu}^{-1}C_{u}\\right] \\\\ \n", "%\n", "\\Omega_y &= (Z_{1u}-Z_{1s}S_{ss}^{-1}S_{su})+Z_{1s}S_{ss}^{-1}(T_{su}-T_{ss}Z_{1s}^{-1}Z_{1u})T_{uu}^{-1}S_{uu}\n", "\\end{align}\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\\begin{align}\n", "\\Psi_x &= Z_{2s}Z_{1s}^{-1} \\\\\n", "\\Psi_y &= Z_{2u}-Z_{2s}Z_{1s}^{-1}Z_{1u}\n", "\\end{align}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "を計算すればよい. " ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [], "source": [ "iSss = la.inv(S[_ss])\n", "iTuu = la.inv(T[_uu])\n", "iZ1s = la.inv(Z[_1s])\n", "\n", "Omega_x = Z[_1s].dot(iSss).dot(T[_ss]).dot(iZ1s)\n", "Omega_u = Z[_1s].dot(iSss).dot(\n", " C[_s] - (T[_su] - T[_ss].dot(iZ1s).dot(Z[_1u]))\\\n", " .dot(iTuu).dot(C[_u]))\n", "Omega_y = Z[_1u] - Z[_1s].dot(iSss).dot(S[_su]) + Z[_1s].dot(iSss).dot(\n", " T[_su] - T[_ss].dot(iZ1s).dot(Z[_1u])).dot(iTuu).dot(S[_uu])\n", "\n", "Psi_x = Z[_2s].dot(iZ1s)\n", "Psi_y = Z[_2u] - Z[_2s].dot(iZ1s).dot(Z[_1u])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## シミュレーション" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false }, "outputs": [], "source": [ "steps = 200\n", "\n", "u = np.zeros(steps)\n", "for i in range(1, 11):\n", " u[i] = 0.01\n", "\n", "y = yu(u)\n", "\n", "# Initial State\n", "x1_0 = np.array([0, 0]).reshape((2,1))\n", "\n", "x1 = [x1_0]\n", "x2 = []\n", "\n", "for i in range(steps):\n", " try:\n", " x1_1 = Omega_x.dot(x1[i]) + Omega_u.dot(u[i]) + Omega_y.dot(y[i+1])\n", " except IndexError:\n", " pass\n", " else:\n", " x1.append(x1_1)\n", " \n", " x2_0 = Psi_x.dot(x1[i]) + Psi_y.dot(y[i])\n", " x2.append(x2_0)\n", " \n", "x1 = np.asarray(x1)\n", "x2 = np.asarray(x2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "このモデル $(E, A, B)$ は Hansen の RBCモデルを特定のパラメータに対して計算した結果である. 同じパラメータのもとで定常状態は次のように計算できる." ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": true }, "outputs": [], "source": [ "cb, kb, nb, zb = (0.5511920622464518, 2.772987427012156, 0.29583592293349903, 1.)" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "