这是小插图中的相关代码,稍作改动以使其适合此处的页面,并使其易于复制。省略了可视化代码。评论来自小插图作者。
(完整的小插图:https ://cran.r-project.org/web/packages/pbo/vignettes/pbo.html )
library(pbo)
#First, we assemble the trials into an NxT matrix where each column
#represents a trial and each trial has the same length T. This example
#is random data so the backtest should be overfit.`
set.seed(765)
n <- 100
t <- 2400
m <- data.frame(matrix(rnorm(n*t),nrow=t,ncol=n,
dimnames=list(1:t,1:n)), check.names=FALSE)
sr_base <- 0
mu_base <- sr_base/(252.0)
sigma_base <- 1.00/(252.0)**0.5
for ( i in 1:n ) {
m[,i] = m[,i] * sigma_base / sd(m[,i]) # re-scale
m[,i] = m[,i] + mu_base - mean(m[,i]) # re-center
}
#We can use any performance evaluation function that can work with the
#reassembled sub-matrices during the cross validation iterations.
#Following the original paper we can use the Sharpe ratio as
sharpe <- function(x,rf=0.03/252) {
sr <- apply(x,2,function(col) {
er = col - rf
return(mean(er)/sd(er))
})
return(sr)
}
#Now that we have the trials matrix we can pass it to the pbo function
#for analysis.
my_pbo <- pbo(m,s=8,f=sharpe,threshold=0)
summary(my_pbo)
这是我很好奇的部分:
sr_base <- 0
mu_base <- sr_base/(252.0)
sigma_base <- 1.00/(252.0)**0.5
for ( i in 1:n ) {
m[,i] = m[,i] * sigma_base / sd(m[,i]) # re-scale
m[,i] = m[,i] + mu_base - mean(m[,i]) # re-center
}
为什么要在 for 循环中转换数据,这种重新缩放和重新居中是否需要用真实的回报来完成?或者这只是作者为了让他的模拟回报看起来更像真实的东西而做的事情?
谷歌搜索和通过 stackoverflow 搜索发现了一些关于将波动率缩放到时间的平方根的文章和帖子,但这看起来与我所看到的不太一样。通常它们涉及将一些短期(即每日)波动性度量乘以时间的根,但这并不完全是这样。此外,该包的文档不包括这块重新缩放和重新居中代码。文档:https ://cran.r-project.org/web/packages/pbo/pbo.pdf
所以:
为什么以这种方式转换数据/这种转换的结果是什么?
是否只需要这个模拟数据,还是我需要
类似地转换实际回报?