PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/R2008-08-24/main/statistics/inst/random.m

#
MATLAB | 171 lines | 168 code | 3 blank | 0 comment | 28 complexity | 41c6b74a8de7a909dcfbc4c3d2d8397c MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, GPL-3.0, LGPL-3.0
  1. ## Copyright (C) 2007 Soren Hauberg
  2. ##
  3. ## This program is free software; you can redistribute it and/or modify
  4. ## it under the terms of the GNU General Public License as published by
  5. ## the Free Software Foundation; either version 3, or (at your option)
  6. ## any later version.
  7. ##
  8. ## This program is distributed in the hope that it will be useful, but
  9. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ## General Public License for more details.
  12. ##
  13. ## You should have received a copy of the GNU General Public License
  14. ## along with this file. If not, see <http://www.gnu.org/licenses/>.
  15. ## -*- texinfo -*-
  16. ## @deftypefn {Function File} @var{r} = random(@var{name}, @var{arg1})
  17. ## @deftypefnx{Function File} @var{r} = random(@var{name}, @var{arg1}, @var{arg2})
  18. ## @deftypefnx{Function File} @var{r} = random(@var{name}, @var{arg1}, @var{arg2}, @var{arg3})
  19. ## @deftypefnx{Function File} @var{r} = random(@var{name}, ..., @var{s1}, ...)
  20. ## Generates pseudo-random numbers from a given one-, two-, or three-parameter
  21. ## distribution.
  22. ##
  23. ## The variable @var{name} must be a string that names the distribution from
  24. ## which to sample. If this distribution is a one-parameter distribution @var{arg1}
  25. ## should be supplied, if it is a two-paramter distribution @var{arg2} must also
  26. ## be supplied, and if it is a three-parameter distribution @var{arg3} must also
  27. ## be present. Any arguments following the distribution paramters will determine
  28. ## the size of the result.
  29. ##
  30. ## As an example, the following code generates a 10 by 20 matrix containing
  31. ## random numbers from a normal distribution with mean 5 and standard deviation
  32. ## 2.
  33. ## @example
  34. ## R = random("normal", 5, 2, [10, 20]);
  35. ## @end example
  36. ##
  37. ## The variable @var{name} can be one of the following strings
  38. ##
  39. ## @table @asis
  40. ## @item "beta"
  41. ## @itemx "beta distribution"
  42. ## Samples are drawn from the Beta distribution.
  43. ## @item "bino"
  44. ## @itemx "binomial"
  45. ## @itemx "binomial distribution"
  46. ## Samples are drawn from the Binomial distribution.
  47. ## @item "chi2"
  48. ## @itemx "chi-square"
  49. ## @itemx "chi-square distribution"
  50. ## Samples are drawn from the Chi-Square distribution.
  51. ## @item "exp"
  52. ## @itemx "exponential"
  53. ## @itemx "exponential distribution"
  54. ## Samples are drawn from the Exponential distribution.
  55. ## @item "f"
  56. ## @itemx "f distribution"
  57. ## Samples are drawn from the F distribution.
  58. ## @item "gam"
  59. ## @itemx "gamma"
  60. ## @itemx "gamma distribution"
  61. ## Samples are drawn from the Gamma distribution.
  62. ## @item "geo"
  63. ## @itemx "geometric"
  64. ## @itemx "geometric distribution"
  65. ## Samples are drawn from the Geometric distribution.
  66. ## @item "hyge"
  67. ## @itemx "hypergeometric"
  68. ## @itemx "hypergeometric distribution"
  69. ## Samples are drawn from the Hypergeometric distribution.
  70. ## @item "logn"
  71. ## @itemx "lognormal"
  72. ## @itemx "lognormal distribution"
  73. ## Samples are drawn from the Log-Normal distribution.
  74. ## @item "nbin"
  75. ## @itemx "negative binomial"
  76. ## @itemx "negative binomial distribution"
  77. ## Samples are drawn from the Negative Binomial distribution.
  78. ## @item "norm"
  79. ## @itemx "normal"
  80. ## @itemx "normal distribution"
  81. ## Samples are drawn from the Normal distribution.
  82. ## @item "poiss"
  83. ## @itemx "poisson"
  84. ## @itemx "poisson distribution"
  85. ## Samples are drawn from the Poisson distribution.
  86. ## @item "rayl"
  87. ## @itemx "rayleigh"
  88. ## @itemx "rayleigh distribution"
  89. ## Samples are drawn from the Rayleigh distribution.
  90. ## @item "t"
  91. ## @itemx "t distribution"
  92. ## Samples are drawn from the T distribution.
  93. ## @item "unif"
  94. ## @itemx "uniform"
  95. ## @itemx "uniform distribution"
  96. ## Samples are drawn from the Uniform distribution.
  97. ## @item "unid"
  98. ## @itemx "discrete uniform"
  99. ## @itemx "discrete uniform distribution"
  100. ## Samples are drawn from the Uniform Discrete distribution.
  101. ## @item "wbl"
  102. ## @itemx "weibull"
  103. ## @itemx "weibull distribution"
  104. ## Samples are drawn from the Weibull distribution.
  105. ## @end table
  106. ## @seealso{rand, betarnd, binornd, chi2rnd, exprnd, frnd, gamrnd, geornd, hygernd,
  107. ## lognrnd, nbinrnd, normrnd, poissrnd, raylrnd, trnd, unifrnd, unidrnd, wblrnd}
  108. ## @end deftypefn
  109. function retval = random(name, varargin)
  110. ## General input checking
  111. if (nargin < 2)
  112. print_usage();
  113. endif
  114. if (!ischar(name))
  115. error("random: first input argument must be a string");
  116. endif
  117. ## Select distribution
  118. switch (lower(name))
  119. case {"beta", "beta distribution"}
  120. retval = betarnd(varargin{:});
  121. case {"bino", "binomial", "binomial distribution"}
  122. retval = binornd(varargin{:});
  123. case {"chi2", "chi-square", "chi-square distribution"}
  124. retval = chi2rnd(varargin{:});
  125. case {"exp", "exponential", "exponential distribution"}
  126. retval = exprnd(varargin{:});
  127. case {"ev", "extreme value", "extreme value distribution"}
  128. error("random: distribution type '%s' is not yet implemented", name);
  129. case {"f", "f distribution"}
  130. retval = frnd(varargin{:});
  131. case {"gam", "gamma", "gamma distribution"}
  132. retval = gamrnd(varargin{:});
  133. case {"gev", "generalized extreme value", "generalized extreme value distribution"}
  134. error("random: distribution type '%s' is not yet implemented", name);
  135. case {"gp", "generalized pareto", "generalized pareto distribution"}
  136. error("random: distribution type '%s' is not yet implemented", name);
  137. case {"geo", "geometric", "geometric distribution"}
  138. retval = geornd(varargin{:});
  139. case {"hyge", "hypergeometric", "hypergeometric distribution"}
  140. retval = hygernd(varargin{:});
  141. case {"logn", "lognormal", "lognormal distribution"}
  142. retval = lognrnd(varargin{:});
  143. case {"nbin", "negative binomial", "negative binomial distribution"}
  144. retval = nbinrnd(varargin{:});
  145. case {"ncf", "noncentral f", "noncentral f distribution"}
  146. error("random: distribution type '%s' is not yet implemented", name);
  147. case {"nct", "noncentral t", "noncentral t distribution"}
  148. error("random: distribution type '%s' is not yet implemented", name);
  149. case {"ncx2", "noncentral chi-square", "noncentral chi-square distribution"}
  150. error("random: distribution type '%s' is not yet implemented", name);
  151. case {"norm", "normal", "normal distribution"}
  152. retval = normrnd(varargin{:});
  153. case {"poiss", "poisson", "poisson distribution"}
  154. retval = poissrnd(varargin{:});
  155. case {"rayl", "rayleigh", "rayleigh distribution"}
  156. retval = raylrnd(varargin{:});
  157. case {"t", "t distribution"}
  158. retval = trnd(varargin{:});
  159. case {"unif", "uniform", "uniform distribution"}
  160. retval = unifrnd(varargin{:});
  161. case {"unid", "discrete uniform", "discrete uniform distribution"}
  162. retval = unidrnd(varargin{:});
  163. case {"wbl", "weibull", "weibull distribution"}
  164. retval = wblrnd(varargin{:});
  165. otherwise
  166. error("random: unsupported distribution type '%s'", name);
  167. endswitch
  168. endfunction