Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 朴素投资组合选择规则_R_Performanceanalytics_Back Testing_R Portfolioanalytics_Tidyquant - Fatal编程技术网

R 朴素投资组合选择规则

R 朴素投资组合选择规则,r,performanceanalytics,back-testing,r-portfolioanalytics,tidyquant,R,Performanceanalytics,Back Testing,R Portfolioanalytics,Tidyquant,我有一个xts文件,里面有17个行业投资组合的月度回报。数据如下: Cars Chems Clths Cnstr Cnsum Durbl FabPr Finan Food Machn Mines Oil Other Rtail Steel Trans 1926-07-31 4.77 1.20 -0.09 4.20 2.05 1.33 0.61 0.44 0.46 2.06 2.65 -2.57 1.99 1.46 3.05 -0.69

我有一个xts文件,里面有17个行业投资组合的月度回报。数据如下:

             Cars Chems Clths Cnstr Cnsum Durbl FabPr Finan  Food Machn Mines   Oil Other Rtail Steel Trans
1926-07-31   4.77  1.20 -0.09  4.20  2.05  1.33  0.61  0.44  0.46  2.06  2.65 -2.57  1.99  1.46  3.05 -0.69
1926-08-31  -1.11  3.55  3.57  0.85  4.10  0.75 -0.49  8.84  4.72  5.57  1.16  3.85  4.81  0.63 -0.58  4.96
1926-09-30  -3.39  1.85 -4.89 -1.06  2.50  1.27 -3.10 -2.55  1.66  0.52  1.44 -4.93 -2.09 -1.20  2.28  0.06
1926-10-31 -10.66 -9.15  0.49 -6.49 -1.41 -5.02 -3.92 -4.40 -4.79 -4.52  5.73  0.23 -3.50 -2.44 -4.98 -2.79
1926-11-30  -0.73  4.98  2.66  2.91  8.35  0.12  1.36 -0.27  7.04 -0.75  1.13  2.92 -0.47  1.72  1.81  1.38
1926-12-31   5.14  2.59  2.30  3.37  1.96  4.23  2.22  2.40 -1.39  2.93 -1.38  6.39  2.59  3.06  2.17  2.18
           Utils
1926-07-31  4.85
1926-08-31 -2.00
1926-09-30  2.06
1926-10-31 -2.98
1926-11-30  5.71
1926-12-31  1.72
我的目标是使用简单的投资组合选择规则执行回溯测试。我不想持有相等权重的投资组合,而是想根据以下简单规则分配权重:

  • 将权重2/N分配给历史回报高于中值的每项资产
  • 如果低于历史回报中值,则指定权重0
而不是等权重向量:

w <- c(rep(1/17,17))

w如果一个替代软件包也可以接受:下面是一个如何使用它的草图,我维护它。我从一个示例数据集开始:Kenneth French网站上的17个行业投资组合(可能与您使用的数据集相同)

此函数传递给
btest
,并指示每季度调用一次

bt <- btest(prices = list(as.matrix(P)),
            timestamp = as.Date(row.names(P)),
            signal = above_median,
            do.signal = "lastofquarter",
            b = 250, ## burnin
            initial.cash = 100,
            convert.weights = TRUE)

更新,在注释之后:
btest
不会对数据频率施加限制。以下是月度数据示例,从月度回报开始

P <- French(tempdir(),
            "17_Industry_Portfolios_CSV.zip",
            price.series = FALSE)
head(P)  ## returns
##               Food   Mines     Oil  Clths   Durbl   Chems  Cnsum   Cnstr
## 1926-07-31  0.0048  0.0378 -0.0141 0.0602 -0.0162  0.0846 0.0142  0.0231
## 1926-08-31  0.0291  0.0069  0.0360 0.0015 -0.0196  0.0570 0.0584  0.0433
## ....

在堆栈溢出中,您的问题需要更加面向代码。你可以解释问题的上下文,但如果可能的话,尽量让不一定理解下划线主题的人仍然可以回答问题中与代码相关的部分。另外,发布您的数据(使用
dput()
函数),以便我们可以使用运行代码。非常感谢@Enrico Schumann!我刚刚读完了你的书(R投资组合管理)和你关于回溯测试的文章。出现了一些额外的问题:1)是否可以在PMwR包中仅使用退货(无价格)?2) 我尝试了上面的函数,将中值计算的天数以及bt函数中的老化率更改为12(1年,我使用月度回报)。我得到的信息是:如果(max(abs(dXs))cumprod(1+R)
创建可以用作输入的系列。2)
b测试
不关心频率;月度数据也应该有效。但您的错误表明您的数据中有
NA
s。(在您的数据集上尝试
any(is.na())
)非常感谢您的回答和更新上述代码。我会再试一次!非常感谢你!
above_median <- function() {
    ## get the most recent 250 days
    H <- Close(n = 250)

    ## compute total return of industries
    R <- H[nrow(H), ] / H [1L, ]

    ## include only those with an above-median return
    include <- R > median(R)
    w <- numeric(ncol(H))
    w[include] <- 1/sum(include)
    w
}
bt <- btest(prices = list(as.matrix(P)),
            timestamp = as.Date(row.names(P)),
            signal = above_median,
            do.signal = "lastofquarter",
            b = 250, ## burnin
            initial.cash = 100,
            convert.weights = TRUE)
summary(NAVseries(bt))
journal(bt)
P <- French(tempdir(),
            "17_Industry_Portfolios_CSV.zip",
            price.series = FALSE)
head(P)  ## returns
##               Food   Mines     Oil  Clths   Durbl   Chems  Cnsum   Cnstr
## 1926-07-31  0.0048  0.0378 -0.0141 0.0602 -0.0162  0.0846 0.0142  0.0231
## 1926-08-31  0.0291  0.0069  0.0360 0.0015 -0.0196  0.0570 0.0584  0.0433
## ....
P <- apply(P + 1, 2, cumprod)
head(P)  ## returns => 'prices'
##                Food    Mines       Oil    Clths     Durbl    Chems    Cnsum
## 1926-07-31 1.004800 1.037800 0.9859000 1.060200 0.9838000 1.084600 1.014200
## 1926-08-31 1.034040 1.044961 1.0213924 1.061790 0.9645175 1.146422 1.073429
above_median <- function() {
    ## get the most recent 12 months
    H <- Close(n = 12)

    ## compute total return of industries
    R <- H[nrow(H), ] / H [1L, ]

    ## include only those with an above-median return
    include <- R > median(R)
    w <- numeric(ncol(H))
    w[include] <- 1/sum(include)
    w
}
bt <- btest(prices = list(as.matrix(P)),
            timestamp = as.Date(row.names(P)),
            signal = above_median,
            do.signal = "lastofquarter",
            b = 12, ## burnin
            initial.cash = 100,
            convert.weights = TRUE)

unique(journal(bt)$timestamp)  ## timestamps of trades 
## [1] "1927-09-30" "1927-12-31" "1928-03-31" "1928-06-30" "1928-09-30"
## [6] "1928-12-31" "1929-03-31" "1929-06-30" "1929-09-30" "1929-12-31"
## ....