]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/distributions/empirical_rnd.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / distributions / empirical_rnd.m
1 ## Copyright (C) 2012 Rik Wehbring
2 ## Copyright (C) 1996-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
20 ## -*- texinfo -*-
21 ## @deftypefn  {Function File} {} empirical_rnd (@var{data})
22 ## @deftypefnx {Function File} {} empirical_rnd (@var{data}, @var{r})
23 ## @deftypefnx {Function File} {} empirical_rnd (@var{data}, @var{r}, @var{c}, @dots{})
24 ## @deftypefnx {Function File} {} empirical_rnd (@var{data}, [@var{sz}])
25 ## Return a matrix of random samples from the empirical distribution obtained
26 ## from the univariate sample @var{data}.
27 ##
28 ## When called with a single size argument, return a square matrix with
29 ## the dimension specified.  When called with more than one scalar argument the
30 ## first two arguments are taken as the number of rows and columns and any
31 ## further arguments specify additional matrix dimensions.  The size may also
32 ## be specified with a vector of dimensions @var{sz}.
33 ## 
34 ## If no size arguments are given then the result matrix is a random ordering
35 ## of the sample @var{data}.
36 ## @end deftypefn
37
38 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
39 ## Description: Bootstrap samples from the empirical distribution
40
41 function rnd = empirical_rnd (data, varargin)
42
43   if (nargin < 1)
44     print_usage ();
45   endif
46
47   if (! isvector (data))
48     error ("empirical_rnd: DATA must be a vector");
49   endif
50
51   rnd = discrete_rnd (data, ones (size (data)), varargin{:});
52
53 endfunction
54
55
56 %!assert(size (empirical_rnd (ones (3, 1))), [3, 1]);
57 %!assert(size (empirical_rnd (1:2, [4 1])), [4, 1]);
58 %!assert(size (empirical_rnd (1:2, 4, 1)), [4, 1]);
59
60 %% Test class of input preserved
61 %!assert(class (empirical_rnd (1:2, 1)), "double");
62 %!assert(class (empirical_rnd (single(1:2), 1)), "single");
63
64 %% Test input validation
65 %!error empirical_rnd ()
66 %!error empirical_rnd (ones(2), 1)
67 %% test data verification
68 %!error empirical_rnd (ones(2), 1, 1)
69