File: variancef.sci

package info (click to toggle)
scilab 5.2.2-9
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 334,832 kB
  • ctags: 52,586
  • sloc: xml: 526,945; ansic: 223,590; fortran: 163,080; java: 56,934; cpp: 33,840; tcl: 27,936; sh: 20,397; makefile: 9,908; ml: 9,451; perl: 1,323; cs: 614; lisp: 30
file content (62 lines) | stat: -rw-r--r-- 2,839 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) 2000 - INRIA - Carlos Klimann
// 
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// you should have received as part of this distribution.  The terms
// are also available at
// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
// 

function [s]=variancef(x,fre,orien)
//
//This function  computes  the variance  of the values  of   a vector or
//matrix x, each  of  them  counted with  a  frequency signaled   by the
//corresponding values of the integer vector or matrix fre with the same
//type of x.
//
//For a vector or matrix  x, s=variancef(x,fre) (or s=variancef(x,fre,'*') returns
//in scalar s the variance  of all the  entries of x, each value counted
//with the multiplicity indicated by the corresponding value of fre.
//
//s=variancef(x,fre,'r')(or,   equivalently, s=variancef(x,fre,1)) returns in each
//entry of the row vector s  of type 1xsize(x,'c')  the variance of each
//column of x, each value counted with the multiplicity indicated by the
//corresponding value of fre.
//
//s=variancef(x,fre,'c')(or, equivalently,   s=variancef(x,fre,2)) returns in each
//entry of  the column vector  s of type   size(x,'c')x1 the variance of
//each row of  x, each value counted with  the multiplicity indicated by
//the corresponding value of fre.
//
//
  if x==[] then s=%nan, return, end
  [lhs,rhs]=argn(0)
  if rhs<2|rhs>3 then
    error(msprintf(gettext("%s: Wrong number of input arguments: %d to %d expected.\n"),"variancef",2,3)),
  end
  if x==[]|fre==[]|fre==0, s=%nan;return,end
  if rhs==2 then
    sumfre=sum(fre)
    if sumfre <= 1 then error(msprintf(gettext("%s: Wrong value for input argument #%d: Must be > %d.\n"),"variancef", 2, 1)), end
    s=(sum(((x-meanf(x,fre)).^2).*fre))/(sumfre-1),
    return,
  end
  if orien=='*',
    sumfre=sum(fre)
    if sumfre <= 1 then error(msprintf(gettext("%s: Wrong value for input argument #%d: Must be > %d.\n"),"variancef", 2, 1)),end
    s=(sum(((x-meanf(x,fre)).^2).*fre))/(sumfre-1),
  elseif orien=='r'|orien==1,
    sumfre=sum(fre,'r')
    if or(sumfre==0) then error(msprintf(gettext("%s: Wrong value for input argument #%d: Must be > %d.\n"),"variancef",2,1)),end
    s=(sum(((x-ones(size(x,'r'),1)*meanf(x,fre,'r')).^2).*fre))./ ..
      (sumfre-1)
  elseif orien=='c'|orien==2,
    sumfre=sum(fre,'c')
    if or(sumfre==0) then error(msprintf(gettext("%s: Wrong value for input argument #%d: Must be > %d.\n"),"variancef",2,1)),end
    s=(sum((x-(meanf(x,fre,'c')*ones(1,size(x,'c')))).^2,'c'))./..
      (sumfre-1)
  else error(msprintf(gettext("%s: Wrong value for input argument #%d: ''%s'', ''%s'', ''%s'', %d or %d expected.\n"),"variancef",3,"*","c","r",1,2))
  end
endfunction