Automatic Differentiation
 
Loading...
Searching...
No Matches
binomial_logit_lpmf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_BINOMIAL_LOGIT_LPMF_HPP
2#define STAN_MATH_PRIM_PROB_BINOMIAL_LOGIT_LPMF_HPP
3
17
18namespace stan {
19namespace math {
20
34template <bool propto, typename T_n, typename T_N, typename T_prob,
36 T_n, T_N, T_prob>* = nullptr>
37return_type_t<T_prob> binomial_logit_lpmf(const T_n& n, const T_N& N,
38 const T_prob& alpha) {
39 using T_partials_return = partials_return_t<T_n, T_N, T_prob>;
40 using T_n_ref = ref_type_if_not_constant_t<T_n>;
41 using T_N_ref = ref_type_if_not_constant_t<T_N>;
42 using T_alpha_ref = ref_type_if_not_constant_t<T_prob>;
43 static constexpr const char* function = "binomial_logit_lpmf";
44 check_consistent_sizes(function, "Successes variable", n,
45 "Population size parameter", N,
46 "Probability parameter", alpha);
47
48 T_n_ref n_ref = n;
49 T_N_ref N_ref = N;
50 T_alpha_ref alpha_ref = alpha;
51
52 decltype(auto) n_val = to_ref(as_value_column_array_or_scalar(n_ref));
53 decltype(auto) N_val = to_ref(as_value_column_array_or_scalar(N_ref));
54 decltype(auto) alpha_val = to_ref(as_value_column_array_or_scalar(alpha_ref));
55
56 check_bounded(function, "Successes variable", value_of(n_val), 0, N_val);
57 check_nonnegative(function, "Population size parameter", N_val);
58 check_finite(function, "Probability parameter", alpha_val);
59
60 if (size_zero(n, N, alpha)) {
61 return 0.0;
62 }
64 return 0.0;
65 }
66 const auto& log_inv_logit_alpha
67 = to_ref_if<!is_constant_all<T_prob>::value>(log_inv_logit(alpha_val));
68 const auto& log1m_inv_logit_alpha
69 = to_ref_if<!is_constant_all<T_prob>::value>(log1m_inv_logit(alpha_val));
70
71 size_t maximum_size = max_size(n, N, alpha);
72 T_partials_return logp = sum(n_val * log_inv_logit_alpha
73 + (N_val - n_val) * log1m_inv_logit_alpha);
75 logp += sum(binomial_coefficient_log(N_val, n_val)) * maximum_size
76 / max_size(n, N);
77 }
78
79 auto ops_partials = make_partials_propagator(alpha_ref);
81 edge<0>(ops_partials).partials_ = n_val - N_val * exp(log_inv_logit_alpha);
82 }
83
84 return ops_partials.build(logp);
85}
86
87template <typename T_n, typename T_N, typename T_prob>
88inline return_type_t<T_prob> binomial_logit_lpmf(const T_n& n, const T_N& N,
89 const T_prob& alpha) {
90 return binomial_logit_lpmf<false>(n, N, alpha);
91}
92
93} // namespace math
94} // namespace stan
95#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.
binomial_coefficient_log_< as_operation_cl_t< T1 >, as_operation_cl_t< T2 > > binomial_coefficient_log(T1 &&a, T2 &&b)
return_type_t< T_prob_cl > binomial_logit_lpmf(const T_n_cl &n, const T_N_cl N, const T_prob_cl &alpha)
Binomial log PMF in logit parametrization.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
void check_nonnegative(const char *function, const char *name, const T_y &y)
Check if y is non-negative.
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.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
fvar< T > log_inv_logit(const fvar< T > &x)
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
void check_finite(const char *function, const char *name, const T_y &y)
Return true if all values in y are finite.
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:23
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:20
fvar< T > log1m_inv_logit(const fvar< T > &x)
Return the natural logarithm of one minus the inverse logit of the specified argument.
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:13
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 ...
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...