implement bootstrap using parallel computing -凯发k8网页登录

main content

implement bootstrap using parallel computing

bootstrap in serial and parallel

here is an example timing a bootstrap in parallel versus in serial. the example generates data from a mixture of two gaussians, constructs a nonparametric estimate of the resulting data, and uses a bootstrap to get a sense of the sampling variability.

  1. generate the data:

    % generate a random sample of size 1000,
    % from a mixture of two gaussian distributions 
    x = [randn(700,1); 4   2*randn(300,1)];
  2. construct a nonparametric estimate of the density from the data:

    latt = -4:0.01:12;
    myfun = @(x) ksdensity(x,latt); 
    pdfestimate = myfun(x);
  3. bootstrap the estimate to get a sense of its sampling variability. run the bootstrap in serial for timing comparison.

    tic;b = bootstrp(200,myfun,x);toc
    elapsed time is 10.878654 seconds.
  4. run the bootstrap in parallel for timing comparison:

    mypool = parpool()
    starting parpool using the 'local' profile ... connected to 2 workers.
    mypool = 
      pool with properties:
        attachedfiles: {0x1 cell}
           numworkers: 2
          idletimeout: 30
              cluster: [1x1 parallel.cluster.local]
         requestqueue: [1x1 parallel.requestqueue]
          spmdenabled: 1
    
    opt = statset('useparallel',true);
    tic;b = bootstrp(200,myfun,x,'options',opt);toc
    elapsed time is 6.304077 seconds.

    computing in parallel is nearly twice as fast as computing in serial for this example.

overlay the ksdensity density estimate with the 200 bootstrapped estimates obtained in the parallel bootstrap. you can get a sense of how to assess the accuracy of the density estimate from this plot.

hold on
for i=1:size(b,1),
    plot(latt,b(i,:),'c:')
end
plot(latt,pdfestimate);
xlabel('x');ylabel('density estimate')

reproducible parallel bootstrap

to run the example in parallel in a reproducible fashion, set the options appropriately (see running reproducible parallel computations). first set up the problem and parallel environment as in bootstrap in serial and parallel. then set the options to use substreams along with a stream that supports substreams.

s = randstream('mlfg6331_64'); % has substreams
opts = statset('useparallel',true,...
    'streams',s,'usesubstreams',true);
b2 = bootstrp(200,myfun,x,'options',opts);

to rerun the bootstrap and get the same result:

reset(s) % set the stream to initial state
b3 = bootstrp(200,myfun,x,'options',opts);
isequal(b2,b3) % check if same results
ans =
     1
网站地图