/octave-3.6.2/scripts/statistics/distributions/stdnormal_inv.m

# · Objective C · 57 lines · 45 code · 12 blank · 0 comment · 4 complexity · f7a026257a331306301da7df22028b4f MD5 · raw file

  1. ## Copyright (C) 2012 Rik Wehbring
  2. ## Copyright (C) 1995-2012 Kurt Hornik
  3. ##
  4. ## This file is part of Octave.
  5. ##
  6. ## Octave is free software; you can redistribute it and/or modify it
  7. ## under the terms of the GNU General Public License as published by
  8. ## the Free Software Foundation; either version 3 of the License, or (at
  9. ## your option) any later version.
  10. ##
  11. ## Octave is distributed in the hope that it will be useful, but
  12. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. ## General Public License for more details.
  15. ##
  16. ## You should have received a copy of the GNU General Public License
  17. ## along with Octave; see the file COPYING. If not, see
  18. ## <http://www.gnu.org/licenses/>.
  19. ## -*- texinfo -*-
  20. ## @deftypefn {Function File} {} stdnormal_inv (@var{x})
  21. ## For each element of @var{x}, compute the quantile (the
  22. ## inverse of the CDF) at @var{x} of the standard normal distribution
  23. ## (mean = 0, standard deviation = 1).
  24. ## @end deftypefn
  25. ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
  26. ## Description: Quantile function of the standard normal distribution
  27. function inv = stdnormal_inv (x)
  28. if (nargin != 1)
  29. print_usage ();
  30. endif
  31. if (iscomplex (x))
  32. error ("stdnormal_inv: X must not be complex");
  33. endif
  34. inv = sqrt (2) * erfinv (2 * x - 1);
  35. endfunction
  36. %!shared x
  37. %! x = [-1 0 0.5 1 2];
  38. %!assert(stdnormal_inv (x), [NaN -Inf 0 Inf NaN]);
  39. %% Test class of input preserved
  40. %!assert(stdnormal_inv ([x, NaN]), [NaN -Inf 0 Inf NaN NaN]);
  41. %!assert(stdnormal_inv (single([x, NaN])), single([NaN -Inf 0 Inf NaN NaN]));
  42. %% Test input validation
  43. %!error stdnormal_inv ()
  44. %!error stdnormal_inv (1,2)
  45. %!error stdnormal_inv (i)