From 822c07a3680150de5836d5fd0871495d38030c0d Mon Sep 17 00:00:00 2001 From: Tetsu Haruyama Date: Sat, 10 Aug 2024 13:18:45 +0900 Subject: [PATCH] x y in plot + 1.21 legacy --- 13_random_TFP.ipynb | 63 ++++++++++++++++++----------- 14_linearsolve.ipynb | 2 +- 15_RBC.ipynb | 90 ++++++++++++++++++++++++++---------------- 16_data_on_money.ipynb | 25 +++++++----- 4 files changed, 113 insertions(+), 67 deletions(-) diff --git a/13_random_TFP.ipynb b/13_random_TFP.ipynb index 36d329e0..13e7a21f 100644 --- a/13_random_TFP.ipynb +++ b/13_random_TFP.ipynb @@ -29,7 +29,7 @@ "import statsmodels.formula.api as smf\n", "\n", "# numpy v1の表示を使用\n", - "np.set_printoptions(legacy='1.25')\n", + "np.set_printoptions(legacy='1.21')\n", "# 警告メッセージを非表示\n", "import warnings\n", "warnings.filterwarnings(\"ignore\")" @@ -37,7 +37,9 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "jp-MarkdownHeadingCollapsed": true + }, "source": [ "## はじめに" ] @@ -101,7 +103,8 @@ { "cell_type": "markdown", "metadata": { - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### ホワイト・ノイズ" @@ -180,7 +183,8 @@ { "cell_type": "markdown", "metadata": { - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "#### 図示" @@ -279,7 +283,8 @@ { "cell_type": "markdown", "metadata": { - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "#### 自己共分散(自己相関)" @@ -430,7 +435,8 @@ "cell_type": "markdown", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### 自己回帰モデル:AR(1)" @@ -440,7 +446,8 @@ "cell_type": "markdown", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "#### 説明" @@ -470,7 +477,8 @@ "cell_type": "markdown", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "#### 3つの例:持続性の違い" @@ -622,7 +630,8 @@ "cell_type": "markdown", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### 説明" @@ -647,7 +656,8 @@ "cell_type": "markdown", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### データ" @@ -696,7 +706,8 @@ "cell_type": "markdown", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### 労働時間と雇用の特徴" @@ -757,7 +768,7 @@ }, "outputs": [], "source": [ - "df[['hours','hours_trend']].plot()\n", + "df.plot(y=['hours','hours_trend'])\n", "pass" ] }, @@ -778,7 +789,7 @@ }, "outputs": [], "source": [ - "df[['employed','employed_trend']].plot()\n", + "df.plot(y=['employed','employed_trend'])\n", "pass" ] }, @@ -799,7 +810,7 @@ }, "outputs": [], "source": [ - "df[['total_hours','total_hours_trend']].plot()\n", + "df.plot(y=['total_hours','total_hours_trend'])\n", "pass" ] }, @@ -822,7 +833,7 @@ }, "outputs": [], "source": [ - "ax_ = df[['hours_cycle','employed_cycle']].plot()\n", + "ax_ = df.plot(y=['hours_cycle','employed_cycle'])\n", "ax_.axhline(0, c='red', lw=0.5)\n", "pass" ] @@ -911,7 +922,8 @@ "cell_type": "markdown", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### TFPの特徴" @@ -955,7 +967,7 @@ "df['tfp_cycle'] = df['tfp_log'] - df['tfp_log_trend']\n", "\n", "# 全要素生産性とトレンドのプロット\n", - "ax = df[['tfp_log','tfp_log_trend']].plot()\n", + "ax = df.plot(y=['tfp_log','tfp_log_trend'])\n", "ax.set_title('全要素生産性とトレンド(対数)', size=20)\n", "pass" ] @@ -977,7 +989,7 @@ }, "outputs": [], "source": [ - "ax = (100 * df['tfp_cycle']).plot()\n", + "ax = ( 100 * df['tfp_cycle'] ).plot()\n", "ax.axhline(0, c='red')\n", "ax.set_title('全要素生産性の変動(%)', size=20)\n", "pass" @@ -1022,7 +1034,7 @@ }, "outputs": [], "source": [ - "ax_ = (100 * df[['gdp_cycle','tfp_cycle']]).plot()\n", + "ax_ = ( 100 * df[['gdp_cycle','tfp_cycle']] ).plot()\n", "ax_.set_title('GDPとTFPの変動(%)',size=20)\n", "pass" ] @@ -1102,7 +1114,8 @@ "cell_type": "markdown", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### AR(1)としてのTFP" @@ -1296,8 +1309,7 @@ { "cell_type": "markdown", "metadata": { - "heading_collapsed": true, - "jp-MarkdownHeadingCollapsed": true + "heading_collapsed": true }, "source": [ "### 説明" @@ -1592,7 +1604,7 @@ "metadata": {}, "outputs": [], "source": [ - "s = (df['investment']/df['gdp']).mean()\n", + "s = ( df['investment']/df['gdp'] ).mean()\n", "s" ] }, @@ -1781,7 +1793,7 @@ }, "outputs": [], "source": [ - "np.log(df_sim['Y']).plot(title='シミュレーション:Yとトレンド(対数)')\n", + "np.log( df_sim['Y'] ).plot(title='シミュレーション:Yとトレンド(対数)')\n", "df_sim['Y_log_trend'].plot()\n", "pass" ] @@ -1857,6 +1869,7 @@ "print('\\n--- シミュレーション:変動の自己相関係数 ---\\n')\n", "\n", "for v in var_list:\n", + " \n", " ac = df_sim[f'{v}の変動'].autocorr()\n", " print(f'{v}の変動: {ac:.3f}')" ] @@ -1892,6 +1905,7 @@ "data_var_list = ['consumption','investment','capital']\n", "\n", "for v in data_var_list:\n", + " \n", " df[f'{v}_cycle'] = np.log( df[v]/py4macro.trend(df[v]) )" ] }, @@ -1926,6 +1940,7 @@ "print('\\n--- データ:変動の自己相関係数 ---\\n')\n", "\n", "for v in data_var_list:\n", + " \n", " ac = df[f'{v}_cycle'].autocorr()\n", " print(f'{v:>11}の変動: {ac:>5.3f}') # 1" ] diff --git a/14_linearsolve.ipynb b/14_linearsolve.ipynb index 84b4af98..fb4ed658 100644 --- a/14_linearsolve.ipynb +++ b/14_linearsolve.ipynb @@ -27,7 +27,7 @@ "import pandas as pd\n", "\n", "# numpy v1の表示を使用\n", - "np.set_printoptions(legacy='1.25')\n", + "np.set_printoptions(legacy='1.21')\n", "# 警告メッセージを非表示\n", "import warnings\n", "warnings.filterwarnings(\"ignore\")" diff --git a/15_RBC.ipynb b/15_RBC.ipynb index 310f5d9b..d59dc50c 100644 --- a/15_RBC.ipynb +++ b/15_RBC.ipynb @@ -28,7 +28,7 @@ "import pandas as pd\n", "\n", "# numpy v1の表示を使用\n", - "np.set_printoptions(legacy='1.25')\n", + "np.set_printoptions(legacy='1.21')\n", "# 警告メッセージを非表示\n", "import warnings\n", "warnings.filterwarnings(\"ignore\")" @@ -37,7 +37,8 @@ { "cell_type": "markdown", "metadata": { - "heading_collapsed": true + "heading_collapsed": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "## 説明" @@ -73,7 +74,9 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "jp-MarkdownHeadingCollapsed": true + }, "source": [ "## 基本RBCモデルの詳細" ] @@ -174,7 +177,7 @@ "U_0=u_0+\\beta u_1+\\beta^2 u_2+\\beta^3 u_3+\\cdots\n", "$$ (eq:15-lifetimeU)\n", "\n", - "ここで$0<\\beta<1$(例えば、0.99)は主観的割引因子と呼ばれるものであり、将来の効用の重要性(ウェイト)を捉えている。注意する点は,$\\beta^t>\\beta^{t+1}$が必ず成立するため,消費者にとって将来の効用の重要性は減少するということである。将来も重要だが,将来よりも今の方が大事だという考えを捉えている。$\\beta$の意味をもう少し説明するために$\\beta=1$としてみよう。この場合、50年後や100年後の将来の効用は今の効用と等しく重要だという意味であり,そのような消費者は極端に我慢強いと言える。そういう意味で、高い$\\beta$はより我慢強い選好を表している。\n", + "ここで$0<\\beta<1$(例えば、0.99)は主観的割引因子と呼ばれるものであり、将来の効用の重要性(ウェイト)を捉えている。まず、$\\beta=0$を考えてみよう。この場合、将来の効用は`0`であり、将来は全く重要ではなく、今期だけが重要だということになる。次に、$\\beta=1$としてみよう。この場合、50年後や100年後の将来の効用は今の効用と等しく重要だという意味であり,そのような消費者は極端に我慢強いと言える。そういう意味で、高い$\\beta$はより我慢強い選好を表している。通常は$0<\\beta<1$を仮定し、$\\beta^t>\\beta^{t+1}$が必ず成立する。即ち,消費者にとって将来の効用の重要性は減少するということである。将来も重要だが,将来よりも今の方が大事だという考えを捉えている。\n", "\n", "無限期間生きる消費者の効用最大化問題は非常に難しいと思うかもしれない。実際,数学的に緻密な計算をするのは高度な数学が求められる。しかし,ミクロ経済学で学んだ単純な消費者の効用最大化問題を理解することができれば,簡単に理解できるものとなっている。そのために,まず2財の効用最大化問題を復習してみよう。" ] @@ -1148,6 +1151,7 @@ "var_list = ['y','c','i']\n", "\n", "for v in var_list:\n", + " \n", " var = rbc_basic_model.simulated[v].std() / rbc_basic_model.simulated['y'].std()\n", " print(f'{v} {var:.2f}')" ] @@ -1316,7 +1320,9 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "jp-MarkdownHeadingCollapsed": true + }, "source": [ "## 内生的労働供給の導入" ] @@ -1324,7 +1330,8 @@ { "cell_type": "markdown", "metadata": { - "heading_collapsed": true + "heading_collapsed": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### 消費者の最適化行動" @@ -1385,7 +1392,8 @@ { "cell_type": "markdown", "metadata": { - "heading_collapsed": true + "heading_collapsed": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### 均衡条件" @@ -1443,7 +1451,8 @@ { "cell_type": "markdown", "metadata": { - "heading_collapsed": true + "heading_collapsed": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### シミュレーション" @@ -1601,21 +1610,21 @@ " p = parameters\n", "\n", " # 均衡条件\n", - " euler = p['beta']*( 1+fwd['r'])/fwd['c'] - 1/cur['c']\n", + " euler = p['beta'] * ( 1+fwd['r']) / fwd['c'] - 1/cur['c']\n", " \n", - " labour_supply = p['m']/( (1-cur['l'])*cur['w'] ) - 1/cur['c']\n", + " labour_supply = p['m'] / ( (1-cur['l'])*cur['w'] ) - 1/cur['c']\n", "\n", - " capital_change = cur['i'] + (1-p['d'])*cur['k'] - fwd['k']\n", + " capital_change = cur['i'] + (1-p['d']) * cur['k'] - fwd['k']\n", " \n", - " production_function = cur['a']*cur['k']**p['alpha']*cur['l']**(1-p['alpha']) - cur['y'] # (5) \n", + " production_function = cur['a'] * cur['k']**p['alpha'] * cur['l']**(1-p['alpha']) - cur['y'] # (5) \n", " \n", - " income_id = cur['c']+cur['i']-cur['y']\n", + " income_id = cur['c'] + cur['i'] - cur['y']\n", " \n", - " wage = (1-p['alpha'])*cur['y']/cur['l'] - cur['w']\n", + " wage = ( 1-p['alpha'] ) * cur['y'] / cur['l'] - cur['w']\n", " \n", - " interest_rate = p['alpha']*cur['y']/cur['k']-p['d']-cur['r']\n", + " interest_rate = p['alpha'] * cur['y'] / cur['k'] - p['d'] - cur['r']\n", " \n", - " tfp = cur['a']**p['rho']-fwd['a']\n", + " tfp = cur['a']**p['rho'] - fwd['a']\n", " \n", " # 返り値\n", " return np.array([euler,\n", @@ -1710,7 +1719,8 @@ { "cell_type": "markdown", "metadata": { - "heading_collapsed": true + "heading_collapsed": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### インパクト効果" @@ -1759,7 +1769,8 @@ { "cell_type": "markdown", "metadata": { - "heading_collapsed": true + "heading_collapsed": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### インパルス反応" @@ -1865,7 +1876,9 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "jp-MarkdownHeadingCollapsed": true + }, "source": [ "### コードを1つのセルにまとめる" ] @@ -1903,21 +1916,21 @@ " p = parameters\n", "\n", " # 均衡条件\n", - " euler = p['beta']*( 1+fwd['r'])/fwd['c'] - 1/cur['c']\n", + " euler = p['beta'] * ( 1+fwd['r'] ) / fwd['c'] - 1/cur['c']\n", " \n", - " labour_supply = p['m']/( (1-cur['l'])*cur['w'] ) - 1/cur['c']\n", + " labour_supply = p['m'] / ( (1-cur['l'])*cur['w'] ) - 1/cur['c']\n", "\n", - " capital_change = cur['i'] + (1-p['d'])*cur['k'] - fwd['k']\n", + " capital_change = cur['i'] + (1-p['d']) * cur['k'] - fwd['k']\n", " \n", - " production_function = cur['a']*cur['k']**p['alpha']*cur['l']**(1-p['alpha']) - cur['y'] # (5) \n", + " production_function = cur['a'] *cur['k']**p['alpha'] * cur['l']**(1-p['alpha']) - cur['y'] # (5) \n", " \n", - " income_id = cur['c']+cur['i']-cur['y']\n", + " income_id = cur['c']+cur['i'] - cur['y']\n", " \n", - " wage = (1-p['alpha'])*cur['y']/cur['l'] - cur['w']\n", + " wage = (1-p['alpha']) * cur['y'] / cur['l'] - cur['w']\n", " \n", - " interest_rate = p['alpha']*cur['y']/cur['k']-p['d']-cur['r']\n", + " interest_rate = p['alpha'] * cur['y'] / cur['k'] - p['d'] - cur['r']\n", " \n", - " tfp = cur['a']**p['rho']-fwd['a']\n", + " tfp = cur['a']**p['rho'] - fwd['a']\n", " \n", " # 返り値\n", " return np.array([euler,\n", @@ -1940,13 +1953,16 @@ "# 定常状態の計算\n", "rbc_model.compute_ss([1, 10, 1, 2, 0.1, 1, 1, 0.5])\n", "print('定常値')\n", + "\n", "for i in range(len(rbc_model.ss)):\n", " print(f'{rbc_model.ss.index[i]}: {rbc_model.ss.iloc[i]:.3f}')\n", + " \n", "print('')\n", "\n", "# 対数線形近似\n", "rbc_model.approximate_and_solve()\n", "print('対数線形近似')\n", + "\n", "for s in rbc_model.solved().split('\\n\\n')[1:]:\n", " print(s, sep='')\n", "\n", @@ -1967,7 +1983,8 @@ { "cell_type": "markdown", "metadata": { - "heading_collapsed": true + "heading_collapsed": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### 確率的シミュレーション" @@ -2019,7 +2036,8 @@ "cell_type": "markdown", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "#### TFPと産出量" @@ -2059,7 +2077,8 @@ "cell_type": "markdown", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "#### 変動の大きさ" @@ -2113,7 +2132,8 @@ "cell_type": "markdown", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "#### 自己相関" @@ -2130,6 +2150,7 @@ "print(' 内生的労働供給\\t基本モデル')\n", "\n", "for v in var_list:\n", + " \n", " autocorr = rbc_model.simulated[v].autocorr()\n", " autocorr_basic = rbc_basic_model.simulated[v].autocorr()\n", " print(f'{v}\\t{autocorr:.2f}\\t{autocorr_basic:.2f}')" @@ -2155,7 +2176,8 @@ "cell_type": "markdown", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "#### GDPとの相関度" @@ -2172,6 +2194,7 @@ "print(' 内生的労働供給\\t基本モデル')\n", "\n", "for v in var_list:\n", + " \n", " corr = rbc_model.simulated[['y', v]].corr().iloc[0,1]\n", " corr_basic = rbc_basic_model.simulated[['y', v]].corr().iloc[0,1]\n", " print(f'{v}\\t{corr:.2f}\\t{corr_basic:.2f}')" @@ -2196,7 +2219,8 @@ { "cell_type": "markdown", "metadata": { - "heading_collapsed": true + "heading_collapsed": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "## RBCモデルの意義と限界" diff --git a/16_data_on_money.ipynb b/16_data_on_money.ipynb index 0d363c8d..e3824b51 100644 --- a/16_data_on_money.ipynb +++ b/16_data_on_money.ipynb @@ -29,7 +29,7 @@ "import py4macro\n", "\n", "# numpy v1の表示を使用\n", - "np.set_printoptions(legacy='1.25')\n", + "np.set_printoptions(legacy='1.21')\n", "# 警告メッセージを非表示\n", "import warnings\n", "warnings.filterwarnings(\"ignore\")" @@ -38,7 +38,8 @@ { "cell_type": "markdown", "metadata": { - "heading_collapsed": true + "heading_collapsed": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "## はじめに" @@ -62,7 +63,8 @@ { "cell_type": "markdown", "metadata": { - "heading_collapsed": true + "heading_collapsed": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "## GDPとインフレ率" @@ -110,7 +112,7 @@ "df['GDPの変動'] = 100*np.log( df['gdp']/py4macro.trend(df['gdp']) )\n", "df['インフレ率の変動'] = df['inflation'] - py4macro.trend(df['inflation'])\n", "\n", - "ax_ = df[['GDPの変動','インフレ率の変動']].plot()\n", + "ax_ = df.plot(y=['GDPの変動','インフレ率の変動'])\n", "ax_.set_title('トレンドからの乖離(%)', size=20)\n", "pass" ] @@ -168,7 +170,8 @@ { "cell_type": "markdown", "metadata": { - "heading_collapsed": true + "heading_collapsed": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "## 金融政策" @@ -187,7 +190,8 @@ "cell_type": "markdown", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### 相対価格(おさらい)" @@ -263,7 +267,8 @@ "cell_type": "markdown", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### ブレトンウッズ体制" @@ -290,7 +295,8 @@ "cell_type": "markdown", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### データ" @@ -468,7 +474,8 @@ "cell_type": "markdown", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### 結果の要因"