# AR, ARMA
#install.packages("forecast")
#install.packages("ggplot2")
# Charger les packages
library(forecast)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(ggplot2)
library(stats)
# modèle ARCH/GARCH
#install.packages("rugarch")
#install.package("FinTS)
library(FinTS) # pour la fonction ARCH, Test LM, vérifier la présence d'effet ARCH
## Le chargement a nécessité le package : zoo
##
## Attachement du package : 'zoo'
## Les objets suivants sont masqués depuis 'package:base':
##
## as.Date, as.Date.numeric
##
## Attachement du package : 'FinTS'
## L'objet suivant est masqué depuis 'package:forecast':
##
## Acf
library(rugarch) # Pratique pour les modèle de type ARCH
## Le chargement a nécessité le package : parallel
##
## Attachement du package : 'rugarch'
## L'objet suivant est masqué depuis 'package:stats':
##
## sigma
# Générer la série temporelle
set.seed(123) # Pour la reproductibilité
n <- 1000 # Longueur de la série
e <- rnorm(n) # Générer le bruit blanc
X <- e - 0.5 * c(0, e[-n]) # Générer la série moyenne mobile
plot(x=e,y=c(0, e[-n]))
# Calculer l'ACF et le PACF
par(mfrow=c(1,2))
acf_values <- acf(X, main="Corrélogramme simple", lag.max = 10, col="red")
pacf_values <- pacf(X, main="Corrélogramme partiel",lag.max = 10, col="red")
# Définir les paramètres du modèle
n <- 500 # Nombre d'observations
phi <- 0.6 # Coefficient AR(1)
theta <- -0.5 # Coefficient MA(1)
# Générer un bruit blanc de loi normale N(0,1)
set.seed(1234) # Pour la reproductibilité
epsilon <- rnorm(n)
# Simuler le processus ARMA(1,1)
X <- arima.sim(n = n, model = list(ar = phi, ma = theta), innov = epsilon)
# Afficher la série temporelle
plot(X, type = "l", col = "gray", lwd = 2, main = "Représentation des données de l'exemple",
xlab = "Temps", ylab = "X_t")
# Calculer l'ACF et le PACF
par(mfrow=c(1,2))
# Calculer et tracer l'ACF
acf(X, main="Corrélogramme simple", col="red",lag.max = 10)
# Calculer et tracer le PACF
pacf(X, main="corrélogramme partiel", col="red",lag.max = 10)
# Ajustement du modèle ARMA(1,1)
model <- Arima(X, order = c(1,0,1))
# Affichage des résultats
summary(model)
## Series: X
## ARIMA(1,0,1) with non-zero mean
##
## Coefficients:
## ar1 ma1 mean
## 0.3687 -0.1837 0.0018
## s.e. 0.2693 0.2870 0.0595
##
## sigma^2 = 1.067: log likelihood = -724.31
## AIC=1456.62 AICc=1456.7 BIC=1473.47
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 0.0005613991 1.03008 0.8118521 96.80518 123.197 0.7580308
## ACF1
## Training set 0.002634725
data("EuStockMarkets")
head(EuStockMarkets)
## Time Series:
## Start = c(1991, 130)
## End = c(1991, 135)
## Frequency = 260
## DAX SMI CAC FTSE
## 1991.496 1628.75 1678.1 1772.8 2443.6
## 1991.500 1613.63 1688.5 1750.5 2460.2
## 1991.504 1606.51 1678.6 1718.0 2448.2
## 1991.508 1621.04 1684.1 1708.1 2470.4
## 1991.512 1618.16 1686.6 1723.1 2484.7
## 1991.515 1610.61 1671.6 1714.3 2466.8
summary(EuStockMarkets)
## DAX SMI CAC FTSE
## Min. :1402 Min. :1587 Min. :1611 Min. :2281
## 1st Qu.:1744 1st Qu.:2166 1st Qu.:1875 1st Qu.:2843
## Median :2141 Median :2796 Median :1992 Median :3247
## Mean :2531 Mean :3376 Mean :2228 Mean :3566
## 3rd Qu.:2722 3rd Qu.:3812 3rd Qu.:2274 3rd Qu.:3994
## Max. :6186 Max. :8412 Max. :4388 Max. :6179
par(mfrow = c(2,2))
plot(EuStockMarkets[,'DAX'], type = "l", col = "yellow", lwd = 2, main = "Indice boursiers Allemagne",
xlab = "Temps", ylab = "Prix de clôture")
plot(EuStockMarkets[,'SMI'], type = "l", col = "red", lwd = 2, main = "Indice boursiers Suisse",
xlab = "Temps", ylab = "Prix de clôture")
plot(EuStockMarkets[,'CAC'], type = "l", col = "darkblue", lwd = 2, main = "Indice boursiers France",
xlab = "Temps", ylab = "Prix de clôture")
plot(EuStockMarkets[,'FTSE'], type = "l", col = "darkred", lwd = 2, main = "Indice boursiers Royaume Uni",
xlab = "Temps", ylab = "Prix de clôture")
Calcul du log des rendements. On commence par le log
plot(log(EuStockMarkets[,'DAX']), type = "l", col = "red", lwd = 2, main = "Indice boursiers Allemagne",
xlab = "Temps", ylab = "log du Prix de clôture")
data("EuStockMarkets")
df_rend = diff(log(EuStockMarkets[, "DAX"]))*100
df_rend = na.omit(df_rend)
Représentation graphique des rendements différenciés
par(mfrow=c(1,3))
# Représentation graphique
plot(df_rend, type = "l", col = "red", lwd = 2, main = "Représentation des rendements differencie",
xlab = "Temps", ylab = "X_t")
# Calculer et tracer l'ACF
acf(df_rend, main="Corrélogramme simple", col="red",lag.max = 30)
# Calculer et tracer le PACF
pacf(df_rend, main="corrélogramme partiel", col="red",lag.max = 30)
rend_2 = ts(df_rend^2) # rendement au carré
Analyse graphique
par(mfrow=c(1,3))
# Représentation graphique
plot(rend_2, type = "l", col = "red", lwd = 2, main = "série au carré",
xlab = "Temps", ylab = "X_t")
# Calculer et tracer l'ACF
acf(rend_2, main="Corrélogramme simple", col="red",lag.max = 30)
# Calculer et tracer le PACF
pacf(rend_2, main="corrélogramme partiel", col="red",lag.max = 30)
Test statistique pour vérifier l’effet ARCH
Box.test(rend_2,lag=2,type="Ljung-Box")
##
## Box-Ljung test
##
## data: rend_2
## X-squared = 66.271, df = 2, p-value = 4.108e-15
ArchTest(rend_2, lags=2)
##
## ARCH LM-test; Null hypothesis: no ARCH effects
##
## data: rend_2
## Chi-squared = 9.8504, df = 2, p-value = 0.007261
specification_model_1 = ugarchspec(variance.model = list(model="sGARCH", garchOrder=c(1,0)),
mean.model = list(armaOrder=c(0,0)),
distribution.model = "norm")
# Estimation du modèle
model_1 = ugarchfit(spec=specification_model_1,
data=df_rend)
infocriteria(model_1)
##
## Akaike 2.882582
## Bayes 2.891502
## Shibata 2.882576
## Hannan-Quinn 2.885869
print(model_1)
##
## *---------------------------------*
## * GARCH Model Fit *
## *---------------------------------*
##
## Conditional Variance Dynamics
## -----------------------------------
## GARCH Model : sGARCH(1,0)
## Mean Model : ARFIMA(0,0,0)
## Distribution : norm
##
## Optimal Parameters
## ------------------------------------
## Estimate Std. Error t value Pr(>|t|)
## mu 0.071817 0.023474 3.0594 0.002218
## omega 0.952798 0.037273 25.5625 0.000000
## alpha1 0.101534 0.026295 3.8613 0.000113
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu 0.071817 0.022630 3.1735 0.001506
## omega 0.952798 0.100986 9.4349 0.000000
## alpha1 0.101534 0.046227 2.1964 0.028061
##
## LogLikelihood : -2676.36
##
## Information Criteria
## ------------------------------------
##
## Akaike 2.8826
## Bayes 2.8915
## Shibata 2.8826
## Hannan-Quinn 2.8859
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.1742 0.6764
## Lag[2*(p+q)+(p+q)-1][2] 0.8561 0.5473
## Lag[4*(p+q)+(p+q)-1][5] 1.9326 0.6344
## d.o.f=0
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.05781 8.100e-01
## Lag[2*(p+q)+(p+q)-1][2] 19.82901 4.935e-06
## Lag[4*(p+q)+(p+q)-1][5] 36.89354 3.539e-10
## d.o.f=1
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[2] 39.46 0.500 2.000 3.353e-10
## ARCH Lag[4] 45.30 1.397 1.611 2.642e-12
## ARCH Lag[6] 48.76 2.222 1.500 5.008e-13
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 2.561
## Individual Statistics:
## mu 0.7592
## omega 1.0713
## alpha1 0.8528
##
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic: 0.846 1.01 1.35
## Individual Statistic: 0.35 0.47 0.75
##
## Sign Bias Test
## ------------------------------------
## t-value prob sig
## Sign Bias 0.73501 0.4624
## Negative Sign Bias 0.03445 0.9725
## Positive Sign Bias 0.37444 0.7081
## Joint Effect 2.05210 0.5617
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 155.3 2.113e-23
## 2 30 191.7 6.794e-26
## 3 40 223.5 9.992e-28
## 4 50 255.5 1.014e-29
##
##
## Elapsed time : 0.2987611
specification_model_2 = ugarchspec(variance.model = list(model="sGARCH", garchOrder=c(1,1)),
mean.model = list(armaOrder=c(0,0)),
distribution.model = "norm")
# Estimation du modèle
model_2 = ugarchfit(spec=specification_model_2,
data=df_rend)
print(infocriteria(model_2))
##
## Akaike 2.795908
## Bayes 2.807802
## Shibata 2.795899
## Hannan-Quinn 2.800291
print(model_2)
##
## *---------------------------------*
## * GARCH Model Fit *
## *---------------------------------*
##
## Conditional Variance Dynamics
## -----------------------------------
## GARCH Model : sGARCH(1,1)
## Mean Model : ARFIMA(0,0,0)
## Distribution : norm
##
## Optimal Parameters
## ------------------------------------
## Estimate Std. Error t value Pr(>|t|)
## mu 0.065353 0.021576 3.0290 0.002454
## omega 0.047563 0.012813 3.7121 0.000206
## alpha1 0.068454 0.014975 4.5713 0.000005
## beta1 0.887569 0.023897 37.1417 0.000000
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu 0.065353 0.022151 2.9503 0.003175
## omega 0.047563 0.034132 1.3935 0.163465
## alpha1 0.068454 0.025102 2.7270 0.006391
## beta1 0.887569 0.045481 19.5153 0.000000
##
## LogLikelihood : -2594.796
##
## Information Criteria
## ------------------------------------
##
## Akaike 2.7959
## Bayes 2.8078
## Shibata 2.7959
## Hannan-Quinn 2.8003
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.1997 0.6550
## Lag[2*(p+q)+(p+q)-1][2] 0.3475 0.7699
## Lag[4*(p+q)+(p+q)-1][5] 0.7977 0.9035
## d.o.f=0
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.1255 0.7231
## Lag[2*(p+q)+(p+q)-1][5] 0.3366 0.9797
## Lag[4*(p+q)+(p+q)-1][9] 0.4989 0.9985
## d.o.f=2
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[3] 0.01578 0.500 2.000 0.9000
## ARCH Lag[5] 0.32054 1.440 1.667 0.9348
## ARCH Lag[7] 0.37485 2.315 1.543 0.9883
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 2.5476
## Individual Statistics:
## mu 0.7035
## omega 0.1271
## alpha1 0.2864
## beta1 0.1054
##
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic: 1.07 1.24 1.6
## Individual Statistic: 0.35 0.47 0.75
##
## Sign Bias Test
## ------------------------------------
## t-value prob sig
## Sign Bias 1.4183 0.1563
## Negative Sign Bias 0.8066 0.4200
## Positive Sign Bias 0.4291 0.6679
## Joint Effect 4.2421 0.2365
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 97.12 1.776e-12
## 2 30 132.02 4.270e-15
## 3 40 130.52 8.456e-12
## 4 50 195.57 2.093e-19
##
##
## Elapsed time : 0.1550949
residu_std = residuals(model_2,
standardize = TRUE)
Méthode graphique
plot(residu_std, type = "l", col = "darkgreen", lwd = 2, main = "Graphe des résidus",
xlab = "Temps", ylab = "e_t")
par(mfrow=c(1,2))
# Calculer et tracer l'ACF
acf(ts(residu_std), main="Corrélogramme simple", col="red",lag.max = 30)
# Calculer et tracer le PACF
pacf(ts(residu_std), main="corrélogramme partiel", col="red",lag.max = 30)
Vérification de la distribution
qqnorm(residu_std)
qqline(residu_std, col="red")
Se rassurer de l’abscence d’effet ARCH
Box.test(residu_std, lag=1, type="Ljung-Box")
##
## Box-Ljung test
##
## data: residu_std
## X-squared = 0.19966, df = 1, p-value = 0.655
ArchTest(residu_std^2, lags = 1)
##
## ARCH LM-test; Null hypothesis: no ARCH effects
##
## data: residu_std^2
## Chi-squared = 0.0012546, df = 1, p-value = 0.9717