Automatic Differentiation
 
Loading...
Searching...
No Matches
bernoulli_logit_glm_lpmf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_BERNOULLI_LOGIT_GLM_LPMF_HPP
2#define STAN_MATH_PRIM_PROB_BERNOULLI_LOGIT_GLM_LPMF_HPP
3
17#include <cmath>
18
19namespace stan {
20namespace math {
21
49template <bool propto, typename T_y, typename T_x, typename T_alpha,
50 typename T_beta, require_matrix_t<T_x>* = nullptr>
52 const T_y& y, const T_x& x, const T_alpha& alpha, const T_beta& beta) {
53 using Eigen::Array;
54 using Eigen::Dynamic;
55 using Eigen::log1p;
56 using Eigen::Matrix;
57 using std::exp;
58 using std::isfinite;
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_y, T_x, T_alpha, T_beta>;
62 using T_ytheta_tmp =
63 typename std::conditional_t<T_x_rows == 1, T_partials_return,
64 Array<T_partials_return, Dynamic, 1>>;
65 using T_xbeta_tmp =
66 typename std::conditional_t<T_x_rows == 1, T_xbeta_partials,
67 Array<T_xbeta_partials, Dynamic, 1>>;
68 using T_x_ref = ref_type_if_not_constant_t<T_x>;
69 using T_alpha_ref = ref_type_if_not_constant_t<T_alpha>;
70 using T_beta_ref = ref_type_if_not_constant_t<T_beta>;
71
72 const size_t N_instances = T_x_rows == 1 ? stan::math::size(y) : x.rows();
73 const size_t N_attributes = x.cols();
74
75 static constexpr const char* function = "bernoulli_logit_glm_lpmf";
76 check_consistent_size(function, "Vector of dependent variables", y,
77 N_instances);
78 check_consistent_size(function, "Weight vector", beta, N_attributes);
79 check_consistent_size(function, "Vector of intercepts", alpha, N_instances);
80 if (size_zero(y)) {
81 return 0;
82 }
83
84 const auto& y_ref = to_ref(y);
85 check_bounded(function, "Vector of dependent variables", y_ref, 0, 1);
86
88 return 0;
89 }
90
91 T_x_ref x_ref = x;
92 T_alpha_ref alpha_ref = alpha;
93 T_beta_ref beta_ref = beta;
94
95 const auto& y_val = value_of(y_ref);
96 const auto& x_val = to_ref_if<!is_constant<T_beta>::value>(value_of(x_ref));
97 const auto& alpha_val = value_of(alpha_ref);
98 const auto& beta_val = value_of(beta_ref);
99
100 const auto& y_val_vec = as_column_vector_or_scalar(y_val);
101 const auto& alpha_val_vec = as_column_vector_or_scalar(alpha_val);
102 const auto& beta_val_vec = to_ref_if<!is_constant<T_x>::value>(
104
105 auto signs = to_ref_if<!is_constant_all<T_beta, T_x, T_alpha>::value>(
106 2 * as_array_or_scalar(y_val_vec) - 1);
107
108 Array<T_partials_return, Dynamic, 1> ytheta(N_instances);
109 if (T_x_rows == 1) {
110 T_ytheta_tmp ytheta_tmp
111 = forward_as<T_xbeta_tmp>((x_val * beta_val_vec)(0, 0));
112 ytheta = signs * (ytheta_tmp + as_array_or_scalar(alpha_val_vec));
113 } else {
114 ytheta = (x_val * beta_val_vec).array();
115 ytheta = signs * (ytheta + as_array_or_scalar(alpha_val_vec));
116 }
117
118 // Compute the log-density and handle extreme values gracefully
119 // using Taylor approximations.
120 // And compute the derivatives wrt theta.
121 static constexpr double cutoff = 20.0;
122 Eigen::Array<T_partials_return, Dynamic, 1> exp_m_ytheta = exp(-ytheta);
123 T_partials_return logp = sum(
124 (ytheta > cutoff)
125 .select(-exp_m_ytheta,
126 (ytheta < -cutoff).select(ytheta, -log1p(exp_m_ytheta))));
127
128 if (!isfinite(logp)) {
129 check_finite(function, "Weight vector", beta);
130 check_finite(function, "Intercept", alpha);
131 check_finite(function, "Matrix of independent variables", ytheta);
132 }
133
134 auto ops_partials = make_partials_propagator(x_ref, alpha_ref, beta_ref);
135 // Compute the necessary derivatives.
137 Matrix<T_partials_return, Dynamic, 1> theta_derivative
138 = (ytheta > cutoff)
139 .select(-exp_m_ytheta,
140 (ytheta < -cutoff)
141 .select(signs * T_partials_return(1.0),
142 signs * exp_m_ytheta / (exp_m_ytheta + 1)));
144 if (T_x_rows == 1) {
145 edge<2>(ops_partials).partials_
146 = forward_as<Matrix<T_partials_return, 1, Dynamic>>(
147 theta_derivative.sum() * x_val);
148 } else {
149 partials<2>(ops_partials) = x_val.transpose() * theta_derivative;
150 }
151 }
153 if (T_x_rows == 1) {
154 edge<0>(ops_partials).partials_
155 = forward_as<Array<T_partials_return, Dynamic, T_x_rows>>(
156 beta_val_vec * theta_derivative.sum());
157 } else {
158 edge<0>(ops_partials).partials_
159 = (beta_val_vec * theta_derivative.transpose()).transpose();
160 }
161 }
163 partials<1>(ops_partials) = theta_derivative;
164 }
165 }
166 return ops_partials.build(logp);
167}
168
169template <typename T_y, typename T_x, typename T_alpha, typename T_beta>
171 const T_y& y, const T_x& x, const T_alpha& alpha, const T_beta& beta) {
172 return bernoulli_logit_glm_lpmf<false>(y, x, alpha, beta);
173}
174} // namespace math
175} // namespace stan
176#endif
isfinite_< as_operation_cl_t< T > > isfinite(T &&a)
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.
auto transpose(Arg &&a)
Transposes a kernel generator expression.
return_type_t< T_x_cl, T_alpha_cl, T_beta_cl > bernoulli_logit_glm_lpmf(const T_y_cl &y, const T_x_cl &x, const T_alpha_cl &alpha, const T_beta_cl &beta)
Returns the log PMF of the Generalized Linear Model (GLM) with Bernoulli distribution and logit link ...
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
int64_t size(const T &m)
Returns the size (number of the elements) of a matrix_cl or var_value<matrix_cl<T>>.
Definition size.hpp:19
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.
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
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_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
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
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 ...
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...