From help-octave-request at bevo dot che dot wisc dot edu Tue Nov 12 11:46:30 2002 Subject: weighted var, etc. (was "var(x) and std(x)") From: Mike Miller To: Francesco Potorti` cc: Help-Octave List Date: Tue, 12 Nov 2002 11:46:12 -0600 (CST) On Tue, 12 Nov 2002, Francesco Potorti` wrote: > If you compute the mean from the samples, this is correct. If you know > the mean a priori, then you should divide by n. The code you cite > computes the mean from the samples, so that's okay. That's true. A related issue (question): Is there an Octave procedure for computing weighted means and variances? If we have a vector x of values and a corresponding (same size) vector p of probabilities, the mean of x is p'*x and the variance is p'*(x-p'*x). If length(x)=n, and p=ones(n,1)/n, then p'*(x-p'*x) = ((n-1)/n)*var(x). If appropriate functions don't already exist, I would recommend the following names: wmean, wvar, wstd and wcov. I would recommend that they work like this: y=wmean(x,p) <--where p is a matrix the same size as x y=wmean(x,p) <--where p is column vector with length(p)=size(x,1) y=wmean(x) <--where p is set to p=ones(size(x,1),1)/size(x,1) wmean would compute weighted means of the columns of matrix x using the weights given in matrix (or vector) p. The same system would be used for wvar, etc. This approach has the nice side effect that wvar(x) provides var(x)*(n-1)/n saving the user a few keystrokes. wmean(x)=mean(x). Has this already been done? Mike ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------