Rev 3265 | Rev 4161 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
20 | reyssat | 1 | !if $wims_read_parm!=slib_header |
2 | !goto proc |
||
3 | !endif |
||
4 | slib_title=Generation of binomial random data |
||
5 | slib_parms=3\ |
||
6 | ,m\ |
||
7 | ,n\ |
||
8 | ,p |
||
9 | slib_author=Adeline Grelot |
||
10 | slib_out=random data of m integers following a binomial law of parameters n and p (n is an integer and p a real between 0 and 1) |
||
11 | slib_comment= P(X=k)=binomial(n,k)*p^k*(1-p)^(n-k) |
||
12 | slib_example=15,10,0.5 |
||
3265 | bpr | 13 | slib_require=pari |
3307 | bpr | 14 | !exit |
15 | |||
20 | reyssat | 16 | :proc |
17 | slib_out= |
||
18 | |||
19 | !distribute item $wims_read_parm into slib_m, slib_n, slib_p |
||
20 | slib_u=!random 0,1 repeat $slib_m |
||
21 | slib_C=!values x for x=0 to $[$slib_n] |
||
22 | |||
23 | slib_out=!exec pari {slib_g(p,x,m)=local(L,u,q,i,k) ;L=listcreate(m);\ |
||
24 | for(k=1,m,u=[$slib_u][k];q=p[1];i=1;\ |
||
25 | if(u<q,listput(L,x[1]),while(u>=q,i=i+1;q+=p[i]);listput(L,x[i])));L}\ |
||
26 | {slib_bino(n,p,m)=local(q,b,prob);b=listcreate(n+1);\ |
||
27 | q=p/(1-p);prob=(1-p)^n;listput(b,prob);\ |
||
28 | for(i=1,n,prob=prob*(n-i+1)/i*q;listput(b,prob));\ |
||
29 | print(Vec(slib_g(b,[$slib_C],m)))}\ |
||
30 | print(slib_bino($slib_n,$slib_p,$slib_m)); |
||
31 | |||
32 | slib_out =!word 1 to -2 of $slib_out |
||
33 | slib_out=!declosing $slib_out |
||
34 | slib_out=!trim $slib_out |
||
35 |