home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / statistics / distributions / lognormal_cdf.m < prev    next >
Encoding:
Text File  |  1997-02-19  |  2.3 KB  |  78 lines

  1. ## Copyright (C) 1995, 1996, 1997  Kurt Hornik
  2. ## 
  3. ## This program is free software; you can redistribute it and/or modify
  4. ## it under the terms of the GNU General Public License as published by
  5. ## the Free Software Foundation; either version 2, or (at your option)
  6. ## any later version.
  7. ## 
  8. ## This program is distributed in the hope that it will be useful, but
  9. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11. ## General Public License for more details. 
  12. ## 
  13. ## You should have received a copy of the GNU General Public License
  14. ## along with this file.  If not, write to the Free Software Foundation,
  15. ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16.  
  17. ## usage:  lognormal_cdf (x [, a, v])
  18. ##
  19. ## For each element of x, compute the cumulative distribution function
  20. ## (CDF) at x of the lognormal distribution with parameters a and v. If
  21. ## a random variable follows this distribution, its logarithm is
  22. ## normally distributed with mean log (a) and variance v.
  23. ##
  24. ## Default values are a = 1, v = 1.
  25.  
  26. ## Author:  KH <Kurt.Hornik@ci.tuwien.ac.at>
  27. ## Description:  CDF of the log normal distribution
  28.  
  29. function cdf = lognormal_cdf (x, a, v)
  30.  
  31.   if !((nargin == 1) || (nargin == 3))
  32.     usage ("lognormal_cdf (x [, a, v])");
  33.   endif
  34.  
  35.   if (nargin == 1)
  36.     a = 1;
  37.     v = 1;
  38.   endif
  39.  
  40.   ## The following "straightforward" implementation unfortunately does
  41.   ## not work (because exp (Inf) -> NaN etc):
  42.   ## cdf = normal_cdf (log (x), log (a), v);
  43.   ## Hence ...
  44.  
  45.   [retval, x, a, v] = common_size (x, a, v);
  46.   if (retval > 0)
  47.     error (["lognormal_cdf:  ", ...
  48.         "x, a and v must be of common size or scalars"]);
  49.   endif
  50.  
  51.   [r, c] = size (x);
  52.   s = r * c;
  53.   x = reshape (x, 1, s);
  54.   a = reshape (a, 1, s);
  55.   v = reshape (v, 1, s);
  56.   cdf = zeros (1, s);
  57.   
  58.   k = find (isnan (x) | !(a > 0) | !(a < Inf) ...
  59.       | !(v > 0) | !(v < Inf));
  60.   if any (k)
  61.     cdf(k) = NaN * ones (1, length (k));
  62.   endif
  63.   
  64.   k = find ((x == Inf) & (a > 0) & (a < Inf) & (v > 0) & (v < Inf));
  65.   if any (k)
  66.     cdf(k) = ones (1, length (k));
  67.   endif
  68.   
  69.   k = find ((x > 0) & (x < Inf) & (a > 0) & (a < Inf) ...
  70.       & (v > 0) & (v < Inf));
  71.   if any (k)
  72.     cdf(k) = stdnormal_cdf ((log (x(k)) - log (a(k))) ./ sqrt (v(k)));
  73.   endif
  74.   
  75.   cdf = reshape (cdf, r, c);
  76.   
  77. endfunction
  78.