UNIVERSITY OF TORONTO Rotman School of Management RSM316 PROBLEM SET #3 Raymond Kan The file PredictorData2020.xlsx contains the data for the Welch and Goyal (RFS 2008) paper, but updated to the end...

1 answer below »
Please see hw3.pdf for questions


UNIVERSITY OF TORONTO Rotman School of Management RSM316 PROBLEM SET #3 Raymond Kan The file PredictorData2020.xlsx contains the data for the Welch and Goyal (RFS 2008) paper, but updated to the end of 2020. The data is from Professor Amit Goyal’s website. We will be using only the monthly data for this problem set. A demo Jupyter notebook hw3 demo.ipynb demonstrates how to read the data from this spreadsheet and replicates some of the figures in the paper. 1. Consider the following predictive regression rt = α + βxt−1 + ϵt, t = 1927/1, . . . , 2020/12, where rt is the excess (simple arithmetic) return of the value-weighted market portfolio (in excess of risk-free rate), and xt−1 is a predictive variable. Run this predictive regression for each one from the following set of predictive variables: d/e, svar, dfr, lty, ltr, infl, tms, tbl, dfy, d/p, d/y, e/p, b/m, and ntis. For each predictive regression, report (1) In-sample R2, (2) Out-of-sample R2 (using expanding window with an initial estimation period of 240 months), (3) ∆CEV for a mean-variance investor with γ = 3 (and a maximum weight of 150% on the market portfolio). Also perform this with a kitchen-sink predictive regression that includes all the predictive variables. For this multiple regression, drop d/e and tms to avoid multicollinearity issue. 2. Recompute the out-of-sample R2 and ∆CEV in part (1) by forcing the predicted market risk premium to be nonnegative. 3. Consider two combination forecasts from the various predictive regressions with single predictor. The first combination forecast is based on the median of various forecasts and the second combination forecast is based on the mean of various forecasts. Compute the out-of-sample R2 and ∆CEV for these two combination forecasts. 1 { "cells": [ { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "# Import the modules\n", "import pandas as pd\n", "import numpy as np\n", "from datetime import datetime\n", "from tqdm import tqdm_notebook as tqdm\n", "import statsmodels.api as sm" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "# Import Data\n", "df_monthly = pd.read_excel('PredictorData2020.xlsx',sheet_name=\"Monthly\")\n", "# Parse the dates properly\n", "time = [str(d) for d in df_monthly.yyyymm]\n", "df_monthly.index = pd.to_datetime(time,format=\"%Y%m\")" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "# Variable construction\n", "df_monthly['ExRet'] = df_monthly['CRSP_SPvw']-df_monthly['Rfree']\n", "df_monthly['DP'] = np.log(df_monthly['D12'])-np.log(df_monthly['Index'])\n", "df_monthly['DY'] = np.log(df_monthly['D12'])-np.log(df_monthly['Index'].shift())\n", "df_monthly['EP'] = np.log(df_monthly['E12'])-np.log(df_monthly['Index'])\n", "df_monthly['DE'] = np.log(df_monthly['D12'])-np.log(df_monthly['E12'])\n", "df_monthly['tms'] = df_monthly['lty']-df_monthly['tbl']\n", "df_monthly['dfr'] = df_monthly['corpr']-df_monthly['ltr']\n", "df_monthly['dfy'] = df_monthly['BAA']-df_monthly['AAA']\n", "\n", "# infl needs to be lagged one more month\n", "df_monthly['infl'] = df_monthly['infl'].shift().copy()\n", "\n", "# Construction of dependent and independent variables\n", "dep_var = 'ExRet'\n", "indep_vars = ['DE','svar','dfr','lty','ltr','infl','tms','tbl','dfy','DP','DY','EP','b/m','ntis']\n", "\n", "# Use the data from 1926/12 to 2020/12\n", "subperiod = df_monthly.index>='1926-12-01'\n", "df = df_monthly[subperiod]\n", "M = 240 # Initial length of estimation window\n", "gam = 3 # risk aversion coefficient\n", "\n", "# Create the benchmark using historical average\n", "Hist_Mean = np.asarray(df[dep_var].expanding().mean().shift())\n", "Hist_Variance = np.asarray(df[dep_var].expanding().var().shift())\n", "\n", "# Benchmark SSE (Historical Average)\n", "OOS_SSE_Hist = np.sum((df[dep_var][M+1:]-Hist_Mean[M+1:])**2)\n", "\n", "# Benchmark Certainty Equivalence\n", "w0 = ((1/gam)*(Hist_Mean/Hist_Variance)).clip(None,1.5);\n", "r0 = df[dep_var]*w0\n", "CE_Hist = np.mean(r0[M+1:])-gam/2*np.var(r0[M+1:],ddof=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The following codes demonstrate how to compute OOS $R^2$ and CEV for one predictive regression (using DY)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "Y = np.asarray(df[dep_var])\n", "X = np.asarray(df['DY'])\n", "Y_Hat = np.full(len(Y), np.nan)\n", "X = sm.add_constant(X)\n", "# Note that we start the index at M+1 because the first element of predicted return is at t=M+2.\n", "for i in range(M+1,len(Y)):\n", " Y1 = Y[1:i]\n", " X1 = X[0:i-1,:] \n", " reg = sm.OLS(Y1, X1, missing='drop').fit()\n", " Y_Hat[i] = reg.predict(X[i-1,:]) # The predicted value is based on the observation before" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "OOS_SSE = np.sum((Y[M+1:]-Y_Hat[M+1:])**2)\n", "OOS_R2 = 1-OOS_SSE/OOS_SSE_Hist\n", "w1 = ((1/gam)*(Y_Hat/Hist_Variance)).clip(None,1.5);\n", "r1 = Y*w1\n", "CE = np.mean(r1[M+1:])-gam/2*np.var(r1[M+1:],ddof=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### In-sample $R^2$ and out-of-sample $R^2$" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "IS R^2 = 0.402\n", "OOS R^2 = -0.942\n" ] } ], "source": [ "reg1 = sm.OLS(Y[1:],X[0:len(Y)-1,:],missing='drop').fit()\n", "IS_R2 = reg1.rsquared\n", "print(\"IS R^2 = %6.3f\"%(100.0*IS_R2))\n", "print(\"OOS R^2 = %6.3f\"%(100.0*OOS_R2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### $\\Delta CEV$" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Difference in Certainty Equivalence = -0.1519\n" ] } ], "source": [ "print('Difference in Certainty Equivalence = %7.4f'%(100*(CE-CE_Hist)))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.10" } }, "nbformat": 4, "nbformat_minor": 2 } Monthly yyyymmIndexD12E12b/mtblAAABAAltyntisRfreeinflltrcorprsvarcspCRSP_SPvwCRSP_SPvwx 1871014.440.26000.4000NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN 1871024.500.26000.4000NaNNaNNaNNaNNaNNaN0.004967NaNNaNNaNNaNNaNNaNNaN 1871034.610.26000.4000NaNNaNNaNNaNNaNNaN0.004525NaNNaNNaNNaNNaNNaNNaN 1871044.740.26000.4000NaNNaNNaNNaNNaNNaN0.004252NaNNaNNaNNaNNaNNaNNaN 1871054.860.26000.4000NaNNaNNaNNaNNaNNaN0.004643NaNNaNNaNNaNNaNNaNNaN 1871064.820.26000.4000NaNNaNNaNNaNNaNNaN0.003698NaNNaNNaNNaNNaNNaNNaN 1871074.730.26000.4000NaNNaNNaNNaNNaNNaN0.003373NaNNaNNaNNaNNaNNaNNaN 1871084.790.26000.4000NaNNaNNaNNaNNaNNaN0.003255NaNNaNNaNNaNNaNNaNNaN 1871094.840.26000.4000NaNNaNNaNNaNNaNNaN0.003713NaNNaNNaNNaNNaNNaNNaN 1871104.590.26000.4000NaNNaNNaNNaNNaNNaN0.004554NaNNaNNaNNaNNaNNaNNaN 1871114.640.26000.4000NaNNaNNaNNaNNaNNaN0.007041NaNNaNNaNNaNNaNNaNNaN 1871124.740.26000.4000NaNNaNNaNNaNNaNNaN0.006488NaNNaNNaNNaNNaNNaNNaN 1872014.860.26330.4025NaNNaNNaNNaNNaNNaN0.006975NaNNaNNaNNaNNaNNaNNaN 1872024.880.26670.4050NaNNaNNaNNaNNaNNaN0.005764NaNNaNNaNNaNNaNNaNNaN 1872035.040.27000.4075NaNNaNNaNNaNNaNNaN0.005218NaNNaNNaNNaNNaNNaNNaN 1872045.180.27330.4100NaNNaNNaNNaNNaNNaN0.006001NaNNaNNaNNaNNaNNaNNaN 1872055.180.27670.4125NaNNaNNaNNaNNaNNaN0.006030NaNNaNNaNNaNNaNNaNNaN 1872065.130.28000.4150NaNNaNNaNNaNNaNNaN0.004953NaNNaNNaNNaNNaNNaNNaN 1872075.100.28330.4175NaNNaNNaNNaNNaNNaN0.004067NaNNaNNaNNaNNaNNaNNaN 1872085.040.28670.4200NaNNaNNaNNaNNaNNaN0.004399NaNNaNNaNNaNNaNNaNNaN 1872094.950.29000.4225NaNNaNNaNNaNNaNNaN0.005218NaNNaNNaNNaNNaNNaNNaN 1872104.970.29330.4250NaNNaNNaNNaNNaNNaN0.007019NaNNaNNaNNaNNaNNaNNaN 1872114.950.29670.4275NaNNaNNaNNaNNaNNaN0.007609NaNNaNNaNNaNNaNNaNNaN 1872125.070.30000.4300NaNNaNNaNNaNNaNNaN0.008214NaNNaNNaNNaNNaNNaNNaN 1873015.110.30250.4325NaNNaNNaNNaNNaNNaN0.007631NaNNaNNaNNaNNaNNaNNaN 1873025.150.30500.4350NaNNaNNaNNaNNaNNaN0.006488NaNNaNNaNNaNNaNNaNNaN 1873035.110.30750.4375NaNNaNNaNNaNNaNNaN0.006561NaNNaNNaNNaNNaNNaNNaN 1873045.040.31000.4400NaNNaNNaNNaNNaNNaN0.007107NaNNaNNaNNaNNaNNaNNaN 1873055.050.31250.4425NaNNaNNaNNaNNaNNaN0.008052NaNNaNNaNNaNNaNNaNNaN 1873064.980.31500.4450NaNNaNNaNNaNNaNNaN0.005632NaNNaNNaNNaNNaNNaNNaN 1873074.970.31750.4475NaNNaNNaNNaNNaNNaN0.004680NaNNaNNaNNaNNaNNaNNaN 1873084.970.32000.4500NaNNaNNaNNaNNaNNaN0.004392NaNNaNNaNNaNNaNNaNNaN 1873094.590.32250.4525NaNNaNNaNNaNNaNNaN0.004849NaNNaNNaNNaNNaNNaNNaN 1873104.190.32500.4550NaNNaNNaNNaNNaNNaN0.010177NaNNaNNaNNaNNaNNaNNaN 1873114.040.32750.4575NaNNaNNaNNaNNaNNaN0.011815NaNNaNNaNNaNNaNNaNNaN 1873124.420.33000.4600NaNNaNNaNNaNNaNNaN0.010340NaNNaNNaNNaNNaNNaNNaN 1874014.660.33000.4600NaNNaNNaNNaNNaNNaN0.006871NaNNaNNaNNaNNaNNaNNaN 1874024.800.33000.4600NaNNaNNaNNaNNaNNaN0.005130NaNNaNNaNNaNNaNNaNNaN 1874034.730.33000.4600NaNNaNNaNNaNNaNNaN0.004067NaNNaNNaNNaNNaNNaNNaN 1874044.600.33000.4600NaNNaNNaNNaNNaNNaN0.004170NaNNaNNaNNaNNaNNaNNaN 1874054.480.33000.4600NaNNaNNaNNaNNaNNaN0.004252NaNNaNNaNNaNNaNNaNNaN 1874064.460.33000.4600NaNNaNNaNNaNNaNNaN0.003816NaNNaNNaNNaNNaNNaNNaN 1874074.460.33000.4600NaNNaNNaNNaNNaNNaN0.003742NaNNaNNaNNaNNaNNaNNaN 1874084.470.33000.4600NaNNaNNaNNaNNaNNaN0.003779NaNNaNNaNNaNNaNNaNNaN 1874094.540.33000.4600NaNNaNNaNNaNNaNNaN0.003654NaNNaNNaNNaNNaNNaNNaN 1874104.530.33000.4600NaNNaNNaNNaNNaNNaN0.004252NaNNaNNaNNaNNaNNaNNaN 1874114.570.33000.4600NaNNaNNaNNaNNaNNaN0.003927NaNNaNNaNNaNNaNNaNNaN 1874124.540.33000.4600NaNNaNNaNNaNNaNNaN0.003787NaNNaNNaNNaNNaNNaNNaN 1875014.540.32750.4517NaNNaNNaNNaNNaNNaN0.004067NaNNaNNaNNaNNaNNaNNaN 1875024.530.32500.4433NaNNaNNaNNaNNaNNaN0.003514NaNNaNNaNNaNNaNNaNNaN 1875034.590.32250.4350NaNNaNNaNNaNNaNNaN0.003469NaNNaNNaNNaNNaNNaNNaN 1875044.650.32000.4267NaNNaNNaNNaNNaNNaN0.003993NaNNaNNaNNaNNaNNaNNaN 1875054.470.31750.4183NaNNaNNaNNaNNaNNaN0.003654NaNNaNNaNNaNNaNNaNNaN 1875064.380.31500.4100NaNNaNNaNNaNNaNNaN0.003005NaNNaNNaNNaNNaNNaNNaN 1875074.390.31250.4017NaNNaNNaNNaNNaNNaN0.002997NaNNaNNaNNaNNaNNaNNaN 1875084.410.31000.3933NaNNaNNaNNaNNaNNaN0.002820NaNNaNNaNNaNNaNNaNNaN 1875094.370.30750.3850NaNNaNNaNNaNNaNNaN0.003285NaNNaNNaNNaNNaNNaNNaN 1875104.300.30500.3767NaNNaNNaNNaNNaNNaN0.003986NaNNaNNaNNaNNaNNaNNaN 1875114.370.30250.3683NaNNaNNaNNaNNaNNaN0.004296NaNNaNNaNNaNNaNNaNNaN 1875124.370.30000.3600NaNNaNNaNNaNNaNNaN0
Answered 1 days AfterOct 21, 2021

Answer To: UNIVERSITY OF TORONTO Rotman School of Management RSM316 PROBLEM SET #3 Raymond Kan The file...

Sathishkumar answered on Oct 22 2021
122 Votes
solutions/forecast.ipynb
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# Import the modules\n",
"import pandas as pd\n",
"import numpy as np\n",
"from datetime import datetime\n",
"from tqdm import tqdm_notebook as tqdm\n",
"import statsmodels.api as sm"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Import Data\n",
"df_monthly = pd.read_excel('PredictorData2020.xlsx',sheet_name=\"Monthly\")\n",
"# Parse the dates properly\n",
"time = [str(d) for d in df_monthly.yyyymm]\n",
"df_monthly.index = pd.to_datetime(time,format=\"%Y%m\")"
]
},
{
"cell_type": "code",
"execution_count": 99,
"metadata": {},
"outputs": [],
"source": [
"# Variable construction\n",
"df_monthly['ExRet'] = df_monthly['CRSP_SPvw']-df_monthly['Rfree']\n",
"df_monthly['DP'] = np.log(df_monthly['D12'])-np.log(df_monthly['Index'])\n",
"df_monthly['DY'] = np.log(df_monthly['D12'])-np.log(df_monthly['Index'].shift())\n",
"df_monthly['EP'] = np.log(df_monthly['E12'])-np.log(df_monthly['Index'])\n",
"df_monthly['DE'] = np.log(df_monthly['D12'])-np.log(df_monthly['E12'])\n",
"df_
monthly['tms'] = df_monthly['lty']-df_monthly['tbl']\n",
"df_monthly['dfr'] = df_monthly['corpr']-df_monthly['ltr']\n",
"df_monthly['dfy'] = df_monthly['BAA']-df_monthly['AAA']\n",
"\n",
"# infl needs to be lagged one more month\n",
"df_monthly['infl'] = df_monthly['infl'].shift().copy()\n",
"\n",
"# Construction of dependent and independent variables\n",
"dep_var = 'ExRet'\n",
"indep_vars = ['DE','svar','dfr','lty','ltr','infl','tms','tbl','dfy','DP','DY','EP','b/m','ntis']\n",
"\n",
"# Use the data from 1926/12 to 2020/12\n",
"subperiod = df_monthly.index>='1926-12-01'\n",
"df = df_monthly[subperiod]\n",
"M = 240 # Initial length of estimation window\n",
"gam = 3 # risk aversion coefficient\n",
"\n",
"# Create the benchmark using historical average\n",
"Hist_Mean = np.asarray(df[dep_var].expanding().mean().shift())\n",
"Hist_Variance = np.asarray(df[dep_var].expanding().var().shift())\n",
"\n",
"# Benchmark SSE (Historical Average)\n",
"OOS_SSE_Hist = np.sum((df[dep_var][M+1:]-Hist_Mean[M+1:])**2)\n",
"\n",
"# Benchmark Certainty Equivalence\n",
"w0 = ((1/gam)*(Hist_Mean/Hist_Variance)).clip(None,1.5);\n",
"r0 = df[dep_var]*w0\n",
"CE_Hist = np.mean(r0[M+1:])-gam/2*np.var(r0[M+1:],ddof=1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### The following codes demonstrate how to compute OOS $R^2$ and CEV for one predictive regression (using DY)"
]
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {},
"outputs": [],
"source": [
"Y = np.asarray(df[dep_var])\n",
"X = np.asarray(df['DY'])\n",
"Y_Hat = np.full(len(Y), np.nan)\n",
"X = sm.add_constant(X)\n",
"# Note that we start the index at M+1 because the first element of predicted return is at t=M+2.\n",
"for i in range(M+1,len(Y)):\n",
" Y1 = Y[1:i]\n",
" X1 = X[0:i-1,:] \n",
" reg = sm.OLS(Y1, X1, missing='drop').fit()\n",
" Y_Hat[i] = reg.predict(X[i-1,:]) # The predicted value is based on the observation before"
]
},
{
"cell_type": "code",
"execution_count": 101,
"metadata": {},
"outputs": [],
"source": [
"OOS_SSE = np.sum((Y[M+1:]-Y_Hat[M+1:])**2)\n",
"OOS_R2 = 1-OOS_SSE/OOS_SSE_Hist\n",
"w1 = ((1/gam)*(Y_Hat/Hist_Variance)).clip(None,1.5);\n",
"r1 = Y*w1\n",
"CE = np.mean(r1[M+1:])-gam/2*np.var(r1[M+1:],ddof=1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### In-sample $R^2$ and out-of-sample $R^2$"
]
},
{
"cell_type": "code",
"execution_count": 102,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"IS R^2 = 0.402\n",
"OOS R^2 = -0.942\n"
]
}
],
"source": [
"reg1 = sm.OLS(Y[1:],X[0:len(Y)-1,:],missing='drop').fit()\n",
"IS_R2 = reg1.rsquared\n",
"print(\"IS R^2 = %6.3f\"%(100.0*IS_R2))\n",
"print(\"OOS R^2 = %6.3f\"%(100.0*OOS_R2))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### $\\Delta CEV$"
]
},
{
"cell_type": "code",
"execution_count": 103,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Difference in Certainty Equivalence = 0.1519\n"
]
}
],
"source": [
"print('Difference in Certainty Equivalence = %7.4f'%(100*(CE-CE_Hist)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Recompute the out-of-sample R2 and ∆CEV in part (1) by forcing the predicted market risk premium to be nonnegative."
]
},
{
"cell_type": "code",
"execution_count": 93,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"IS R^2 = 0.402\n",
"OOS R^2 = -0.942\n",
"Difference in Certainty Equivalence = -0.0046\n"
]
}
],
"source": [
"# Import the modules\n",
"import pandas as pd\n",
"import numpy as np\n",
"from datetime import datetime\n",
"from tqdm import tqdm_notebook as tqdm\n",
"import statsmodels.api as sm\n",
"\n",
"# Import Data\n",
"df_monthly = pd.read_excel('PredictorData2020.xlsx',sheet_name=\"Monthly\")\n",
"# Parse the dates properly\n",
"time = [str(d) for d in df_monthly.yyyymm]\n",
"df_monthly.index = pd.to_datetime(time,format=\"%Y%m\")\n",
"\n",
"# Variable construction\n",
"df_monthly['ExRet'] = df_monthly['CRSP_SPvw']-df_monthly['Rfree']\n",
"df_monthly['DP'] = np.log(df_monthly['D12'])-np.log(df_monthly['Index'])\n",
"df_monthly['DY'] = np.log(df_monthly['D12'])-np.log(df_monthly['Index'].shift())\n",
"df_monthly['EP'] = np.log(df_monthly['E12'])-np.log(df_monthly['Index'])\n",
"df_monthly['DE'] = np.log(df_monthly['D12'])-np.log(df_monthly['E12'])\n",
"df_monthly['tms'] = df_monthly['lty']-df_monthly['tbl']\n",
"df_monthly['dfr'] = df_monthly['corpr']-df_monthly['ltr']\n",
"df_monthly['dfy'] = df_monthly['BAA']-df_monthly['AAA']\n",
"\n",
"# infl needs to be lagged one more month\n",
"df_monthly['infl'] = df_monthly['infl'].shift().copy()\n",
"\n",
"# Construction of dependent and independent variables\n",
"dep_var = 'ExRet'\n",
"indep_vars = ['svar','dfr','lty','ltr','infl','tbl','dfy','DP','DY','EP','b/m','ntis']\n",
"\n",
"# Use the data from 1926/12 to 2020/12\n",
"subperiod = df_monthly.index>='1926-12-01'\n",
"df = df_monthly[subperiod]\n",
"M = 240 # Initial length of estimation window\n",
"gam = -3 # risk aversion coefficient\n",
"\n",
"# Create the benchmark using historical average\n",
"Hist_Mean = np.asarray(df[dep_var].expanding().mean().shift())\n",
"Hist_Variance = np.asarray(df[dep_var].expanding().var().shift())\n",
"\n",
"# Benchmark SSE (Historical Average)\n",
"OOS_SSE_Hist = np.sum((df[dep_var][M+1:]-Hist_Mean[M+1:])**2)\n",
"\n",
"# Benchmark Certainty Equivalence\n",
"w0 = ((1/gam)*(Hist_Mean/Hist_Variance)).clip(None,1.5);\n",
"r0 = df[dep_var]*w0\n",
"CE_Hist = np.mean(r0[M+1:])-gam/2*np.var(r0[M+1:],ddof=1)\n",
"Y = np.asarray(df[dep_var])\n",
"X = np.asarray(df['DY'])\n",
"Y_Hat = np.full(len(Y), np.nan)\n",
"X = sm.add_constant(X)\n",
"# Note that we start the index at M+1 because the first element of predicted return is at t=M+2.\n",
"for i in range(M+1,len(Y)):\n",
" Y1 = Y[1:i]\n",
" X1 = X[0:i-1,:] \n",
" reg = sm.OLS(Y1, X1, missing='drop').fit()\n",
" Y_Hat[i] = reg.predict(X[i-1,:]) # The predicted value is based on the observation before\n",
"\n",
"OOS_SSE = np.sum((Y[M+1:]-Y_Hat[M+1:])**2)\n",
"OOS_R2 = 1-OOS_SSE/OOS_SSE_Hist\n",
"w1 = ((1/gam)*(Y_Hat/Hist_Variance)).clip(None,1.5);\n",
"r1 = Y*w1\n",
"CE = np.mean(r1[M+1:])-gam/2*np.var(r1[M+1:],ddof=1)\n",
"reg1 = sm.OLS(Y[1:],X[0:len(Y)-1,:],missing='drop').fit()\n",
"IS_R2 = reg1.rsquared\n",
"print(\"IS R^2 = %6.3f\"%(100.0*IS_R2))\n",
"print(\"OOS R^2 = %6.3f\"%(100.0*OOS_R2))\n",
"print('Difference in Certainty Equivalence = %7.4f'%(100*(CE-CE_Hist)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#median of various forecasts "
]
},
{
"cell_type": "code",
"execution_count": 104,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"IS R^2 = 0.402\n",
"OOS R^2 = -0.162\n",
"Difference in Certainty Equivalence = 0.6681\n"
]
}
],
"source": [
"# Import the modules\n",
"import pandas as pd\n",
"import numpy as np\n",
"from datetime import datetime\n",
"from tqdm import tqdm_notebook as tqdm\n",
"import statsmodels.api as sm\n",
"\n",
"# Import Data\n",
"df_monthly = pd.read_excel('PredictorData2020.xlsx',sheet_name=\"Monthly\")\n",
"# Parse the dates properly\n",
"time = [str(d) for d in df_monthly.yyyymm]\n",
"df_monthly.index = pd.to_datetime(time,format=\"%Y%m\")\n",
"\n",
"# Variable construction\n",
"df_monthly['ExRet'] = df_monthly['CRSP_SPvw']-df_monthly['Rfree']\n",
"df_monthly['DP'] = np.log(df_monthly['D12'])-np.log(df_monthly['Index'])\n",
"df_monthly['DY'] = np.log(df_monthly['D12'])-np.log(df_monthly['Index'].shift())\n",
"df_monthly['EP'] = np.log(df_monthly['E12'])-np.log(df_monthly['Index'])\n",
"df_monthly['DE'] = np.log(df_monthly['D12'])-np.log(df_monthly['E12'])\n",
"df_monthly['tms'] = df_monthly['lty']-df_monthly['tbl']\n",
"df_monthly['dfr'] = df_monthly['corpr']-df_monthly['ltr']\n",
"df_monthly['dfy'] = df_monthly['BAA']-df_monthly['AAA']\n",
"\n",
"# infl needs to be lagged one more month\n",
"df_monthly['infl'] = df_monthly['infl'].shift().copy()\n",
"\n",
"# Construction of dependent and independent variables\n",
"dep_var = 'ExRet'\n",
"indep_vars = ['svar','dfr','lty','ltr','infl','tbl','dfy','DP','DY','EP','b/m','ntis']\n",
"\n",
"# Use the data from 1926/12 to 2020/12\n",
"subperiod = df_monthly.index>='1926-12-01'\n",
"df = df_monthly[subperiod]\n",
"M = 240 # Initial length of estimation window\n",
"gam = -3 # risk aversion coefficient\n",
"\n",
"# Create the benchmark using historical average\n",
"Hist_median = np.asarray(df[dep_var].expanding().median().shift())\n",
"Hist_Variance = np.asarray(df[dep_var].expanding().var().shift())\n",
"\n",
"# Benchmark SSE (Historical Average)\n",
"OOS_SSE_Hist = np.sum((df[dep_var][M+1:]-Hist_median[M+1:])**2)\n",
"\n",
"# Benchmark Certainty Equivalence\n",
"w0 = ((1/gam)*(Hist_median/Hist_Variance)).clip(None,1.5);\n",
"r0 = df[dep_var]*w0\n",
"CE_Hist = np.median(r0[M+1:])-gam/2*np.var(r0[M+1:],ddof=1)\n",
"Y = np.asarray(df[dep_var])\n",
"X = np.asarray(df['DY'])\n",
"Y_Hat = np.full(len(Y), np.nan)\n",
"X = sm.add_constant(X)\n",
"# Note that we start the index at M+1 because the first element of predicted return is at t=M+2.\n",
"for i in range(M+1,len(Y)):\n",
" Y1 = Y[1:i]\n",
" X1 = X[0:i-1,:] \n",
" reg = sm.OLS(Y1, X1, missing='drop').fit()\n",
" Y_Hat[i] = reg.predict(X[i-1,:]) # The predicted value is based on the observation before\n",
"\n",
"OOS_SSE = np.sum((Y[M+1:]-Y_Hat[M+1:])**2)\n",
"OOS_R2 = 1-OOS_SSE/OOS_SSE_Hist\n",
"w1 = ((1/gam)*(Y_Hat/Hist_Variance)).clip(None,1.5);\n",
"r1 = Y*w1\n",
"CE = np.median(r1[M+1:])-gam/2*np.var(r1[M+1:],ddof=1)\n",
"reg1 = sm.OLS(Y[1:],X[0:len(Y)-1,:],missing='drop').fit()\n",
"IS_R2 = reg1.rsquared\n",
"print(\"IS R^2 = %6.3f\"%(100.0*IS_R2))\n",
"print(\"OOS R^2 = %6.3f\"%(100.0*OOS_R2))\n",
"print('Difference in Certainty Equivalence = %7.4f'%(100*(CE-CE_Hist)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.8"
}
},
"nbformat": 4,
"nbformat_minor":...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here