Mercurial > octave
annotate scripts/statistics/discrete_pdf.m @ 30330:01de0045b2e3
maint: Shorten some long lines to <= 80 characters (bug #57599)
* audiorecorder.m, disable_diagonal_matrix.m, disable_permutation_matrix.m,
disable_range.m, atan2d.m, accumdim.m, bitcmp.m, blkdiag.m, cplxpair.m,
dblquad.m, integral2.m, integral3.m, interp2.m, interpn.m, logspace.m,
delaunayn.m, griddata3.m, imread.m, genvarname.m, strread.m, nthargout.m,
parseparams.m, lsqnonneg.m, qp.m, sqp.m, material.m, __gnuplot_legend__.m,
plot.m, __gnuplot_drawnow__.m, polygcd.m, polyvalm.m, setdiff.m, fftfilt.m,
eigs.m, betainc.m, cosint.m, factor.m, gallery.m, toeplitz.m, discrete_cdf.m,
discrete_inv.m, discrete_pdf.m, iqr.m, kurtosis.m, quantile.m, bin2dec.m,
isstrprop.m, mat2str.m, strchr.m, strsplit.m, __have_feature__.m, datenum.m,
datevec.m, eomday.m, weboptions.m:
maint: Shorten some long lines to <= 80 characters.
author | Rik <rik@octave.org> |
---|---|
date | Tue, 23 Nov 2021 13:28:34 -0800 |
parents | 7854d5752dd2 |
children | 796f54d4ddbf |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 ######################################################################## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 ## |
29358
0a5b15007766
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
27984
diff
changeset
|
3 ## Copyright (C) 1996-2021 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
4 ## |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 ## See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 ## distribution or <https://octave.org/copyright/>. |
3426 | 7 ## |
3922 | 8 ## This file is part of Octave. |
9 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23084
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
3922 | 11 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23084
diff
changeset
|
12 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 ## (at your option) any later version. |
3426 | 14 ## |
3922 | 15 ## Octave is distributed in the hope that it will be useful, but |
3191 | 16 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
18 ## GNU General Public License for more details. |
3426 | 19 ## |
3191 | 20 ## You should have received a copy of the GNU General Public License |
7016 | 21 ## along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23084
diff
changeset
|
22 ## <https://www.gnu.org/licenses/>. |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 ## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 ######################################################################## |
3191 | 25 |
3456 | 26 ## -*- texinfo -*- |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20174
diff
changeset
|
27 ## @deftypefn {} {} discrete_pdf (@var{x}, @var{v}, @var{p}) |
20174
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
28 ## For each element of @var{x}, compute the probability density function (PDF) |
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
29 ## at @var{x} of a univariate discrete distribution which assumes the values |
d9341b422488
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
30 ## in @var{v} with probabilities @var{p}. |
3456 | 31 ## @end deftypefn |
3191 | 32 |
3456 | 33 function pdf = discrete_pdf (x, v, p) |
3426 | 34 |
3191 | 35 if (nargin != 3) |
6046 | 36 print_usage (); |
3191 | 37 endif |
38 | |
4030 | 39 if (! isvector (v)) |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
11156
diff
changeset
|
40 error ("discrete_pdf: V must be a vector"); |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
41 elseif (any (isnan (v))) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
42 error ("discrete_pdf: V must not have any NaN elements"); |
4030 | 43 elseif (! isvector (p) || (length (p) != length (v))) |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
11156
diff
changeset
|
44 error ("discrete_pdf: P must be a vector with length (V) elements"); |
3456 | 45 elseif (! (all (p >= 0) && any (p))) |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
46 error ("discrete_pdf: P must be a nonzero, non-negative vector"); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
47 endif |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
48 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
49 ## Reshape and normalize probability vector. Values not in table get 0 prob. |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
50 p = [0 ; p(:)/sum(p)]; |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
51 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
52 if (isa (x, "single") || isa (v, "single") || isa (p, "single")) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
53 pdf = NaN (size (x), "single"); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
54 else |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
55 pdf = NaN (size (x)); |
3191 | 56 endif |
57 | |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
58 k = ! isnan (x); |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
59 [vs, vi] = sort (v(:)); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
60 pdf(k) = p([0 ; vi](lookup (vs, x(k), 'm') + 1) + 1); |
3191 | 61 |
62 endfunction | |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
63 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
64 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
65 %!shared x,v,p,y |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
66 %! x = [-1 0.1 1.1 1.9 3]; |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
67 %! v = 0.1:0.2:1.9; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
68 %! p = 1/length (v) * ones (1, length (v)); |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
69 %! y = [0 0.1 0.1 0.1 0]; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
70 %!assert (discrete_pdf ([x, NaN], v, p), [y, NaN], 5*eps) |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
71 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
72 ## Test class of input preserved |
30330
01de0045b2e3
maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
73 %!assert (discrete_pdf (single ([x, NaN]), v, p), |
01de0045b2e3
maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
74 %! single ([y, NaN]), 5*eps ("single")) |
01de0045b2e3
maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
75 %!assert (discrete_pdf ([x, NaN], single (v), p), |
01de0045b2e3
maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
76 %! single ([y, NaN]), 5*eps ("single")) |
01de0045b2e3
maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
77 %!assert (discrete_pdf ([x, NaN], v, single (p)), |
01de0045b2e3
maint: Shorten some long lines to <= 80 characters (bug #57599)
Rik <rik@octave.org>
parents:
29359
diff
changeset
|
78 %! single ([y, NaN]), 5*eps ("single")) |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
79 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
80 ## Test input validation |
28886
d8318c12d903
test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents:
27984
diff
changeset
|
81 %!error <Invalid call> discrete_pdf () |
d8318c12d903
test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents:
27984
diff
changeset
|
82 %!error <Invalid call> discrete_pdf (1) |
d8318c12d903
test: remove unnecessary BIST tests in m-files checking for excessive number of inputs.
Rik <rik@octave.org>
parents:
27984
diff
changeset
|
83 %!error <Invalid call> discrete_pdf (1,2) |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
84 %!error discrete_pdf (1, ones (2), ones (2,1)) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
85 %!error discrete_pdf (1, [1 ; NaN], ones (2,1)) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
86 %!error discrete_pdf (1, ones (2,1), ones (1,1)) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
87 %!error discrete_pdf (1, ones (2,1), [1 -1]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
88 %!error discrete_pdf (1, ones (2,1), [1 NaN]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
89 %!error discrete_pdf (1, ones (2,1), [0 0]) |