MIQP for portfolio selection
Good morning, I have implemented the following portfolio selection model
Then I calculated the markowitz frontier to find the minimum and maximum rho value to trace the frontier (and then find the optimal weights for each expected performance level), for a given K. The problem is that by starting the optimization, after some iterations the program crashes and gives me the following error :
Reference to non-existent field 'x'.
Error in F_MIQP (line 79)
x_miqp(:,k)=results_.x(1:n);
I have identified the problem in the values of rho, that is; Because to the last values of rho correspond portfolios composed by 1-2 assets, having K<=5 for example, the program freezes and cannot find the vectors of the weights corresponding to those values. I tried also to use the '<' instead of '=' or '<=', but the problem remains. Here is the script used:
mu=mean(RR_while);
sigma=cov(RR_while);
[T,n]=size(RR_while);
%%
H=2*sigma;
f=[];
A=[];
b=[];
Aeq=ones(1,n);
beq=1;
LB=zeros(n,1);
UB=ones(n,1);
X = quadprog(H,f,A,b,Aeq,beq,LB,UB);
eta_min=mu*X;
eta_max=max(mu);
N=100;
eta=linspace(eta_min,eta_max,N);
%%
li=0.001;
ui=0.9;
x_i=eye(n);
y_i=eye(n);
Q=[sigma zeros(n);zeros(n,2*n)];
Aeq_miqp=[mu zeros(1,n);ones(1,n) zeros(1,n);zeros(1,n) ones(1,n)];
A_miqp=[-x_i y_i*li;x_i -y_i*ui];
model.Q=sparse(Q);
model.A=sparse([Aeq_miqp(2:3,:);A_miqp]);
model.obj=zeros(2*n,1);
model.rhs=[1;K;zeros(2*n,1)];
model.sense=['=';'<';repmat('<',2*n,1)];
model.lb=[zeros(2*n,1)];
model.ub=[ones(2*n,1)];
model.modelsense='min'
model.vtype=[repmat('C',n,1) ; repmat('B',n,1)];
results=gurobi(model);
weigths=results.x(1:n);
eta_min_m=mu*weigths;
var_min=weigths'*sigma*weigths;
eta_max=max(mu);
eta_=linspace(eta_min_m,eta_max,N);
x_miqp=NaN(n,length(eta_));
Risk_miqp=NaN(1,length(eta_));
tic
for k=1:length(eta_)
model1.Q=sparse(Q);
model1.A=sparse([Aeq_miqp;A_miqp]);
model1.obj=zeros(2*n,1);
model1.rhs=[eta_(k);1;K;zeros(2*n,1)];
model1.sense=['=';'<';'<';repmat('<',2*n,1)];
model1.lb=zeros(2*n,1);
model1.ub=ones(2*n,1);
model1.vtype=[repmat('C',n,1) ; repmat('B',n,1)];;
results_=gurobi(model1);
x_miqp(:,k)=results_.x(1:n);
Risk_miqp(k)=(x_miqp(:,k))'*sigma*x_miqp(:,k);
end
time=toc
-
Official comment
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?. -
Did you check the status code of the Gurobi optimization? May it be that at some point you have produced an infeasible model? In this case, there would indeed be no X attribute that can be queried.
0
Post is closed for comments.
Comments
2 comments