spm_invFcdf.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. function x = spm_invFcdf(F,v,w)
  2. % Inverse Cumulative Distribution (CDF) of F (Fisher-Snedecor) distribution
  3. % FORMAT x = spm_invFcdf(F,df)
  4. % FORMAT x = spm_invFcdf(F,v,w)
  5. %
  6. % F - CDF (lower tail p-value)
  7. % df - Degrees of freedom, concatenated along last dimension
  8. % Eg. Scalar (or column vector) v & w. Then df=[v,w];
  9. % v - Shape parameter 1 / numerator degrees of freedom (v>0)
  10. % w - Shape parameter 2 / denominator degrees of freedom (w>0)
  11. % x - F-variate (F has range [0,Inf) )
  12. %__________________________________________________________________________
  13. %
  14. % spm_Fcdf implements the inverse Cumulative Distribution Function
  15. % for the F-distribution.
  16. %
  17. % Definition:
  18. %--------------------------------------------------------------------------
  19. % The CDF F(x) of the F distribution with degrees of freedom v & w,
  20. % defined for positive integer degrees of freedom v & w, is the
  21. % probability that a realisation of an F random variable X has value
  22. % less than x F(x)=Pr{X<x} for X~F(v,w). The F-distribution is defined
  23. % for v>0 & w>0, and for x in [0,Inf) (See Evans et al., Ch16).
  24. %
  25. % Variate relationships: (Evans et al., Ch16 & 37)
  26. %--------------------------------------------------------------------------
  27. % The square of a Student's t variate with w degrees of freedom is
  28. % distributed as an F-distribution with [1,w] degrees of freedom.
  29. %
  30. % For X an F-variate with v,w degrees of freedom, w/(w+v*X^2) has
  31. % distribution related to a Beta random variable with shape parameters
  32. % w/2 & v/2, as described below.
  33. %
  34. % Algorithm:
  35. %--------------------------------------------------------------------------
  36. % Using the routine spm_invBcdf for the Beta distribution, with
  37. % appropriate parameters: The CDF of the F-distribution with v,w
  38. % degrees of freedom is related to the incomplete beta function by:
  39. % Pr(X<x) = 1 - betainc(w/(w+v*x^2),w/2,v/2)
  40. % See Abramowitz & Stegun, 26.6.2; Press et al., Sec6.4 for
  41. % definitions of the incomplete beta function. The relationship is
  42. % easily verified by substituting for w/(w+v*x^2) in the integral of the
  43. % incomplete beta function.
  44. %
  45. %
  46. % References:
  47. %--------------------------------------------------------------------------
  48. % Evans M, Hastings N, Peacock B (1993)
  49. % "Statistical Distributions"
  50. % 2nd Ed. Wiley, New York
  51. %
  52. % Abramowitz M, Stegun IA, (1964)
  53. % "Handbook of Mathematical Functions"
  54. % US Government Printing Office
  55. %
  56. % Press WH, Teukolsky SA, Vetterling AT, Flannery BP (1992)
  57. % "Numerical Recipes in C"
  58. % Cambridge
  59. %
  60. %__________________________________________________________________________
  61. % Copyright (C) 1993-2011 Wellcome Trust Centre for Neuroimaging
  62. % Andrew Holmes
  63. % $Id: spm_invFcdf.m 6378 2015-03-15 14:46:41Z karl $
  64. %-Format arguments, note & check sizes
  65. %--------------------------------------------------------------------------
  66. if nargin<2, error('Insufficient arguments'), end
  67. %-Unpack degrees of freedom v & w from single df parameter (v)
  68. if nargin<3
  69. vs = size(v);
  70. if prod(vs)==2
  71. %-DF is a 2-vector
  72. w = v(2); v = v(1);
  73. elseif vs(end)==2
  74. %-DF has last dimension 2 - unpack v & w
  75. nv = prod(vs);
  76. w = reshape(v(nv/2+1:nv),vs(1:end-1));
  77. v = reshape(v(1:nv/2) ,vs(1:end-1));
  78. else
  79. error('Can''t unpack both df components from single argument')
  80. end
  81. end
  82. %-Check argument sizes
  83. ad = [ndims(F);ndims(v);ndims(w)];
  84. rd = max(ad);
  85. as = [[size(F),ones(1,rd-ad(1))];...
  86. [size(v),ones(1,rd-ad(2))];...
  87. [size(w),ones(1,rd-ad(3))]];
  88. rs = max(as);
  89. xa = prod(as,2)>1;
  90. if sum(xa)>1 && any(any(diff(as(xa,:)),1))
  91. error('non-scalar args must match in size');
  92. end
  93. %-Computation
  94. %--------------------------------------------------------------------------
  95. %-Initialise result to zeros
  96. x = zeros(rs);
  97. %-Only defined for F in [0,1] & strictly positive v & w.
  98. % Return NaN if undefined.
  99. md = ( F>=0 & F<=1 & v>0 & w>0 );
  100. if any(~md(:))
  101. x(~md) = NaN;
  102. warning('Returning NaN for out of range arguments');
  103. end
  104. %-Special cases: x=0 when F=0, x=Inf when F=1
  105. x(md & F==1) = Inf;
  106. %-Compute where defined & not special case
  107. Q = find( md & F>0 & F<1 );
  108. if isempty(Q), return, end
  109. if xa(1), QF=Q; else QF=1; end
  110. if xa(2), Qv=Q; else Qv=1; end
  111. if xa(3), Qw=Q; else Qw=1; end
  112. %-Compute
  113. bQ = spm_invBcdf(1-F(QF),w(Qw)/2,v(Qv)/2);
  114. x(Q) = (w(Qw)./bQ -w(Qw))./v(Qv);