下面是來自小插曲的相關代碼,稍微修改以使其適合頁面,並使其易於再現。可視化代碼被忽略。評論來自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
所以:
爲什麼以這種方式改變了數據/這是什麼 轉型的結果呢?
難道只需要這個模擬數據,還是我需要
同樣改變真實的回報?
我通過電子郵件發送軟件包的作者,他確認這個解釋是準確的 –