1 #ifndef STAN_MATH_PRIM_FUN_PHI_HPP 2 #define STAN_MATH_PRIM_FUN_PHI_HPP 3 4 #include <stan/math/prim/meta.hpp> 5 #include <stan/math/prim/err.hpp> 6 #include <stan/math/prim/fun/constants.hpp> 7 #include <stan/math/prim/fun/erf.hpp> 8 #include <stan/math/prim/fun/erfc.hpp> 9 #include <stan/math/prim/fun/Phi.hpp> 10 #include <stan/math/prim/functor/apply_scalar_unary.hpp> 11 12 namespace stan { 13 namespace math { 14 15 /** 16 * The unit normal cumulative distribution function. 17 * 18 * The return value for a specified input is the probability that 19 * a random unit normal variate is less than or equal to the 20 * specified value, defined by 21 * 22 * \f$\Phi(x) = \int_{-\infty}^x \mbox{\sf Norm}(x|0, 1) \ dx\f$ 23 * 24 * This function can be used to implement the inverse link function 25 * for probit regression. 26 * 27 * Phi will underflow to 0 below -37.5 and overflow to 1 above 8 28 * 29 * @param x Argument. 30 * @return Probability random sample is less than or equal to argument. 31 */ Phi(double x)32inline double Phi(double x) { 33 check_not_nan("Phi", "x", x); 34 if (x < -37.5) { 35 return 0; 36 } else if (x < -5.0) { 37 return 0.5 * erfc(-INV_SQRT_TWO * x); 38 } else if (x > 8.25) { 39 return 1; 40 } else { 41 return 0.5 * (1.0 + erf(INV_SQRT_TWO * x)); 42 } 43 } 44 45 /** 46 * Structure to wrap Phi() so it can be vectorized. 47 * 48 * @tparam T type of argument 49 * @param x argument 50 * @return Unit normal CDF of x. 51 */ 52 struct Phi_fun { 53 template <typename T> funstan::math::Phi_fun54 static inline T fun(const T& x) { 55 return Phi(x); 56 } 57 }; 58 59 /** 60 * Vectorized version of Phi(). 61 * 62 * @tparam T type of container 63 * @param x container 64 * @return Unit normal CDF of each value in x. 65 */ 66 template < 67 typename T, 68 require_all_not_nonscalar_prim_or_rev_kernel_expression_t<T>* = nullptr, 69 require_not_var_matrix_t<T>* = nullptr> Phi(const T & x)70inline auto Phi(const T& x) { 71 return apply_scalar_unary<Phi_fun, T>::apply(x); 72 } 73 74 } // namespace math 75 } // namespace stan 76 77 #endif 78