从博客运行准确的代码时获取错误

时间:2019-07-02 18:10:48

标签: r

我正在关注教程here。几天前,我能够无错误地运行此代码,并在自己的数据集上运行它(获取此错误总是有点失败)-但是现在,我尝试运行该代码,并且始终获得相同的代码错误。

  

solve.QP(Dmat,dvec,Amat,bvec = b0,meq = 2)错误:
  约束不一致,没有解决办法!

我知道solver不能求解方程,但是我对为什么它以前可以工作却现在却不起作用感到有些困惑。 >

library(tseries)
library(data.table)
link <- "https://raw.githubusercontent.com/DavZim/Efficient_Frontier/master/data/mult_assets.csv"

df <- data.table(read.csv(link))

df_table <- melt(df)[, .(er = mean(value),
                         sd = sd(value)), by = variable]

er_vals <- seq(from = min(df_table$er), to = max(df_table$er), length.out = 1000)

# find an optimal portfolio for each possible possible expected return 
# (note that the values are explicitly set between the minimum and maximum of the expected returns per asset)
sd_vals <- sapply(er_vals, function(er) {
  op <- portfolio.optim(as.matrix(df), er)
  return(op$ps)
})

SessionInfo:

R version 3.5.3 (2019-03-11)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=Spanish_Spain.1252  LC_CTYPE=Spanish_Spain.1252    LC_MONETARY=Spanish_Spain.1252
[4] LC_NUMERIC=C                   LC_TIME=Spanish_Spain.1252    

attached base packages:
[1] parallel  stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] lpSolve_5.6.13.1  data.table_1.12.0 tseries_0.10-46   rugarch_1.4-0    

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.0                  MASS_7.3-51.1               mclust_5.4.2               
 [4] lattice_0.20-38             quadprog_1.5-5              Rsolnp_1.16                
 [7] TTR_0.23-4                  tools_3.5.3                 xts_0.11-2                 
[10] SkewHyperbolic_0.4-0        GeneralizedHyperbolic_0.8-4 quantmod_0.4-13.1          
[13] spd_2.0-1                   grid_3.5.3                  KernSmooth_2.23-15         
[16] yaml_2.2.0                  numDeriv_2016.8-1           Matrix_1.2-15              
[19] nloptr_1.2.1                DistributionUtils_0.6-0     ks_1.11.3                  
[22] curl_3.3                    compiler_3.5.3              expm_0.999-3               
[25] truncnorm_1.0-8             mvtnorm_1.0-8               zoo_1.8-4 

0 个答案:

没有答案
相关问题