#generate facebook data
fb<-getSymbols("FB",auto.assign =FALSE,from = "2011-09-01", to = "2021-09-01") #as my train
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
## 
## This message is shown once per session and may be disabled by setting 
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
head(fb)
##            FB.Open FB.High FB.Low FB.Close FB.Volume FB.Adjusted
## 2012-05-18   42.05   45.00  38.00    38.23 573576400       38.23
## 2012-05-21   36.53   36.66  33.00    34.03 168192700       34.03
## 2012-05-22   32.61   33.59  30.94    31.00 101786600       31.00
## 2012-05-23   31.37   32.50  31.36    32.00  73600000       32.00
## 2012-05-24   32.95   33.21  31.77    33.03  50237200       33.03
## 2012-05-25   32.90   32.95  31.11    31.91  37149800       31.91
plot(fb)

fbClose<-fb$FB.Close
plot(fbClose)

#Standard Garch Model, 
#arma order=1,1: look one day back, use yesterday's stock price and error term to
#analyze today's price
fb1<-ugarchspec(variance.model=list(model="sGARCH",garchOrder=c(1,1)),
                mean.model = list(armaOrder=c(1,1)),distribution.model = "std")
fbGarch1<-ugarchfit(spec=fb1,data=fbClose)
## Warning in .sgarchfit(spec = spec, data = data, out.sample = out.sample, : 
## ugarchfit-->warning: solver failer to converge.
fbGarch1
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : sGARCH(1,1)
## Mean Model   : ARFIMA(1,0,1)
## Distribution : std 
## 
## Convergence Problem:
## Solver Message:
#Information Criteria: Akaike  4.4656
#armaOrder=2,2: look two days back
fb2<-ugarchspec(variance.model=list(model="sGARCH",garchOrder=c(1,1)),
                mean.model = list(armaOrder=c(2,2)),distribution.model = "std")
fbGarch2<-ugarchfit(spec=fb2,data=fbClose)
fbGarch2
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : sGARCH(1,1)
## Mean Model   : ARFIMA(2,0,2)
## Distribution : std 
## 
## Optimal Parameters
## ------------------------------------
##         Estimate  Std. Error   t value Pr(>|t|)
## mu     37.744650    0.339608  111.1420 0.000000
## ar1     0.030485    0.002284   13.3501 0.000000
## ar2     0.972865    0.002307  421.7466 0.000000
## ma1     0.926465    0.000153 6044.7362 0.000000
## ma2    -0.045453    0.000622  -73.0815 0.000000
## omega   0.010934    0.006102    1.7918 0.073164
## alpha1  0.054384    0.008584    6.3358 0.000000
## beta1   0.944616    0.009278  101.8134 0.000000
## shape   4.159885    0.312259   13.3219 0.000000
## 
## Robust Standard Errors:
##         Estimate  Std. Error  t value Pr(>|t|)
## mu     37.744650    0.092673  407.287 0.000000
## ar1     0.030485    0.001379   22.112 0.000000
## ar2     0.972865    0.001387  701.166 0.000000
## ma1     0.926465    0.000135 6883.389 0.000000
## ma2    -0.045453    0.001630  -27.884 0.000000
## omega   0.010934    0.009516    1.149 0.250570
## alpha1  0.054384    0.014750    3.687 0.000227
## beta1   0.944616    0.017323   54.531 0.000000
## shape   4.159885    0.359502   11.571 0.000000
## 
## LogLikelihood : -5010.874 
## 
## Information Criteria
## ------------------------------------
##                    
## Akaike       4.2960
## Bayes        4.3182
## Shibata      4.2960
## Hannan-Quinn 4.3041
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                          statistic p-value
## Lag[1]                      0.8723  0.3503
## Lag[2*(p+q)+(p+q)-1][11]    5.6336  0.7213
## Lag[4*(p+q)+(p+q)-1][19]    9.5885  0.5379
## d.o.f=4
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                   0.005205  0.9425
## Lag[2*(p+q)+(p+q)-1][5]  0.153267  0.9956
## Lag[4*(p+q)+(p+q)-1][9]  0.411361  0.9992
## d.o.f=2
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale P-Value
## ARCH Lag[3]    0.1505 0.500 2.000  0.6981
## ARCH Lag[5]    0.2575 1.440 1.667  0.9513
## ARCH Lag[7]    0.3858 2.315 1.543  0.9875
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  3.3744
## Individual Statistics:                
## mu     0.0008058
## ar1    0.0063391
## ar2    0.0222204
## ma1    0.6716270
## ma2    0.5580412
## omega  0.7575060
## alpha1 0.3187442
## beta1  0.4689022
## shape  0.6610613
## 
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic:          2.1 2.32 2.82
## Individual Statistic:     0.35 0.47 0.75
## 
## Sign Bias Test
## ------------------------------------
##                    t-value   prob sig
## Sign Bias          0.45744 0.6474    
## Negative Sign Bias 0.08927 0.9289    
## Positive Sign Bias 0.46011 0.6455    
## Joint Effect       0.81277 0.8464    
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     34.61     0.015542
## 2    30     49.97     0.009098
## 3    40     60.07     0.016662
## 4    50     66.66     0.047336
## 
## 
## Elapsed time : 1.050828
plot(fbGarch2,which='all')
## 
## please wait...calculating quantiles...

#Information Criteria: Akaike  4.4547
#armaOrder=3,3: look three days back
fb3<-ugarchspec(variance.model=list(model="sGARCH",garchOrder=c(1,1)),
                mean.model = list(armaOrder=c(3,3)),distribution.model = "std")
fbGarch3<-ugarchfit(spec=fb3,data=fbClose)
fbGarch3
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : sGARCH(1,1)
## Mean Model   : ARFIMA(3,0,3)
## Distribution : std 
## 
## Optimal Parameters
## ------------------------------------
##         Estimate  Std. Error     t value Pr(>|t|)
## mu     33.304800    0.972333  3.4252e+01 0.000000
## ar1     1.780682    0.000231  7.7116e+03 0.000000
## ar2    -0.795481    0.000141 -5.6416e+03 0.000000
## ar3     0.015211    0.000077  1.9817e+02 0.000000
## ma1    -0.826075    0.019480 -4.2406e+01 0.000000
## ma2     0.007276    0.024951  2.9161e-01 0.770585
## ma3    -0.000680    0.019170 -3.5466e-02 0.971708
## omega   0.011332    0.006200  1.8277e+00 0.067588
## alpha1  0.053956    0.008580  6.2883e+00 0.000000
## beta1   0.945044    0.009278  1.0186e+02 0.000000
## shape   4.121346    0.309715  1.3307e+01 0.000000
## 
## Robust Standard Errors:
##         Estimate  Std. Error     t value Pr(>|t|)
## mu     33.304800    0.481970  6.9101e+01 0.000000
## ar1     1.780682    0.000781  2.2811e+03 0.000000
## ar2    -0.795481    0.000339 -2.3483e+03 0.000000
## ar3     0.015211    0.000010  1.5766e+03 0.000000
## ma1    -0.826075    0.020850 -3.9620e+01 0.000000
## ma2     0.007276    0.026451  2.7507e-01 0.783264
## ma3    -0.000680    0.019259 -3.5303e-02 0.971838
## omega   0.011332    0.009551  1.1865e+00 0.235409
## alpha1  0.053956    0.014595  3.6969e+00 0.000218
## beta1   0.945044    0.017062  5.5388e+01 0.000000
## shape   4.121346    0.351802  1.1715e+01 0.000000
## 
## LogLikelihood : -5003.884 
## 
## Information Criteria
## ------------------------------------
##                    
## Akaike       4.2917
## Bayes        4.3188
## Shibata      4.2917
## Hannan-Quinn 4.3016
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                          statistic p-value
## Lag[1]                      0.8888  0.3458
## Lag[2*(p+q)+(p+q)-1][17]    4.4502  1.0000
## Lag[4*(p+q)+(p+q)-1][29]   12.8892  0.7590
## d.o.f=6
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                   0.002839  0.9575
## Lag[2*(p+q)+(p+q)-1][5]  0.132137  0.9967
## Lag[4*(p+q)+(p+q)-1][9]  0.388335  0.9993
## d.o.f=2
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale P-Value
## ARCH Lag[3]    0.1263 0.500 2.000  0.7223
## ARCH Lag[5]    0.2521 1.440 1.667  0.9527
## ARCH Lag[7]    0.3761 2.315 1.543  0.9882
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  3.3081
## Individual Statistics:               
## mu     0.002986
## ar1    0.012905
## ar2    0.013183
## ar3    0.013375
## ma1    0.524620
## ma2    0.408995
## ma3    0.488961
## omega  0.775343
## alpha1 0.292935
## beta1  0.431370
## shape  0.562630
## 
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic:          2.49 2.75 3.27
## Individual Statistic:     0.35 0.47 0.75
## 
## Sign Bias Test
## ------------------------------------
##                    t-value   prob sig
## Sign Bias           0.5503 0.5821    
## Negative Sign Bias  0.1615 0.8717    
## Positive Sign Bias  0.4758 0.6342    
## Joint Effect        1.0054 0.7999    
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     36.65     0.008774
## 2    30     45.55     0.025970
## 3    40     55.97     0.038353
## 4    50     62.12     0.098714
## 
## 
## Elapsed time : 1.048802
#Information Criteria: Akaike  4.4573
#armarOrder=2,2 is the optimal model
#predict the stock price in ten days
fbPredict<-ugarchboot(fbGarch2,n.ahead=10,method=c("Partial","Full")[1])
plot(fbPredict,which=2)

#the plot shows the upper and lower limits of the stock price estimation

#compare with actual value
fb1<-getSymbols("FB",auto.assign =FALSE,from = "2021-09-01", to = "2021-09-11") #as my train
fbClose1<-fb1$FB.Close
plot(fbClose1)