1 #ifndef __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__DISCRETE__BERNOULLI_HPP__
2 #define __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__DISCRETE__BERNOULLI_HPP__
17 template <
bool propto,
18 typename T_n,
typename T_prob,
24 static const char*
function =
"stan::prob::bernoulli_log(%1%)";
44 if (!
check_finite(
function, theta,
"Probability parameter", &logp, Policy()))
47 "Probability parameter", &logp, Policy()))
51 "Random variable",
"Probability parameter",
67 for (
size_t n = 0; n < N; n++) {
70 const double theta_dbl =
value_of(theta_vec[0]);
71 const double log_theta =
log(theta_dbl);
72 const double log1m_theta =
log1m(theta_dbl);
74 logp +=
sum * log_theta;
75 logp += (N -
sum) * log1m_theta;
77 operands_and_partials.
d_x1[0] +=
sum / theta_dbl;
78 operands_and_partials.
d_x1[0] += (N -
sum) / (theta_dbl - 1);
81 for (
size_t n = 0; n < N; n++) {
83 const int n_int =
value_of(n_vec[n]);
84 const double theta_dbl =
value_of(theta_vec[n]);
88 logp +=
log(theta_dbl);
90 logp +=
log1m(theta_dbl);
96 operands_and_partials.
d_x1[n] += 1.0 / theta_dbl;
98 operands_and_partials.
d_x1[n] += 1.0 / (theta_dbl - 1);
102 return operands_and_partials.
to_var(logp);
105 template <
bool propto,
111 const T_prob& theta) {
115 template <
typename T_y,
123 return bernoulli_log<false>(n,theta,Policy());
126 template <
typename T_y,
typename T_prob>
130 const T_prob& theta) {
137 template <
bool propto,
145 static const char*
function =
"stan::prob::bernoulli_logit_log(%1%)";
167 if (!
check_not_nan(
function, theta,
"Logit transformed probability parameter",
172 "Random variable",
"Probability parameter",
186 for (
size_t n = 0; n < N; n++) {
188 const int n_int =
value_of(n_vec[n]);
189 const double theta_dbl =
value_of(theta_vec[n]);
192 const int sign = 2*n_int-1;
193 const double ntheta = sign * theta_dbl;
194 const double exp_m_ntheta =
exp(-ntheta);
198 const static double cutoff = 20.0;
200 logp -= exp_m_ntheta;
201 else if (ntheta < -cutoff)
204 logp -=
log1p(exp_m_ntheta);
209 const static double cutoff = 20.0;
211 operands_and_partials.
d_x1[n] -= exp_m_ntheta;
212 else if (ntheta < -cutoff)
213 operands_and_partials.
d_x1[n] += sign;
215 operands_and_partials.
d_x1[n] += sign * exp_m_ntheta / (exp_m_ntheta + 1);
218 return operands_and_partials.
to_var(logp);
222 template <
bool propto,
228 const T_prob& theta) {
233 template <
typename T_n,
241 return bernoulli_logit_log<false>(n,theta,Policy());
245 template <
typename T_n,
250 const T_prob& theta) {
var log1m(const stan::agrad::var &a)
The log (1 - x) function for variables.
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 sum(const Eigen::Matrix< var, R, C > &m)
Returns the sum of the coefficients of the specified matrix, column vector or row vector.
var exp(const var &a)
Return the exponentiation of the specified variable (cmath).
boost::math::tools::promote_args< T >::type log1m(T x)
Return the natural logarithm of one minus the specified value.
boost::math::tools::promote_args< T >::type log1p(T x)
Return the natural logarithm of one plus the specified value.
boost::math::tools::promote_args< T >::type inv_logit(T a)
Returns the inverse logit function applied to the argument.
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_bounded(const char *function, const T_y &y, const T_low &low, const T_high &high, 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_prob >::type bernoulli_log(const T_n &n, const T_prob &theta, const Policy &)
return_type< T_prob >::type bernoulli_logit_log(const T_n &n, const T_prob &theta, 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
T_return_type to_var(double logp)
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