spm_Fcdf.m 4.2 KB

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