diff --git a/19_PPP.ipynb b/19_PPP.ipynb index 419d8e90..1b3645b9 100644 --- a/19_PPP.ipynb +++ b/19_PPP.ipynb @@ -34,7 +34,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\")" @@ -44,7 +44,8 @@ "cell_type": "markdown", "id": "7c0ddd8c", "metadata": { - "heading_collapsed": true + "heading_collapsed": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "## はじめに" @@ -84,7 +85,8 @@ "cell_type": "markdown", "id": "3647d0a7", "metadata": { - "heading_collapsed": true + "heading_collapsed": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "## 国内での一物一価" @@ -145,7 +147,8 @@ "id": "b0c31a03", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### チョコレート" @@ -379,7 +382,7 @@ }, "outputs": [], "source": [ - "choco['mean_deviation'].hist(bins=12, ec='white')\n", + "choco.plot(y='mean_deviation', kind='hist', bins=12, ec='white')\n", "pass" ] }, @@ -513,7 +516,8 @@ "id": "99fbafd9", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### アイスクリーム" @@ -676,7 +680,7 @@ "source": [ "ice['mean_deviation'] = ice['price'] / ice['price'].mean() - 1\n", "\n", - "ice['mean_deviation'].hist(bins=16, ec='white')\n", + "ice.plot(y='mean_deviation', kind='hist', bins=16, ec='white')\n", "pass" ] }, @@ -771,7 +775,8 @@ "id": "b9a17c97", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### ガソリン" @@ -934,7 +939,7 @@ "source": [ "gas['mean_deviation'] = gas['price'] / gas['price'].mean() - 1\n", "\n", - "gas['mean_deviation'].hist(ec='white')\n", + "gas.plot(y='mean_deviation', kind='hist', ec='white')\n", "pass" ] }, @@ -1013,7 +1018,8 @@ "id": "cb38c1aa", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### 結論" @@ -1059,7 +1065,8 @@ "cell_type": "markdown", "id": "0f726eb0", "metadata": { - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "(sec:19-bigmac)=\n", @@ -1107,7 +1114,8 @@ "id": "f65cfb13", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### データ" @@ -1527,7 +1535,7 @@ }, "outputs": [], "source": [ - "bigmac['price_yen_deviation'].hist(ec='white')\n", + "bigmac.plot(y='price_yen_deviation', kind='hist', ec='white')\n", "pass" ] }, @@ -1689,7 +1697,8 @@ "id": "c2ef0b6b", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### 所得の影響" @@ -1797,7 +1806,7 @@ }, "outputs": [], "source": [ - "ax = bigmac.plot('gdppc_yen', 'price_yen', kind='scatter', logx=True)\n", + "ax = bigmac.plot(x='gdppc_yen', y='price_yen', kind='scatter', logx=True)\n", "ax.set_xlabel('一人当たり実質GDP(円,常用対数)', fontsize=15)\n", "ax.set_ylabel('Big Mac価格(円)', fontsize=15)\n", "pass" @@ -1854,7 +1863,8 @@ "id": "69ef4782", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### PPPとは" @@ -2002,7 +2012,8 @@ "id": "e0407287", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### PPPレートと市場為替レートのデータ" @@ -2288,7 +2299,8 @@ "id": "af458300", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### 相対一般物価水準の分布" @@ -2385,7 +2397,7 @@ "source": [ "pwt19['pl_gdpo_log'] = np.log( pwt19['pl_gdpo'] )\n", "\n", - "pwt19['pl_gdpo_log'].plot(kind='hist', ec='white')\n", + "pwt19.plot(y='pl_gdpo_log', kind='hist', ec='white')\n", "pass" ] }, @@ -2586,7 +2598,8 @@ "id": "639f4d19", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### 所得の影響" @@ -2716,7 +2729,7 @@ }, "outputs": [], "source": [ - "ax = pwt19.plot('cgdpo_pc_log', 'pl_gdpo_log', kind='scatter')\n", + "ax = pwt19.plot(x='cgdpo_pc_log', y='pl_gdpo_log', kind='scatter')\n", "ax.set_xlabel('一人当たりGDP(対数,百万米ドル)', fontsize=15)\n", "ax.set_ylabel('米国の2017年価格を基準とした\\n2019年相対価格(対数)', fontsize=15)\n", "pass" @@ -2741,7 +2754,8 @@ "id": "76176a3d", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### 所得効果の分離" @@ -2874,7 +2888,8 @@ "id": "05a96845", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### 推定" @@ -2997,13 +3012,15 @@ "outputs": [], "source": [ "# 散布図\n", - "ax = pwt19.plot('cgdpo_pc_ratio_log', 'pl_gdpo_log', kind='scatter')\n", + "ax = pwt19.plot(x='cgdpo_pc_ratio_log', y='pl_gdpo_log', kind='scatter')\n", "ax.set_xlabel('米国を基準とした一人当たりGDP(対数,2019年)', fontsize=15)\n", "ax.set_ylabel('米国の2017年価格を基準とした\\n2019年相対価格(対数)', fontsize=15)\n", "\n", "# 回帰曲線\n", - "ax.plot('cgdpo_pc_ratio_log', 'fitted', color='red',\n", - " data=pwt19.sort_values('cgdpo_pc_ratio_log')) #1\n", + "pwt19.sort_values('cgdpo_pc_ratio_log').plot(x='cgdpo_pc_ratio_log', #1\n", + " y='fitted',\n", + " color='red',\n", + " ax=ax)\n", "pass" ] }, @@ -3024,7 +3041,8 @@ "cell_type": "markdown", "id": "35b819d4", "metadata": { - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### ヒストグラムの比較" @@ -3111,7 +3129,8 @@ "id": "9b98e6dd", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### 所得効果の大きさ" @@ -3351,7 +3370,8 @@ "cell_type": "markdown", "id": "33fc0f87", "metadata": { - "heading_collapsed": true + "heading_collapsed": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "## 所得効果の含意" @@ -3412,7 +3432,8 @@ "id": "bc5e0889", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### ペン効果" @@ -3490,7 +3511,8 @@ "id": "6d069838", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### 一人当たりGDPの分布" @@ -3581,7 +3603,7 @@ }, "outputs": [], "source": [ - "ax = pwt19.plot('cgdpo_pc_log','cgdpo_pc_xr_log', kind='scatter')\n", + "ax = pwt19.plot(x='cgdpo_pc_log', y='cgdpo_pc_xr_log', kind='scatter')\n", "ax.axline((9, 9), (10, 10), color='red', label='45度線') #1\n", "ax.set_xlabel('一人当たりGDP(PPPレート)', fontsize=13)\n", "ax.set_ylabel('一人当たりGDP(市場為替レート)', fontsize=13)\n", @@ -3631,7 +3653,7 @@ }, "outputs": [], "source": [ - "ax = pwt19[['cgdpo_pc_log','cgdpo_pc_xr_log']].plot(kind='kde')\n", + "ax = pwt19.plot(y=['cgdpo_pc_log','cgdpo_pc_xr_log'], kind='kde')\n", "ax.legend(['PPPレート','市場為替レート'], fontsize=13)\n", "ax.set_title('一人当たりGDP(米ドル,対数)', fontsize=20)\n", "pass" @@ -3715,8 +3737,8 @@ }, "outputs": [], "source": [ - "cc_ppp = pwt19['cgdpo_pc_log'].std()/pwt19['cgdpo_pc_log'].mean()\n", - "cc_xr = pwt19['cgdpo_pc_xr_log'].std()/pwt19['cgdpo_pc_xr_log'].mean()\n", + "cc_ppp = pwt19['cgdpo_pc_log'].std() / pwt19['cgdpo_pc_log'].mean()\n", + "cc_xr = pwt19['cgdpo_pc_xr_log'].std() / pwt19['cgdpo_pc_xr_log'].mean()\n", "\n", "print('--- GDPの変動係数 ---')\n", "print(f'PPPレート:{cc_ppp:>8.3f}')\n", @@ -3737,7 +3759,8 @@ "cell_type": "markdown", "id": "ed7a6eb0", "metadata": { - "heading_collapsed": true + "heading_collapsed": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "## まとめ" diff --git a/5a_Development_Accounting.ipynb b/5a_Development_Accounting.ipynb index f7e7868a..6878683d 100644 --- a/5a_Development_Accounting.ipynb +++ b/5a_Development_Accounting.ipynb @@ -34,7 +34,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\")" diff --git a/5b_Growth_Accounting.ipynb b/5b_Growth_Accounting.ipynb index fa9c511f..934eb9dd 100644 --- a/5b_Growth_Accounting.ipynb +++ b/5b_Growth_Accounting.ipynb @@ -30,7 +30,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\")" @@ -497,7 +497,7 @@ "metadata": {}, "outputs": [], "source": [ - "df_growth['gdppc'].plot(kind='hist',bins=15)\n", + "df_growth.plot(y='gdppc', kind='hist',bins=15)\n", "pass" ] }, @@ -590,7 +590,9 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "jp-MarkdownHeadingCollapsed": true + }, "source": [ "## 蓄積生産要素の成長率" ] @@ -630,7 +632,7 @@ "metadata": {}, "outputs": [], "source": [ - "df_growth['factors'].plot(kind='hist',bins=15)\n", + "df_growth.plot(y='factors', kind='hist',bins=15)\n", "pass" ] }, @@ -653,7 +655,9 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "jp-MarkdownHeadingCollapsed": true + }, "source": [ "## 全要素生産性" ] @@ -691,7 +695,7 @@ "metadata": {}, "outputs": [], "source": [ - "df_growth['tfp'].plot(kind='hist',bins=15)\n", + "df_growth.plot(y='tfp', kind='hist',bins=15)\n", "pass" ] }, @@ -745,7 +749,7 @@ }, "outputs": [], "source": [ - "df_growth[['tfp','factors']].plot(kind='hist',bins=20,alpha=0.5)\n", + "df_growth.plot(y=['tfp','factors'], kind='hist',bins=20,alpha=0.5)\n", "pass" ] }, @@ -1258,7 +1262,7 @@ }, "outputs": [], "source": [ - "df_jp['gdppc_growth'].plot(kind='bar')\n", + "df_jp.plot(y='gdppc_growth', kind='bar')\n", "pass" ] }, @@ -1281,7 +1285,8 @@ }, "outputs": [], "source": [ - "df_jp.iloc[:,[0,-3,-2]].plot(kind='bar')\n", + "cols = ['gdppc_growth', 'factors_growth', 'tfp_growth']\n", + "df_jp.plot(y=cols, kind='bar')\n", "pass" ] }, @@ -1322,7 +1327,7 @@ }, "outputs": [], "source": [ - "df_jp.iloc[:,[-3,-2]].plot(kind='bar', stacked=True)\n", + "df_jp.plot(y=cols[1:], kind='bar', stacked=True)\n", "pass" ] }, @@ -1343,8 +1348,8 @@ }, "outputs": [], "source": [ - "ax_ = df_jp.iloc[:,0].plot(marker='o',color='k', legend=True)\n", - "df_jp.iloc[:,[-3,-2]].plot(kind='bar', stacked=True, legend=True, ax=ax_)\n", + "ax_ = df_jp.plot(y=cols[0], marker='o',color='k', legend=True)\n", + "df_jp.plot(y=cols[1:], kind='bar', stacked=True, legend=True, ax=ax_)\n", "pass" ] }, diff --git a/6_Regression.ipynb b/6_Regression.ipynb index afd8b8cb..3b99d158 100644 --- a/6_Regression.ipynb +++ b/6_Regression.ipynb @@ -32,7 +32,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\")" @@ -80,7 +80,8 @@ "cell_type": "markdown", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### データ" @@ -141,7 +142,7 @@ "outputs": [], "source": [ "# 資本の所得シャア\n", - "a=1/3.0\n", + "a = 1 / 3\n", "\n", "# 労働者一人当たりGDP\n", "df2019['gdp_pc'] = df2019['cgdpo'] / df2019['emp']\n", @@ -185,7 +186,7 @@ }, "outputs": [], "source": [ - "df2019.plot('tfp_relative', 'gdp_pc_relative', kind='scatter')\n", + "df2019.plot(x='tfp_relative', y='gdp_pc_relative', kind='scatter')\n", "pass" ] }, @@ -193,7 +194,8 @@ "cell_type": "markdown", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### 回帰分析" diff --git a/7_DifferenceEq.ipynb b/7_DifferenceEq.ipynb index fa8dfc5d..401a8486 100644 --- a/7_DifferenceEq.ipynb +++ b/7_DifferenceEq.ipynb @@ -29,7 +29,7 @@ "import statsmodels.formula.api as sm\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\")" @@ -246,7 +246,7 @@ "\n", "for i in range(20):\n", " \n", - " x = 0.4*x + 3 # 3\n", + " x = 0.4 * x + 3 # 3\n", " \n", " x_list.append(x) # 4" ] @@ -369,7 +369,7 @@ "\n", "for i in range(20):\n", " \n", - " x = 0.4*x_list[i] + 3 # 2\n", + " x = 0.4 * x_list[i] + 3 # 2\n", " \n", " x_list.append(x) # 3\n", "\n", @@ -419,14 +419,14 @@ }, "outputs": [], "source": [ - "n = 20 # 1\n", + "n = 20 # 1\n", "\n", - "arr = np.zeros(1+n) # 2\n", - "arr[0] = 1 # 3\n", + "arr = np.zeros(1+n) # 2\n", + "arr[0] = 1 # 3\n", "\n", "for i in range(n):\n", " \n", - " arr[i+1] = 0.4*arr[i] + 3 # 4\n", + " arr[i+1] = 0.4 * arr[i] + 3 # 4\n", "\n", "pd.DataFrame({'X':x_list}).plot()\n", "pass" @@ -502,7 +502,7 @@ "\n", "for i in range(50):\n", " \n", - " x = 1.2*x - 0.2\n", + " x = 1.2 * x - 0.2\n", " \n", " x_list.append(x)\n", "\n", @@ -656,7 +656,7 @@ "outputs": [], "source": [ "def model45(y0,a,b,d,f,n=10):\n", - " \"\"\"引数 # 1\n", + " \"\"\"引数 #1\n", " y0: GDPの初期値\n", " a: 所得に依存しない消費\n", " b: 限界消費性向\n", @@ -666,18 +666,18 @@ " 戻り値\n", " yの値からなるDataFrame\"\"\"\n", " \n", - " y = y0 # 2\n", + " y = y0 #2\n", " \n", - " y_list = [y0] # 3\n", + " y_list = [y0] #3\n", "\n", " for i in range(n):\n", " \n", - " y = y*f/(1-b) + (a+d)/(1-b) # 4\n", - " y_list.append(y) # 5\n", + " y = y * f / (1-b) + (a+d) / (1-b) # 4\n", + " y_list.append(y) # 5\n", "\n", - " yss = (a+d)/(1-b-f) # 6\n", + " yss = (a+d) / (1-b-f) # 6\n", " \n", - " print(f'定常状態での産出量:{yss:.1f}') # 7\n", + " print(f'定常状態での産出量:{yss:.1f}') # 7\n", " \n", " return pd.DataFrame({'output':y_list}) # 8" ] @@ -873,15 +873,15 @@ "\n", " for i in range(n):\n", " \n", - " p = (a+c)/b - (d/b)*p\n", - " q = a-b*p\n", + " p = (a+c) / b - (d/b) * p\n", + " q = a - b * p\n", "\n", " q_list.append(q)\n", " p_list.append(p)\n", "\n", " # 定常状態\n", - " pss = (a+c)/(b+d)\n", - " qss = a-b*pss\n", + " pss = (a+c) / (b+d)\n", + " qss = a - b * pss\n", " \n", " print(f'定常状態での価格:{pss:.1f}\\n定常状態での量: {qss:.1f}')\n", " \n", @@ -906,7 +906,7 @@ }, "outputs": [], "source": [ - "cobweb(45,a=100,b=1.1,c=1,d=1,n=50).plot(secondary_y='price')\n", + "cobweb(45, a=100, b=1.1, c=1, d=1, n=50).plot(secondary_y='price')\n", "pass" ] }, @@ -933,7 +933,7 @@ }, "outputs": [], "source": [ - "cobweb(45,a=100,b=1,c=1,d=1.1,n=50).plot(secondary_y='price')\n", + "cobweb(45, a=100, b=1, c=1, d=1.1, n=50).plot(secondary_y='price')\n", "pass" ] } diff --git a/8_Solow.ipynb b/8_Solow.ipynb index 0639394a..16ed8b81 100644 --- a/8_Solow.ipynb +++ b/8_Solow.ipynb @@ -31,7 +31,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\")" @@ -40,8 +40,7 @@ { "cell_type": "markdown", "metadata": { - "heading_collapsed": true, - "jp-MarkdownHeadingCollapsed": true + "heading_collapsed": true }, "source": [ "## はじめに" @@ -243,7 +242,7 @@ }, "outputs": [], "source": [ - "def solow_model(k0, A,a,s,n,d,T=20):\n", + "def solow_model(k0, A=10, a=0.3, s=0.3, n=0.02, d=0.05, T=100):\n", " \"\"\"引数\n", " k0: 資本の初期値\n", " A: 生産性\n", @@ -256,15 +255,15 @@ " 資本と産出量からなるDataFrame\"\"\"\n", " \n", " k = k0\n", - " y = A*k0**a\n", + " y = A * k0**a\n", " \n", " k_list = [k]\n", " y_list = [y]\n", "\n", " for t in range(T):\n", " \n", - " k = ( s*A*k**a+(1-d)*k )/( 1+n )\n", - " y = A*k**a\n", + " k = ( s * A * k**a + (1-d) * k )/( 1+n )\n", + " y = A * k**a\n", "\n", " k_list.append(k)\n", " y_list.append(y)\n", @@ -293,7 +292,7 @@ }, "outputs": [], "source": [ - "df = solow_model(k0=200, A=10, a=0.3, s=0.3, n=0.02, d=0.05, T=100)\n", + "df = solow_model(k0=200)\n", "df.plot(subplots=True)\n", "pass" ] @@ -310,18 +309,17 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "hidden": true - }, + "metadata": {}, "outputs": [], "source": [ "initial_list = range(100,300,10) # 1\n", "\n", - "fig, ax = plt.subplots() # 2\n", + "ax = pd.DataFrame({'capital':[],\n", + " 'output':[]}).plot(legend=False) # 2\n", "\n", "for i in initial_list: # 3\n", - " df_temp = solow_model(k0=i, A=10, a=0.3, s=0.3, n=0.02, d=0.05, T=150)\n", - " ax.plot('capital', data=df_temp)\n", + " \n", + " solow_model(k0=i).plot(y='capital', legend=False, ax=ax)\n", " \n", "ax.set(title='ソロー・モデルの移行過程', # 4\n", " xlabel='ループの回数',\n", @@ -338,13 +336,12 @@ "````{admonition} コードの説明\n", ":class: dropdown\n", "\n", - "1. `range(100,300,10)`は100から300-1=299までの間の数を10間隔で生成する。100,110,120,$cdots$,290となる。\n", - "1. `.subplots()`を使ってキャンバスを`fig`に,「軸」を`ax`に割り当てる。\n", - "1. `initial_list`に対して`for`ループを設定する。一回のループごとに以下をおこなう。\n", + "* `#1`:`range(100,300,10)`は100から300-1=299までの間の数を10間隔で生成する。100,110,120,$cdots$,290となる。\n", + "* `#2`:空の`DataFrame`を使って空の軸を作成し,`ax`に割り当てる。\n", + "* `#3`:`initial_list`に対して`for`ループを設定する。一回のループごとに以下をおこなう。\n", " * `i`は`k0`の引数に使う値になる。\n", - " * `solow_model()`で資本ストックの数値を計算し,その`DataFrame`を`df_temp`に割り当てる。\n", " * `.plot()`を使い`ax`に図をプロットする。これにより図が重ねて描かれる。\n", - "1. `ax`のメソッド`set()`を使い,引数を使い以下を追加する。\n", + "* `#4`:`ax`のメソッド`set()`を使い,引数を使い以下を追加する。\n", " * `title`:図のタイトル\n", " * `xlabel`:横軸のラベル\n", " * `ylabel`:縦軸のラベル\n", @@ -370,7 +367,8 @@ { "cell_type": "markdown", "metadata": { - "heading_collapsed": true + "heading_collapsed": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "## 定常状態での変数の値" @@ -393,10 +391,10 @@ }, "outputs": [], "source": [ - "def calculate_steady_state(A,a,s,n,d):\n", + "def calculate_steady_state(A=10, a=0.3, s=0.3, n=0.02, d=0.05):\n", " \n", - " k_ss = ( s*A/(n+d) )**(1/(1-a)) \n", - " y_ss = A*k_ss**(a/(1-a)) \n", + " k_ss = ( s * A / (n+d) )**( 1/(1-a) ) \n", + " y_ss = A * k_ss**( a/(1-a) ) \n", " \n", " return k_ss, y_ss" ] @@ -409,7 +407,7 @@ }, "outputs": [], "source": [ - "ss = calculate_steady_state(A=10, a=0.3, s=0.3, n=0.02, d=0.05)\n", + "ss = calculate_steady_state()\n", "\n", "print(f'定常状態での資本ストック:{ss[0]:.1f}'\n", " f'\\n定常状態での産出量: {ss[1]:.1f}')" @@ -418,7 +416,8 @@ { "cell_type": "markdown", "metadata": { - "heading_collapsed": true + "heading_collapsed": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "## 線形近似" @@ -428,7 +427,8 @@ "cell_type": "markdown", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### 説明" @@ -524,7 +524,8 @@ { "cell_type": "markdown", "metadata": { - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### $\\lambda$の解釈" @@ -599,7 +600,8 @@ "cell_type": "markdown", "metadata": { "heading_collapsed": true, - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "### 線形近似による誤差" @@ -622,7 +624,7 @@ }, "outputs": [], "source": [ - "def solow_model_approx(k0,A,a,s,n,d,T=20):\n", + "def solow_model_approx(k0, A=10, a=0.3, s=0.3, n=0.02, d=0.05, T=20):\n", " \"\"\"引数\n", " k0: 資本の初期値\n", " A: 生産性\n", @@ -635,7 +637,7 @@ " 線形近似モデルを使い計算した資本と産出量からなるDataFrame\"\"\"\n", " \n", " k = k0\n", - " y = A*k0**a\n", + " y = A * k0**a\n", " \n", " k_list = [k]\n", " y_list = [y]\n", @@ -645,9 +647,9 @@ " \n", " for t in range(T):\n", " \n", - " lamb = 1-(1-a)*(n+d)/(1+n) # lambda\n", - " k = lamb*k+(1-lamb)*k_ss # 線形近似\n", - " y = A*k**a\n", + " lamb = 1 - (1-a) * (n+d) / (1+n) # lambda\n", + " k = lamb*k + (1-lamb) * k_ss # 線形近似\n", + " y = A * k**a\n", "\n", " k_list.append(k)\n", " y_list.append(y)\n", @@ -656,7 +658,7 @@ " dic = {'capital':k_list, 'output':y_list}\n", " df = pd.DataFrame(dic)\n", " \n", - " return df " + " return df" ] }, { @@ -676,7 +678,7 @@ }, "outputs": [], "source": [ - "df_approx = solow_model_approx(k0=200, A=10, a=0.3, s=0.3, n=0.02, d=0.05, T=150)" + "df_approx = solow_model_approx(k0=200, T=150)" ] }, { @@ -696,8 +698,8 @@ }, "outputs": [], "source": [ - "df_approx['capital'].plot(label='線形近似モデル', legend=True)\n", - "df['capital'].plot(label='ソロー・モデル', legend=True)\n", + "ax = df_approx.plot(y='capital', label='線形近似モデル')\n", + "df.plot(y='capital', label='ソロー・モデル', ax=ax)\n", "pass" ] }, @@ -1183,7 +1185,7 @@ }, "outputs": [], "source": [ - "ky_ratio['ky_ratio'] = np.log( ky_ratio['cn']/ky_ratio['cgdpo'] )" + "ky_ratio['ky_ratio'] = np.log( ky_ratio['cn'] / ky_ratio['cgdpo'] )" ] }, { @@ -1311,6 +1313,7 @@ "outputs": [], "source": [ "for df_right in [saving, depreciation, emp_growth]: # 1\n", + " \n", " ky_ratio = pd.merge(ky_ratio, df_right, # 2\n", " left_index=True, # 3\n", " right_index=True, # 4\n", @@ -1429,26 +1432,28 @@ "metadata": {}, "outputs": [], "source": [ - "for var in ky_ratio.columns[-3:]: # 1\n", + "for var in ky_ratio.columns[-3:]: #1\n", " \n", - " df_temp = ky_ratio.copy() # 2\n", - " res = smf.ols(f'ky_ratio ~ {var}', # 3\n", - " data=df_temp).fit() # 4\n", - " bhat = res.params.iloc[1] # 5\n", - " pval = res.pvalues.iloc[1] # 6\n", + " df_tmp = ky_ratio.copy() #2\n", + " res = smf.ols(f'ky_ratio ~ {var}', #3\n", + " data=df_tmp).fit() #4\n", + " bhat = res.params.iloc[1] #5\n", + " pval = res.pvalues.iloc[1] #6\n", " \n", - " df_temp['Trend'] = res.fittedvalues # 7\n", + " df_tmp['Trend'] = res.fittedvalues #7\n", "\n", - " fig, ax = plt.subplots(tight_layout=True) # 8\n", - " ax.scatter(var, 'ky_ratio', data=df_temp) # 9\n", - " ax.plot(var, 'Trend', # 10\n", - " data=df_temp.sort_values('Trend'), # 11\n", - " c='r') # 12\n", - " ax.set_title(f'トレンドの傾き:{bhat:.2f}\\n' # 13\n", - " f'p値:{pval:.3f}', size=20) # 14\n", - " ax.set_ylabel('資本ストック対GDP比(対数)', # 15\n", - " size=15) # 16\n", - " ax.set_xlabel(f'{var}', size=20) # 17" + " ax = df_tmp.plot(x=var, y='ky_ratio', #8\n", + " kind='scatter')\n", + " \n", + " df_tmp.sort_values('Trend').plot(x=var, #9\n", + " y='Trend', \n", + " color='red',\n", + " ax=ax)\n", + " ax.set_title(f'トレンドの傾き:{bhat:.2f}\\n' #10\n", + " f'p値:{pval:.3f}', size=20) #11\n", + " ax.set_ylabel('資本ストック対GDP比(対数)', #12\n", + " size=15) #13\n", + " ax.set_xlabel(f'{var}', size=20) #14" ] }, { @@ -1458,27 +1463,24 @@ "```{admonition} コードの説明\n", ":class: dropdown\n", "\n", - "1. `ky_ratio`の最後の3列のラベルに対して`for`ループを組んで,`var`はその列ラベルを指す。\n", - "2. `ky_ratio`のコピーを作り`df_temp`に割り当てる。`.copy()`は実態としては別物のコピーを作成する。詳細は割愛するが`.copy()`がないと実態は同じで参照記号のみが異なることになり,予期しない結果につながることを防ぐために`.copy()`を使っている。\n", - "3. 回帰分析をおこなっているが,`f-string`を使い回帰式の説明変数を指定している。また回帰分析の結果を`res`に割り当てている。\n", - "4. `data`で回帰分析のデータの指定をおこない,`.fit()`で自動計算!\n", - "5. `res`の属性`.params`は推定値を2つ返すが,1番目に傾きの推定値が格納されているため`[1]`で抽出し,`bhat`に割り当てている。\n", - "6. `res`の属性`.pvalues`は$p$値を2つ返すが,1番目に傾きの$p$値が格納されているため`[1]`で抽出し,`pval`に割り当てている。\n", - "7. `res`の属性`fittedvalues`は予測値を返すが,それを新たな列(ラベルは`Trend`)として`df_temp`に追加している。\n", - "8. `.subplots()`によるキャンバスを`fig`に,「軸」を`ax`に割り当てている。引数の`tight_layout=True`はスペースに余裕がある様に表示にするためであり,なくても構わない。\n", - "9. `.scatter()`は散布図を描くメソッド。横軸は`var`,縦軸は`ky_ratio`とし,`df_temp`をデータとして使う。\n", - "10. `.plot()`を使いトレンド線を描く。横軸は`var`,縦軸は`Trend`。\n", - "11. データには`df_temp`を使うが,メソッド`.sort_values('Trend')`で列`Trend`に従って昇順に並び替える。これはトレンド線が正確に表示されるために必要。\n", - "12. トレンド線の色を赤に指定する。\n", - "13. 図のタイトルを設定する。\n", + "* `#1`:`ky_ratio`の最後の3列のラベルに対して`for`ループを組んで,`var`はその列ラベルを指す。\n", + "* `#2`:`ky_ratio`のコピーを作り`df_tmp`に割り当てる。`.copy()`は実態としては別物のコピーを作成する。詳細は割愛するが`.copy()`がないと実態は同じで参照記号のみが異なることになり,予期しない結果につながることを防ぐために`.copy()`を使っている。\n", + "* `#3`:回帰分析をおこなっているが,`f-string`を使い回帰式の説明変数を指定している。また回帰分析の結果を`res`に割り当てている。\n", + "* `#4`:`data`で回帰分析のデータの指定をおこない,`.fit()`で自動計算!\n", + "* `#5`:`res`の属性`.params`は推定値を2つ返すが,1番目に傾きの推定値が格納されているため`.iloc[1]`で抽出し,`bhat`に割り当てている。\n", + "* `#6`:`res`の属性`.pvalues`は$p$値を2つ返すが,1番目に傾きの$p$値が格納されているため`.iloc[1]`で抽出し,`pval`に割り当てている。\n", + "* `#7`:`res`の属性`fittedvalues`は予測値を返すが,それを新たな列(ラベルは`Trend`)として`df_tmp`に追加している。\n", + "* `#8`:`df_tmp`を使い横軸は`var`,縦軸は`ky_ratio`の散布図を表示し、その軸を`ax`に割り当てる。\n", + "* `#9`:`df_tmp`を使い横軸は`var`,縦軸は`ky_ratio`の回帰直線を表示。その際、メソッド`.sort_values('Trend')`で列`Trend`に従って昇順に並び替える。これはトレンド線が正確に表示されるために必要。\n", + "* `#10`:図のタイトルを設定する。\n", " * `f-string`を使い`bhat`の値を代入する。\n", " * `:.2f`は小数点第2位までの表示を指定。\n", " * `\\n`は改行の意味。\n", " * 行を変えるので`\\n`の後に`'`が必要となる。\n", - "14. `f-string`を使い`pval`の値を代入する。`:.3f`は小数点第3位までの表示を指定し,`size=20`はフォントの大きさの指定。\n", - "15. 縦軸のラベルの設定。\n", - "16. フォントの大きさの指定。\n", - "17. 横軸のラベルの設定であり,`f-string`を使い`var`の値を代入している。`size=20`はフォントの大きさの指定。\n", + "* `#11`:`f-string`を使い`pval`の値を代入する。`:.3f`は小数点第3位までの表示を指定し,`size=20`はフォントの大きさの指定。\n", + "* `#12`:縦軸のラベルの設定。\n", + "* `#13`:フォントの大きさの指定。\n", + "* `#14`:横軸のラベルの設定であり,`f-string`を使い`var`の値を代入している。`size=20`はフォントの大きさの指定。\n", "```" ] }, diff --git a/9_Convergence.ipynb b/9_Convergence.ipynb index e465f67d..d0f09163 100644 --- a/9_Convergence.ipynb +++ b/9_Convergence.ipynb @@ -34,7 +34,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\")" @@ -44,7 +44,8 @@ "cell_type": "markdown", "id": "twenty-speed", "metadata": { - "heading_collapsed": true + "heading_collapsed": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "## はじめに" @@ -86,6 +87,7 @@ "id": "conceptual-probe", "metadata": { "heading_collapsed": true, + "jp-MarkdownHeadingCollapsed": true, "tags": [ "remove-cell" ] @@ -98,7 +100,8 @@ "cell_type": "markdown", "id": "magnetic-wrestling", "metadata": { - "hidden": true + "hidden": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "(sec:9-distribution)=\n", @@ -234,10 +237,10 @@ " density=True)\n", "\n", "# 2 密度関数\n", - "pwt.loc[cond,'gdp_pc_log'].plot(kind='density',ax=ax_)\n", + "pwt.loc[cond,'gdp_pc_log'].plot(kind='density', ax=ax_)\n", "\n", "# 3 日本\n", - "ax_.axvline(y_jp.iloc[0],color='red')\n", + "ax_.axvline(y_jp.iloc[0], color='red')\n", "\n", "# 4 横軸の表示範囲\n", "ax_.set_xlim(4,12)\n", @@ -708,6 +711,7 @@ "cell_type": "markdown", "id": "engaging-supplier", "metadata": { + "jp-MarkdownHeadingCollapsed": true, "tags": [ "remove-cell" ] @@ -719,7 +723,9 @@ { "cell_type": "markdown", "id": "decreased-copying", - "metadata": {}, + "metadata": { + "jp-MarkdownHeadingCollapsed": true + }, "source": [ "(sec:9-convergence)=\n", "## 所得収斂" @@ -1035,8 +1041,10 @@ "outputs": [], "source": [ "def mean_nan(x): # 1\n", + " \n", " if x.notna().all(): # 2\n", " return x.mean() # 3\n", + " \n", " else:\n", " return np.nan # 4" ] @@ -1121,10 +1129,13 @@ "outputs": [], "source": [ "def mean_growth_nan(x):\n", + " \n", " t = len(x)-1\n", + " \n", " if x.notna().all():\n", " x_growth = ( x.iloc[-1]/x.iloc[0] )**(1/t)-1 # 1\n", " return x_growth\n", + " \n", " else:\n", " return np.nan" ] @@ -1416,22 +1427,19 @@ { "cell_type": "code", "execution_count": null, - "id": "statutory-frontier", - "metadata": { - "hidden": true - }, + "id": "3ddd3879-1e80-4fc9-b8a8-9c97a27b4705", + "metadata": {}, "outputs": [], "source": [ - "growth_average = df_convergence.loc[:,'gdp_pc_growth'].mean() # 1\n", + "growth_avr = df_convergence.loc[:,'gdp_pc_growth'].mean() #1\n", "\n", - "fig, ax = plt.subplots(tight_layout=True)\n", - "ax.hist('gdp_pc_growth', data=df_convergence, # 2 \n", - " bins=25, edgecolor='k') # 3\n", - "ax.axvline(growth_average , c='red') # 4\n", - "ax.set_ylabel('国の数', size=15)\n", - "ax.set_title('赤い線は平均', size=15) # 5\n", - "fig.suptitle(f'{len(df_convergence)}ヵ国の平均成長率 (%)', # 6\n", - " size=20)\n", + "ax = df_convergence.plot(y='gdp_pc_growth', kind='hist', #2\n", + " bins=25, edgecolor='k') #3\n", + "ax.axvline(growth_avr, color='red') #4\n", + "ax.set_ylabel('国の数', size=15) #5\n", + "ax.set_title(\n", + " f'{len(df_convergence)}ヵ国の平均成長率\\n赤い線は平均', #6\n", + " size=17) #7\n", "pass" ] }, @@ -1445,19 +1453,17 @@ "```{admonition} コードの説明\n", ":class: dropdown\n", "\n", - "1. 平均成長率の平均を計算し,結果を変数`growth_average`に割り当てている。\n", - "2. `.hist`はヒストグラムをプロットする`ax`のメソッド。\n", - " * 第一引数はプロットしたい列ラベル(ここでは`gdp_pc_growth`)\n", - " * `data`は使用する`DataFrame`を指定する(ここでは`df_convergence`)\n", - "3. `bins`は階級の数を指定する引数(ここでは`25`)。`edgecolor`はヒストグラムの棒の枠線の色を指定する引数で,`k`は黒を示す。`black`としても構わない。\n", - "4. `axvline()`は縦の線を引く`ax`のメソッド\n", + "* `#1`:平均成長率の平均を計算し,結果を変数`growth_avr`に割り当てている。\n", + "* `#2`:`df_convergence`を使い`gdp_pc_growth`のヒストグラムをプロットし、軸を`ax`に割り当てる。\n", + "* `#3`:`bins`は階級の数を指定する引数(ここでは`25`)。`edgecolor`はヒストグラムの棒の枠線の色を指定する引数で,`k`は黒を示す。`black`としても構わない。\n", + "* `#4`:`axvline()`は縦の線を引く`ax`のメソッド\n", " * 第一引数は横軸の値\n", - " * `c`は色を指定する引数\n", - "5. `plt.subplots()`によって「軸」が`ax`に割り当てられるが,「軸」のタイトルを設定する`ax`のメソッドが`.set_title()`。\n", + " * `color`は色を指定する引数\n", + "* `#5`:軸のタイトルを設定する`ax`のメソッドが`.set_title()`。\n", " * `size`はフォントの大きさを指定する。\n", - "6. `plt.subplots()`によって「キャンバス」が`fig`に割り当てられるが,「キャンバス」のタイトルを設定する`fig`のメソッドが`.suptitle()`。\n", + "* `#6`:軸のタイトルを設定する。\n", " * `f-string`を使って`{}`の中に`len(df_reg)`を代入している。\n", - " * `size`はフォントの大きさを指定する。\n", + "* `#7`:`size`はフォントの大きさを指定する。\n", " \n", "上のヒストグラムは[発展会計](chap:5a-accounting)の章の「はじめに」で表示したヒストグラムと次の点で異なる。\n", "* `df_convergence`は1970年以降のデータを使っている,「はじめに」のヒストグラムは1960年以降のデータを使っている。\n", @@ -1485,7 +1491,7 @@ }, "outputs": [], "source": [ - "( df_convergence.loc[:,'gdp_pc_growth'] < growth_average ).sum() / len(df_convergence)" + "( df_convergence.loc[:,'gdp_pc_growth'] < growth_avr ).sum() / len(df_convergence)" ] }, { @@ -1703,10 +1709,10 @@ "source": [ "def remaining_percent(n, s=speed1970):\n", " \n", - " x0 = gdp_pc_us-gdp_pc_mean\n", - " x = ( 1-s )**n*x0\n", + " x0 = gdp_pc_us - gdp_pc_mean\n", + " x = ( 1-s )**n * x0\n", " \n", - " return 100*x/x0" + " return 100 * x / x0" ] }, { @@ -2480,7 +2486,8 @@ "cell_type": "markdown", "id": "fe333faa", "metadata": { - "heading_collapsed": true + "heading_collapsed": true, + "jp-MarkdownHeadingCollapsed": true }, "source": [ "## まとめ" @@ -2525,6 +2532,7 @@ "id": "sealed-festival", "metadata": { "heading_collapsed": true, + "jp-MarkdownHeadingCollapsed": true, "tags": [ "remove-cell" ]