mynormcdf.m
Upload User: fs_yukai
Upload Date: 2016-02-21
Package Size: 22739k
Code Size: 1k
Category:

AI-NN-PR

Development Platform:

Matlab

  1. function p = normcdf(x,mu,sigma)
  2. %NORMCDF Normal cumulative distribution function (cdf).
  3. %   P = NORMCDF(X,MU,SIGMA) computes the normal cdf with mean MU and
  4. %   standard deviation SIGMA at the values in X.
  5. %
  6. %   The size of P is the common size of X, MU and SIGMA. A scalar input  
  7. %   functions as a constant matrix of the same size as the other inputs.    
  8. %
  9. %   Default values for MU and SIGMA are 0 and 1 respectively.
  10. %   References:
  11. %      [1]  M. Abramowitz and I. A. Stegun, "Handbook of Mathematical
  12. %      Functions", Government Printing Office, 1964, 26.2.
  13. %   Copyright (c) 1993-98 by The MathWorks, Inc.
  14. %   $Revision: 2.6 $  $Date: 1997/11/29 01:46:13 $
  15. if nargin < 3, 
  16.     sigma = 1;
  17. end
  18. if nargin < 2;
  19.     mu = 0;
  20. end
  21. [errorcode x mu sigma] = distchck(3,x,mu,sigma);
  22. if errorcode > 0
  23.     error('Requires non-scalar arguments to match in size.');
  24. end
  25. %   Initialize P to zero.
  26. p = zeros(size(x));
  27. % Return NaN if SIGMA is not positive.
  28. k1 = find(sigma <= 0);
  29. if any(k1)
  30.     tmp   = NaN;
  31.     p(k1) = tmp(ones(size(k1))); 
  32. end
  33. % Express normal CDF in terms of the error function.
  34. k = find(sigma > 0);
  35. if any(k)
  36.     p(k) = 0.5 * erfc( - (x(k) - mu(k)) ./ (sigma(k) * sqrt(2)));
  37. end
  38. % Make sure that round-off errors never make P greater than 1.
  39. k2 = find(p > 1);
  40. if any(k2)
  41.     p(k2) = ones(size(k2));
  42. end