我有代码,当我不把它太多的约束工作.然而,我想运行大量的约束.无论如何,有帮助解决问题,或围绕这个错误工作.我试图绘制一个有效的边界.谢谢.我有dput下面的数据时间序列.
library("fPortfolio")
library('fPortfolio')
data = SPISECTOR.RET
asset = dim(data)[2]
# I'm putting a lot of constraints here. I want to do this.
# If I take away some of the constraints, the formula works.
# But I don't want to take away the constraints.
constraints <- c('minW[1:asset]=0','maxW[1:asset]=0.3', 'minsumW[c("ACWI", "ACWX", "AGED", "CEMA", "CEMG", "CMXC", "CNYA", "COMF", "CSJP", "CSKR", "CSRU", "CSUK", "DGTL", "DLTM", "ECAR", "EEM", "EFA", "EIDO", "EIRL", "EMB", "ENOR", "EPHE", "EPOL", "EUSA", "GUNR", "HEAL", "ICDU", "ICSU", "IESU", "IGAA", "IH2O", "IHCU", "IHYA", "IIND", "IISU", "IITU", "IJS", "IJT", "IMSU", "INRG", "IQLT", "IUSU", "KWEB", "LAND", "LOCK", "MGK", "OPEN", "QQQ", "RBOT", "SAUS", "SPAG", "UIFS", "UNG", "USMV", "VB", "VNQ", "VNQI", "VTV", "VUG", "WOOD", "XFVT")]=0.01', 'maxsumW[c("ACWI", "ACWX", "AGED", "CEMA", "CEMG", "CMXC", "CNYA", "COMF", "CSJP", "CSKR", "CSRU", "CSUK", "DGTL", "DLTM", "ECAR", "EEM", "EFA", "EIDO", "EIRL", "EMB", "ENOR", "EPHE", "EPOL", "EUSA", "GUNR", "HEAL", "ICDU", "ICSU", "IESU", "IGAA", "IH2O", "IHCU", "IHYA", "IIND", "IISU", "IITU", "IJS", "IJT", "IMSU", "INRG", "IQLT", "IUSU", "KWEB", "LAND", "LOCK", "MGK", "OPEN", "QQQ", "RBOT", "SAUS", "SPAG", "UIFS", "UNG", "USMV", "VB", "VNQ", "VNQI", "VTV", "VUG", "WOOD", "XFVT")]=0.3', 'minsumW[c("BND", "CBON", "CRPA", "GLD", "SLV", "SPGP", "SWAN")]=0.01', 'maxsumW[c("BND", "CBON", "CRPA", "GLD", "SLV", "SPGP", "SWAN")]=0.3', 'minsumW[c("GLTL", "GLTS", "IBGE", "IBGY", "IBGZ", "IEF", "IGLT", "SEGA", "SHY", "TIP", "TLT", "VVUILG")]=0.01', 'maxsumW[c("GLTL", "GLTS", "IBGE", "IBGY", "IBGZ", "IEF", "IGLT", "SEGA", "SHY", "TIP", "TLT", "VVUILG")]=0.3')
spec <- portfolioSpec()
setNFrontierPoints(spec) <- 25
setSolver(spec)<- "solveRquadprog"
frontier <-portfolioFrontier(data, spec, constraints)
字符串
当我运行最后一行frontier <-portfolioFrontier(data, spec, constraints)
时,我得到错误:
Error in `colnames<-`(`*tmp*`, value = names(getMu(Data))) :
attempt to set 'colnames' on an object with less than two dimensions
型
2条答案
按热度按时间iyfamqjs1#
好的,我让它工作了。我没有把约束加到一个,我放进了其他的库。现在一切都好了。
eh57zj3b2#
导致我在生成两个资产行时出现相同错误的原因是由于
portfolioFrontier()
中的编码不小心,如果一对中的任何资产具有负平均回报,则由于双浮点精度问题,无法识别有效投资组合解决方案。为了找到内部解决方案,portfolioFrontier()
的内部编码引入了公差项.Machine$double.eps
,旨在仅在每个资产的平均回报形成的限制内调整目标回报。但函数定义脚本规定调整因子的方式仅在两种资产都具有正平均回报时缩小解决方案边界。但是,如果其中任何一种资产具有负回报,调整因子将目标回报扩展到每个资产的平均回报之外,使问题无法解决。这导致返回空的权重矩阵NULL
,从而导致在从资产数据分配列名时出错。解决方案是在函数的脚本中更改两行,如下所示,使调整因子取决于资产回报的符号。
字符串
在控制台中创建此函数对象,然后使用is替换{fPortfolio}包中的原始
portfolioFrontier()
。