Loading [MathJax]/extensions/TeX/AMSsymbols.js
Automatic Differentiation
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
22
23namespace stan {
24namespace math {
25
27 template <typename ThetaVec, typename Eta, typename Mean,
29 inline auto operator()(const ThetaVec& theta, const Eta& eta,
30 const std::vector<int>& y,
31 const std::vector<int>& y_index, Mean&& mean,
32 std::ostream* pstream) const {
33 Eigen::VectorXi n_per_group = Eigen::VectorXi::Zero(theta.size());
34 Eigen::VectorXi counts_per_group = Eigen::VectorXi::Zero(theta.size());
35
36 for (int i = 0; i < y.size(); i++) {
37 n_per_group[y_index[i] - 1]++;
38 counts_per_group[y_index[i] - 1] += y[i];
39 }
40 Eigen::Map<const Eigen::VectorXi> y_map(y.data(), y.size());
41
42 auto theta_offset = to_ref(add(theta, mean));
43 auto log_eta_plus_exp_theta = eval(log(add(eta, exp(theta_offset))));
44 return sum(binomial_coefficient_log(subtract(add(y_map, eta), 1), y_map))
45 + sum(
46 add(elt_multiply(counts_per_group,
47 subtract(theta_offset, log_eta_plus_exp_theta)),
48 elt_multiply(multiply(n_per_group, eta),
49 subtract(log(eta), log_eta_plus_exp_theta))));
50 }
51};
52
74template <bool propto = false, typename Eta, typename ThetaVec, typename Mean,
75 typename CovarFun, typename CovarArgs,
78 const std::vector<int>& y, const std::vector<int>& y_index, const Eta& eta,
79 Mean&& mean, CovarFun&& covariance_function, CovarArgs&& covar_args,
80 const ThetaVec& theta_0, double tolerance, int max_num_steps,
81 const int hessian_block_size, const int solver,
82 const int max_steps_line_search, std::ostream* msgs) {
83 laplace_options_user_supplied ops{hessian_block_size, solver,
84 max_steps_line_search, tolerance,
85 max_num_steps, value_of(theta_0)};
88 std::forward_as_tuple(eta, y, y_index, std::forward<Mean>(mean)),
89 std::forward<CovarFun>(covariance_function),
90 std::forward<CovarArgs>(covar_args), ops, msgs);
91}
92
111template <bool propto = false, typename Eta, typename Mean, typename CovarFun,
112 typename CovarArgs>
114 const std::vector<int>& y, const std::vector<int>& y_index, const Eta& eta,
115 Mean&& mean, CovarFun&& covariance_function, CovarArgs&& covar_args,
116 std::ostream* msgs) {
119 std::forward_as_tuple(eta, y, y_index, std::forward<Mean>(mean)),
120 std::forward<CovarFun>(covariance_function),
121 std::forward<CovarArgs>(covar_args), laplace_options_default{}, msgs);
122}
123
125 template <typename ThetaVec, typename Eta, typename Mean,
127 inline auto operator()(const ThetaVec& theta, const Eta& eta,
128 const std::vector<int>& y,
129 const std::vector<int>& n_per_group,
130 const std::vector<int>& counts_per_group, Mean&& mean,
131 std::ostream* pstream) const {
132 Eigen::Map<const Eigen::VectorXi> y_map(y.data(), y.size());
133 Eigen::Map<const Eigen::VectorXi> n_per_group_map(n_per_group.data(),
134 n_per_group.size());
135 Eigen::Map<const Eigen::VectorXi> counts_per_group_map(
136 counts_per_group.data(), counts_per_group.size());
137
138 auto theta_offset = to_ref(add(theta, mean));
139 auto log_eta_plus_exp_theta = eval(log(add(eta, exp(theta_offset))));
140
141 return sum(binomial_coefficient_log(subtract(add(y_map, eta), 1.0), y_map))
142 + sum(
143 add(elt_multiply(counts_per_group_map,
144 subtract(theta_offset, log_eta_plus_exp_theta)),
145 elt_multiply(multiply(n_per_group_map, eta),
146 subtract(log(eta), log_eta_plus_exp_theta))));
147 }
148};
149
171template <bool propto = false, typename Eta, typename ThetaVec, typename Mean,
172 typename CovarFun, typename CovarArgs,
175 const std::vector<int>& y, const std::vector<int>& n_per_group,
176 const std::vector<int>& counts_per_group, const Eta& eta, Mean&& mean,
177 CovarFun&& covariance_function, CovarArgs&& covar_args,
178 const ThetaVec& theta_0, double tolerance, int max_num_steps,
179 const int hessian_block_size, const int solver,
180 const int max_steps_line_search, std::ostream* msgs) {
181 laplace_options_user_supplied ops{hessian_block_size, solver,
182 max_steps_line_search, tolerance,
183 max_num_steps, value_of(theta_0)};
186 std::forward_as_tuple(eta, y, n_per_group, counts_per_group,
187 std::forward<Mean>(mean)),
188 std::forward<CovarFun>(covariance_function),
189 std::forward<CovarArgs>(covar_args), ops, msgs);
190}
191
210template <bool propto = false, typename Eta, typename Mean, typename CovarFun,
211 typename CovarArgs>
213 const std::vector<int>& y, const std::vector<int>& n_per_group,
214 const std::vector<int>& counts_per_group, const Eta& eta, Mean&& mean,
215 CovarFun&& covariance_function, CovarArgs&& covar_args,
216 std::ostream* msgs) {
219 std::forward_as_tuple(eta, y, n_per_group, counts_per_group,
220 std::forward<Mean>(mean)),
221 std::forward<CovarFun>(covariance_function),
222 std::forward<CovarArgs>(covar_args), laplace_options_default{}, msgs);
223}
224
225} // namespace math
226} // namespace stan
227
228#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),...
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_tol_neg_binomial_2_log_lpmf(const std::vector< int > &y, const std::vector< int > &y_index, const Eta &eta, Mean &&mean, CovarFun &&covariance_function, CovarArgs &&covar_args, const ThetaVec &theta_0, double tolerance, int max_num_steps, const int hessian_block_size, const int solver, const int max_steps_line_search, 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, CovarFun &&covariance_function, CovarArgs &&covar_args, std::ostream *msgs)
Wrapper function around the laplace_marginal function for a negative binomial likelihood.
auto laplace_marginal_neg_binomial_2_log_lpmf(const std::vector< int > &y, const std::vector< int > &y_index, const Eta &eta, Mean &&mean, CovarFun &&covariance_function, CovarArgs &&covar_args, std::ostream *msgs)
Wrapper function around the laplace_marginal function for a negative binomial likelihood.
T eval(T &&arg)
Inputs which have a plain_type equal to the own time are forwarded unmodified (for Eigen expressions ...
Definition eval.hpp:20
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:18
auto multiply(const Mat1 &m1, const Mat2 &m2)
Return the product of the specified matrices.
Definition multiply.hpp:19
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:23
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, CovarFun &&covariance_function, CovarArgs &&covar_args, const ThetaVec &theta_0, double tolerance, int max_num_steps, const int hessian_block_size, const int solver, const int max_steps_line_search, std::ostream *msgs)
Wrapper function around the laplace_marginal function for a negative binomial likelihood.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:18
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:15
double 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,...
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