~ubuntu-branches/debian/sid/octave3.0/sid

« back to all changes in this revision

Viewing changes to scripts/statistics/distributions/geornd.m

  • Committer: Bazaar Package Importer
  • Author(s): Rafael Laboissiere
  • Date: 2007-12-23 16:04:15 UTC
  • Revision ID: james.westby@ubuntu.com-20071223160415-n4gk468dihy22e9v
Tags: upstream-3.0.0
ImportĀ upstreamĀ versionĀ 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
## Copyright (C) 1995, 1996, 1997, 2005, 2006, 2007 Kurt Hornik
 
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
 
7
## the Free Software Foundation; either version 3 of the License, or (at
 
8
## your option) any later version.
 
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
 
16
## along with Octave; see the file COPYING.  If not, see
 
17
## <http://www.gnu.org/licenses/>.
 
18
 
 
19
## -*- texinfo -*-
 
20
## @deftypefn {Function File} {} geornd (@var{p}, @var{r}, @var{c})
 
21
## @deftypefnx {Function File} {} geornd (@var{p}, @var{sz})
 
22
## Return an @var{r} by @var{c} matrix of random samples from the
 
23
## geometric distribution with parameter @var{p}, which must be a scalar
 
24
## or of size @var{r} by @var{c}.
 
25
##
 
26
## If @var{r} and @var{c} are given create a matrix with @var{r} rows and
 
27
## @var{c} columns. Or if @var{sz} is a vector, create a matrix of size
 
28
## @var{sz}.
 
29
## @end deftypefn
 
30
 
 
31
## Author: KH <Kurt.Hornik@wu-wien.ac.at>
 
32
## Description: Random deviates from the geometric distribution
 
33
 
 
34
function rnd = geornd (p, r, c)
 
35
 
 
36
  if (nargin == 3)
 
37
    if (! (isscalar (r) && (r > 0) && (r == round (r))))
 
38
      error ("geornd: r must be a positive integer");
 
39
    endif
 
40
    if (! (isscalar (c) && (c > 0) && (c == round (c))))
 
41
      error ("geornd: c must be a positive integer");
 
42
    endif
 
43
    sz = [r, c];
 
44
 
 
45
    if (any (size (p) != 1) && ((length (size (p)) != length (sz)) ||
 
46
                                any (size (p) != sz)))
 
47
      error ("geornd: p must be scalar or of size [r, c]");
 
48
    endif
 
49
  elseif (nargin == 2)
 
50
    if (isscalar (r) && (r > 0))
 
51
      sz = [r, r];
 
52
    elseif (isvector(r) && all (r > 0))
 
53
      sz = r(:)';
 
54
    else
 
55
      error ("geornd: r must be a positive integer or vector");
 
56
    endif
 
57
 
 
58
    if (any (size (p) != 1) && ((length (size (p)) != length (sz)) ||
 
59
                                any (size (p) != sz)))
 
60
      error ("geornd: n must be scalar or of size sz");
 
61
    endif
 
62
  elseif (nargin == 1)
 
63
    sz = size(n);
 
64
  elseif (nargin != 1)
 
65
    print_usage ();
 
66
  endif
 
67
 
 
68
 
 
69
  if (isscalar (p))
 
70
    if (!(p >= 0) || !(p <= 1))
 
71
      rnd = NaN * ones (sz);
 
72
    elseif (p == 0)
 
73
      rnd = Inf * ones (sz);
 
74
    elseif ((p > 0) & (p < 1));
 
75
      rnd = floor (- rande(sz) ./ log (1 - p));
 
76
    else
 
77
      rnd = zeros (sz);
 
78
    endif
 
79
  else
 
80
    rnd = floor (- rande(sz) ./ log (1 - p));
 
81
 
 
82
    k = find (!(p >= 0) | !(p <= 1));
 
83
    if (any (k))
 
84
      rnd(k) = NaN * ones (1, length (k));
 
85
    endif
 
86
 
 
87
    k = find (p == 0);
 
88
    if (any (k))
 
89
      rnd(k) = Inf * ones (1, length (k));
 
90
    endif
 
91
  endif
 
92
 
 
93
endfunction