1 #ifndef STAN_MATH_PRIM_SCAL_PROB_BERNOULLI_LPMF_HPP 2 #define STAN_MATH_PRIM_SCAL_PROB_BERNOULLI_LPMF_HPP 28 template <
bool propto,
typename T_n,
typename T_prob>
30 const T_prob& theta) {
31 static const char*
function =
"bernoulli_lpmf";
40 T_partials_return logp(0.0);
44 check_bounded(
function,
"Probability parameter", theta, 0.0, 1.0);
46 "Probability parameter", theta);
58 for (
size_t n = 0; n < N; n++) {
61 const T_partials_return theta_dbl =
value_of(theta_vec[0]);
64 logp += N *
log(theta_dbl);
66 ops_partials.
edge1_.partials_[0] += N / theta_dbl;
67 }
else if (sum == 0) {
68 logp += N *
log1m(theta_dbl);
70 ops_partials.
edge1_.partials_[0] += N / (theta_dbl - 1);
72 const T_partials_return log_theta =
log(theta_dbl);
73 const T_partials_return log1m_theta =
log1m(theta_dbl);
75 logp += sum * log_theta;
76 logp += (N -
sum) * log1m_theta;
79 ops_partials.
edge1_.partials_[0] += sum / theta_dbl;
80 ops_partials.
edge1_.partials_[0] += (N -
sum) / (theta_dbl - 1);
84 for (
size_t n = 0; n < N; n++) {
85 const int n_int =
value_of(n_vec[n]);
86 const T_partials_return theta_dbl =
value_of(theta_vec[n]);
89 logp +=
log(theta_dbl);
91 logp +=
log1m(theta_dbl);
95 ops_partials.
edge1_.partials_[n] += 1.0 / theta_dbl;
97 ops_partials.
edge1_.partials_[n] += 1.0 / (theta_dbl - 1);
101 return ops_partials.
build(logp);
104 template <
typename T_y,
typename T_prob>
106 const T_prob& theta) {
107 return bernoulli_lpmf<false>(n, theta);
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
void check_finite(const char *function, const char *name, const T_y &y)
Check if y is finite.
boost::math::tools::promote_args< double, typename partials_type< typename scalar_type< T >::type >::type, typename partials_return_type< T_pack... >::type >::type type
void check_bounded(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
Check if the value is between the low and high values, inclusively.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...
fvar< T > log(const fvar< T > &x)
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
This template builds partial derivatives with respect to a set of operands.
size_t length(const std::vector< T > &x)
Returns the length of the provided std::vector.
bool size_zero(T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
return_type< T_prob >::type bernoulli_lpmf(const T_n &n, const T_prob &theta)
Returns the log PMF of the Bernoulli distribution.
boost::math::tools::promote_args< double, typename scalar_type< T >::type, typename return_type< Types_pack... >::type >::type type
size_t max_size(const T1 &x1, const T2 &x2)
T_return_type build(double value)
Build the node to be stored on the autodiff graph.
void check_consistent_sizes(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)
Check if the dimension of x1 is consistent with x2.
fvar< T > log1m(const fvar< T > &x)
internal::ops_partials_edge< double, Op1 > edge1_