spm_invNcdf.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. function x = spm_invNcdf(F,u,v)
  2. % Inverse Cumulative Distribution Function (CDF) for univariate Normal
  3. % FORMAT x = spm_invNcdf(F,u,v)
  4. %
  5. % F - CDF (lower tail p-value)
  6. % u - mean [Defaults to 0]
  7. % v - variance (v>0) [Defaults to 1]
  8. % x - ordinates of N(u,v) at which CDF F(x)=F
  9. %__________________________________________________________________________
  10. %
  11. % spm_invNcdf implements the inverse of the Cumulative Distribution
  12. % Function (CDF) for the Normal (Gaussian) family of distributions.
  13. %
  14. % Returns the variate x, such that Pr{X<x} = F for X~N(u,v), a
  15. % univariate random variable distributed Normally with mean u and
  16. % variance v.
  17. %
  18. % Definition:
  19. %--------------------------------------------------------------------------
  20. % The CDF F(x) of a Normal distribution with mean u and variance v is
  21. % the probability that a random realisation X from this distribution
  22. % will be less than x. F(x)=Pr(X<=x) for X~N(u,v). The inverse CDF
  23. % returns the normal ordinate x for which the CDF is F. See Evans et
  24. % al., Ch29 for further definitions and variate relationships.
  25. %
  26. % If X~N(u,v), then Z=(Z-u)/sqrt(v) has a standard normal distribution,
  27. % Z~N(0,1). The CDF of the standard normal distribution is known as
  28. % \Phi(z), its inverse as \Phi^{-1}(F).
  29. %
  30. % Algorithm:
  31. %--------------------------------------------------------------------------
  32. % The CDF for a standard N(0,1) Normal distribution, \Phi(z), is
  33. % related to the error function by: (Abramowitz & Stegun, 26.2.29)
  34. %
  35. % \Phi(z) = 0.5 + erf(z/sqrt(2))/2
  36. % so
  37. %
  38. % \Phi^{-1}(p) = sqrt(2) * erfinv(2*p-1)
  39. %
  40. % where erfinv(.) is the inverse error function.
  41. %
  42. % MATLAB's implementation of the inverse error function is used for
  43. % computation of z=\Phi^{-1}(F), the corresponding standard normal
  44. % variate, which converted to a variate x from a N(u,v) distribution by:
  45. % x = u+z*sqrt(v)
  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-2016 Wellcome Trust Centre for Neuroimaging
  63. % Andrew Holmes
  64. % $Id: spm_invNcdf.m 6859 2016-08-24 16:46:16Z guillaume $
  65. %-Format arguments, note & check sizes
  66. %--------------------------------------------------------------------------
  67. if nargin<3, v = 1; end
  68. if nargin<2, u = 0; end
  69. if nargin<1, x = []; return, end
  70. ad = [ndims(F);ndims(u);ndims(v)];
  71. rd = max(ad);
  72. as = [ [size(F),ones(1,rd-ad(1))];...
  73. [size(u),ones(1,rd-ad(2))];...
  74. [size(v),ones(1,rd-ad(3))] ];
  75. rs = max(as);
  76. xa = prod(as,2) > 1;
  77. if sum(xa)>1 && any(any(diff(as(xa,:)),1))
  78. error('Non-scalar arguments must match in size.');
  79. end
  80. %-Computation
  81. %--------------------------------------------------------------------------
  82. %-Initialise result to zeros
  83. x = zeros(rs);
  84. %-Only defined for F in [0,1], & strictly positive variance v.
  85. % Return NaN if undefined.
  86. md = ( F>=0 & F<=1 & ones(size(u)) & v>0 );
  87. if any(~md(:))
  88. x(~md) = NaN;
  89. warning('SPM:outOfRangeNormal','Returning NaN for out of range arguments.');
  90. end
  91. %-Compute where defined
  92. Q = find(md);
  93. if isempty(Q), return, end
  94. if xa(1), QF=Q; else QF=1; end
  95. if xa(2), Qu=Q; else Qu=1; end
  96. if xa(3), Qv=Q; else Qv=1; end
  97. %-Compute
  98. x(Q) = ( -sqrt(2)*erfcinv(2*F(QF)) .* sqrt(v(Qv)) ) + u(Qu);