annotate scripts/statistics/distributions/normpdf.m @ 12749:e7cc2d4a6db3 stable

Fix range of sigma in normal distribution to exclude 0. * normcdf.m, normpdf.m: Correct 's >= 0' to 's > 0'.
author Rik <octave@nomad.inbox5.com>
date Thu, 16 Jun 2011 21:05:10 -0700
parents fd0a3ac60b0e
children 19b9f17d22af
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11523
fd0a3ac60b0e update copyright notices
John W. Eaton <jwe@octave.org>
parents: 11472
diff changeset
1 ## Copyright (C) 1995-2011 Kurt Hornik
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
2 ##
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
3 ## This file is part of Octave.
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
4 ##
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6617
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6617
diff changeset
8 ## your option) any later version.
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
9 ##
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
13 ## General Public License for more details.
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
14 ##
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6617
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6617
diff changeset
17 ## <http://www.gnu.org/licenses/>.
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
18
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
19 ## -*- texinfo -*-
6617
55da54f6c5c2 [project @ 2007-05-14 16:17:46 by jwe]
jwe
parents: 6046
diff changeset
20 ## @deftypefn {Function File} {} normpdf (@var{x}, @var{m}, @var{s})
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
21 ## For each element of @var{x}, compute the probability density function
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
22 ## (PDF) at @var{x} of the normal distribution with mean @var{m} and
6617
55da54f6c5c2 [project @ 2007-05-14 16:17:46 by jwe]
jwe
parents: 6046
diff changeset
23 ## standard deviation @var{s}.
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
24 ##
6617
55da54f6c5c2 [project @ 2007-05-14 16:17:46 by jwe]
jwe
parents: 6046
diff changeset
25 ## Default values are @var{m} = 0, @var{s} = 1.
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
26 ## @end deftypefn
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
27
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
28 ## Author: TT <Teresa.Twaroch@ci.tuwien.ac.at>
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
29 ## Description: PDF of the normal distribution
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
30
6617
55da54f6c5c2 [project @ 2007-05-14 16:17:46 by jwe]
jwe
parents: 6046
diff changeset
31 function pdf = normpdf (x, m, s)
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
32
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
33 if (nargin != 1 && nargin != 3)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5411
diff changeset
34 print_usage ();
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
35 endif
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
36
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
37 if (nargin == 1)
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
38 m = 0;
6617
55da54f6c5c2 [project @ 2007-05-14 16:17:46 by jwe]
jwe
parents: 6046
diff changeset
39 s = 1;
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
40 endif
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
41
6617
55da54f6c5c2 [project @ 2007-05-14 16:17:46 by jwe]
jwe
parents: 6046
diff changeset
42 if (!isscalar (m) || !isscalar (s))
55da54f6c5c2 [project @ 2007-05-14 16:17:46 by jwe]
jwe
parents: 6046
diff changeset
43 [retval, x, m, s] = common_size (x, m, s);
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
44 if (retval > 0)
11472
1740012184f9 Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents: 10525
diff changeset
45 error ("normpdf: X, M and S must be of common size or scalars");
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
46 endif
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
47 endif
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
48
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
49 sz = size (x);
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
50 pdf = zeros (sz);
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
51
6617
55da54f6c5c2 [project @ 2007-05-14 16:17:46 by jwe]
jwe
parents: 6046
diff changeset
52 if (isscalar (m) && isscalar (s))
12749
e7cc2d4a6db3 Fix range of sigma in normal distribution to exclude 0.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
53 if (find (isinf (m) | isnan (m) | !(s > 0) | !(s < Inf)))
10525
3306cfcb856e Replace constructs like "NaN * one()" with "NaN()" and "Inf * ones ()" with "Inf()"
David Bateman <dbateman@free.fr>
parents: 7017
diff changeset
54 pdf = NaN (sz);
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
55 else
6617
55da54f6c5c2 [project @ 2007-05-14 16:17:46 by jwe]
jwe
parents: 6046
diff changeset
56 pdf = stdnormal_pdf ((x - m) ./ s) ./ s;
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
57 endif
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
58 else
12749
e7cc2d4a6db3 Fix range of sigma in normal distribution to exclude 0.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
59 k = find (isinf (m) | isnan (m) | !(s > 0) | !(s < Inf));
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
60 if (any (k))
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
61 pdf(k) = NaN;
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
62 endif
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
63
12749
e7cc2d4a6db3 Fix range of sigma in normal distribution to exclude 0.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
64 k = find (!isinf (m) & !isnan (m) & (s > 0) & (s < Inf));
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
65 if (any (k))
6617
55da54f6c5c2 [project @ 2007-05-14 16:17:46 by jwe]
jwe
parents: 6046
diff changeset
66 pdf(k) = stdnormal_pdf ((x(k) - m(k)) ./ s(k)) ./ s(k);
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
67 endif
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
68 endif
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
69
6617
55da54f6c5c2 [project @ 2007-05-14 16:17:46 by jwe]
jwe
parents: 6046
diff changeset
70 pdf((s == 0) & (x == m)) = Inf;
55da54f6c5c2 [project @ 2007-05-14 16:17:46 by jwe]
jwe
parents: 6046
diff changeset
71 pdf((s == 0) & ((x < m) | (x > m))) = 0;
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
72
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
73 endfunction