spm_Icdf.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. function F = spm_Icdf(x,n,p)
  2. % Cumulative Distribution Function (CDF) of Binomial Bin(n,p) distribution
  3. % FORMAT F = spm_Icdf(x,n,p)
  4. %
  5. % x - ordinate
  6. % n - Binomial n
  7. % p - Binomial p [Defaults to 0.5]
  8. % F - CDF
  9. %__________________________________________________________________________
  10. %
  11. % spm_Icdf returns the Cumulative Distribution Function for the
  12. % Binomial family of distributions.
  13. %
  14. % Definition:
  15. %--------------------------------------------------------------------------
  16. % The Bin(n,p) distribution is the distribution of the number of
  17. % successes from n identical independent Bernoulli trials each with
  18. % success probability p. If random variable X is the number of
  19. % successes from such a set of Bernoulli trials, then the CDF F(x) is
  20. % Pr{X<=x}, the probability of x or less sucesses.
  21. %
  22. % The Binomial CDF is defined for whole n (i.e. non-negative integer n)
  23. % and p in [0,1], given by: (See Evans et al., Ch6)
  24. %
  25. % { 0 for x<0
  26. % | _ floor(x)
  27. % F(x) = | > nCi * p^i * (1-p)^(n-i) for 0<=x<=n
  28. % | - i=0
  29. % { 1 for x>n
  30. %
  31. % where nCx is the Binomial coefficient "n-choose-x", given by n!/(x!(n-x)!)
  32. %
  33. % Normal approximation:
  34. %--------------------------------------------------------------------------
  35. % For (npq>5 & 0.1<=p<=0.9) | min(np,nq)>10 | npq>25 the Normal
  36. % approximation to the Binomial may be used:
  37. % X~Bin(n,p), X~:~N(np,npq) ( ~:~ -> approx. distributed as)
  38. % where q=1-p. With continuity correction this gives:
  39. % F(x) \approx \Phi((x+0.5-n*p)/sqrt(n*p*q))
  40. % for Phi the standard normal CDF, related to the error function by
  41. % \Phi(x) = 0.5+0.5*erf(x/sqrt(2))
  42. %
  43. % Algorithm:
  44. %--------------------------------------------------------------------------
  45. % F(x), the CDF of the Binomial distribution, for X~Bin(n,p), is related
  46. % to the incomplete beta function, by:
  47. %
  48. % F(x) = 1 - betainc(p,x+1,n-x) (0<=x<n)
  49. %
  50. % See Abramowitz & Stegun, 26.5.24; and Press et al., Sec6.1 for
  51. % further details.
  52. %
  53. % References:
  54. %--------------------------------------------------------------------------
  55. % Evans M, Hastings N, Peacock B (1993)
  56. % "Statistical Distributions"
  57. % 2nd Ed. Wiley, New York
  58. %
  59. % Abramowitz M, Stegun IA, (1964)
  60. % "Handbook of Mathematical Functions"
  61. % US Government Printing Office
  62. %
  63. % Press WH, Teukolsky SA, Vetterling AT, Flannery BP (1992)
  64. % "Numerical Recipes in C"
  65. % Cambridge
  66. %
  67. %__________________________________________________________________________
  68. % Copyright (C) 1999-2011 Wellcome Trust Centre for Neuroimaging
  69. % Andrew Holmes
  70. % $Id: spm_Icdf.m 4182 2011-02-01 12:29:09Z guillaume $
  71. %-Format arguments, note & check sizes
  72. %--------------------------------------------------------------------------
  73. if nargin<3, p=0.5; end
  74. if nargin<2, error('Insufficient arguments'), end
  75. ad = [ndims(x);ndims(n);ndims(p)];
  76. rd = max(ad);
  77. as = [ [size(x),ones(1,rd-ad(1))];...
  78. [size(n),ones(1,rd-ad(2))];...
  79. [size(p),ones(1,rd-ad(3))] ];
  80. rs = max(as);
  81. xa = prod(as,2)>1;
  82. if sum(xa)>1 && any(any(diff(as(xa,:)),1))
  83. error('non-scalar args must match in size');
  84. end
  85. %-Computation
  86. %--------------------------------------------------------------------------
  87. %-Initialise result to zeros
  88. F = zeros(rs);
  89. %-Only defined for whole n, and for p in [0,1]. Return NaN if undefined.
  90. md = ( ones(size(x)) & n==floor(n) & n>=0 & p>=0 & p<=1 );
  91. if any(~md(:))
  92. F(~md) = NaN;
  93. warning('Returning NaN for out of range arguments');
  94. end
  95. %-F is 1 where x>=n, or (p=0 & x>=0) (where betainc involves log of zero)
  96. m1 = ( x>=n | (p==0 & x>=0) );
  97. F(md&m1) = 1;
  98. %-Non-zero only where defined & x>=0 & ~(p==1 & x<n)
  99. % (Leave those already set to 1)
  100. Q = find( md & ~m1 & x>=0 & ~(p==1 & x<n) );
  101. if isempty(Q), return, end
  102. if xa(1), Qx=Q; else Qx=1; end
  103. if xa(2), Qn=Q; else Qn=1; end
  104. if xa(3), Qp=Q; else Qp=1; end
  105. %-F(x) is a step function with steps at {0,1,...,n}, so floor(x)
  106. fxQx = floor(x(Qx));
  107. %-Compute
  108. F(Q) = 1 - betainc(p(Qp),fxQx+1,n(Qn)-fxQx);