LazStats: Better numerical stability of PoissonPDF.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7721 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2020-09-30 10:18:54 +00:00
parent 98cf0d0621
commit c50e122263
2 changed files with 136 additions and 21 deletions

View File

@ -52,13 +52,14 @@ function KolmogorovProb(z: double): double;
function KolmogorovTest(na: integer; const a: DblDyneVec; nb: integer;
const b: DblDyneVec; option: String; AReport: TStrings): double;
procedure poisson_cdf ( x : integer; a : double; VAR cdf : double );
//procedure poisson_cdf ( x : integer; a : double; VAR cdf : double );
procedure poisson_cdf_values (VAR n : integer; VAR a : double; VAR x : integer;
VAR fx : double );
procedure poisson_cdf_inv (VAR cdf : double; VAR a : double; VAR x : integer );
procedure poisson_check ( a : double );
function factorial(x : integer) : integer;
procedure poisson_pdf ( x : integer; VAR a : double; VAR pdf : double );
//function PoissonPDF(x: integer; a: double): Double;
implementation
@ -1932,6 +1933,7 @@ begin
result := prob;
end;
(* wp: moved to MathUnit for easier testing
procedure poisson_cdf ( x : integer; a : double; VAR cdf : double );
VAR
@ -1982,7 +1984,7 @@ begin
cdf := sum2;
end;
end;
*)
procedure poisson_cdf_values (VAR n : integer; VAR a : double; VAR x : integer;
VAR fx : double );
VAR
@ -2202,24 +2204,22 @@ begin
ShowMessage('POISSON_CHECK - Fatal error. A <= 0.');
end;
function factorial(x : integer) : longint; //integer;
VAR
decx : longint; // integer;
product : longint; //integer;
function Factorial(x: integer): longint; //integer;
var
decx: longint; // integer;
product: longint; //integer;
begin
decx := x;
product := 1;
while (decx > 0) do
begin
product := decx * product;
decx := decx - 1;
end;
result := product;
decx := x;
product := 1;
while (decx > 0) do
begin
product := decx * product;
decx := decx - 1;
end;
result := product;
end;
procedure poisson_pdf ( x : integer; VAR a : double; VAR pdf : double );
begin
(* wp: moved to MathUnit for easier testing
//
//*******************************************************************************
//
@ -2261,11 +2261,14 @@ begin
//
// Output, real PDF, the value of the PDF.
//
if ( x < 0 ) then pdf := 0.0E+00
function PoissonPDF(x: integer; a: double): Double;
begin
if (x < 0) then
Result := 0.0
else
pdf := exp ( - a ) * power(a,x) / factorial ( x );
Result := exp(-a) * power(a, x) / factorial(x);
// pdf := exp ( - a ) * power(a,x) / exp(logfactorial( x ));
end;
*)
end.