Stan Math Library  2.20.0
reverse mode automatic differentiation
inv_Phi.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_FUN_INV_PHI_HPP
2 #define STAN_MATH_PRIM_SCAL_FUN_INV_PHI_HPP
3 
10 
11 namespace stan {
12 namespace math {
13 
28 inline double inv_Phi(double p) {
29  check_bounded("inv_Phi", "Probability variable", p, 0, 1);
30 
31  if (p < 8e-311)
32  return NEGATIVE_INFTY;
33  if (p == 1)
34  return INFTY;
35 
36  static const double a[6]
37  = {-3.969683028665376e+01, 2.209460984245205e+02, -2.759285104469687e+02,
38  1.383577518672690e+02, -3.066479806614716e+01, 2.506628277459239e+00};
39  static const double b[5]
40  = {-5.447609879822406e+01, 1.615858368580409e+02, -1.556989798598866e+02,
41  6.680131188771972e+01, -1.328068155288572e+01};
42  static const double c[6]
43  = {-7.784894002430293e-03, -3.223964580411365e-01, -2.400758277161838e+00,
44  -2.549732539343734e+00, 4.374664141464968e+00, 2.938163982698783e+00};
45  static const double d[4] = {7.784695709041462e-03, 3.224671290700398e-01,
46  2.445134137142996e+00, 3.754408661907416e+00};
47 
48  static const double p_low = 0.02425;
49  static const double p_high = 0.97575;
50 
51  double x;
52  if ((p_low <= p) && (p <= p_high)) {
53  double q = p - 0.5;
54  double r = square(q);
55  x = (((((a[0] * r + a[1]) * r + a[2]) * r + a[3]) * r + a[4]) * r + a[5])
56  * q
57  / (((((b[0] * r + b[1]) * r + b[2]) * r + b[3]) * r + b[4]) * r + 1.0);
58  } else if (p < p_low) {
59  double q = std::sqrt(-2.0 * std::log(p));
60  x = (((((c[0] * q + c[1]) * q + c[2]) * q + c[3]) * q + c[4]) * q + c[5])
61  / ((((d[0] * q + d[1]) * q + d[2]) * q + d[3]) * q + 1.0);
62  } else {
63  double q = std::sqrt(-2.0 * log1m(p));
64  x = -(((((c[0] * q + c[1]) * q + c[2]) * q + c[3]) * q + c[4]) * q + c[5])
65  / ((((d[0] * q + d[1]) * q + d[2]) * q + d[3]) * q + 1.0);
66  }
67 
68  if (x < 37.6) { // gradient blows up past here
69  double e = Phi(x) - p;
70  double u = e * SQRT_2_TIMES_SQRT_PI * std::exp(0.5 * square(x));
71  x -= u / (1.0 + 0.5 * x * u);
72  }
73 
74  return x;
75 }
76 
77 } // namespace math
78 } // namespace stan
79 #endif
fvar< T > sqrt(const fvar< T > &x)
Definition: sqrt.hpp:13
fvar< T > inv_Phi(const fvar< T > &p)
Definition: inv_Phi.hpp:14
void check_bounded(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
Check if the value is between the low and high values, inclusively.
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:12
fvar< T > square(const fvar< T > &x)
Definition: square.hpp:12
const double SQRT_2_TIMES_SQRT_PI
Definition: constants.hpp:134
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:11
fvar< T > Phi(const fvar< T > &x)
Definition: Phi.hpp:13
double e()
Return the base of the natural logarithm.
Definition: constants.hpp:87
const double INFTY
Positive infinity.
Definition: constants.hpp:48
const double NEGATIVE_INFTY
Negative infinity.
Definition: constants.hpp:53
fvar< T > log1m(const fvar< T > &x)
Definition: log1m.hpp:12

     [ Stan Home Page ] © 2011–2018, Stan Development Team.