$Title Portfolio Analysis with Matlab & GAMS $ontext In this example we use a QP to analyze a portfolio selection problem. The QP model is essentially taken from QALAN in the GAMS model library. The data starts in Matlab and is dumped to a standard (i.e. non-indexed) GDX file. This data drives the QP model below. The results are dumped to a second standard GDX file, read back in to Matlab, and used to generate a plot of the efficient frontier. Contributor: Steve $offtext set i 'securities'; alias (i,j); parameters mean(i) 'mean annual returns on individual securities (%)' cov(i,j) 'variance-covariance array (%-squared annual return)' ; $if not set INFILE $set INFILE dataInput.gdx $if not set OUTFILE $set OUTFILE resultOutput.gdx $if not set N $set N 100 $if not exist %INFILE% $abort Input file %INFILE% does not exist $gdxin %INFILE% $loaddc i mean cov $gdxin positive variable x(i) 'fraction of portfolio invested in asset i'; free variable variance 'variance of portfolio'; free variable target 'target mean annual return on portfolio (%)'; equations fsum fractions must add to 1.0 dmean definition of mean return on portfolio dvar definition of variance; fsum.. sum{i, x(i)} =e= 1.0; dmean.. sum{i, mean(i)*x(i)} =e= target; dvar.. sum{i, x(i)*sum{j,cov(i,j)*x(j)}} =e= variance; model portfolio / fsum, dmean, dvar /; scalars tmin, tmax; * add some commands to speed up the solve, since we're looping later option solprint=silent, limrow=0, limcol=0; option solvelink = %solvelink.LoadLibrary%; solve portfolio using QCP minimizing target; abort$[portfolio.solvestat <> 1] 'Problem finding minimum return', portfolio.solvestat; tmin = target.l; solve portfolio using QCP maximizing target; abort$[portfolio.solvestat <> 1] 'Problem finding maximum return', portfolio.solvestat; tmax = target.l; file log / '' /; putclose log ' ' / 'target min = ', tmin:8:4, '%' / 'target max = ', tmax:8:4, '%' /; * compute the efficient frontier set s / s0 * s%N% /; parameters t(s), v(s); t(s) = eps + tmin + (ord(s)-1)/(card(s)-1) * (tmax-tmin); loop{s, target.fx = t(s); solve portfolio using QCP minimizing variance; abort$[portfolio.solvestat <> 1] 'Problem finding minimum variance', portfolio.solvestat, s, t; v(s) = eps + variance.l; }; execute_unload '%OUTFILE%';