Code covered by the BSD License  

Highlights from
Enhancing e-Infrastructures with Advanced Technical Computing: Parallel MATLAB® on the Grid

Demo1_PorfolioOptim.m
%% Parfor version of Portfolio Optimization
%
% This demo uses the Parallel Computing Toolbox(TM) to perform a mean-variance 
% portfolio optimization of a stock portfolio, and generates an efficient
% frontier.  The portfolios on the frontier are optimal in the sense that they
% offer the minimal risk for some given level of expected return.
%
% We are given the daily returns of a group of stocks over a fixed 
% time period, and try to choose a portfolio such that it achieves some given
% return mu, and has minimal risk in the mean-variance sense.
% This leads us to solve a quadratic minimization problem with equality
% constraints.  Solving this minimization problem for a range of values of 
% mu gives us the efficient frontier.
%

%% Demo Setup
% This function gives us the desired returns, |muVec|, for which we should
% find the minimal risk. The demo difficulty level controls the length of the
% vector |muVec|.  Additionally, the function |Demo1_setup_optim| displays
% for reference a graph of the daily returns of a few of the stocks in the
% portfolio. 

clear all
difficulty = 1; 
[fig, muVec, covMat, expRet ] = Demo1_setup_optim(difficulty);

%% Serial Version

tic
[risk, ret] = Demo1_task_optim(covMat, expRet, muVec);
elapsedTimeSerial = toc;

%% Plot the final results

fig1 = figure;
Demo1_plot_optim(fig1, risk, ret);

%% Changing to a parfor
% For this all you have to do is change the for
% to a parfor in the serial code and save as Demo1_task_optim_parfor

edit('Demo1_task_optim.m')

%% Opening up a matlabpool using default configuration

matlabpool open 2  % uses default configuration

%% Running the optimization that uses a parfor

tic
[risk, ret] = Demo1_task_optim_parfor(covMat, expRet, muVec);
elapsedTimeParallel = toc;

%% Plot the final results

fig2 = figure;
Demo1_plot_optim(fig2, risk, ret);

%% Speedup ratio

ratio = elapsedTimeSerial/elapsedTimeParallel;
display(strcat('Your speed up ratio is: ', num2str(ratio)))

%% Closing the matlabpool

matlabpool close

Contact us at files@mathworks.com