1 #ifndef __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__LOGISTIC_HPP__
2 #define __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__LOGISTIC_HPP__
16 template <
bool propto,
17 typename T_y,
typename T_loc,
typename T_scale,
22 static const char*
function =
"stan::prob::logistic_log(%1%)";
41 if (!
check_finite(
function, y,
"Random variable", &logp, Policy()))
46 if (!
check_finite(
function, sigma,
"Scale parameter", &logp,
54 "Random variable",
"Location parameter",
"Scale parameter",
73 for (
size_t i = 0; i <
length(sigma); i++) {
74 inv_sigma[i] = 1.0 /
value_of(sigma_vec[i]);
80 exp_mu_div_sigma(
max_size(mu,sigma));
84 for (
size_t n = 0; n <
max_size(mu,sigma); n++)
86 for (
size_t n = 0; n <
max_size(y,sigma); n++)
91 for (
size_t n = 0; n < N; n++) {
92 const double y_dbl =
value_of(y_vec[n]);
93 const double mu_dbl =
value_of(mu_vec[n]);
95 const double y_minus_mu = y_dbl - mu_dbl;
96 const double y_minus_mu_div_sigma = y_minus_mu * inv_sigma[n];
97 double exp_m_y_minus_mu_div_sigma(0);
99 exp_m_y_minus_mu_div_sigma =
exp(-y_minus_mu_div_sigma);
100 double inv_1p_exp_y_minus_mu_div_sigma(0);
102 inv_1p_exp_y_minus_mu_div_sigma = 1 / (1 +
exp(y_minus_mu_div_sigma));
105 logp -= y_minus_mu_div_sigma;
107 logp -= log_sigma[n];
109 logp -= 2.0 *
log1p(exp_m_y_minus_mu_div_sigma);
112 operands_and_partials.
d_x1[n] += (2 * inv_1p_exp_y_minus_mu_div_sigma - 1) * inv_sigma[n];
114 operands_and_partials.
d_x2[n] +=
115 (1 - 2 * exp_mu_div_sigma[n] / (exp_mu_div_sigma[n] + exp_y_div_sigma[n])) * inv_sigma[n];
117 operands_and_partials.
d_x3[n] +=
118 ((1 - 2 * inv_1p_exp_y_minus_mu_div_sigma)*y_minus_mu*inv_sigma[n] - 1) * inv_sigma[n];
120 return operands_and_partials.
to_var(logp);
123 template <
bool propto,
124 typename T_y,
typename T_loc,
typename T_scale>
131 template <
typename T_y,
typename T_loc,
typename T_scale,
137 return logistic_log<false>(y,mu,sigma,Policy());
140 template <
typename T_y,
typename T_loc,
typename T_scale>
var log1p(const stan::agrad::var &a)
The log (1 + x) 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).
var exp(const var &a)
Return the exponentiation of the specified variable (cmath).
boost::math::tools::promote_args< T >::type log1p(T x)
Return the natural logarithm of one plus the specified value.
double value_of(T x)
Return the value of the specified scalar argument converted to a double value.
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 logistic_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