#Disyuntiva riesgo retorno

inicialmente se debe subir la libreria correspondiente para el cargue de datos, asi mismo se establece un periodo de tiempo determinado. Para este ejercicio se tomara los activos FDX y TBILL del periodo 2014 - 2017

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_fdx=getSymbols("FDX", from="2014-12-31", to="2017-01-01", auto.assign = F)
head(data_fdx,2)
##            FDX.Open FDX.High FDX.Low FDX.Close FDX.Volume FDX.Adjusted
## 2014-12-31   175.70   176.28  173.51    173.66     854200     153.7029
## 2015-01-02   173.78   174.64  170.33    172.45    1249700     152.6320
tail(data_fdx,2)
##            FDX.Open FDX.High FDX.Low FDX.Close FDX.Volume FDX.Adjusted
## 2016-12-29   188.85   189.64  186.42    186.95    1293100     167.9043
## 2016-12-30   187.80   187.96  185.68    186.20    1323200     167.2307
data_TBill=getSymbols("BIL",from="2014-12-31", to="2017-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.24044
tail(data_TBill,2)
##            BIL.Open BIL.High BIL.Low BIL.Close BIL.Volume BIL.Adjusted
## 2016-12-29    91.44    91.44    91.4     91.44     286650     81.27596
## 2016-12-30    91.42    91.42    91.4     91.40     156600     81.24043

##Retornos

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

ret_fdx=Delt(data_fdx$FDX.Adjusted)
ret_TBill=Delt(data_TBill$BIL.Adjusted)
rets=cbind(ret_fdx,ret_TBill)
head(rets,2)
##            Delt.1.arithmetic Delt.1.arithmetic.1
## 2014-12-31                NA                  NA
## 2015-01-02      -0.006967289       -0.0002181083
names(rets)=c("FDX","TBILL")

rets[1,]=c(0,0)
gross_ret=1+rets
head(rets,3)
##                     FDX         TBILL
## 2014-12-31  0.000000000  0.0000000000
## 2015-01-02 -0.006967289 -0.0002181083
## 2015-01-05 -0.015367059  0.0002181559
head(gross_ret,3)
##                  FDX     TBILL
## 2014-12-31 1.0000000 1.0000000
## 2015-01-02 0.9930327 0.9997819
## 2015-01-05 0.9846329 1.0002182
cum_rets=cumprod(gross_ret)
head(cum_rets)
##                  FDX     TBILL
## 2014-12-31 1.0000000 1.0000000
## 2015-01-02 0.9930327 0.9997819
## 2015-01-05 0.9777727 1.0000000
## 2015-01-06 0.9777150 0.9997819
## 2015-01-07 0.9849130 0.9997819
## 2015-01-08 1.0079462 0.9997819
tail(cum_rets)
##                 FDX     TBILL
## 2016-12-22 1.115418 0.9998035
## 2016-12-23 1.121085 0.9998035
## 2016-12-27 1.113431 1.0000223
## 2016-12-28 1.101160 1.0000004
## 2016-12-29 1.092395 1.0002190
## 2016-12-30 1.088013 0.9997818
plot(rets$FDX,col="red", main="retornos de FDX y T-Bills")

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

plot(cum_rets$FDX,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

head(rets)
##                      FDX         TBILL
## 2014-12-31  0.000000e+00  0.0000000000
## 2015-01-02 -6.967289e-03 -0.0002181083
## 2015-01-05 -1.536706e-02  0.0002181559
## 2015-01-06 -5.898971e-05 -0.0002181083
## 2015-01-07  7.362066e-03  0.0000000000
## 2015-01-08  2.338602e-02  0.0000000000
returns=rets[-1,]
head(returns)
##                      FDX         TBILL
## 2015-01-02 -6.967289e-03 -0.0002181083
## 2015-01-05 -1.536706e-02  0.0002181559
## 2015-01-06 -5.898971e-05 -0.0002181083
## 2015-01-07  7.362066e-03  0.0000000000
## 2015-01-08  2.338602e-02  0.0000000000
## 2015-01-09 -1.359652e-02  0.0000000000
sd_fdx=sd(returns$FDX) 
sd_fdx
## [1] 0.01428839
sd_bill=sd(returns$TBILL)
sd_bill
## [1] 0.0001754679
sd_fdx/sd_bill
## [1] 81.43023
hist(returns$FDX,breaks = 50)

sd_fdx*100
## [1] 1.428839
sd_fdx*3
## [1] 0.04286517
pnorm(3)-pnorm(-1)
## [1] 0.8399948
pnorm(2)-pnorm(2)
## [1] 0
#desv est. 2014
head(returns)
##                      FDX         TBILL
## 2015-01-02 -6.967289e-03 -0.0002181083
## 2015-01-05 -1.536706e-02  0.0002181559
## 2015-01-06 -5.898971e-05 -0.0002181083
## 2015-01-07  7.362066e-03  0.0000000000
## 2015-01-08  2.338602e-02  0.0000000000
## 2015-01-09 -1.359652e-02  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"
str(returns)
## An xts object on 2015-01-02 / 2016-12-30 containing: 
##   Data:    double [504, 2]
##   Columns: FDX, TBILL
##   Index:   Date [504] (TZ: "UTC")
##   xts Attributes:
##     $ src    : chr "yahoo"
##     $ updated: POSIXct[1:1], format: "2024-03-14 19:17:53"
sd(returns["2014"]) #no sirve
## [1] NA
sd_2014=apply(X=returns["2014"],MARGIN = 2,FUN = sd)
sd_2014
## numeric(0)
#desv est 2015
returns["2015"]
##                      FDX         TBILL
## 2015-01-02 -6.967289e-03 -0.0002181083
## 2015-01-05 -1.536706e-02  0.0002181559
## 2015-01-06 -5.898971e-05 -0.0002181083
## 2015-01-07  7.362066e-03  0.0000000000
## 2015-01-08  2.338602e-02  0.0000000000
## 2015-01-09 -1.359652e-02  0.0000000000
## 2015-01-12 -5.213145e-04  0.0000000000
## 2015-01-13 -6.200295e-03  0.0000000000
## 2015-01-14  7.288659e-03  0.0000000000
## 2015-01-15  6.367481e-03  0.0000000000
##        ...                            
## 2015-12-17  2.022480e-02 -0.0002189110
## 2015-12-18 -3.088811e-02  0.0002189589
## 2015-12-21 -9.514130e-03 -0.0002189110
## 2015-12-22  1.825058e-02  0.0000000000
## 2015-12-23  1.556524e-02  0.0000000000
## 2015-12-24 -7.099507e-03  0.0002189589
## 2015-12-28 -8.285999e-03 -0.0002189110
## 2015-12-29  5.593057e-03  0.0000000000
## 2015-12-30 -4.022775e-04  0.0002189589
## 2015-12-31 -1.273951e-03 -0.0002189110
sd_2015=apply(X=returns["2015"],MARGIN = 2,FUN = sd)
sd_2015
##         FDX       TBILL 
## 0.013019434 0.000165677
sd_2016=apply(X=returns["2016"],MARGIN = 2,FUN = sd)
sd_2016
##          FDX        TBILL 
## 0.0154414753 0.0001849484
sd_2017=apply(X=returns["2017"],MARGIN = 2,FUN = sd)
sd_2017
## numeric(0)
sd_todo=rbind(sd_2014,sd_2015,sd_2016,sd_2017)
sd_todo
##                FDX        TBILL
## sd_2015 0.01301943 0.0001656770
## sd_2016 0.01544148 0.0001849484
nrow(returns["2017"])
## [1] 0
sd_todo_annual=sqrt(252)*sd_todo
sd_todo_annual
##               FDX       TBILL
## sd_2015 0.2066771 0.002630040
## sd_2016 0.2451258 0.002935965
barplot(t(sd_todo_annual),beside = T, col = c("blue","red"),main="desv est anualizada",legend.text = c("FDX","T-BILLS"))

Se debe recordar que sd hace referencia a la desviacion estandar (indica qué tan dispersos están los datos con respecto a la media)

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