spm_Fpdf.m 3.9 KB

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