2016年9月25日日曜日

function for S&P 500 forecast.


my_sp5 is the most simple one. Given GDP, Payroll and Housing under construction, the function returns the theoretical value for S&P 500.

my_sp5 <- function(x,y,z)
{
  # x <- GDP  :like 17556
  # y <- PAYEMS :like 150000
  # z <- UNDCONTSA :like 1037
  # 2.371e+04+y*-2.615e-01+z*-5.720e+00+x*-1.707e+00+y*z*1.590e-04+y*x*1.926e-05+z*x*-5.615e-04+y*z*x*-3.970e-09
  #
  # start_date <- "1992-01-01"
  # end_date <-  "2016-04-01"
  # please see > (summary(lm(to.quarterly(SP5[paste(start_date,end_date,sep='::')])[,4] ~
  #                to.quarterly(PAYEMS[paste(start_date,end_date,sep='::')])[,4] *
  #                 to.quarterly(UNDCONTSA[paste(start_date,end_date,sep='::')])[,4] *
  #                  GDPC96[paste(start_date,end_date,sep='::')])))
  2.805e+04+y*-2.959e-01+z*-1.095e+01+x*-2.02e+00+y*z*2.007e-04+y*x*2.174e-05+z*x*-1.949e-04+y*z*x*-6.888e-09
}

This will be given the length of terms of forecast as the parameter. If 12 is given, it will return values for next 12 quarters. GDP, payroll and housing data are derived from VAR model v_GPC_q_1992_2016.

my_sp5_fc <- function(x)
{
  # x <- span of forecast by # of quarter. 4 = 1 yr, 8= 2yrs.
   print(my_sp5(predict(VAR(v_GPC_q_1992_2016,lag.max=10),n.ahead=x)$fcst$GDPC96[,1],predict(VAR(v_GPC_q_1992_2016,lag.max=10),n.ahead=x)$fcst$PAYEMS[,1],predict(VAR(v_GPC_q_1992_2016,lag.max=10),n.ahead=x)$fcst$UNDCONTSA[,1]))
   # seq(as.Date(Sys.Date()),as.Date(last(seq(mondate(Sys.Date()), by=1, length.out=x*3))),
   # plot(seq(as.Date(Sys.Date()),as.Date(last(seq(mondate(Sys.Date()), by=1, length.out=x*3))), by="quarters"),my_sp5(predict(VAR(v_GPC_q_1992_2016,lag.max=10),n.ahead=x)$fcst$GDPC96[,1],predict(VAR(v_GPC_q_1992_2016,lag.max=10),n.ahead=x)$fcst$PAYEMS[,1],predict(VAR(v_GPC_q_1992_2016,lag.max=10),n.ahead=x)$fcst$UNDCONTSA[,1]),ylab="",xlab="s&p500")
 }

This function is extended to accept another parameter for VAR data structure. The limit lies where the structure must have GDPC96,PAYEMS and UNDCONSTA as members and, other than those 3 will be discarded.

my_sp5_fc <- function(x,var_p)
{
  # x <- span of forecast by # of quarter. 4 = 1 yr, 8= 2yrs.
  # VAR structre like v_GPC_q_2001_2016
  print(my_sp5(predict(VAR(var_p,lag.max=10),n.ahead=x)$fcst$GDPC96[,1],predict(VAR(var_p,lag.max=10),n.ahead=x)$fcst$PAYEMS[,1],predict(VAR(var_p,lag.max=10),n.ahead=x)$fcst$UNDCONTSA[,1]))
  # seq(as.Date(Sys.Date()),as.Date(last(seq(mondate(Sys.Date()), by=1, length.out=x*3))),
  plot(seq(as.Date(Sys.Date()),as.Date(last(seq(mondate(Sys.Date()), by=1, length.out=x*3))), by="quarters"),my_sp5(predict(VAR(var_p,lag.max=10),n.ahead=x)$fcst$GDPC96[,1],predict(VAR(var_p,lag.max=10),n.ahead=x)$fcst$PAYEMS[,1],predict(VAR(var_p,lag.max=10),n.ahead=x)$fcst$UNDCONTSA[,1]),ylab="",xlab="s&p500")
}

Please see below as a sample output for next 36 quarters.

 my_sp5_fc(36,v_GPC_q_2001_2016)
 [1] 2169.916 2230.324 2306.517 2374.391 2442.389 2482.522 2494.811 2514.468 2513.888 2503.617 2500.096
[12] 2514.607 2536.827 2573.543 2628.566 2683.178 2727.886 2772.437 2806.057 2829.112 2850.500 2874.021
[23] 2898.234 2928.297 2968.731 3010.476 3053.568 3097.477 3138.014 3174.560 3208.802 3241.897 3273.784

[34] 3308.672 3349.468 3396.346

0 件のコメント: