summaryrefslogtreecommitdiff
path: root/modules/randlib/demos
diff options
context:
space:
mode:
authorShashank2017-05-29 12:40:26 +0530
committerShashank2017-05-29 12:40:26 +0530
commit0345245e860375a32c9a437c4a9d9cae807134e9 (patch)
treead51ecbfa7bcd3cc5f09834f1bb8c08feaa526a4 /modules/randlib/demos
downloadscilab_for_xcos_on_cloud-0345245e860375a32c9a437c4a9d9cae807134e9.tar.gz
scilab_for_xcos_on_cloud-0345245e860375a32c9a437c4a9d9cae807134e9.tar.bz2
scilab_for_xcos_on_cloud-0345245e860375a32c9a437c4a9d9cae807134e9.zip
CMSCOPE changed
Diffstat (limited to 'modules/randlib/demos')
-rwxr-xr-xmodules/randlib/demos/binomial.dem.sce71
-rwxr-xr-xmodules/randlib/demos/discrete.dem.sce54
-rwxr-xr-xmodules/randlib/demos/discrete_uni.dem.sce46
-rwxr-xr-xmodules/randlib/demos/erlang.dem.sce54
-rwxr-xr-xmodules/randlib/demos/exp.dem.sce47
-rwxr-xr-xmodules/randlib/demos/geometric.dem.sce53
-rwxr-xr-xmodules/randlib/demos/hypergeom.dem.sce45
-rwxr-xr-xmodules/randlib/demos/poisson.dem.sce57
-rwxr-xr-xmodules/randlib/demos/randlib.dem.gateway.sce27
-rwxr-xr-xmodules/randlib/demos/weibull.dem.sce40
10 files changed, 494 insertions, 0 deletions
diff --git a/modules/randlib/demos/binomial.dem.sce b/modules/randlib/demos/binomial.dem.sce
new file mode 100755
index 000000000..02cc3766c
--- /dev/null
+++ b/modules/randlib/demos/binomial.dem.sce
@@ -0,0 +1,71 @@
+// 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 [zt]=BinomialT(n)
+
+ function [y]=Binomial(m,n,pb,nb)
+ // Binomial law (p,N)
+ // P{X=n} = C_N^n p^n (1-p)^(N-n)
+ //----------------------------------
+ res = [];
+ // we use blocks of size 100 to avoid overflows
+ ntir = 100;
+ ntirc = ntir;
+ y = rand(ntir,nb,"uniform");
+ indy = find( y < pb);
+ y = 0*ones(y);
+ y(indy) = 1;
+ y = sum(y,"c")
+ res = [res;y];
+ while ( ntirc < m*n )
+ y = rand(ntir,nb,"uniform");
+ indy = find(y< pb);
+ y = 0*ones(y);
+ y(indy) = 1;
+ y = sum(y,"c")
+ res = [res;y];
+ ntirc = ntirc + ntir;
+ end
+ y = matrix(res(1:m*n),m,n);
+ endfunction
+
+
+ [lhs, rhs] = argn(0)
+ if rhs <= 0 ; n=10000;end
+ prb = 0.5;
+ N = 10;
+ y = Binomial(1, n, prb, N);
+ i = 0:10;
+ z = [];
+ for i1=i, z=[z,prod(size(find(y==i1)))],end
+
+ drawlater();
+
+ my_handle = scf(100001);
+ clf(my_handle, "reset");
+ demo_viewCode("binomial.dem.sce");
+ plot2d3("onn",i',z'/n,[1,3]);
+ deff("[y]=fact(n)", "y=prod(1:n)");
+ deff("[z]=C(N,n)", "z= fact(N)/(fact(n)*fact(N-n))");
+ i = 0:N;
+ zt = [];
+ for j=i, zt=[zt, C(N,j)*prb^j*(1-prb)^(N-j)];end
+ plot2d1("onn",i',zt',[-2,6]);
+ xtitle(_("Simulation of a binomial random variable"));
+ current_axe = gca();
+ current_axe.title.font_size = 3;
+ current_axe.background = color(232,230,200);
+ legend([_("Simulation");_("Theory")]);
+
+ drawnow();
+
+endfunction
+
+BinomialT();
+clear BinomialT;
+
diff --git a/modules/randlib/demos/discrete.dem.sce b/modules/randlib/demos/discrete.dem.sce
new file mode 100755
index 000000000..0b174c4bc
--- /dev/null
+++ b/modules/randlib/demos/discrete.dem.sce
@@ -0,0 +1,54 @@
+// 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] = RndDiscT(n)
+
+ function [y] = RndDisc(m,n,x,p)
+ // discrete law random number
+ // sum p_i delta_{x_i}
+ //-------------------------------
+ p1 = [0, p];
+ p1 = cumsum(p1);
+ y = rand(m, n, "uniform");
+ N = prod(size(x));
+ res = 0*ones(m*n);
+ for i=1:N,z=0*ones(m*n,1),id=find( p1(i) <= y & y < p1(i+1) ),
+ z(id) = x(i)*ones(prod(size(id))),res=res+z;
+ end
+ y = matrix(res, m, n);
+ endfunction
+
+ [lhs,rhs] = argn(0)
+ if rhs <= 0 ; n = 10000;end
+ x = [1,3,4,6,10,12];
+ pr = [0.1,0.2,0.3,0.2,0.1,0.1];
+ y = RndDisc(1,n,x,pr);
+ i = 0:13
+ z = [];
+ for i1=i, z=[z,prod(size(find(y==i1)))],end
+
+ drawlater();
+
+ my_handle = scf(100001);
+ clf(my_handle, "reset");
+ demo_viewCode("discrete.dem.sce");
+
+ plot2d3("onn",i',z'/n,[1,3],,,[0,0,14,0.5]);
+ plot2d1("onn",x',pr',[-2,6]);
+ xtitle(_("Simulation of a discrete random variable"));
+ current_axe = gca();
+ current_axe.title.font_size = 3;
+ current_axe.background = color(232,230,200);
+ legend([_("Simulation"); _("Theory")]);
+
+ drawnow();
+endfunction
+
+RndDiscT();
+clear RndDiscT;
+
diff --git a/modules/randlib/demos/discrete_uni.dem.sce b/modules/randlib/demos/discrete_uni.dem.sce
new file mode 100755
index 000000000..4e1d3f4dc
--- /dev/null
+++ b/modules/randlib/demos/discrete_uni.dem.sce
@@ -0,0 +1,46 @@
+// 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]=RndIntT(n)
+
+ function [y] = RndInt(m,n,imin,imax)
+ // discrete uniform random number
+ //-------------------------------
+ y = rand(m, n, "uniform");
+ y = int(floor(y*(imax+1-imin)+ imin ));
+ endfunction
+
+ [lhs,rhs] = argn(0)
+ if rhs <= 0 ; n = 10000;end
+ imin = -10;
+ imax = 10;
+ y = RndInt(1,n,-10,10);
+ i = imin-2:imax+2;
+ z = [];
+ for i1=i, z=[z,prod(size(find(y==i1)))],end
+
+ drawlater();
+ my_handle = scf(100001);
+ clf(my_handle,"reset");
+ demo_viewCode("discrete_uni.dem.sce");
+ plot2d3("onn",i',z'/n,[1,2],,,[-12,0,12,0.1]);
+ i1=(imin:imax)';
+ plot2d1("onn",i1,ones(i1)/prod(size(imin:imax)),[-2,5]);
+
+ xtitle(_("Simulation of a discrete uniform random variable"));
+ current_axe = gca();
+ current_axe.title.font_size = 3;
+ current_axe.background = color(232,230,200);
+ legend([_("Simulation");_("Theory")]);
+ drawnow();
+
+endfunction
+
+RndIntT();
+clear RndIntT;
+
diff --git a/modules/randlib/demos/erlang.dem.sce b/modules/randlib/demos/erlang.dem.sce
new file mode 100755
index 000000000..c50dfa71c
--- /dev/null
+++ b/modules/randlib/demos/erlang.dem.sce
@@ -0,0 +1,54 @@
+// 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 [y]=ErlangT(n)
+
+ function [y] = Erlang(m, n, pMean, pVariance)
+ k = int( (pMean * pMean ) / pVariance + 0.5 );
+ if (k <= 0) then k = 1;end
+ a = k / pMean;
+ // we use blocks of size 100 to avoid overflows
+ res = [];
+ ntir = 100;
+ ntirc = ntir;
+ y = rand(ntir, k, "uniform");
+ y = -log(prod(y, "r"))/a;
+ res = [res; y];
+ while ( ntirc < m*n )
+ y = rand(ntir, k, "uniform");
+ y = -log(prod(y, "r"))/a;
+ res = [res; y];
+ ntirc = ntirc + ntir;
+ end
+ y = matrix(res(1:m*n), m, n);
+ endfunction
+
+ [lhs, rhs] = argn(0);
+ if rhs <= 0 ; n = 10000;end
+ y = Erlang(1, n, 10, 1);
+
+ drawlater();
+ my_handle = scf(100001);
+ clf(my_handle, "reset");
+ demo_viewCode("erlang.dem.sce");
+ histplot(20, y, [1,1], "061");
+ xtitle(_("Simulation of a Erlang random variable"));
+ BackgroundColorId = color(232,230,200);
+ PolylineFillColorId = color(179,179,0);
+ current_axe = gca();
+ current_axe.title.font_size = 3;
+ current_axe.background = color(232,230,200);
+ current_axe.children.children.fill_mode = "on";
+ current_axe.children.children.background = PolylineFillColorId;
+ current_axe.grid = [-1 PolylineFillColorId];
+ drawnow();
+endfunction
+
+ErlangT();
+clear Erlang;
+clear ErlangT;
diff --git a/modules/randlib/demos/exp.dem.sce b/modules/randlib/demos/exp.dem.sce
new file mode 100755
index 000000000..ec4d5fe4e
--- /dev/null
+++ b/modules/randlib/demos/exp.dem.sce
@@ -0,0 +1,47 @@
+// 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 [] = ExpT(n)
+
+ function [y]=Exp(m,n,lambda)
+ // lambda exp(-lambda x) x>=0
+ // ---------------------------
+ y=(-1/lambda)* log(rand(m,n,"uniform"));
+ endfunction
+
+ // lambda exp(-lambda x) x>=0
+ // ---------------------------
+ [lhs,rhs] = argn(0);
+ if rhs <= 0 ; n = 1000;end
+ lambda = 3;
+ y = Exp(1, n, lambda);
+ drawlater();
+ my_handle = scf(100001);
+ clf(my_handle, "reset");
+ demo_viewCode("exp.dem.sce");
+ histplot([0:0.1:10],y,[1,1],"051"," ",[0,0,5,3]);
+ deff("[y]=f(x)","y=lambda*exp(-lambda*x);");
+ x=[0:0.1:10]';
+ plot2d(x, f(x), 1, "000");
+ titre= _("Simulation of an exponential random variable");
+ xtitle(titre,_("Classes"),"N(C)/Nmax");
+ PolylineFillColorId = color(179,179,0);
+ current_axe = gca();
+ current_axe.title.font_size = 3;
+ current_axe.background = color(232,230,200);
+ current_axe.children(2).children.fill_mode = "on";
+ current_axe.children(2).children.background = PolylineFillColorId;
+ current_axe.grid = [-1 PolylineFillColorId];
+ legend([_("Simulation");_("Theory")]);
+ drawnow();
+endfunction
+
+ExpT();
+clear ExpT;
+
diff --git a/modules/randlib/demos/geometric.dem.sce b/modules/randlib/demos/geometric.dem.sce
new file mode 100755
index 000000000..77b22acdf
--- /dev/null
+++ b/modules/randlib/demos/geometric.dem.sce
@@ -0,0 +1,53 @@
+// 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 []=GeomT(n)
+
+ function [y]=Geom(m,n,p)
+ // P(0)= 0 P(i) = p*(1-p)^{n-1} P(inf)=0
+ // E = 1/p ; sig2= (1-p)/p^2
+ //--------------------------------------
+ if p >= 1 then disp(_("p must be < 1"));end
+ y = 0*ones(m, n);
+ for i=1:m*n,
+ samples = 1;
+ z = rand(1,1,"uniform");
+ while( z < 1-p) ,z=rand(1,1,"uniform"); samples=samples+1;end
+ y(i) = samples;
+ end
+ y = matrix(y, m, n);
+ endfunction
+
+ [lhs, rhs] = argn(0);
+ if rhs <= 0 ; n = 10000;end
+ pr = 0.2;
+ y = Geom(1, n, pr);
+ 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("geometric.dem.sce");
+ plot2d3("onn", i', z'/n, [1,3]);
+ zt = [0];
+ for i1=1:N; zt=[zt,pr*(1-pr)^(i1-1)];end
+ plot2d1("onn", i', zt', [-2,6]);
+ xtitle(_("Simulation of a geometric random variable"));
+ current_axe = gca();
+ current_axe.title.font_size = 3;
+ current_axe.background = color(232,230,200);
+ legend([_("Simulation");_("Theory")]);
+ drawnow();
+endfunction
+
+GeomT();
+clear GeomT;
diff --git a/modules/randlib/demos/hypergeom.dem.sce b/modules/randlib/demos/hypergeom.dem.sce
new file mode 100755
index 000000000..69c2efac1
--- /dev/null
+++ b/modules/randlib/demos/hypergeom.dem.sce
@@ -0,0 +1,45 @@
+// 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 [] = HyperGeomT(n)
+
+ function [y] = HyperGeom(m, n, Mean, var)
+ z = var / (Mean * Mean);
+ pP = 0.5 * (1.0 - sqrt((z - 1.0) / ( z + 1.0 )));
+ y = rand(m, n, "uniform")
+ zz = find( y > pP) ;
+ y = pP * ones(y);
+ y(zz) = (1-pP) * ones(zz);
+ y1 = rand(m, n, "uniform")
+ y = -Mean * log(y1) ./ (2.0 * y) ;
+ endfunction
+
+ [lhs,rhs]=argn(0)
+ if rhs <= 0 ; n=10000;end
+ y=HyperGeom(1,n,1,10)
+
+ drawlater();
+ my_handle = scf(100001);
+ clf(my_handle,"reset");
+ demo_viewCode("hypergeom.dem.sce");
+ histplot([0:0.25:10], y, [1,1], "061", " ", [0,0,10,0.4]);
+ xtitle(_("Simulation of a hyper geometric random variable"));
+ BackgroundColorId = color(232,230,200);
+ PolylineFillColorId = color(179,179,0);
+ current_axe = gca();
+ current_axe.title.font_size = 3;
+ current_axe.background = color(232,230,200);
+ current_axe.children.children.fill_mode = "on";
+ current_axe.children.children.background = PolylineFillColorId;
+ current_axe.grid = [-1 PolylineFillColorId];
+ drawnow();
+
+endfunction
+
+HyperGeomT();
+clear HyperGeomT;
diff --git a/modules/randlib/demos/poisson.dem.sce b/modules/randlib/demos/poisson.dem.sce
new file mode 100755
index 000000000..bab33da2b
--- /dev/null
+++ b/modules/randlib/demos/poisson.dem.sce
@@ -0,0 +1,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;
diff --git a/modules/randlib/demos/randlib.dem.gateway.sce b/modules/randlib/demos/randlib.dem.gateway.sce
new file mode 100755
index 000000000..d4c530555
--- /dev/null
+++ b/modules/randlib/demos/randlib.dem.gateway.sce
@@ -0,0 +1,27 @@
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) ????-2008 - INRIA
+// Copyright (C) 2010 - DIGITEO - Allan CORNET
+//
+// This file is released under the 3-clause BSD license. See COPYING-BSD.
+
+function subdemolist = demo_gateway()
+
+ demopath = get_absolute_file_path("randlib.dem.gateway.sce");
+ add_demo(_("Random"), demopath + "randlib.dem.gateway.sce");
+
+ subdemolist = [_("binomial random variable") ,"binomial.dem.sce" ; ..
+ _("discrete random variable") ,"discrete.dem.sce" ; ..
+ _("discrete uniform random variable"),"discrete_uni.dem.sce" ; ..
+ _("geometric random variable") ,"geometric.dem.sce" ; ..
+ _("Poisson random variable") ,"poisson.dem.sce" ; ..
+ _("Exponential random variable") ,"exp.dem.sce" ; ..
+ _("Weibull random variable") ,"weibull.dem.sce" ; ..
+ _("Hyper geometric random variable") ,"hypergeom.dem.sce" ; ..
+ _("Erlang random variable") ,"erlang.dem.sce" ];
+
+ subdemolist(:,2) = demopath + subdemolist(:,2);
+
+endfunction
+
+subdemolist = demo_gateway();
+clear demo_gateway;
diff --git a/modules/randlib/demos/weibull.dem.sce b/modules/randlib/demos/weibull.dem.sce
new file mode 100755
index 000000000..335881e01
--- /dev/null
+++ b/modules/randlib/demos/weibull.dem.sce
@@ -0,0 +1,40 @@
+// 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 [] = WeibullT(n)
+
+ function [y] = Weibull(m, n, Alpha, Beta)
+ //-------------------------------
+ y = rand(m, n, "uniform");
+ y = (Beta*( - log(1-y))).^(1/Alpha);
+ endfunction
+
+ [lhs, rhs] = argn(0);
+ if rhs <= 0 ; n = 10000;end
+ y = Weibull(1, n, 1, 2);
+
+ drawlater();
+ my_handle = scf(100001);
+ clf(my_handle, "reset");
+ demo_viewCode("weibull.dem.sce");
+ histplot(20, y, [1,1], "061");
+ xtitle(_("Simulation of a Weibull random variable"));
+ BackgroundColorId = color(232,230,200);
+ PolylineFillColorId = color(179,179,0);
+ current_axe = gca();
+ current_axe.title.font_size = 3;
+ current_axe.background = color(232,230,200);
+ current_axe.children.children.fill_mode = "on";
+ current_axe.children.children.background = PolylineFillColorId;
+ current_axe.grid = [-1 PolylineFillColorId];
+ drawnow();
+
+endfunction
+
+WeibullT();
+clear WeibullT;