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
|
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) INRIA
//
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// you should have received as part of this distribution. The terms
// are also available at
// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
function [P] = binomial(p,N)
//
// PURPOSE
// Compute probabilities of the binomial law B(N,p)
//
// PARAMETERS
// p : a real in [0,1]
// N : an integer >= 0
// P : a row vector with N+1 components and :
//
// P(k+1) = P(X=k) = C(k,N) (1-p)^k p^(N-k)
//
// Rewritten by Bruno for a gain in speed (by using
// cdfbin which computes the cumulative probability)
//
if type(p) ~= 1 then
error(msprintf(gettext("%s: Wrong type for input argument #%d: A real expected.\n"),"binomial",1));
end
if type(N) ~= 1 then
error(msprintf(gettext("%s: Wrong type for input argument #%d: An integer expected.\n"),"binomial",2));
end
if length(p) ~= 1 then
error(msprintf(gettext("%s: Wrong size for input argument #%d: A real expected.\n"),"binomial",1));
end
if length(N) ~= 1 then
error(msprintf(gettext("%s: Wrong size for input argument #%d: An integer expected.\n"),"binomial",2));
end
if p < 0 | p > 1 then
error(msprintf(gettext("%s: Wrong value for input argument #%d: Must be between %d and %d.\n"),"binomial",1,0,1));
end
if N < 1 | floor(N)-N ~= 0 then
error(msprintf(gettext("%s: Wrong value for input argument #%d: Integer >= %d expected.\n"),"binomial",2,1));
end
un = ones(1,N+1);
P = cdfbin("PQ", 0:N, N*un, p*un, (1-p)*un)
P(2:N+1) = P(2:N+1) - P(1:N)
endfunction
|