library(quantmod)
## Warning: package 'quantmod' was built under R version 4.3.2
## Loading required package: xts
## Warning: package 'xts' was built under R version 4.3.2
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.3.2
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(lorem)
## Warning: package 'lorem' was built under R version 4.3.3
Se instala la libreria quantmod y lorem
ipsum(paragraphs = 4,avg_words_per_sentence = 10)
Ipsum a habitasse, cras et: lacinia lacus ridiculus condimentum dignissim eu cum eleifend. Litora fringilla luctus pretium natoque – orci ligula, inceptos, tincidunt commodo feugiat. Libero fermentum sociosqu torquent habitasse suscipit habitasse. Venenatis bibendum semper phasellus conubia laoreet gravida lacinia. Ultrices dis varius vestibulum sagittis nostra eleifend, erat lectus.
Adipiscing sodales nam porta dictum mattis. Sapien ante, hac penatibus lectus aliquam magnis facilisi, vulputate per congue? Urna id quisque tristique, sodales; sagittis at natoque diam! Commodo arcu aliquam venenatis gravida conubia mi porttitor vel. Condimentum at tempor, mauris a vel tristique, eros natoque quis. Platea class curae eleifend magna tristique phasellus auctor varius porta eros? Sed natoque, fringilla imperdiet gravida ac semper ullamcorper curae ornare netus dis pellentesque, tempus ullamcorper bibendum.
Elit litora montes arcu interdum ridiculus ornare magnis; tempor nascetur erat? Et aptent hac eget potenti, morbi accumsan laoreet vestibulum faucibus etiam eget. Dictumst tempus condimentum tempor – penatibus, mi ridiculus augue curae nec curabitur blandit. Imperdiet curabitur interdum potenti augue egestas: mattis – nullam ad tristique! Natoque gravida dui pulvinar semper suscipit; nisl elementum himenaeos, quam molestie tincidunt. Quis!
Amet quam vitae, faucibus porttitor inceptos, mollis facilisis turpis at vel. Enim posuere leo vulputate porttitor neque id mi. Proin venenatis at tincidunt at, massa ullamcorper ac accumsan molestie. Vestibulum vestibulum venenatis cursus habitasse; ornare neque nisl urna! con esto, según Markowitz (1950), el riesgo es:
\[ \sigma^2 = \frac{1}{T-1} \sum_{i = 1}^{T} (r_i - \bar{r})^2 \]
data_spy =getSymbols("SPY", from= "2014-12-31",
to="2020-01-01",
auto.assign = F)
head(data_spy,2)
## 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.8068
tail(data_spy,2)
## SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted
## 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 = F)
head(data_TBILL,2)
## 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.25816
## 2015-01-02 91.46 91.48 91.46 91.46 341000 81.24043
tail(data_TBILL,2)
## BIL.Open BIL.High BIL.Low BIL.Close BIL.Volume BIL.Adjusted
## 2019-12-30 91.44 91.44 91.43 91.43 1381300 84.91242
## 2019-12-31 91.45 91.45 91.43 91.43 1406200 84.91242
Se toma la acción SPDR S&P 500 ETF Trust, el cual refleja el desempeño de las 500 principales empresas que cotizan en la bolsa de EE.UU y la acción SPDR Bloomberg 1-3 Month T-Bill ETF la cual refleja el desempeño de los bonos del tesoro de los estados unidos
ret_spy=Delt(data_spy$SPY.Adjusted)
ret_TBILL=Delt(data_TBILL$BIL.Adjusted)
rets=cbind(ret_spy, ret_TBILL)
names(rets)=c("SPY","TBILL")
head(rets,6)
## SPY TBILL
## 2014-12-31 NA NA
## 2015-01-02 -0.0005352342 -0.0002182023
## 2015-01-05 -0.0180595796 0.0002182499
## 2015-01-06 -0.0094187671 -0.0002182023
## 2015-01-07 0.0124608632 0.0000000000
## 2015-01-08 0.0177452087 0.0000000000
Se Calcula los retornos porcentuales sucesivos de una serie temporal de precios ajustados para dos instrumentos financieros: SPY y BIL
rets[1, ]=c(0,0)
gross_ret=1+rets
head(rets,3)
## SPY TBILL
## 2014-12-31 0.0000000000 0.0000000000
## 2015-01-02 -0.0005352342 -0.0002182023
## 2015-01-05 -0.0180595796 0.0002182499
head(gross_ret, 3)
## SPY TBILL
## 2014-12-31 1.0000000 1.0000000
## 2015-01-02 0.9994648 0.9997818
## 2015-01-05 0.9819404 1.0002182
cum_rets=cumprod(gross_ret)
head(cum_rets)
## SPY TBILL
## 2014-12-31 1.0000000 1.0000000
## 2015-01-02 0.9994648 0.9997818
## 2015-01-05 0.9814149 1.0000000
## 2015-01-06 0.9721711 0.9997818
## 2015-01-07 0.9842852 0.9997818
## 2015-01-08 1.0017516 0.9997818
tail(cum_rets)
## SPY TBILL
## 2019-12-23 1.724588 1.044742
## 2019-12-24 1.724641 1.044857
## 2019-12-26 1.733822 1.044971
## 2019-12-27 1.733392 1.044857
## 2019-12-30 1.723836 1.044971
## 2019-12-31 1.728024 1.044971
Se calculan los retornos acumulados y brutos para un lapso de cinco años, esto mediante factores o matriz de crecimiento, sobre los activos financieras
plot(rets$SPY, col="red",
main="retornos de S&P 500 y T-BILLS")
lines(rets$TBILL, col="darkgreen", lwd=4)
plot(cum_rets$SPY, col="blue")
lines(cum_rets$TBILL, col="red", lwd=3)
"center"
## [1] "center"
En las primeras dos graficas las cuales llevan como titulo “retornos de S&P 500 y T-BILLS” se muestra los retornos del S&P 500 en rojo y los retornos diarios de T-BILLS en verde oscuro
Luego, en la tercera y cuarta grafica se observa los retornos acumulativos del S&P 500 en azul y los retornos acumulativos de T-BILLS en rojo
head(rets)
## SPY TBILL
## 2014-12-31 0.0000000000 0.0000000000
## 2015-01-02 -0.0005352342 -0.0002182023
## 2015-01-05 -0.0180595796 0.0002182499
## 2015-01-06 -0.0094187671 -0.0002182023
## 2015-01-07 0.0124608632 0.0000000000
## 2015-01-08 0.0177452087 0.0000000000
returns=rets[-1, ]
head(returns)
## SPY TBILL
## 2015-01-02 -0.0005352342 -0.0002182023
## 2015-01-05 -0.0180595796 0.0002182499
## 2015-01-06 -0.0094187671 -0.0002182023
## 2015-01-07 0.0124608632 0.0000000000
## 2015-01-08 0.0177452087 0.0000000000
## 2015-01-09 -0.0080136049 0.0000000000
sd_spy=sd(returns$SPY)
sd_spy
## [1] 0.008455189
sd_bill=sd(returns$TBILL)
sd_bill
## [1] 0.0001624218
sd_spy/sd_bill
## [1] 52.05697
¨Se encuentra que el activo más riesgoso es SPY. Es 52 veces más riesgoso que BIL
hist(returns$SPY, breaks=50)
hist(returns$TBILL, breaks=50)
sd_spy*100
## [1] 0.8455189
sd_spy*3
## [1] 0.02536557
pnorm(1)-pnorm(-1)
## [1] 0.6826895
pnorm(2)-pnorm(-2)
## [1] 0.9544997
El activo BIL no presenta mucha variación en su histograma, mientras que SPY tiene tendencia LEPTOCURTICA. Además se calcula la probabilidad en el rango de datos de la muestra
head(returns)
## SPY TBILL
## 2015-01-02 -0.0005352342 -0.0002182023
## 2015-01-05 -0.0180595796 0.0002182499
## 2015-01-06 -0.0094187671 -0.0002182023
## 2015-01-07 0.0124608632 0.0000000000
## 2015-01-08 0.0177452087 0.0000000000
## 2015-01-09 -0.0080136049 0.0000000000
index(returns)
## [1] "2015-01-02" "2015-01-05" "2015-01-06" "2015-01-07" "2015-01-08"
## [6] "2015-01-09" "2015-01-12" "2015-01-13" "2015-01-14" "2015-01-15"
## [11] "2015-01-16" "2015-01-20" "2015-01-21" "2015-01-22" "2015-01-23"
## [16] "2015-01-26" "2015-01-27" "2015-01-28" "2015-01-29" "2015-01-30"
## [21] "2015-02-02" "2015-02-03" "2015-02-04" "2015-02-05" "2015-02-06"
## [26] "2015-02-09" "2015-02-10" "2015-02-11" "2015-02-12" "2015-02-13"
## [31] "2015-02-17" "2015-02-18" "2015-02-19" "2015-02-20" "2015-02-23"
## [36] "2015-02-24" "2015-02-25" "2015-02-26" "2015-02-27" "2015-03-02"
## [41] "2015-03-03" "2015-03-04" "2015-03-05" "2015-03-06" "2015-03-09"
## [46] "2015-03-10" "2015-03-11" "2015-03-12" "2015-03-13" "2015-03-16"
## [51] "2015-03-17" "2015-03-18" "2015-03-19" "2015-03-20" "2015-03-23"
## [56] "2015-03-24" "2015-03-25" "2015-03-26" "2015-03-27" "2015-03-30"
## [61] "2015-03-31" "2015-04-01" "2015-04-02" "2015-04-06" "2015-04-07"
## [66] "2015-04-08" "2015-04-09" "2015-04-10" "2015-04-13" "2015-04-14"
## [71] "2015-04-15" "2015-04-16" "2015-04-17" "2015-04-20" "2015-04-21"
## [76] "2015-04-22" "2015-04-23" "2015-04-24" "2015-04-27" "2015-04-28"
## [81] "2015-04-29" "2015-04-30" "2015-05-01" "2015-05-04" "2015-05-05"
## [86] "2015-05-06" "2015-05-07" "2015-05-08" "2015-05-11" "2015-05-12"
## [91] "2015-05-13" "2015-05-14" "2015-05-15" "2015-05-18" "2015-05-19"
## [96] "2015-05-20" "2015-05-21" "2015-05-22" "2015-05-26" "2015-05-27"
## [101] "2015-05-28" "2015-05-29" "2015-06-01" "2015-06-02" "2015-06-03"
## [106] "2015-06-04" "2015-06-05" "2015-06-08" "2015-06-09" "2015-06-10"
## [111] "2015-06-11" "2015-06-12" "2015-06-15" "2015-06-16" "2015-06-17"
## [116] "2015-06-18" "2015-06-19" "2015-06-22" "2015-06-23" "2015-06-24"
## [121] "2015-06-25" "2015-06-26" "2015-06-29" "2015-06-30" "2015-07-01"
## [126] "2015-07-02" "2015-07-06" "2015-07-07" "2015-07-08" "2015-07-09"
## [131] "2015-07-10" "2015-07-13" "2015-07-14" "2015-07-15" "2015-07-16"
## [136] "2015-07-17" "2015-07-20" "2015-07-21" "2015-07-22" "2015-07-23"
## [141] "2015-07-24" "2015-07-27" "2015-07-28" "2015-07-29" "2015-07-30"
## [146] "2015-07-31" "2015-08-03" "2015-08-04" "2015-08-05" "2015-08-06"
## [151] "2015-08-07" "2015-08-10" "2015-08-11" "2015-08-12" "2015-08-13"
## [156] "2015-08-14" "2015-08-17" "2015-08-18" "2015-08-19" "2015-08-20"
## [161] "2015-08-21" "2015-08-24" "2015-08-25" "2015-08-26" "2015-08-27"
## [166] "2015-08-28" "2015-08-31" "2015-09-01" "2015-09-02" "2015-09-03"
## [171] "2015-09-04" "2015-09-08" "2015-09-09" "2015-09-10" "2015-09-11"
## [176] "2015-09-14" "2015-09-15" "2015-09-16" "2015-09-17" "2015-09-18"
## [181] "2015-09-21" "2015-09-22" "2015-09-23" "2015-09-24" "2015-09-25"
## [186] "2015-09-28" "2015-09-29" "2015-09-30" "2015-10-01" "2015-10-02"
## [191] "2015-10-05" "2015-10-06" "2015-10-07" "2015-10-08" "2015-10-09"
## [196] "2015-10-12" "2015-10-13" "2015-10-14" "2015-10-15" "2015-10-16"
## [201] "2015-10-19" "2015-10-20" "2015-10-21" "2015-10-22" "2015-10-23"
## [206] "2015-10-26" "2015-10-27" "2015-10-28" "2015-10-29" "2015-10-30"
## [211] "2015-11-02" "2015-11-03" "2015-11-04" "2015-11-05" "2015-11-06"
## [216] "2015-11-09" "2015-11-10" "2015-11-11" "2015-11-12" "2015-11-13"
## [221] "2015-11-16" "2015-11-17" "2015-11-18" "2015-11-19" "2015-11-20"
## [226] "2015-11-23" "2015-11-24" "2015-11-25" "2015-11-27" "2015-11-30"
## [231] "2015-12-01" "2015-12-02" "2015-12-03" "2015-12-04" "2015-12-07"
## [236] "2015-12-08" "2015-12-09" "2015-12-10" "2015-12-11" "2015-12-14"
## [241] "2015-12-15" "2015-12-16" "2015-12-17" "2015-12-18" "2015-12-21"
## [246] "2015-12-22" "2015-12-23" "2015-12-24" "2015-12-28" "2015-12-29"
## [251] "2015-12-30" "2015-12-31" "2016-01-04" "2016-01-05" "2016-01-06"
## [256] "2016-01-07" "2016-01-08" "2016-01-11" "2016-01-12" "2016-01-13"
## [261] "2016-01-14" "2016-01-15" "2016-01-19" "2016-01-20" "2016-01-21"
## [266] "2016-01-22" "2016-01-25" "2016-01-26" "2016-01-27" "2016-01-28"
## [271] "2016-01-29" "2016-02-01" "2016-02-02" "2016-02-03" "2016-02-04"
## [276] "2016-02-05" "2016-02-08" "2016-02-09" "2016-02-10" "2016-02-11"
## [281] "2016-02-12" "2016-02-16" "2016-02-17" "2016-02-18" "2016-02-19"
## [286] "2016-02-22" "2016-02-23" "2016-02-24" "2016-02-25" "2016-02-26"
## [291] "2016-02-29" "2016-03-01" "2016-03-02" "2016-03-03" "2016-03-04"
## [296] "2016-03-07" "2016-03-08" "2016-03-09" "2016-03-10" "2016-03-11"
## [301] "2016-03-14" "2016-03-15" "2016-03-16" "2016-03-17" "2016-03-18"
## [306] "2016-03-21" "2016-03-22" "2016-03-23" "2016-03-24" "2016-03-28"
## [311] "2016-03-29" "2016-03-30" "2016-03-31" "2016-04-01" "2016-04-04"
## [316] "2016-04-05" "2016-04-06" "2016-04-07" "2016-04-08" "2016-04-11"
## [321] "2016-04-12" "2016-04-13" "2016-04-14" "2016-04-15" "2016-04-18"
## [326] "2016-04-19" "2016-04-20" "2016-04-21" "2016-04-22" "2016-04-25"
## [331] "2016-04-26" "2016-04-27" "2016-04-28" "2016-04-29" "2016-05-02"
## [336] "2016-05-03" "2016-05-04" "2016-05-05" "2016-05-06" "2016-05-09"
## [341] "2016-05-10" "2016-05-11" "2016-05-12" "2016-05-13" "2016-05-16"
## [346] "2016-05-17" "2016-05-18" "2016-05-19" "2016-05-20" "2016-05-23"
## [351] "2016-05-24" "2016-05-25" "2016-05-26" "2016-05-27" "2016-05-31"
## [356] "2016-06-01" "2016-06-02" "2016-06-03" "2016-06-06" "2016-06-07"
## [361] "2016-06-08" "2016-06-09" "2016-06-10" "2016-06-13" "2016-06-14"
## [366] "2016-06-15" "2016-06-16" "2016-06-17" "2016-06-20" "2016-06-21"
## [371] "2016-06-22" "2016-06-23" "2016-06-24" "2016-06-27" "2016-06-28"
## [376] "2016-06-29" "2016-06-30" "2016-07-01" "2016-07-05" "2016-07-06"
## [381] "2016-07-07" "2016-07-08" "2016-07-11" "2016-07-12" "2016-07-13"
## [386] "2016-07-14" "2016-07-15" "2016-07-18" "2016-07-19" "2016-07-20"
## [391] "2016-07-21" "2016-07-22" "2016-07-25" "2016-07-26" "2016-07-27"
## [396] "2016-07-28" "2016-07-29" "2016-08-01" "2016-08-02" "2016-08-03"
## [401] "2016-08-04" "2016-08-05" "2016-08-08" "2016-08-09" "2016-08-10"
## [406] "2016-08-11" "2016-08-12" "2016-08-15" "2016-08-16" "2016-08-17"
## [411] "2016-08-18" "2016-08-19" "2016-08-22" "2016-08-23" "2016-08-24"
## [416] "2016-08-25" "2016-08-26" "2016-08-29" "2016-08-30" "2016-08-31"
## [421] "2016-09-01" "2016-09-02" "2016-09-06" "2016-09-07" "2016-09-08"
## [426] "2016-09-09" "2016-09-12" "2016-09-13" "2016-09-14" "2016-09-15"
## [431] "2016-09-16" "2016-09-19" "2016-09-20" "2016-09-21" "2016-09-22"
## [436] "2016-09-23" "2016-09-26" "2016-09-27" "2016-09-28" "2016-09-29"
## [441] "2016-09-30" "2016-10-03" "2016-10-04" "2016-10-05" "2016-10-06"
## [446] "2016-10-07" "2016-10-10" "2016-10-11" "2016-10-12" "2016-10-13"
## [451] "2016-10-14" "2016-10-17" "2016-10-18" "2016-10-19" "2016-10-20"
## [456] "2016-10-21" "2016-10-24" "2016-10-25" "2016-10-26" "2016-10-27"
## [461] "2016-10-28" "2016-10-31" "2016-11-01" "2016-11-02" "2016-11-03"
## [466] "2016-11-04" "2016-11-07" "2016-11-08" "2016-11-09" "2016-11-10"
## [471] "2016-11-11" "2016-11-14" "2016-11-15" "2016-11-16" "2016-11-17"
## [476] "2016-11-18" "2016-11-21" "2016-11-22" "2016-11-23" "2016-11-25"
## [481] "2016-11-28" "2016-11-29" "2016-11-30" "2016-12-01" "2016-12-02"
## [486] "2016-12-05" "2016-12-06" "2016-12-07" "2016-12-08" "2016-12-09"
## [491] "2016-12-12" "2016-12-13" "2016-12-14" "2016-12-15" "2016-12-16"
## [496] "2016-12-19" "2016-12-20" "2016-12-21" "2016-12-22" "2016-12-23"
## [501] "2016-12-27" "2016-12-28" "2016-12-29" "2016-12-30" "2017-01-03"
## [506] "2017-01-04" "2017-01-05" "2017-01-06" "2017-01-09" "2017-01-10"
## [511] "2017-01-11" "2017-01-12" "2017-01-13" "2017-01-17" "2017-01-18"
## [516] "2017-01-19" "2017-01-20" "2017-01-23" "2017-01-24" "2017-01-25"
## [521] "2017-01-26" "2017-01-27" "2017-01-30" "2017-01-31" "2017-02-01"
## [526] "2017-02-02" "2017-02-03" "2017-02-06" "2017-02-07" "2017-02-08"
## [531] "2017-02-09" "2017-02-10" "2017-02-13" "2017-02-14" "2017-02-15"
## [536] "2017-02-16" "2017-02-17" "2017-02-21" "2017-02-22" "2017-02-23"
## [541] "2017-02-24" "2017-02-27" "2017-02-28" "2017-03-01" "2017-03-02"
## [546] "2017-03-03" "2017-03-06" "2017-03-07" "2017-03-08" "2017-03-09"
## [551] "2017-03-10" "2017-03-13" "2017-03-14" "2017-03-15" "2017-03-16"
## [556] "2017-03-17" "2017-03-20" "2017-03-21" "2017-03-22" "2017-03-23"
## [561] "2017-03-24" "2017-03-27" "2017-03-28" "2017-03-29" "2017-03-30"
## [566] "2017-03-31" "2017-04-03" "2017-04-04" "2017-04-05" "2017-04-06"
## [571] "2017-04-07" "2017-04-10" "2017-04-11" "2017-04-12" "2017-04-13"
## [576] "2017-04-17" "2017-04-18" "2017-04-19" "2017-04-20" "2017-04-21"
## [581] "2017-04-24" "2017-04-25" "2017-04-26" "2017-04-27" "2017-04-28"
## [586] "2017-05-01" "2017-05-02" "2017-05-03" "2017-05-04" "2017-05-05"
## [591] "2017-05-08" "2017-05-09" "2017-05-10" "2017-05-11" "2017-05-12"
## [596] "2017-05-15" "2017-05-16" "2017-05-17" "2017-05-18" "2017-05-19"
## [601] "2017-05-22" "2017-05-23" "2017-05-24" "2017-05-25" "2017-05-26"
## [606] "2017-05-30" "2017-05-31" "2017-06-01" "2017-06-02" "2017-06-05"
## [611] "2017-06-06" "2017-06-07" "2017-06-08" "2017-06-09" "2017-06-12"
## [616] "2017-06-13" "2017-06-14" "2017-06-15" "2017-06-16" "2017-06-19"
## [621] "2017-06-20" "2017-06-21" "2017-06-22" "2017-06-23" "2017-06-26"
## [626] "2017-06-27" "2017-06-28" "2017-06-29" "2017-06-30" "2017-07-03"
## [631] "2017-07-05" "2017-07-06" "2017-07-07" "2017-07-10" "2017-07-11"
## [636] "2017-07-12" "2017-07-13" "2017-07-14" "2017-07-17" "2017-07-18"
## [641] "2017-07-19" "2017-07-20" "2017-07-21" "2017-07-24" "2017-07-25"
## [646] "2017-07-26" "2017-07-27" "2017-07-28" "2017-07-31" "2017-08-01"
## [651] "2017-08-02" "2017-08-03" "2017-08-04" "2017-08-07" "2017-08-08"
## [656] "2017-08-09" "2017-08-10" "2017-08-11" "2017-08-14" "2017-08-15"
## [661] "2017-08-16" "2017-08-17" "2017-08-18" "2017-08-21" "2017-08-22"
## [666] "2017-08-23" "2017-08-24" "2017-08-25" "2017-08-28" "2017-08-29"
## [671] "2017-08-30" "2017-08-31" "2017-09-01" "2017-09-05" "2017-09-06"
## [676] "2017-09-07" "2017-09-08" "2017-09-11" "2017-09-12" "2017-09-13"
## [681] "2017-09-14" "2017-09-15" "2017-09-18" "2017-09-19" "2017-09-20"
## [686] "2017-09-21" "2017-09-22" "2017-09-25" "2017-09-26" "2017-09-27"
## [691] "2017-09-28" "2017-09-29" "2017-10-02" "2017-10-03" "2017-10-04"
## [696] "2017-10-05" "2017-10-06" "2017-10-09" "2017-10-10" "2017-10-11"
## [701] "2017-10-12" "2017-10-13" "2017-10-16" "2017-10-17" "2017-10-18"
## [706] "2017-10-19" "2017-10-20" "2017-10-23" "2017-10-24" "2017-10-25"
## [711] "2017-10-26" "2017-10-27" "2017-10-30" "2017-10-31" "2017-11-01"
## [716] "2017-11-02" "2017-11-03" "2017-11-06" "2017-11-07" "2017-11-08"
## [721] "2017-11-09" "2017-11-10" "2017-11-13" "2017-11-14" "2017-11-15"
## [726] "2017-11-16" "2017-11-17" "2017-11-20" "2017-11-21" "2017-11-22"
## [731] "2017-11-24" "2017-11-27" "2017-11-28" "2017-11-29" "2017-11-30"
## [736] "2017-12-01" "2017-12-04" "2017-12-05" "2017-12-06" "2017-12-07"
## [741] "2017-12-08" "2017-12-11" "2017-12-12" "2017-12-13" "2017-12-14"
## [746] "2017-12-15" "2017-12-18" "2017-12-19" "2017-12-20" "2017-12-21"
## [751] "2017-12-22" "2017-12-26" "2017-12-27" "2017-12-28" "2017-12-29"
## [756] "2018-01-02" "2018-01-03" "2018-01-04" "2018-01-05" "2018-01-08"
## [761] "2018-01-09" "2018-01-10" "2018-01-11" "2018-01-12" "2018-01-16"
## [766] "2018-01-17" "2018-01-18" "2018-01-19" "2018-01-22" "2018-01-23"
## [771] "2018-01-24" "2018-01-25" "2018-01-26" "2018-01-29" "2018-01-30"
## [776] "2018-01-31" "2018-02-01" "2018-02-02" "2018-02-05" "2018-02-06"
## [781] "2018-02-07" "2018-02-08" "2018-02-09" "2018-02-12" "2018-02-13"
## [786] "2018-02-14" "2018-02-15" "2018-02-16" "2018-02-20" "2018-02-21"
## [791] "2018-02-22" "2018-02-23" "2018-02-26" "2018-02-27" "2018-02-28"
## [796] "2018-03-01" "2018-03-02" "2018-03-05" "2018-03-06" "2018-03-07"
## [801] "2018-03-08" "2018-03-09" "2018-03-12" "2018-03-13" "2018-03-14"
## [806] "2018-03-15" "2018-03-16" "2018-03-19" "2018-03-20" "2018-03-21"
## [811] "2018-03-22" "2018-03-23" "2018-03-26" "2018-03-27" "2018-03-28"
## [816] "2018-03-29" "2018-04-02" "2018-04-03" "2018-04-04" "2018-04-05"
## [821] "2018-04-06" "2018-04-09" "2018-04-10" "2018-04-11" "2018-04-12"
## [826] "2018-04-13" "2018-04-16" "2018-04-17" "2018-04-18" "2018-04-19"
## [831] "2018-04-20" "2018-04-23" "2018-04-24" "2018-04-25" "2018-04-26"
## [836] "2018-04-27" "2018-04-30" "2018-05-01" "2018-05-02" "2018-05-03"
## [841] "2018-05-04" "2018-05-07" "2018-05-08" "2018-05-09" "2018-05-10"
## [846] "2018-05-11" "2018-05-14" "2018-05-15" "2018-05-16" "2018-05-17"
## [851] "2018-05-18" "2018-05-21" "2018-05-22" "2018-05-23" "2018-05-24"
## [856] "2018-05-25" "2018-05-29" "2018-05-30" "2018-05-31" "2018-06-01"
## [861] "2018-06-04" "2018-06-05" "2018-06-06" "2018-06-07" "2018-06-08"
## [866] "2018-06-11" "2018-06-12" "2018-06-13" "2018-06-14" "2018-06-15"
## [871] "2018-06-18" "2018-06-19" "2018-06-20" "2018-06-21" "2018-06-22"
## [876] "2018-06-25" "2018-06-26" "2018-06-27" "2018-06-28" "2018-06-29"
## [881] "2018-07-02" "2018-07-03" "2018-07-05" "2018-07-06" "2018-07-09"
## [886] "2018-07-10" "2018-07-11" "2018-07-12" "2018-07-13" "2018-07-16"
## [891] "2018-07-17" "2018-07-18" "2018-07-19" "2018-07-20" "2018-07-23"
## [896] "2018-07-24" "2018-07-25" "2018-07-26" "2018-07-27" "2018-07-30"
## [901] "2018-07-31" "2018-08-01" "2018-08-02" "2018-08-03" "2018-08-06"
## [906] "2018-08-07" "2018-08-08" "2018-08-09" "2018-08-10" "2018-08-13"
## [911] "2018-08-14" "2018-08-15" "2018-08-16" "2018-08-17" "2018-08-20"
## [916] "2018-08-21" "2018-08-22" "2018-08-23" "2018-08-24" "2018-08-27"
## [921] "2018-08-28" "2018-08-29" "2018-08-30" "2018-08-31" "2018-09-04"
## [926] "2018-09-05" "2018-09-06" "2018-09-07" "2018-09-10" "2018-09-11"
## [931] "2018-09-12" "2018-09-13" "2018-09-14" "2018-09-17" "2018-09-18"
## [936] "2018-09-19" "2018-09-20" "2018-09-21" "2018-09-24" "2018-09-25"
## [941] "2018-09-26" "2018-09-27" "2018-09-28" "2018-10-01" "2018-10-02"
## [946] "2018-10-03" "2018-10-04" "2018-10-05" "2018-10-08" "2018-10-09"
## [951] "2018-10-10" "2018-10-11" "2018-10-12" "2018-10-15" "2018-10-16"
## [956] "2018-10-17" "2018-10-18" "2018-10-19" "2018-10-22" "2018-10-23"
## [961] "2018-10-24" "2018-10-25" "2018-10-26" "2018-10-29" "2018-10-30"
## [966] "2018-10-31" "2018-11-01" "2018-11-02" "2018-11-05" "2018-11-06"
## [971] "2018-11-07" "2018-11-08" "2018-11-09" "2018-11-12" "2018-11-13"
## [976] "2018-11-14" "2018-11-15" "2018-11-16" "2018-11-19" "2018-11-20"
## [981] "2018-11-21" "2018-11-23" "2018-11-26" "2018-11-27" "2018-11-28"
## [986] "2018-11-29" "2018-11-30" "2018-12-03" "2018-12-04" "2018-12-06"
## [991] "2018-12-07" "2018-12-10" "2018-12-11" "2018-12-12" "2018-12-13"
## [996] "2018-12-14" "2018-12-17" "2018-12-18" "2018-12-19" "2018-12-20"
## [1001] "2018-12-21" "2018-12-24" "2018-12-26" "2018-12-27" "2018-12-28"
## [1006] "2018-12-31" "2019-01-02" "2019-01-03" "2019-01-04" "2019-01-07"
## [1011] "2019-01-08" "2019-01-09" "2019-01-10" "2019-01-11" "2019-01-14"
## [1016] "2019-01-15" "2019-01-16" "2019-01-17" "2019-01-18" "2019-01-22"
## [1021] "2019-01-23" "2019-01-24" "2019-01-25" "2019-01-28" "2019-01-29"
## [1026] "2019-01-30" "2019-01-31" "2019-02-01" "2019-02-04" "2019-02-05"
## [1031] "2019-02-06" "2019-02-07" "2019-02-08" "2019-02-11" "2019-02-12"
## [1036] "2019-02-13" "2019-02-14" "2019-02-15" "2019-02-19" "2019-02-20"
## [1041] "2019-02-21" "2019-02-22" "2019-02-25" "2019-02-26" "2019-02-27"
## [1046] "2019-02-28" "2019-03-01" "2019-03-04" "2019-03-05" "2019-03-06"
## [1051] "2019-03-07" "2019-03-08" "2019-03-11" "2019-03-12" "2019-03-13"
## [1056] "2019-03-14" "2019-03-15" "2019-03-18" "2019-03-19" "2019-03-20"
## [1061] "2019-03-21" "2019-03-22" "2019-03-25" "2019-03-26" "2019-03-27"
## [1066] "2019-03-28" "2019-03-29" "2019-04-01" "2019-04-02" "2019-04-03"
## [1071] "2019-04-04" "2019-04-05" "2019-04-08" "2019-04-09" "2019-04-10"
## [1076] "2019-04-11" "2019-04-12" "2019-04-15" "2019-04-16" "2019-04-17"
## [1081] "2019-04-18" "2019-04-22" "2019-04-23" "2019-04-24" "2019-04-25"
## [1086] "2019-04-26" "2019-04-29" "2019-04-30" "2019-05-01" "2019-05-02"
## [1091] "2019-05-03" "2019-05-06" "2019-05-07" "2019-05-08" "2019-05-09"
## [1096] "2019-05-10" "2019-05-13" "2019-05-14" "2019-05-15" "2019-05-16"
## [1101] "2019-05-17" "2019-05-20" "2019-05-21" "2019-05-22" "2019-05-23"
## [1106] "2019-05-24" "2019-05-28" "2019-05-29" "2019-05-30" "2019-05-31"
## [1111] "2019-06-03" "2019-06-04" "2019-06-05" "2019-06-06" "2019-06-07"
## [1116] "2019-06-10" "2019-06-11" "2019-06-12" "2019-06-13" "2019-06-14"
## [1121] "2019-06-17" "2019-06-18" "2019-06-19" "2019-06-20" "2019-06-21"
## [1126] "2019-06-24" "2019-06-25" "2019-06-26" "2019-06-27" "2019-06-28"
## [1131] "2019-07-01" "2019-07-02" "2019-07-03" "2019-07-05" "2019-07-08"
## [1136] "2019-07-09" "2019-07-10" "2019-07-11" "2019-07-12" "2019-07-15"
## [1141] "2019-07-16" "2019-07-17" "2019-07-18" "2019-07-19" "2019-07-22"
## [1146] "2019-07-23" "2019-07-24" "2019-07-25" "2019-07-26" "2019-07-29"
## [1151] "2019-07-30" "2019-07-31" "2019-08-01" "2019-08-02" "2019-08-05"
## [1156] "2019-08-06" "2019-08-07" "2019-08-08" "2019-08-09" "2019-08-12"
## [1161] "2019-08-13" "2019-08-14" "2019-08-15" "2019-08-16" "2019-08-19"
## [1166] "2019-08-20" "2019-08-21" "2019-08-22" "2019-08-23" "2019-08-26"
## [1171] "2019-08-27" "2019-08-28" "2019-08-29" "2019-08-30" "2019-09-03"
## [1176] "2019-09-04" "2019-09-05" "2019-09-06" "2019-09-09" "2019-09-10"
## [1181] "2019-09-11" "2019-09-12" "2019-09-13" "2019-09-16" "2019-09-17"
## [1186] "2019-09-18" "2019-09-19" "2019-09-20" "2019-09-23" "2019-09-24"
## [1191] "2019-09-25" "2019-09-26" "2019-09-27" "2019-09-30" "2019-10-01"
## [1196] "2019-10-02" "2019-10-03" "2019-10-04" "2019-10-07" "2019-10-08"
## [1201] "2019-10-09" "2019-10-10" "2019-10-11" "2019-10-14" "2019-10-15"
## [1206] "2019-10-16" "2019-10-17" "2019-10-18" "2019-10-21" "2019-10-22"
## [1211] "2019-10-23" "2019-10-24" "2019-10-25" "2019-10-28" "2019-10-29"
## [1216] "2019-10-30" "2019-10-31" "2019-11-01" "2019-11-04" "2019-11-05"
## [1221] "2019-11-06" "2019-11-07" "2019-11-08" "2019-11-11" "2019-11-12"
## [1226] "2019-11-13" "2019-11-14" "2019-11-15" "2019-11-18" "2019-11-19"
## [1231] "2019-11-20" "2019-11-21" "2019-11-22" "2019-11-25" "2019-11-26"
## [1236] "2019-11-27" "2019-11-29" "2019-12-02" "2019-12-03" "2019-12-04"
## [1241] "2019-12-05" "2019-12-06" "2019-12-09" "2019-12-10" "2019-12-11"
## [1246] "2019-12-12" "2019-12-13" "2019-12-16" "2019-12-17" "2019-12-18"
## [1251] "2019-12-19" "2019-12-20" "2019-12-23" "2019-12-24" "2019-12-26"
## [1256] "2019-12-27" "2019-12-30" "2019-12-31"
str(returns)
## An xts object on 2015-01-02 / 2019-12-31 containing:
## Data: double [1258, 2]
## Columns: SPY, TBILL
## Index: Date [1258] (TZ: "UTC")
## xts Attributes:
## $ src : chr "yahoo"
## $ updated: POSIXct[1:1], format: "2024-03-10 20:30:08"
# Desv Est. 2015
returns["2015"]
## SPY TBILL
## 2015-01-02 -0.0005352342 -0.0002182023
## 2015-01-05 -0.0180595796 0.0002182499
## 2015-01-06 -0.0094187671 -0.0002182023
## 2015-01-07 0.0124608632 0.0000000000
## 2015-01-08 0.0177452087 0.0000000000
## 2015-01-09 -0.0080136049 0.0000000000
## 2015-01-12 -0.0078333970 0.0000000000
## 2015-01-13 -0.0028129100 0.0000000000
## 2015-01-14 -0.0060370995 0.0000000000
## 2015-01-15 -0.0091606454 0.0000000000
## ...
## 2015-12-17 -0.0152379590 -0.0002182531
## 2015-12-18 -0.0178153683 0.0002183007
## 2015-12-21 0.0082492901 -0.0002182531
## 2015-12-22 0.0090740705 0.0000000000
## 2015-12-23 0.0123834813 0.0000000000
## 2015-12-24 -0.0016504178 0.0002183007
## 2015-12-28 -0.0022851736 -0.0002182531
## 2015-12-29 0.0106719879 0.0000000000
## 2015-12-30 -0.0070878753 0.0002183007
## 2015-12-31 -0.0100031601 -0.0002182531
sd_2015=apply(X =returns["2015"], MARGIN=2, FUN=sd)
sd_2015
## SPY TBILL
## 0.0097211722 0.0001655439
# Desv Est. 2016
returns["2016"]
## SPY TBILL
## 2016-01-04 -0.0139793613 2.183007e-04
## 2016-01-05 0.0016911191 0.000000e+00
## 2016-01-06 -0.0126139474 -4.369762e-04
## 2016-01-07 -0.0239917259 2.188187e-04
## 2016-01-08 -0.0109765483 0.000000e+00
## 2016-01-11 0.0009899679 0.000000e+00
## 2016-01-12 0.0080682982 2.183007e-04
## 2016-01-13 -0.0249406176 -2.182531e-04
## 2016-01-14 0.0164168910 -2.187708e-04
## 2016-01-15 -0.0214659166 0.000000e+00
## ...
## 2016-12-16 -0.0019558507 -2.188546e-04
## 2016-12-19 0.0021772818 2.189025e-04
## 2016-12-20 0.0038578482 -2.188546e-04
## 2016-12-21 -0.0027828390 2.189025e-04
## 2016-12-22 -0.0017273866 -2.188546e-04
## 2016-12-23 0.0014644165 0.000000e+00
## 2016-12-27 0.0024806629 2.189025e-04
## 2016-12-28 -0.0082643020 -2.196996e-05
## 2016-12-29 -0.0002229711 2.189533e-04
## 2016-12-30 -0.0036548883 -4.374353e-04
sd_2016=apply(X =returns["2016"], MARGIN=2, FUN=sd)
sd_2016
## SPY TBILL
## 0.0082278353 0.0001848743
# Desv Est. 2017
returns["2017"]
## SPY TBILL
## 2017-01-03 0.0076501502 4.376267e-04
## 2017-01-04 0.0059491233 0.000000e+00
## 2017-01-05 -0.0007943351 -2.189054e-04
## 2017-01-06 0.0035775017 2.189533e-04
## 2017-01-09 -0.0033007045 0.000000e+00
## 2017-01-10 0.0000000000 -2.189054e-04
## 2017-01-11 0.0028260017 2.189533e-04
## 2017-01-12 -0.0025100138 0.000000e+00
## 2017-01-13 0.0022954553 0.000000e+00
## 2017-01-17 -0.0035233236 0.000000e+00
## ...
## 2017-12-15 0.0083274265 1.096289e-04
## 2017-12-18 0.0063410838 1.091505e-04
## 2017-12-19 -0.0038406658 2.173443e-05
## 2017-12-20 -0.0005236452 0.000000e+00
## 2017-12-21 0.0020594738 0.000000e+00
## 2017-12-22 -0.0002614844 2.191119e-04
## 2017-12-26 -0.0011963188 -1.093921e-04
## 2017-12-27 0.0004865392 2.186215e-04
## 2017-12-28 0.0020571769 0.000000e+00
## 2017-12-29 -0.0037700049 -2.185737e-04
sd_2017=apply(X =returns["2017"], MARGIN=2, FUN=sd)
sd_2017
## SPY TBILL
## 0.0042482131 0.0001925443
# Desv Est. 2018
returns["2018"]
## SPY TBILL
## 2018-01-02 0.007157208 0.0001094040
## 2018-01-03 0.006325304 0.0002188774
## 2018-01-04 0.004214588 0.0001091816
## 2018-01-05 0.006663743 0.0000000000
## 2018-01-08 0.001828999 0.0001089833
## 2018-01-09 0.002263165 0.0000000000
## 2018-01-10 -0.001529624 0.0000000000
## 2018-01-11 0.007296081 -0.0001089714
## 2018-01-12 0.006518605 0.0003278821
## 2018-01-16 -0.003418041 -0.0002188271
## ...
## 2018-12-17 -0.019618280 0.0001090182
## 2018-12-18 -0.001096450 0.0000000000
## 2018-12-19 -0.014975732 -0.0001094650
## 2018-12-20 -0.016278052 0.0002190458
## 2018-12-21 -0.020489438 0.0002184474
## 2018-12-24 -0.026423062 0.0001097961
## 2018-12-26 0.050524887 -0.0001097840
## 2018-12-27 0.007677444 0.0003282875
## 2018-12-28 -0.001290115 0.0001094849
## 2018-12-31 0.008758842 0.0001094729
sd_2018=apply(X =returns["2018"], MARGIN=2, FUN=sd)
sd_2018
## SPY TBILL
## 0.010732180 0.000116941
# Desv Est. 2019
returns["2019"]
## SPY TBILL
## 2019-01-02 1.040273e-03 -1.094610e-04
## 2019-01-03 -2.386271e-02 3.279604e-04
## 2019-01-04 3.349580e-02 1.094371e-04
## 2019-01-07 7.884584e-03 0.000000e+00
## 2019-01-08 9.395465e-03 0.000000e+00
## 2019-01-09 4.673512e-03 0.000000e+00
## 2019-01-10 3.527468e-03 1.094251e-04
## 2019-01-11 3.863703e-04 2.182764e-04
## 2019-01-14 -6.101028e-03 -1.089312e-04
## 2019-01-15 1.146073e-02 2.188023e-04
## ...
## 2019-12-17 2.189552e-04 -1.092217e-04
## 2019-12-18 6.254492e-05 2.184672e-04
## 2019-12-19 4.099093e-03 1.095693e-04
## 2019-12-20 4.384119e-03 5.455396e-05
## 2019-12-23 1.527749e-03 0.000000e+00
## 2019-12-24 3.106079e-05 1.097311e-04
## 2019-12-26 5.323269e-03 1.090900e-04
## 2019-12-27 -2.476660e-04 -1.090781e-04
## 2019-12-30 -5.513218e-03 1.090900e-04
## 2019-12-31 2.429365e-03 0.000000e+00
sd_2019=apply(X =returns["2019"], MARGIN=2, FUN=sd)
sd_2019
## SPY TBILL
## 0.0078811216 0.0001192186
Se desmuestra que el riesgo cambia en el tiempo, es mediante calcular y comparar las desviaciones estándar de los retornos de los activos financieros para cada año de 2015 a 2019.
# row - rbind #
# column- cbind#
sd_all= rbind(sd_2015, sd_2016, sd_2017, sd_2018, sd_2019)
sd_all
## SPY TBILL
## sd_2015 0.009721172 0.0001655439
## sd_2016 0.008227835 0.0001848743
## sd_2017 0.004248213 0.0001925443
## sd_2018 0.010732180 0.0001169410
## sd_2019 0.007881122 0.0001192186
Se dejan los datos en una sola matriz de estudio, cambio de columna a fila, con el fin de comparar el retorno anual de cada uno de los activos SPY y BIL
nrow(returns["2019"]) # DÍAS DE TRANSACCIÓN #
## [1] 252
sd_all_annual=sqrt(252) *sd_all
sd_all_annual
## SPY TBILL
## sd_2015 0.15431883 0.002627928
## sd_2016 0.13061284 0.002934788
## sd_2017 0.06743829 0.003056545
## sd_2018 0.17036807 0.001856381
## sd_2019 0.12510893 0.001892536
barplot(t(sd_all_annual),beside = T,
col=c("blue","red"),
main="Desv.Est. anualizada",
legend.text=c("SPY","T-BILLS")) # TRASNCRITA #
se calculan las desviaciones estándar anualizadas de los retornos de los activos financieros SPY y BIL. Luego, se crea un gráfico de barras para comparar las desviaciones y visualizar la volatilidad a lo largo del tiempo. En el gráfico, las barras azules representan la volatilidad de SPY, mientras que las barras rojas representan la volatilidad de BIL. Esta visualización nos ayuda a entender cómo varía la volatilidad de estos activos financieros a lo largo del tiempo
1. Se puede concluir que bonos de gobierno con madurez entre uno y tres meses poseen un muy bajo riesgo, esto contrario al EFT del S&P 500 el cual presenta un riesgo tan alto que es 52 veces mayor que el activo BIL.
2. En cuestión de volatilidad según el histograma se puede observar que el activo BIL tiene una volatilidad baja, en cambio el activo SPY presenta una tendencia leptocurtica es decir presenta una mayor volatidad o riesgo a lo largo del tiempo
3. Por ultimó en base a los retornos se puede observar que el activo SPY presenta mayor retorno para cada uno de los años en comparación al activo BIL, esto debido a su mayor riesgo y volatilidad “Entre mayor riesgo mayor retorno”