【发布时间】:2018-06-02 23:02:56
【问题描述】:
我正在寻找一种在 R 中进行非负分位数和 Huber 回归的快速方法(即所有系数都 > 0 的约束)。我尝试使用 CVXR 包进行分位数和 Huber 回归,使用 quantreg 包进行分位数回归,但 CVXR 非常慢,当我使用非负约束时,quantreg 似乎有问题。有谁知道 R 中有一个好的快速的解决方案,例如使用Rcplex 包或R gurobi API,从而使用更快的CPLEX 或gurobi 优化器?
请注意,我需要运行低于 80 000 次的问题大小,因此我只需要在每次迭代中更新 y 向量,但仍使用相同的预测矩阵 X。从这个意义上说,我觉得在CVXR 中我现在必须在每次迭代中执行obj <- sum(quant_loss(y - X %*% beta, tau=0.01)); prob <- Problem(Minimize(obj), constraints = list(beta >= 0)) 效率低下,而问题实际上保持不变,而我只想更新y。有什么想法可以更好/更快地完成这一切吗?
小例子:
## Generate problem data
n <- 7 # n predictor vars
m <- 518 # n cases
set.seed(1289)
beta_true <- 5 * matrix(stats::rnorm(n), nrow = n)+20
X <- matrix(stats::rnorm(m * n), nrow = m, ncol = n)
y_true <- X %*% beta_true
eps <- matrix(stats::rnorm(m), nrow = m)
y <- y_true + eps
使用 CVXR 的非负分位数回归:
## Solve nonnegative quantile regression problem using CVX
require(CVXR)
beta <- Variable(n)
quant_loss <- function(u, tau) { 0.5*abs(u) + (tau - 0.5)*u }
obj <- sum(quant_loss(y - X %*% beta, tau=0.01))
prob <- Problem(Minimize(obj), constraints = list(beta >= 0))
system.time(beta_cvx <- pmax(solve(prob, solver="SCS")$getValue(beta), 0)) # estimated coefficients, note that they ocasionally can go - though and I had to clip at 0
# 0.47s
cor(beta_true,beta_cvx) # correlation=0.99985, OK but very slow
非负 Huber 回归的语法相同,但会使用
M <- 1 ## Huber threshold
obj <- sum(CVXR::huber(y - X %*% beta, M))
使用quantreg 包的非负分位数回归:
### Solve nonnegative quantile regression problem using quantreg package with method="fnc"
require(quantreg)
R <- rbind(diag(n),-diag(n))
r <- c(rep(0,n),-rep(1E10,n)) # specify bounds of coefficients, I want them to be nonnegative, and 1E10 should ideally be Inf
system.time(beta_rq <- coef(rq(y~0+X, R=R, r=r, tau=0.5, method="fnc"))) # estimated coefficients
# 0.12s
cor(beta_true,beta_rq) # correlation=-0.477, no good, and even worse with tau=0.01...
【问题讨论】:
标签: r cplex cvx quantile-regression cvxr