Mercurial > hg > octave-lyh
annotate scripts/statistics/distributions/unidpdf.m @ 13171:19b9f17d22af
Overhaul of statistical distribution functions
Support class "single"
75% reduction in memory usage
More Matlab compatibility for corner cases
* betacdf.m, betainv.m, betapdf.m, betarnd.m, binocdf.m, binoinv.m, binopdf.m,
binornd.m, cauchy_cdf.m, cauchy_inv.m, cauchy_pdf.m, cauchy_rnd.m, chi2cdf.m,
chi2inv.m, chi2pdf.m, chi2rnd.m, discrete_cdf.m, discrete_inv.m,
discrete_pdf.m, discrete_rnd.m, empirical_cdf.m, empirical_inv.m,
empirical_pdf.m, empirical_rnd.m, expcdf.m, expinv.m, exppdf.m, exprnd.m,
fcdf.m, finv.m, fpdf.m, frnd.m, gamcdf.m, gaminv.m, gampdf.m, gamrnd.m,
geocdf.m, geoinv.m, geopdf.m, geornd.m, hygecdf.m, hygeinv.m, hygepdf.m,
hygernd.m, kolmogorov_smirnov_cdf.m, laplace_cdf.m, laplace_inv.m,
laplace_pdf.m, laplace_rnd.m, logistic_cdf.m, logistic_inv.m, logistic_pdf.m,
logistic_rnd.m, logncdf.m, logninv.m, lognpdf.m, lognrnd.m, nbincdf.m,
nbininv.m, nbinpdf.m, nbinrnd.m, normcdf.m, norminv.m, normpdf.m, normrnd.m,
poisscdf.m, poissinv.m, poisspdf.m, poissrnd.m, stdnormal_cdf.m,
stdnormal_inv.m, stdnormal_pdf.m, stdnormal_rnd.m, tcdf.m, tinv.m, tpdf.m,
trnd.m, unidcdf.m, unidinv.m, unidpdf.m, unidrnd.m, unifcdf.m, unifinv.m,
unifpdf.m, unifrnd.m, wblcdf.m, wblinv.m, wblpdf.m, wblrnd.m:
Return "single" outputs for "single" inputs,
Use logical indexing rather than find() for 75% memory savings,
Add tests for all functions,
Use consistent documentation across all functions,
More Matlab compatibilitcy for corner cases.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Tue, 20 Sep 2011 12:13:13 -0700 |
parents | 4675ce154a55 |
children | 72c96de7a403 |
rev | line source |
---|---|
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
1 ## Copyright (C) 2011 Rik Wehbring |
11523 | 2 ## Copyright (C) 2007-2011 David Bateman |
6356 | 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 | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
6356 | 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 | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
6356 | 19 |
20 ## -*- texinfo -*- | |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
21 ## @deftypefn {Function File} {} unidpdf (@var{x}, @var{n}) |
6356 | 22 ## For each element of @var{x}, compute the probability density function |
12495
4675ce154a55
Correctly refer to "discrete uniform" distribution in documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
23 ## (PDF) at @var{x} of a discrete uniform distribution which assumes |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
24 ## the integer values 1--@var{n} with equal probability. |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
25 ## |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
26 ## Warning: The underlying implementation uses the double class and |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
27 ## will only be accurate for @var{n} @leq{} @code{bitmax} |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
28 ## (@w{@math{2^{53} - 1}} on IEEE-754 compatible systems). |
6356 | 29 ## @end deftypefn |
30 | |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
31 function pdf = unidpdf (x, n) |
6356 | 32 |
33 if (nargin != 2) | |
34 print_usage (); | |
35 endif | |
36 | |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
37 if (! isscalar (n)) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
38 [retval, x, n] = common_size (x, n); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
39 if (retval > 0) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
40 error ("unidpdf: X and N must be of common size or scalars"); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
41 endif |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
42 endif |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
43 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
44 if (iscomplex (x) || iscomplex (n)) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
45 error ("unidpdf: X and N must not be complex"); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
46 endif |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
47 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
48 if (isa (x, "single") || isa (n, "single")) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
49 pdf = zeros (size (x), "single"); |
6356 | 50 else |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
51 pdf = zeros (size (x)); |
6356 | 52 endif |
53 | |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
54 k = isnan (x) | ! (n > 0 & n == fix (n)); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
55 pdf(k) = NaN; |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
56 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
57 k = !k & (x >= 1) & (x <= n) & (x == fix (x)); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
58 if (isscalar (n)) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
59 pdf(k) = 1 / n; |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
60 else |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
61 pdf(k) = 1 ./ n(k); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
62 endif |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
63 |
6356 | 64 endfunction |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
65 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
66 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
67 %!shared x,y |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
68 %! x = [-1 0 1 2 10 11]; |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
69 %! y = [0 0 0.1 0.1 0.1 0]; |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
70 %!assert(unidpdf (x, 10*ones(1,6)), y); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
71 %!assert(unidpdf (x, 10), y); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
72 %!assert(unidpdf (x, 10*[0 NaN 1 1 1 1]), [NaN NaN y(3:6)]); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
73 %!assert(unidpdf ([x, NaN], 10), [y, NaN]); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
74 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
75 %% Test class of input preserved |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
76 %!assert(unidpdf (single([x, NaN]), 10), single([y, NaN])); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
77 %!assert(unidpdf ([x, NaN], single(10)), single([y, NaN])); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
78 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
79 %% Test input validation |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
80 %!error unidpdf () |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
81 %!error unidpdf (1) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
82 %!error unidpdf (1,2,3) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
83 %!error unidpdf (ones(3),ones(2)) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
84 %!error unidpdf (ones(2),ones(3)) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
85 %!error unidpdf (i, 2) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
86 %!error unidpdf (2, i) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12495
diff
changeset
|
87 |