function x = spm_invBcdf(F,v,w,tol) % Inverse Cumulative Distribution Function (CDF) of Beta distribution % FORMAT x = spm_invBcdf(F,v,w,tol) % % F - CDF (lower tail p-value) % v - Shape parameter (v>0) % w - Shape parameter (w>0) % x - Beta ordinates at which CDF F(x)=F % tol - tolerance [default 10^-6] %__________________________________________________________________________ % % spm_invBcdf implements the inverse Cumulative Distribution Function % for Beta distributions. % % Returns the Beta-variate x such that Pr{X0 & w>0 and for x in [0,1] (See Evans et al., Ch5). % The Cumulative Distribution Function (CDF) F(x) is the probability % that a realisation of a Beta random variable X has value less than % x. F(x)=Pr{X1; if sum(xa)>1 && any(any(diff(as(xa,:)),1)) error('non-scalar args must match in size'); end %-Computation - Undefined and special cases %-------------------------------------------------------------------------- %-Initialise result to zeros x = zeros(rs); %-Only defined for F in [0,1] & strictly positive v & w. % Return NaN if undefined. md = ( F>=0 & F<=1 & v>0 & w>0 ); if any(~md(:)) x(~md) = NaN; warning('Returning NaN for out of range arguments'); end %-Special cases: x=0 when F=0, x=1 when F=1 x(md & F==1) = 1; %-Compute where defined & not special case %-------------------------------------------------------------------------- Q = find( md & F>0 & F<1 ); if isempty(Q), return, end if xa(1), FQ=F(Q); FQ=FQ(:); else FQ=F*ones(length(Q),1); end if xa(2), vQ=v(Q); vQ=vQ(:); else vQ=v*ones(length(Q),1); end if xa(3), wQ=w(Q); wQ=wQ(:); else wQ=w*ones(length(Q),1); end %-Interval bisection %-------------------------------------------------------------------------- a = zeros(length(Q),1); fa = a-FQ; b = ones(length(Q),1); fb = b-FQ; i = 0; xQ = a+1/2; QQ = 1:length(Q); while ~isempty(QQ) && i 0; a(QQ(mQQ)) = xQ(QQ(mQQ)); fa(QQ(mQQ)) = fxQQ(mQQ); b(QQ(~mQQ)) = xQ(QQ(~mQQ)); fb(QQ(~mQQ)) = fxQQ(~mQQ); xQ(QQ) = a(QQ) + (b(QQ)-a(QQ))/2; QQ = QQ( (b(QQ)-a(QQ))>tol ); i = i+1; end if i==maxIt warning('convergence criteria not reached - maxIt reached'); end x(Q) = xQ;