Automatic Differentiation
 
Loading...
Searching...
No Matches
neg_binomial_2_log_glm_lpmf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_NEG_BINOMIAL_2_LOG_GLM_LPMF_HPP
2#define STAN_MATH_PRIM_PROB_NEG_BINOMIAL_2_LOG_GLM_LPMF_HPP
3
21#include <cmath>
22
23namespace stan {
24namespace math {
25
63template <bool propto, typename T_y, typename T_x, typename T_alpha,
64 typename T_beta, typename T_precision,
65 require_matrix_t<T_x>* = nullptr>
66inline return_type_t<T_x, T_alpha, T_beta, T_precision>
67neg_binomial_2_log_glm_lpmf(const T_y& y, const T_x& x, const T_alpha& alpha,
68 const T_beta& beta, const T_precision& phi) {
69 using Eigen::Array;
70 using Eigen::Dynamic;
71 using Eigen::exp;
72 using Eigen::log1p;
73 using Eigen::Matrix;
74 constexpr int T_x_rows = T_x::RowsAtCompileTime;
75 using T_partials_return
77 using T_precision_val = typename std::conditional_t<
79 Eigen::Array<partials_return_t<T_precision>, -1, 1>,
81 using T_sum_val = typename std::conditional_t<
83 Eigen::Array<partials_return_t<T_y, T_precision>, -1, 1>,
85 using T_theta_tmp =
86 typename std::conditional_t<T_x_rows == 1, T_partials_return,
87 Array<T_partials_return, Dynamic, 1>>;
88 using T_x_ref = ref_type_if_not_constant_t<T_x>;
89 using T_alpha_ref = ref_type_if_not_constant_t<T_alpha>;
90 using T_beta_ref = ref_type_if_not_constant_t<T_beta>;
92
93 const size_t N_instances = T_x_rows == 1 ? stan::math::size(y) : x.rows();
94 const size_t N_attributes = x.cols();
95
96 static constexpr const char* function = "neg_binomial_2_log_glm_lpmf";
97 check_consistent_size(function, "Vector of dependent variables", y,
98 N_instances);
99 check_consistent_size(function, "Weight vector", beta, N_attributes);
100 check_consistent_size(function, "Vector of precision parameters", phi,
101 N_instances);
102 check_consistent_size(function, "Vector of intercepts", alpha, N_instances);
103 T_alpha_ref alpha_ref = alpha;
104 T_beta_ref beta_ref = beta;
105 const auto& beta_val = value_of(beta_ref);
106 const auto& alpha_val = value_of(alpha_ref);
107 const auto& beta_val_vec = to_ref(as_column_vector_or_scalar(beta_val));
108 const auto& alpha_val_vec = to_ref(as_column_vector_or_scalar(alpha_val));
109 check_finite(function, "Weight vector", beta_val_vec);
110 check_finite(function, "Intercept", alpha_val_vec);
111
112 if (size_zero(y, phi)) {
113 return 0;
114 }
115
116 const auto& y_ref = to_ref(y);
117 T_phi_ref phi_ref = phi;
118
119 const auto& y_val = value_of(y_ref);
120 const auto& phi_val = value_of(phi_ref);
121
122 const auto& y_val_vec = to_ref(as_column_vector_or_scalar(y_val));
123 const auto& phi_val_vec = to_ref(as_column_vector_or_scalar(phi_val));
124 check_nonnegative(function, "Failures variables", y_val_vec);
125 check_positive_finite(function, "Precision parameter", phi_val_vec);
126
127 if constexpr (!include_summand<propto, T_x, T_alpha, T_beta,
128 T_precision>::value) {
129 return 0;
130 }
131
132 T_x_ref x_ref = x;
133
134 const auto& x_val = to_ref_if<is_autodiff_v<T_beta>>(value_of(x_ref));
135
136 const auto& y_arr = as_array_or_scalar(y_val_vec);
137 const auto& phi_arr = as_array_or_scalar(phi_val_vec);
138
139 Array<T_partials_return, Dynamic, 1> theta(N_instances);
140 if constexpr (T_x_rows == 1) {
141 T_theta_tmp theta_tmp = (x_val * beta_val_vec)(0, 0);
142 theta = theta_tmp + as_array_or_scalar(alpha_val_vec);
143 } else {
144 theta = multiply(x_val, beta_val_vec).array();
145 theta += as_array_or_scalar(alpha_val_vec);
146 }
147 check_finite(function, "Matrix of independent variables", theta);
148 T_precision_val log_phi = log(phi_arr);
149 Array<T_partials_return, Dynamic, 1> logsumexp_theta_logphi
150 = (theta > log_phi)
151 .select(theta + log1p_exp(log_phi - theta),
152 log_phi + log1p_exp(theta - log_phi));
153
154 T_sum_val y_plus_phi = y_arr + phi_arr;
155
156 // Compute the log-density.
157 T_partials_return logp(0);
158 if constexpr (include_summand<propto>::value) {
159 if constexpr (is_vector<T_y>::value) {
160 logp -= sum(lgamma(y_arr + 1.0));
161 } else {
162 logp -= sum(lgamma(y_arr + 1.0)) * N_instances;
163 }
164 }
166 if constexpr (is_vector<T_precision>::value) {
167 scalar_seq_view<decltype(phi_val_vec)> phi_vec(phi_val_vec);
168 for (size_t n = 0; n < N_instances; ++n) {
169 logp += lmultiply(phi_vec[n], phi_vec[n]) - lgamma(phi_vec[n]);
170 }
171 } else {
172 logp += N_instances * (lmultiply(phi_val, phi_val) - lgamma(phi_val));
173 }
174 }
175 logp -= sum(y_plus_phi * logsumexp_theta_logphi);
176
178 logp += sum(y_arr * theta);
179 }
182 logp += sum(lgamma(y_plus_phi));
183 } else {
184 logp += sum(lgamma(y_plus_phi)) * N_instances;
185 }
186 }
187
188 // Compute the necessary derivatives.
189 auto ops_partials
190 = make_partials_propagator(x_ref, alpha_ref, beta_ref, phi_ref);
191 if constexpr (is_any_autodiff_v<T_x, T_beta, T_alpha, T_precision>) {
192 Array<T_partials_return, Dynamic, 1> theta_exp = theta.exp();
193 if constexpr (is_any_autodiff_v<T_x, T_beta, T_alpha>) {
194 Matrix<T_partials_return, Dynamic, 1> theta_derivative
195 = y_arr - theta_exp * y_plus_phi / (theta_exp + phi_arr);
196 if constexpr (is_autodiff_v<T_beta>) {
197 if constexpr (T_x_rows == 1) {
198 edge<2>(ops_partials).partials_ = theta_derivative.sum() * x_val;
199 } else if constexpr (T_x::ColsAtCompileTime == 1) {
200 edge<2>(ops_partials).partials_[0]
201 = dot_product(x_val, theta_derivative);
202 } else {
203 edge<2>(ops_partials).partials_
204 = multiply(x_val.transpose(), theta_derivative);
205 }
206 }
207 if constexpr (is_autodiff_v<T_x>) {
208 if constexpr (T_x_rows == 1) {
209 edge<0>(ops_partials).partials_
210 = beta_val_vec * theta_derivative.sum();
211 } else {
212 edge<0>(ops_partials).partials_
213 = (beta_val_vec * theta_derivative.transpose()).transpose();
214 }
215 }
216 if constexpr (is_autodiff_v<T_alpha>) {
217 if constexpr (is_vector<T_alpha>::value) {
218 partials<1>(ops_partials) = std::move(theta_derivative);
219 } else {
220 partials<1>(ops_partials)[0] = sum(theta_derivative);
221 }
222 }
223 }
224 if constexpr (is_autodiff_v<T_precision>) {
225 if constexpr (is_vector<T_precision>::value) {
226 edge<3>(ops_partials).partials_
227 = 1 - y_plus_phi / (theta_exp + phi_arr) + log_phi
228 - logsumexp_theta_logphi + digamma(y_plus_phi) - digamma(phi_arr);
229 } else {
230 partials<3>(ops_partials)[0]
231 = N_instances
232 + sum(-y_plus_phi / (theta_exp + phi_arr) + log_phi
233 - logsumexp_theta_logphi + digamma(y_plus_phi)
234 - digamma(phi_arr));
235 }
236 }
237 }
238 return ops_partials.build(logp);
239}
240
241template <typename T_y, typename T_x, typename T_alpha, typename T_beta,
242 typename T_precision>
244neg_binomial_2_log_glm_lpmf(const T_y& y, const T_x& x, const T_alpha& alpha,
245 const T_beta& beta, const T_precision& phi) {
246 return neg_binomial_2_log_glm_lpmf<false>(y, x, alpha, beta, phi);
247}
248} // namespace math
249} // namespace stan
250#endif
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
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, T_phi_cl > neg_binomial_2_log_glm_lpmf(const T_y_cl &y, const T_x_cl &x, const T_alpha_cl &alpha, const T_beta_cl &beta, const T_phi_cl &phi)
Returns the log PMF of the Generalized Linear Model (GLM) with Negative-Binomial-2 distribution and l...
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.
void check_nonnegative(const char *function, const char *name, const T_y &y)
Check if y is non-negative.
auto multiply(Mat1 &&m1, Mat2 &&m2)
Return the product of the specified matrices.
Definition multiply.hpp:20
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition size_zero.hpp:19
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(const fvar< T > &x)
Definition log.hpp:18
fvar< T > lmultiply(const fvar< T > &x1, const fvar< T > &x2)
Definition lmultiply.hpp:14
fvar< T > log1p_exp(const fvar< T > &x)
Definition log1p_exp.hpp:14
void check_finite(const char *function, const char *name, const T_y &y)
Return true if all values in y are finite.
fvar< T > lgamma(const fvar< T > &x)
Return the natural logarithm of the gamma function applied to the specified argument.
Definition lgamma.hpp:21
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 dot_product(const T_a &a, const T_b &b)
Returns the dot product of the specified vectors.
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.
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
fvar< T > digamma(const fvar< T > &x)
Return the derivative of the log gamma function at the specified argument.
Definition digamma.hpp:23
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...