Automatic Differentiation
 
Loading...
Searching...
No Matches
laplace_marginal_poisson_log_lpmf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_MIX_PROB_LAPLACE_MARGINAL_POISSON_LOG_LPMF_HPP
2#define STAN_MATH_MIX_PROB_LAPLACE_MARGINAL_POISSON_LOG_LPMF_HPP
3
15
16namespace stan {
17namespace math {
18
34 template <typename Theta, typename YVec, typename Mean,
36 inline auto operator()(const Theta& theta, const YVec& y,
37 const std::vector<int>& y_index, Mean&& mean,
38 std::ostream* /*pstream*/) const {
39 Eigen::VectorXd counts_per_group = Eigen::VectorXd::Zero(theta.size());
40 Eigen::VectorXd n_per_group = Eigen::VectorXd::Zero(theta.size());
41
42 double norm_constant = 0;
43 for (size_t i = 0; i < y_index.size(); i++) {
44 counts_per_group(y_index[i] - 1) += y[i];
45 n_per_group(y_index[i] - 1) += 1;
46 norm_constant -= lgamma(y[i] + 1.0);
47 }
48
49 auto theta_offset = to_ref(add(theta, mean));
50
51 return norm_constant + dot_product(theta_offset, counts_per_group)
52 - dot_product(n_per_group, exp(theta_offset));
53 }
54};
55
77template <bool propto = false, typename Mean, typename CovarFun,
78 typename CovarArgs, typename OpsTuple>
80 const std::vector<int>& y, const std::vector<int>& y_index, Mean&& mean,
81 int hessian_block_size, CovarFun&& covariance_function,
82 CovarArgs&& covar_args, OpsTuple&& ops, std::ostream* msgs) {
83 auto options
84 = internal::tuple_to_laplace_options(std::forward<OpsTuple>(ops));
85 options.hessian_block_size = hessian_block_size;
88 std::forward_as_tuple(y, y_index, std::forward<Mean>(mean)),
89 covariance_function, std::forward<CovarArgs>(covar_args),
90 std::move(options), msgs);
91}
92
111template <bool propto = false, typename CovarFun, typename CovarArgs,
112 typename Mean>
114 const std::vector<int>& y, const std::vector<int>& y_index, Mean&& mean,
115 int hessian_block_size, CovarFun&& covariance_function,
116 CovarArgs&& covar_args, std::ostream* msgs) {
117 auto options = laplace_options_default{hessian_block_size};
120 std::forward_as_tuple(y, y_index, std::forward<Mean>(mean)),
121 covariance_function, std::forward<CovarArgs>(covar_args), options, msgs);
122}
123
124} // namespace math
125} // namespace stan
126
127#endif
require_t< is_eigen_vector< std::decay_t< T > > > require_eigen_vector_t
Require type satisfies is_eigen_vector.
addition_< as_operation_cl_t< T_a >, as_operation_cl_t< T_b > > add(T_a &&a, T_b &&b)
Reference for calculations of marginal and its gradients: Margossian et al (2020),...
constexpr auto tuple_to_laplace_options(Options &&ops)
scalar_type_t< T > mean(const T &m)
Returns the sample mean (i.e., average) of the coefficients in the specified std vector,...
Definition mean.hpp:20
auto laplace_marginal_density(LLFun &&ll_fun, LLTupleArgs &&ll_args, CovarFun &&covariance_function, CovarArgs &&covar_args, const laplace_options< InitTheta > &options, std::ostream *msgs)
For a latent Gaussian model with global parameters phi, latent variables theta, and observations y,...
auto laplace_marginal_tol_poisson_log_lpmf(const std::vector< int > &y, const std::vector< int > &y_index, Mean &&mean, int hessian_block_size, CovarFun &&covariance_function, CovarArgs &&covar_args, OpsTuple &&ops, std::ostream *msgs)
Wrapper function around the laplace_marginal function for a log poisson likelihood.
fvar< T > lgamma(const fvar< T > &x)
Return the natural logarithm of the gamma function applied to the specified argument.
Definition lgamma.hpp:21
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:18
auto laplace_marginal_poisson_log_lpmf(const std::vector< int > &y, const std::vector< int > &y_index, Mean &&mean, int hessian_block_size, CovarFun &&covariance_function, CovarArgs &&covar_args, std::ostream *msgs)
Wrapper function around the laplace_marginal function for a log poisson likelihood.
auto dot_product(const T_a &a, const T_b &b)
Returns the dot product of the specified vectors.
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:15
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
auto operator()(const Theta &theta, const YVec &y, const std::vector< int > &y_index, Mean &&mean, std::ostream *) const
Returns the lpmf for a Poisson with a log link across multiple groups.