如何从R中的每日收益计算历史月度波动率?

如何从R中的每日收益计算历史月度波动率?,r,time-series,xts,standard-deviation,volatility,R,Time Series,Xts,Standard Deviation,Volatility,首先,我创建了一个xts对象,它包含36个时间序列,显示从1980-01-02到2020-10-06的每日价格 ENERGY_data$time <- as.Date(ENERGY_data$time, format("%Y/%m/%d")) ENERGY_xts <- ENERGY_data[order(ENERGY_data$time), ] ENERGY_xts <- as.xts(ENERGY_xts[, 2:37], order.by=ENERGY

首先,我创建了一个xts对象,它包含36个时间序列,显示从1980-01-02到2020-10-06的每日价格

ENERGY_data$time <- as.Date(ENERGY_data$time, format("%Y/%m/%d"))
ENERGY_xts <- ENERGY_data[order(ENERGY_data$time), ]
ENERGY_xts <- as.xts(ENERGY_xts[, 2:37], order.by=ENERGY_xts$time)

ENERGY\u data$time看看这个函数,请注意我模拟了返回,因为您没有提供您的返回

library(xts)

set.seed(123)
returns <- matrix(rnorm(30*365*5, 0.0001, 0.0002), ncol = 30)
timeindex <- seq.Date(from = as.Date('2000-01-01'), length.out = 365*5, by = 'days')

test_xts <- xts(returns, order.by = timeindex)

calcFrenchVolOneAsset <- function(x){
  ndays <- nrow(x)
  first_part_of_formula <- sum(x^2)
  second_part_of_formula <- 2*sum(x[-1]*x[-nrow(x)])
  res <- sqrt(first_part_of_formula + second_part_of_formula)
  return(res)
}

calcFrenchVolMultipleAssets <- function(x){
  ndays <- nrow(x)
  first_part_of_formula <- colSums(x^2)
  second_part_of_formula <- 2*colSums(x[-1, ]*x[-nrow(x), ])
  res <- sqrt(first_part_of_formula + second_part_of_formula)
  return(res)
}

# test for the first month and the first asset
calcFrenchVolOneAsset(test_xts['2000-01', 1])
calcFrenchVolMultipleAssets(test_xts['2000-01', 1])

# apply monthly and on columns
monthly_vols <- apply.monthly(test_xts, calcFrenchVolMultipleAssets)
head(monthly_vols[, c(1:5)])
                    e1        e1.1        e1.2        e1.3        e1.4
2000-01-31 0.002030192 0.002402946 0.001717494 0.001888513 0.002322648
2000-02-29 0.001983995 0.002343783 0.001789346 0.001671332 0.001824278
2000-03-31 0.001910535 0.002429689 0.001709092 0.002492223 0.002068032
2000-04-30 0.001765052 0.002114554 0.001946232 0.002160436 0.002139949
2000-05-31 0.002269842 0.002476424 0.001626455 0.002030027 0.002400690
2000-06-30 0.002082933 0.001905620 0.001681579 0.001992082 0.002010535
库(xts)
种子集(123)
返回
library(xts)

set.seed(123)
returns <- matrix(rnorm(30*365*5, 0.0001, 0.0002), ncol = 30)
timeindex <- seq.Date(from = as.Date('2000-01-01'), length.out = 365*5, by = 'days')

test_xts <- xts(returns, order.by = timeindex)

calcFrenchVolOneAsset <- function(x){
  ndays <- nrow(x)
  first_part_of_formula <- sum(x^2)
  second_part_of_formula <- 2*sum(x[-1]*x[-nrow(x)])
  res <- sqrt(first_part_of_formula + second_part_of_formula)
  return(res)
}

calcFrenchVolMultipleAssets <- function(x){
  ndays <- nrow(x)
  first_part_of_formula <- colSums(x^2)
  second_part_of_formula <- 2*colSums(x[-1, ]*x[-nrow(x), ])
  res <- sqrt(first_part_of_formula + second_part_of_formula)
  return(res)
}

# test for the first month and the first asset
calcFrenchVolOneAsset(test_xts['2000-01', 1])
calcFrenchVolMultipleAssets(test_xts['2000-01', 1])

# apply monthly and on columns
monthly_vols <- apply.monthly(test_xts, calcFrenchVolMultipleAssets)
head(monthly_vols[, c(1:5)])
                    e1        e1.1        e1.2        e1.3        e1.4
2000-01-31 0.002030192 0.002402946 0.001717494 0.001888513 0.002322648
2000-02-29 0.001983995 0.002343783 0.001789346 0.001671332 0.001824278
2000-03-31 0.001910535 0.002429689 0.001709092 0.002492223 0.002068032
2000-04-30 0.001765052 0.002114554 0.001946232 0.002160436 0.002139949
2000-05-31 0.002269842 0.002476424 0.001626455 0.002030027 0.002400690
2000-06-30 0.002082933 0.001905620 0.001681579 0.001992082 0.002010535