Automatic Differentiation
 
Loading...
Searching...
No Matches
poisson_log_glm_lpmf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_POISSON_LOG_GLM_LPMF_HPP
2#define STAN_MATH_PRIM_PROB_POISSON_LOG_GLM_LPMF_HPP
3
19#include <cmath>
20
21namespace stan {
22namespace math {
23
53template <bool propto, typename T_y, typename T_x, typename T_alpha,
54 typename T_beta, require_matrix_t<T_x>* = nullptr>
56 const T_y& y, const T_x& x, const T_alpha& alpha, const T_beta& beta) {
57 using Eigen::Array;
58 using Eigen::Dynamic;
59 using Eigen::Matrix;
60 using std::exp;
61 using std::isfinite;
62 constexpr int T_x_rows = T_x::RowsAtCompileTime;
63 using T_partials_return = partials_return_t<T_y, T_x, T_alpha, T_beta>;
64 using T_theta_tmp =
65 typename std::conditional_t<T_x_rows == 1, T_partials_return,
66 Array<T_partials_return, Dynamic, 1>>;
67 using T_x_ref = ref_type_if_not_constant_t<T_x>;
68 using T_alpha_ref = ref_type_if_not_constant_t<T_alpha>;
69 using T_beta_ref = ref_type_if_not_constant_t<T_beta>;
70
71 const size_t N_instances = T_x_rows == 1 ? stan::math::size(y) : x.rows();
72 const size_t N_attributes = x.cols();
73
74 static constexpr const char* function = "poisson_log_glm_lpmf";
75 check_consistent_size(function, "Vector of dependent variables", y,
76 N_instances);
77 check_consistent_size(function, "Weight vector", beta, N_attributes);
78 check_consistent_size(function, "Vector of intercepts", alpha, N_instances);
79 const auto& y_ref = to_ref(y);
80 check_nonnegative(function, "Vector of dependent variables", y_ref);
81
82 if (size_zero(y)) {
83 return 0;
84 }
86 return 0;
87 }
88
89 T_x_ref x_ref = x;
90 T_alpha_ref alpha_ref = alpha;
91 T_beta_ref beta_ref = beta;
92
93 const auto& y_val = value_of(y_ref);
94 const auto& x_val = to_ref_if<is_autodiff_v<T_beta>>(value_of(x_ref));
95 const auto& alpha_val = value_of(alpha_ref);
96 const auto& beta_val = value_of(beta_ref);
97
98 const auto& y_val_vec = to_ref(as_column_vector_or_scalar(y_val));
99 const auto& alpha_val_vec = as_column_vector_or_scalar(alpha_val);
100 const auto& beta_val_vec
101 = to_ref_if<is_autodiff_v<T_x>>(as_column_vector_or_scalar(beta_val));
102
103 Array<T_partials_return, Dynamic, 1> theta(N_instances);
104 if constexpr (T_x_rows == 1) {
105 T_theta_tmp theta_tmp = (x_val * beta_val_vec).coeff(0, 0);
106 theta = theta_tmp + as_array_or_scalar(alpha_val_vec);
107 } else {
108 theta = multiply(x_val, beta_val_vec);
109 theta += as_array_or_scalar(alpha_val_vec);
110 }
111
112 Matrix<T_partials_return, Dynamic, 1> theta_derivative
113 = as_array_or_scalar(y_val_vec) - exp(theta.array());
114 T_partials_return theta_derivative_sum = sum(theta_derivative);
115 if (!isfinite(theta_derivative_sum)) {
116 check_finite(function, "Weight vector", beta);
117 check_finite(function, "Intercept", alpha);
118 check_finite(function, "Matrix of independent variables", theta);
119 }
120
121 T_partials_return logp(0);
122 if constexpr (include_summand<propto>::value) {
123 logp -= sum(lgamma(as_array_or_scalar(y_val_vec) + 1));
124 }
125
126 logp += sum(as_array_or_scalar(y_val_vec) * theta.array()
127 - exp(theta.array()));
128
129 auto ops_partials = make_partials_propagator(x_ref, alpha_ref, beta_ref);
130 // Compute the necessary derivatives.
131 if constexpr (is_autodiff_v<T_beta>) {
132 if constexpr (T_x_rows == 1) {
133 edge<2>(ops_partials).partials_ = theta_derivative.sum() * x_val;
134 } else if constexpr (T_x::ColsAtCompileTime == 1) {
135 partials<2>(ops_partials)[0] = dot_product(x_val, theta_derivative);
136 } else {
137 partials<2>(ops_partials) = multiply(x_val.transpose(), theta_derivative);
138 }
139 }
140 if constexpr (is_autodiff_v<T_x>) {
141 if constexpr (T_x_rows == 1) {
142 edge<0>(ops_partials).partials_ = beta_val_vec * theta_derivative.sum();
143 } else {
144 edge<0>(ops_partials).partials_
145 = (beta_val_vec * theta_derivative.transpose()).transpose();
146 }
147 }
148 if constexpr (is_autodiff_v<T_alpha>) {
149 if constexpr (is_vector<T_alpha>::value) {
150 partials<1>(ops_partials) = theta_derivative;
151 } else {
152 partials<1>(ops_partials)[0] = theta_derivative_sum;
153 }
154 }
155 return ops_partials.build(logp);
156}
157
158template <typename T_y, typename T_x, typename T_alpha, typename T_beta>
160 const T_y& y, const T_x& x, const T_alpha& alpha, const T_beta& beta) {
161 return poisson_log_glm_lpmf<false>(y, x, alpha, beta);
162}
163
164} // namespace math
165} // namespace stan
166#endif
isfinite_< as_operation_cl_t< T > > isfinite(T &&a)
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 > poisson_log_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 Poisson distribution and log link func...
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
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.
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:15
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...