1 ## Copyright (C) 2012 Rik Wehbring
2 ## Copyright (C) 1995-2012 Kurt Hornik
4 ## This file is part of Octave.
6 ## Octave is free software; you can redistribute it and/or modify it
7 ## under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 3 of the License, or (at
9 ## your option) any later version.
11 ## Octave is distributed in the hope that it will be useful, but
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ## General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with Octave; see the file COPYING. If not, see
18 ## <http://www.gnu.org/licenses/>.
21 ## @deftypefn {Function File} {} normcdf (@var{x})
22 ## @deftypefnx {Function File} {} normcdf (@var{x}, @var{mu}, @var{sigma})
23 ## For each element of @var{x}, compute the cumulative distribution
24 ## function (CDF) at @var{x} of the normal distribution with mean
25 ## @var{mu} and standard deviation @var{sigma}.
27 ## Default values are @var{mu} = 0, @var{sigma} = 1.
30 ## Author: TT <Teresa.Twaroch@ci.tuwien.ac.at>
31 ## Description: CDF of the normal distribution
33 function cdf = normcdf (x, mu = 0, sigma = 1)
35 if (nargin != 1 && nargin != 3)
39 if (!isscalar (mu) || !isscalar (sigma))
40 [retval, x, mu, sigma] = common_size (x, mu, sigma);
42 error ("normcdf: X, MU, and SIGMA must be of common size or scalars");
46 if (iscomplex (x) || iscomplex (mu) || iscomplex (sigma))
47 error ("normcdf: X, MU, and SIGMA must not be complex");
50 if (isa (x, "single") || isa (mu, "single") || isa (sigma, "single"));
51 cdf = zeros (size (x), "single");
53 cdf = zeros (size (x));
56 if (isscalar (mu) && isscalar (sigma))
57 if (!isinf (mu) && !isnan (mu) && (sigma > 0) && (sigma < Inf))
58 cdf = stdnormal_cdf ((x - mu) / sigma);
60 cdf = NaN (size (x), class (cdf));
63 k = isinf (mu) | isnan (mu) | !(sigma > 0) | !(sigma < Inf);
67 cdf(k) = stdnormal_cdf ((x(k) - mu(k)) ./ sigma(k));
74 %! x = [-Inf 1 2 Inf];
75 %! y = [0, 0.5, 1/2*(1+erf(1/sqrt(2))), 1];
76 %!assert(normcdf (x, ones(1,4), ones(1,4)), y);
77 %!assert(normcdf (x, 1, ones(1,4)), y);
78 %!assert(normcdf (x, ones(1,4), 1), y);
79 %!assert(normcdf (x, [0 -Inf NaN Inf], 1), [y(1) NaN NaN NaN]);
80 %!assert(normcdf (x, 1, [Inf NaN -1 0]), [NaN NaN NaN NaN]);
81 %!assert(normcdf ([x(1:2) NaN x(4)], 1, 1), [y(1:2) NaN y(4)]);
83 %% Test class of input preserved
84 %!assert(normcdf ([x, NaN], 1, 1), [y, NaN]);
85 %!assert(normcdf (single([x, NaN]), 1, 1), single([y, NaN]), eps("single"));
86 %!assert(normcdf ([x, NaN], single(1), 1), single([y, NaN]), eps("single"));
87 %!assert(normcdf ([x, NaN], 1, single(1)), single([y, NaN]), eps("single"));
89 %% Test input validation
92 %!error normcdf (1,2,3,4)
93 %!error normcdf (ones(3),ones(2),ones(2))
94 %!error normcdf (ones(2),ones(3),ones(2))
95 %!error normcdf (ones(2),ones(2),ones(3))
96 %!error normcdf (i, 2, 2)
97 %!error normcdf (2, i, 2)
98 %!error normcdf (2, 2, i)