Mercurial > octave-nkf
comparison scripts/statistics/distributions/empirical_pdf.m @ 18194:1461b9cfac4e
empirical_pdf.m: Fix calculation when distribution has ties (bug #41008).
empirical_pdf.m: Use histc to calculate relative frequency when the the same
value occurs multiple times in the data.
author | Rik <rik@octave.org> |
---|---|
date | Thu, 02 Jan 2014 13:23:51 -0800 |
parents | d63878346099 |
children | 4197fc428c7d |
comparison
equal
deleted
inserted
replaced
18193:d3c6842ef188 | 18194:1461b9cfac4e |
---|---|
35 | 35 |
36 if (! isvector (data)) | 36 if (! isvector (data)) |
37 error ("empirical_pdf: DATA must be a vector"); | 37 error ("empirical_pdf: DATA must be a vector"); |
38 endif | 38 endif |
39 | 39 |
40 pdf = discrete_pdf (x, data, ones (size (data))); | 40 uniq_vals = unique (data); |
41 if (numel (data) != numel (uniq_vals)) | |
42 ## Handle ties, multiple elements with same value | |
43 p = histc (data, uniq_vals); | |
44 data = uniq_vals; | |
45 else | |
46 p = ones (size (data)); | |
47 endif | |
48 | |
49 pdf = discrete_pdf (x, data, p); | |
41 | 50 |
42 endfunction | 51 endfunction |
43 | 52 |
44 | 53 |
45 %!shared x,v,y | 54 %!shared x,v,y |
50 | 59 |
51 %% Test class of input preserved | 60 %% Test class of input preserved |
52 %!assert (empirical_pdf (single (x), v), single (y)) | 61 %!assert (empirical_pdf (single (x), v), single (y)) |
53 %!assert (empirical_pdf (x, single (v)), single (y)) | 62 %!assert (empirical_pdf (x, single (v)), single (y)) |
54 | 63 |
64 %% Test distribution with ties | |
65 %!assert (empirical_pdf (2, [1 2 3 2]), 0.5) | |
66 | |
55 %% Test input validation | 67 %% Test input validation |
56 %!error empirical_pdf () | 68 %!error empirical_pdf () |
57 %!error empirical_pdf (1) | 69 %!error empirical_pdf (1) |
58 %!error empirical_pdf (1,2,3) | 70 %!error empirical_pdf (1,2,3) |
59 %!error empirical_inv (1, ones (2)) | 71 %!error empirical_inv (1, ones (2)) |