1 #ifndef __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__LOGNORMAL_HPP__
2 #define __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__LOGNORMAL_HPP__
16 template <
bool propto,
17 typename T_y,
typename T_loc,
typename T_scale,
22 static const char*
function =
"stan::prob::lognormal_log(%1%)";
43 if (!
check_not_nan(
function, y,
"Random variable", &logp, Policy()))
56 "Random variable",
"Location parameter",
"Scale parameter",
66 for (
size_t n = 0; n <
length(y); n++)
74 using stan::prob::NEG_LOG_SQRT_TWO_PI;
79 for (
size_t n = 0; n <
length(sigma); n++)
84 for (
size_t n = 0; n <
length(sigma); n++)
85 inv_sigma[n] = 1 /
value_of(sigma_vec[n]);
87 for (
size_t n = 0; n <
length(sigma); n++)
88 inv_sigma_sq[n] = inv_sigma[n] * inv_sigma[n];
92 for (
size_t n = 0; n <
length(y); n++)
96 for (
size_t n = 0; n <
length(y); n++)
100 logp += N * NEG_LOG_SQRT_TWO_PI;
102 for (
size_t n = 0; n < N; n++) {
103 const double mu_dbl =
value_of(mu_vec[n]);
108 logy_m_mu = log_y[n] - mu_dbl;
110 double logy_m_mu_sq = logy_m_mu * logy_m_mu;
111 double logy_m_mu_div_sigma(0);
115 logy_m_mu_div_sigma = logy_m_mu * inv_sigma_sq[n];
120 logp -= log_sigma[n];
124 logp -= 0.5 * logy_m_mu_sq * inv_sigma_sq[n];
128 operands_and_partials.
d_x1[n] -= (1 + logy_m_mu_div_sigma) * inv_y[n];
130 operands_and_partials.
d_x2[n] += logy_m_mu_div_sigma;
132 operands_and_partials.
d_x3[n] += (logy_m_mu_div_sigma * logy_m_mu - 1) * inv_sigma[n];
134 return operands_and_partials.
to_var(logp);
137 template <
bool propto,
138 typename T_y,
typename T_loc,
typename T_scale>
145 template <
typename T_y,
typename T_loc,
typename T_scale,
151 return lognormal_log<false>(y,mu,sigma,Policy());
154 template <
typename T_y,
typename T_loc,
typename T_scale>
164 template <
typename T_y,
typename T_loc,
typename T_scale,
166 typename boost::math::tools::promote_args<T_y,T_loc,T_scale>::type
169 static const char*
function =
"stan::prob::lognormal_cdf(%1%)";
174 using boost::math::tools::promote_args;
176 typename promote_args<T_y,T_loc,T_scale>::type lp;
177 if (!
check_not_nan(
function, y,
"Random variable", &lp, Policy()))
179 if (!
check_finite(
function, mu,
"Location parameter", &lp, Policy()))
191 template <
typename T_y,
typename T_loc,
typename T_scale>
193 typename boost::math::tools::promote_args<T_y,T_loc,T_scale>::type
var erfc(const stan::agrad::var &a)
The complementary error function for variables (C99).
double value_of(const agrad::var &v)
Return the value of the specified variable.
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.
T square(T x)
Return the square of the specified argument.
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.
boost::math::tools::promote_args< T_y, T_loc, T_scale >::type lognormal_cdf(const T_y &y, const T_loc &mu, const T_scale &sigma, const Policy &)
return_type< T_y, T_loc, T_scale >::type lognormal_log(const T_y &y, const T_loc &mu, const T_scale &sigma, const Policy &)
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