DEFINITION MODULE SpezFunkt4;

  (*------------------------------------------------------------------------*)
  (* Verschiedene Dichte- und Wahrscheinlichkeitsfunktionen                 *)
  (* Various density and probability functions                              *)
  (*------------------------------------------------------------------------*)
  (* Based on work found in fpmath Pascal library                           *)
  (* https://github.com/neurolabusc/MRIcron/tree/master/fpmath              *)
  (* Additional documentation can be found there in document tpmath.pdf     *)
  (*------------------------------------------------------------------------*)
  (* Implementation : Michael Riedl                                         *)
  (* Licence        : GNU Lesser General Public License (LGPL)              *)
  (*------------------------------------------------------------------------*)

  (* $Id: SpezFunkt4.def,v 1.1 2016/09/03 22:08:43 mriedl Exp mriedl $ *) 

PROCEDURE DExpo(A,X : LONGREAL) : LONGREAL;

          (*----------------------------------------------------------------*)
          (* Density of exponential distribution with parameter A           *)
          (*                                                                *)
          (*   $ f_A(x) = A \exp\(- Ax)$  with $x > 0$                      *)
          (*                                                                *)
          (*----------------------------------------------------------------*)

PROCEDURE FExpo(A, X : LONGREAL) : LONGREAL;

          (*----------------------------------------------------------------*)
          (* Cumulative probability function for exponential distibution    *)
          (* with parameter A                                               *)
          (*                                                                *)
          (*   $\Phi_A(x) = \int_0^x A e^{-Ax} dx = 1 - e^{-xA}$            *)
          (*                                                                *)
          (*----------------------------------------------------------------*)

PROCEDURE DBeta(A,B : LONGREAL;
                X   : LONGREAL) : LONGREAL;

          (*----------------------------------------------------------------*)
          (* Density of Beta distribution with parameters A and B           *)
          (*                                                                *)
          (*   $ f_{A,B}(x) = {1 \over {\Beta(A,B)}} X^{A-1} (1-x)}^{B-1} $ *)
          (*                                                                *)
          (*   with $ 0 < x < 1$  and                                       *)
          (*                                                                *)
          (*  $$                                                            *)
          (*    \Beta(A,B) = \int_0^1 t^{A-1}(1-t)^{B-1}dt \eq              *)
          (*    {\Ģamma(A) \GAMMA(B) \over {\Gamma(A+B)}}                   *)
          (*  $$                                                            *)
          (*----------------------------------------------------------------*)

PROCEDURE FBeta(A,B : LONGREAL;
                X   : LONGREAL) : LONGREAL;

          (*----------------------------------------------------------------*)
          (* Cumulative probability for Beta distrib. with param. A and B   *)
          (*                                                                *)
          (*  $\Phi_{A,B}(x) = \int_0^x f_{A,B}(x) dx = I_B(A,B,x)$         *)
          (*                                                                *)
          (*----------------------------------------------------------------*)

PROCEDURE FBinom(N : INTEGER;
                 P : LONGREAL;
                 K : INTEGER) : LONGREAL;

          (*----------------------------------------------------------------*)
          (* Cumulative probability for binomial distrib with               *)
          (* P in [0,1], N > 0, N > K                                       *)
          (*----------------------------------------------------------------*)

PROCEDURE DStudent(Nu : INTEGER;
                   X  : LONGREAL) : LONGREAL;

          (*----------------------------------------------------------------*)
          (* Density of Student distribution with Nu degrees of freedom     *)
          (*----------------------------------------------------------------*)

PROCEDURE FStudent(Nu : INTEGER;
                   X  : LONGREAL) : LONGREAL;

          (*----------------------------------------------------------------*)
          (* Cumulative probability for Student distrib. with Nu degrees    *)
          (* of freedom                                                     *)
          (*----------------------------------------------------------------*)

PROCEDURE PStudent(Nu : INTEGER;
                   X  : LONGREAL) : LONGREAL;

          (*----------------------------------------------------------------*)
          (* Prob(|t| > X) for Student distrib. with Nu degrees of freedom  *)
          (*----------------------------------------------------------------*)

PROCEDURE DSnedecor(Nu1,Nu2 : INTEGER;
                    X       : LONGREAL) : LONGREAL;

          (*----------------------------------------------------------------*)
          (* Density of Fisher-Snedecor distribution with Nu1 and Nu2       *)
          (* degrees of freedom                                             *)
          (*----------------------------------------------------------------*)

PROCEDURE FSnedecor(Nu1, Nu2 : INTEGER;
                    X        : LONGREAL) : LONGREAL;

          (*----------------------------------------------------------------*)
          (* Cumulative prob. for Fisher-Snedecor distribution  with Nu1    *)
          (* and Nu2 degrees of freedom                                     *)
          (*----------------------------------------------------------------*)

PROCEDURE PSnedecor(Nu1,Nu2 : INTEGER;
                    X       : LONGREAL) : LONGREAL;

          (*----------------------------------------------------------------*)
          (* Prob(F > X) for Fisher-Snedecor distribution with Nu1 and Nu2  *)
          (* degrees of freedom                                             *)
          (*----------------------------------------------------------------*)

PROCEDURE DGamma(A,B : LONGREAL;
                 X   : LONGREAL) : LONGREAL;

          (*----------------------------------------------------------------*)
          (* Density of Gamma distribution with parameters A and B          *)
          (*----------------------------------------------------------------*)

PROCEDURE FGamma(A,B : LONGREAL;
                 X   : LONGREAL) : LONGREAL;

          (*----------------------------------------------------------------*)
          (* Cumulative probability for Gamma distrib. with param. A and B  *)
          (*----------------------------------------------------------------*)

PROCEDURE PPoisson(Mu : LONGREAL;
                   K  : INTEGER) : LONGREAL;

          (*----------------------------------------------------------------*)
          (* Probability of Poisson distribution. Returns the probability   *)
          (* of observing the value K if the mean is \mu with the           *)
          (* definition  Prob(K) = e^{-\mu} { \mu^K \over K! }              *) 
          (*----------------------------------------------------------------*)

PROCEDURE FPoisson(Mu : LONGREAL;
                   K  : INTEGER) : LONGREAL;

          (*----------------------------------------------------------------*)
          (* Cumulative probability for Poisson distribution defined by     *)
          (* Prob(x <= K) = \sum_{i=0}^K e^{-\mu} {\mu^i \over i!}          *)
          (*----------------------------------------------------------------*)

PROCEDURE FNorm(X : LONGREAL) : LONGREAL;

          (*----------------------------------------------------------------*)
          (* Cumulative probability for standard normal distrib.            *)
          (* See NormCDF as well                                            *)
          (*----------------------------------------------------------------*)

PROCEDURE PNorm(X : LONGREAL) : LONGREAL;

          (*----------------------------------------------------------------*)
          (* Prob(|U| > X) for standard normal distrib.                     *)
          (*----------------------------------------------------------------*)

PROCEDURE NormCDF(Z : LONGREAL) : LONGREAL;

          (*----------------------------------------------------------------*)
          (* Cumulative distribution function of the normal distribution    *)
          (* probabilities accurate to 1.0E-15                              *)
          (*                                                                *)
          (*   $\Phi(Z)= {1 \over {2\pi}} \int_{-\infty}^Z e^{-x^2} dx$     *)
          (*                                                                *)
          (* Based upon algorithm 5666 for the error function, from:        *)
          (* Hart, J. F. et al, computer approximations, wiley (1968)       *)
          (* Can be used as alternative to FNorm.                           *)
          (*----------------------------------------------------------------*)

PROCEDURE DKhi2(Nu : INTEGER;
                X  : LONGREAL) : LONGREAL;

          (*----------------------------------------------------------------*)
          (* Density of Khi-2 distribution with Nu degrees of freedom       *)
          (*----------------------------------------------------------------*)

PROCEDURE FKhi2(Nu : INTEGER;
                X  : LONGREAL) : LONGREAL;

          (*----------------------------------------------------------------*)
          (* Cumulative prob. for khi-2 distrib. with Nu d.o.f.             *)
          (*----------------------------------------------------------------*)

PROCEDURE PKhi2(Nu : INTEGER;
                X  : LONGREAL) : LONGREAL;

          (*----------------------------------------------------------------*)
          (* Prob(Khi2 > X) for khi-2 distrib. with Nu d.o.f.               *)
          (*----------------------------------------------------------------*)

PROCEDURE DisLandau(X : LONGREAL) : LONGREAL;

          (*----------------------------------------------------------------*)
          (* Landau distributen (5-6 sig. figures)                          *)
          (*                                                                *)
          (* Reference:                                                     *)
          (*                                                                *)
          (* Kolbig, K.S.; Schorr, B; "A program package for the Landau     *)
          (* distribution", Comp. Phys. Comm. 31 pp 97–111 (1984)           *)
          (*                                                                *)
          (* CERN library Fortran routine DISLAN translated to Modula-2     *)
          (*----------------------------------------------------------------*)

PROCEDURE DenLandau(X : LONGREAL) : LONGREAL;

          (*----------------------------------------------------------------*)
          (* Density of the Landau distributen (5-6 sig. figures)           *)
          (*                                                                *)
          (* Reference:                                                     *)
          (*                                                                *)
          (* Kolbig, K.S.; Schorr, B; "A program package for the Landau     *)
          (* distribution", Comp. Phys. Comm. 31 pp 97–111 (1984)           *)
          (*                                                                *)
          (* CERN library Fortran routine DENLAN translated to Modula-2     *)
          (*----------------------------------------------------------------*)

END SpezFunkt4.
