Automatic Differentiation
 
Loading...
Searching...
No Matches
binomial_logit_glm_lpmf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_BINOMIAL_LOGIT_GLM_LPMF_HPP
2#define STAN_MATH_PRIM_PROB_BINOMIAL_LOGIT_GLM_LPMF_HPP
3
17
18namespace stan {
19namespace math {
20
54template <bool propto, typename T_n, typename T_N, typename T_x,
55 typename T_alpha, typename T_beta, require_matrix_t<T_x>* = nullptr>
57 const T_n& n, const T_N& N, const T_x& x, const T_alpha& alpha,
58 const T_beta& beta) {
59 constexpr int T_x_rows = T_x::RowsAtCompileTime;
60 using T_xbeta_partials = partials_return_t<T_x, T_beta>;
61 using T_partials_return = partials_return_t<T_x, T_alpha, T_beta>;
62 using T_xbeta_tmp =
63 typename std::conditional_t<T_x_rows == 1, T_xbeta_partials,
64 Eigen::Array<T_xbeta_partials, -1, 1>>;
65 using T_n_ref = ref_type_if_t<!is_constant<T_n>::value, T_n>;
66 using T_N_ref = ref_type_if_t<!is_constant<T_N>::value, T_N>;
67 using T_x_ref = ref_type_if_t<!is_constant<T_x>::value, T_x>;
68 using T_alpha_ref = ref_type_if_t<!is_constant<T_alpha>::value, T_alpha>;
69 using T_beta_ref = ref_type_if_t<!is_constant<T_beta>::value, T_beta>;
70
71 T_n_ref n_ref = n;
72 T_N_ref N_ref = N;
73 T_x_ref x_ref = x;
74 T_alpha_ref alpha_ref = alpha;
75 T_beta_ref beta_ref = beta;
76
77 if (size_zero(n, N, alpha, beta, x)) {
78 return 0;
79 }
80
82 return 0;
83 }
84
85 const size_t N_instances = max_size(n, N, x.col(0), alpha);
86 const size_t N_attributes = x.cols();
87
88 static const char* function = "binomial_logit_glm_lpmf";
89 check_consistent_sizes(function, "Successes variable", n,
90 "Population size parameter", N);
91 check_consistent_size(function, "Successes variable", n, N_instances);
92 check_consistent_size(function, "Population size parameter", N, N_instances);
93 check_consistent_size(function, "Weight vector", beta, N_attributes);
94 check_consistent_size(function, "Vector of intercepts", alpha, N_instances);
95
96 auto&& n_val = as_value_column_array_or_scalar(n_ref);
97 auto&& N_val = as_value_column_array_or_scalar(N_ref);
98
99 check_bounded(function, "Successes variable", n_val, 0, N_val);
100 check_nonnegative(function, "Population size parameter", N_val);
101
102 auto&& alpha_val = as_value_column_array_or_scalar(alpha_ref);
103 auto&& beta_val = as_value_column_vector_or_scalar(beta_ref);
104 auto&& x_val = value_of(x_ref);
105 Eigen::Array<T_partials_return, -1, 1> theta(N_instances);
106 if (T_x_rows == 1) {
107 theta = forward_as<T_xbeta_tmp>((x_val * beta_val)(0, 0)) + alpha_val;
108 } else {
109 theta = (x_val * beta_val).array() + alpha_val;
110 }
111
112 constexpr bool gradients_calc = !is_constant_all<T_beta, T_x, T_alpha>::value;
113 auto&& log_inv_logit_theta = to_ref_if<gradients_calc>(log_inv_logit(theta));
114
115 T_partials_return logp = sum(n_val * log_inv_logit_theta
116 + (N_val - n_val) * log1m_inv_logit(theta));
117
118 using std::isfinite;
119 if (!isfinite(logp)) {
120 check_finite(function, "Weight vector", beta);
121 check_finite(function, "Intercept", alpha);
122 check_finite(function, "Matrix of independent variables", x);
123 }
124
126 size_t broadcast_n = max_size(N, n) == N_instances ? 1 : N_instances;
127 logp += sum(binomial_coefficient_log(N_val, n_val)) * broadcast_n;
128 }
129
130 auto ops_partials = make_partials_propagator(x_ref, alpha_ref, beta_ref);
131 if (gradients_calc) {
132 Eigen::Matrix<T_partials_return, -1, 1> theta_derivative
133 = n_val - N_val * exp(log_inv_logit_theta);
134
136 if (T_x_rows == 1) {
137 edge<2>(ops_partials).partials_
138 = forward_as<Eigen::Matrix<T_partials_return, 1, -1>>(
139 theta_derivative.sum() * x_val);
140 } else {
141 partials<2>(ops_partials) = x_val.transpose() * theta_derivative;
142 }
143 }
144
146 if (T_x_rows == 1) {
147 edge<0>(ops_partials).partials_
148 = forward_as<Eigen::Array<T_partials_return, -1, T_x_rows>>(
149 beta_val * theta_derivative.sum());
150 } else {
151 edge<0>(ops_partials).partials_
152 = (beta_val * theta_derivative.transpose()).transpose();
153 }
154 }
156 partials<1>(ops_partials) = theta_derivative;
157 }
158 }
159 return ops_partials.build(logp);
160}
161
162template <typename T_n, typename T_N, typename T_x, typename T_alpha,
163 typename T_beta>
165 const T_n& n, const T_N& N, const T_x& x, const T_alpha& alpha,
166 const T_beta& beta) {
167 return binomial_logit_glm_lpmf<false>(n, N, x, alpha, beta);
168}
169} // namespace math
170} // namespace stan
171#endif
isfinite_< as_operation_cl_t< T > > isfinite(T &&a)
binomial_coefficient_log_< as_operation_cl_t< T1 >, as_operation_cl_t< T2 > > binomial_coefficient_log(T1 &&a, T2 &&b)
auto transpose(Arg &&a)
Transposes a kernel generator expression.
T_actual && forward_as(T_actual &&a)
Assume which type we get.
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.
void check_consistent_size(const char *function, const char *name, const T &x, size_t expected_size)
Check if x is consistent with size expected_size.
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.
return_type_t< T_x_cl, T_alpha_cl, T_beta_cl > binomial_logit_glm_lpmf(const T_n_cl &n, const T_N_cl &N, const T_x_cl &x, const T_alpha_cl &alpha, const T_beta_cl &beta)
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
auto as_value_column_vector_or_scalar(T &&a)
Extract values from input argument and transform to a column vector.
fvar< T > beta(const fvar< T > &x1, const fvar< T > &x2)
Return fvar with the beta function applied to the specified arguments and its gradient.
Definition beta.hpp:51
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:15
typename ref_type_if< Condition, T >::type ref_type_if_t
Definition ref_type.hpp:58
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...