Automatic Differentiation
 
Loading...
Searching...
No Matches
laplace_marginal_neg_binomial_2_log_lpmf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_MIX_PROB_LAPLACE_MARGINAL_NEG_BINOMIAL_2_LOG_LPMF_HPP
2#define STAN_MATH_MIX_PROB_LAPLACE_MARGINAL_NEG_BINOMIAL_2_LOG_LPMF_HPP
3
6
24
25namespace stan {
26namespace math {
27
29 template <typename ThetaVec, typename Eta, typename Mean,
31 inline auto operator()(const ThetaVec& theta, const Eta& eta,
32 const std::vector<int>& y,
33 const std::vector<int>& y_index, Mean&& mean,
34 std::ostream* pstream) const {
35 Eigen::VectorXi n_per_group = Eigen::VectorXi::Zero(theta.size());
36 Eigen::VectorXi counts_per_group = Eigen::VectorXi::Zero(theta.size());
37
38 for (int i = 0; i < y.size(); i++) {
39 n_per_group[y_index[i] - 1]++;
40 counts_per_group[y_index[i] - 1] += y[i];
41 }
42 Eigen::Map<const Eigen::VectorXi> y_map(y.data(), y.size());
43
44 auto theta_offset = add(theta, mean);
45 auto log_eta = log(eta);
46 auto lse = to_ref(log_sum_exp(theta_offset, log_eta));
47
48 return sum(binomial_coefficient_log(subtract(add(y_map, eta), 1.0), y_map))
49 + sum(add(
50 // counts_per_group * (theta - log(eta + exp(theta)))
51 elt_multiply(counts_per_group, subtract(theta_offset, lse)),
52 // n_per_group * eta * (log(eta) - log(eta + exp(theta)))
53 elt_multiply(multiply(n_per_group, eta),
54 subtract(log_eta, lse))));
55 }
56};
57
81template <bool propto = false, typename Eta, typename Mean, typename CovarFun,
82 typename CovarArgs, typename OpsTuple>
84 const std::vector<int>& y, const std::vector<int>& y_index, const Eta& eta,
85 Mean&& mean, int hessian_block_size, CovarFun&& covariance_function,
86 CovarArgs&& covar_args, OpsTuple&& ops, std::ostream* msgs) {
87 auto options
88 = internal::tuple_to_laplace_options(std::forward<OpsTuple>(ops));
89 options.hessian_block_size = hessian_block_size;
92 std::forward_as_tuple(eta, y, y_index, std::forward<Mean>(mean)),
93 std::forward<CovarFun>(covariance_function),
94 std::forward<CovarArgs>(covar_args), std::move(options), msgs);
95}
96
117template <bool propto = false, typename Eta, typename Mean, typename CovarFun,
118 typename CovarArgs>
120 const std::vector<int>& y, const std::vector<int>& y_index, const Eta& eta,
121 Mean&& mean, int hessian_block_size, CovarFun&& covariance_function,
122 CovarArgs&& covar_args, std::ostream* msgs) {
123 auto options = laplace_options_default{hessian_block_size};
126 std::forward_as_tuple(eta, y, y_index, std::forward<Mean>(mean)),
127 std::forward<CovarFun>(covariance_function),
128 std::forward<CovarArgs>(covar_args), options, msgs);
129}
130
132 template <typename ThetaVec, typename Eta, typename Mean,
134 inline auto operator()(const ThetaVec& theta, const Eta& eta,
135 const std::vector<int>& y,
136 const std::vector<int>& n_per_group,
137 const std::vector<int>& counts_per_group, Mean&& mean,
138 std::ostream* pstream) const {
139 Eigen::Map<const Eigen::VectorXi> y_map(y.data(), y.size());
140 Eigen::Map<const Eigen::VectorXi> n_per_group_map(n_per_group.data(),
141 n_per_group.size());
142 Eigen::Map<const Eigen::VectorXi> counts_per_group_map(
143 counts_per_group.data(), counts_per_group.size());
144
145 auto theta_offset = add(theta, mean);
146 auto log_eta = log(eta);
147 auto lse = to_ref(log_sum_exp(theta_offset, log_eta));
148
149 return sum(binomial_coefficient_log(subtract(add(y_map, eta), 1.0), y_map))
150 + sum(add(
151 // counts_per_group * (theta - log(eta + exp(theta)))
152 elt_multiply(counts_per_group_map, subtract(theta_offset, lse)),
153 // n_per_group * eta * (log(eta) - log(eta + exp(theta)))
154 elt_multiply(multiply(n_per_group_map, eta),
155 subtract(log_eta, lse))));
156 }
157};
158
182template <bool propto = false, typename Eta, typename Mean, typename CovarFun,
183 typename CovarArgs, typename OpsTuple>
185 const std::vector<int>& y, const std::vector<int>& n_per_group,
186 const std::vector<int>& counts_per_group, const Eta& eta, Mean&& mean,
187 int hessian_block_size, CovarFun&& covariance_function,
188 CovarArgs&& covar_args, OpsTuple&& ops, std::ostream* msgs) {
189 auto options
190 = internal::tuple_to_laplace_options(std::forward<OpsTuple>(ops));
191 options.hessian_block_size = hessian_block_size;
194 std::forward_as_tuple(eta, y, n_per_group, counts_per_group,
195 std::forward<Mean>(mean)),
196 std::forward<CovarFun>(covariance_function),
197 std::forward<CovarArgs>(covar_args), std::move(options), msgs);
198}
199
220template <bool propto = false, typename Eta, typename Mean, typename CovarFun,
221 typename CovarArgs>
223 const std::vector<int>& y, const std::vector<int>& n_per_group,
224 const std::vector<int>& counts_per_group, const Eta& eta, Mean&& mean,
225 int hessian_block_size, CovarFun&& covariance_function,
226 CovarArgs&& covar_args, std::ostream* msgs) {
227 auto options = laplace_options_default{hessian_block_size};
230 std::forward_as_tuple(eta, y, n_per_group, counts_per_group,
231 std::forward<Mean>(mean)),
232 std::forward<CovarFun>(covariance_function),
233 std::forward<CovarArgs>(covar_args), options, msgs);
234}
235
236} // namespace math
237} // namespace stan
238
239#endif
require_t< is_eigen_vector< std::decay_t< T > > > require_eigen_vector_t
Require type satisfies is_eigen_vector.
require_all_t< is_eigen_vector< std::decay_t< Types > >... > require_all_eigen_vector_t
Require all of the types satisfy is_eigen_vector.
elt_multiply_< as_operation_cl_t< T_a >, as_operation_cl_t< T_b > > elt_multiply(T_a &&a, T_b &&b)
subtraction_< as_operation_cl_t< T_a >, as_operation_cl_t< T_b > > subtract(T_a &&a, T_b &&b)
binomial_coefficient_log_< as_operation_cl_t< T1 >, as_operation_cl_t< T2 > > binomial_coefficient_log(T1 &&a, T2 &&b)
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
fvar< T > log(const fvar< T > &x)
Definition log.hpp:18
auto multiply(const Mat1 &m1, const Mat2 &m2)
Return the product of the specified matrices.
Definition multiply.hpp:19
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_neg_binomial_2_log_summary_lpmf(const std::vector< int > &y, const std::vector< int > &n_per_group, const std::vector< int > &counts_per_group, const Eta &eta, 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 negative binomial likelihood.
auto laplace_marginal_neg_binomial_2_log_summary_lpmf(const std::vector< int > &y, const std::vector< int > &n_per_group, const std::vector< int > &counts_per_group, const Eta &eta, Mean &&mean, int hessian_block_size, CovarFun &&covariance_function, CovarArgs &&covar_args, std::ostream *msgs)
Wrapper function around the laplace_marginal function for a negative binomial likelihood.
auto laplace_marginal_tol_neg_binomial_2_log_lpmf(const std::vector< int > &y, const std::vector< int > &y_index, const Eta &eta, 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 negative binomial likelihood.
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 laplace_marginal_neg_binomial_2_log_lpmf(const std::vector< int > &y, const std::vector< int > &y_index, const Eta &eta, Mean &&mean, int hessian_block_size, CovarFun &&covariance_function, CovarArgs &&covar_args, std::ostream *msgs)
Wrapper function around the laplace_marginal function for a negative binomial likelihood.
fvar< T > log_sum_exp(const fvar< T > &x1, const fvar< T > &x2)
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
auto operator()(const ThetaVec &theta, const Eta &eta, const std::vector< int > &y, const std::vector< int > &n_per_group, const std::vector< int > &counts_per_group, Mean &&mean, std::ostream *pstream) const
auto operator()(const ThetaVec &theta, const Eta &eta, const std::vector< int > &y, const std::vector< int > &y_index, Mean &&mean, std::ostream *pstream) const