1 #ifndef STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__LOGISTIC_HPP
2 #define STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__LOGISTIC_HPP
4 #include <boost/random/exponential_distribution.hpp>
5 #include <boost/random/variate_generator.hpp>
24 template <
bool propto,
25 typename T_y,
typename T_loc,
typename T_scale>
26 typename return_type<T_y,T_loc,T_scale>::type
27 logistic_log(
const T_y& y,
const T_loc& mu,
const T_scale& sigma) {
28 static const char*
function =
"stan::prob::logistic_log(%1%)";
52 "Random variable",
"Location parameter",
63 operands_and_partials(y, mu, sigma);
74 for (
size_t i = 0; i <
length(sigma); i++) {
75 inv_sigma[i] = 1.0 /
value_of(sigma_vec[i]);
82 exp_mu_div_sigma(
max_size(mu,sigma));
87 for (
size_t n = 0; n <
max_size(mu,sigma); n++)
90 for (
size_t n = 0; n <
max_size(y,sigma); n++)
96 for (
size_t n = 0; n < N; n++) {
97 const double y_dbl =
value_of(y_vec[n]);
98 const double mu_dbl =
value_of(mu_vec[n]);
100 const double y_minus_mu = y_dbl - mu_dbl;
101 const double y_minus_mu_div_sigma = y_minus_mu * inv_sigma[n];
102 double exp_m_y_minus_mu_div_sigma(0);
104 exp_m_y_minus_mu_div_sigma =
exp(-y_minus_mu_div_sigma);
105 double inv_1p_exp_y_minus_mu_div_sigma(0);
108 inv_1p_exp_y_minus_mu_div_sigma = 1 / (1 +
exp(y_minus_mu_div_sigma));
111 logp -= y_minus_mu_div_sigma;
113 logp -= log_sigma[n];
115 logp -= 2.0 *
log1p(exp_m_y_minus_mu_div_sigma);
118 operands_and_partials.
d_x1[n]
119 += (2 * inv_1p_exp_y_minus_mu_div_sigma - 1) * inv_sigma[n];
121 operands_and_partials.
d_x2[n] +=
122 (1 - 2 * exp_mu_div_sigma[n] / (exp_mu_div_sigma[n]
123 + exp_y_div_sigma[n]))
126 operands_and_partials.
d_x3[n] +=
127 ((1 - 2 * inv_1p_exp_y_minus_mu_div_sigma)
128 *y_minus_mu*inv_sigma[n] - 1) * inv_sigma[n];
130 return operands_and_partials.
to_var(logp);
133 template <
typename T_y,
typename T_loc,
typename T_scale>
137 return logistic_log<false>(y,mu,sigma);
141 template <
typename T_y,
typename T_loc,
typename T_scale>
151 static const char*
function =
"stan::prob::logistic_cdf(%1%)";
158 using boost::math::tools::promote_args;
166 "Random variable",
"Location parameter",
167 "Scale parameter", &P);
176 operands_and_partials(y, mu, sigma);
182 if (
value_of(y_vec[i]) == -std::numeric_limits<double>::infinity())
183 return operands_and_partials.
to_var(0.0);
187 for (
size_t n = 0; n < N; n++) {
191 if (
value_of(y_vec[n]) == std::numeric_limits<double>::infinity()) {
196 const double y_dbl =
value_of(y_vec[n]);
197 const double mu_dbl =
value_of(mu_vec[n]);
198 const double sigma_dbl =
value_of(sigma_vec[n]);
199 const double sigma_inv_vec = 1.0 /
value_of(sigma_vec[n]);
202 const double Pn = 1.0 / ( 1.0 +
exp( - (y_dbl - mu_dbl)
208 operands_and_partials.
d_x1[n]
211 operands_and_partials.
d_x2[n]
214 operands_and_partials.
d_x3[n] += - (y_dbl - mu_dbl) * sigma_inv_vec
220 operands_and_partials.
d_x1[n] *= P;
224 operands_and_partials.
d_x2[n] *= P;
228 operands_and_partials.
d_x3[n] *= P;
231 return operands_and_partials.
to_var(P);
235 template <
typename T_y,
typename T_loc,
typename T_scale>
244 static const char*
function =
"stan::prob::logistic_cdf_log(%1%)";
251 using boost::math::tools::promote_args;
259 "Random variable",
"Location parameter",
260 "Scale parameter", &P);
269 operands_and_partials(y, mu, sigma);
275 if (
value_of(y_vec[i]) == -std::numeric_limits<double>::infinity())
276 return operands_and_partials.
to_var(-std::numeric_limits<double>::infinity());
280 for (
size_t n = 0; n < N; n++) {
284 if (
value_of(y_vec[n]) == std::numeric_limits<double>::infinity()) {
289 const double y_dbl =
value_of(y_vec[n]);
290 const double mu_dbl =
value_of(mu_vec[n]);
291 const double sigma_dbl =
value_of(sigma_vec[n]);
292 const double sigma_inv_vec = 1.0 /
value_of(sigma_vec[n]);
295 const double Pn = 1.0 / ( 1.0 +
exp( - (y_dbl - mu_dbl)
300 operands_and_partials.
d_x1[n]
303 operands_and_partials.
d_x2[n]
306 operands_and_partials.
d_x3[n] += - (y_dbl - mu_dbl) * sigma_inv_vec
310 return operands_and_partials.
to_var(P);
313 template <
typename T_y,
typename T_loc,
typename T_scale>
322 static const char*
function =
"stan::prob::logistic_cdf_log(%1%)";
329 using boost::math::tools::promote_args;
337 "Random variable",
"Location parameter",
338 "Scale parameter", &P);
347 operands_and_partials(y, mu, sigma);
353 if (
value_of(y_vec[i]) == -std::numeric_limits<double>::infinity())
354 return operands_and_partials.
to_var(0.0);
358 for (
size_t n = 0; n < N; n++) {
362 if (
value_of(y_vec[n]) == std::numeric_limits<double>::infinity()) {
367 const double y_dbl =
value_of(y_vec[n]);
368 const double mu_dbl =
value_of(mu_vec[n]);
369 const double sigma_dbl =
value_of(sigma_vec[n]);
370 const double sigma_inv_vec = 1.0 /
value_of(sigma_vec[n]);
373 const double Pn = 1.0 - 1.0 / ( 1.0 +
exp( - (y_dbl - mu_dbl)
378 operands_and_partials.
d_x1[n]
381 operands_and_partials.
d_x2[n]
384 operands_and_partials.
d_x3[n] -= - (y_dbl - mu_dbl) * sigma_inv_vec
388 return operands_and_partials.
to_var(P);
396 using boost::variate_generator;
397 using boost::random::exponential_distribution;
399 static const char*
function =
"stan::prob::logistic_rng(%1%)";
404 check_finite(
function, mu,
"Location parameter", (
double*)0);
407 variate_generator<RNG&, exponential_distribution<> >
408 exp_rng(rng, exponential_distribution<>(1));
409 return mu - sigma *
std::log(exp_rng() / exp_rng());
return_type< T_y, T_loc, T_scale >::type logistic_cdf_log(const T_y &y, const T_loc &mu, const T_scale &sigma)
boost::math::tools::promote_args< T >::type log1p(const T x)
Return the natural logarithm of one plus the specified value.
T_return_type to_var(double logp)
bool check_positive_finite(const char *function, const T_y &y, const char *name, T_result *result)
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.
return_type< T_y, T_loc, T_scale >::type logistic_log(const T_y &y, const T_loc &mu, const T_scale &sigma)
T value_of(const fvar< T > &v)
Return the value of the specified variable.
return_type< T_y, T_loc, T_scale >::type logistic_ccdf_log(const T_y &y, const T_loc &mu, const T_scale &sigma)
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...
VectorView< double *, is_vector< T2 >::value, is_constant_struct< T2 >::value > d_x2
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)
double logistic_rng(const double mu, const double sigma, RNG &rng)
bool check_not_nan(const char *function, const T_y &y, const char *name, T_result *result)
Checks if the variable y is nan.
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
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)
fvar< T > log1p(const fvar< T > &x)
double negative_infinity()
Return negative infinity.
return_type< T_y, T_loc, T_scale >::type logistic_cdf(const T_y &y, const T_loc &mu, const T_scale &sigma)