1 #ifndef STAN_MATH_PRIM_FUN_PHI_APPROX_HPP
2 #define STAN_MATH_PRIM_FUN_PHI_APPROX_HPP
3 
4 #include <stan/math/prim/meta.hpp>
5 #include <stan/math/prim/fun/inv_logit.hpp>
6 #include <stan/math/prim/functor/apply_scalar_unary.hpp>
7 #include <cmath>
8 
9 namespace stan {
10 namespace math {
11 
12 /**
13  * Return an approximation of the unit normal CDF.
14  *
15  * http://www.jiem.org/index.php/jiem/article/download/60/27
16  *
17  * This function can be used to implement the inverse link function
18  * for probit regression.
19  *
20  * @param x Argument.
21  * @return Probability random sample is less than or equal to argument.
22  */
Phi_approx(double x)23 inline double Phi_approx(double x) {
24   using std::pow;
25   return inv_logit(0.07056 * pow(x, 3.0) + 1.5976 * x);
26 }
27 
28 /**
29  * Return an approximation of the unit normal CDF.
30  *
31  * @param x argument.
32  * @return approximate probability random sample is less than or
33  * equal to argument.
34  */
Phi_approx(int x)35 inline double Phi_approx(int x) { return Phi_approx(static_cast<double>(x)); }
36 
37 /**
38  * Structure to wrap Phi_approx() so it can be vectorized.
39  */
40 struct Phi_approx_fun {
41   /**
42    * Return the approximate value of the Phi() function applied to
43    * the argument.
44    *
45    * @tparam T type of argument
46    * @param x argument
47    * @return approximate value of Phi applied to argument
48    */
49   template <typename T>
funstan::math::Phi_approx_fun50   static inline T fun(const T& x) {
51     return Phi_approx(x);
52   }
53 };
54 
55 /**
56  * Return the elementwise application of <code>Phi_approx()</code> to
57  * specified argument container.  The return type promotes the
58  * underlying scalar argument type to double if it is an integer,
59  * and otherwise is the argument type.
60  *
61  * @tparam T type of container
62  * @param x container
63  * @return elementwise Phi_approx of container elements
64  */
65 template <
66     typename T,
67     require_all_not_nonscalar_prim_or_rev_kernel_expression_t<T>* = nullptr,
68     require_not_var_matrix_t<T>* = nullptr>
Phi_approx(const T & x)69 inline auto Phi_approx(const T& x) {
70   return apply_scalar_unary<Phi_approx_fun, T>::apply(x);
71 }
72 
73 }  // namespace math
74 }  // namespace stan
75 
76 #endif
77