Parfor version of Portfolio Optimization
This demo uses the Parallel Computing Toolbox™ 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.
Contents
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 pctdemo_setup_optim displays for reference a graph of the daily returns of a few of the stocks in the portfolio.
difficulty = 1; [fig, muVec, covMat, expRet ] = pctdemo_setup_optim(difficulty);
Serial Version
tic [risk, ret] = pctdemo_task_optim(covMat, expRet, muVec); elapsedTimeSerial = toc;
Plot the final results
fig1 = figure; pctdemo_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 pctdemo_task_optim_parfor.
edit('pctdemo_task_optim.m')
Opening up a matlabpool using default configuration
matlabpool open 2 % uses default configuration
Starting matlabpool using the parallel configuration 'local'. Waiting for parallel job to start... Connected to a matlabpool session with 2 labs.
Running the optimization that uses a parfor
tic [risk, ret] = pctdemo_task_optim_parfor(covMat, expRet, muVec); elapsedTimeParallel = toc;
Plot the final results
fig2 = figure; pctdemo_plot_optim(fig2, risk, ret);
Speedup ratio
ratio = elapsedTimeSerial/elapsedTimeParallel;
display(strcat('Your speed up ratio is:', num2str(ratio)))
Your speed up ratio is:1.8673
Closing the matlabpool
matlabpool close
Sending a stop signal to all the labs... Waiting for parallel job to finish... Performing parallel job cleanup... Done.