Mercurial > hg > octave-nkf
diff scripts/statistics/base/cov.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 | c792872f8942 |
children | cad4cba03f19 |
line wrap: on
line diff
--- a/scripts/statistics/base/cov.m +++ b/scripts/statistics/base/cov.m @@ -67,7 +67,8 @@ print_usage (); endif - if (! (isnumeric (x) && isnumeric (y))) + if ( ! (isnumeric (x) || islogical (x)) + || ! (isnumeric (y) || islogical (y))) error ("cov: X and Y must be numeric matrices or vectors"); endif @@ -75,7 +76,7 @@ error ("cov: X and Y must be 2-D matrices or vectors"); endif - if (nargin == 2 && isscalar(y)) + if (nargin == 2 && isscalar (y)) opt = y; endif @@ -83,22 +84,27 @@ error ("cov: normalization OPT must be 0 or 1"); endif + ## Special case, scalar has zero covariance if (isscalar (x)) - c = 0; + if (isa (x, 'single')) + c = single (0); + else + c = 0; + endif return; endif - if (rows (x) == 1) - x = x'; + if (isrow (x)) + x = x.'; endif n = rows (x); - if (nargin == 1 || isscalar(y)) + if (nargin == 1 || isscalar (y)) x = center (x, 1); c = conj (x' * x / (n - 1 + opt)); else - if (rows (y) == 1) - y = y'; + if (isrow (y)) + y = y.'; endif if (rows (y) != n) error ("cov: X and Y must have the same number of observations"); @@ -110,17 +116,36 @@ endfunction + %!test %! x = rand (10); %! cx1 = cov (x); %! cx2 = cov (x, x); -%! assert(size (cx1) == [10, 10] && size (cx2) == [10, 10] && norm(cx1-cx2) < 1e1*eps); +%! assert(size (cx1) == [10, 10] && size (cx2) == [10, 10]); +%! assert(cx1, cx2, 1e1*eps); + +%!test +%! x = [1:3]'; +%! y = [3:-1:1]'; +%! assert (cov (x,y), -1, 5*eps) +%! assert (cov (x,flipud (y)), 1, 5*eps) +%! assert (cov ([x, y]), [1 -1; -1 1], 5*eps) + +%!test +%! x = single ([1:3]'); +%! y = single ([3:-1:1]'); +%! assert (cov (x,y), single (-1), 5*eps) +%! assert (cov (x,flipud (y)), single (1), 5*eps) +%! assert (cov ([x, y]), single ([1 -1; -1 1]), 5*eps) %!test %! x = [1:5]; %! c = cov (x); -%! assert(isscalar (c)); -%! assert(c, 2.5); +%! assert (isscalar (c)); +%! assert (c, 2.5); + +%!assert(cov (5), 0); +%!assert(cov (single(5)), single(0)); %!test %! x = [1:5]; @@ -129,13 +154,10 @@ %! c = cov (x, 1); %! assert(c, 2); -%!assert(cov (5), 0); - %% Test input validation %!error cov (); %!error cov (1, 2, 3, 4); -%!error cov ([true, true]); -%!error cov ([1, 2], [true, true]); +%!error cov ([1; 2], ["A", "B"]); %!error cov (ones (2,2,2)); %!error cov (ones (2,2), ones (2,2,2)); %!error cov (1, 3);