# parfor loop: random samples are dependent

1 view (last 30 days)
Abhinav Gupta on 29 Nov 2021
Edited: Abhinav Gupta on 2 Dec 2021
In one sentence my question is how do I carry out MC simulations in parallel using parfor loop without introducing statistical dependecies?
Here are the details. I am using parfor loop to do a Monte Carlo simulation. Basically, I draw some samples from multivariate-Normal random variable and do something with these samples
parfor i = 1:N
% samps = draw n samples using mvnrnd
% x(i) = a complicated function of samps
end
The thing is that I want my N samples to be independent of each other. Samples seems to be statistically independent when I use 'for loop' instead of 'parfor loop', but the samples seem to be statistically dependent when I use parfor loop.
I have tried to address this problem by two methods
(1) generating random seeds before the parfor loop and then changing the state of random number generator in each loop as follows:
rng('default');
rng_seeds = randi(100000,n,1);
parfor i = 1:N
rng(rng_seeds(i), 'twister')
% samps = draw n samples using mvnrnd
% x(i) = a complicated function of samps
end
This addresses the problem to some extent but not completely as there is still some statistical dependecy visible in x series. I have attached the sereis of x below for N = 100.
(2) Generate N random samples out of pafor loop and computing x for ith sample in parfor loop as follows
for i = 1:N
% samps{i} = draw n samples using mvnrnd
end
parfor i = 1:N
% x(i) = a complicated function of samps{i}
end
This also has statistical dependecies as shown below.
When I use for loop, the following plot is obtained
Clearly, the series obtained by using for loop is qualitatively very different from the series obtained by using parfor loop.
So, my question is how do I carry out MC simulations in parallel using parfor loop without introducing statistical dependecies?
Edit:
I have also tried the 'substream' solution now it again does not help. For and parfor loop are still giving different results. I tried the following code with for and parfor loop and the results are very different:
stream = RandStream('mrg32k3a');
parfor i = 1:N
set(stream,'Substream',i);
% samps = draw n samples using mvnrnd
% x(i) = a complicated function of samps
end
Below is the plot for N = 1000. The red line is the one obtained by parfor and the blue line is obtained by for loop.

Raymond Norris on 30 Nov 2021
##### 2 CommentsShowHide 1 older comment
Abhinav Gupta on 30 Nov 2021
I gathered following from the MATLAB documentation
(1) create a random number stream using randStream
(2) assign a value to substream using parfor loop index.
The code should be as follows:
myStream = randStream('Threefry');
parfor i = 1:N
myStream.substream = i;
% samps = draw samples from mvnrnd
% x(i) = a complicated function of samps{i}
end
Can you please confirm if this makes sense? Also, I do not really understand the logic behind this. It would be helpful if you can explain that a bit. Furthermore, why would solution 1 posted in my question not work?

R2021a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by