Variance

Return variance of values

Declaration

Source position: math.pp line 525

  function Variance(const data: Array of Single) : Float;
  function Variance(const data: PSingle; const N: Integer) : Float;
  function Variance(const data: Array of Double) : Float;
  function Variance(const data: PDouble; const N: Integer) : Float;
  function Variance(const data: Array of Extended) : Float;
  function Variance(const data: PExtended; const N: Integer) : Float;

Description

Variance returns the variance of the values in the data array. It returns zero if there is only one value.

The second form of the function accepts a pointer to an array of N values.

Errors

None.

See also

Name Description
mean Return mean value of array
stddev Return standard deviation of data
totalvariance Return total variance of values

Example

Program Example50;
{ Program to demonstrate the Variance function. }
{ @ should return typed pointer }
{$T+}
Uses math;
Var
  I : 1..100;
  ExArray : Array[1..100] of Float;
  V : float;
begin
  Randomize;
  for I:=low(ExArray) to high(ExArray) do
    ExArray[i]:=(Random-Random)*100;
  V:=Variance(ExArray);
  Writeln('Variance     : ',V:8:4);
  V:=Variance(@ExArray[1],100);
  Writeln('Variance (b) : ',V:8:4);
end.

Example

Program Example51;
{ 
  Program to demonstrate the Variance function.
  It demonstrates the absence of large errors in the calculation.
}
Uses math;
const 
  Size = 1000000;

var 
  dataS: array of Single;
  dataD: array of Double;
  dataE: array of Extended;
  i,n: longint;

begin
  WriteLn('Each run should return a value near unity.');
  WriteLn('Single:');
  SetLength( dataS, Size );
  for n := 1 to 4 do
  begin
    for i := 0 to Size - 1 do
    begin
      dataS[i] := 10000000 + RandG(0,1);
    end;
    WriteLn( Math.Variance( dataS ):5:3 );
  end;
  WriteLn('Double:');
  SetLength( dataD, Size );
  for n := 1 to 4 do
  begin
    for i := 0 to Size - 1 do
    begin
      dataD[i] := 1000000000000000 + RandG(0,1);
    end;
    WriteLn( Math.Variance( dataD ):5:3 );
  end;
  WriteLn('Extended:');
  SetLength( dataE, Size );
  for n := 1 to 4 do
  begin
    for i := 0 to Size - 1 do
    begin
      dataE[i] := 1000000000000000000 + RandG(0,1);
    end;
    WriteLn( Math.Variance( dataE ):5:3 );
  end;
end.