Tengo la tasa de rendimiento anual y me gustaría obtener la volatilidad anualizada.
> head(yearly_return, 5)
.SXQR .SXTR .SXNR .SXMR .SXAR .SX3R
[1,] -0.04211651 -0.01692493 -0.13407901 -0.11054265 -0.13658011 0.27697419
[2,] -0.06170831 -0.06020640 -0.27029244 -0.24498356 0.03086915 -0.01268035
[3,] -0.13181676 -0.31916713 -0.37079853 -0.45051590 -0.28119776 -0.11107462
[4,] 0.12010929 0.23596541 0.19548718 0.09116377 0.18582579 -0.02995400
[5,] 0.09574009 0.07114804 0.09357744 0.07972675 0.02328106 0.06988229
.SX6R .SXFR .SXOR .SXDR .SX4R .SXRR
[1,] 0.10468134 0.1599970 -0.04359011 0.26370803 0.05147042 -0.225365403
[2,] -0.07140654 -0.1555470 0.01994745 -0.07791252 -0.13987785 -0.009904841
[3,] -0.23011230 -0.3433740 -0.28950626 -0.28954601 -0.21697798 -0.291441728
[4,] 0.10787296 0.1305620 0.24454257 0.08783802 0.04559812 0.088919239
[5,] 0.28179398 0.2397919 0.24245656 0.02901212 0.15874338 0.125110885
.SXER .SXKR .SX7R .SX8R .SXIR .SXPR
[1,] 0.043774723 -0.3615836 0.1283161 -0.17028728 0.21292564 -0.1806196
[2,] -0.009732258 -0.2833454 -0.0602872 -0.39441865 -0.28494845 0.1871347
[3,] -0.150425114 -0.3655823 -0.2367712 -0.55863189 -0.49789995 -0.1753154
[4,] 0.022473035 0.1512421 0.2105499 0.26432695 0.07890223 0.1914243
[5,] 0.138461912 0.1211539 0.1215245 -0.02500651 0.09043759 0.1280607
Los precios diarios provienen de este archivo y este código :
df <- read.xlsx("Data.xlsx", sheet = "Sector-STOXX600", startRow = 2,colNames = TRUE, detectDates = TRUE, skipEmptyRows = FALSE)
df[2:19] <- data.matrix(df[2:19])
Creo que casi lo hice con:
volatility_function <- function(x)sqrt(252) * sd(diff(log(x))) * 100
annualized_volatility <- df %>%
group_by(gr = floor_date(Date, unit = "year"))%>%
summarize_at(vars(-Date, -gr), volatility_function) %>%
ungroup() %>%
select(-gr) %>%
as.matrix()
head(annualized_volatility, 5)
Sin embargo, hay muchos valores faltantes en la respuesta:
> head(annualized_volatility, 5)
.SXQR .SXTR .SXNR .SXMR .SXAR .SX3R .SX6R .SXFR
[1,] 22.02142 20.43130 17.13465 40.97723 18.29027 18.04644 14.48501 16.99419
[2,] 25.76975 26.87473 NA NA NA NA NA NA
[3,] 25.25178 28.91409 24.11453 36.82997 37.22605 20.28364 22.79909 31.74122
[4,] 19.99782 23.64921 NA NA NA NA NA NA
[5,] 12.40295 14.65185 NA NA NA NA NA NA
.SXOR .SXDR .SX4R .SXRR .SXER .SXKR .SX7R .SX8R
[1,] 16.30835 19.55287 18.35675 16.96180 26.01240 42.23152 18.29375 49.62865
[2,] NA NA NA 16.81732 NA NA NA NA
[3,] 23.35008 26.94605 27.67144 25.93838 31.77843 41.12819 32.13011 52.25582
[4,] NA NA NA 20.59843 NA NA NA NA
[5,] NA NA NA 12.10395 NA NA NA NA
.SXIR .SXPR
[1,] 17.17219 23.48967
[2,] NA NA
[3,] 45.71421 28.83333
[4,] NA NA
[5,] NA NA
Mi intento
Me deshago de las filas con valores faltantes.
volatility_function <- function(x)sqrt(252) * sd(diff(log(x))) * 100
annualized_volatility <- df[complete.cases(df), ] %>% # Para deshacerse de las filas con valores faltantes.
group_by(gr = floor_date(Date, unit = "year"))%>%
summarize_at(vars(-Date, -gr), volatility_function) %>%
ungroup() %>%
select(-gr) %>%
as.matrix()
head(annualized_volatility, 5)
Y me devuelve:
> head(annualized_volatility)
.SXQR .SXTR .SXNR .SXMR .SXAR .SX3R .SX6R .SXFR
[1,] 22.02142 20.43130 17.13465 40.97723 18.29027 18.046439 14.48501 16.99419
[2,] 25.83552 26.96622 25.09452 33.39206 30.85724 17.746178 16.42671 27.80582
[3,] 25.25178 28.91409 24.11453 36.82997 37.22605 20.283639 22.79909 31.74122
[4,] 20.07738 23.74381 18.83412 27.63602 30.18198 17.968572 18.78502 19.42008
[5,] 12.40780 14.68115 13.51682 16.64709 17.37418 10.152944 10.50091 11.00480
[6,] 10.38680 11.28891 10.63116 10.78472 14.39485 9.100692 11.62565 10.53404
.SXOR .SXDR .SX4R .SXRR .SXER .SXKR .SX7R .SX8R
[1,] 16.30835 19.55287 18.35675 16.961803 26.01240 42.23152 18.293753 49.62865
[2,] 18.38574 21.79742 22.60158 16.857792 28.00532 38.37951 27.417289 57.48731
[3,] 23.35008 26.94605 27.67144 25.938379 31.77843 41.12819 32.130105 52.25582
[4,] 20.16187 21.80619 26.31914 20.631465 22.91296 22.86394 22.374202 34.38963
[5,] 11.83515 12.52007 13.50968 12.125941 14.67853 14.84850 12.219546 26.21946
[6,] 10.96996 10.67187 12.16943 9.789476 16.24038 11.90261 9.993306 15.67066
.SXIR .SXPR
[1,] 17.17219 23.48967
[2,] 29.55573 25.81690
[3,] 45.71421 28.83333
[4,] 37.07314 24.29006
[5,] 16.75831 19.40763
[6,] 11.85841 18.42887
head(df[complete.cases(df), ])
que te lista eldata.frame
original. No debiera serhead(annualized_volatility, 5)
?