PageRenderTime 5285ms CodeModel.GetById 40ms RepoModel.GetById 7ms app.codeStats 0ms

/scripts/statistics/distributions/normrnd.m

https://bitbucket.org/melior/octave
Objective C | 131 lines | 115 code | 16 blank | 0 comment | 25 complexity | 7402ec15cb94a1e6bca1dbac695087c7 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, GPL-3.0, LGPL-2.0, LGPL-2.1
  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} {} normrnd (@var{mu}, @var{sigma})
  21. ## @deftypefnx {Function File} {} normrnd (@var{mu}, @var{sigma}, @var{r})
  22. ## @deftypefnx {Function File} {} normrnd (@var{mu}, @var{sigma}, @var{r}, @var{c}, @dots{})
  23. ## @deftypefnx {Function File} {} normrnd (@var{mu}, @var{sigma}, [@var{sz}])
  24. ## Return a matrix of random samples from the normal distribution with
  25. ## parameters mean @var{mu} and standard deviation @var{sigma}.
  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 common size of
  34. ## @var{mu} and @var{sigma}.
  35. ## @end deftypefn
  36. ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
  37. ## Description: Random deviates from the normal distribution
  38. function rnd = normrnd (mu, sigma, varargin)
  39. if (nargin < 2)
  40. print_usage ();
  41. endif
  42. if (!isscalar (mu) || !isscalar (sigma))
  43. [retval, mu, sigma] = common_size (mu, sigma);
  44. if (retval > 0)
  45. error ("normrnd: mu and sigma must be of common size or scalars");
  46. endif
  47. endif
  48. if (iscomplex (mu) || iscomplex (sigma))
  49. error ("normrnd: MU and SIGMA must not be complex");
  50. endif
  51. if (nargin == 2)
  52. sz = size (mu);
  53. elseif (nargin == 3)
  54. if (isscalar (varargin{1}) && varargin{1} >= 0)
  55. sz = [varargin{1}, varargin{1}];
  56. elseif (isrow (varargin{1}) && all (varargin{1} >= 0))
  57. sz = varargin{1};
  58. else
  59. error ("normrnd: dimension vector must be row vector of non-negative integers");
  60. endif
  61. elseif (nargin > 3)
  62. if (any (cellfun (@(x) (!isscalar (x) || x < 0), varargin)))
  63. error ("normrnd: dimensions must be non-negative integers");
  64. endif
  65. sz = [varargin{:}];
  66. endif
  67. if (!isscalar (mu) && !isequal (size (mu), sz))
  68. error ("normrnd: mu and sigma must be scalar or of size SZ");
  69. endif
  70. if (isa (mu, "single") || isa (sigma, "single"))
  71. cls = "single";
  72. else
  73. cls = "double";
  74. endif
  75. if (isscalar (mu) && isscalar (sigma))
  76. if (!isnan (mu) && !isinf (mu) && (sigma > 0) && (sigma < Inf))
  77. rnd = mu + sigma * randn (sz);
  78. else
  79. rnd = NaN (sz, cls);
  80. endif
  81. else
  82. rnd = mu + sigma .* randn (sz);
  83. k = isnan (mu) | isinf (mu) | !(sigma > 0) | !(sigma < Inf);
  84. rnd(k) = NaN;
  85. endif
  86. endfunction
  87. %!assert (size (normrnd (1,2)), [1, 1])
  88. %!assert (size (normrnd (ones (2,1), 2)), [2, 1])
  89. %!assert (size (normrnd (ones (2,2), 2)), [2, 2])
  90. %!assert (size (normrnd (1, 2*ones (2,1))), [2, 1])
  91. %!assert (size (normrnd (1, 2*ones (2,2))), [2, 2])
  92. %!assert (size (normrnd (1, 2, 3)), [3, 3])
  93. %!assert (size (normrnd (1, 2, [4 1])), [4, 1])
  94. %!assert (size (normrnd (1, 2, 4, 1)), [4, 1])
  95. %% Test class of input preserved
  96. %!assert (class (normrnd (1, 2)), "double")
  97. %!assert (class (normrnd (single (1), 2)), "single")
  98. %!assert (class (normrnd (single ([1 1]), 2)), "single")
  99. %!assert (class (normrnd (1, single (2))), "single")
  100. %!assert (class (normrnd (1, single ([2 2]))), "single")
  101. %% Test input validation
  102. %!error normrnd ()
  103. %!error normrnd (1)
  104. %!error normrnd (ones (3), ones (2))
  105. %!error normrnd (ones (2), ones (3))
  106. %!error normrnd (i, 2)
  107. %!error normrnd (2, i)
  108. %!error normrnd (1,2, -1)
  109. %!error normrnd (1,2, ones (2))
  110. %!error normrnd (1, 2, [2 -1 2])
  111. %!error normrnd (1,2, 1, ones (2))
  112. %!error normrnd (1,2, 1, -1)
  113. %!error normrnd (ones (2,2), 2, 3)
  114. %!error normrnd (ones (2,2), 2, [3, 2])
  115. %!error normrnd (ones (2,2), 2, 2, 3)