1#ifndef STAN_MATH_PRIM_PROB_BERNOULLI_LOGIT_LPMF_HPP
2#define STAN_MATH_PRIM_PROB_BERNOULLI_LOGIT_LPMF_HPP
33template <
bool propto,
typename T_n,
typename T_prob,
35 T_n, T_prob>* =
nullptr>
38 using T_partials_array = Eigen::Array<T_partials_return, Eigen::Dynamic, 1>;
42 static constexpr const char* function =
"bernoulli_logit_lpmf";
44 "Probability parameter", theta);
49 T_theta_ref theta_ref = theta;
54 check_not_nan(function,
"Logit transformed probability parameter", theta_val);
62 auto signs = to_ref_if<!is_constant<T_prob>::value>(
64 T_partials_array ntheta;
66 ntheta = forward_as<T_partials_array>(signs * theta_val);
68 T_partials_return ntheta_s
69 = forward_as<T_partials_return>(signs * theta_val);
70 ntheta = T_partials_array::Constant(1, 1, ntheta_s);
72 T_partials_array exp_m_ntheta =
exp(-ntheta);
73 static constexpr double cutoff = 20.0;
74 T_partials_return logp =
sum(
77 (ntheta < -cutoff).
select(ntheta, -
log1p(exp_m_ntheta))));
81 edge<0>(ops_partials).partials_
85 .select(signs * exp_m_ntheta / (exp_m_ntheta + 1),
88 return ops_partials.build(logp);
91template <
typename T_n,
typename T_prob>
93 const T_prob& theta) {
94 return bernoulli_logit_lpmf<false>(n, theta);
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.
select_< as_operation_cl_t< T_condition >, as_operation_cl_t< T_then >, as_operation_cl_t< T_else > > select(T_condition &&condition, T_then &&then, T_else &&els)
Selection operation on kernel generator expressions.
auto as_column_vector_or_scalar(T &&a)
as_column_vector_or_scalar of a kernel generator expression.
return_type_t< T_prob_cl > bernoulli_logit_lpmf(const T_n_cl &n, const T_prob_cl &theta)
Returns the log PMF of the logit-parametrized Bernoulli distribution.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
double value_of_rec(const fvar< T > &v)
Return the value of the specified variable.
T as_array_or_scalar(T &&v)
Returns specified input value.
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.
auto as_value_column_array_or_scalar(T &&a)
Extract the value from an object and for eigen vectors and std::vectors convert to an eigen column ar...
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.
fvar< T > log1p(const fvar< T > &x)
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
fvar< T > exp(const fvar< T > &x)
typename ref_type_if<!is_constant< T >::value, T >::type ref_type_if_not_constant_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 ...
If the input type T is either an eigen matrix with 1 column or 1 row at compile time or a standard ve...
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...