Stan Math Library  2.20.0
reverse mode automatic differentiation
poisson_log_glm_lpmf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_PROB_POISSON_LOG_GLM_LPMF_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_POISSON_LOG_GLM_LPMF_HPP
3 
12 #include <cmath>
13 #include <limits>
14 
15 namespace stan {
16 namespace math {
17 
44 template <bool propto, typename T_y, typename T_x, typename T_alpha,
45  typename T_beta>
47  const T_y& y, const T_x& x, const T_alpha& alpha, const T_beta& beta) {
48  static const char* function = "poisson_log_glm_lpmf";
50  T_partials_return;
51  typedef typename std::conditional<
53  Eigen::Array<typename partials_return_type<T_alpha>::type, -1, 1>,
54  typename partials_return_type<T_alpha>::type>::type T_alpha_val;
55 
56  using Eigen::Dynamic;
57  using Eigen::Matrix;
58  using std::exp;
59 
60  const size_t N = x.rows();
61  const size_t M = x.cols();
62 
63  check_nonnegative(function, "Vector of dependent variables", y);
64  check_consistent_size(function, "Vector of dependent variables", y, N);
65  check_consistent_size(function, "Weight vector", beta, M);
66  if (is_vector<T_alpha>::value)
67  check_consistent_sizes(function, "Vector of intercepts", alpha,
68  "Vector of dependent variables", y);
69  if (size_zero(y, x, beta))
70  return 0;
71 
73  return 0;
74 
75  T_partials_return logp(0);
76 
77  const auto& x_val = value_of_rec(x);
78  const auto& y_val = value_of_rec(y);
79  const auto& beta_val = value_of_rec(beta);
80  const auto& alpha_val = value_of_rec(alpha);
81 
82  const auto& y_val_vec = as_column_vector_or_scalar(y_val);
83  const auto& beta_val_vec = as_column_vector_or_scalar(beta_val);
84  const auto& alpha_val_vec = as_column_vector_or_scalar(alpha_val);
85 
86  Matrix<T_partials_return, Dynamic, 1> theta = x_val * beta_val_vec;
87  theta.array() += as_array_or_scalar(alpha_val_vec);
88 
89  Matrix<T_partials_return, Dynamic, 1> theta_derivative
90  = as_array_or_scalar(y_val_vec) - exp(theta.array());
91  double theta_derivative_sum = sum(theta_derivative);
92  if (!std::isfinite(theta_derivative_sum)) {
93  check_finite(function, "Weight vector", beta);
94  check_finite(function, "Intercept", alpha);
95  check_finite(function, "Matrix of independent variables", theta);
96  }
99  logp -= sum(lgamma(as_array_or_scalar(y_val_vec) + 1));
100  } else {
101  logp -= lgamma(as_scalar(y_val) + 1);
102  }
103  }
105  logp += sum(as_array_or_scalar(y_val_vec) * theta.array()
106  - exp(theta.array()));
107  }
108 
109  // Compute the necessary derivatives.
110  operands_and_partials<T_x, T_alpha, T_beta> ops_partials(x, alpha, beta);
112  ops_partials.edge3_.partials_ = x_val.transpose() * theta_derivative;
113  }
115  ops_partials.edge1_.partials_
116  = (beta_val_vec * theta_derivative.transpose()).transpose();
117  }
119  if (is_vector<T_alpha>::value)
120  ops_partials.edge2_.partials_ = theta_derivative;
121  else
122  ops_partials.edge2_.partials_[0] = theta_derivative_sum;
123  }
124  return ops_partials.build(logp);
125 }
126 
127 template <typename T_y, typename T_x, typename T_alpha, typename T_beta>
129  const T_y& y, const T_x& x, const T_alpha& alpha, const T_beta& beta) {
130  return poisson_log_glm_lpmf<false>(y, x, alpha, beta);
131 }
132 } // namespace math
133 } // namespace stan
134 #endif
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition: sum.hpp:20
const Eigen::Matrix< T, Eigen::Dynamic, 1 > & as_column_vector_or_scalar(const Eigen::Matrix< T, Eigen::Dynamic, 1 > &a)
Converts input argument to a column vector or a scalar.
void check_finite(const char *function, const char *name, const T_y &y)
Check if y is finite.
bool isfinite(const stan::math::var &v)
Checks if the given number has finite value.
boost::math::tools::promote_args< double, typename partials_type< typename scalar_type< T >::type >::type, typename partials_return_type< T_pack... >::type >::type type
fvar< T > lgamma(const fvar< T > &x)
Return the natural logarithm of the gamma function applied to the specified argument.
Definition: lgamma.hpp:21
double value_of_rec(const fvar< T > &v)
Return the value of the specified variable.
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...
Definition: conjunction.hpp:14
This template builds partial derivatives with respect to a set of operands.
bool size_zero(T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition: size_zero.hpp:18
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
void check_nonnegative(const char *function, const char *name, const T_y &y)
Check if y is non-negative.
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
void check_consistent_size(const char *function, const char *name, const T &x, size_t expected_size)
Check if the dimension of x is consistent, which is defined to be expected_size if x is a vector or 1...
boost::math::tools::promote_args< double, typename scalar_type< T >::type, typename return_type< Types_pack... >::type >::type type
Definition: return_type.hpp:36
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:11
T_return_type build(double value)
Build the node to be stored on the autodiff graph.
double as_scalar(const std::vector< T > &a)
Converts input to a scalar.
Definition: as_scalar.hpp:20
matrix_cl transpose(const matrix_cl &src)
Takes the transpose of the matrix on the OpenCL device.
Definition: transpose.hpp:20
internal::ops_partials_edge< double, Op2 > edge2_
return_type< T_x, T_alpha, T_beta >::type poisson_log_glm_lpmf(const T_y &y, const T_x &x, const T_alpha &alpha, const T_beta &beta)
Returns the log PMF of the Generalized Linear Model (GLM) with Poisson distribution and log link func...
Eigen::ArrayWrapper< const Eigen::Matrix< T, R, C > > as_array_or_scalar(const Eigen::Matrix< T, R, C > &v)
Converts a matrix type to an array.
void check_consistent_sizes(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)
Check if the dimension of x1 is consistent with x2.
internal::ops_partials_edge< double, Op3 > edge3_
internal::ops_partials_edge< double, Op1 > edge1_

     [ Stan Home Page ] © 2011–2018, Stan Development Team.