Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different solutions for cvxpy and cvxpylayers #149

Open
ManuelP96 opened this issue Sep 5, 2023 · 1 comment
Open

Different solutions for cvxpy and cvxpylayers #149

ManuelP96 opened this issue Sep 5, 2023 · 1 comment

Comments

@ManuelP96
Copy link

ManuelP96 commented Sep 5, 2023

I have a little example of a cvxpy problem

import numpy as np
import cvxpy as cp
import tensorflow as tf
from   cvxpylayers.tensorflow import CvxpyLayer

b=np.ones(6)/6
vol = np.array([0.2, 0.2, 0.15, 0.15, 0.1, 0.1])
rho   = np.array([[   1,  0.7,  0.8,  0.3, -0.2,  0.4],
                  [ 0.7,    1,  0.5,  0.2, -0.1,  0.1], 
                  [ 0.8,  0.5,    1,  0.3, -0.1, 0.1], 
                  [ 0.3,  0.2,  0.3,    1, -0.2,  0.2],
                  [-0.2, -0.1, -0.1, -0.2,    1,    0], 
                  [ 0.4,  0.1,  0.1,  0.2,    0,    1]])
Sigma = np.matmul(np.diag(vol), np.matmul(rho, np.diag(vol)))

btf = tf.Variable(b)
Sigmatf = tf.Variable(Sigma)

w = cp.Variable(6) 

obj = 0.5 * cp.quad_form(w, Sigma) - cp.sum(cp.multiply(b, cp.log(w)))
constr = [w>=0] 
prob = cp.Problem(cp.Minimize(obj), constr)

prob.solve()
w = w/cp.sum(w)

print("Portfolio weights\t", w.value)

but, when I replicate the problem into a cvxpylayer, I get different results. In fact, it seems like the cvxpylayer returns the parameter values:

n,_ = Sigmatf.get_shape()

w = cp.Variable(n)
risk = cp.Parameter(n, nonneg = True)
obj = 0.5 * cp.quad_form(w, Sigma) - risk.T @ cp.log(w)
constr = [w>=0]
prob = cp.Problem(cp.Minimize(obj), constr)
cvxpylayer = CvxpyLayer(prob, parameters=[risk], variables=[w])

b1 = tf.reshape(btf,[6])

with tf.GradientTape() as tape:
    solution,  = cvxpylayer(b1)

solution = solution/tf.reduce_sum(solution)
print(solution.numpy())
@ManuelP96 ManuelP96 changed the title Defining CvxLayer in sequential NN Different ssolutions for cvxpy and cvxpylayers Sep 8, 2023
@ManuelP96 ManuelP96 changed the title Different ssolutions for cvxpy and cvxpylayers Different solutions for cvxpy and cvxpylayers Sep 8, 2023
@ManuelP96
Copy link
Author

I could write the problem using cvxpylayers on torch

b   = np.ones(6)/6                                                          #Risk Budgets
vol = np.array([0.3, 0.25, 0.2, 0.15, 0.1, 0.5])                            #Volatility of Assets
rho = np.array([[   1,  0.7,  0.8,  0.3,  0.2,  0.4],                       #Correlation matrix
                [ 0.7,    1,  0.5,  0.2,  0.1,  0.1],
                [ 0.8,  0.5,    1,  0.3,  0.1,  0.1],
                [ 0.3,  0.2,  0.3,    1,  0.2,  0.2],
                [ 0.2,  0.1,  0.1,  0.2,    1,    0],
                [ 0.4,  0.1,  0.1,  0.2,    0,    1]])
Sigma = np.matmul(np.diag(vol), np.matmul(rho, np.diag(vol)))               #Covariance Matrix

bt = Variable(torch.tensor(b))
Sigmat = Variable(torch.tensor(Sigma))

n = 6

w = cp.Variable(n)
bp = cp.Parameter(n, pos=True)

obj = 0.5 * cp.quad_form(w, Sigma) - cp.sum(cp.multiply(bp, cp.log(w)))
constr = [w>=0]
prob = cp.Problem(cp.Minimize(obj), constr)

cvxpylayer = CvxpyLayer(prob, parameters=[bp], variables=[w])

# solve the problem
solution, = cvxpylayer(bt, solver_args={"solve_method": "ECOS"})

but I need it in tensorflow. Using solver_args={"solve_method": "ECOS"} in tensorflow gives me the error diffcp.cone_program.SolverError: Solver ecos errored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant