Hw6Code.py - coding utf-8*import numpy as np import...

This preview shows page 1 - 2 out of 5 pages.

# -*- coding: utf-8 -*- import numpy as np import matplotlib.pyplot as plt import scipy.stats as sct import pandas as pd from statsmodels.tsa.api import VAR data = pd.read_excel("hw5_data.xlsx",sheet_name=None) ff25_portfolios = data["ff25"].drop(["SAS Date. Last Trading Day of the Month"],axis=1) ff_factors = data["fffactors"].drop(['Dates','RF'],axis=1) macros_variables = data['mdfred'].drop(['dates'],axis=1) T,N = ff25_portfolios.shape K = ff_factors.shape[1] for j in np.arange(N): ff25_portfolios[[ff25_portfolios.keys()[j]]] = ( ff25_portfolios[[ff25_portfolios.keys()[j]]].values - data['fffactors'][["RF"]].values ) print("I make excess returns") forecast_OOS_VAR = np.full((T,K),np.nan) forecast_OOS_VARX = np.full((T,K),np.nan) forecast_OOS_VAR_PTF = np.full((T,N),np.nan) forecast_OOS_VARX_PTF = np.full((T,N),np.nan) forecast_OOS_FAVAR = np.full((T,K),np.nan) forecast_OOS_FAVAR_PTF = np.full((T,N),np.nan) forecast_OOS_BENCH_PTF = np.full((T,N),np.nan) t_initial = 300 for t in np.arange(t_initial,T): portfolios_t = ff25_portfolios.values[:t,:] factors_t = ff_factors.values[:t,:] macros_t = macros_variables.values[:t,:] zscores_t = sct.zscore(macros_t) VAR_results_t = VAR(factors_t).fit(1) VARX_results_t = VAR(factors_t,exog=macros_t).fit(1) U_t,S_t,V_t = np.linalg.svd(zscores_t) FAVAR_results_t = VAR(factors_t,exog=U_t[:,:5]*S_t[:5]).fit(1) B_t = np.linalg.lstsq(factors_t,portfolios_t,rcond=None)[0] forecast_OOS_VAR[t,:] = ( [email protected]_t[-1,:].T
Image of page 1

Subscribe to view the full document.

+ VAR_results_t.coefs_exog.T ) forecast_OOS_VARX[t,:] = ( [email protected]_t[-1,:].T + [email protected]((1,macros_t[-1,:]))
Image of page 2
You've reached the end of this preview.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern