I have a function which randomly picks stocks to buy, subject to the constraint of a fixed number of holding periods and fixed number of stocks that can be bought at any one time. The actual function is as follows:
function profit=monteCarloSim(stockPrices,numHoldingPeriods,numStocks,numSimuls)[rows,cols] = size(stockPrices);stockReturns = diff(stockPrices);profit = zeros(numSimuls,1);parfor i=1:numSimuls buyWeights = zeros(rows-1,cols); buySignal = randperm(rows-1,numHoldingPeriods); buyRandomAssetPicks = randperm(cols, numStocks); buyWeights(buySignal,buyRandomAssetPicks)=1/numStocks; portfolioReturns = sum(stockReturns.*buyWeights,2); profit(i) = sum(portfolioReturns);endend
I was planning to limit the number of operations in the loop by assigning each iteration of buyWeights to a 3D matrix. This way, I would take the portfolioReturns and profits(i) steps out of the loop, and completely vectorize them.
However, this approach becomes complicated when I have a huge number of simulations and a large data set, because there's not enough memory to store the whole 3D matrix. So I have to come up with a few extra steps to manage memory, e.g. build smaller chunks of the 3D matrix iteratively.
Even if I had the memory to do the 3D matrix approach, I'll still have to iterate the randperm function. I can't find a function to repeat this more effectively and I'm still a novice at using bsxfun.
Is there a way to speed this function up? I also have a good number of GPU cores, if a gpuArray method helps.
Thanks in advance!
Best Answer