if(STATUS!= 0){:参数的长度为零时出错

时间:2017-11-01 14:13:27

标签: r

我想获取一组资产的优化分配,因此我使用了包fPortfolioBLCOP。这是我的代码,

library(xts);library(fPortfolio);library(BLCOP)
sh_return <- xts(ret[,-1],order.by = as.Date(ret[,1]))
prior_mean <- colMeans(sh_return)
prior_mean
prior_cov_matrix <- cov(sh_return)

## onstruct the confidenec interval
pick_matrix <- matrix(0,2,ncol(sh_return))
colnames(pick_matrix) <- colnames(sh_return)
pick_matrix[1,1:4] <- 1
pick_matrix[2,c(1,2,5,ncol(sh_return)-1)] <- c(0.5,0.6,-1,0.8)
pick_matrix
# views
q <- c(0.4,0.32)
confidence <- c(90,95)
views <- BLViews(pick_matrix,q,confidence,assetNames = colnames(sh_return))
views
# posterior
tau <- 0.3
posterior <- posteriorEst(views,tau=tau,prior_mean,prior_cov_matrix)

# optimazation
optimal_portfolio <- optimalPortfolios.fPort(posterior,inputData = NULL,spec = NULL,constraints = "LongOnly",optimizer = "tangencyPortfolio",numSimulations  = 100)

错误结果是:

Error in if (STATUS != 0) { : argument is of length zero

问题是,当我检查源代码时,对我来说似乎没问题,并且没有这样的STATUS存在。因此,我不知道这个代码怎么会出错。任何想法都可能有所帮助。

或者如果你想测试数据,这里是包的源文件中的代码,存在同样的错误:

entries <- c(0.001005,0.001328,-0.000579,-0.000675,0.000121,0.000128,
             -0.000445, -0.000437, 0.001328,0.007277,-0.001307,-0.000610,
             -0.002237,-0.000989,0.001442,-0.001535, -0.000579,-0.001307,
             0.059852,0.027588,0.063497,0.023036,0.032967,0.048039,-0.000675,
             -0.000610,0.027588,0.029609,0.026572,0.021465,0.020697,0.029854,
             0.000121,-0.002237,0.063497,0.026572,0.102488,0.042744,0.039943,
             0.065994 ,0.000128,-0.000989,0.023036,0.021465,0.042744,0.032056,
             0.019881,0.032235 ,-0.000445,0.001442,0.032967,0.020697,0.039943,
             0.019881,0.028355,0.035064 ,-0.000437,-0.001535,0.048039,0.029854,
             0.065994,0.032235,0.035064,0.079958 )

varcov <- matrix(entries, ncol = 8, nrow = 8)
mu <- c(0.08, 0.67,6.41, 4.08, 7.43, 3.70, 4.80, 6.60) / 100
pick <- matrix(0, ncol = 8, nrow = 3, dimnames = list(NULL, letters[1:8]))
pick[1,7] <- 1
pick[2,1] <- -1; pick[2,2] <- 1
pick[3, 3:6] <- c(0.9, -0.9, .1, -.1)
confidences <- 1 / c(0.00709, 0.000141, 0.000866)
views <- BLViews(pick, c(0.0525, 0.0025, 0.02), confidences, letters[1:8])
posterior <- posteriorEst(views, tau = 0.025, mu, varcov )

optimalPortfolios.fPort(posterior, optimizer = "tangencyPortfolio")

0 个答案:

没有答案