spm_Npdf.m 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. function f = spm_Npdf(x,u,v)
  2. % Probability Density Function (PDF) of univariate Normal distribution
  3. % FORMAT f = spm_Npdf(x,u,v)
  4. %
  5. % x - ordinates
  6. % u - mean [Defaults to 0]
  7. % v - variance (v>0) [Defaults to 1]
  8. % f - pdf of N(u,v) at x
  9. %__________________________________________________________________________
  10. %
  11. % spm_Npdf returns the Probability Density Function (PDF) for the
  12. % univariate Normal (Gaussian) family of distributions.
  13. %
  14. % Definition:
  15. %--------------------------------------------------------------------------
  16. % Let random variable X have a Normal distribution with mean u and
  17. % variance v, then Z~N(u,v). The Probability Density Function (PDF) of
  18. % the Normal (sometimes called Gaussian) family is f(x), defined on all
  19. % real x, given by: (See Evans et al., Ch29)
  20. %
  21. % 1 ( (x-u)^2 )
  22. % f(r) = ------------ x exp| ------ |
  23. % sqrt(v*2*pi) ( 2v )
  24. %
  25. % The PDF of the standard Normal distribution, with zero mean and unit
  26. % variance, Z~N(0,1), is commonly referred to as \phi(z).
  27. %
  28. % References:
  29. %--------------------------------------------------------------------------
  30. % Evans M, Hastings N, Peacock B (1993)
  31. % "Statistical Distributions"
  32. % 2nd Ed. Wiley, New York
  33. %
  34. % Abramowitz M, Stegun IA, (1964)
  35. % "Handbook of Mathematical Functions"
  36. % US Government Printing Office
  37. %
  38. % Press WH, Teukolsky SA, Vetterling AT, Flannery BP (1992)
  39. % "Numerical Recipes in C"
  40. % Cambridge
  41. %
  42. %__________________________________________________________________________
  43. % Copyright (C) 1994-2011 Wellcome Trust Centre for Neuroimaging
  44. % Andrew Holmes
  45. % $Id: spm_Npdf.m 4182 2011-02-01 12:29:09Z guillaume $
  46. %-Format arguments, note & check sizes
  47. %--------------------------------------------------------------------------
  48. if nargin<3, v=1; end
  49. if nargin<2, u=0; end
  50. if nargin<1, f=[]; return, end
  51. ad = [ndims(x);ndims(u);ndims(v)];
  52. rd = max(ad);
  53. as = [[size(x),ones(1,rd-ad(1))];...
  54. [size(u),ones(1,rd-ad(2))];...
  55. [size(v),ones(1,rd-ad(3))]];
  56. rs = max(as);
  57. xa = prod(as,2)>1;
  58. if sum(xa)>1 && any(any(diff(as(xa,:)),1))
  59. error('non-scalar args must match in size');
  60. end
  61. %-Computation
  62. %--------------------------------------------------------------------------
  63. %-Initialise result to zeros
  64. f = zeros(rs);
  65. %-Only defined for strictly positive variance v. Return NaN if undefined.
  66. md = ( ones(size(x)) & ones(size(u)) & v>0 );
  67. if any(~md(:))
  68. f(~md) = NaN;
  69. warning('Returning NaN for out of range arguments');
  70. end
  71. %-Non-zero where defined
  72. Q = find( md );
  73. if isempty(Q), return, end
  74. if xa(1), Qx=Q; else Qx=1; end
  75. if xa(2), Qu=Q; else Qu=1; end
  76. if xa(3), Qv=Q; else Qv=1; end
  77. %-Compute
  78. f(Q) = exp( -(x(Qx)-u(Qu)).^2 ./ (2*v(Qv)) ) ./ sqrt(2*pi*v(Qv));