library(quantmod)
library(fBasics)
Esempio titolo IBM da Yahoo Finance dal 01-01-2019 al 31-12-2019
getSymbols("IBM",from="2019-01-01", to="2019-12-31")
## [1] "IBM"
head(IBM)
## IBM.Open IBM.High IBM.Low IBM.Close IBM.Volume IBM.Adjusted
## 2019-01-02 112.01 115.98 111.69 115.21 4239900 108.8010
## 2019-01-03 114.53 114.88 112.69 112.91 4346700 106.6289
## 2019-01-04 114.91 117.49 114.44 117.32 4477800 110.7936
## 2019-01-07 117.50 118.83 116.67 118.15 3751200 111.5774
## 2019-01-08 119.66 120.57 118.98 119.83 4763600 113.1640
## 2019-01-09 120.91 121.40 119.87 120.69 3633700 113.9762
Si crea un oggetto IBM contenente le quotazioni giornaliere del titolo: prezzo di apertura (open), il massimo (High) e il minimo (low) giornaliero, il prezzo di chiusura (close) e la quotazione “Adjusted” per split e dividendi
Ibm=IBM$IBM.Adjusted
plot.xts(Ibm, type = "l", col = "blue", lwd = 2, main = "IBM - Adjusted")
chartSeries(IBM)
rs=periodReturn(IBM,period="daily",type="arithmetic")
#moltiplichiamo per 100 per avere rendimenti percentuali
rs=rs*100
plot.xts(rs, type = "l", col = "blue", lwd = 2, main = "IBM - Rendimenti semplici")
head(Ibm)
## IBM.Adjusted
## 2019-01-02 108.8010
## 2019-01-03 106.6289
## 2019-01-04 110.7936
## 2019-01-07 111.5774
## 2019-01-08 113.1640
## 2019-01-09 113.9762
head(rs)
## daily.returns
## 2019-01-02 2.8568850
## 2019-01-03 -1.9963502
## 2019-01-04 3.9057620
## 2019-01-07 0.7074685
## 2019-01-08 1.4219213
## 2019-01-09 0.7176834
rl=periodReturn(IBM,period="daily",type="log")
#moltiplichiamo per 100 per avere rendimenti percentuali
rl=rl*100
plot.xts(rl, type = "l", col = "green", lwd = 2, main = "IBM - Rendimenti logaritmici")
head(Ibm)
## IBM.Adjusted
## 2019-01-02 108.8010
## 2019-01-03 106.6289
## 2019-01-04 110.7936
## 2019-01-07 111.5774
## 2019-01-08 113.1640
## 2019-01-09 113.9762
head(rl)
## daily.returns
## 2019-01-02 2.8168370
## 2019-01-03 -2.0165465
## 2019-01-04 3.8314167
## 2019-01-07 0.7049776
## 2019-01-08 1.4119068
## 2019-01-09 0.7151203
r=rl
hist(r, breaks=35,col="green",xlab = "log-returns",main = "Rendimenti logaritmici IBM anno 2019",cex.main = 1,prob=T)
#densità empirica
hist(r, breaks=35,col="green",xlab = "log-returns",main = "Rendimenti logaritmici IBM anno 2019",cex.main = 1,prob=T)
#densità empirica
lines(density(r),lwd=3,col="blue")
Prima generiamo una Normale con stessa media e deviazione standard dei nostri dati
retNorm=rnorm(5000,mean(r),sd(r))
hist(r, breaks=35,col="green",xlab = "log-returns",main = "Rendimenti logaritmici IBM anno 2019",cex.main = 1,prob=T)
#densità empirica
lines(density(r),lwd=3,col="blue")
#densità Normale
lines(density(retNorm),lwd=3,col="red")
Curva rossa è la funzione di densità Normale, la curva blu è la funzione di densità empirica
Minimo, massimo, Q1, Q2, Q3 e media
summary(r)
## Index daily.returns
## Min. :2019-01-02 Min. :-5.68232
## 1st Qu.:2019-04-02 1st Qu.:-0.56463
## Median :2019-07-02 Median : 0.13383
## Mean :2019-07-01 Mean : 0.06786
## 3rd Qu.:2019-09-30 3rd Qu.: 0.76709
## Max. :2019-12-30 Max. : 8.12475
Media, varianza e deviazione standard (Volatilità)
mean(r)
## [1] 0.0678611
var(r)
## daily.returns
## daily.returns 1.709813
sd(r)
## [1] 1.307598
Percentili della distribuzione
quantile(r,0.05) #quinto percentile
## 5%
## -1.991225
quantile(r,0.95) #95-esimo percentile
## 95%
## 1.51809
s3=skewness(r);s3=as.numeric(s3);s3
## [1] 0.2995488
Il comando kurtosis calcola l’eccesso di Kurtosi ossia K(r)-3
s4=kurtosis(r);s4=as.numeric(s4);s4
## [1] 7.74467
qqnorm(r)
qqline(r)
# Testing mean return = 0
#Attenzione: inserire as.vector perchè r è un xts object
ttest=t.test(as.vector(r))
#errore standard
ttest$stderr
## [1] 0.08253488
#statistica test
ttest$statistic
## t
## 0.8222112
#pvalue
ttest$p.value
## [1] 0.4117402
normalTest(as.vector(r),method="jb")
##
## Title:
## Jarque - Bera Normalality Test
##
## Test Results:
## STATISTIC:
## X-squared: 645.1188
## P VALUE:
## Asymptotic p Value: < 2.2e-16
##
## Description:
## Thu Apr 30 10:31:31 2020 by user: sagat