@@ -25,13 +25,36 @@ namespace stan {
2525namespace math {
2626
2727struct bernoulli_logit_likelihood {
28+ /* *
29+ * Returns the lpmf for a Bernoulli with a logit link across
30+ * multiple groups. No need to compute the log normalizing constant.
31+ * @tparam Theta A type inheriting from `Eigen::EigenBase` with dynamic
32+ * sized rows and 1 column.
33+ * @tparam YVec A vector type containing integers.
34+ * @tparam Mean type of the mean of the latent normal distribution
35+ * @param[in] theta log Poisson rate for each group.
36+ * @param[in] y binary observations
37+ * @param[in] y_index group to which each observation belongs
38+ * return lpmf for a Poisson with a log link.
39+ * @param[in] mean the mean of the latent normal variable
40+ * \msg_arg
41+ */
2842 template <typename ThetaVec, typename YVec, typename Mean>
2943 inline auto operator ()(const ThetaVec& theta, const YVec& y,
30- const std::vector<int >& delta_int, Mean&& mean,
31- std::ostream* pstream) const {
44+ const std::vector<int >& y_index, Mean&& mean,
45+ std::ostream* msgs) const {
46+ Eigen::VectorXd counts_per_group = Eigen::VectorXd::Zero (theta.size ());
47+ Eigen::VectorXd n_per_group = Eigen::VectorXd::Zero (theta.size ());
48+
49+ for (int i = 0 ; i < theta.size (); i++) {
50+ counts_per_group (y_index[i] - 1 ) += y[i];
51+ n_per_group (y_index[i] - 1 ) += 1 ;
52+ }
53+
3254 auto theta_offset = to_ref (add (theta, mean));
33- return sum (elt_multiply (theta_offset, y)
34- - elt_multiply (to_vector (delta_int), log1p_exp (theta_offset)));
55+
56+ return sum (elt_multiply (theta_offset, counts_per_group)
57+ - elt_multiply (to_vector (n_per_group), log1p_exp (theta_offset)));
3558 }
3659};
3760
@@ -47,9 +70,8 @@ struct bernoulli_logit_likelihood {
4770 * with dynamic sized rows and 1 column.
4871 * @tparam Mean type of the mean of the latent normal distribution
4972 * \laplace_common_template_args
50- * @param[in] y total counts per group. Second sufficient statistics.
51- * @param[in] n_samples number of samples per group. First sufficient
52- * statistics.
73+ * @param[in] y binary observations
74+ * @param[in] y_index group to which each observation belongs
5375 * @param[in] mean the mean of the latent normal variable.
5476 * \laplace_common_args
5577 * @param[in] hessian_block_size Block size for the Hessian approximation with
@@ -60,15 +82,15 @@ struct bernoulli_logit_likelihood {
6082template <bool propto = false , typename Mean, typename CovarFun,
6183 typename CovarArgs, typename OpsTuple>
6284inline auto laplace_marginal_tol_bernoulli_logit_lpmf (
63- const std::vector<int >& y, const std::vector<int >& n_samples , Mean&& mean,
85+ const std::vector<int >& y, const std::vector<int >& y_index , Mean&& mean,
6486 int hessian_block_size, CovarFun&& covariance_function,
6587 CovarArgs&& covar_args, OpsTuple&& ops, std::ostream* msgs) {
6688 auto options
6789 = internal::tuple_to_laplace_options (std::forward<OpsTuple>(ops));
6890 options.hessian_block_size = hessian_block_size;
6991 return laplace_marginal_density (
7092 bernoulli_logit_likelihood{},
71- std::forward_as_tuple (to_vector (y), n_samples , std::forward<Mean>(mean)),
93+ std::forward_as_tuple (to_vector (y), y_index , std::forward<Mean>(mean)),
7294 std::forward<CovarFun>(covariance_function),
7395 std::forward<CovarArgs>(covar_args), std::move (options), msgs);
7496}
@@ -83,9 +105,8 @@ inline auto laplace_marginal_tol_bernoulli_logit_lpmf(
83105 * @tparam propto boolean ignored
84106 * @tparam Mean type of the mean of the latent normal distribution
85107 * \laplace_common_template_args
86- * @param[in] y total counts per group. Second sufficient statistics.
87- * @param[in] n_samples number of samples per group. First sufficient
88- * statistics.
108+ * @param[in] y binary observations
109+ * @param[in] y_index group to which each observation belongs
89110 * @param[in] mean the mean of the latent normal variable.
90111 * \laplace_common_args
91112 * @param[in] hessian_block_size Block size for the Hessian approximation with
@@ -95,13 +116,13 @@ inline auto laplace_marginal_tol_bernoulli_logit_lpmf(
95116template <bool propto = false , typename Mean, typename CovarFun,
96117 typename CovarArgs>
97118inline auto laplace_marginal_bernoulli_logit_lpmf (
98- const std::vector<int >& y, const std::vector<int >& n_samples , Mean&& mean,
119+ const std::vector<int >& y, const std::vector<int >& y_index , Mean&& mean,
99120 int hessian_block_size, CovarFun&& covariance_function,
100121 CovarArgs&& covar_args, std::ostream* msgs) {
101122 auto options = laplace_options_default{hessian_block_size};
102123 return laplace_marginal_density (
103124 bernoulli_logit_likelihood{},
104- std::forward_as_tuple (to_vector (y), n_samples , std::forward<Mean>(mean)),
125+ std::forward_as_tuple (to_vector (y), y_index , std::forward<Mean>(mean)),
105126 std::forward<CovarFun>(covariance_function),
106127 std::forward<CovarArgs>(covar_args), options, msgs);
107128}
0 commit comments