#Disyuntiva riesgo retorno

Para realizar este ejercicio se debe subir la libreria correspondiente para el cargue de datos,en este caso se utilizara la quantmod. Asi mismo se establece un periodo de tiempo determinado de 3 años o mas. Por lo cual tendremos un periodo de 2017 a 2020 con los activos apple y T bills

library(quantmod)
## Warning: package 'quantmod' was built under R version 4.2.3
## Loading required package: xts
## Warning: package 'xts' was built under R version 4.2.3
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.2.3
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## Warning: package 'TTR' was built under R version 4.2.3
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
data_aapl=getSymbols("AAPL", from="2017-12-31", to="2020-01-01", auto.assign = F)
head(data_aapl,2)
##            AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
## 2018-01-02   42.5400   43.0750   42.315    43.0650   102223600      40.67097
## 2018-01-03   43.1325   43.6375   42.990    43.0575   118071600      40.66390
tail(data_aapl,2)
##            AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
## 2019-12-30   72.3650   73.1725   71.305    72.8800   144114400      70.91152
## 2019-12-31   72.4825   73.4200   72.380    73.4125   100805600      71.42966
data_TBill=getSymbols("BIL",from="2017-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
## 2018-01-02    91.41    91.43   91.41     91.41     790000     81.80923
## 2018-01-03    91.43    91.43   91.42     91.43     218300     81.82711
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.91241
## 2019-12-31    91.45    91.45   91.43     91.43    1406200     84.91241

##Retornos

A partir de los datos obtenidos, se corre el codigo de retornos y retornos acumulados

ret_aapl=Delt(data_aapl$AAPL.Adjusted)
ret_TBill=Delt(data_TBill$BIL.Adjusted)
rets=cbind(ret_aapl,ret_TBill)
head(rets,2)
##            Delt.1.arithmetic Delt.1.arithmetic.1
## 2018-01-02                NA                  NA
## 2018-01-03     -0.0001738943        0.0002185976
names(rets)=c("AAPL","TBILL")

rets[1,]=c(0,0)
gross_ret=1+rets
head(rets,3)
##                     AAPL        TBILL
## 2018-01-02  0.0000000000 0.0000000000
## 2018-01-03 -0.0001738943 0.0002185976
## 2018-01-04  0.0046449290 0.0001094614
head(gross_ret,3)
##                 AAPL    TBILL
## 2018-01-02 1.0000000 1.000000
## 2018-01-03 0.9998261 1.000219
## 2018-01-04 1.0046449 1.000109
cum_rets=cumprod(gross_ret)
head(cum_rets)
##                 AAPL    TBILL
## 2018-01-02 1.0000000 1.000000
## 2018-01-03 0.9998261 1.000219
## 2018-01-04 1.0044702 1.000328
## 2018-01-05 1.0159061 1.000328
## 2018-01-08 1.0121331 1.000438
## 2018-01-09 1.0120168 1.000438
tail(cum_rets)
##                AAPL    TBILL
## 2019-12-23 1.698566 1.037705
## 2019-12-24 1.700181 1.037819
## 2019-12-26 1.733913 1.037932
## 2019-12-27 1.733255 1.037819
## 2019-12-30 1.743541 1.037932
## 2019-12-31 1.756281 1.037932
plot(rets$AAPL,col="red", main="retornos de apple y T-Bills")

lines(rets$TBILL,col="blue",lwd = 4)

plot(cum_rets$AAPL,col="pink")

lines(cum_rets$TBILL, col="purple", lwd = 3)

De igual modo, se hacen las graficas correspondientes de los retonos obtenidos

##Riesgos

Seguido de los retornos, se obtendra cual activo es mas riesgoso Por lo cual se corre el siguiente codigo Se debe recordar que sd hace referencia a la desviacion estandar (indica qué tan dispersos están los datos con respecto a la media)

head(rets)
##                     AAPL        TBILL
## 2018-01-02  0.0000000000 0.0000000000
## 2018-01-03 -0.0001738943 0.0002185976
## 2018-01-04  0.0046449290 0.0001094614
## 2018-01-05  0.0113849528 0.0000000000
## 2018-01-08 -0.0037138882 0.0001095426
## 2018-01-09 -0.0001149105 0.0000000000
returns=rets[-1,]
head(returns)
##                     AAPL        TBILL
## 2018-01-03 -0.0001738943 0.0002185976
## 2018-01-04  0.0046449290 0.0001094614
## 2018-01-05  0.0113849528 0.0000000000
## 2018-01-08 -0.0037138882 0.0001095426
## 2018-01-09 -0.0001149105 0.0000000000
## 2018-01-10 -0.0002293839 0.0000000000
sd_aapl=sd(returns$AAPL) 
sd_aapl
## [1] 0.0173413
sd_bill=sd(returns$TBILL)
sd_bill
## [1] 0.0001182017
sd_aapl/sd_bill
## [1] 146.7094
hist(returns$AAPL,breaks = 50)

sd_aapl*100
## [1] 1.73413
sd_aapl*3
## [1] 0.05202389
pnorm(3)-pnorm(-1)
## [1] 0.8399948
pnorm(2)-pnorm(2)
## [1] 0
#desv est. 2017
head(returns)
##                     AAPL        TBILL
## 2018-01-03 -0.0001738943 0.0002185976
## 2018-01-04  0.0046449290 0.0001094614
## 2018-01-05  0.0113849528 0.0000000000
## 2018-01-08 -0.0037138882 0.0001095426
## 2018-01-09 -0.0001149105 0.0000000000
## 2018-01-10 -0.0002293839 0.0000000000
index(returns)
##   [1] "2018-01-03" "2018-01-04" "2018-01-05" "2018-01-08" "2018-01-09"
##   [6] "2018-01-10" "2018-01-11" "2018-01-12" "2018-01-16" "2018-01-17"
##  [11] "2018-01-18" "2018-01-19" "2018-01-22" "2018-01-23" "2018-01-24"
##  [16] "2018-01-25" "2018-01-26" "2018-01-29" "2018-01-30" "2018-01-31"
##  [21] "2018-02-01" "2018-02-02" "2018-02-05" "2018-02-06" "2018-02-07"
##  [26] "2018-02-08" "2018-02-09" "2018-02-12" "2018-02-13" "2018-02-14"
##  [31] "2018-02-15" "2018-02-16" "2018-02-20" "2018-02-21" "2018-02-22"
##  [36] "2018-02-23" "2018-02-26" "2018-02-27" "2018-02-28" "2018-03-01"
##  [41] "2018-03-02" "2018-03-05" "2018-03-06" "2018-03-07" "2018-03-08"
##  [46] "2018-03-09" "2018-03-12" "2018-03-13" "2018-03-14" "2018-03-15"
##  [51] "2018-03-16" "2018-03-19" "2018-03-20" "2018-03-21" "2018-03-22"
##  [56] "2018-03-23" "2018-03-26" "2018-03-27" "2018-03-28" "2018-03-29"
##  [61] "2018-04-02" "2018-04-03" "2018-04-04" "2018-04-05" "2018-04-06"
##  [66] "2018-04-09" "2018-04-10" "2018-04-11" "2018-04-12" "2018-04-13"
##  [71] "2018-04-16" "2018-04-17" "2018-04-18" "2018-04-19" "2018-04-20"
##  [76] "2018-04-23" "2018-04-24" "2018-04-25" "2018-04-26" "2018-04-27"
##  [81] "2018-04-30" "2018-05-01" "2018-05-02" "2018-05-03" "2018-05-04"
##  [86] "2018-05-07" "2018-05-08" "2018-05-09" "2018-05-10" "2018-05-11"
##  [91] "2018-05-14" "2018-05-15" "2018-05-16" "2018-05-17" "2018-05-18"
##  [96] "2018-05-21" "2018-05-22" "2018-05-23" "2018-05-24" "2018-05-25"
## [101] "2018-05-29" "2018-05-30" "2018-05-31" "2018-06-01" "2018-06-04"
## [106] "2018-06-05" "2018-06-06" "2018-06-07" "2018-06-08" "2018-06-11"
## [111] "2018-06-12" "2018-06-13" "2018-06-14" "2018-06-15" "2018-06-18"
## [116] "2018-06-19" "2018-06-20" "2018-06-21" "2018-06-22" "2018-06-25"
## [121] "2018-06-26" "2018-06-27" "2018-06-28" "2018-06-29" "2018-07-02"
## [126] "2018-07-03" "2018-07-05" "2018-07-06" "2018-07-09" "2018-07-10"
## [131] "2018-07-11" "2018-07-12" "2018-07-13" "2018-07-16" "2018-07-17"
## [136] "2018-07-18" "2018-07-19" "2018-07-20" "2018-07-23" "2018-07-24"
## [141] "2018-07-25" "2018-07-26" "2018-07-27" "2018-07-30" "2018-07-31"
## [146] "2018-08-01" "2018-08-02" "2018-08-03" "2018-08-06" "2018-08-07"
## [151] "2018-08-08" "2018-08-09" "2018-08-10" "2018-08-13" "2018-08-14"
## [156] "2018-08-15" "2018-08-16" "2018-08-17" "2018-08-20" "2018-08-21"
## [161] "2018-08-22" "2018-08-23" "2018-08-24" "2018-08-27" "2018-08-28"
## [166] "2018-08-29" "2018-08-30" "2018-08-31" "2018-09-04" "2018-09-05"
## [171] "2018-09-06" "2018-09-07" "2018-09-10" "2018-09-11" "2018-09-12"
## [176] "2018-09-13" "2018-09-14" "2018-09-17" "2018-09-18" "2018-09-19"
## [181] "2018-09-20" "2018-09-21" "2018-09-24" "2018-09-25" "2018-09-26"
## [186] "2018-09-27" "2018-09-28" "2018-10-01" "2018-10-02" "2018-10-03"
## [191] "2018-10-04" "2018-10-05" "2018-10-08" "2018-10-09" "2018-10-10"
## [196] "2018-10-11" "2018-10-12" "2018-10-15" "2018-10-16" "2018-10-17"
## [201] "2018-10-18" "2018-10-19" "2018-10-22" "2018-10-23" "2018-10-24"
## [206] "2018-10-25" "2018-10-26" "2018-10-29" "2018-10-30" "2018-10-31"
## [211] "2018-11-01" "2018-11-02" "2018-11-05" "2018-11-06" "2018-11-07"
## [216] "2018-11-08" "2018-11-09" "2018-11-12" "2018-11-13" "2018-11-14"
## [221] "2018-11-15" "2018-11-16" "2018-11-19" "2018-11-20" "2018-11-21"
## [226] "2018-11-23" "2018-11-26" "2018-11-27" "2018-11-28" "2018-11-29"
## [231] "2018-11-30" "2018-12-03" "2018-12-04" "2018-12-06" "2018-12-07"
## [236] "2018-12-10" "2018-12-11" "2018-12-12" "2018-12-13" "2018-12-14"
## [241] "2018-12-17" "2018-12-18" "2018-12-19" "2018-12-20" "2018-12-21"
## [246] "2018-12-24" "2018-12-26" "2018-12-27" "2018-12-28" "2018-12-31"
## [251] "2019-01-02" "2019-01-03" "2019-01-04" "2019-01-07" "2019-01-08"
## [256] "2019-01-09" "2019-01-10" "2019-01-11" "2019-01-14" "2019-01-15"
## [261] "2019-01-16" "2019-01-17" "2019-01-18" "2019-01-22" "2019-01-23"
## [266] "2019-01-24" "2019-01-25" "2019-01-28" "2019-01-29" "2019-01-30"
## [271] "2019-01-31" "2019-02-01" "2019-02-04" "2019-02-05" "2019-02-06"
## [276] "2019-02-07" "2019-02-08" "2019-02-11" "2019-02-12" "2019-02-13"
## [281] "2019-02-14" "2019-02-15" "2019-02-19" "2019-02-20" "2019-02-21"
## [286] "2019-02-22" "2019-02-25" "2019-02-26" "2019-02-27" "2019-02-28"
## [291] "2019-03-01" "2019-03-04" "2019-03-05" "2019-03-06" "2019-03-07"
## [296] "2019-03-08" "2019-03-11" "2019-03-12" "2019-03-13" "2019-03-14"
## [301] "2019-03-15" "2019-03-18" "2019-03-19" "2019-03-20" "2019-03-21"
## [306] "2019-03-22" "2019-03-25" "2019-03-26" "2019-03-27" "2019-03-28"
## [311] "2019-03-29" "2019-04-01" "2019-04-02" "2019-04-03" "2019-04-04"
## [316] "2019-04-05" "2019-04-08" "2019-04-09" "2019-04-10" "2019-04-11"
## [321] "2019-04-12" "2019-04-15" "2019-04-16" "2019-04-17" "2019-04-18"
## [326] "2019-04-22" "2019-04-23" "2019-04-24" "2019-04-25" "2019-04-26"
## [331] "2019-04-29" "2019-04-30" "2019-05-01" "2019-05-02" "2019-05-03"
## [336] "2019-05-06" "2019-05-07" "2019-05-08" "2019-05-09" "2019-05-10"
## [341] "2019-05-13" "2019-05-14" "2019-05-15" "2019-05-16" "2019-05-17"
## [346] "2019-05-20" "2019-05-21" "2019-05-22" "2019-05-23" "2019-05-24"
## [351] "2019-05-28" "2019-05-29" "2019-05-30" "2019-05-31" "2019-06-03"
## [356] "2019-06-04" "2019-06-05" "2019-06-06" "2019-06-07" "2019-06-10"
## [361] "2019-06-11" "2019-06-12" "2019-06-13" "2019-06-14" "2019-06-17"
## [366] "2019-06-18" "2019-06-19" "2019-06-20" "2019-06-21" "2019-06-24"
## [371] "2019-06-25" "2019-06-26" "2019-06-27" "2019-06-28" "2019-07-01"
## [376] "2019-07-02" "2019-07-03" "2019-07-05" "2019-07-08" "2019-07-09"
## [381] "2019-07-10" "2019-07-11" "2019-07-12" "2019-07-15" "2019-07-16"
## [386] "2019-07-17" "2019-07-18" "2019-07-19" "2019-07-22" "2019-07-23"
## [391] "2019-07-24" "2019-07-25" "2019-07-26" "2019-07-29" "2019-07-30"
## [396] "2019-07-31" "2019-08-01" "2019-08-02" "2019-08-05" "2019-08-06"
## [401] "2019-08-07" "2019-08-08" "2019-08-09" "2019-08-12" "2019-08-13"
## [406] "2019-08-14" "2019-08-15" "2019-08-16" "2019-08-19" "2019-08-20"
## [411] "2019-08-21" "2019-08-22" "2019-08-23" "2019-08-26" "2019-08-27"
## [416] "2019-08-28" "2019-08-29" "2019-08-30" "2019-09-03" "2019-09-04"
## [421] "2019-09-05" "2019-09-06" "2019-09-09" "2019-09-10" "2019-09-11"
## [426] "2019-09-12" "2019-09-13" "2019-09-16" "2019-09-17" "2019-09-18"
## [431] "2019-09-19" "2019-09-20" "2019-09-23" "2019-09-24" "2019-09-25"
## [436] "2019-09-26" "2019-09-27" "2019-09-30" "2019-10-01" "2019-10-02"
## [441] "2019-10-03" "2019-10-04" "2019-10-07" "2019-10-08" "2019-10-09"
## [446] "2019-10-10" "2019-10-11" "2019-10-14" "2019-10-15" "2019-10-16"
## [451] "2019-10-17" "2019-10-18" "2019-10-21" "2019-10-22" "2019-10-23"
## [456] "2019-10-24" "2019-10-25" "2019-10-28" "2019-10-29" "2019-10-30"
## [461] "2019-10-31" "2019-11-01" "2019-11-04" "2019-11-05" "2019-11-06"
## [466] "2019-11-07" "2019-11-08" "2019-11-11" "2019-11-12" "2019-11-13"
## [471] "2019-11-14" "2019-11-15" "2019-11-18" "2019-11-19" "2019-11-20"
## [476] "2019-11-21" "2019-11-22" "2019-11-25" "2019-11-26" "2019-11-27"
## [481] "2019-11-29" "2019-12-02" "2019-12-03" "2019-12-04" "2019-12-05"
## [486] "2019-12-06" "2019-12-09" "2019-12-10" "2019-12-11" "2019-12-12"
## [491] "2019-12-13" "2019-12-16" "2019-12-17" "2019-12-18" "2019-12-19"
## [496] "2019-12-20" "2019-12-23" "2019-12-24" "2019-12-26" "2019-12-27"
## [501] "2019-12-30" "2019-12-31"
str(returns)
## An xts object on 2018-01-03 / 2019-12-31 containing: 
##   Data:    double [502, 2]
##   Columns: AAPL, TBILL
##   Index:   Date [502] (TZ: "UTC")
##   xts Attributes:
##     $ src    : chr "yahoo"
##     $ updated: POSIXct[1:1], format: "2024-03-14 19:46:51"
sd(returns["2017"]) #no sirve
## [1] NA
sd_2017=apply(X=returns["2017"],MARGIN = 2,FUN = sd)
sd_2017
## numeric(0)
#desv est 2018
returns["2018"]
##                     AAPL         TBILL
## 2018-01-03 -0.0001738943  0.0002185976
## 2018-01-04  0.0046449290  0.0001094614
## 2018-01-05  0.0113849528  0.0000000000
## 2018-01-08 -0.0037138882  0.0001095426
## 2018-01-09 -0.0001149105  0.0000000000
## 2018-01-10 -0.0002293839  0.0000000000
## 2018-01-11  0.0056802933 -0.0001095306
## 2018-01-12  0.0103263379  0.0003279753
## 2018-01-16 -0.0050821141 -0.0002183611
## 2018-01-17  0.0165161639  0.0002184088
##        ...                            
## 2018-12-17 -0.0093061764  0.0001092935
## 2018-12-18  0.0129924743  0.0000000000
## 2018-12-19 -0.0311915758 -0.0001094651
## 2018-12-20 -0.0252347975  0.0002189541
## 2018-12-21 -0.0388952996  0.0002192732
## 2018-12-24 -0.0258739897  0.0001087870
## 2018-12-26  0.0704211778 -0.0001087752
## 2018-12-27 -0.0064898453  0.0003277369
## 2018-12-28  0.0005125563  0.0001093932
## 2018-12-31  0.0096653157  0.0001092896
sd_2018=apply(X=returns["2018"],MARGIN = 2,FUN = sd)
sd_2018
##         AAPL        TBILL 
## 0.0181062670 0.0001171324
sd_2019=apply(X=returns["2019"],MARGIN = 2,FUN = sd)
sd_2019
##         AAPL        TBILL 
## 0.0164656042 0.0001192154
sd_2020=apply(X=returns["2020"],MARGIN = 2,FUN = sd)
sd_2020
## numeric(0)
sd_todo=rbind(sd_2018,sd_2019, sd_2020)
sd_todo
##               AAPL        TBILL
## sd_2018 0.01810627 0.0001171324
## sd_2019 0.01646560 0.0001192154
nrow(returns["2020"])
## [1] 0
sd_todo_annual=sqrt(252)*sd_todo
sd_todo_annual
##              AAPL       TBILL
## sd_2018 0.2874281 0.001859420
## sd_2019 0.2613834 0.001892485
barplot(t(sd_todo_annual),beside = T, col = c("blue","red"),main="desv est anualizada",legend.text = c("AAPL","T-BILLS"))

Finalmente se puede concluir a partir de los datos, que el activo mas riesgoso es AAPL, sin embargo el riesgo puede cambiar con el tiempo. Por lo tanto se realizan pruebas en otros periodos.