PageRenderTime 64ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/octave-3.6.2/scripts/statistics/distributions/poissrnd.m

#
Objective C | 122 lines | 106 code | 16 blank | 0 comment | 18 complexity | 4d07a4941adc2951e5d4477de62af4ae MD5 | raw file
Possible License(s): GPL-3.0
  1. ## Copyright (C) 2012 Rik Wehbring
  2. ## Copyright (C) 1995-2012 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. ## -*- texinfo -*-
  20. ## @deftypefn {Function File} {} poissrnd (@var{lambda})
  21. ## @deftypefnx {Function File} {} poissrnd (@var{lambda}, @var{r})
  22. ## @deftypefnx {Function File} {} poissrnd (@var{lambda}, @var{r}, @var{c}, @dots{})
  23. ## @deftypefnx {Function File} {} poissrnd (@var{lambda}, [@var{sz}])
  24. ## Return a matrix of random samples from the Poisson distribution with
  25. ## parameter @var{lambda}.
  26. ##
  27. ## When called with a single size argument, return a square matrix with
  28. ## the dimension specified. When called with more than one scalar argument the
  29. ## first two arguments are taken as the number of rows and columns and any
  30. ## further arguments specify additional matrix dimensions. The size may also
  31. ## be specified with a vector of dimensions @var{sz}.
  32. ##
  33. ## If no size arguments are given then the result matrix is the size of
  34. ## @var{lambda}.
  35. ## @end deftypefn
  36. ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
  37. ## Description: Random deviates from the Poisson distribution
  38. function rnd = poissrnd (lambda, varargin)
  39. if (nargin < 1)
  40. print_usage ();
  41. endif
  42. if (nargin == 1)
  43. sz = size (lambda);
  44. elseif (nargin == 2)
  45. if (isscalar (varargin{1}) && varargin{1} >= 0)
  46. sz = [varargin{1}, varargin{1}];
  47. elseif (isrow (varargin{1}) && all (varargin{1} >= 0))
  48. sz = varargin{1};
  49. else
  50. error ("poissrnd: dimension vector must be row vector of non-negative integers");
  51. endif
  52. elseif (nargin > 2)
  53. if (any (cellfun (@(x) (!isscalar (x) || x < 0), varargin)))
  54. error ("poissrnd: dimensions must be non-negative integers");
  55. endif
  56. sz = [varargin{:}];
  57. endif
  58. if (!isscalar (lambda) && !isequal (size (lambda), sz))
  59. error ("poissrnd: LAMBDA must be scalar or of size SZ");
  60. endif
  61. if (iscomplex (lambda))
  62. error ("poissrnd: LAMBDA must not be complex");
  63. endif
  64. if (isa (lambda, "single"))
  65. cls = "single";
  66. else
  67. cls = "double";
  68. endif
  69. if (isscalar (lambda))
  70. if (lambda >= 0 && lambda < Inf)
  71. rnd = randp (lambda, sz);
  72. if (strcmp (cls, "single"))
  73. rnd = single (rnd);
  74. endif
  75. else
  76. rnd = NaN (sz, cls);
  77. endif
  78. else
  79. rnd = NaN (sz, cls);
  80. k = (lambda >= 0) & (lambda < Inf);
  81. rnd(k) = randp (lambda(k));
  82. endif
  83. endfunction
  84. %!assert(size (poissrnd (2)), [1, 1]);
  85. %!assert(size (poissrnd (ones(2,1))), [2, 1]);
  86. %!assert(size (poissrnd (ones(2,2))), [2, 2]);
  87. %!assert(size (poissrnd (1, 3)), [3, 3]);
  88. %!assert(size (poissrnd (1, [4 1])), [4, 1]);
  89. %!assert(size (poissrnd (1, 4, 1)), [4, 1]);
  90. %% Test class of input preserved
  91. %!assert(class (poissrnd (2)), "double");
  92. %!assert(class (poissrnd (single(2))), "single");
  93. %!assert(class (poissrnd (single([2 2]))), "single");
  94. %% Test input validation
  95. %!error poissrnd ()
  96. %!error poissrnd (1, -1)
  97. %!error poissrnd (1, ones(2))
  98. %!error poissrnd (1, 2, ones(2))
  99. %!error poissrnd (i)
  100. %!error poissrnd (1, 2, -1)
  101. %!error poissrnd (1, [2 -1 2])
  102. %!error poissrnd (ones(2,2), 3)
  103. %!error poissrnd (ones(2,2), [3, 2])
  104. %!error poissrnd (ones(2,2), 2, 3)
  105. %!assert (poissrnd (0, 1, 1), 0)
  106. %!assert (poissrnd ([0, 0, 0], [1, 3]), [0 0 0])