diff scripts/statistics/base/moment.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 72c96de7a403
line wrap: on
line diff
--- a/scripts/statistics/base/moment.m	Sat May 07 14:30:20 2011 -0700
+++ b/scripts/statistics/base/moment.m	Sat May 07 14:52:08 2011 -0700
@@ -110,27 +110,27 @@
 
 function m = moment (x, p, opt1, opt2)
 
-  if ((nargin < 2) || (nargin > 4))
+  if (nargin < 2 || nargin > 4)
     print_usage ();
   endif
 
-  if (!isnumeric(x) || isempty(x) )
+  if (!(isnumeric (x) || islogical (x)) || isempty (x))
     error ("moment: X must be a non-empty numeric matrix or vector");
   endif
 
-  if (!(isnumeric(p) && isscalar(p)))
+  if (! (isnumeric (p) && isscalar (p)))
     error ("moment: P must be a numeric scalar");
   endif
 
-  need_dim = 0;
+  need_dim = false;
 
   if (nargin == 2)
     type = "";
-    need_dim = 1;
+    need_dim = true;
   elseif (nargin == 3)
     if (ischar (opt1))
       type = opt1;
-      need_dim = 1;
+      need_dim = true;
     else
       dim = opt1;
       type = "";
@@ -151,10 +151,7 @@
   sz = size (x);
   if (need_dim)
     ## Find the first non-singleton dimension.
-    dim = find (sz > 1, 1);
-    if (isempty (dim))
-      dim = 1;
-    endif
+    (dim = find (sz > 1, 1)) || (dim = 1);
   else
     if (!(isscalar (dim) && dim == fix (dim)) ||
         !(1 <= dim && dim <= nd))
@@ -164,10 +161,8 @@
 
   n = sz(dim);
 
-  if any (type == "c")
-    rng = ones (1, length (sz));
-    rng(dim) = sz(dim);
-    x = x - repmat (sum (x, dim), rng) / n;
+  if (any (type == "c"))
+    x = center (x, dim);
   endif
   if any (type == "a")
     x = abs (x);
@@ -178,11 +173,21 @@
 endfunction
 
 
+%!test
+%! x = rand (10);
+%! assert (moment (x,1), mean (x), 1e1*eps);
+%! assert (moment (x,2), meansq (x), 1e1*eps);
+%! assert (moment (x,1,2), mean (x,2), 1e1*eps);
+%! assert (moment (x,1,'c'), mean (center (x)), 1e1*eps);
+%! assert (moment (x,1,'a'), mean (abs (x)), 1e1*eps);
+
+%!assert (moment (single([1 2 3]),1), single(2));
+
 %% Test input validation
 %!error moment ()
 %!error moment (1)
 %!error moment (1, 2, 3, 4, 5)
-%!error moment ([true true], 2)
+%!error moment (['A'; 'B'], 2)
 %!error moment (ones(2,0,3), 2)
 %!error moment (1, true)
 %!error moment (1, ones(2,2))