我有一个加权股票组合和他们的每日回报历史。假设权重不移动,我试图计算投资组合在每个历史日的回报,并用零替换缺失数据(我试图计算历史 VaR ,或< em>风险价值)。
这是一个简化版本:
# portfolio
pfolio = data.frame(ticker = c("stock_a", "stock_b", "stock_noob"), weight = c(0.25, 0.6, 0.15))
# Daily stock returns (with some NA values for one stock):
m = matrix(c(0.0016, 0.0037, -0.0042, -0.0096, -0.0006, -0.0043, -0.0292, -0.0158, 0.0128, 0.0113, 0.0016, 0.0042, NA, NA, 0.0168, -0.0293, 0.0037, -0.0083),
nrow = 6,
ncol = 3,
dimnames = list(c("2017-03-01", "2017-03-02", "2017-03-03", "2017-03-06", "2017-03-07", "2017-03-08"), c("stock_a", "stock_b", "stock_noob"))
)
我正在尝试使用聪明的apply
或mapply
方法,但我能想到的最好的方法是首先清理数据,然后应用for循环(yuck):
m_clean = apply(m, c(1, 2), function(x) if (is.na(x)) 0 else x)
answer = numeric(0)
for (i in 1:nrow(m_clean)) {
answer = c(answer, sum(m_clean[i, pfolio$ticker] * pfolio$weight))
}
所以主要的问题是:什么是干净的,单行的方式呢?
答案 0 :(得分:3)
你可以这样做:
m_clean <- ifelse(is.na(m), 0, m) # or
m_clean <- m; m_clean[is.na(m_clean)] <- 0
然后
answer <- apply(m_clean, 1, weighted.mean, w=pfolio$weight) # or
answer <- colSums(t(m_clean) * pfolio$weight) # or
answer <- tcrossprod(pfolio$weight, m_clean)
最后一个是给出一个矩阵:
#> tcrossprod(pfolio$weight, m_clean)
# 2017-03-01 2017-03-02 2017-03-03 2017-03-06 2017-03-07 2017-03-08
#[1,] -0.01712 -0.008555 0.00915 -1.5e-05 0.001365 2e-04
其他解决方案正在给出一个命名向量。
library("microbenchmark")
microbenchmark(
a= apply(m_clean, 1, weighted.mean, w=pfolio$weight),
c= colSums(t(m_clean) * pfolio$weight),
p= tcrossprod(pfolio$weight, m_clean),
m= m_clean %*% pfolio$weight
)
# Unit: microseconds
# expr min lq mean median uq max neval cld
# a 49.115 51.0590 54.46379 52.3685 53.9815 99.023 100 c
# c 12.688 13.8385 15.02912 14.8460 15.7560 32.366 100 b
# p 5.978 6.8955 7.75998 7.4170 7.8770 30.771 100 a
# m 5.438 6.4330 6.95056 6.8615 7.2710 17.109 100 a