perm filename SIGMEA.SIM[SIM,SYS] blob sn#460227 filedate 1979-07-20 generic text, type T, neo UTF8
COMMENT Procedure SIGMEAN calculates new sigma2 (variance) and mean value
for a statistical material where the following properties are known -

Old variance				SIGMA2
Old mean value				MEAN
Number of observations made before	K
Value of new observation		XK1

At return the following values are available -

New variance				SIGMA2
New mean value				MEAN
Number of observations (=K+1)		K

An example:

! REAL sigma2,mean;
! ....
! k:= 0;
! mean:= sigma2:= 0;
! ...
! WHILE ... DO
!    ... x:= <new observed value>;
!    sigmean(sigma2,mean,k,x);
! END loop;
! ...

Note that some precision will be lost using SIGMEAN, compared with
the result of calculating the variance and mean value from all
the observations. However, since SIGMEAN calculates the variance and
mean value recursively, there is no need saving all the earlier
observations in an array.
PROCEDURE sigmean(sigma2,mean,k,xk1);
NAME sigma2,mean,k;   REAL sigma2,mean,xk1;   INTEGER k;
BEGIN   REAL oldsigma2,oldmean,ik1,z;   INTEGER oldk,k1;

    oldsigma2:= sigma2;
    oldmean:= mean;
    oldk:= k;
    k:= k1:= oldk + 1;
    ik1:= 1.0/k1;
    z:= (oldmean-xk1)*ik1;
    mean:= xk1 + oldk*z;
    sigma2:= oldk*(oldsigma2*ik1 + z↑2);
END of sigmean;