下面是来自小插曲的相关代码,稍微修改以使其适合页面,并使其易于再现。可视化代码被忽略。评论来自vignette作者。为什么模拟股票收益率在R中的pbo(回测过拟合概率)包中的“pbo”小插图中重新缩放并重新居中?
(全小插曲: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循环转化数据,并执行这种重新的缩放和重新定位需要用真实的回报来完成?或者,这只是作者为了让他的模拟回报更像真实的东西而做的事情?
谷歌搜索和搜索通过计算器翻转了一些文章和关于缩放到时间的平方根的波动,但这看起来不像我见过的。通常它们涉及将短期(即每日)波动性度量乘以时间的根源,但这不完全是这样。此外,该软件包的文档不包含这部分重新缩放和重新对中代码。文档:https://cran.r-project.org/web/packages/pbo/pbo.pdf
所以:
为什么以这种方式改变了数据/这是什么 转型的结果呢?
难道只需要这个模拟数据,还是我需要
同样改变真实的回报?
我通过电子邮件发送软件包的作者,他确认这个解释是准确的 –