I am using the CVXR
modelling package to solve a convex optimization problem. I know for sure that the problem is convex and that it follows the DCP rules, but if I check the DCP rules using CVXR
it returns False
. However, if I take the exact same problem and check it using CVXPY
it returns True
(as expected)
What is happening here? I attach a minimal reproducible example of this behavior in R and Python:
R code using CVXR
library(splines2)
library(CVXR)
deriv_basis = splines2::dbs(seq(0, 1, length.out=100), degree=3, intercept=T, df=30, derivs=2)
R = t(deriv_basis) %*% deriv_basis
beta_var = CVXR::::Variable(nrow(R))
q = CVXR::quad_form(beta_var, R)
CVXR::is_dcp(q)
[1] FALSE
write.table(x=R, file='R.csv'), row.names=F, sep=';')
Python code using CVXPY
import cvxpy
import pandas as pd
R = pd.read_csv('R.csv', sep=';').values
beta_var = cvxpy.Variable(R.shape[1])
q = cvxpy.quad_form(beta_var, R)
q.is_dcp()
Out[1]: True
Can someone explain what is happening here and how to solve it so I can use CVXR?
The problem is the negative eigenvalue in the R matrix. If you fix that by setting it to zero, say, then it satisfies the dcp condition. I have also fixed the syntax errors in the code in the question and removed the redundant :: . Another possibility (not shown) is to use
nearest_spd
in the pracma package to adjust the R matrix.