1 #ifndef __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__NORMAL_HPP__
2 #define __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__NORMAL_HPP__
4 #include <boost/random/normal_distribution.hpp>
5 #include <boost/random/variate_generator.hpp>
36 template <
bool propto,
37 typename T_y,
typename T_loc,
typename T_scale,
39 typename return_type<T_y,T_loc,T_scale>::type
40 normal_log(
const T_y& y,
const T_loc& mu,
const T_scale& sigma,
42 static const char*
function =
"stan::prob::normal_log(%1%)";
63 if (!
check_not_nan(
function, y,
"Random variable", &logp, Policy()))
73 "Random variable",
"Location parameter",
"Scale parameter",
91 for (
size_t i = 0; i <
length(sigma); i++) {
92 inv_sigma[i] = 1.0 /
value_of(sigma_vec[i]);
97 for (
size_t n = 0; n < N; n++) {
99 const double y_dbl =
value_of(y_vec[n]);
100 const double mu_dbl =
value_of(mu_vec[n]);
103 const double y_minus_mu_over_sigma
104 = (y_dbl - mu_dbl) * inv_sigma[n];
105 const double y_minus_mu_over_sigma_squared
106 = y_minus_mu_over_sigma * y_minus_mu_over_sigma;
108 static double NEGATIVE_HALF = - 0.5;
112 logp += NEG_LOG_SQRT_TWO_PI;
114 logp -= log_sigma[n];
116 logp += NEGATIVE_HALF * y_minus_mu_over_sigma_squared;
119 double scaled_diff = inv_sigma[n] * y_minus_mu_over_sigma;
121 operands_and_partials.
d_x1[n] -= scaled_diff;
123 operands_and_partials.
d_x2[n] += scaled_diff;
125 operands_and_partials.
d_x3[n]
126 += -inv_sigma[n] + inv_sigma[n] * y_minus_mu_over_sigma_squared;
128 return operands_and_partials.
to_var(logp);
132 template <
bool propto,
133 typename T_y,
typename T_loc,
typename T_scale>
136 normal_log(
const T_y& y,
const T_loc& mu,
const T_scale& sigma) {
140 template <
typename T_y,
typename T_loc,
typename T_scale,
144 normal_log(
const T_y& y,
const T_loc& mu,
const T_scale& sigma,
146 return normal_log<false>(y,mu,sigma,Policy());
149 template <
typename T_y,
typename T_loc,
typename T_scale>
152 normal_log(
const T_y& y,
const T_loc& mu,
const T_scale& sigma) {
175 template <
typename T_y,
typename T_loc,
typename T_scale,
178 normal_cdf(
const T_y& y,
const T_loc& mu,
const T_scale& sigma,
180 static const char*
function =
"stan::prob::normal_cdf(%1%)";
188 if (!
check_not_nan(
function, y,
"Random variable", &cdf, Policy()))
190 if (!
check_finite(
function, mu,
"Location parameter", &cdf, Policy()))
200 "Random variable",
"Location parameter",
"Scale parameter",
215 for (
size_t n = 0; n < N; n++) {
216 cdf *= 0.5 + 0.5 *
erf((y_vec[n] - mu_vec[n]) / (sigma_vec[n] *
SQRT_2));
221 template <
typename T_y,
typename T_loc,
typename T_scale>
224 normal_cdf(
const T_y& y,
const T_loc& mu,
const T_scale& sigma) {
229 template <
typename T_loc,
typename T_scale,
class RNG>
232 using boost::variate_generator;
233 using boost::normal_distribution;
235 variate_generator<RNG&, normal_distribution<> >
236 rng_unit_norm(rng, normal_distribution<>());