7017
|
1 ## Copyright (C) 1995, 1996, 1997, 2005, 2006, 2007 Kurt Hornik |
5410
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
5410
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
5410
|
18 |
|
19 ## -*- texinfo -*- |
5411
|
20 ## @deftypefn {Function File} {} geopdf (@var{x}, @var{p}) |
5410
|
21 ## For each element of @var{x}, compute the probability density function |
|
22 ## (PDF) at @var{x} of the geometric distribution with parameter @var{p}. |
|
23 ## @end deftypefn |
|
24 |
5428
|
25 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
5410
|
26 ## Description: PDF of the geometric distribution |
|
27 |
5411
|
28 function pdf = geopdf (x, p) |
5410
|
29 |
|
30 if (nargin != 2) |
6046
|
31 print_usage (); |
5410
|
32 endif |
|
33 |
|
34 if (!isscalar (x) && !isscalar (p)) |
|
35 [retval, x, p] = common_size (x, p); |
|
36 if (retval > 0) |
5411
|
37 error ("geopdf: x and p must be of common size or scalar"); |
5410
|
38 endif |
|
39 endif |
|
40 |
|
41 pdf = zeros (size (x)); |
|
42 |
|
43 k = find (isnan (x) | !(p >= 0) | !(p <= 1)); |
|
44 if (any (k)) |
|
45 pdf(k) = NaN; |
|
46 endif |
|
47 |
|
48 ## Just for the fun of it ... |
|
49 k = find ((x == Inf) & (p == 0)); |
|
50 if (any (k)) |
|
51 pdf(k) = 1; |
|
52 endif |
|
53 |
|
54 k = find ((x >= 0) & (x < Inf) & (x == round (x)) & (p > 0) & (p <= 1)); |
|
55 if (any (k)) |
|
56 if (isscalar (x)) |
|
57 pdf(k) = p(k) .* ((1 - p(k)) .^ x); |
|
58 elseif (isscalar (p)) |
|
59 pdf(k) = p .* ((1 - p) .^ x(k)); |
|
60 else |
|
61 pdf(k) = p(k) .* ((1 - p(k)) .^ x(k)); |
|
62 endif |
|
63 endif |
|
64 |
|
65 endfunction |