The functions for variance in the Math unit are broken
Original Reporter info from Mantis: Ant_222
-
Reporter name: Anton Shepelev
Original Reporter info from Mantis: Ant_222
- Reporter name: Anton Shepelev
Description:
Hello, all.
The functions for the calculation of variance and
standard deviaion in the Math unit are broken. See
for yourselves:
Program Vartest;
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.
The blame is on the formula used throughout the
unit:
Summ(x_i^)/n - mean^2
which, although mathematically correct, is totally
unfit for numerical calculations. Attached is a
patch that fixed the problem.
Steps to reproduce:
Compile and run the attached program Vartest.pas .
Mantis conversion info:
- Mantis ID: 32804
- Version: 3.0.2
- Fixed in version: 3.1.1
- Fixed in revision: 37791 (#fb7d2d9e)
- Monitored by: » Ant_222 (Anton Shepelev)
- Target version: 3.2.0