
The Poisson distribution
The Poisson distribution deals with seemingly random occurrences of phenomena per unit of meaure, like:
Poisson : the source
Below is the source code for the program to calculate a theoretical Poisson distribution. Use it to suit your needs.
MODULE poisson;
IMPORT Arguments, InOut, MathLib, NumConv, RealConv;
VAR mu, g : REAL;
m, n : CARDINAL;
buffer : Arguments.ArgTable;
count : SHORTCARD;
PROCEDURE fact (n : CARDINAL) : REAL;
VAR m : REAL;
BEGIN
IF (n = 0) OR (n = 1) THEN RETURN 1.0 END;
m := 1.0;
WHILE n > 1 DO
m := m * FLOAT (n);
DEC (n)
END;
RETURN m
END fact;
PROCEDURE power (p : REAL; q : CARDINAL) : REAL;
VAR result : REAL;
BEGIN
result := 1.0;
WHILE q > 0 DO
result := result * p;
DEC (q)
END;
RETURN result
END power;
PROCEDURE Poisson (mu : REAL; x : CARDINAL) : REAL;
VAR factor1, factor2, factor3 : REAL;
BEGIN
factor1 := power (mu, x);
factor2 := MathLib.exp (-mu);
factor3 := fact (x);
RETURN factor1 * factor2 / factor3;
END Poisson;
PROCEDURE Init;
VAR ok : BOOLEAN;
BEGIN
Arguments.GetArgs (count, buffer);
IF count # 3 THEN
InOut.WriteString ("Syntax : poisson n mu");
InOut.WriteLn;
HALT
END;
NumConv.Str2Num (n, 10, buffer^[1]^, ok);
IF NOT ok THEN
InOut.WriteString (buffer^[1]^);
InOut.WriteString (" contains invalid tokens. Aborting.");
InOut.WriteLn;
HALT
END;
mu := RealConv.Str2Real (buffer^[2]^, ok);
IF NOT ok THEN
InOut.WriteString (buffer^[2]^);
InOut.WriteString (" contains invalid tokens. Aborting.");
InOut.WriteLn;
HALT
END
END Init;
BEGIN
Init;
m := 0;
REPEAT
InOut.WriteCard (m, 4);
InOut.Write (11C);
g := Poisson (mu, m);
InOut.WriteReal (g, 20, 10);
InOut.WriteLn;
INC (m)
UNTIL m > n;
END poisson.
The program is invoked with two parameters: n and mu (classes and average) as follows:
jan@Beryllium:~/modula/sampl$ poisson 12 4 0 0.0183156393 1 0.0732625574 2 0.1465251148 3 0.1953668147 4 0.1953668147 5 0.1562934518 6 0.1041956395 7 0.0595403649 8 0.0297701824 9 0.0132311918 10 0.0052924766 11 0.0019245370 12 0.0006415123 jan@Beryllium:~/modula/sampl$thereby producing the plot below:

Some more plots:
Page created on 15 September 2009 and
Page equipped with FroogleBuster technology