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,
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<>());
double value_of(const agrad::var &v)
Return the value of the specified variable.
var erf(const stan::agrad::var &a)
The error function for variables (C99).
var log(const var &a)
Return the natural log of the specified variable (cmath).
const double SQRT_2
The value of the square root of 2, .
double value_of(T x)
Return the value of the specified scalar argument converted to a double value.
bool check_not_nan(const char *function, const T_y &y, const char *name, T_result *result, const Policy &)
Checks if the variable y is nan.
bool check_consistent_sizes(const char *function, const T1 &x1, const T2 &x2, const char *name1, const char *name2, T_result *result, const Policy &)
bool check_positive(const char *function, const T_y &y, const char *name, T_result *result, const Policy &)
bool check_finite(const char *function, const T_y &y, const char *name, T_result *result, const Policy &)
Checks if the variable y is finite.
boost::math::policies::policy default_policy
Default error-handling policy from Boost.
return_type< T_y, T_loc, T_scale >::type normal_log(const T_y &y, const T_loc &mu, const T_scale &sigma, const Policy &)
The log of the normal density for the specified scalar(s) given the specified mean(s) and deviation(s...
double normal_random(const T_loc &mu, const T_scale &sigma, RNG &rng)
return_type< T_y, T_loc, T_scale >::type normal_cdf(const T_y &y, const T_loc &mu, const T_scale &sigma, const Policy &)
Calculates the normal cumulative distribution function for the given variate, location,...
Probability, optimization and sampling library.
size_t length(const T &x)
size_t max_size(const T1 &x1, const T2 &x2)
A variable implementation that stores operands and derivatives with respect to the variable.
VectorView< double *, is_vector< T1 >::value > d_x1
VectorView< double *, is_vector< T2 >::value > d_x2
T_return_type to_var(double logp)
VectorView< double *, is_vector< T3 >::value > d_x3
Metaprogram to determine if a type has a base scalar type that can be assigned to type double.
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
boost::math::tools::promote_args< typename scalar_type< T1 >::type, typename scalar_type< T2 >::type, typename scalar_type< T3 >::type, typename scalar_type< T4 >::type, typename scalar_type< T5 >::type, typename scalar_type< T6 >::type >::type type