home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / statistics / distributions / beta_inv.m < prev    next >
Encoding:
Text File  |  1997-02-19  |  2.4 KB  |  93 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:  beta_inv (x, a, b)
  18. ##
  19. ## For each component of x, compute the quantile (the inverse of the
  20. ## CDF) at x of the Beta distribution with parameters a and b.
  21.   
  22. ## Author:  KH <Kurt.Hornik@ci.tuwien.ac.at>
  23. ## Description:  Quantile function of the Beta distribution
  24.  
  25. function inv = beta_inv (x, a, b)
  26.   
  27.   if (nargin != 3)
  28.     usage ("beta_inv (x, a, b)");
  29.   endif
  30.   
  31.   [retval, x, a, b] = common_size (x, a, b);
  32.   if (retval > 0)
  33.     error ("beta_inv:  x, a and b must be of common size or scalars");
  34.   endif
  35.  
  36.   [r, c] = size (x);
  37.   s   = r * c;
  38.   x   = reshape (x, s, 1);
  39.   a   = reshape (a, s, 1);
  40.   b   = reshape (b, s, 1);
  41.   inv = zeros (s, 1);
  42.   
  43.   k = find ((x < 0) | (x > 1) | !(a > 0) | !(b > 0) | isnan (x));
  44.   if any (k)
  45.     inv (k) = NaN * ones (length (k), 1);
  46.   endif
  47.   
  48.   k = find ((x == 1) & (a > 0) & (b > 0));
  49.   if any (k)
  50.     inv (k) = ones (length (k), 1);
  51.   endif
  52.   
  53.   k = find ((x > 0) & (x < 1) & (a > 0) & (b > 0));
  54.   if any (k)
  55.     a = a (k);
  56.     b = b (k);
  57.     x = x (k);
  58.     y = a ./ b;
  59.     l = find (y < eps);
  60.     if any (l)
  61.       y(l) = sqrt (eps) * ones (length (l), 1);
  62.     endif
  63.     l = find (y > 1 - eps);
  64.     if any (l)
  65.       y(l) = 1 - sqrt (eps) * ones (length (l), 1);
  66.     endif
  67.  
  68.     y_old = y;
  69.     for i = 1 : 100
  70.       h     = (beta_cdf (y_old, a, b) - x) ./ beta_pdf (y_old, a, b);
  71.       y_new = y_old - h;
  72.       ind   = find (y_new <= eps);
  73.       if (any (ind))
  74.     y_new (ind) = y_old (ind) / 10;
  75.       endif
  76.       ind = find (y_new >= 1 - eps);
  77.       if any (ind)
  78.     y_new (ind) = 1 - (1 - y_old (ind)) / 10;
  79.       endif
  80.       h = y_old - y_new;
  81.       if (max (abs (h)) < sqrt (eps))
  82.     break;
  83.       endif
  84.       y_old = y_new;
  85.     endfor
  86.     
  87.     inv (k) = y_new;
  88.   endif
  89.  
  90.   inv = reshape (inv, r, c);
  91.   
  92. endfunction
  93.