I've worked for several days trying to get this code to run faster. The imported data is 15MB.
cyclesperday =
CloudGet["https://www.wolframcloud.com/objects/heaneym/\
cyclesperday"];
nc = 10^5;
AbsoluteTiming[
samplemanycyclesper5years =
ParallelTable[Total[RandomChoice[cyclesperday, 1826]], {100*nc}];
cycledatatofit = Partition[samplemanycyclesper5years, 100];
samplecycledistributions =
ParallelTable[
EstimatedDistribution[cycledatatofit[[i]],
LogNormalDistribution[\[Mu], \[Sigma]]], {i, 1, nc}];
cyclesamples =
Round[ParallelTable[
RandomVariate[samplecycledistributions[[j]]], {j, 1, nc}]];
]
The above code takes about 110 sec to run.
Here is the code revised as suggested below:
cyclesperday =
CloudGet["https://www.wolframcloud.com/objects/heaneym/\
cyclesperday"];
nc = 10^5;
rand = Compile[{{cycl, _Real, 1}, {i, _Integer, 0}},
Module[{pos = RandomInteger[{1, Length[cycl]}, i]},
Total[cycl[[pos]]]], RuntimeAttributes -> {Listable},
Parallelization -> True, RuntimeOptions -> "Speed"];
maxLikelihood =
Compile[{{values, _Real, 1}},
Module[{\[Mu] = Mean[Log[values]]}, {\[Mu],
Sqrt@Mean[(Log[values] - \[Mu])^2]}],
RuntimeAttributes -> {Listable}, Parallelization -> True];
AbsoluteTiming[
samplemanycyclesper5years = rand[cyclesperday, Array[1826 &, 100*nc]];
cycledatatofit = Partition[samplemanycyclesper5years, 100];
samplecycledistributions = maxLikelihood[cycledatatofit];
cyclesamples =
Round[ParallelTable[
RandomVariate[LogNormalDistribution @@ parms], {parms,
samplecycledistributions}]];
]
The above revised code takes about 107 sec to run.
How can the different components of the code be significantly faster (see below), but the components put together not be?