spm_Ppdf.m 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. function f = spm_Ppdf(x,l)
  2. % Probability Distribution Function (PDF) of Poisson distribution
  3. % FORMAT f = spm_Ppdf(x,l)
  4. %
  5. % x - ordinates
  6. % l - Poisson mean parameter (lambda l>0) [Defaults to 1]
  7. % f - Poisson PDF
  8. %__________________________________________________________________________
  9. %
  10. % spm_Ppdf implements the Probaility Distribution Function of the
  11. % Poisson distribution.
  12. %
  13. % Definition:
  14. %--------------------------------------------------------------------------
  15. % The Poisson Po(l) distribution is the distribution of the number of
  16. % events in unit time for a stationary Poisson process with mean
  17. % parameter lambda=1, or equivalently rate 1/l. If random variable X is
  18. % the number of such events, then X~Po(l), and the PDF f(x) is
  19. % Pr({X=x}.
  20. %
  21. % f(x) is defined for strictly positive l, given by: (See Evans et al., Ch31)
  22. %
  23. % { l^x * exp(-l) / x! for r=0,1,...
  24. % f(r) = |
  25. % { 0 otherwise
  26. %
  27. % Algorithm:
  28. %--------------------------------------------------------------------------
  29. % To avoid roundoff errors for large x (in x! & l^x) & l (in l^x),
  30. % computation is done in logs.
  31. %
  32. % Normal approximation:
  33. %--------------------------------------------------------------------------
  34. % For large lambda the normal approximation Y~:~N(l,l) may be used.
  35. % With continuity correction this gives
  36. % f(x) ~=~ Phi((x+.5-l)/sqrt(l)) -Phi((x-.5-l)/sqrt(l));
  37. % where Phi is the standard normal CDF, and ~=~ means "appox. =".
  38. %
  39. % References:
  40. %--------------------------------------------------------------------------
  41. % Evans M, Hastings N, Peacock B (1993)
  42. % "Statistical Distributions"
  43. % 2nd Ed. Wiley, New York
  44. %
  45. % Abramowitz M, Stegun IA, (1964)
  46. % "Handbook of Mathematical Functions"
  47. % US Government Printing Office
  48. %
  49. % Press WH, Teukolsky SA, Vetterling AT, Flannery BP (1992)
  50. % "Numerical Recipes in C"
  51. % Cambridge
  52. %
  53. %__________________________________________________________________________
  54. % Copyright (C) 1996-2011 Wellcome Trust Centre for Neuroimaging
  55. % Andrew Holmes
  56. % $Id: spm_Ppdf.m 4182 2011-02-01 12:29:09Z guillaume $
  57. %-Format arguments, note & check sizes
  58. %--------------------------------------------------------------------------
  59. if nargin<2, l=1; end
  60. if nargin<1, error('Insufficient arguments'), end
  61. ad = [ndims(x);ndims(l)];
  62. rd = max(ad);
  63. as = [[size(x),ones(1,rd-ad(1))];...
  64. [size(l),ones(1,rd-ad(2))];];
  65. rs = max(as);
  66. xa = prod(as,2)>1;
  67. if all(xa) && any(diff(as(xa,:)))
  68. error('non-scalar args must match in size');
  69. end
  70. %-Computation
  71. %--------------------------------------------------------------------------
  72. %-Initialise result to zeros
  73. f = zeros(rs);
  74. %-Only defined for l>0. Return NaN if undefined.
  75. md = ( ones(size(x)) & l>0 );
  76. if any(~md(:))
  77. f(~md) = NaN;
  78. warning('Returning NaN for out of range arguments');
  79. end
  80. %-Non-zero only where defined and x is whole
  81. Q = find( md & x>=0 & x==floor(x) );
  82. if isempty(Q), return, end
  83. if xa(1), Qx=Q; else Qx=1; end
  84. if xa(2), Ql=Q; else Ql=1; end
  85. %-Compute
  86. f(Q) = exp(-l(Ql) + x(Qx).*log(l(Ql)) - gammaln(x(Qx)+1));