# Impor Librarie
im nmp a np # package for cienific comping
im pandas a pd # package for daa maniplaion
im os # package for commnicaing ih operaing em
Copyright By PowCoder代写 加微信 powcoder
im statsmodels.formla.api a smf # package for aiical model i.e. OLS im scip.stats a st
# Read daa
df = pd.EcelFile(‘Talor rle fndamentals data.ls’) japan = pd.read_ecel(df, ‘Japan’)
japan.head(10)
### Daa Cleaning : Ue oberaion from 1973M3 o 2006M6
## Dependen Variable : _+1, _
# _crren : log of nominal echange rae a
s_crrent = np.log(japan.loc[26:len(japan)-2,’EX_JAP_2′]).reset_inde(drop=Te) #Wh ree inde? To combine ino a ingle daa e la s_crrent = s_crrent.rename(‘s_crrent’) #make re hing are named he a e an hem.
#I ed and ill e “26” aboe becae i j an he daa aring from 1973,hich hen I looked a he ecel file I a ared a r #Of core, o can ala be he daae o eclde eerhing from before 1973.Hoeer, e alo need daa ha came before 1973 o
## Eimae of Op Gap
# Linear Trend
# Op Gap in Japan and US
japan[‘t’] = pd.DataFrame(’t’ : range(1,len(japan)+1)) #j e imple ime for he linear rend
f i i range(0,len(japan_const)): # Updaing he poenial op each period
linear_trend_japan = smf.ols(formla = ‘IPSA_JAP t’, data=japan[0:27+i]).fit() #gdp regreed again ime japan_const.loc[i,’japan_potential’] = linear_trend_japan.predict(japan[‘t’][27+i:])[27+i] # e need he econd [27+i] o enre he linear_trend_s = smf.ols(formla = ‘IPSA_US t’, data=japan[0:27+i]).fit()
japan_const.loc[i,’s_potential’] = linear_trend_s.predict(japan[‘t’][27+i:])[27+i]
japan_const[‘japan_gap’] = (japan_const[‘IPSA_JAP’] – japan_const[‘japan_potential’])/japan_const[‘japan_potential’] japan_const[‘s_gap’] = (japan_const[‘IPSA_US’] – japan_const[‘s_potential’])/japan_const[‘s_potential’]
# Op Gap differenial beeen US and Japan
japan_const[‘gap_diff’] = japan_const[‘s_gap’] – japan_const[‘japan_gap’] japan_const.head(10)
# _fre : log of nominal echange rae a +1
s_ftre = np.log(japan.loc[27:,’EX_JAP_2′]).reset_inde(drop=Te) s_ftre = s_ftre.rename(‘s_ftre’)
# _change : change in log of echange rae (_+1 – _), fre – crren
s_change = s_ftre – s_crrent s_change = s_change.rename(‘s_change’)
## Independen Variable
# inf_, inf_japan : inflaion a , japan inflaion a
inf_japan = japan.loc[26:len(japan)-2,’INFL_JAP’].reset_inde(drop=Te) inf_s = japan.loc[26:len(japan)-2,’INFL_US’].reset_inde(drop=Te) inf_diff = inf_s – inf_japan
inf_diff = inf_diff.rename(‘inf_diff’) #inflaion difference
# op_, op_japan : op a , japan op a (GDP)
otpt_japan = japan.loc[26:len(japan)-2,’IPSA_JAP’].reset_inde(drop=Te) otpt_s = japan.loc[26:len(japan)-2,’IPSA_US’].reset_inde(drop=Te)
# dae : Time a +1
date = japan.loc[27:,’date’].reset_inde(drop=Te)
# Combine all ariable ino a ne daa e
japan_const = pd.concat([date, s_ftre, s_crrent, s_change, inf_diff, otpt_japan, otpt_s], ais=1) japan_const.head(10)
# Qadraic Trend Op Gap in Japan and US
japan[‘X’] = japan[“t”]^2 #ih he qadraic rend e j qare ime
f i i range(0,len(japan_const)): # Updaing he poenial op each period
qad_trend_japan = smf.ols(formla = ‘IPSA_JAP X’, data=japan[0:27+i]).fit() japan_const.loc[i,’japan_potential_qad’] = qad_trend_japan.predict(japan[‘X’][27+i:])[27+i] qad_trend_s = smf.ols(formla = ‘IPSA_US X’, data=japan[0:27+i]).fit() japan_const.loc[i,’s_potential_qad’] = qad_trend_s.predict(japan[‘X’][27+i:])[27+i]
japan_const[‘japan_gap_qad’] = (japan_const[‘IPSA_JAP’] – japan_const[‘japan_potential_qad’])/japan_const[‘japan_potential_qad’] japan_const[‘s_gap_qad’] = (japan_const[‘IPSA_US’] – japan_const[‘s_potential_qad’])/japan_const[‘s_potential_qad’]
# Op Gap differenial beeen US and Japan
japan_const[‘gap_diff_qad’] = japan_const[‘s_gap_qad’] – japan_const[‘japan_gap_qad’] # Keep ariable onl ed for or model
#japan_con=japan_con[[‘dae’, ‘_fre’, ‘_crren’, ‘_change’, ‘inf_diff’, ‘gap_diff’, ‘gap_diff_qad’]]
japan_const.head(10)
# HP Filer Trend Op Gap in Japan and US
im statsmodels.api a sm
ccle_JAP, trend_JAP = sm.tsa.filters.hpfilter(japan[‘IPSA_JAP’],14400)
ccle_US, trend_US = sm.tsa.filters.hpfilter(japan[‘IPSA_US’],14400) japan[‘trend_JAP’] = trend_JAP
japan[‘trend_US’] = trend_US
f i i range(0,len(japan_const)): # Updaing he poenial op each period
hp_trend_japan = smf.ols(formla = ‘IPSA_JAP trend_JAP’, data=japan[0:27+i]).fit() japan_const.loc[i,’japan_potential_hp’] = hp_trend_japan.predict(japan[‘trend_JAP’][27+i:])[27+i] hp_trend_s = smf.ols(formla = ‘IPSA_US trend_US’, data=japan[0:27+i]).fit() japan_const.loc[i,’s_potential_hp’] = hp_trend_s.predict(japan[‘trend_US’][27+i:])[27+i]
japan_const[‘japan_gap_hp’] = (japan_const[‘IPSA_JAP’] – japan_const[‘japan_potential_hp’])/japan_const[‘japan_potential_hp’] japan_const[‘s_gap_hp’] = (japan_const[‘IPSA_US’] – japan_const[‘s_potential_hp’])/japan_const[‘s_potential_hp’]
# Op Gap differenial beeen US and Japan
japan_const[‘gap_diff_hp’] = japan_const[‘s_gap_hp’] – japan_const[‘japan_gap_hp’] # Keep ariable onl ed for or model
japan_const=japan_const[[‘date’, ‘s_ftre’, ‘s_crrent’, ‘s_change’, ‘inf_diff’, ‘gap_diff’, ‘gap_diff_qad’, ‘gap_diff_hp’]] japan_const.head(10)
## Make re all colmn hae floa nmber, meaning ha all nmber conain decimal poin
japan_const.loc[:,’s_ftre’] = japan_const.loc[:,’s_ftre’].appl(float) japan_const.loc[:,’s_crrent’] = japan_const.loc[:,’s_crrent’].appl(float) japan_const.loc[:,’s_change’] = japan_const.loc[:,’s_change’].appl(float) japan_const.loc[:,’inf_diff’] = japan_const.loc[:,’inf_diff’].appl(float) japan_const.loc[:,’gap_diff’] = japan_const.loc[:,’gap_diff’].appl(float) japan_const.loc[:,’gap_diff_qad’] = japan_const.loc[:,’gap_diff_qad’].appl(float) japan_const.loc[:,’gap_diff_hp’] = japan_const.loc[:,’gap_diff_hp’].appl(float)
# Creae emp colmn for fied ale of change i.e. change of log echange rae/o ore rel
japan_const[‘s_change_fitted’] = np.nan japan_const[‘s_change_fittedQ’] = np.nan japan_const[‘s_change_fittedHP’] = np.nan
#WITH LINEAR TREND
# O-of-ample Foreca ih rolling indo
f i i range(120,len(japan_const)):
tmp = smf.ols(formla = ‘s_change inf_diff + gap_diff’, data=japan_const[i-120:i]).fit() japan_const.loc[i,’s_change_fitted’] = tmp.predict(japan_const[[‘inf_diff’, ‘gap_diff’]][i:i+1])[i]
# Foreca = he fied ale + original crren nominal echange rae
japan_const[‘s_forecast’] = japan_const[‘s_change_fitted’] + japan_const[‘s_crrent’] # Foreca Error = he acal fre echange rae – he foreca
japan_const[‘error’] = japan_const[‘s_ftre’]-japan_const[‘s_forecast’] #WITH QUAD TREND
# O-of-ample Foreca ih rolling indo
f i i range(120,len(japan_const)):
tmpQ = smf.ols(formla = ‘s_change inf_diff + gap_diff_qad’, data=japan_const[i-120:i]).fit() japan_const.loc[i,’s_change_fittedQ’] = tmpQ.predict(japan_const[[‘inf_diff’, ‘gap_diff_qad’]][i:i+1])[i]
# Foreca = he fied ale + original crren nominal echange rae
japan_const[‘s_forecastQad’] = japan_const[‘s_change_fittedQ’] + japan_const[‘s_crrent’] # Foreca Error = he acal fre echange rae – he foreca
japan_const[‘errorQad’] = japan_const[‘s_ftre’]-japan_const[‘s_forecastQad’] #WITH HP Filer
# O-of-ample Foreca ih rolling indo
f i i range(120,len(japan_const)):
tmpHP = smf.ols(formla = ‘s_change inf_diff + gap_diff_hp’, data=japan_const[i-120:i]).fit() japan_const.loc[i,’s_change_fittedHP’] = tmpHP.predict(japan_const[[‘inf_diff’, ‘gap_diff_hp’]][i:i+1])[i]
# Foreca = he fied ale + original crren nominal echange rae
japan_const[‘s_forecastHP’] = japan_const[‘s_change_fittedHP’] + japan_const[‘s_crrent’] # Foreca Error = he acal fre echange rae – he foreca
japan_const[‘errorHP’] = japan_const[‘s_ftre’]-japan_const[‘s_forecastHP’] japan_const.tail(10)
Ot[10]: In [11]:
Ot[11]: In [12]:
Ot[12]: In [13]:
0.000790213959120891
0.0007896189706440049
0.000759292803156716
0.0007702707364058667
### Mean qared error
## Talor Rle Model Linear Trend
P = len(japan_const[‘error’][120:])
MSE_T = np.sm(np.sqare(japan_const[‘error’])[120:])/P MSE_T
## Talor Rle Model Qad rend
MSE_TQ = np.sm(np.sqare(japan_const[‘errorQad’])[120:])/P MSE_TQ
## Talor Rle Model HP rend
MSE_THP = np.sm(np.sqare(japan_const[‘errorHP’])[120:])/P MSE_THP
## Random Walk
MSE_R = np.sm(np.sqare(japan_const[‘s_change’])[120:])/P MSE_R
### DMW aiic
error_R = japan_const[‘s_change’][120:].reset_inde(drop=Te) #random alk
error_T = japan_const[‘error’][120:].reset_inde(drop=Te) # linear
tmp = np.sqare(error_R) – np.sqare(error_T) – np.sqare(MSE_R – MSE_T) #mean qared predicion error of he fir erie braced b V_hat = np.sm(np.sqare(tmp))/P #mean abole error
## Saiic
#DMW = (MSE_R – MSE_T)/np.qr(V_ha/P)
Since the DMW statitsic is eqal to -0.8377179022382378, hich is less than the critical ale (1.645) e fail to reject the nll hpothe sis that the MP model does not otperform the random alk model.
-0.8377179022382378
Since the DMW statitsic is eqal to -0.8371763520198173, hich is less than the critical ale (1.645) e fail to reject the nll hpothe sis that the MP model does not otperform the random alk model.
-0.8371763520198173
Since the DMW statitsic is eqal to -0.909682915066713, hich is less than the critical ale (1.645) e fail to reject the nll hpothes is that the MP model does not otperform the random alk model.
-0.909682915066713
error_TQ = japan_const[‘errorQad’][120:].reset_inde(drop=Te)
tmpQ = np.sqare(error_R) – np.sqare(error_TQ) – np.sqare(MSE_R – MSE_TQ) V_hatq = np.sm(np.sqare(tmpQ))/P
DQ = DMWTest(tmpQ, V_hatq, P, Z = 1.645) DQ
error_THP = japan_const[‘errorHP’][120:].reset_inde(drop=Te)
tmpHP = np.sqare(error_R) – np.sqare(error_THP) – np.sqare(MSE_R – MSE_THP) V_hatHP = np.sm(np.sqare(tmpHP))/P
DHP = DMWTest(tmpHP, V_hatHP, P, Z = 1.645) DHP
### CW aiic
tmp2 = np.sm(np.sqare(japan_const[‘s_change_fitted’][120:]))/P
def ClarkWest (DMW, tmp2, V_hat, P, Z):
CW = (DMW + (tmp2 *(1/np.sqrt(V_hat/P)))) if (CW < Z ):
#df_cop["e_rel"] = "e FAIL o rejec he nll hpohei ha he MP model doe no operform he random alk mode
print('Since the CW statitsic is eqal to ' + str(CW) + ',' + ' hich is less than the critical ale (' + str(Z) + ') e fa
ele: e(CW)
#df_cop["e_rel"] = "We rejec he nll hpohei"
print("We reject the nll hpothesis")
ClarkWest(D, tmp2, V_hat, P, Z=1.645)
Since the CW statitsic is eqal to 1.450682889111174, hich is less than the critical ale (1.645) e fail to reject the nll hpothesis that the MP model does not otperform the random alk model.
1.450682889111174
Since the CW statitsic is eqal to 1.4491407568703316, hich is less than the critical ale (1.645) e fail to reject the nll hpothesi s that the MP model does not otperform the random alk model.
1.4491407568703316
Since the CW statitsic is eqal to 0.8812034441279899, hich is less than the critical ale (1.645) e fail to reject the nll hpothesi s that the MP model does not otperform the random alk model.
0.8812034441279899
tmp3 = np.sm(np.sqare(japan_const['s_change_fittedQ'][120:]))/P ClarkWest(DQ, tmp3, V_hatq, P, Z=1.645)
tmp4 = np.sm(np.sqare(japan_const['s_change_fittedHP'][120:]))/P ClarkWest(DHP, tmp4, V_hatHP, P, Z=1.645)
def DMWTest (tmp, V_hat, P, Z):
DMW = (MSE_R - MSE_T)/np.sqrt(V_hat/P) if (DMW < Z ):
#df_cop["e_rel"] = "e FAIL o rejec he nll hpohei ha he MP model doe no operform he random alk model
print('Since the DMW statitsic is eqal to ' + str(DMW) + ',' + ' hich is less than the critical ale (' + str(Z) + ') e
ele: e(DMW)
#df_cop["e_rel"] = "We rejec he nll hpohei"
print("We reject the nll hpothesis")
D = DMWTest(tmp, V_hat, P, Z = 1.645) D
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com