1#ifndef STAN_MATH_PRIM_PROB_BERNOULLI_LPMF_HPP
2#define STAN_MATH_PRIM_PROB_BERNOULLI_LPMF_HPP
31template <
bool propto,
typename T_n,
typename T_prob,
33 T_n, T_prob>* =
nullptr>
39 static constexpr const char* function =
"bernoulli_lpmf";
41 "Probability parameter", theta);
42 const T_n_ref n_ref =
to_ref(n);
43 const T_theta_ref theta_ref =
to_ref(theta);
55 T_partials_return logp(0.0);
64 for (
size_t n = 0; n < N; n++) {
67 const T_partials_return theta_dbl = theta_vec.val(0);
70 logp += N *
log(theta_dbl);
72 partials<0>(ops_partials)[0] += N / theta_dbl;
74 }
else if (
sum == 0) {
75 logp += N *
log1m(theta_dbl);
77 partials<0>(ops_partials)[0] += N / (theta_dbl - 1);
80 const T_partials_return log_theta =
log(theta_dbl);
81 const T_partials_return log1m_theta =
log1m(theta_dbl);
83 logp +=
sum * log_theta;
84 logp += (N -
sum) * log1m_theta;
87 partials<0>(ops_partials)[0] +=
sum *
inv(theta_dbl);
88 partials<0>(ops_partials)[0] += (N -
sum) *
inv(theta_dbl - 1);
92 for (
size_t n = 0; n < N; n++) {
93 const int n_int = n_vec.val(n);
94 const T_partials_return theta_dbl = theta_vec.val(n);
97 logp +=
log(theta_dbl);
99 logp +=
log1m(theta_dbl);
104 partials<0>(ops_partials)[n] +=
inv(theta_dbl);
106 partials<0>(ops_partials)[n] +=
inv(theta_dbl - 1);
111 return ops_partials.build(logp);
114template <
typename T_y,
typename T_prob>
116 return bernoulli_lpmf<false>(n, theta);
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
require_all_not_t< is_nonscalar_prim_or_rev_kernel_expression< std::decay_t< Types > >... > require_all_not_nonscalar_prim_or_rev_kernel_expression_t
Require none of the types satisfy is_nonscalar_prim_or_rev_kernel_expression.
return_type_t< T_prob_cl > bernoulli_lpmf(const T_n_cl &n, const T_prob_cl &theta)
Returns the log PMF of the Bernoulli distribution.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
int64_t size(const T &m)
Returns the size (number of the elements) of a matrix_cl or var_value<matrix_cl<T>>.
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
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.
fvar< T > log(const fvar< T > &x)
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
fvar< T > log1m(const fvar< T > &x)
fvar< T > inv(const fvar< T > &x)
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
typename ref_type_if< true, T >::type ref_type_t
typename partials_return_type< Args... >::type partials_return_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...