2
$\begingroup$

Function AppendTo seems to be very slow for generating a lot of scenarios, is there any chance to change it and make it faster?

alphaxt= 100; betaxt=130; 
     q=RandomReal[{0,1},106]
        fun[n0_, q_] := 
         Module[{DD = {}, t = 41, j, a = alphaxt, b = betaxt},
           For[j = 0, j <= t - 1, j++, 
           AppendTo[DD, 
            Min[RandomInteger[
              NegativeBinomialDistribution[a, 
               b/((n0 - Total[DD])*q[[66 + j]] + b)]], n0 - Total[DD]]]]; 
          DD]

SeedRandom[123];
 Map[fun[5000, q] &, Range[5000]]
$\endgroup$
4
  • $\begingroup$ Why don't you use Table[]? $\endgroup$ Mar 26, 2016 at 0:40
  • $\begingroup$ because of the overflow of memory $\endgroup$
    – Radka
    Mar 26, 2016 at 0:44
  • 1
    $\begingroup$ But you are then gradually forming DD anyway… $\endgroup$ Mar 26, 2016 at 0:49
  • $\begingroup$ I typically use Associations now instead of Lists with AppendTo, as they scale better. In this case replacing the list with an Association does not change the timing. I'm inclined to believe that list appending is not the rate determining step in this code. $\endgroup$ Mar 26, 2016 at 2:15

1 Answer 1

4
$\begingroup$

As J.M. has pointed out: using Table[] is a better idea, or else i modified your code a little bit with the use of Reap[] and Sow[] functions. It is on average 12 seconds faster:

alphaxt = 100; betaxt = 130;
q = RandomReal[{0, 1}, 106];
fun[n0_, q_] := Reap@Module[{t = 41, j, a = alphaxt, b = betaxt},
DD = 0;
For[j = 0, j <= t - 1, j++,
 DD += 
  Sow[Min[RandomInteger[
     NegativeBinomialDistribution[a, 
      b/((n0 - DD)*q[[66 + j]] + b)]], n0 - DD]]
 ];
];

Map[fun[5000, q] &, Range[5000]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.