El riesgo se puede definri a la probabilidad de que ocurra un evento desfavorable o perdida, con la consiguente incertidumbre sobre sus consecuencias.Según Markowitz el riesgo de un activo individual no se mide simplemente por la volatilidad o la variabilidad de sus rendimientos, sino en el contexto de su contribución al riesgo total de una cartera. \[ \sigma^2=\frac{1}{t-1} \sum_{i=1}^{T}(r_i -\bar{r})^2 \]
Los retornos acumulados se calculan apartir de
\[ 1+r_{total}=\prod_{i=1}^T (1+r_i) \] ## Cargar la base de datos
Primeros con library(quantmod), escoger dos activos de su preferencia para este caso,uno que tenga alto riesgo (SPY) y bonos (BIL) que tenga poco riesgo.
data_spy=getSymbols("SPY",from="2014-12-31",
to="2020-01-01",
auto.assign = FALSE)
head(data_spy)
## SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted
## 2014-12-31 207.99 208.19 205.39 205.54 130333800 174.9004
## 2015-01-02 206.38 206.88 204.18 205.43 121465900 174.8067
## 2015-01-05 204.17 204.37 201.35 201.72 169632600 171.6498
## 2015-01-06 202.09 202.72 198.86 199.82 209151400 170.0330
## 2015-01-07 201.42 202.72 200.88 202.31 125346700 172.1519
## 2015-01-08 204.01 206.16 203.99 205.90 147217800 175.2067
tail(data_spy)
## SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted
## 2019-12-23 321.59 321.65 321.06 321.22 52990000 301.6310
## 2019-12-24 321.47 321.52 320.90 321.23 20270000 301.6404
## 2019-12-26 321.65 322.95 321.64 322.94 30911200 303.2461
## 2019-12-27 323.74 323.80 322.28 322.86 42528800 303.1710
## 2019-12-30 322.95 323.10 320.55 321.08 49729100 301.4995
## 2019-12-31 320.53 322.13 320.15 321.86 57077300 302.2320
data_tbill=getSymbols("BIL",from="2014-12-31",
to="2020-01-01",
auto.assign = FALSE)
head(data_tbill)
## BIL.Open BIL.High BIL.Low BIL.Close BIL.Volume BIL.Adjusted
## 2014-12-31 91.48 91.48 91.46 91.48 348100 81.25819
## 2015-01-02 91.46 91.48 91.46 91.46 341000 81.24039
## 2015-01-05 91.46 91.48 91.46 91.48 2518350 81.25819
## 2015-01-06 91.46 91.48 91.46 91.46 541250 81.24039
## 2015-01-07 91.48 91.48 91.46 91.46 153800 81.24039
## 2015-01-08 91.48 91.48 91.46 91.46 236450 81.24039
tail(data_tbill)
## BIL.Open BIL.High BIL.Low BIL.Close BIL.Volume BIL.Adjusted
## 2019-12-23 91.40 91.41 91.40 91.41 2846500 84.89384
## 2019-12-24 91.42 91.42 91.41 91.42 1042700 84.90313
## 2019-12-26 91.41 91.43 91.41 91.43 956200 84.91243
## 2019-12-27 91.42 91.43 91.42 91.42 1325600 84.90313
## 2019-12-30 91.44 91.44 91.43 91.43 1381300 84.91243
## 2019-12-31 91.45 91.45 91.43 91.43 1406200 84.91243
seguidamente , establecesmos que retorno de cada es delta de los precios ajustados
## retornos
ret_spy=Delt(data_spy$SPY.Adjusted)
ret_bill=Delt(data_tbill$BIL.Adjusted)
rets=cbind(ret_spy,ret_bill)
head(rets)
## Delt.1.arithmetic Delt.1.arithmetic.1
## 2014-12-31 NA NA
## 2015-01-02 -0.0005352343 -0.0002189533
## 2015-01-05 -0.0180594939 0.0002190013
## 2015-01-06 -0.0094191227 -0.0002189533
## 2015-01-07 0.0124612266 0.0000000000
## 2015-01-08 0.0177452087 0.0000000000
names(rets)=c("SPY","Tbill")
Para el la organizacion de los retornos acumulados y la sumatoria del producto de estas
###Retornos acumulados
rets[1,]=c(0,0)
gross_ret=1+rets
head(rets)
## SPY Tbill
## 2014-12-31 0.0000000000 0.0000000000
## 2015-01-02 -0.0005352343 -0.0002189533
## 2015-01-05 -0.0180594939 0.0002190013
## 2015-01-06 -0.0094191227 -0.0002189533
## 2015-01-07 0.0124612266 0.0000000000
## 2015-01-08 0.0177452087 0.0000000000
head(gross_ret)
## SPY Tbill
## 2014-12-31 1.0000000 1.000000
## 2015-01-02 0.9994648 0.999781
## 2015-01-05 0.9819405 1.000219
## 2015-01-06 0.9905809 0.999781
## 2015-01-07 1.0124612 1.000000
## 2015-01-08 1.0177452 1.000000
cum_rets=cumprod(gross_ret)
head(cum_rets)
## SPY Tbill
## 2014-12-31 1.0000000 1.000000
## 2015-01-02 0.9994648 0.999781
## 2015-01-05 0.9814149 1.000000
## 2015-01-06 0.9721709 0.999781
## 2015-01-07 0.9842853 0.999781
## 2015-01-08 1.0017517 0.999781
tail(cum_rets)
## SPY Tbill
## 2019-12-23 1.724588 1.044742
## 2019-12-24 1.724641 1.044856
## 2019-12-26 1.733822 1.044971
## 2019-12-27 1.733392 1.044856
## 2019-12-30 1.723836 1.044971
## 2019-12-31 1.728024 1.044971
Con sus respectivas graficas
plot(rets$SPY,col="red",main="Retornos de S&P y Tbils")
lines(rets$Tbill,col="blue",lwd=4)
plot(cum_rets$SPY,col = "red")
lines(cum_rets$Tbill,col="blue",lwd = 4)
Para calcular los riesgos por medio de la desviacion estandar
returns=rets[-1,]
head(returns)
## SPY Tbill
## 2015-01-02 -0.0005352343 -0.0002189533
## 2015-01-05 -0.0180594939 0.0002190013
## 2015-01-06 -0.0094191227 -0.0002189533
## 2015-01-07 0.0124612266 0.0000000000
## 2015-01-08 0.0177452087 0.0000000000
## 2015-01-09 -0.0080138662 0.0000000000
sd_spy=sd(returns$SPY)
sd_spy
## [1] 0.008455192
sd_tbill=sd(returns$Tbill)
sd_tbill
## [1] 0.0001625056
para comparar los dos activos , miramos que activo tiene mayor varianza esta es muestral, adicionalmente pordemos ver la partipacion que tiene
\[ \frac{Activo Riesgoso}{Activo no riesgoso} \]
sd_spy/sd_tbill
## [1] 52.03014
Su lectura es, una compracion SPY es altamente riesgoso, 52 veces mas riesgoso,adicionalmente podemos por medio de un histograma la distribucion se comporta de manera leptocurtica, y adcionamos una distribucion normla para su comparacion
hist(returns$SPY,breaks = 50,)
sd_spy*100
## [1] 0.8455192
sd_spy*3
## [1] 0.02536557
curve(dnorm(x, mean = mean(returns$SPY), sd = sd(returns$SPY)),
col = "blue", lwd = 2, add = TRUE)
ver las colas de de una distribucion normal
pnorm(3)-pnorm(-3)
## [1] 0.9973002
pnorm(2)-pnorm(-2)
## [1] 0.9544997
pnorm(1)-pnorm(-1)
## [1] 0.6826895
Ahora podemos analizar los riesgos cada año 2015,2016,2017,2018 y 2019, con sus respectivas desviaciones
sd_2015=apply(X=returns["2015"],MARGIN = 2,FUN = sd)
sd_2015
## SPY Tbill
## 0.0097211745 0.0001657073
## desviacione standar 2016
sd_2016=apply(X=returns["2016"],MARGIN = 2,FUN = sd)
sd_2016
## SPY Tbill
## 0.0082278506 0.0001850196
## desviacione standar 2017
sd_2017=apply(X=returns["2017"],MARGIN = 2,FUN = sd)
sd_2017
## SPY Tbill
## 0.0042482042 0.0001926173
## desviacione standar 2018
sd_2018=apply(X=returns["2018"],MARGIN = 2,FUN = sd)
sd_2018
## SPY Tbill
## 0.0107321858 0.0001169504
## desviacione standar 2019
sd_2019=apply(X=returns["2019"],MARGIN = 2,FUN = sd)
sd_2019
## SPY Tbill
## 0.0078811150 0.0001192099
Para unir todo por fila r-bind para unir por columa cbind
sd_all=rbind(sd_2015,sd_2016,sd_2017,sd_2018,sd_2019)
sd_all
## SPY Tbill
## sd_2015 0.009721174 0.0001657073
## sd_2016 0.008227851 0.0001850196
## sd_2017 0.004248204 0.0001926173
## sd_2018 0.010732186 0.0001169504
## sd_2019 0.007881115 0.0001192099
para una mejor comparacion podemos hacerlo por medio de
sd_all_anual=sqrt(252)*sd_all
sd_all_anual
## SPY Tbill
## sd_2015 0.15431886 0.002630522
## sd_2016 0.13061308 0.002937095
## sd_2017 0.06743815 0.003057704
## sd_2018 0.17036817 0.001856530
## sd_2019 0.12510882 0.001892399
barplot(t(sd_all_anual),beside = T,
col=c("blue", "red"),legend.text = c("SPY","TBills"),
main = "Desv.Est.Anualizada")