Stan Math Library  2.6.3
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
normal_cdf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_NORMAL_CDF_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_NORMAL_CDF_HPP
3 
4 #include <boost/random/normal_distribution.hpp>
5 #include <boost/random/variate_generator.hpp>
15 #include <cmath>
16 
17 
18 namespace stan {
19 
20  namespace math {
21 
36  template <typename T_y, typename T_loc, typename T_scale>
37  typename return_type<T_y, T_loc, T_scale>::type
38  normal_cdf(const T_y& y, const T_loc& mu, const T_scale& sigma) {
39  static const char* function("stan::math::normal_cdf");
41  T_partials_return;
42 
49  using std::exp;
50 
51  T_partials_return cdf(1.0);
52 
53  // check if any vectors are zero length
54  if (!(stan::length(y)
55  && stan::length(mu)
56  && stan::length(sigma)))
57  return cdf;
58 
59  check_not_nan(function, "Random variable", y);
60  check_finite(function, "Location parameter", mu);
61  check_not_nan(function, "Scale parameter", sigma);
62  check_positive(function, "Scale parameter", sigma);
63  check_consistent_sizes(function,
64  "Random variable", y,
65  "Location parameter", mu,
66  "Scale parameter", sigma);
67 
68 
70  operands_and_partials(y, mu, sigma);
71 
72  VectorView<const T_y> y_vec(y);
73  VectorView<const T_loc> mu_vec(mu);
74  VectorView<const T_scale> sigma_vec(sigma);
75  size_t N = max_size(y, mu, sigma);
76  const double SQRT_TWO_OVER_PI = std::sqrt(2.0 / stan::math::pi());
77 
78  for (size_t n = 0; n < N; n++) {
79  const T_partials_return y_dbl = value_of(y_vec[n]);
80  const T_partials_return mu_dbl = value_of(mu_vec[n]);
81  const T_partials_return sigma_dbl = value_of(sigma_vec[n]);
82  const T_partials_return scaled_diff = (y_dbl - mu_dbl)
83  / (sigma_dbl * SQRT_2);
84  T_partials_return cdf_;
85  if (scaled_diff < -37.5 * INV_SQRT_2)
86  cdf_ = 0.0;
87  else if (scaled_diff < -5.0 * INV_SQRT_2)
88  cdf_ = 0.5 * erfc(-scaled_diff);
89  else if (scaled_diff > 8.25 * INV_SQRT_2)
90  cdf_ = 1;
91  else
92  cdf_ = 0.5 * (1.0 + erf(scaled_diff));
93 
94  // cdf
95  cdf *= cdf_;
96 
97  // gradients
99  const T_partials_return rep_deriv
100  = scaled_diff < -37.5 * INV_SQRT_2
101  ? 0.0
102  : SQRT_TWO_OVER_PI * 0.5
103  * exp(-scaled_diff * scaled_diff) / cdf_ / sigma_dbl;
105  operands_and_partials.d_x1[n] += rep_deriv;
107  operands_and_partials.d_x2[n] -= rep_deriv;
109  operands_and_partials.d_x3[n] -= rep_deriv * scaled_diff * SQRT_2;
110  }
111  }
112 
114  for (size_t n = 0; n < stan::length(y); ++n)
115  operands_and_partials.d_x1[n] *= cdf;
116  }
118  for (size_t n = 0; n < stan::length(mu); ++n)
119  operands_and_partials.d_x2[n] *= cdf;
120  }
122  for (size_t n = 0; n < stan::length(sigma); ++n)
123  operands_and_partials.d_x3[n] *= cdf;
124  }
125 
126  return operands_and_partials.to_var(cdf, y, mu, sigma);
127  }
128  }
129 }
130 #endif
fvar< T > sqrt(const fvar< T > &x)
Definition: sqrt.hpp:15
bool check_not_nan(const char *function, const char *name, const T_y &y)
Return true if y is not NaN.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition: value_of.hpp:16
size_t length(const std::vector< T > &x)
Definition: length.hpp:10
fvar< T > erf(const fvar< T > &x)
Definition: erf.hpp:14
T_return_type to_var(T_partials_return logp, const T1 &x1=0, const T2 &x2=0, const T3 &x3=0, const T4 &x4=0, const T5 &x5=0, const T6 &x6=0)
VectorView< T_partials_return, is_vector< T1 >::value, is_constant_struct< T1 >::value > d_x1
Metaprogram to determine if a type has a base scalar type that can be assigned to type double...
const double SQRT_2
The value of the square root of 2, .
Definition: constants.hpp:21
const double INV_SQRT_2
The value of 1 over the square root of 2, .
Definition: constants.hpp:27
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10
VectorView< T_partials_return, is_vector< T3 >::value, is_constant_struct< T3 >::value > d_x3
A variable implementation that stores operands and derivatives with respect to the variable...
bool check_positive(const char *function, const char *name, const T_y &y)
Return true if y is positive.
size_t max_size(const T1 &x1, const T2 &x2)
Definition: max_size.hpp:9
bool check_finite(const char *function, const char *name, const T_y &y)
Return true if y is finite.
bool check_consistent_sizes(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)
Return true if the dimension of x1 is consistent with x2.
VectorView< T_partials_return, is_vector< T2 >::value, is_constant_struct< T2 >::value > d_x2
fvar< T > erfc(const fvar< T > &x)
Definition: erfc.hpp:14
double pi()
Return the value of pi.
Definition: constants.hpp:86
VectorView is a template metaprogram that takes its argument and allows it to be used like a vector...
Definition: VectorView.hpp:41
boost::math::tools::promote_args< typename partials_type< typename scalar_type< T1 >::type >::type, typename partials_type< typename scalar_type< T2 >::type >::type, typename partials_type< typename scalar_type< T3 >::type >::type, typename partials_type< typename scalar_type< T4 >::type >::type, typename partials_type< typename scalar_type< T5 >::type >::type, typename partials_type< typename scalar_type< T6 >::type >::type >::type type
return_type< T_y, T_loc, T_scale >::type normal_cdf(const T_y &y, const T_loc &mu, const T_scale &sigma)
Calculates the normal cumulative distribution function for the given variate, location, and scale.
Definition: normal_cdf.hpp:38

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