summaryrefslogtreecommitdiff
path: root/modules/randlib/demos/poisson.dem.sce
blob: bab33da2bdc8730769b8469e5b398c9c3fb8d574 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) ENPC
// Copyright (C) ????-2008 - INRIA
// Copyright (C) 2010 - DIGITEO - Allan CORNET
//
// This file is released under the 3-clause BSD license. See COPYING-BSD.


function [z]=PoissonT(n)

    function [y]=Poisson(m,n,pmean)
        // P{n} = exp(-lambda)lambda^n/n!
        // pmean =lambda
        //----------------------------
        y=0*ones(m,n)
        bound= exp(-pmean);
        for i=1:m*n,
            count = 0;
            lprod = 1;
            while( lprod >= bound), lprod = lprod*rand(1,1,"uniform");
                count = count + 1;
            end
            y(i) = count - 1;
        end
        y = matrix(y, m, n);
    endfunction


    [lhs, rhs] = argn(0);
    if rhs <= 0 ; n = 1000;end
    pmean = 3;
    y = Poisson(1, n, pmean);
    N = 20;
    i = 0:N;
    z = [];
    for i1=i, z=[z,prod(size(find(y==i1)))],end

    drawlater();
    my_handle = scf(100001);
    clf(my_handle, "reset");
    demo_viewCode("poisson.dem.sce");

    plot2d3("onn",i',z'/n,1);
    deff("[y]=fact(n)","if n==0 then y=1;else y=n*fact(n-1);end");
    zt=[];for i1=0:N; zt=[zt, exp(-pmean) *pmean^i1/fact(i1)];end
    plot2d1("onn",i',zt',[-2,6]);
    xtitle(_("Simulation of a Poisson random variable"));
    current_axe = gca();
    current_axe.title.font_size = 3;
    current_axe.background = color(232,230,200);
    legend([_("Simulation");_("Theory")]);
    drawnow();

endfunction

PoissonT();
clear PoissonT;