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
17#include <cmath>
18
19namespace stan {
20namespace math {
21
34template <bool propto, typename T_n, typename T_prob,
36 T_n, T_prob>* = nullptr>
38 const T_prob& theta) {
39 using T_partials_return = partials_return_t<T_n, T_prob>;
40 using T_partials_array = Eigen::Array<T_partials_return, Eigen::Dynamic, 1>;
41 using std::exp;
42 using T_n_ref = ref_type_if_not_constant_t<T_n>;
43 using T_theta_ref = ref_type_if_not_constant_t<T_prob>;
44 static constexpr const char* function = "bernoulli_logit_lpmf";
45 check_consistent_sizes(function, "Random variable", n,
46 "Probability parameter", theta);
47 if (size_zero(n, theta)) {
48 return 0.0;
49 }
50 T_n_ref n_ref = n;
51 T_theta_ref theta_ref = theta;
52 check_bounded(function, "n", n_ref, 0, 1);
53
54 decltype(auto) theta_val = to_ref(as_value_column_array_or_scalar(theta_ref));
55
56 check_not_nan(function, "Logit transformed probability parameter", theta_val);
58 return 0.0;
59 }
60
61 const auto& n_col = as_column_vector_or_scalar(n_ref);
62 const auto& n_double = value_of_rec(n_col);
63
64 auto signs = to_ref_if<is_autodiff_v<T_prob>>(
65 (2 * as_array_or_scalar(n_double) - 1));
66 T_partials_array ntheta;
68 ntheta = signs * theta_val;
69 } else {
70 T_partials_return ntheta_s = signs * theta_val;
71 ntheta = T_partials_array::Constant(1, 1, ntheta_s);
72 }
73 T_partials_array exp_m_ntheta = exp(-ntheta);
74 static constexpr double cutoff = 20.0;
75 T_partials_return logp = sum(
76 (ntheta > cutoff)
77 .select(-exp_m_ntheta,
78 (ntheta < -cutoff).select(ntheta, -log1p(exp_m_ntheta))));
79
80 auto ops_partials = make_partials_propagator(theta_ref);
81 if constexpr (is_autodiff_v<T_prob>) {
82 edge<0>(ops_partials).partials_
83 = (ntheta > cutoff)
84 .select(
85 -exp_m_ntheta,
86 (ntheta >= -cutoff)
87 .select(promote_scalar<T_partials_return>(
88 signs * exp_m_ntheta / (exp_m_ntheta + 1)),
89 promote_scalar<T_partials_return>(signs)));
90 }
91 return ops_partials.build(logp);
92}
93
94template <typename T_n, typename T_prob>
96 const T_prob& theta) {
97 return bernoulli_logit_lpmf<false>(n, theta);
98}
99
100} // namespace math
101} // namespace stan
102#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.
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
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:18
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_autodiff_v< T >, T >::type ref_type_if_not_constant_t
Definition ref_type.hpp:63
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...
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...