2 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
\r
3 // Copyright (C) 2013 - Scilab Enterprises - Paul BIGNIER : m parameter added
\r
4 // Copyright (C) 2013 - Samuel GOUGEON : http://bugzilla.scilab.org/11209 fixed
\r
5 // Copyright (C) 2000 - INRIA - Carlos Klimann
\r
7 // This file must be used under the terms of the CeCILL.
\r
8 // This source file is licensed as described in the file COPYING, which
\r
9 // you should have received as part of this distribution. The terms
\r
10 // are also available at
\r
11 // http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
\r
14 function [s, m] = variancef(x, fre, orien, m)
\r
16 //This function computes the variance of the values of a vector or
\r
17 //matrix x, each of them counted with a frequency signaled by the
\r
18 //corresponding values of the integer vector or matrix fre with the same
\r
21 //For a vector or matrix x, s=variancef(x,fre) (or s=variancef(x,fre,'*') returns
\r
22 //in scalar s the variance of all the entries of x, each value counted
\r
23 //with the multiplicity indicated by the corresponding value of fre.
\r
25 //s=variancef(x,fre,'r')(or, equivalently, s=variancef(x,fre,1)) returns in each
\r
26 //entry of the row vector s of type 1xsize(x,'c') the variance of each
\r
27 //column of x, each value counted with the multiplicity indicated by the
\r
28 //corresponding value of fre.
\r
30 //s=variancef(x,fre,'c')(or, equivalently, s=variancef(x,fre,2)) returns in each
\r
31 //entry of the column vector s of type size(x,'c')x1 the variance of
\r
32 //each row of x, each value counted with the multiplicity indicated by
\r
33 //the corresponding value of fre.
\r
35 //The input argument m represents the a priori mean. If it is present, then the sum is
\r
36 //divided by n. Otherwise ("sample variance"), it is divided by n-1.
\r
41 if rhs<2 | rhs>4 then
\r
42 msg = gettext("%s: Wrong number of input arguments: %d to %d expected.\n")
\r
43 error(msprintf(msg, "variancef", 2, 4))
\r
45 if x==[] | fre==[] | fre==0
\r
52 msg = gettext("%s: Wrong value for input argument #%d: Must be > %d.\n")
\r
53 error(msprintf(msg, "variancef", 2, 1)), end
\r
55 s = sum((abs(x-m).^2).*fre) / (sumfre-1)
\r
60 if typeof(m)~="constant" then
\r
61 tmp = gettext("%s: Wrong value of m : a priori mean expected.\n")
\r
62 error(msprintf(tmp, "variancef", ))
\r
63 elseif orien=="*" then
\r
64 if ~isscalar(m) then
\r
65 tmp = gettext("%s: Wrong value of m : a priori mean expected.\n")
\r
66 error(msprintf(tmp, "variancef", ))
\r
68 elseif orien=="r" | orien==1 then
\r
69 if size(m)~=[1 size(x,"c")] & ~isscalar(m) then
\r
70 tmp = gettext("%s: Wrong value of m : a priori mean expected.\n")
\r
71 error(msprintf(tmp, "variancef", ))
\r
73 elseif orien=="c" | orien==2 then
\r
74 if size(m)~=[size(x,"r") 1] & ~isscalar(m) then
\r
75 tmp = gettext("%s: Wrong value of m : a priori mean expected.\n")
\r
76 error(msprintf(tmp, "variancef", ))
\r
80 biased = %t; // Compute the biased variance
\r
86 msg = _("%s: Wrong value for input argument #%d: Must be > %d.\n")
\r
87 error(msprintf(msg, "variancef", 2, 1)),end
\r
90 s = sum((abs(x-m).^2).*fre) / (sumfre-1)
\r
93 s = sum((abs(x-m).^2).*fre) / sumfre
\r
95 s = sum((abs(x-m).^2).*fre) / sumfre
\r
97 elseif orien=="r" | orien==1,
\r
98 sumfre = sum(fre, "r")
\r
99 if or(sumfre==0) then
\r
100 msg = _("%s: Wrong value for input argument #%d: Must be > %d.\n")
\r
101 error(msprintf(msg, "variancef", 2, 1))
\r
103 if rhs<4 | biased == %t then
\r
104 m = meanf(x,fre,"r")
\r
105 elseif isscalar(m) then
\r
106 m = m*ones(1, size(x,"c"));
\r
108 m2 = ones(size(x,"r"),1)*m
\r
110 s = sum((abs(x-m2).^2).*fre, "r") ./ (sumfre-1)
\r
112 s = sum((abs(x-m2).^2).*fre, "r") ./ sumfre
\r
114 elseif orien=="c" | orien==2,
\r
115 sumfre = sum(fre, "c")
\r
116 if or(sumfre==0) then
\r
117 msg = _("%s: Wrong value for input argument #%d: Must be > %d.\n")
\r
118 error(msprintf(msg, "variancef", 2, 1))
\r
120 if rhs<4 | biased == %t then
\r
121 m = meanf(x,fre,"c")
\r
122 elseif isscalar(m) then
\r
123 m = m*ones(size(x,"r"), 1);
\r
125 m2 = m*ones(1,size(x,"c"))
\r
127 s = sum((abs(x-m2).^2).*fre, "c") ./ (sumfre-1)
\r
129 s = sum((abs(x-m2).^2).*fre, "c") ./ sumfre
\r
132 msg = _("%s: Wrong value for input argument #%d: ''%s'', ''%s'', ''%s'', %d or %d expected.\n")
\r
133 error(msprintf(msg, "variancef", 3, "*", "c", "r", 1, 2))
\r