Hi all,
In the model I am currently working on, I need to be able to draw from a multinomial distribution. Unfortunately, Eidos does not possess a function such as rmultinom that would allow me to do that. I have tried to code one in Eidos in multiple ways, but they all run very slowly. See underneath the two ways I have tried to do it that run the fastest.
This one uses a series of binomial draws :
function (integer) multinom (integer$ size, float probs){
s=size;
rho=1;
k=length(probs);
counts=c(1:k);
for (i in 0:(k-2)){
if (rho != 0){
p=probs[i]/rho;
if(p>1.0){p=1.0;}
counts[i] = rbinom(1,s,p);}
else
{counts[i] = 0;}
s=s-counts[i];
rho=rho-probs[i];
}
counts[k-1]=s;
return counts;}
This one draws size samples from k categories, with probabilities probs, then counts how many times each category appears :
function(integer)multinom2(integer$ k, integer$ size, float probs) {
draws=sample(x=0:(k-1),size=size,replace=T,weights=probs);
counts=tabulate(draws,maxbin=99);
return counts;}
No matter how i try to code it, it runs very slowly. It really is a problem because I call this function very often. Has anybody encountered this problem before ? Any way to code this in a way that runs fast ? Thanks!
Claire Runser.
Cheers, -B. Benjamin C. Haller Messer Lab Cornell University
--
SLiM forward genetic simulation: http://messerlab.org/slim/
---
You received this message because you are subscribed to the Google Groups "slim-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to slim-discuss...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/slim-discuss/b45712ea-653c-4e57-a706-3d9e38254888n%40googlegroups.com.