parfor loop: random samples are dependent
1 view (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!