%% 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.
%
%
% Copyright 2008 The MathWorks, Inc.
% $Revision: 1.1.6.1 $ $Date: 2007/11/09 20:04:57 $
% $Revision: modified to use parfor, S.Zaranek, 2008/29/09 $
%% 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 |Demo4_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 ] = Demo4_setup_optim(difficulty);
%% Serial Version
tic
[risk, ret] = Demo4_task_optim(covMat, expRet, muVec);
elapsedTimeSerial = toc;
%% Plot the final results
fig1 = figure;
Demo4_plot_optim(fig1, risk, ret);
%% Running the optimization that uses scheduled jobs
numTasks = 2;
tic
[risk, ret] = Demo4_task_optim_jobs(covMat, expRet, muVec, numTasks);
elapsedTimeParallel = toc;
%% Plot the final results
fig2 = figure;
Demo4_plot_optim(fig2, risk, ret);
%% Speedup ratio
ratio = elapsedTimeSerial/elapsedTimeParallel;
display(strcat('Your speed up ratio is: ', num2str(ratio)))