Skip to content

The objective function is linear, the constraints are in the form of the subtraction of the convex function, and the linear equality, but the expression is not "dccp". #86

@zwc1914

Description

@zwc1914

Good work!

Hello! I am a cvxpy dccp novice, recently in doing a convex difference programming problem, the optimization model is written, the code running shows that it is not dccp, but my objective function is linear, the constraints are convex difference constraints, and linear constraints. What is the problem? Excuse me!

My complete code is as follows:

`
#Construct mean and variance matrix
import pandas as pd
import numpy as np
import akshare as ak

#Read 5 stocks 2015-01-01 to 2023-6-13 logarithmic returns
def get_ret(code):
data = ak.stock_zh_a_hist(symbol=code, period="daily", start_date="20150101", end_date="20230613")
data.index = pd.to_datetime(data['日期'],format='%Y-%m-%d') # Set the date index
close = data['收盘'] # Daily Closing price
close.name = code
ret = np.log(close/close.shift(1)) # Daily yield
return ret

codes=['000001','000651','300015','600519','000625']
ret = pd.DataFrame()
for code in codes:
ret_ = get_ret(code)
ret = pd.concat([ret,ret_],axis=1)
ret = ret.dropna()

R_cov = ret.cov() # Calculate the covariance
cov= np.array(R_cov)

#Covariance extraction function
def cutcout_cov(seq, index):
return pd.concat([seq.iloc[:,:index],seq.iloc[:,index+1:]], axis = 1).cov()
`

The above code has been tested, no problem, the problem appears in the following code

`
#Weight vector extraction function
def cutcout_w(seq, index):
if index==0:
return seq[index+1:]
elif index==n-1:
return seq[:n-1]
else:
return cp.hstack([seq[:index],seq[index+1:]])

#Optimization model
import cvxpy as cp
import dccp

n=5
w=cp.Variable(n)
Lmax=cp.Parameter(n)
Lmax.value = [10, 10, 10, 10, 10]

constr = []
for index in range(n):
w_new=cutcout_w(w,index)
cov_new = cutcout_cov(ret,index).cov()
constr.append(cp.quad_form(w,cov)-cp.quad_form(w_new,cov_new)-Lmax[index].value<=0)
constr.append(cp.sum(w)==1)

prob = cp.Problem(cp.Maximize(ret.mean().values.T@w), constr)
prob.solve(method="dccp", max_iter=100, solver="SCS")

`

The error message is as follows:

Exception Traceback (most recent call last)
Cell In[39], line 59
56 constr.append(cp.sum(w)==1)
58 prob = cp.Problem(cp.Maximize(ret.mean().values.T@w), constr)
---> 59 prob.solve(method="dccp", max_iter=100, solver="SCS")

File D:\Anaconda\Anaconda_set_up\lib\site-packages\cvxpy\problems\problem.py:493, in Problem.solve(self, *args, **kwargs)
491 else:
492 solve_func = Problem._solve
--> 493 return solve_func(self, *args, **kwargs)

File D:\Anaconda\Anaconda_set_up\lib\site-packages\dccp\problem.py:43, in dccp(self, max_iter, tau, mu, tau_max, solver, ccp_times, max_slack, ep, **kwargs)
31 """
32 main algorithm ccp
33 :param max_iter: maximum number of iterations in ccp
(...)
40 if the transformed problem is infeasible, return None;
41 """
42 if not is_dccp(self):
---> 43 raise Exception("Problem is not DCCP.")
44 if is_contain_complex_numbers(self):
45 warnings.warn("Problem contains complex numbers and may not be supported by DCCP.")

Exception: Problem is not DCCP.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions