~gabriel1984sibiu/octave/octave

« back to all changes in this revision

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

  • Committer: Grevutiu Gabriel
  • Date: 2014-01-02 13:05:54 UTC
  • Revision ID: gabriel1984sibiu@gmail.com-20140102130554-3r7ivdjln1ni6kcg
New version (3.8.0) from upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
## Copyright (C) 2012 Rik Wehbring
 
2
## Copyright (C) 1997-2013 Kurt Hornik
 
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
 
8
## the Free Software Foundation; either version 3 of the License, or (at
 
9
## your option) any later version.
 
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
 
17
## along with Octave; see the file COPYING.  If not, see
 
18
## <http://www.gnu.org/licenses/>.
 
19
 
 
20
## -*- texinfo -*-
 
21
## @deftypefn {Function File} {} hygecdf (@var{x}, @var{t}, @var{m}, @var{n})
 
22
## Compute the cumulative distribution function (CDF) at @var{x} of the
 
23
## hypergeometric distribution with parameters @var{t}, @var{m}, and
 
24
## @var{n}.  This is the probability of obtaining not more than @var{x}
 
25
## marked items when randomly drawing a sample of size @var{n} without
 
26
## replacement from a population of total size @var{t} containing
 
27
## @var{m} marked items.
 
28
##
 
29
## The parameters @var{t}, @var{m}, and @var{n} must be positive integers
 
30
## with @var{m} and @var{n} not greater than @var{t}.
 
31
## @end deftypefn
 
32
 
 
33
## Author: KH <Kurt.Hornik@wu-wien.ac.at>
 
34
## Description: CDF of the hypergeometric distribution
 
35
 
 
36
function cdf = hygecdf (x, t, m, n)
 
37
 
 
38
  if (nargin != 4)
 
39
    print_usage ();
 
40
  endif
 
41
 
 
42
  if (!isscalar (t) || !isscalar (m) || !isscalar (n))
 
43
    [retval, x, t, m, n] = common_size (x, t, m, n);
 
44
    if (retval > 0)
 
45
      error ("hygecdf: X, T, M, and N must be of common size or scalars");
 
46
    endif
 
47
  endif
 
48
 
 
49
  if (iscomplex (x) || iscomplex (t) || iscomplex (m) || iscomplex (n))
 
50
    error ("hygecdf: X, T, M, and N must not be complex");
 
51
  endif
 
52
 
 
53
  if (isa (x, "single") || isa (t, "single") || isa (m, "single") || isa (n, "single"))
 
54
    cdf = NaN (size (x), "single");
 
55
  else
 
56
    cdf = NaN (size (x));
 
57
  endif
 
58
 
 
59
  ok = ((t >= 0) & (m >= 0) & (n > 0) & (m <= t) & (n <= t) &
 
60
        (t == fix (t)) & (m == fix (m)) & (n == fix (n)));
 
61
 
 
62
  if (isscalar (t))
 
63
    if (ok)
 
64
      cdf = discrete_cdf (x, 0 : n, hygepdf (0 : n, t, m, n));
 
65
    endif
 
66
  else
 
67
    for i = find (ok(:)')  # Must be row vector arg to for loop
 
68
      v = 0 : n(i);
 
69
      cdf(i) = discrete_cdf (x(i), v, hygepdf (v, t(i), m(i), n(i)));
 
70
    endfor
 
71
  endif
 
72
 
 
73
endfunction
 
74
 
 
75
 
 
76
%!shared x,y
 
77
%! x = [-1 0 1 2 3];
 
78
%! y = [0 1/6 5/6 1 1];
 
79
%!assert (hygecdf (x, 4*ones (1,5), 2, 2), y, eps)
 
80
%!assert (hygecdf (x, 4, 2*ones (1,5), 2), y, eps)
 
81
%!assert (hygecdf (x, 4, 2, 2*ones (1,5)), y, eps)
 
82
%!assert (hygecdf (x, 4*[1 -1 NaN 1.1 1], 2, 2), [y(1) NaN NaN NaN y(5)], eps)
 
83
%!assert (hygecdf (x, 4, 2*[1 -1 NaN 1.1 1], 2), [y(1) NaN NaN NaN y(5)], eps)
 
84
%!assert (hygecdf (x, 4, 5, 2), [NaN NaN NaN NaN NaN])
 
85
%!assert (hygecdf (x, 4, 2, 2*[1 -1 NaN 1.1 1]), [y(1) NaN NaN NaN y(5)], eps)
 
86
%!assert (hygecdf (x, 4, 2, 5), [NaN NaN NaN NaN NaN])
 
87
%!assert (hygecdf ([x(1:2) NaN x(4:5)], 4, 2, 2), [y(1:2) NaN y(4:5)], eps)
 
88
 
 
89
%% Test class of input preserved
 
90
%!assert (hygecdf ([x, NaN], 4, 2, 2), [y, NaN], eps)
 
91
%!assert (hygecdf (single ([x, NaN]), 4, 2, 2), single ([y, NaN]), eps ("single"))
 
92
%!assert (hygecdf ([x, NaN], single (4), 2, 2), single ([y, NaN]), eps ("single"))
 
93
%!assert (hygecdf ([x, NaN], 4, single (2), 2), single ([y, NaN]), eps ("single"))
 
94
%!assert (hygecdf ([x, NaN], 4, 2, single (2)), single ([y, NaN]), eps ("single"))
 
95
 
 
96
%% Test input validation
 
97
%!error hygecdf ()
 
98
%!error hygecdf (1)
 
99
%!error hygecdf (1,2)
 
100
%!error hygecdf (1,2,3)
 
101
%!error hygecdf (1,2,3,4,5)
 
102
%!error hygecdf (ones (2), ones (3), 1, 1)
 
103
%!error hygecdf (1, ones (2), ones (3), 1)
 
104
%!error hygecdf (1, 1, ones (2), ones (3))
 
105
%!error hygecdf (i, 2, 2, 2)
 
106
%!error hygecdf (2, i, 2, 2)
 
107
%!error hygecdf (2, 2, i, 2)
 
108
%!error hygecdf (2, 2, 2, i)
 
109