1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) 2000 - INRIA - Carlos Klimann
3 // Copyright (C) 2013 - Samuel GOUGEON
5 // This file must be used under the terms of the CeCILL.
6 // This source file is licensed as described in the file COPYING, which
7 // you should have received as part of this distribution. The terms
8 // are also available at
9 // http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
12 function [s, m] = variance(x, orien, m)
14 //This function computes the variance of the values of a vector or
17 //For a vector or a matrix x, s=variance(x) returns in the scalar s the
18 //variance of all the entries of x.
20 //s=variance(x,'r') (or, equivalently, s=variance(x,1)) is the rowwise
21 //variance. It returns in each entry of the row vector s the variance of
24 //s=variance(x,'c') (or, equivalently, s=variance(x,2)) is the columnwise standard
25 //deviation. It returns in each entry of the column vector y the
26 //variance of each row of x.
28 //The input argument m represents the a priori mean. If it is present, then the sum is
29 //divided by n. Otherwise ("sample variance"), it is divided by n-1.
32 // Checking and normalizing input arguments:
33 // ----------------------------------------
36 tmp = gettext("%s: Wrong number of input arguments: %d to %d expected.\n")
37 error(msprintf(tmp, "variance", 1, 2))
45 if ~isdef("orien","local") then
50 if typeof(m)~="constant" then
51 tmp = gettext("%s: Wrong value of m : a priori mean expected.\n")
52 error(msprintf(tmp, "variance"))
53 elseif orien=="*" then
55 tmp = gettext("%s: Wrong value of m : a priori mean expected.\n")
56 error(msprintf(tmp, "variance"))
58 elseif orien=="r" | orien==1 then
59 if size(m)~=[1 size(x,"c")] & ~isscalar(m) then
60 tmp = gettext("%s: Wrong value of m : a priori mean expected.\n")
61 error(msprintf(tmp, "variance"))
63 elseif orien=="c" | orien==2 then
64 if size(m)~=[size(x,"r") 1] & ~isscalar(m) then
65 tmp = gettext("%s: Wrong value of m : a priori mean expected.\n")
66 error(msprintf(tmp, "variance"))
71 transposed = %f // to refer and process as in "r", we priorly transpose any "c" request
72 if orien=="r" | orien==1 | orien=="c" | orien==2 | orien=="*"
73 if orien=="c" | orien==2 then
79 tmp = gettext("%s: Wrong value for input argument #%d: ''%s'', ''%s'', %d or %d expected.\n")
80 error(msprintf(tmp, "variance", 2, "c", "r", 1, 2))
86 d = size(x, orien) - 1 + exists("m","local") // Denominator. If m is given, then the a priori mean is known and we divide by size(n,orien)
88 if rhs == 3 & isnan(m) then
89 // This will compute the "biased variance": the denominator is size(x,orien) but the a priori mean is not considered as provided.
98 m = mean(x, orien).*.ones(size(x,1),1)
102 tmp = _("%s: The significance of input argument #%d has been modified. Please refer to the variance help page.\n")
103 warning(msprintf(tmp, "variance", 3))
105 // If m is a scalar, extend it to the size of x.
106 // If lhs==1, we do not need to perform this operation, because in the following 'x - m', m can be a scalar
112 m = m.*.ones(size(x,1),1)
117 s = sum(abs(x - m).^2, orien) / d