Mercurial > hg > octave-lyh
comparison scripts/statistics/base/statistics.m @ 12656:6b2f14af2360
Overhaul functions in statistics/base directory.
Widen input validation to accept logicals.
Return correct class of output, e.g., 'single' depending on class of input.
Correct or add tests for above.
* center.m, cov.m, kendall.m, mean.m, meansq.m, median.m, mode.m, prctile.m,
quantile.m, ranks.m, run_count.m, runlength.m, spearman.m, statistics.m,
std.m, var.m, logistic_inv.m: Overhaul as described above
* corrcoef.m: Overhaul + remove input validation already done by cov().
* cor.m, logit.m, ppplot.m, table.m: Only align test blocks.
* gls.m, ols.m: Only correct class of output, no logical inputs for regression.
* histc.m: Only change spacing of code to be uniform.
* iqr.m: Overhaul + 2X speedup by calling empirical_inv just once.
* kurtosis.m: Overhaul + replace repmat instances with center().
* mahalanobis.m: Overhaul + use bsxfun for centering data.
* moment.m: Overhaul + replace repmat instances with center().
* probit.m, range.m: Redo input validation and add tests.
* skewness.m: Overhaul + replace repmat instances with center().
* zscore.m: Overhaul + replace repmat instances with center() + use bsxfun.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sat, 07 May 2011 14:52:08 -0700 |
parents | d0b799dafede |
children | 984359717d71 |
comparison
equal
deleted
inserted
replaced
12655:52d79740a76c | 12656:6b2f14af2360 |
---|---|
36 | 36 |
37 if (nargin != 1 && nargin != 2) | 37 if (nargin != 1 && nargin != 2) |
38 print_usage (); | 38 print_usage (); |
39 endif | 39 endif |
40 | 40 |
41 if (!isnumeric(x)) | 41 if (! (isnumeric (x) || islogical (x))) |
42 error ("statistics: X must be a numeric vector or matrix"); | 42 error ("statistics: X must be a numeric vector or matrix"); |
43 endif | 43 endif |
44 | 44 |
45 nd = ndims (x); | 45 nd = ndims (x); |
46 sz = size (x); | 46 sz = size (x); |
47 if (nargin != 2) | 47 if (nargin != 2) |
48 ## Find the first non-singleton dimension. | 48 ## Find the first non-singleton dimension. |
49 dim = find (sz > 1, 1); | 49 (dim = find (sz > 1, 1)) || (dim = 1); |
50 if (isempty (dim)) | |
51 dim = 1; | |
52 endif | |
53 else | 50 else |
54 if (!(isscalar (dim) && dim == round (dim)) | 51 if (!(isscalar (dim) && dim == round (dim)) |
55 || !(1 <= dim && dim <= nd)) | 52 || !(1 <= dim && dim <= nd)) |
56 error ("statistics: DIM must be an integer and a valid dimension"); | 53 error ("statistics: DIM must be an integer and a valid dimension"); |
57 endif | 54 endif |
66 stats = cat (dim, min (x, [], dim), emp_inv, max (x, [], dim), mean (x, dim), | 63 stats = cat (dim, min (x, [], dim), emp_inv, max (x, [], dim), mean (x, dim), |
67 std (x, [], dim), skewness (x, dim), kurtosis (x, dim)); | 64 std (x, [], dim), skewness (x, dim), kurtosis (x, dim)); |
68 | 65 |
69 endfunction | 66 endfunction |
70 | 67 |
68 | |
71 %!test | 69 %!test |
72 %! x = rand(7,5); | 70 %! x = rand (7,5); |
73 %! s = statistics (x); | 71 %! s = statistics (x); |
74 %! m = median (x); | 72 %! assert (min (x), s(1,:), eps); |
75 %! assert (m, s(3,:), eps); | 73 %! assert (median (x), s(3,:), eps); |
74 %! assert (max (x), s(5,:), eps); | |
75 %! assert (mean (x), s(6,:), eps); | |
76 %! assert (std (x), s(7,:), eps); | |
77 %! assert (skewness (x), s(8,:), eps); | |
78 %! assert (kurtosis (x), s(9,:), eps); | |
76 | 79 |
77 %% Test input validation | 80 %% Test input validation |
78 %!error statistics () | 81 %!error statistics () |
79 %!error statistics (1, 2, 3) | 82 %!error statistics (1, 2, 3) |
80 %!error statistics ([true true]) | 83 %!error statistics (['A'; 'B']) |
81 %!error statistics (1, ones(2,2)) | 84 %!error statistics (1, ones(2,2)) |
82 %!error statistics (1, 1.5) | 85 %!error statistics (1, 1.5) |
83 %!error statistics (1, 0) | 86 %!error statistics (1, 0) |
84 %!error statistics (1, 3) | 87 %!error statistics (1, 3) |
85 %!error statistics (1) | 88 %!error statistics (1) |