This work is licensed under the Creative Commons Attribution-ShareAlike 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/4.0/ or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
License: CC BY-SA 4.0
Sugestão de citação: FIGUEIREDO, Adriano Marcos Rodrigues. Séries Temporais com R: Exemplo ARCH com stocks. Campo Grande-MS,Brasil: RStudio/Rpubs, 2019. Disponível em http://rpubs.com/amrofi/stocks_garch.
Este artigo procura ilustrar os procedimentos para trabalhar com ações de uma empresa no âmbito dos métodos de séries temporais, com modelos da família ARCH. É baseado e adaptado a partir do script do Prof. Pedro Costa Ferreira (FGV) disponível em: https://github.com/pedrocostaferreira/SeriesTemporais/blob/master/Aula8_GARCHmodel_PETR4.R.
O primeiro passo para tal é baixar os dados da empresa desejada. Usaremos como exemplo a Petrobras (PETR4), a Usiminas (USIM5) e a Bovespa (BVSP). O pacote quantmod
nos auxiliará a obter os dados eletronicamente. O pesquisador deve conhecer os tickers do papel desejado.
if (!require(quantmod)) {
install.packages('quantmod')
library(quantmod)
}
if (!require(fpp2)) {
install.packages('fpp2')
library(fpp2)
}
if (!require(Metrics)) {
install.packages('Metrics')
library(Metrics)
}
if (!require(stockPortfolio)) {
install.packages('stockPortfolio')
library(stockPortfolio)
}
if (!require(rugarch)) {
install.packages('rugarch')
library(rugarch)
}
PETR4.SA
require(quantmod)
getSymbols('PETR4.SA',src='yahoo') # pelo {quantmod}
## [1] "PETR4.SA"
head(PETR4.SA)
## PETR4.SA.Open PETR4.SA.High PETR4.SA.Low PETR4.SA.Close
## 2007-01-02 25.000 25.225 24.880 25.160
## 2007-01-03 25.080 25.200 24.005 24.395
## 2007-01-04 24.250 24.375 23.700 23.850
## 2007-01-05 23.600 23.995 22.550 23.125
## 2007-01-08 23.250 23.570 22.900 23.395
## 2007-01-09 22.985 23.195 22.305 22.680
## PETR4.SA.Volume PETR4.SA.Adjusted
## 2007-01-02 10244800 19.29404
## 2007-01-03 19898600 18.70740
## 2007-01-04 21060200 18.28946
## 2007-01-05 24864000 17.73349
## 2007-01-08 19440200 17.94054
## 2007-01-09 25883600 17.39224
tail(PETR4.SA)
## PETR4.SA.Open PETR4.SA.High PETR4.SA.Low PETR4.SA.Close
## 2019-09-25 27.03 27.35 26.78 27.34
## 2019-09-26 27.40 27.70 27.23 27.70
## 2019-09-27 27.55 27.95 27.52 27.66
## 2019-09-30 27.56 27.64 27.40 27.55
## 2019-10-01 27.60 27.73 27.36 27.51
## 2019-10-02 27.22 27.26 26.61 26.72
## PETR4.SA.Volume PETR4.SA.Adjusted
## 2019-09-25 29038400 27.34
## 2019-09-26 33705900 27.70
## 2019-09-27 31886800 27.66
## 2019-09-30 23143400 27.55
## 2019-10-01 32423200 27.51
## 2019-10-02 51261200 26.72
PETR4.SA.A <- PETR4.SA$PETR4.SA.Adjusted
autoplot(PETR4.SA.A)
chartSeries(PETR4.SA)
USIM5.SA
getSymbols('USIM5.SA',src='yahoo') # pelo {quantmod}
## [1] "USIM5.SA"
head(USIM5.SA)
## USIM5.SA.Open USIM5.SA.High USIM5.SA.Low USIM5.SA.Close
## 2007-01-02 17.7089 18.1444 17.4733 17.9556
## 2007-01-03 17.7800 17.8444 17.1556 17.3333
## 2007-01-04 17.3311 17.3311 16.8911 17.1222
## 2007-01-05 16.9978 17.0667 16.2444 16.2444
## 2007-01-08 16.5111 16.7333 16.1156 16.4000
## 2007-01-09 16.4511 16.6533 15.8467 16.1556
## USIM5.SA.Volume USIM5.SA.Adjusted
## 2007-01-02 2535750 15.86188
## 2007-01-03 6224850 15.31215
## 2007-01-04 4378500 15.12566
## 2007-01-05 4972500 14.35022
## 2007-01-08 5725800 14.48767
## 2007-01-09 7182450 14.27177
tail(USIM5.SA)
## USIM5.SA.Open USIM5.SA.High USIM5.SA.Low USIM5.SA.Close
## 2019-09-25 7.89 8.09 7.77 8.05
## 2019-09-26 8.10 8.17 7.96 8.04
## 2019-09-27 8.05 8.10 7.90 8.00
## 2019-09-30 7.98 7.98 7.81 7.81
## 2019-10-01 7.68 7.82 7.65 7.74
## 2019-10-02 7.65 7.68 7.46 7.48
## USIM5.SA.Volume USIM5.SA.Adjusted
## 2019-09-25 12112600 8.05
## 2019-09-26 8803100 8.04
## 2019-09-27 7618800 8.00
## 2019-09-30 12398600 7.81
## 2019-10-01 16711400 7.74
## 2019-10-02 9671600 7.48
USIM5.SA.A <- USIM5.SA$USIM5.SA.Adjusted
autoplot(USIM5.SA.A)
chartSeries(USIM5.SA)
^BVSP
getSymbols('^BVSP',src='yahoo') # pelo {quantmod}
## [1] "^BVSP"
chartSeries(BVSP)
require(stockPortfolio)
head(PETR4.SA)
## PETR4.SA.Open PETR4.SA.High PETR4.SA.Low PETR4.SA.Close
## 2007-01-02 25.000 25.225 24.880 25.160
## 2007-01-03 25.080 25.200 24.005 24.395
## 2007-01-04 24.250 24.375 23.700 23.850
## 2007-01-05 23.600 23.995 22.550 23.125
## 2007-01-08 23.250 23.570 22.900 23.395
## 2007-01-09 22.985 23.195 22.305 22.680
## PETR4.SA.Volume PETR4.SA.Adjusted
## 2007-01-02 10244800 19.29404
## 2007-01-03 19898600 18.70740
## 2007-01-04 21060200 18.28946
## 2007-01-05 24864000 17.73349
## 2007-01-08 19440200 17.94054
## 2007-01-09 25883600 17.39224
tail(PETR4.SA)
## PETR4.SA.Open PETR4.SA.High PETR4.SA.Low PETR4.SA.Close
## 2019-09-25 27.03 27.35 26.78 27.34
## 2019-09-26 27.40 27.70 27.23 27.70
## 2019-09-27 27.55 27.95 27.52 27.66
## 2019-09-30 27.56 27.64 27.40 27.55
## 2019-10-01 27.60 27.73 27.36 27.51
## 2019-10-02 27.22 27.26 26.61 26.72
## PETR4.SA.Volume PETR4.SA.Adjusted
## 2019-09-25 29038400 27.34
## 2019-09-26 33705900 27.70
## 2019-09-27 31886800 27.66
## 2019-09-30 23143400 27.55
## 2019-10-01 32423200 27.51
## 2019-10-02 51261200 26.72
PETR4.SA.Close <- PETR4.SA$PETR4.SA.Close
plot(PETR4.SA.Close)
# Ver que tem outliers devido aos NAs e valores inconsistentes que necessitam correção
# primeiro mudarei os zeros por NA, depois uso locf para tirar os NA
PETR4_clean<-is.na(PETR4.SA.Close) <- PETR4.SA.Close == 0
PETR4_clean<-na.locf(PETR4.SA.Close, fromLast = FALSE, coredata = NULL)
max(PETR4_clean)
## [1] 52.51
min(PETR4_clean)
## [1] 4.2
USIM5.SA.Close <- USIM5.SA$USIM5.SA.Close
plot(USIM5.SA.Close)
USIM5_clean<-is.na(USIM5.SA.Close) <- USIM5.SA.Close == 0
USIM5_clean<-na.locf(USIM5.SA.Close, fromLast = FALSE, coredata = NULL)
max(USIM5_clean)
## [1] 47.3
min(USIM5_clean)
## [1] 0.85
BVSP.Close <- BVSP$BVSP.Close
BVSP_clean<-is.na(BVSP.Close) <- BVSP.Close == 0
BVSP_clean<-na.locf(BVSP.Close, fromLast = FALSE, coredata = NULL)
max(BVSP_clean)
## [1] 105817
min(BVSP_clean)
## [1] 29435
plot(BVSP_clean)
dados_basicos<-cbind.xts(BVSP_clean,PETR4_clean)
library(dygraphs)
dygraph(window(PETR4_clean,start = "2010-01-01"),
main="Valor da PETR4.SA após limpeza")
dygraph(window(USIM5_clean,start = "2010-01-01"),
main="Valor da USIM5.SA após limpeza")
dygraph(window(BVSP_clean,start = "2010-01-01"),
main="Valor da BVSP após limpeza")
r_PETR4 <- dailyReturn(PETR4_clean,type = "log")
plot(r_PETR4)
hist(r_PETR4,100)
max(r_PETR4)
## [1] 0.152217
min(r_PETR4)
## [1] -0.17149
# ainda tem um outlier bem forte negativo
r_USIM5 <- dailyReturn(USIM5_clean,type = "log")
plot(r_USIM5)
hist(r_USIM5,100)
max(r_USIM5)
## [1] 0.3008923
min(r_USIM5)
## [1] -0.1759781
r_BVSP<-dailyReturn(BVSP_clean,type = "log")
plot(r_BVSP)
hist(r_BVSP,100)
max(r_BVSP)
## [1] 0.1367661
min(r_BVSP)
## [1] -0.1209605
## vamos encurtar um pouco a ST para termos uma informação mais relevante
r_PETR4_pos2010 <- window(r_PETR4, start = "2010-01-01")
plot(r_PETR4_pos2010)
hist(r_PETR4_pos2010,100)
max(r_PETR4_pos2010)
## [1] 0.1508583
min(r_PETR4_pos2010)
## [1] -0.17149
r_USIM5_pos2010 <- window(r_USIM5, start = "2010-01-01")
plot(r_USIM5_pos2010)
hist(r_USIM5_pos2010,100)
max(r_USIM5_pos2010)
## [1] 0.3008923
min(r_USIM5_pos2010)
## [1] -0.1759781
r_BVSP_pos2010<-window(r_BVSP,start = "2010-01-01")
plot(r_BVSP_pos2010)
hist(r_BVSP_pos2010,100)
max(r_BVSP_pos2010)
## [1] 0.06388665
min(r_BVSP_pos2010)
## [1] -0.09210685
# Correlograma de r_PETR4_pos2010
BETS::corrgram(r_PETR4_pos2010, lag.max = 36, mode = "bartlett", style="normal", knit = T)
# Função de autocorrelação parcial de r_PETR4_pos2010
BETS::corrgram(r_PETR4_pos2010, lag.max = 36, type = "partial", style="normal", knit = T)
# opção nativa para ACF e PACF
acf(r_PETR4_pos2010) # observar alteração na escala do eixo vertical
pacf(r_PETR4_pos2010)
# Correlograma de r_USIM5_pos2010
BETS::corrgram(r_USIM5_pos2010, lag.max = 36, mode = "bartlett", style="normal", knit = T)
# Função de autocorrelação parcial de r_USIM5_pos2010
BETS::corrgram(r_USIM5_pos2010, lag.max = 36, type = "partial", style="normal", knit = T)
# Correlograma de r_BVSP_pos2010
BETS::corrgram(r_BVSP_pos2010, lag.max = 36, mode = "bartlett", style="normal", knit = T)
# Função de autocorrelação parcial de r_BVSP_pos2010
BETS::corrgram(r_BVSP_pos2010, lag.max = 36, type = "partial", style="normal", knit = T)
# Fat tails of returns
library(moments);library(normtest)
# Curtose
kurtosis(r_PETR4_pos2010)
## daily.returns
## 7.020553
kurtosis.norm.test(r_PETR4_pos2010)
##
## Kurtosis test for normality
##
## data: r_PETR4_pos2010
## T = 7.0206, p-value < 2.2e-16
# assimetria
skewness(r_PETR4_pos2010)
## daily.returns
## -0.1465884
skewness.norm.test(r_PETR4_pos2010)
##
## Skewness test for normality
##
## data: r_PETR4_pos2010
## T = -0.14659, p-value = 0.003
# Jarque-Bera
jb.norm.test(r_PETR4_pos2010)
##
## Jarque-Bera test for normality
##
## data: r_PETR4_pos2010
## JB = 1639.3, p-value < 2.2e-16
# Fat tails of returns
# Curtose
kurtosis(r_USIM5_pos2010)
## daily.returns
## 8.463703
kurtosis.norm.test(r_USIM5_pos2010)
##
## Kurtosis test for normality
##
## data: r_USIM5_pos2010
## T = 8.4637, p-value < 2.2e-16
# assimetria
skewness(r_USIM5_pos2010)
## daily.returns
## 0.573222
skewness.norm.test(r_USIM5_pos2010)
##
## Skewness test for normality
##
## data: r_USIM5_pos2010
## T = 0.57322, p-value < 2.2e-16
# Jarque-Bera
jb.norm.test(r_USIM5_pos2010)
##
## Jarque-Bera test for normality
##
## data: r_USIM5_pos2010
## JB = 3143.9, p-value < 2.2e-16
# Fat tails of returns
# Curtose
kurtosis(r_BVSP_pos2010)
## daily.returns
## 4.899375
kurtosis.norm.test(r_BVSP_pos2010)
##
## Kurtosis test for normality
##
## data: r_BVSP_pos2010
## T = 4.8994, p-value < 2.2e-16
# assimetria
skewness(r_BVSP_pos2010)
## daily.returns
## -0.1551278
skewness.norm.test(r_BVSP_pos2010)
##
## Skewness test for normality
##
## data: r_BVSP_pos2010
## T = -0.15513, p-value = 0.001
# Jarque-Bera
jb.norm.test(r_BVSP_pos2010)
##
## Jarque-Bera test for normality
##
## data: r_BVSP_pos2010
## JB = 373.63, p-value < 2.2e-16
require(urca)
# Level
adf.none <- ur.df(y = r_PETR4_pos2010,
type = c("none"),
lags = 24, selectlags = "AIC")
summary(adf.none)
##
## ###############################################
## # Augmented Dickey-Fuller Test Unit Root Test #
## ###############################################
##
## Test regression none
##
##
## Call:
## lm(formula = z.diff ~ z.lag.1 - 1 + z.diff.lag)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.16983 -0.01480 0.00010 0.01393 0.14985
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## z.lag.1 -0.9851123 0.0495661 -19.875 <2e-16 ***
## z.diff.lag1 -0.0296744 0.0456357 -0.650 0.5156
## z.diff.lag2 -0.0106835 0.0408067 -0.262 0.7935
## z.diff.lag3 0.0006969 0.0355072 0.020 0.9843
## z.diff.lag4 -0.0146721 0.0291019 -0.504 0.6142
## z.diff.lag5 0.0397461 0.0204257 1.946 0.0518 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.02802 on 2390 degrees of freedom
## Multiple R-squared: 0.5117, Adjusted R-squared: 0.5105
## F-statistic: 417.4 on 6 and 2390 DF, p-value: < 2.2e-16
##
##
## Value of test-statistic is: -19.8747
##
## Critical values for test statistics:
## 1pct 5pct 10pct
## tau1 -2.58 -1.95 -1.62
BETS::corrgram(adf.none@res, lag.max = 36, mode = "bartlett", style="normal", knit = T)
require(urca)
# Level
adf.none <- ur.df(y = r_USIM5_pos2010,
type = c("none"),
lags = 24, selectlags = "AIC")
summary(adf.none)
##
## ###############################################
## # Augmented Dickey-Fuller Test Unit Root Test #
## ###############################################
##
## Test regression none
##
##
## Call:
## lm(formula = z.diff ~ z.lag.1 - 1 + z.diff.lag)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.173882 -0.019786 -0.001275 0.016939 0.298904
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## z.lag.1 -0.95329 0.02791 -34.154 <2e-16 ***
## z.diff.lag 0.02215 0.02044 1.084 0.278
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.03475 on 2394 degrees of freedom
## Multiple R-squared: 0.4665, Adjusted R-squared: 0.466
## F-statistic: 1047 on 2 and 2394 DF, p-value: < 2.2e-16
##
##
## Value of test-statistic is: -34.1543
##
## Critical values for test statistics:
## 1pct 5pct 10pct
## tau1 -2.58 -1.95 -1.62
BETS::corrgram(adf.none@res, lag.max = 36, mode = "bartlett", style="normal", knit = T)
require(urca)
# Level
adf.none <- ur.df(y = r_BVSP_pos2010,
type = c("none"),
lags = 24, selectlags = "AIC")
summary(adf.none)
##
## ###############################################
## # Augmented Dickey-Fuller Test Unit Root Test #
## ###############################################
##
## Test regression none
##
##
## Call:
## lm(formula = z.diff ~ z.lag.1 - 1 + z.diff.lag)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.092200 -0.008152 0.000256 0.008651 0.063727
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## z.lag.1 -1.013743 0.029031 -34.920 <2e-16 ***
## z.diff.lag 0.006919 0.020449 0.338 0.735
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.01414 on 2394 degrees of freedom
## Multiple R-squared: 0.503, Adjusted R-squared: 0.5026
## F-statistic: 1211 on 2 and 2394 DF, p-value: < 2.2e-16
##
##
## Value of test-statistic is: -34.9196
##
## Critical values for test statistics:
## 1pct 5pct 10pct
## tau1 -2.58 -1.95 -1.62
BETS::corrgram(adf.none@res, lag.max = 36, mode = "bartlett", style="normal", knit = T)
ts.plot(r_PETR4_pos2010)
abline(h = mean(r_PETR4_pos2010),col=2, lwd=2)
vol_PETR4<-r_PETR4_pos2010^2
plot(vol_PETR4)
vol_USIM5<-r_USIM5_pos2010^2
plot(vol_USIM5)
vol_BVSP<-r_BVSP_pos2010^2
plot(vol_BVSP)
# teste para efeitos ARCH no {FinTS}
library(FinTS)
ArchTest(r_PETR4_pos2010)
##
## ARCH LM-test; Null hypothesis: no ARCH effects
##
## data: r_PETR4_pos2010
## Chi-squared = 256.67, df = 12, p-value < 2.2e-16
ArchTest(r_USIM5_pos2010)
##
## ARCH LM-test; Null hypothesis: no ARCH effects
##
## data: r_USIM5_pos2010
## Chi-squared = 246.59, df = 12, p-value < 2.2e-16
ArchTest(r_BVSP_pos2010)
##
## ARCH LM-test; Null hypothesis: no ARCH effects
##
## data: r_BVSP_pos2010
## Chi-squared = 101.46, df = 12, p-value = 2.878e-16
library(rugarch)
garch11.spec = ugarchspec(mean.model = list(armaOrder = c(2,2),
include.mean=TRUE),
variance.model = list(garchOrder = c(1,1),
model = "sGARCH"))
garch.fit = ugarchfit(garch11.spec,
data = r_PETR4_pos2010*100,
fit.control=list(scale=TRUE),
distribution.model = "norm")
## Warning in arima(data, order = c(modelinc[2], 0, modelinc[3]), include.mean
## = modelinc[1], : possible convergence problem: optim gave code = 1
print(garch.fit)
##
## *---------------------------------*
## * GARCH Model Fit *
## *---------------------------------*
##
## Conditional Variance Dynamics
## -----------------------------------
## GARCH Model : sGARCH(1,1)
## Mean Model : ARFIMA(2,0,2)
## Distribution : norm
##
## Optimal Parameters
## ------------------------------------
## Estimate Std. Error t value Pr(>|t|)
## mu -0.004017 0.046794 -0.085844 0.931590
## ar1 -0.985290 0.018915 -52.091570 0.000000
## ar2 -0.958289 0.007288 -131.483257 0.000000
## ma1 0.976372 0.014279 68.375909 0.000000
## ma2 0.968387 0.002128 455.115294 0.000000
## omega 0.150207 0.037971 3.955829 0.000076
## alpha1 0.067363 0.010621 6.342152 0.000000
## beta1 0.912420 0.014085 64.778347 0.000000
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu -0.004017 0.050307 -0.079849 0.936358
## ar1 -0.985290 0.018066 -54.537987 0.000000
## ar2 -0.958289 0.006828 -140.351691 0.000000
## ma1 0.976372 0.014108 69.206055 0.000000
## ma2 0.968387 0.001307 740.982340 0.000000
## omega 0.150207 0.066019 2.275213 0.022893
## alpha1 0.067363 0.018990 3.547369 0.000389
## beta1 0.912420 0.022872 39.891705 0.000000
##
## LogLikelihood : -5668.525
##
## Information Criteria
## ------------------------------------
##
## Akaike 4.6894
## Bayes 4.7085
## Shibata 4.6894
## Hannan-Quinn 4.6964
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.07085 0.7901
## Lag[2*(p+q)+(p+q)-1][11] 3.17928 1.0000
## Lag[4*(p+q)+(p+q)-1][19] 6.26215 0.9590
## d.o.f=4
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.2584 0.6112
## Lag[2*(p+q)+(p+q)-1][5] 0.6413 0.9339
## Lag[4*(p+q)+(p+q)-1][9] 1.2063 0.9761
## d.o.f=2
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[3] 0.1169 0.500 2.000 0.7324
## ARCH Lag[5] 0.8240 1.440 1.667 0.7858
## ARCH Lag[7] 1.0154 2.315 1.543 0.9110
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 2.6619
## Individual Statistics:
## mu 0.53254
## ar1 0.06217
## ar2 0.07813
## ma1 0.03444
## ma2 0.05402
## omega 0.13723
## alpha1 0.47822
## beta1 0.25534
##
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic: 1.89 2.11 2.59
## Individual Statistic: 0.35 0.47 0.75
##
## Sign Bias Test
## ------------------------------------
## t-value prob sig
## Sign Bias 0.2121 0.8321
## Negative Sign Bias 0.2131 0.8312
## Positive Sign Bias 0.2699 0.7873
## Joint Effect 0.5302 0.9122
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 78.79 3.009e-09
## 2 30 93.96 8.852e-09
## 3 40 101.61 1.744e-07
## 4 50 112.02 7.694e-07
##
##
## Elapsed time : 1.320244
coef(garch.fit)
## mu ar1 ar2 ma1 ma2
## -0.004016979 -0.985289871 -0.958289407 0.976372108 0.968386599
## omega alpha1 beta1
## 0.150206770 0.067362982 0.912420154
####### Diagnóstico #######################
# 1 Time SeriesPlot
# 2 Conditional Standard Deviation Plot
# 3 Series Plot with 2 Conditional SD Superimposed
# 4 Autocorrelation function Plot of Observations
# 5 Autocorrelation function Plot of Squared Observations
# 6 Cross Correlation Plot
# 7 Residuals Plot
# 8 Conditional Standard Deviations Plot
# 9 Standardized Residuals Plot
# 10 ACF Plot of Standardized Residuals
# 11 ACF Plot of Squared Standardized Residuals
# 12 Cross Correlation Plot between $r^2$ and r
# 13 Quantile-Quantile Plot of Standardized Residuals
plot(garch.fit, which = "all")
##
## please wait...calculating quantiles...
plot(garch.fit, which = 1)
plot(garch.fit, which = 2)
##
## please wait...calculating quantiles...
plot(garch.fit, which = 3)
plot(garch.fit, which = 4)
plot(garch.fit, which = 5)
plot(garch.fit, which = 6)
plot(garch.fit, which = 7)
plot(garch.fit, which = 8)
####### plot in-sample and forecast 100-steps-ahead ############
# plot in-sample volatility estimates
hist <- ts(r_PETR4_pos2010,
start=c(2010,1,1),
frequency = 252)
fitted <- ts(garch.fit@fit$fitted.values,
start=c(2010,1,1),
frequency = 252)
ts.plot(r_PETR4_pos2010,fitted,col=c("red","blue"))
##### Obtain fitted volatility series
v1=sigma(garch.fit)
ts.plot(as.numeric(v1))
# Time Series Prediction (unconditional)
previsao <- ugarchforecast(garch.fit,n.ahead = 50)
plot(previsao,which = 1)
#fpm(previsao)
# Sigma Prediction (unconditional)
previsao <- ugarchforecast(garch.fit,n.ahead = 50)
plot(previsao,which = 3)
require(rugarch)
garch11.spec1 = ugarchspec(variance.model = list(garchOrder = c(1,1),
model="sGARCH"),
mean.model = list(armaOrder=c(2,2),
include.mean=TRUE))
fit1 = ugarchfit(data = r_PETR4_pos2010*100,
spec = garch11.spec1)
show(fit1)
##
## *---------------------------------*
## * GARCH Model Fit *
## *---------------------------------*
##
## Conditional Variance Dynamics
## -----------------------------------
## GARCH Model : sGARCH(1,1)
## Mean Model : ARFIMA(2,0,2)
## Distribution : norm
##
## Optimal Parameters
## ------------------------------------
## Estimate Std. Error t value Pr(>|t|)
## mu -0.004019 0.046794 -0.085887 0.931556
## ar1 -0.985289 0.018912 -52.097504 0.000000
## ar2 -0.958291 0.007288 -131.485536 0.000000
## ma1 0.976371 0.014278 68.384556 0.000000
## ma2 0.968388 0.002128 455.154466 0.000000
## omega 0.150206 0.037973 3.955657 0.000076
## alpha1 0.067361 0.010622 6.341904 0.000000
## beta1 0.912423 0.014085 64.777460 0.000000
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu -0.004019 0.050306 -0.07989 0.936325
## ar1 -0.985289 0.018063 -54.54621 0.000000
## ar2 -0.958291 0.006828 -140.35482 0.000000
## ma1 0.976371 0.014106 69.21756 0.000000
## ma2 0.968388 0.001307 740.99916 0.000000
## omega 0.150206 0.066024 2.27501 0.022905
## alpha1 0.067361 0.018990 3.54715 0.000389
## beta1 0.912423 0.022874 39.88937 0.000000
##
## LogLikelihood : -5668.525
##
## Information Criteria
## ------------------------------------
##
## Akaike 4.6894
## Bayes 4.7085
## Shibata 4.6894
## Hannan-Quinn 4.6964
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.07085 0.7901
## Lag[2*(p+q)+(p+q)-1][11] 3.17929 1.0000
## Lag[4*(p+q)+(p+q)-1][19] 6.26215 0.9590
## d.o.f=4
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.2584 0.6112
## Lag[2*(p+q)+(p+q)-1][5] 0.6413 0.9339
## Lag[4*(p+q)+(p+q)-1][9] 1.2063 0.9761
## d.o.f=2
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[3] 0.1169 0.500 2.000 0.7324
## ARCH Lag[5] 0.8241 1.440 1.667 0.7858
## ARCH Lag[7] 1.0154 2.315 1.543 0.9110
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 2.6618
## Individual Statistics:
## mu 0.53252
## ar1 0.06218
## ar2 0.07813
## ma1 0.03444
## ma2 0.05402
## omega 0.13727
## alpha1 0.47826
## beta1 0.25537
##
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic: 1.89 2.11 2.59
## Individual Statistic: 0.35 0.47 0.75
##
## Sign Bias Test
## ------------------------------------
## t-value prob sig
## Sign Bias 0.2121 0.8321
## Negative Sign Bias 0.2132 0.8312
## Positive Sign Bias 0.2698 0.7873
## Joint Effect 0.5302 0.9122
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 78.79 3.009e-09
## 2 30 93.96 8.852e-09
## 3 40 101.61 1.744e-07
## 4 50 112.02 7.694e-07
##
##
## Elapsed time : 1.13735
##### Obtendo serie de volatility ajustada
v1=sigma(fit1)
ts.plot(as.numeric(v1))
# Previsao da Serie temporal (incondicional)
previsao.fit1 <- ugarchforecast(fit1,n.ahead = 50)
plot(previsao.fit1, which = 1)
# Sigma Prediction (unconditional)
plot(previsao.fit1, which = 3)
# Acuracia
require(Metrics)
predicted<-ts(fitted(fit1))
actual<-ts(r_PETR4_pos2010*100)
(smape<-smape(actual, predicted))
## [1] 1.789236
(rmse<-rmse(actual, predicted))
## [1] 2.795217
require(rugarch)
archm.spec1 = ugarchspec(variance.model = list(model="sGARCH"),
mean.model = list(armaOrder=c(2,2),
archm=TRUE))
fit.ARCHM = ugarchfit(data = r_PETR4_pos2010*100,
spec = archm.spec1)
show(fit.ARCHM)
##
## *---------------------------------*
## * GARCH Model Fit *
## *---------------------------------*
##
## Conditional Variance Dynamics
## -----------------------------------
## GARCH Model : sGARCH(1,1)
## Mean Model : ARFIMA(2,0,2)
## Distribution : norm
##
## Optimal Parameters
## ------------------------------------
## Estimate Std. Error t value Pr(>|t|)
## mu -0.223266 0.249215 -0.89588 0.370317
## ar1 -0.985050 0.018590 -52.98885 0.000000
## ar2 -0.958114 0.007641 -125.39286 0.000000
## ma1 0.976096 0.014128 69.08948 0.000000
## ma2 0.968240 0.001861 520.26670 0.000000
## archm 0.096585 0.108002 0.89428 0.371171
## omega 0.148369 0.036026 4.11843 0.000038
## alpha1 0.067416 0.010026 6.72382 0.000000
## beta1 0.912657 0.013156 69.37126 0.000000
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu -0.223266 0.379508 -0.58830 0.556327
## ar1 -0.985050 0.017523 -56.21581 0.000000
## ar2 -0.958114 0.007364 -130.10034 0.000000
## ma1 0.976096 0.013747 71.00535 0.000000
## ma2 0.968240 0.001994 485.59167 0.000000
## archm 0.096585 0.162790 0.59331 0.552975
## omega 0.148369 0.060939 2.43471 0.014904
## alpha1 0.067416 0.017523 3.84735 0.000119
## beta1 0.912657 0.020433 44.66523 0.000000
##
## LogLikelihood : -5667.747
##
## Information Criteria
## ------------------------------------
##
## Akaike 4.6896
## Bayes 4.7111
## Shibata 4.6896
## Hannan-Quinn 4.6974
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.07173 0.7888
## Lag[2*(p+q)+(p+q)-1][11] 3.18335 1.0000
## Lag[4*(p+q)+(p+q)-1][19] 6.30959 0.9565
## d.o.f=4
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.2771 0.5986
## Lag[2*(p+q)+(p+q)-1][5] 0.6500 0.9323
## Lag[4*(p+q)+(p+q)-1][9] 1.2001 0.9764
## d.o.f=2
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[3] 0.1225 0.500 2.000 0.7263
## ARCH Lag[5] 0.7879 1.440 1.667 0.7968
## ARCH Lag[7] 0.9551 2.315 1.543 0.9208
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 2.5833
## Individual Statistics:
## mu 0.35822
## ar1 0.06154
## ar2 0.07742
## ma1 0.03441
## ma2 0.05302
## archm 0.25095
## omega 0.15317
## alpha1 0.48685
## beta1 0.26630
##
## 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.2478 0.8043
## Negative Sign Bias 0.3999 0.6893
## Positive Sign Bias 0.5247 0.5998
## Joint Effect 0.5269 0.9129
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 86.65 1.294e-10
## 2 30 100.13 9.334e-10
## 3 40 111.89 5.802e-09
## 4 50 130.45 2.516e-09
##
##
## Elapsed time : 2.779408
##### Obtendo serie de volatility ajustada
v2=sigma(fit.ARCHM)
ts.plot(as.numeric(v2))
#Previsao da Serie temporal (incondicional)
previsao.fitARCHM <- ugarchforecast(fit.ARCHM,n.ahead = 50)
plot(previsao.fitARCHM, which = 1)
# Sigma prediction (unconditional)
plot(previsao.fitARCHM, which = 3)
# Acuracia
predicted2<-ts(fitted(fit.ARCHM))
actual<-ts(r_PETR4_pos2010*100)
(smape<-smape(actual, predicted2))
## [1] 1.777007
(rmse<-rmse(actual, predicted2))
## [1] 2.796718
require(rugarch)
egarch.spec1 = ugarchspec(variance.model = list(model="eGARCH"),
mean.model = list(armaOrder=c(2,2)))
fit.eGARCH = ugarchfit(data = r_PETR4_pos2010*100,
spec = egarch.spec1)
show(fit.eGARCH)
##
## *---------------------------------*
## * GARCH Model Fit *
## *---------------------------------*
##
## Conditional Variance Dynamics
## -----------------------------------
## GARCH Model : eGARCH(1,1)
## Mean Model : ARFIMA(2,0,2)
## Distribution : norm
##
## Optimal Parameters
## ------------------------------------
## Estimate Std. Error t value Pr(>|t|)
## mu -0.038900 0.023925 -1.6259 0.103971
## ar1 -1.565549 0.014215 -110.1304 0.000000
## ar2 -0.814752 0.018522 -43.9890 0.000000
## ma1 1.531517 0.017078 89.6767 0.000000
## ma2 0.770265 0.019807 38.8885 0.000000
## omega 0.045785 0.001775 25.7917 0.000000
## alpha1 -0.019580 0.008831 -2.2171 0.026614
## beta1 0.978611 0.000295 3315.0089 0.000000
## gamma1 0.105257 0.002885 36.4840 0.000000
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu -0.038900 0.014114 -2.7562 0.005848
## ar1 -1.565549 0.007061 -221.7303 0.000000
## ar2 -0.814752 0.009108 -89.4591 0.000000
## ma1 1.531517 0.010536 145.3614 0.000000
## ma2 0.770265 0.011969 64.3525 0.000000
## omega 0.045785 0.004137 11.0659 0.000000
## alpha1 -0.019580 0.013921 -1.4065 0.159567
## beta1 0.978611 0.001093 894.9690 0.000000
## gamma1 0.105257 0.008134 12.9396 0.000000
##
## LogLikelihood : -5678.681
##
## Information Criteria
## ------------------------------------
##
## Akaike 4.6986
## Bayes 4.7202
## Shibata 4.6986
## Hannan-Quinn 4.7065
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.8364 0.3604
## Lag[2*(p+q)+(p+q)-1][11] 2.8615 1.0000
## Lag[4*(p+q)+(p+q)-1][19] 6.9755 0.9104
## d.o.f=4
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.2433 0.6218
## Lag[2*(p+q)+(p+q)-1][5] 1.4689 0.7476
## Lag[4*(p+q)+(p+q)-1][9] 2.1531 0.8860
## d.o.f=2
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[3] 0.07238 0.500 2.000 0.7879
## ARCH Lag[5] 1.26723 1.440 1.667 0.6556
## ARCH Lag[7] 1.45763 2.315 1.543 0.8298
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 3.1609
## Individual Statistics:
## mu 0.77736
## ar1 0.12391
## ar2 0.18947
## ma1 0.12145
## ma2 0.19087
## omega 0.15022
## alpha1 0.05083
## beta1 0.36133
## gamma1 0.10045
##
## 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.5072 0.6121
## Negative Sign Bias 1.0756 0.2822
## Positive Sign Bias 0.1293 0.8971
## Joint Effect 1.1747 0.7591
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 74.32 1.732e-08
## 2 30 74.80 6.504e-06
## 3 40 87.00 1.607e-05
## 4 50 103.23 9.700e-06
##
##
## Elapsed time : 1.930895
plot(fit.eGARCH, which = "all")
##
## please wait...calculating quantiles...
##### Obtendo serie de volatility ajustada
v3=sigma(fit.eGARCH)
ts.plot(as.numeric(v3))
# previsao da serie temporal (incondicional)
previsao.eGARCH <- ugarchforecast(fit.eGARCH,n.ahead = 50)
plot(previsao.eGARCH, which = 1)
# Sigma Prediction (unconditional)
plot(previsao.eGARCH, which = 3)
predicted3<-ts(fitted(fit.eGARCH))
actual<-ts(r_PETR4_pos2010*100)
(smape<-smape(actual, predicted3))
## [1] 1.783881
(rmse<-rmse(actual, predicted3))
## [1] 2.793125
require(rugarch)
gjrgarch.spec1 = ugarchspec(variance.model = list(model="gjrGARCH"),
mean.model = list(armaOrder=c(2,2)))
fit.gjrGARCH = ugarchfit(data = r_PETR4_pos2010*100,
spec = gjrgarch.spec1)
show(fit.gjrGARCH)
##
## *---------------------------------*
## * GARCH Model Fit *
## *---------------------------------*
##
## Conditional Variance Dynamics
## -----------------------------------
## GARCH Model : gjrGARCH(1,1)
## Mean Model : ARFIMA(2,0,2)
## Distribution : norm
##
## Optimal Parameters
## ------------------------------------
## Estimate Std. Error t value Pr(>|t|)
## mu -0.011321 0.046875 -2.4152e-01 0.809150
## ar1 -1.141242 0.021288 -5.3609e+01 0.000000
## ar2 -0.169164 0.020825 -8.1233e+00 0.000000
## ma1 1.120346 0.000006 1.9543e+05 0.000000
## ma2 0.149273 0.000944 1.5809e+02 0.000000
## omega 0.162841 0.039829 4.0886e+00 0.000043
## alpha1 0.064034 0.011611 5.5150e+00 0.000000
## beta1 0.908395 0.014072 6.4556e+01 0.000000
## gamma1 0.011529 0.015907 7.2479e-01 0.468581
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu -0.011321 0.049275 -0.22976 0.818278
## ar1 -1.141242 0.020884 -54.64598 0.000000
## ar2 -0.169164 0.020492 -8.25529 0.000000
## ma1 1.120346 0.000022 50603.61000 0.000000
## ma2 0.149273 0.002635 56.64717 0.000000
## omega 0.162841 0.071329 2.28295 0.022433
## alpha1 0.064034 0.015650 4.09168 0.000043
## beta1 0.908395 0.023736 38.27078 0.000000
## gamma1 0.011529 0.028351 0.40665 0.684262
##
## LogLikelihood : -5673.032
##
## Information Criteria
## ------------------------------------
##
## Akaike 4.6940
## Bayes 4.7155
## Shibata 4.6939
## Hannan-Quinn 4.7018
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.04268 0.8363
## Lag[2*(p+q)+(p+q)-1][11] 5.56393 0.7604
## Lag[4*(p+q)+(p+q)-1][19] 9.58575 0.5384
## d.o.f=4
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.3452 0.5568
## Lag[2*(p+q)+(p+q)-1][5] 0.8607 0.8903
## Lag[4*(p+q)+(p+q)-1][9] 1.5064 0.9550
## d.o.f=2
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[3] 0.1887 0.500 2.000 0.6640
## ARCH Lag[5] 1.0771 1.440 1.667 0.7102
## ARCH Lag[7] 1.2857 2.315 1.543 0.8631
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 3.2096
## Individual Statistics:
## mu 0.6058
## ar1 0.1241
## ar2 0.2190
## ma1 0.1223
## ma2 0.2209
## omega 0.1555
## alpha1 0.5081
## beta1 0.2784
## gamma1 0.7345
##
## 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.01205 0.9904
## Negative Sign Bias 0.14970 0.8810
## Positive Sign Bias 0.34327 0.7314
## Joint Effect 0.24681 0.9697
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 115.4 7.880e-16
## 2 30 129.5 1.158e-14
## 3 40 145.4 3.490e-14
## 4 50 152.3 1.626e-12
##
##
## Elapsed time : 2.773414
##### Obtendo serie de volatility ajustada
v4=sigma(fit.gjrGARCH)
ts.plot(as.numeric(v4))
# previsao da serie temporal (incondicional)
previsao.gjrGARCH <- ugarchforecast(fit.gjrGARCH,n.ahead = 50)
plot(previsao.gjrGARCH,which=1)
# Sigma Prediction (unconditional)
plot(previsao.gjrGARCH, which = 3)
predicted4<-ts(fitted(fit.gjrGARCH))
actual<-ts(r_PETR4_pos2010*100)
(smape<-smape(actual, predicted4))
## [1] 1.903512
(rmse<-rmse(actual, predicted4))
## [1] 2.797725
# install.packages("rugarch")
require(rugarch)
tarch11.spec = ugarchspec(mean.model = list(armaOrder = c(2,2),include.mean=TRUE),
variance.model = list(garchOrder = c(1,1),
model = "fGARCH",
submodel = "TGARCH"))
tarch.fit = ugarchfit(tarch11.spec,
data = r_PETR4_pos2010*100,
fit.control=list(scale=TRUE),
distribution.model = "norm")
## Warning in arima(data, order = c(modelinc[2], 0, modelinc[3]), include.mean
## = modelinc[1], : possible convergence problem: optim gave code = 1
print(tarch.fit)
##
## *---------------------------------*
## * GARCH Model Fit *
## *---------------------------------*
##
## Conditional Variance Dynamics
## -----------------------------------
## GARCH Model : fGARCH(1,1)
## fGARCH Sub-Model : TGARCH
## Mean Model : ARFIMA(2,0,2)
## Distribution : norm
##
## Optimal Parameters
## ------------------------------------
## Estimate Std. Error t value Pr(>|t|)
## mu -0.118193 0.000102 -1157.5506 0.000000
## ar1 0.028663 0.000144 199.2592 0.000000
## ar2 0.968479 0.000250 3878.9314 0.000000
## ma1 -0.046492 0.000021 -2247.9059 0.000000
## ma2 -0.959282 0.000116 -8256.0921 0.000000
## omega 0.055229 0.020688 2.6695 0.007596
## alpha1 0.054165 0.011493 4.7128 0.000002
## beta1 0.937825 0.016546 56.6806 0.000000
## eta11 0.280039 0.080800 3.4658 0.000529
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu -0.118193 0.000278 -424.84928 0.00000
## ar1 0.028663 0.000406 70.66863 0.00000
## ar2 0.968479 0.000826 1173.01505 0.00000
## ma1 -0.046492 0.000036 -1275.38364 0.00000
## ma2 -0.959282 0.001266 -757.72853 0.00000
## omega 0.055229 0.175839 0.31409 0.75345
## alpha1 0.054165 0.103890 0.52137 0.60211
## beta1 0.937825 0.146527 6.40035 0.00000
## eta11 0.280039 0.219619 1.27511 0.20227
##
## LogLikelihood : -5669.496
##
## Information Criteria
## ------------------------------------
##
## Akaike 4.6910
## Bayes 4.7126
## Shibata 4.6910
## Hannan-Quinn 4.6989
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.0754 7.836e-01
## Lag[2*(p+q)+(p+q)-1][11] 9.3746 6.877e-07
## Lag[4*(p+q)+(p+q)-1][19] 14.7994 3.319e-02
## d.o.f=4
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.1225 0.7264
## Lag[2*(p+q)+(p+q)-1][5] 1.8679 0.6500
## Lag[4*(p+q)+(p+q)-1][9] 3.0612 0.7488
## d.o.f=2
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[3] 0.0478 0.500 2.000 0.8269
## ARCH Lag[5] 2.3523 1.440 1.667 0.3985
## ARCH Lag[7] 2.6710 2.315 1.543 0.5777
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 3.671
## Individual Statistics:
## mu 0.02335
## ar1 0.02303
## ar2 0.02264
## ma1 0.02347
## ma2 0.02410
## omega 0.12439
## alpha1 0.51277
## beta1 0.21082
## eta11 0.04649
##
## 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 1.4169 0.1566
## Negative Sign Bias 0.1704 0.8647
## Positive Sign Bias 0.8282 0.4076
## Joint Effect 2.4047 0.4928
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 86.78 1.227e-10
## 2 30 99.11 1.357e-09
## 3 40 101.11 2.047e-07
## 4 50 119.25 8.662e-08
##
##
## Elapsed time : 3.399057
coef(tarch.fit)
## mu ar1 ar2 ma1 ma2 omega
## -0.11819279 0.02866343 0.96847873 -0.04649185 -0.95928229 0.05522862
## alpha1 beta1 eta11
## 0.05416522 0.93782515 0.28003892
####### Diagnóstico #######################
plot(tarch.fit, which = "all")
##
## please wait...calculating quantiles...
####### plot in-sample and forecast 50-steps-ahead ############
# plot in-sample volatility estimates
hist <- ts(r_PETR4_pos2010,
start=c(2010,1,1),
frequency = 252)
fitted <- ts(tarch.fit@fit$fitted.values,
start=c(2010,1,1),
frequency = 252)
ts.plot(r_PETR4_pos2010,fitted,col=c("red","blue"))
# Time Series Prediction (unconditional)
previsao.tarch <- ugarchforecast(tarch.fit,n.ahead = 50)
plot(previsao.tarch,which = 1)
#fpm(previsao.tarch, summary = TRUE)
# Sigma Prediction (unconditional)
previsao.tarch <- ugarchforecast(tarch.fit,n.ahead = 50)
plot(previsao.tarch,which = 3)
predicted5<-ts(fitted(tarch.fit))
actual<-ts(r_PETR4_pos2010*100)
(smape<-smape(actual, predicted5))
## [1] 1.77058
(rmse<-rmse(actual, predicted5))
## [1] 2.795627
FERREIRA, Pedro Costa; SPERANZA, Talitha; COSTA, Jonatha (2018). BETS: Brazilian Economic Time Series. R package version 0.4.9. Disponível em: https://CRAN.R-project.org/package=BETS.
HYNDMAN, Rob. (2018). fpp2: Data for “Forecasting: Principles and Practice” (2nd Edition). R package version 2.3. Disponível em: https://CRAN.R-project.org/package=fpp2.
HYNDMAN, R.J.; ATHANASOPOULOS, G. (2018) Forecasting: principles and practice, 2nd edition, OTexts: Melbourne, Australia. Disponível em: https://otexts.com/fpp2/. Accessed on 12 Set 2019.
PERLIN, M.; RAMOS, H. (2016). GetHFData: A R Package for Downloading and Aggregating High Frequency Trading Data from Bovespa. Brazilian Review of Finance, V. 14, N. 3.