comparison scripts/statistics/distributions/normpdf.m @ 6617:55da54f6c5c2

[project @ 2007-05-14 16:17:46 by jwe]
author jwe
date Mon, 14 May 2007 16:17:46 +0000
parents 34f96dd5441b
children 93c65f2a5668
comparison
equal deleted inserted replaced
6616:66e30383481b 6617:55da54f6c5c2
16 ## along with Octave; see the file COPYING. If not, write to the Free 16 ## along with Octave; see the file COPYING. If not, write to the Free
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 ## 02110-1301, USA. 18 ## 02110-1301, USA.
19 19
20 ## -*- texinfo -*- 20 ## -*- texinfo -*-
21 ## @deftypefn {Function File} {} normpdf (@var{x}, @var{m}, @var{v}) 21 ## @deftypefn {Function File} {} normpdf (@var{x}, @var{m}, @var{s})
22 ## For each element of @var{x}, compute the probability density function 22 ## For each element of @var{x}, compute the probability density function
23 ## (PDF) at @var{x} of the normal distribution with mean @var{m} and 23 ## (PDF) at @var{x} of the normal distribution with mean @var{m} and
24 ## variance @var{v}. 24 ## standard deviation @var{s}.
25 ## 25 ##
26 ## Default values are @var{m} = 0, @var{v} = 1. 26 ## Default values are @var{m} = 0, @var{s} = 1.
27 ## @end deftypefn 27 ## @end deftypefn
28 28
29 ## Author: TT <Teresa.Twaroch@ci.tuwien.ac.at> 29 ## Author: TT <Teresa.Twaroch@ci.tuwien.ac.at>
30 ## Description: PDF of the normal distribution 30 ## Description: PDF of the normal distribution
31 31
32 function pdf = normpdf (x, m, v) 32 function pdf = normpdf (x, m, s)
33 33
34 if (nargin != 1 && nargin != 3) 34 if (nargin != 1 && nargin != 3)
35 print_usage (); 35 print_usage ();
36 endif 36 endif
37 37
38 if (nargin == 1) 38 if (nargin == 1)
39 m = 0; 39 m = 0;
40 v = 1; 40 s = 1;
41 endif 41 endif
42 42
43 if (!isscalar (m) || !isscalar(v)) 43 if (!isscalar (m) || !isscalar (s))
44 [retval, x, m, v] = common_size (x, m, v); 44 [retval, x, m, s] = common_size (x, m, s);
45 if (retval > 0) 45 if (retval > 0)
46 error ("normpdf: x, m and v must be of common size or scalars"); 46 error ("normpdf: x, m and s must be of common size or scalars");
47 endif 47 endif
48 endif 48 endif
49 49
50 sz = size (x); 50 sz = size (x);
51 pdf = zeros (sz); 51 pdf = zeros (sz);
52 52
53 if (isscalar (m) && isscalar(v)) 53 if (isscalar (m) && isscalar (s))
54 if (find (isinf (m) | isnan (m) | !(v >= 0) | !(v < Inf))) 54 if (find (isinf (m) | isnan (m) | !(s >= 0) | !(s < Inf)))
55 pdf = NaN * ones (sz); 55 pdf = NaN * ones (sz);
56 else 56 else
57 pdf = stdnormal_pdf ((x - m) ./ sqrt (v)) ./ sqrt (v); 57 pdf = stdnormal_pdf ((x - m) ./ s) ./ s;
58 endif 58 endif
59 else 59 else
60 k = find (isinf (m) | isnan (m) | !(v >= 0) | !(v < Inf)); 60 k = find (isinf (m) | isnan (m) | !(s >= 0) | !(s < Inf));
61 if (any (k)) 61 if (any (k))
62 pdf(k) = NaN; 62 pdf(k) = NaN;
63 endif 63 endif
64 64
65 k = find (!isinf (m) & !isnan (m) & (v >= 0) & (v < Inf)); 65 k = find (!isinf (m) & !isnan (m) & (s >= 0) & (s < Inf));
66 if (any (k)) 66 if (any (k))
67 pdf(k) = stdnormal_pdf ((x(k) - m(k)) ./ sqrt (v(k))) ./ sqrt (v(k)); 67 pdf(k) = stdnormal_pdf ((x(k) - m(k)) ./ s(k)) ./ s(k);
68 endif 68 endif
69 endif 69 endif
70 70
71 pdf((v == 0) & (x == m)) = Inf; 71 pdf((s == 0) & (x == m)) = Inf;
72 pdf((v == 0) & ((x < m) | (x > m))) = 0; 72 pdf((s == 0) & ((x < m) | (x > m))) = 0;
73 73
74 endfunction 74 endfunction