spm_invBcdf.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. function x = spm_invBcdf(F,v,w,tol)
  2. % Inverse Cumulative Distribution Function (CDF) of Beta distribution
  3. % FORMAT x = spm_invBcdf(F,v,w,tol)
  4. %
  5. % F - CDF (lower tail p-value)
  6. % v - Shape parameter (v>0)
  7. % w - Shape parameter (w>0)
  8. % x - Beta ordinates at which CDF F(x)=F
  9. % tol - tolerance [default 10^-6]
  10. %__________________________________________________________________________
  11. %
  12. % spm_invBcdf implements the inverse Cumulative Distribution Function
  13. % for Beta distributions.
  14. %
  15. % Returns the Beta-variate x such that Pr{X<x} = F for X a Beta
  16. % random variable with shape parameters v and w.
  17. %
  18. % Definition:
  19. %--------------------------------------------------------------------------
  20. % The Beta distribution has two shape parameters, v and w, and is
  21. % defined for v>0 & w>0 and for x in [0,1] (See Evans et al., Ch5).
  22. % The Cumulative Distribution Function (CDF) F(x) is the probability
  23. % that a realisation of a Beta random variable X has value less than
  24. % x. F(x)=Pr{X<x}: This function is usually known as the incomplete Beta
  25. % function. See Abramowitz & Stegun, 26.5; Press et al., Sec6.4 for
  26. % definitions of the incomplete beta function.
  27. %
  28. % Variate relationships:
  29. %--------------------------------------------------------------------------
  30. % Many: See Evans et al., Ch5
  31. %
  32. % Algorithm:
  33. %--------------------------------------------------------------------------
  34. % Interval bisection to find the inverse CDF to within tol.
  35. %
  36. % References:
  37. %--------------------------------------------------------------------------
  38. % Evans M, Hastings N, Peacock B (1993)
  39. % "Statistical Distributions"
  40. % 2nd Ed. Wiley, New York
  41. %
  42. % Abramowitz M, Stegun IA, (1964)
  43. % "Handbook of Mathematical Functions"
  44. % US Government Printing Office
  45. %
  46. % Press WH, Teukolsky SA, Vetterling AT, Flannery BP (1992)
  47. % "Numerical Recipes in C"
  48. % Cambridge
  49. %__________________________________________________________________________
  50. % Copyright (C) 1999-2011 Wellcome Trust Centre for Neuroimaging
  51. % Andrew Holmes
  52. % $Id: spm_invBcdf.m 4182 2011-02-01 12:29:09Z guillaume $
  53. %-Parameters
  54. %--------------------------------------------------------------------------
  55. Dtol = 10^-8;
  56. maxIt = 10000;
  57. %-Format arguments, note & check sizes
  58. %--------------------------------------------------------------------------
  59. if nargin<4, tol = Dtol; end
  60. if nargin<3, error('Insufficient arguments'), end
  61. ad = [ndims(F);ndims(v);ndims(w)];
  62. rd = max(ad);
  63. as = [[size(F),ones(1,rd-ad(1))];...
  64. [size(v),ones(1,rd-ad(2))];...
  65. [size(w),ones(1,rd-ad(3))]];
  66. rs = max(as);
  67. xa = prod(as,2)>1;
  68. if sum(xa)>1 && any(any(diff(as(xa,:)),1))
  69. error('non-scalar args must match in size');
  70. end
  71. %-Computation - Undefined and special cases
  72. %--------------------------------------------------------------------------
  73. %-Initialise result to zeros
  74. x = zeros(rs);
  75. %-Only defined for F in [0,1] & strictly positive v & w.
  76. % Return NaN if undefined.
  77. md = ( F>=0 & F<=1 & v>0 & w>0 );
  78. if any(~md(:))
  79. x(~md) = NaN;
  80. warning('Returning NaN for out of range arguments');
  81. end
  82. %-Special cases: x=0 when F=0, x=1 when F=1
  83. x(md & F==1) = 1;
  84. %-Compute where defined & not special case
  85. %--------------------------------------------------------------------------
  86. Q = find( md & F>0 & F<1 );
  87. if isempty(Q), return, end
  88. if xa(1), FQ=F(Q); FQ=FQ(:); else FQ=F*ones(length(Q),1); end
  89. if xa(2), vQ=v(Q); vQ=vQ(:); else vQ=v*ones(length(Q),1); end
  90. if xa(3), wQ=w(Q); wQ=wQ(:); else wQ=w*ones(length(Q),1); end
  91. %-Interval bisection
  92. %--------------------------------------------------------------------------
  93. a = zeros(length(Q),1); fa = a-FQ;
  94. b = ones(length(Q),1); fb = b-FQ;
  95. i = 0;
  96. xQ = a+1/2;
  97. QQ = 1:length(Q);
  98. while ~isempty(QQ) && i<maxIt
  99. fxQQ = betainc(xQ(QQ),vQ(QQ),wQ(QQ))-FQ(QQ);
  100. mQQ = fa(QQ).*fxQQ > 0;
  101. a(QQ(mQQ)) = xQ(QQ(mQQ)); fa(QQ(mQQ)) = fxQQ(mQQ);
  102. b(QQ(~mQQ)) = xQ(QQ(~mQQ)); fb(QQ(~mQQ)) = fxQQ(~mQQ);
  103. xQ(QQ) = a(QQ) + (b(QQ)-a(QQ))/2;
  104. QQ = QQ( (b(QQ)-a(QQ))>tol );
  105. i = i+1;
  106. end
  107. if i==maxIt
  108. warning('convergence criteria not reached - maxIt reached');
  109. end
  110. x(Q) = xQ;