Mercurial > hg > octave-terminal
changeset 8977:f464119ec165
further simplify some stats funcs
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Fri, 13 Mar 2009 21:26:13 +0100 |
parents | 22a7e4690742 |
children | 0a58c4cd1405 |
files | scripts/ChangeLog scripts/statistics/base/center.m scripts/statistics/base/cov.m scripts/statistics/base/std.m scripts/statistics/base/var.m |
diffstat | 5 files changed, 39 insertions(+), 43 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -2,6 +2,10 @@ * statistics/base/mean.m: Simplify. * statistics/base/meansq.m: Optimize. + * statistics/base/center.m: Fix behvaior with vectors, simplify. + * statistics/base/std.m: Simplify using `center'. + * statistics/base/var.m: Ditto. + * statistics/base/cov.m: Ditto. 2009-03-13 Jaroslav Hajek <highegg@gmail.com>
--- a/scripts/statistics/base/center.m +++ b/scripts/statistics/base/center.m @@ -29,30 +29,34 @@ ## Author: KH <Kurt.Hornik@wu-wien.ac.at> ## Description: Center by subtracting means -function retval = center (x, varargin) +function retval = center (x, dim) if (nargin != 1 && nargin != 2) print_usage (); endif - if (isvector (x)) - retval = x - mean (x, varargin{:}); - elseif (ismatrix (x)) - if nargin < 2 - dim = find (size (x) > 1, 1); - if (isempty (dim)) - dim = 1; - endif; + if (nargin < 2) + t = find (size (x) != 1); + if (isempty (t)) + dim = 1; else - dim = varargin{1}; + dim = t(1); endif - sz = ones (1, ndims (x)); - sz (dim) = size (x, dim); - retval = x - repmat (mean (x, dim), sz); - elseif (isempty (x)) + endif + n = size (x, dim); + + if (n == 1) + retval = zeros (size (x)); + elseif (n > 0) + if (isvector (x)) + retval = x - sum (x) / n; + else + mx = sum (x, dim) / n; + idx(1:ndims (x)) = {':'}; + idx{dim} = ones (1, n); + retval = x - mx(idx{:}); + endif + else retval = x; - else - error ("center: x must be a vector or a matrix"); endif - endfunction
--- a/scripts/statistics/base/cov.m +++ b/scripts/statistics/base/cov.m @@ -57,11 +57,11 @@ if (rows (y) != n) error ("cov: x and y must have the same number of observations"); endif - x = x - ones (n, 1) * sum (x) / n; - y = y - ones (n, 1) * sum (y) / n; + x = center (x, 1); + y = center (y, 1); c = conj (x' * y / (n - 1)); elseif (nargin == 1) - x = x - ones (n, 1) * sum (x) / n; + x = center (x, 1); c = conj (x' * x / (n - 1)); endif
--- a/scripts/statistics/base/std.m +++ b/scripts/statistics/base/std.m @@ -76,19 +76,13 @@ opt = 0; endif - sz = size(a); - if (sz (dim) == 1) - retval = zeros(sz); + n = size (a, dim); + if (n == 1) + retval = zeros (sz); elseif (numel (a) > 0) - rng = ones (1, length (sz)); - rng (dim) = sz (dim); - if (opt == 0) - retval = sqrt (sumsq (a - repmat(mean (a, dim), rng), dim) / (sz(dim) - 1)); - else - retval = sqrt (sumsq (a - repmat(mean (a, dim), rng), dim) / sz(dim)); - endif + retval = sqrt (sumsq (center (a, dim), dim) / (n + opt - 1)); else - error ("std: invalid matrix argument"); + error ("std: x must not be empty"); endif endfunction
--- a/scripts/statistics/base/var.m +++ b/scripts/statistics/base/var.m @@ -56,19 +56,13 @@ opt = 0; endif - sz = size (x); - if (prod (sz) < 1) - error ("var: x must not be empty"); - elseif (sz(dim) == 1) - y = 0; + n = size (a, dim); + if (n == 1) + retval = zeros (sz); + elseif (numel (a) > 0) + retval = sumsq (center (a, dim), dim) / (n + opt - 1); else - rng = ones (1, length (sz)); - rng (dim) = sz (dim); - if (opt == 0) - y = sumsq (x - repmat(mean (x, dim), rng), dim) / (sz(dim) - 1); - else - y = sumsq (x - repmat(mean (x, dim), rng), dim) / sz(dim); - endif + error ("var: x must not be empty"); endif endfunction