1 #ifndef STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__LOGNORMAL_HPP
2 #define STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__LOGNORMAL_HPP
4 #include <boost/random/lognormal_distribution.hpp>
5 #include <boost/random/variate_generator.hpp>
21 template <
bool propto,
22 typename T_y,
typename T_loc,
typename T_scale>
23 typename return_type<T_y,T_loc,T_scale>::type
25 static const char*
function =
"stan::prob::lognormal_log(%1%)";
53 "Random variable",
"Location parameter",
62 for (
size_t n = 0; n <
length(y); n++)
67 operands_and_partials(y, mu, sigma);
71 using stan::prob::NEG_LOG_SQRT_TWO_PI;
77 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];
93 for (
size_t n = 0; n <
length(y); n++)
98 for (
size_t n = 0; n <
length(y); n++)
102 logp += N * NEG_LOG_SQRT_TWO_PI;
104 for (
size_t n = 0; n < N; n++) {
105 const double mu_dbl =
value_of(mu_vec[n]);
110 logy_m_mu = log_y[n] - mu_dbl;
112 double logy_m_mu_sq = logy_m_mu * logy_m_mu;
113 double logy_m_mu_div_sigma(0);
117 logy_m_mu_div_sigma = logy_m_mu * inv_sigma_sq[n];
122 logp -= log_sigma[n];
126 logp -= 0.5 * logy_m_mu_sq * inv_sigma_sq[n];
130 operands_and_partials.
d_x1[n] -= (1 + logy_m_mu_div_sigma) * inv_y[n];
132 operands_and_partials.
d_x2[n] += logy_m_mu_div_sigma;
134 operands_and_partials.
d_x3[n]
135 += (logy_m_mu_div_sigma * logy_m_mu - 1) * inv_sigma[n];
137 return operands_and_partials.
to_var(logp);
140 template <
typename T_y,
typename T_loc,
typename T_scale>
144 return lognormal_log<false>(y,mu,sigma);
148 template <
typename T_y,
typename T_loc,
typename T_scale>
151 static const char*
function =
"stan::prob::lognormal_cdf(%1%)";
159 using boost::math::tools::promote_args;
174 operands_and_partials(y, mu, sigma);
185 return operands_and_partials.
to_var(0.0);
188 for (
size_t n = 0; n < N; n++) {
189 const double y_dbl =
value_of(y_vec[n]);
190 const double mu_dbl =
value_of(mu_vec[n]);
191 const double sigma_dbl =
value_of(sigma_vec[n]);
192 const double scaled_diff = (
log(y_dbl) - mu_dbl) / (sigma_dbl *
SQRT_2);
193 const double rep_deriv = SQRT_2 * 0.5 / sqrt_pi
194 *
exp(-scaled_diff * scaled_diff) / sigma_dbl;
197 const double cdf_ = 0.5 *
erfc(-scaled_diff);
202 operands_and_partials.
d_x1[n] += rep_deriv / cdf_ / y_dbl ;
204 operands_and_partials.
d_x2[n] -= rep_deriv / cdf_ ;
206 operands_and_partials.
d_x3[n] -= rep_deriv * scaled_diff * SQRT_2
212 operands_and_partials.
d_x1[n] *= cdf;
215 operands_and_partials.
d_x2[n] *= cdf;
218 operands_and_partials.
d_x3[n] *= cdf;
220 return operands_and_partials.
to_var(cdf);
223 template <
typename T_y,
typename T_loc,
typename T_scale>
226 static const char*
function =
"stan::prob::lognormal_cdf_log(%1%)";
228 double cdf_log = 0.0;
234 using boost::math::tools::promote_args;
245 check_finite(
function, mu,
"Location parameter", &cdf_log);
249 operands_and_partials(y, mu, sigma);
263 const double log_half =
std::log(0.5);
265 for (
size_t n = 0; n < N; n++) {
266 const double y_dbl =
value_of(y_vec[n]);
267 const double mu_dbl =
value_of(mu_vec[n]);
268 const double sigma_dbl =
value_of(sigma_vec[n]);
269 const double scaled_diff = (
log(y_dbl) - mu_dbl) / (sigma_dbl *
SQRT_2);
270 const double rep_deriv = SQRT_2 / sqrt_pi
271 *
exp(-scaled_diff * scaled_diff) / sigma_dbl;
274 const double erfc_calc =
erfc(-scaled_diff);
275 cdf_log += log_half +
log(erfc_calc);
279 operands_and_partials.
d_x1[n] += rep_deriv / erfc_calc / y_dbl ;
281 operands_and_partials.
d_x2[n] -= rep_deriv / erfc_calc;
283 operands_and_partials.
d_x3[n] -= rep_deriv * scaled_diff * SQRT_2
287 return operands_and_partials.
to_var(cdf_log);
290 template <
typename T_y,
typename T_loc,
typename T_scale>
293 static const char*
function =
"stan::prob::lognormal_ccdf_log(%1%)";
295 double ccdf_log = 0.0;
301 using boost::math::tools::promote_args;
312 check_finite(
function, mu,
"Location parameter", &ccdf_log);
316 operands_and_partials(y, mu, sigma);
327 return operands_and_partials.
to_var(0.0);
330 const double log_half =
std::log(0.5);
332 for (
size_t n = 0; n < N; n++) {
333 const double y_dbl =
value_of(y_vec[n]);
334 const double mu_dbl =
value_of(mu_vec[n]);
335 const double sigma_dbl =
value_of(sigma_vec[n]);
336 const double scaled_diff = (
log(y_dbl) - mu_dbl) / (sigma_dbl *
SQRT_2);
337 const double rep_deriv = SQRT_2 / sqrt_pi
338 *
exp(-scaled_diff * scaled_diff) / sigma_dbl;
341 const double erfc_calc =
erfc(scaled_diff);
342 ccdf_log += log_half +
log(erfc_calc);
346 operands_and_partials.
d_x1[n] -= rep_deriv / erfc_calc / y_dbl ;
348 operands_and_partials.
d_x2[n] += rep_deriv / erfc_calc;
350 operands_and_partials.
d_x3[n] += rep_deriv * scaled_diff * SQRT_2
354 return operands_and_partials.
to_var(ccdf_log);
363 using boost::variate_generator;
364 using boost::random::lognormal_distribution;
366 static const char*
function =
"stan::prob::lognormal_rng(%1%)";
371 check_finite(
function, mu,
"Location parameter", (
double*)0);
374 variate_generator<RNG&, lognormal_distribution<> >
375 lognorm_rng(rng, lognormal_distribution<>(mu, sigma));
376 return lognorm_rng();
T square(const T x)
Return the square of the specified argument.
return_type< T_y, T_loc, T_scale >::type lognormal_cdf(const T_y &y, const T_loc &mu, const T_scale &sigma)
T_return_type to_var(double logp)
bool check_positive_finite(const char *function, const T_y &y, const char *name, T_result *result)
return_type< T_y, T_loc, T_scale >::type lognormal_ccdf_log(const T_y &y, const T_loc &mu, const T_scale &sigma)
return_type< T_y, T_loc, T_scale >::type lognormal_log(const T_y &y, const T_loc &mu, const T_scale &sigma)
fvar< T > erfc(const fvar< T > &x)
DoubleVectorView allocates double values to be used as intermediate values.
bool check_finite(const char *function, const T_y &y, const char *name, T_result *result)
Checks if the variable y is finite.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
A variable implementation that stores operands and derivatives with respect to the variable...
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
Metaprogram to determine if a type has a base scalar type that can be assigned to type double...
double value_of(const T x)
Return the value of the specified scalar argument converted to a double value.
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
const double SQRT_2
The value of the square root of 2, .
fvar< T > sqrt(const fvar< T > &x)
VectorView< double *, is_vector< T2 >::value, is_constant_struct< T2 >::value > d_x2
bool check_nonnegative(const char *function, const T_y &y, const char *name, T_result *result)
bool check_consistent_sizes(const char *function, const T1 &x1, const T2 &x2, const char *name1, const char *name2, T_result *result)
size_t max_size(const T1 &x1, const T2 &x2)
return_type< T_y, T_loc, T_scale >::type lognormal_cdf_log(const T_y &y, const T_loc &mu, const T_scale &sigma)
bool check_not_nan(const char *function, const T_y &y, const char *name, T_result *result)
Checks if the variable y is nan.
double lognormal_rng(const double mu, const double sigma, RNG &rng)
VectorView< double *, is_vector< T1 >::value, is_constant_struct< T1 >::value > d_x1
VectorView< double *, is_vector< T3 >::value, is_constant_struct< T3 >::value > d_x3
double pi()
Return the value of pi.
fvar< T > log(const fvar< T > &x)
VectorView is a template metaprogram that takes its argument and allows it to be used like a vector...
fvar< T > exp(const fvar< T > &x)
double negative_infinity()
Return negative infinity.