| Stan Math Library 2.11.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
4 #include <stan/math/prim/scal/fun/constants.hpp>
5 #include <stan/math/prim/scal/err/check_bounded.hpp>
6 #include <stan/math/prim/scal/fun/Phi.hpp>
7 #include <stan/math/prim/scal/fun/log1m.hpp>
8
9 namespace stan {
10 namespace math {
11
26 inline double inv_Phi(double p) {
27 stan::math::check_bounded<double, double, double>("inv_Phi",
28 "Probability variable", p, 0, 1);
29
30 if (p < 8e-311)
31 return NEGATIVE_INFTY;
32 if (p == 1)
33 return INFTY;
34
35 static const double a[6] = {
36 -3.969683028665376e+01, 2.209460984245205e+02,
37 -2.759285104469687e+02, 1.383577518672690e+02,
38 -3.066479806614716e+01, 2.506628277459239e+00
39 };
40 static const double b[5] = {
41 -5.447609879822406e+01, 1.615858368580409e+02,
42 -1.556989798598866e+02, 6.680131188771972e+01,
43 -1.328068155288572e+01
44 };
45 static const double c[6] = {
46 -7.784894002430293e-03, -3.223964580411365e-01,
47 -2.400758277161838e+00, -2.549732539343734e+00,
48 4.374664141464968e+00, 2.938163982698783e+00
49 };
50 static const double d[4] = {
51 7.784695709041462e-03, 3.224671290700398e-01,
52 2.445134137142996e+00, 3.754408661907416e+00
53 };
54
55 static const double p_low = 0.02425;
56 static const double p_high = 0.97575;
57
58 double x;
59 if ((p_low <= p) && (p <= p_high)) {
60 double q = p - 0.5;
61 double r = q * q;
62 x = (((((a[0]*r + a[1])*r + a[2])*r + a[3])*r + a[4])*r + a[5])*q
63 / (((((b[0]*r + b[1])*r + b[2])*r + b[3])*r + b[4])*r + 1.0);
64 } else if (p < p_low) {
65 double q = std::sqrt(-2.0*std::log(p));
66 x = (((((c[0]*q + c[1])*q + c[2])*q + c[3])*q + c[4])*q + c[5])
67 / ((((d[0]*q + d[1])*q + d[2])*q + d[3])*q + 1.0);
68 } else {
69 double q = std::sqrt(-2.0 * stan::math::log1m(p));
70 x = -(((((c[0]*q + c[1])*q + c[2])*q + c[3])*q + c[4])*q + c[5])
71 / ((((d[0]*q + d[1])*q + d[2])*q + d[3])*q + 1.0);
72 }
73
74 if (x < 37.6) { // gradient blows up past here
75 double e = stan::math::Phi(x) - p;
76 double u = e * SQRT_2_TIMES_SQRT_PI * std::exp(0.5 * x * x);
77 x -= u / (1.0 + 0.5 * x * u);
78 }
79
80 return x;
81 }
82
83 }
84 }
85 #endif
stan::math::sqrt
fvar< T > sqrt(const fvar< T > &x)
Definition: sqrt.hpp:15
stan::math::inv_Phi
fvar< T > inv_Phi(const fvar< T > &p)
Definition: inv_Phi.hpp:15
stan
Definition: log_sum_exp.hpp:8
stan::math::log
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
stan::math::SQRT_2_TIMES_SQRT_PI
const double SQRT_2_TIMES_SQRT_PI
Definition: constants.hpp:158
stan::math::exp
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10
stan::math::Phi
fvar< T > Phi(const fvar< T > &x)
Definition: Phi.hpp:14
log1m.hpp
check_bounded.hpp
stan::math::e
double e()
Return the base of the natural logarithm.
Definition: constants.hpp:95
constants.hpp
stan::math::INFTY
const double INFTY
Positive infinity.
Definition: constants.hpp:44
stan::math::NEGATIVE_INFTY
const double NEGATIVE_INFTY
Negative infinity.
Definition: constants.hpp:50
Phi.hpp
stan::math::log1m
fvar< T > log1m(const fvar< T > &x)
Definition: log1m.hpp:16

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