r - 运行博客中的确切代码时出现错误
问题描述
我在这里学习教程。几天前,我能够毫无错误地运行此代码并在我自己的数据集上运行它(获得此错误总是有点命中注定) - 但是现在我尝试运行代码并且我总是得到相同的错误。
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)
})
会话信息:
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
解决方案
tseries::portfolio.optim
默认情况下不允许卖空,请参阅参数short
。如果short = FALSE
资产权重不能低于 0。并且由于权重总和必须为 1,那么单个资产的权重也不能高于 1。没有杠杆。
(可能,在早期版本的 tseries 中,默认值可能是short = TRUE
. 这可以解释为什么它以前对你有用。)
您的目标回报 ( pm
) 不能超过任何输入资产的最高回报。
解决方案 1:允许卖空,但请记住,这是一个不同的有效边界。(作为参考,请参阅任何讨论马科维茨优化的讲座或书籍。这个问题有一个数学解决方案,没有卖空限制。)
op <- portfolio.optim(as.matrix(df), er, shorts = T)
解决方案 2:将目标回报限制在最差和最佳资产回报之间。
er_vals <- seq(from = min(colMeans(df)), to = max(colMeans(df)), length.out = 1000)
这是获得的有效边界的图。
这是提供两种解决方案的完整脚本。
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)
er_vals1 <- seq(from = 0, to = 0.15, length.out = 1000)
er_vals2 <- seq(from = min(colMeans(df)), to = max(colMeans(df)), 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_vals1 <- sapply(er_vals1, function(er) {
op <- portfolio.optim(as.matrix(df), er, short = T)
return(op$ps)
})
sd_vals2 <- sapply(er_vals2, function(er) {
op <- portfolio.optim(as.matrix(df), er, short = F)
return(op$ps)
})
plot(x = sd_vals1, y = er_vals1, type = "l", col = "red",
xlab = "sd", ylab = "er",
main = "red: allowing short-selling;\nblue: disallowing short-selling")
lines(x = sd_vals2, y = er_vals2, type = "l", col = "blue")
推荐阅读
- javascript - jQuery,如何获取包含“/”的数据属性值的元素并对它们进行排序
- java - 如何使用Java SpringBoot更新表中的现有值而不插入新值
- python-3.x - 用多个轴在一张图中保存 pandas.DataFrame.hist
- arraylist - 无法在 groovy 中的字符串列表上调用 remove() 方法
- numpy - numpy数组中的随机有效数据项
- visual-studio-code - 如何使用平台(或外壳)特定的环境变量值?
- javascript - 如何在 R 中抓取 .aspx 站点并更改下拉框值
- typescript - TS回调函数参数好像检查不全
- c# - 如何将列表的 2 项分组到另一个列表中
- c++ - 替换对象时调用析构函数