Skip to content

Instantly share code, notes, and snippets.

@hidde-jan
Created March 1, 2012 20:48
Show Gist options
  • Save hidde-jan/1953133 to your computer and use it in GitHub Desktop.
Save hidde-jan/1953133 to your computer and use it in GitHub Desktop.
Solve the portfolio QD problem using solvesdp and SeDuMi solver
function [ opt, x_opt, diagnostics ] = solveportfolio( C, r, alpha )
%SOLVEPORTFOLIO Solve the portfolio problem.
% Minizing the risk for investing in `n` different opportunities, while
% keeping the expected return on investment above `alpha`.
%
% This solves the following QD:
%
% min x' C x
%
% st. sum(x) = 1
% r' x >= alpha
% x >= 0
n = length(r);
x = sdpvar(n, 1);
% Set constraints.
F = set( r' * x >= alpha );
F = F + set( sum(x) == 1 );
F = F + set( x >= 0 );
% Minimum investment risk.
objective_function = x' * C * x;
% Solve optimization problem.
diagnostics = solvesdp(F, objective_function, ...
sdpsettings('showprogress', 1, 'solver', 'sedumi'));
% Cleanup return values.
opt = double(objective_function);
x_opt = double(x);
x_opt(x_opt < 1e-5) = 0; % Truncate small values.
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment