fmincon: nonliner constraint

I am using fmincon to find the solution (weights of each assets) that optimizing my investment portfolio.How can I make each of the weight less than 1, instead of less than or equal to 1, but the sum of weights equals 1? The codes are:
w = zeros (4,1)
cov = xlsread('gupiao.xls','equal weights','O13:R16')
sigmaP = w'*cov*w
delta_x = xlsread('gupiao.xls','sheet3','A503:D503')
delta_P = delta_x*w
fun = @(w)-[delta_x*w-0.5*[0.7*w'*cov*w+1/1000000*(0.1*36843+0.2*48772)]^2]
Aeq = ones(1,4)
beq = 1
A = []
b = []
lb = []
ub=ones(size(w))
w = fmincon(fun,w,A,b,Aeq,beq,lb,ub)
It gives me the results:
w =
1.0000
1.0000
1.0000
-2.0000
But, these results violate my constraint that each of the weight is less than 1. Could you please tell me how can I solve this problem?

 Accepted Answer

A = ones(1,4);
b = 1-eps;
Aeq = [];
beq = [];
This forces the sum to be less than or equal to 1-eps, which should be equivalent to being less than 1.

3 Comments

Hi~ Thank you for replying me! I tried your codes, and it gives me w =
1.0000
1.0000
1.0000
-48.8791
I appologize for not stating my problem clearly, it should be: make each of the weight less than 1, instead of less than or equal to 1, but the sum of weights equals 1.
If so, could you please tell me what constraint should I use?
Ah, that is a different problem. To do that use your original setting but set
ub = ones(1,4)-eps;
Xiangnan Liu
Xiangnan Liu on 10 May 2018
Edited: Xiangnan Liu on 10 May 2018
I rewrote my codes applying your suggestions, it works! Thanks a lot!

Sign in to comment.

More Answers (2)

Stephan
Stephan on 10 May 2018
Edited: Stephan on 10 May 2018
Hi,
i assume, negative values are not allowed. A share of -2 of the stock represented by variable 4 makes no sense.
If that is correct, use
lb=zeros(1,4)
Together with your Aeq and beq constraint this should work.
Best regards
Stephan

1 Comment

Hello! Thanks a lot for your reply! Actually, in my portfolio, negative weights are allowed, which means I am short selling. Could you please tell me what constraint should I use then?

Sign in to comment.

Matt J
Matt J on 10 May 2018
If the unconstrained minimum lies at w(i)=1, then there is no sense pursuing a constraint like w(i)<1. It would be like minimizing f(z)=(z-1)^2 with the constraint that z<1. The minimum cannot be attained.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!