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,
20 typename return_type<T_prob>::type
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) {