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 <vector>
22#include <cmath>
23
24namespace stan {
25namespace math {
26
64template <bool propto, typename T_y, typename T_x, typename T_alpha,
65 typename T_beta, typename T_precision,
66 require_matrix_t<T_x>* = nullptr>
68 const T_y& y, const T_x& x, const T_alpha& alpha, const T_beta& beta,
69 const T_precision& phi) {
70 using Eigen::Array;
71 using Eigen::Dynamic;
72 using Eigen::exp;
73 using Eigen::log1p;
74 using Eigen::Matrix;
75 constexpr int T_x_rows = T_x::RowsAtCompileTime;
76 using T_xbeta_partials = partials_return_t<T_x, T_beta>;
77 using T_partials_return
79 using T_precision_val = typename std::conditional_t<
81 Eigen::Array<partials_return_t<T_precision>, -1, 1>,
83 using T_sum_val = typename std::conditional_t<
85 Eigen::Array<partials_return_t<T_y, T_precision>, -1, 1>,
87 using T_theta_tmp =
88 typename std::conditional_t<T_x_rows == 1, T_partials_return,
89 Array<T_partials_return, Dynamic, 1>>;
90 using T_xbeta_tmp =
91 typename std::conditional_t<T_x_rows == 1, T_xbeta_partials,
92 Array<T_xbeta_partials, Dynamic, 1>>;
93 using T_x_ref = ref_type_if_not_constant_t<T_x>;
94 using T_alpha_ref = ref_type_if_not_constant_t<T_alpha>;
95 using T_beta_ref = ref_type_if_not_constant_t<T_beta>;
97
98 const size_t N_instances = T_x_rows == 1 ? stan::math::size(y) : x.rows();
99 const size_t N_attributes = x.cols();
100
101 static constexpr const char* function = "neg_binomial_2_log_glm_lpmf";
102 check_consistent_size(function, "Vector of dependent variables", y,
103 N_instances);
104 check_consistent_size(function, "Weight vector", beta, N_attributes);
105 check_consistent_size(function, "Vector of precision parameters", phi,
106 N_instances);
107 check_consistent_size(function, "Vector of intercepts", alpha, N_instances);
108 T_alpha_ref alpha_ref = alpha;
109 T_beta_ref beta_ref = beta;
110 const auto& beta_val = value_of(beta_ref);
111 const auto& alpha_val = value_of(alpha_ref);
112 const auto& beta_val_vec = to_ref(as_column_vector_or_scalar(beta_val));
113 const auto& alpha_val_vec = to_ref(as_column_vector_or_scalar(alpha_val));
114 check_finite(function, "Weight vector", beta_val_vec);
115 check_finite(function, "Intercept", alpha_val_vec);
116
117 if (size_zero(y, phi)) {
118 return 0;
119 }
120
121 const auto& y_ref = to_ref(y);
122 T_phi_ref phi_ref = phi;
123
124 const auto& y_val = value_of(y_ref);
125 const auto& phi_val = value_of(phi_ref);
126
127 const auto& y_val_vec = to_ref(as_column_vector_or_scalar(y_val));
128 const auto& phi_val_vec = to_ref(as_column_vector_or_scalar(phi_val));
129 check_nonnegative(function, "Failures variables", y_val_vec);
130 check_positive_finite(function, "Precision parameter", phi_val_vec);
131
133 return 0;
134 }
135
136 T_x_ref x_ref = x;
137
138 const auto& x_val = to_ref_if<!is_constant<T_beta>::value>(value_of(x_ref));
139
140 const auto& y_arr = as_array_or_scalar(y_val_vec);
141 const auto& phi_arr = as_array_or_scalar(phi_val_vec);
142
143 Array<T_partials_return, Dynamic, 1> theta(N_instances);
144 if (T_x_rows == 1) {
145 T_theta_tmp theta_tmp
146 = forward_as<T_xbeta_tmp>((x_val * beta_val_vec)(0, 0));
147 theta = theta_tmp + as_array_or_scalar(alpha_val_vec);
148 } else {
149 theta = (x_val * beta_val_vec).array();
150 theta += as_array_or_scalar(alpha_val_vec);
151 }
152 check_finite(function, "Matrix of independent variables", theta);
153 T_precision_val log_phi = log(phi_arr);
154 Array<T_partials_return, Dynamic, 1> logsumexp_theta_logphi
155 = (theta > log_phi)
156 .select(theta + log1p_exp(log_phi - theta),
157 log_phi + log1p_exp(theta - log_phi));
158
159 T_sum_val y_plus_phi = y_arr + phi_arr;
160
161 // Compute the log-density.
162 T_partials_return logp(0);
165 logp -= sum(lgamma(y_arr + 1.0));
166 } else {
167 logp -= sum(lgamma(y_arr + 1.0)) * N_instances;
168 }
169 }
172 scalar_seq_view<decltype(phi_val_vec)> phi_vec(phi_val_vec);
173 for (size_t n = 0; n < N_instances; ++n) {
174 logp += multiply_log(phi_vec[n], phi_vec[n]) - lgamma(phi_vec[n]);
175 }
176 } else {
177 using T_phi_scalar = scalar_type_t<decltype(phi_val_vec)>;
178 logp += N_instances
179 * (multiply_log(forward_as<T_phi_scalar>(phi_val),
180 forward_as<T_phi_scalar>(phi_val))
181 - lgamma(forward_as<T_phi_scalar>(phi_val)));
182 }
183 }
184 logp -= sum(y_plus_phi * logsumexp_theta_logphi);
185
187 logp += sum(y_arr * theta);
188 }
191 logp += sum(lgamma(y_plus_phi));
192 } else {
193 logp += sum(lgamma(y_plus_phi)) * N_instances;
194 }
195 }
196
197 // Compute the necessary derivatives.
198 auto ops_partials
199 = make_partials_propagator(x_ref, alpha_ref, beta_ref, phi_ref);
201 Array<T_partials_return, Dynamic, 1> theta_exp = theta.exp();
203 Matrix<T_partials_return, Dynamic, 1> theta_derivative
204 = y_arr - theta_exp * y_plus_phi / (theta_exp + phi_arr);
206 if (T_x_rows == 1) {
207 edge<2>(ops_partials).partials_
208 = forward_as<Matrix<T_partials_return, 1, Dynamic>>(
209 theta_derivative.sum() * x_val);
210 } else {
211 edge<2>(ops_partials).partials_
212 = x_val.transpose() * theta_derivative;
213 }
214 }
216 if (T_x_rows == 1) {
217 edge<0>(ops_partials).partials_
218 = forward_as<Array<T_partials_return, Dynamic, T_x_rows>>(
219 beta_val_vec * theta_derivative.sum());
220 } else {
221 edge<0>(ops_partials).partials_
222 = (beta_val_vec * theta_derivative.transpose()).transpose();
223 }
224 }
227 partials<1>(ops_partials) = std::move(theta_derivative);
228 } else {
229 partials<1>(ops_partials)[0] = sum(theta_derivative);
230 }
231 }
232 }
235 edge<3>(ops_partials).partials_
236 = 1 - y_plus_phi / (theta_exp + phi_arr) + log_phi
237 - logsumexp_theta_logphi + digamma(y_plus_phi) - digamma(phi_arr);
238 } else {
239 partials<3>(ops_partials)[0]
240 = N_instances
241 + sum(-y_plus_phi / (theta_exp + phi_arr) + log_phi
242 - logsumexp_theta_logphi + digamma(y_plus_phi)
243 - digamma(phi_arr));
244 }
245 }
246 }
247 return ops_partials.build(logp);
248}
249
250template <typename T_y, typename T_x, typename T_alpha, typename T_beta,
251 typename T_precision>
253neg_binomial_2_log_glm_lpmf(const T_y& y, const T_x& x, const T_alpha& alpha,
254 const T_beta& beta, const T_precision& phi) {
255 return neg_binomial_2_log_glm_lpmf<false>(y, x, alpha, beta, phi);
256}
257} // namespace math
258} // namespace stan
259#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...
size_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:18
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
fvar< T > multiply_log(const fvar< T > &x1, const fvar< T > &x2)
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.
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:15
fvar< T > log1p_exp(const fvar< T > &x)
Definition log1p_exp.hpp:13
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:22
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.
fvar< T > lgamma(const fvar< T > &x)
Return the natural logarithm of the gamma function applied to the specified argument.
Definition lgamma.hpp:21
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_constant< T >::value, T >::type ref_type_if_not_constant_t
Definition ref_type.hpp:62
typename scalar_type< T >::type scalar_type_t
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 ...
Definition fvar.hpp:9
If the input type T is either an eigen matrix with 1 column or 1 row at compile time or a standard ve...
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...