MATLAB: How can i simulate equity prices with parfor

parfor

Hi everybody. Now I am learning parfor. Before I use parfor, I had a simulation of equity price with "gbm" and "simBySolution", which gave me what I want. however, when I changed my previous "for" loop to "parfor" loop, the simulation gives no output. Part of my codes are as follows.
parfor v = 1:8;
StoPrice = gbm(StoPriDrift/365,SSigma(v)/365,'StartState',100);
sstoprice = simBySolution(StoPrice,nPeriods-1,'DeltaTime',dt,'nTrials',1);
end
I got the StoPrice from the gbm process. However, I did not get anything from the simBySolution. I will appreciate if someone can help me with this. Thanks Best, Julie

Best Answer

To get output from a parfor loop, you need to write to an output variable in either a "sliced" or "reduction" manner. For a "sliced" variable, this essentially means each iteration of the parfor loop writes to an element of the output array indexed by the loop variable. So, you might be able to do the following:
parfor v = 1:8;
StoPrice = gbm(StoPriDrift/365,SSigma(v)/365,'StartState',100);
sstoprice{v} = simBySolution(StoPrice,nPeriods-1,'DeltaTime',dt,'nTrials',1);
end
After this loop, sstoprice will be a cell array with a single entry per iteration of the loop. (You can use any type of array for the output - it doesn't have to be a cell - I picked that simply because I have no idea with the size of the output of simBySolution is going to be...)
More details about sliced variables in the documentation.