Automatic Differentiation
 
Loading...
Searching...
No Matches
bernoulli_logit_lpmf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_BERNOULLI_LOGIT_LPMF_HPP
2#define STAN_MATH_PRIM_PROB_BERNOULLI_LOGIT_LPMF_HPP
3
16#include <cmath>
17
18namespace stan {
19namespace math {
20
33template <bool propto, typename T_n, typename T_prob,
35 T_n, T_prob>* = nullptr>
36return_type_t<T_prob> bernoulli_logit_lpmf(const T_n& n, const T_prob& theta) {
37 using T_partials_return = partials_return_t<T_n, T_prob>;
38 using T_partials_array = Eigen::Array<T_partials_return, Eigen::Dynamic, 1>;
39 using std::exp;
40 using T_n_ref = ref_type_if_not_constant_t<T_n>;
41 using T_theta_ref = ref_type_if_not_constant_t<T_prob>;
42 static constexpr const char* function = "bernoulli_logit_lpmf";
43 check_consistent_sizes(function, "Random variable", n,
44 "Probability parameter", theta);
45 if (size_zero(n, theta)) {
46 return 0.0;
47 }
48 T_n_ref n_ref = n;
49 T_theta_ref theta_ref = theta;
50 check_bounded(function, "n", n_ref, 0, 1);
51
52 decltype(auto) theta_val = to_ref(as_value_column_array_or_scalar(theta_ref));
53
54 check_not_nan(function, "Logit transformed probability parameter", theta_val);
56 return 0.0;
57 }
58
59 const auto& n_col = as_column_vector_or_scalar(n_ref);
60 const auto& n_double = value_of_rec(n_col);
61
62 auto signs = to_ref_if<!is_constant<T_prob>::value>(
63 (2 * as_array_or_scalar(n_double) - 1));
64 T_partials_array ntheta;
66 ntheta = forward_as<T_partials_array>(signs * theta_val);
67 } else {
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);
71 }
72 T_partials_array exp_m_ntheta = exp(-ntheta);
73 static constexpr double cutoff = 20.0;
74 T_partials_return logp = sum(
75 (ntheta > cutoff)
76 .select(-exp_m_ntheta,
77 (ntheta < -cutoff).select(ntheta, -log1p(exp_m_ntheta))));
78
79 auto ops_partials = make_partials_propagator(theta_ref);
81 edge<0>(ops_partials).partials_
82 = (ntheta > cutoff)
83 .select(-exp_m_ntheta,
84 (ntheta >= -cutoff)
85 .select(signs * exp_m_ntheta / (exp_m_ntheta + 1),
86 signs));
87 }
88 return ops_partials.build(logp);
89}
90
91template <typename T_n, typename T_prob>
93 const T_prob& theta) {
94 return bernoulli_logit_lpmf<false>(n, theta);
95}
96
97} // namespace math
98} // namespace stan
99#endif
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.
Definition select.hpp:148
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.
Definition size_zero.hpp:19
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.
Definition to_ref.hpp:17
fvar< T > log1p(const fvar< T > &x)
Definition log1p.hpp:12
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.
Definition sum.hpp:23
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:15
typename ref_type_if<!is_constant< T >::value, T >::type ref_type_if_not_constant_t
Definition ref_type.hpp:62
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...