2016年7月24日日曜日

New New GDP-S&P500 model



In order to improve readability, use temporary parameters.

> my_q_con <-  to.quarterly(UNDCONTSA["2001-04-01::2016-03-01"])[,4]
> my_q_pay <- to.quarterly(PAYEMS["2001-04-01::2016-03-01"])[,4]

Put all explanatory variables and boolean vector "output"  as cross terms. R^2 improved to 0.954.

> summary(lm(formula = to.quarterly(SP5["2001-04-01::2016-03-01"])[, 4] ~
+                  +my_q_pay * +my_q_con * +GDPC96["2001-04-01::2016-01-01"] * output))

Call:
lm(formula = to.quarterly(SP5["2001-04-01::2016-03-01"])[, 4] ~
    +my_q_pay * +my_q_con * +GDPC96["2001-04-01::2016-01-01"] *
        output)                         

Residuals:                                 
    Min      1Q  Median      3Q     Max 
-206.13  -40.42    0.00   40.04  130.58 

Coefficients:                     
                                                            Estimate Std. Error t value
(Intercept)                                                2.593e+05  7.082e+04   3.662
my_q_pay                                                  -1.981e+00  5.393e-01  -3.673
my_q_con                                                  -2.441e+02  6.914e+01  -3.531
GDPC96["2001-04-01::2016-01-01"]                          -1.782e+01  4.638e+00  -3.843
output                                                    -9.491e+05  2.603e+06  -0.365
my_q_pay:my_q_con                                          1.873e-03  5.271e-04   3.553
my_q_pay:GDPC96["2001-04-01::2016-01-01"]                  1.368e-04  3.524e-05   3.881
my_q_con:GDPC96["2001-04-01::2016-01-01"]                  1.635e-02  4.541e-03   3.600
my_q_pay:output                                            7.490e+00  2.006e+01   0.373
my_q_con:output                                            1.190e+03  3.627e+03   0.328
GDPC96["2001-04-01::2016-01-01"]:output                    6.028e+01  1.720e+02   0.351
my_q_pay:my_q_con:GDPC96["2001-04-01::2016-01-01"]        -1.256e-07  3.450e-08  -3.640
my_q_pay:my_q_con:output                                  -9.284e-03  2.775e-02  -0.335
my_q_pay:GDPC96["2001-04-01::2016-01-01"]:output          -4.766e-04  1.324e-03  -0.360
my_q_con:GDPC96["2001-04-01::2016-01-01"]:output          -7.749e-02  2.434e-01  -0.318
my_q_pay:my_q_con:GDPC96["2001-04-01::2016-01-01"]:output  6.048e-07  1.861e-06   0.325
                                                          Pr(>|t|)
(Intercept)                                               0.000668 ***
my_q_pay                                                  0.000647 ***
my_q_con                                                  0.000986 ***
GDPC96["2001-04-01::2016-01-01"]                          0.000387 ***
output                                                    0.717158
my_q_pay:my_q_con                                         0.000922 ***
my_q_pay:GDPC96["2001-04-01::2016-01-01"]                 0.000345 ***
my_q_con:GDPC96["2001-04-01::2016-01-01"]                 0.000803 ***
my_q_pay:output                                           0.710686
my_q_con:output                                           0.744484
GDPC96["2001-04-01::2016-01-01"]:output                   0.727614
my_q_pay:my_q_con:GDPC96["2001-04-01::2016-01-01"]        0.000713 ***
my_q_pay:my_q_con:output                                  0.739587
my_q_pay:GDPC96["2001-04-01::2016-01-01"]:output          0.720694
my_q_con:GDPC96["2001-04-01::2016-01-01"]:output          0.751710
my_q_pay:my_q_con:GDPC96["2001-04-01::2016-01-01"]:output 0.746693
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 86.73 on 44 degrees of freedom
Multiple R-squared:  0.9541,    Adjusted R-squared:  0.9384
F-statistic: 60.95 on 15 and 44 DF,  p-value: < 2.2e-16

2016年7月23日土曜日

New economic model to simulate S&P 500.

New economic model to simulate S&P 500.

> summary(lm(to.quarterly(SP5["2001-04-01::2016-03-01"])[,4] ~ output + 
+                to.quarterly(PAYEMS["2001-04-01::2016-03-01"])[,4] *
+                to.quarterly(UNDCONTSA["2001-04-01::2016-03-01"])[,4] *
+                GDPC96["2001-04-01::2016-01-01"]))

Call:
lm(formula = to.quarterly(SP5["2001-04-01::2016-03-01"])[, 4] ~ 
    output + to.quarterly(PAYEMS["2001-04-01::2016-03-01"])[, 
        4] * to.quarterly(UNDCONTSA["2001-04-01::2016-03-01"])[, 
        4] * GDPC96["2001-04-01::2016-01-01"])

Residuals:
     Min       1Q   Median       3Q      Max 
-199.610  -67.542    0.544   67.969  183.207 

Coefficients:
                                                                                                                                              Estimate Std. Error t value Pr(>|t|)    
(Intercept)                                                 3.433e+05  7.207e+04   4.763 1.62e-05 ***
output                                                       4.571e+01  4.638e+01   0.986    0.329    
to.quarterly(PAYEMS["2001-04-01::2016-03-01"])[, 4]                                                                                         -2.676e+00  5.445e-01  -4.915 9.61e-06 ***
to.quarterly(UNDCONTSA["2001-04-01::2016-03-01"])[, 4]                                                                                      -3.143e+02  7.151e+01  -4.395 5.62e-05 ***
GDPC96["2001-04-01::2016-01-01"]                                                                                                            -2.271e+01  4.763e+00  -4.769 1.59e-05 ***
to.quarterly(PAYEMS["2001-04-01::2016-03-01"])[, 4]:to.quarterly(UNDCONTSA["2001-04-01::2016-03-01"])[, 4]                                   2.474e-03  5.405e-04   4.576 3.06e-05 ***
to.quarterly(PAYEMS["2001-04-01::2016-03-01"])[, 4]:GDPC96["2001-04-01::2016-01-01"]                                                         1.779e-04  3.592e-05   4.953 8.43e-06 ***
to.quarterly(UNDCONTSA["2001-04-01::2016-03-01"])[, 4]:GDPC96["2001-04-01::2016-01-01"]                                                      2.014e-02  4.743e-03   4.246 9.23e-05 ***
to.quarterly(PAYEMS["2001-04-01::2016-03-01"])[, 4]:to.quarterly(UNDCONTSA["2001-04-01::2016-03-01"])[, 4]:GDPC96["2001-04-01::2016-01-01"] -1.591e-07  3.577e-08  -4.448 4.72e-05 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 96.74 on 51 degrees of freedom
Multiple R-squared:  0.9338, Adjusted R-squared:  0.9234 
F-statistic:  89.9 on 8 and 51 DF,  p-value: < 2.2e-16

2016年7月21日木曜日

Convert diff(GDPC96) into boolean vector

Convert diff(GDPC96) into boolean vector.


> output <- c();for(i in seq(2,length(as.numeric(diff(GDPC96["2001::"]))),1)){if(as.numeric(diff(GDPC96["2001::"])[i]) > 0 ){output <- append(output,0) }else{output <- append(output,-1)}}
> output
 [1]  0 -1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 -1  0 -1 -1 -1 -1  0  0  0  0  0  0 -1  0  0  0  0  0  0  0  0  0  0  0 -1  0  0  0  0  0  0  0  0

2016年7月19日火曜日

how to calculate moving average

when data is a vector of numeric like below.

> tail(data,n=10)
 [1] 199 158 165 137 170 142 127 174 158 156​


moving average is able to calculate with filter function. This is the case for 7 days simple moving average and omitting N/A results.

> filter(data,rep(1,7))[!is.na(filter(inc_daily,rep(1,7)))]/7


GDP forecast by VAR

Thus we should be able to see updated GDP forecast. Most of the figures are almost not changed at all.

> predict(VAR(v_XCPG_1992_2016),lag=6)$fcst$GDP
          fcst    lower    upper        CI
 [1,] 16633.79 16582.69 16684.89  51.09898
 [2,] 16653.44 16581.13 16725.76  72.31579
 [3,] 16672.98 16584.34 16761.61  88.63489
 [4,] 16692.39 16589.96 16794.82 102.42740
 [5,] 16711.69 16597.08 16826.30 114.61098
 [6,] 16730.88 16605.22 16856.53 125.65568
 [7,] 16749.97 16614.13 16885.81 135.84034
 [8,] 16768.95 16623.61 16914.30 145.34629
 [9,] 16787.85 16633.55 16942.15 154.29927
[10,] 16806.65 16643.86 16969.44 162.79063

2016年7月18日月曜日

revisit correlation between GDP adn S&P500. summary(lm(to.quarterly(SP5["2010-04-01::2016-03-01"])[,4] ~ GDPC96["2010-04-01::2016-01-01"]))



revisit correlation between GDP adn S&P500.

> summary(lm(to.quarterly(SP5["2010-04-01::2016-03-01"])[,4] ~ GDPC96["2010-04-01::2016-01-01"]))

Call:
lm(formula = to.quarterly(SP5["2010-04-01::2016-03-01"])[, 4] ~ 
    GDPC96["2010-04-01::2016-01-01"])

Residuals:
    Min      1Q  Median      3Q     Max 
-194.42  -86.31   13.70   73.06  172.41 

Coefficients:
                                   Estimate Std. Error t value Pr(>|t|)    
(Intercept)                      -7.760e+03  6.187e+02  -12.54 1.69e-11 ***
GDPC96["2010-04-01::2016-01-01"]  6.016e-01  3.967e-02   15.17 3.93e-13 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 106.3 on 22 degrees of freedom
Multiple R-squared:  0.9127, Adjusted R-squared:  0.9087 
F-statistic:   230 on 1 and 22 DF,  p-value: 3.93e-13


2016年7月17日日曜日

Why does the 2016 S&P breakout look so much like 2009?


Need to read later.



http://www.marketwatch.com/story/why-does-the-2016-sp-breakout-look-so-much-like-2009-2016-07-15?link=sfmw_tw

2016年7月16日土曜日

Updated dataframe for VAR analysis



use GDP,RSXFS, UNDCONTSA and PAYEMS.

v_XCPG_1992_2016 <- merge(RSXFS["1992::2016-04-01"],UNDCONTSA["1992::2016-04-01"])
v_XCPG_1992_2016 <- merge(v_XCPG_1992_2016,PAYEMS["1992::2016-04-01"])
v_XCPG_1992_2016 <- merge(v_XCPG_1992_2016,GDP_XTS["1992::2016-04-01"])

change column name.

> tail(v_XCPG_1992_2016)
            RSXFS UNDCONTSA PAYEMS GDP_XTS..1992..2016.04.01..
2015-11-01 395480       964 142875                    16485.24
2015-12-01 396723       976 143146                    16499.92
2016-01-01 394878       976 143314                    16514.59
2016-02-01 395126       987 143547                    16547.33
2016-03-01 393775       991 143733                    16580.67
2016-04-01 399134      1001 143877                    16614.00
> names(v_XCPG_1992_2016)[4]
[1] "GDP_XTS..1992..2016.04.01.."
> names(v_XCPG_1992_2016)[4] <- "GDP"

plot past 60 months and next 10 months.

>plot(c(as.numeric(last(GDP_XTS,n=60)),predict(VAR(v_XCPG_1992_2016),lag=6)$fcst$GDP[,1]),type="l")

2016年7月7日木曜日

how to convert GDP to monthly basis.

GDP data is available only for quarterly basis, while other economical data such as payroll, retail and house constructions are monthly. Use approx to convert quaterly data to monthly. this is quite unorthodox, though. the command below will add 2 iterations  between the quarter and the next one, thus convert to monthly basis. Vector length is automatically calculated and adjusted.

last(GDP_XTS);GDP_XTS <- as.xts(read.zoo(data.frame(date=seq(index(first(GDPC96)),index(last(GDPC96)),by="months"),GDP=approx(GDPC96,n=(length(GDPC96)*3-2))$y)))

start/stop apache automatically on mac os x



Recent Mac OS X is no longer able to start Apache from system configuration GUI and have to use terminals instead. Furthermore, init, inted and other legacy daemons are obsoleted. all those things are switched to launchd. The command below upload apache property file(plist) to launchd. Apache is started at boot or on demand or any other designated method written in the property.

  $ sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist

"unload" the property will disable all related set up which are done at "load".

  $ sudo launchctl unload /System/Library/LaunchDaemons/org.apache.httpd.plist