knitr::opts_chunk$set(fig.width=16, fig.height=12, fig.path='Figs/',
                      echo=FALSE, warning=FALSE, message=FALSE)
library(xts)
## Warning: package 'xts' was built under R version 4.0.5
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.0.5
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(quantmod)
## Warning: package 'quantmod' was built under R version 4.0.5
## Loading required package: TTR
## Warning: package 'TTR' was built under R version 4.0.5
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
mes_titres <- new.env(hash=TRUE)# un cree un receptacle dans lequel on va placer les donnees telecharg攼㸹es
tickers = c("^FCHI","^GSPC","^IXIC")
getSymbols(tickers,from='2016-11-21',na.omit="F", env=mes_titres ) 
## '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.
## Warning: ^FCHI contains missing values. Some functions will not work if objects
## contain missing values in the middle of the series. Consider using na.omit(),
## na.approx(), na.fill(), etc to remove or replace them.
## [1] "^FCHI" "^GSPC" "^IXIC"
nrow(mes_titres$FCHI)
## [1] 1280
nrow(mes_titres$GSPC)
## [1] 1259
nrow(mes_titres$IXIC)
## [1] 1259
# Comme il manque des donn攼㸹es et quelles sont pas de m攼㹡me taille. Il faut remplir les vides et mettre toutes les donnees a la meme longueure.
# On commence par les fusionner

data1<-merge(mes_titres$GSPC,mes_titres$IXIC,mes_titres$FCHI)

#Puis on remplie les vide par approximation lin攼㸹raire
Cac<-na.approx(data1$FCHI.Adjusted)
SP<-na.approx(data1$GSPC.Adjusted)
Nas<-na.approx(data1$IXIC.Adjusted)
# Onv攼㸹rifie qu'elles ont toutes le m攼㹡me longueure.
nrow(Cac)
## [1] 1292
nrow(SP)
## [1] 1292
nrow(Nas)
## [1] 1292
# Calcul de base 100 par vectorisation

Nas_100<-(Nas)/as.vector(Nas[1])*100# base 100 par vectorisation, le probl攼㸸me de l'heritage. Lorsqu'on divise un vecteur par un scalaire 攼㸷a donne un scalaire. Pour 攼㸹viter, il faut trasnformer le scalaire en vecteur. 
Cac_100<-(Cac)/as.vector(Cac[1])*100#
SP_100<-(SP)/as.vector(SP[1])*100#


# Graphique de la base 100
# Open a pdf file
#png("base_100.png") 


plot(Cac_100,ylim=(c(90,320)),main='Base 100')

lines(Nas_100,col='blue')

lines(SP_100,col='red')

addLegend("topleft", on=1, 
          legend.names = c("CAC", "Nasdaq","SP500"), 
          lty=c(1, 1), lwd=c(2, 1),
          col=c("black", "blue", "red"))

#dev.off() 
# Calculs automatiques des rendements
rend_nas<-dailyReturn(Nas)       
rend_cac<-dailyReturn(Cac)
rend_sp<-dailyReturn(SP)

# Graphique des rendements
#png("rendements.png") 
plot(rend_nas)

lines(rend_cac,col='red')

lines(rend_sp,col='blue')

addLegend("topleft", on=1, 
          legend.names = c("CAC", "Nasdaq","SP500"), 
          lty=c(1, 1), lwd=c(2, 1),
          col=c("black", "blue", "red"))

#dev.off() 
# Regressions lin攼㸹raire
lm1<-lm(rend_cac~rend_nas)
summary(lm1)
## 
## Call:
## lm(formula = rend_cac ~ rend_nas)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.080460 -0.004152  0.000133  0.004528  0.082525 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -4.981e-06  2.727e-04  -0.018    0.985    
## rend_nas     4.484e-01  2.042e-02  21.958   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.009777 on 1290 degrees of freedom
## Multiple R-squared:  0.2721, Adjusted R-squared:  0.2715 
## F-statistic: 482.2 on 1 and 1290 DF,  p-value: < 2.2e-16
lm2<-lm(rend_cac~rend_sp)
summary(lm2)
## 
## Call:
## lm(formula = rend_cac ~ rend_sp)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.066236 -0.003947  0.000202  0.004273  0.075895 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 2.388e-05  2.519e-04   0.095    0.924    
## rend_sp     5.946e-01  2.126e-02  27.967   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.009042 on 1290 degrees of freedom
## Multiple R-squared:  0.3775, Adjusted R-squared:  0.377 
## F-statistic: 782.2 on 1 and 1290 DF,  p-value: < 2.2e-16
lm3<-lm(rend_nas~rend_sp)
summary(lm3)
## 
## Call:
## lm(formula = rend_nas ~ rend_sp)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.027925 -0.002151  0.000205  0.002415  0.028152 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.0002379  0.0001227   1.938   0.0528 .  
## rend_sp     1.0626798  0.0103575 102.600   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.004405 on 1290 degrees of freedom
## Multiple R-squared:  0.8908, Adjusted R-squared:  0.8907 
## F-statistic: 1.053e+04 on 1 and 1290 DF,  p-value: < 2.2e-16
require(texreg)
## Loading required package: texreg
## Warning: package 'texreg' was built under R version 4.0.5
## Version:  1.37.5
## Date:     2020-06-17
## Author:   Philip Leifeld (University of Essex)
## 
## Consider submitting praise using the praise or praise_interactive functions.
## Please cite the JSS article in your publications -- see citation("texreg").
texreg(lm3)
## 
## \begin{table}
## \begin{center}
## \begin{tabular}{l c}
## \hline
##  & Model 1 \\
## \hline
## (Intercept) & $0.00$       \\
##             & $(0.00)$     \\
## rend\_sp    & $1.06^{***}$ \\
##             & $(0.01)$     \\
## \hline
## R$^2$       & $0.89$       \\
## Adj. R$^2$  & $0.89$       \\
## Num. obs.   & $1292$       \\
## \hline
## \multicolumn{2}{l}{\scriptsize{$^{***}p<0.001$; $^{**}p<0.01$; $^{*}p<0.05$}}
## \end{tabular}
## \caption{Statistical models}
## \label{table:coefficients}
## \end{center}
## \end{table}
# Changement de statut de variable pour le graphique en nuage de points qui supporte pas les xts
data3<-as.data.frame(cbind(rend_sp,rend_cac,rend_nas))
#png("nuage_de_points.png") 
plot(data3[,1],data3[,3], main="Sensibilite du Nadaq au SP500",
     xlab="rdt SP500 ", ylab="rdt Nasdaq", pch=8,cex=0.3,)
abline(lm3,col='blue')
text(-0.08,0.06, paste("R_Nasdaq =", round(lm3$coefficients[1],4),"+ R_sp500 *",round(lm3$coefficients[2],4)),col="blue",cex=0.6)
text(-0.08,0.04,paste("R戼㸲 ajust攼㸹",round(summary(lm3)$adj.r.squared,3)),col="blue",cex=0.6)
grid(NULL,NULL)

#dev.off() 
# Graphique des histogrammes
#png("histogrammes.png") 
par(mfrow = c(1, 3))
hist(rend_cac,50,col='cyan')
hist(rend_nas,50,co='red')
hist(rend_sp,50,col='green')

#dev.off() 
# Calculs des kurtosis et Skweness
require(moments) # Package 攼㸰 mobiliser pour ces calculs
## Loading required package: moments
kurtosis(rend_cac)
## daily.returns 
##      21.31351
kurtosis(rend_nas)
## daily.returns 
##      16.28606
kurtosis(rend_sp)
## daily.returns 
##      24.94082
skewness(rend_cac)
## daily.returns 
##     -1.064931
skewness(rend_nas)
## daily.returns 
##     -0.729938
skewness(rend_sp)
## daily.returns 
##    -0.7623025
# Calculs rendements risque de Markowitz
mean(rend_cac)/sd(rend_cac)
## [1] 0.03626729
mean(rend_nas)/sd(rend_nas)
## [1] 0.07036289
mean(rend_sp)/sd(rend_sp)
## [1] 0.05563705