

Let’s take a look at the amount of memory used by MATLAB after running these two versions. This improvement in speed is not without its cost, however, and the price we have to pay is memory.
Matlab random permute matrix code#
This code takes an average of 4.19 seconds to run on my laptop giving us a factor of 2.58 times speed up over the original. SimulPrices = SimulPrices.*reshape(sim(end,:,:),2,n)'

The cumprod command is the key to doing this and the resulting code looks as follows: optimised_corr1.m %OPTIMISED_CORR1 - A pure-MATLAB optimised code that simulates two correlated assetsĬorrWiener(:,:,i)=randn(T-1,2)*UpperTriangle Now, the most obvious optimisation that we can do with code like this is to use vectorisation to get rid of that double loop. Vectorisation – removing loops to make code go faster

On my laptop, this code takes 10.82 seconds to run on average. % Comment this section out if doing timings UpperTriangle=chol(Corr) %UpperTriangle Matrix by Cholesky decomposition %% Generating the paths of stock prices by Geometric Brownian Motion SimulPriceB=zeros(T,n) %Simulated Price of Asset B SimulPriceA=zeros(T,n) %Simulated Price of Asset A T = 500 %Number of days to simulate = 2years = 500days What I ended up with was a simple piece of code that uses monte carlo techniques to find the distribution of two correlated assets: original_corr.m %ORIGINAL_CORR - The original, unoptimised code that simulates two correlated assetsĬurrentPrice = %Initial Prices of the two stocksĬorr = %Correlation Matrix In doing so I may have made his example close to useless from a computational finance point of view but it gave me something nice and simple to play with. I took one of these examples and stripped it down even further to its very bare bones. I asked for something that was written to make it easy to see the underlying mathematics rather than for speed and he duly obliged with several great examples. So, I asked one of my collaborators, Yong Woong Lee, a doctoral researcher in the Manchester Business School, if he could furnish me with a very simple piece computational finance code. I’d then write these up and publish here for comment. We’ve had some great successes using techniques such as vectorisation, mex files and The NAG Toolbox for MATLAB (among others) combined with the raw computational power of Manchester’s Condor Pool (which I help run along with providing applications support).Ī couple of months ago, I had the idea of taking a standard calculation in computational finance and seeing how fast I could make it run on MATLAB using various techniques. Recently, I’ve been working with members of The Manchester Business School to help optimise their MATLAB code.
