Mercurial > octave-nkf
annotate scripts/statistics/distributions/normcdf.m @ 13171:19b9f17d22af
Overhaul of statistical distribution functions
Support class "single"
75% reduction in memory usage
More Matlab compatibility for corner cases
* betacdf.m, betainv.m, betapdf.m, betarnd.m, binocdf.m, binoinv.m, binopdf.m,
binornd.m, cauchy_cdf.m, cauchy_inv.m, cauchy_pdf.m, cauchy_rnd.m, chi2cdf.m,
chi2inv.m, chi2pdf.m, chi2rnd.m, discrete_cdf.m, discrete_inv.m,
discrete_pdf.m, discrete_rnd.m, empirical_cdf.m, empirical_inv.m,
empirical_pdf.m, empirical_rnd.m, expcdf.m, expinv.m, exppdf.m, exprnd.m,
fcdf.m, finv.m, fpdf.m, frnd.m, gamcdf.m, gaminv.m, gampdf.m, gamrnd.m,
geocdf.m, geoinv.m, geopdf.m, geornd.m, hygecdf.m, hygeinv.m, hygepdf.m,
hygernd.m, kolmogorov_smirnov_cdf.m, laplace_cdf.m, laplace_inv.m,
laplace_pdf.m, laplace_rnd.m, logistic_cdf.m, logistic_inv.m, logistic_pdf.m,
logistic_rnd.m, logncdf.m, logninv.m, lognpdf.m, lognrnd.m, nbincdf.m,
nbininv.m, nbinpdf.m, nbinrnd.m, normcdf.m, norminv.m, normpdf.m, normrnd.m,
poisscdf.m, poissinv.m, poisspdf.m, poissrnd.m, stdnormal_cdf.m,
stdnormal_inv.m, stdnormal_pdf.m, stdnormal_rnd.m, tcdf.m, tinv.m, tpdf.m,
trnd.m, unidcdf.m, unidinv.m, unidpdf.m, unidrnd.m, unifcdf.m, unifinv.m,
unifpdf.m, unifrnd.m, wblcdf.m, wblinv.m, wblpdf.m, wblrnd.m:
Return "single" outputs for "single" inputs,
Use logical indexing rather than find() for 75% memory savings,
Add tests for all functions,
Use consistent documentation across all functions,
More Matlab compatibilitcy for corner cases.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Tue, 20 Sep 2011 12:13:13 -0700 |
parents | e7cc2d4a6db3 |
children | 72c96de7a403 |
rev | line source |
---|---|
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
1 ## Copyright (C) 2011 Rik Wehbring |
11523 | 2 ## Copyright (C) 1995-2011 Kurt Hornik |
5410 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
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 | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
5410 | 10 ## |
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. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
5410 | 19 |
20 ## -*- texinfo -*- | |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
21 ## @deftypefn {Function File} {} normcdf (@var{x}) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
22 ## @deftypefnx {Function File} {} normcdf (@var{x}, @var{mu}, @var{sigma}) |
5410 | 23 ## For each element of @var{x}, compute the cumulative distribution |
24 ## function (CDF) at @var{x} of the normal distribution with mean | |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
25 ## @var{mu} and standard deviation @var{sigma}. |
5410 | 26 ## |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
27 ## Default values are @var{mu} = 0, @var{sigma} = 1. |
5410 | 28 ## @end deftypefn |
29 | |
30 ## Author: TT <Teresa.Twaroch@ci.tuwien.ac.at> | |
31 ## Description: CDF of the normal distribution | |
32 | |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
33 function cdf = normcdf (x, mu = 0, sigma = 1) |
5410 | 34 |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
35 if (nargin != 1 && nargin != 3) |
6046 | 36 print_usage (); |
5410 | 37 endif |
38 | |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
39 if (!isscalar (mu) || !isscalar (sigma)) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
40 [retval, x, mu, sigma] = common_size (x, mu, sigma); |
5410 | 41 if (retval > 0) |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
42 error ("normcdf: X, MU, and SIGMA must be of common size or scalars"); |
5410 | 43 endif |
44 endif | |
45 | |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
46 if (iscomplex (x) || iscomplex (mu) || iscomplex (sigma)) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
47 error ("normcdf: X, MU, and SIGMA must not be complex"); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
48 endif |
5410 | 49 |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
50 if (isa (x, "single") || isa (mu, "single") || isa (sigma, "single")); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
51 cdf = zeros (size (x), "single"); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
52 else |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
53 cdf = zeros (size (x)); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
54 endif |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
55 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
56 if (isscalar (mu) && isscalar (sigma)) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
57 if (!isinf (mu) && !isnan (mu) && (sigma > 0) && (sigma < Inf)) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
58 cdf = stdnormal_cdf ((x - mu) / sigma); |
5410 | 59 else |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
60 cdf = NaN (size (x), class (cdf)); |
5410 | 61 endif |
62 else | |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
63 k = isinf (mu) | isnan (mu) | !(sigma > 0) | !(sigma < Inf); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
64 cdf(k) = NaN; |
5410 | 65 |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
66 k = ! k; |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
67 cdf(k) = stdnormal_cdf ((x(k) - mu(k)) ./ sigma(k)); |
5410 | 68 endif |
69 | |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
70 endfunction |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
71 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
72 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
73 %!shared x,y |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
74 %! x = [-Inf 1 2 Inf]; |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
75 %! y = [0, 0.5, 1/2*(1+erf(1/sqrt(2))), 1]; |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
76 %!assert(normcdf (x, ones(1,4), ones(1,4)), y); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
77 %!assert(normcdf (x, 1, ones(1,4)), y); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
78 %!assert(normcdf (x, ones(1,4), 1), y); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
79 %!assert(normcdf (x, [0 -Inf NaN Inf], 1), [y(1) NaN NaN NaN]); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
80 %!assert(normcdf (x, 1, [Inf NaN -1 0]), [NaN NaN NaN NaN]); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
81 %!assert(normcdf ([x(1:2) NaN x(4)], 1, 1), [y(1:2) NaN y(4)]); |
5410 | 82 |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
83 %% Test class of input preserved |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
84 %!assert(normcdf ([x, NaN], 1, 1), [y, NaN]); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
85 %!assert(normcdf (single([x, NaN]), 1, 1), single([y, NaN]), eps("single")); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
86 %!assert(normcdf ([x, NaN], single(1), 1), single([y, NaN]), eps("single")); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
87 %!assert(normcdf ([x, NaN], 1, single(1)), single([y, NaN]), eps("single")); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
88 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
89 %% Test input validation |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
90 %!error normcdf () |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
91 %!error normcdf (1,2) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
92 %!error normcdf (1,2,3,4) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
93 %!error normcdf (ones(3),ones(2),ones(2)) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
94 %!error normcdf (ones(2),ones(3),ones(2)) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
95 %!error normcdf (ones(2),ones(2),ones(3)) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
96 %!error normcdf (i, 2, 2) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
97 %!error normcdf (2, i, 2) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
98 %!error normcdf (2, 2, i) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12749
diff
changeset
|
99 |