Skip to main content
actualiza datos
Origen Enlace
Robert
  • 176
  • 7

Aqui una solución utilizando monthlyReturn e (s)lapply

library(quantmod)
portfolio_monthly_returns=lapply(xts(df[,-1],order.by = df$Date),monthlyReturn)

portfolio_excess_returns <- lapply(portfolio_monthly_returns,Return.excess, 
                                          Rf = .0003)
sharpe_ratio_manual <- function(portfolio_excess_returns){
  md=mean(portfolio_excess_returns)
  sd=StdDev(portfolio_excess_returns)
  is=round(
  mean(portfolio_excess_returns) / StdDev(portfolio_excess_returns), 4
  )
  c(MD=md,SD=sd,IS=is)}
sapply(portfolio_excess_returns,sharpe_ratio_manual)

O resultado será similar a:

#> sapply(portfolio_excess_returns,sharpe_ratio_manual)
#       Asset1  .SXQR    Asset2   .SXTR  Asset3     Asset4.SXNR     Asset5   .SXMR       .SXAR       .SX3R
#MD# MD 0.00512297007662462 0.03089434004811897 0.02469234004427923 0.028327680009964127 0.02017995008533315 0.007904365
#SD# SD 0.09343994044747675 0.26828429051776959 0.21592801055490708 0.241918590594352491 0.20906925078777333 0.036180954
#IS# IS 0.05480000171200000 0.11520000092900000 0.11440000079800000 0.117100000168000000 0.09650000108300000 0.218500000

Aqui una solución utilizando monthlyReturn e (s)lapply

library(quantmod)
portfolio_monthly_returns=lapply(xts(df[,-1],order.by = df$Date),monthlyReturn)

portfolio_excess_returns <- lapply(portfolio_monthly_returns,Return.excess, 
                                          Rf = .0003)
sharpe_ratio_manual <- function(portfolio_excess_returns){
  md=mean(portfolio_excess_returns)
  sd=StdDev(portfolio_excess_returns)
  is=round(
  mean(portfolio_excess_returns) / StdDev(portfolio_excess_returns), 4
  )
  c(MD=md,SD=sd,IS=is)}
sapply(portfolio_excess_returns,sharpe_ratio_manual)

O resultado será similar a:

#> sapply(portfolio_excess_returns,sharpe_ratio_manual)
#       Asset1     Asset2     Asset3     Asset4     Asset5
#MD 0.00512297 0.03089434 0.02469234 0.02832768 0.02017995
#SD 0.09343994 0.26828429 0.21592801 0.24191859 0.20906925
#IS 0.05480000 0.11520000 0.11440000 0.11710000 0.09650000

Aqui una solución utilizando monthlyReturn e (s)lapply

library(quantmod)
portfolio_monthly_returns=lapply(xts(df[,-1],order.by = df$Date),monthlyReturn)

portfolio_excess_returns <- lapply(portfolio_monthly_returns,Return.excess, 
                                          Rf = .0003)
sharpe_ratio_manual <- function(portfolio_excess_returns){
  md=mean(portfolio_excess_returns)
  sd=StdDev(portfolio_excess_returns)
  is=round(
  mean(portfolio_excess_returns) / StdDev(portfolio_excess_returns), 4
  )
  c(MD=md,SD=sd,IS=is)}
sapply(portfolio_excess_returns,sharpe_ratio_manual)

O resultado será similar a:

#> sapply(portfolio_excess_returns,sharpe_ratio_manual)
#         .SXQR       .SXTR       .SXNR        .SXMR       .SXAR       .SX3R
# MD 0.007662462 0.004811897 0.004427923 0.0009964127 0.008533315 0.007904365
# SD 0.044747675 0.051776959 0.055490708 0.0594352491 0.078777333 0.036180954
# IS 0.171200000 0.092900000 0.079800000 0.0168000000 0.108300000 0.218500000
Origen Enlace
Robert
  • 176
  • 7

Aqui una solución utilizando monthlyReturn e (s)lapply

library(quantmod)
portfolio_monthly_returns=lapply(xts(df[,-1],order.by = df$Date),monthlyReturn)

portfolio_excess_returns <- lapply(portfolio_monthly_returns,Return.excess, 
                                          Rf = .0003)
sharpe_ratio_manual <- function(portfolio_excess_returns){
  md=mean(portfolio_excess_returns)
  sd=StdDev(portfolio_excess_returns)
  is=round(
  mean(portfolio_excess_returns) / StdDev(portfolio_excess_returns), 4
  )
  c(MD=md,SD=sd,IS=is)}
sapply(portfolio_excess_returns,sharpe_ratio_manual)

O resultado será similar a:

#> sapply(portfolio_excess_returns,sharpe_ratio_manual)
#       Asset1     Asset2     Asset3     Asset4     Asset5
#MD 0.00512297 0.03089434 0.02469234 0.02832768 0.02017995
#SD 0.09343994 0.26828429 0.21592801 0.24191859 0.20906925
#IS 0.05480000 0.11520000 0.11440000 0.11710000 0.09650000