spm_ncFcdf.m 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. function F = spm_ncFcdf(x,df,d)
  2. % Cumulative Distribution Function (CDF) of non-central F-distribution
  3. % FORMAT f = spm_ncFcdf(x,df,d)
  4. % x - F-variate (F has range [0,Inf) )
  5. % df - degrees of freedom, df = [v,w] with v>0 and w>0
  6. % d - non-centrality parameter
  7. % F - CDF of non-central F-distribution with [v,w] d.f. at points x
  8. %
  9. % Reference:
  10. % https://en.wikipedia.org/wiki/Noncentral_F-distribution
  11. %__________________________________________________________________________
  12. % Copyright (C) 2018 Wellcome Trust Centre for Neuroimaging
  13. % Guillaume Flandin
  14. % $Id: spm_ncFcdf.m 7354 2018-06-22 10:44:22Z guillaume $
  15. %-Format arguments, note & check sizes
  16. %--------------------------------------------------------------------------
  17. %-Unpack degrees of freedom v & w from single df parameter (df)
  18. if numel(df) == 2
  19. v = df(1);
  20. w = df(2);
  21. elseif size(df,2) == 2
  22. v = df(:,1);
  23. w = df(:,2);
  24. else
  25. error('Cannot unpack degrees of freedom.');
  26. end
  27. %-Check argument sizes
  28. ad = [ndims(x);ndims(v);ndims(w);ndims(d)];
  29. rd = max(ad);
  30. as = [[size(x),ones(1,rd-ad(1))];...
  31. [size(v),ones(1,rd-ad(2))];...
  32. [size(w),ones(1,rd-ad(3))];...
  33. [size(d),ones(1,rd-ad(4))];];
  34. rs = max(as);
  35. xa = prod(as,2)>1;
  36. if all(xa) && any(any(diff(as(xa,:)),1))
  37. error('Non-scalar arguments must match in size.');
  38. end
  39. %-Computation
  40. %--------------------------------------------------------------------------
  41. %-Initialise result to zeros
  42. F = zeros(rs);
  43. %-Only defined for v>0, w>0 and d>=0. Return NaN if undefined.
  44. md = true(size(F)) & v>0 & w>0 & d>=0;
  45. if any(~md(:))
  46. F(~md) = NaN;
  47. warning('Returning NaN for out of range arguments');
  48. end
  49. %-Compute where defined
  50. if ~any(md(:)), return, end
  51. Q = md;
  52. if xa(1), Qx=Q; else Qx=1; end
  53. if xa(2), Qv=Q; else Qv=1; end
  54. if xa(3), Qw=Q; else Qw=1; end
  55. if xa(4), Qd=Q; else Qd=1; end
  56. a = exp(-d(Qd)/2);
  57. x = v(Qv) .* x(Qx) ./ (w(Qw) + v(Qv) .* x(Qx));
  58. F(Q) = a .* betainc(x(Qx), v(Qv)/2, w(Qw)/2);
  59. for i=1:1024
  60. a = a .* d(Qd) / (2 * i);
  61. % could use recursion with:
  62. % Ix(a+1,b)=Ix(a,b)-G(a+b)/(G(a+1)*G(b))*x^a*(1-x)^b and G(a+1)=a*G(a)
  63. e = a .* betainc(x(Qx), v(Qv)/2+i, w(Qw)/2);
  64. if max(e) < 1e-12, break; end
  65. F = F + e;
  66. end