I would like to to MonteCarlo simulations of various regressions - basically, doing OLS under various conditions to test approaches.
On my macpro 2x 2.8 GHz quad core Xeon, 32 Gb RAM DDR2 @ 667 Mhz, the baseline is 56 seconds for 10'000 lines and 6'000 iterations.
ols = {};
beta = {1, 2, 3};
size = 10000;
iterations = 6000;
out = AbsoluteTiming[
Table[x1 =
Table[{RandomVariate[NormalDistribution[0, 1]],
RandomVariate[NormalDistribution[0, 1]]^2,
RandomVariate[NormalDistribution[0, 1]]}, {size}];
u1 = Table[RandomVariate[NormalDistribution[0, 1]], {size}];
y1 = x1.beta + u1;
LeastSquares[x1, y1], {iterations}]];
out[[1]]
out[[2]] // Dimensions
51.895992
{6000, 3}
Simply by switching to ParallelTable, I get down to about 12.5 seconds
After reading Transferring a large amount of data in parallel calculations, I updated my code to use the suggested modifications :
- a modified MemberQ that doesn't do the unpacking (goes down to 12.38 seconds)
- Method-> "CoarsestGrained" (goes down to 11.9 seconds)
- both (goes down to 11.9 seconds)
$IterationLimit = 100000;
out = AbsoluteTiming@withModifiedMemberQ@ParallelTable[
(...)
, {iterations}, Method -> "CoarsestGrained"];
I am wondering what I should do to for further performance gains. I'd like to do 10'000 lines and 10'000 iterations in less than 10 seconds if possible.
Table[RandomVariate[NormalDistribution[0, 1]], {size}]
should beRandomVariate[NormalDistribution[0,1], size]
. There's a significant overhead when callingRandomVariate
. Try to generate as many numbers with one call as possible. $\endgroup$