spm_invPcdf.m 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. function x = spm_invPcdf(F,l)
  2. % Inverse Cumulative Distribution Function (CDF) of Poisson distribution
  3. % FORMAT x = spm_invPcdf(F,l)
  4. %
  5. % F - CDF (lower tail p-value)
  6. % x - ordinates
  7. % l - Poisson mean parameter (lambda l>0) [Defaults to 1]
  8. %__________________________________________________________________________
  9. %
  10. % spm_invPcdf returns the inverse Cumulative Distribution Function for
  11. % the Poisson family of distributions.
  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 CDF F(x) is
  19. % Pr({X<=x}.
  20. %
  21. % The Poisson distribution is discrete, defined for l in [0,Inf) and x
  22. % in {0,1,...}, so F(x) is a discrete function. This inverse CDF
  23. % function returns the smallest Whole x such that the F(x) equals or
  24. % exceeds the given CDF probability F. I.e. F(x) is treated as a step
  25. % function.
  26. %
  27. % Algorithm:
  28. %--------------------------------------------------------------------------
  29. % x is found by direct summation of the Poisson PDFs until F is exceeded.
  30. %
  31. % References:
  32. %--------------------------------------------------------------------------
  33. % Evans M, Hastings N, Peacock B (1993)
  34. % "Statistical Distributions"
  35. % 2nd Ed. Wiley, New York
  36. %
  37. % Abramowitz M, Stegun IA, (1964)
  38. % "Handbook of Mathematical Functions"
  39. % US Government Printing Office
  40. %
  41. % Press WH, Teukolsky SA, Vetterling AT, Flannery BP (1992)
  42. % "Numerical Recipes in C"
  43. % Cambridge
  44. %
  45. %__________________________________________________________________________
  46. % Copyright (C) 1996-2011 Wellcome Trust Centre for Neuroimaging
  47. % Andrew Holmes
  48. % $Id: spm_invPcdf.m 4182 2011-02-01 12:29:09Z guillaume $
  49. %-Format arguments, note & check sizes
  50. %--------------------------------------------------------------------------
  51. if nargin<2, l=1; end
  52. if nargin<1, error('Insufficient arguments'), end
  53. ad = [ndims(F);ndims(l)];
  54. rd = max(ad);
  55. as = [[size(F),ones(1,rd-ad(1))];...
  56. [size(l),ones(1,rd-ad(2))];];
  57. rs = max(as);
  58. xa = prod(as,2)>1;
  59. if all(xa) && any(diff(as(xa,:)))
  60. error('non-scalar args must match in size');
  61. end
  62. %-Computation
  63. %--------------------------------------------------------------------------
  64. %-Initialise result to zeros
  65. x = zeros(rs);
  66. %-Only defined for F in [0,1] & l>0 & . Return NaN if undefined.
  67. md = ( F>=0 & F<=1 & l>0 );
  68. if any(~md(:))
  69. x(~md) = NaN;
  70. warning('Returning NaN for out of range arguments');
  71. end
  72. %-Infinite where defined but F=1
  73. mi = ( F==1 ); x(md&mi) = Inf;
  74. %-Non-zero only where defined, & F>0
  75. Q = find( md & ~mi & F>0 );
  76. if isempty(Q), return, end
  77. if xa(1), QF=Q; else QF=1; end
  78. if xa(2), Ql=Q; else Ql=1; end
  79. %-Compute by directly summing Poisson PDF's for successive x & comparing with F
  80. tx = 0;
  81. Ftx = spm_Ppdf(tx,l(Ql));
  82. while any(F(QF)>Ftx)
  83. tx = tx+1;
  84. i = find(Ftx<F(QF));
  85. x(Q(i)) = x(Q(i)) + 1;
  86. Ftx = Ftx + spm_Ppdf(tx,l(Ql));
  87. end