~ubuntu-branches/ubuntu/hoary/scilab/hoary

« back to all changes in this revision

Viewing changes to macros/statistics/variancef.sci

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2005-01-09 22:58:21 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050109225821-473xr8vhgugxxx5j
Tags: 3.0-12
changed configure.in to build scilab's own malloc.o, closes: #255869

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
function [s]=variancef(x,fre,orien)
 
2
//
 
3
//This function  computes  the variance  of the values  of   a vector or
 
4
//matrix x, each  of  them  counted with  a  frequency signaled   by the
 
5
//corresponding values of the integer vector or matrix fre with the same
 
6
//type of x.
 
7
//
 
8
//For a vector or matrix  x, s=variancef(x,fre) (or s=variancef(x,fre,'*') returns
 
9
//in scalar s the variance  of all the  entries of x, each value counted
 
10
//with the multiplicity indicated by the corresponding value of fre.
 
11
//
 
12
//s=variancef(x,fre,'r')(or,   equivalently, s=variancef(x,fre,1)) returns in each
 
13
//entry of the row vector s  of type 1xsize(x,'c')  the variance of each
 
14
//column of x, each value counted with the multiplicity indicated by the
 
15
//corresponding value of fre.
 
16
//
 
17
//s=variancef(x,fre,'c')(or, equivalently,   s=variancef(x,fre,2)) returns in each
 
18
//entry of  the column vector  s of type   size(x,'c')x1 the variance of
 
19
//each row of  x, each value counted with  the multiplicity indicated by
 
20
//the corresponding value of fre.
 
21
//
 
22
//author: carlos klimann
 
23
//
 
24
//date: 2000-01-20
 
25
//
 
26
//Fixed by ck 2001-03-28: verify if sum(fre)> 1
 
27
//
 
28
  if x==[] then s=%nan, return, end
 
29
  [lhs,rhs]=argn(0)
 
30
  if rhs<2|rhs>3 then
 
31
    error('variancef requires two or three inputs.'),
 
32
  end
 
33
  if x==[]|fre==[]|fre==0, s=%nan;return,end
 
34
  if rhs==2 then
 
35
    sumfre=sum(fre)
 
36
    if sumfre <= 1 then error('Frequencies must be greater than 1'), end
 
37
    s=(sum(((x-meanf(x,fre)).^2).*fre))/(sumfre-1),
 
38
    return,
 
39
  end
 
40
  if orien=='*',
 
41
    sumfre=sum(fre)
 
42
    if sumfre <= 1 then error('Frequencies must be greater than 1'),end
 
43
    s=(sum(((x-meanf(x,fre)).^2).*fre))/(sumfre-1),
 
44
  elseif orien=='r'|orien==1,
 
45
    sumfre=sum(fre,'r')
 
46
    if or(sumfre==0) then error('Frequencies must be greater than 1'),end
 
47
    s=(sum(((x-ones(size(x,'r'),1)*meanf(x,fre,'r')).^2).*fre))./ ..
 
48
      (sumfre-1)
 
49
  elseif orien=='c'|orien==2,
 
50
    sumfre=sum(fre,'c')
 
51
    if or(sumfre==0) then error('Frequencies must be greater than 1'),end
 
52
    s=(sum((x-(meanf(x,fre,'c')*ones(1,size(x,'c')))).^2,'c'))./..
 
53
      (sumfre-1)
 
54
  else error('3rd argument of variancef must be equal to *, c, r, 1 or 2'),
 
55
  end
 
56
endfunction