Automatic Differentiation
 
Loading...
Searching...
No Matches
poisson_binomial_log_probs.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_POISSON_BINOMIAL_LOG_PROBS_HPP
2#define STAN_MATH_PRIM_FUN_POISSON_BINOMIAL_LOG_PROBS_HPP
3
10
11namespace stan {
12namespace math {
13
24template <typename T_theta, typename T_scalar = scalar_type_t<T_theta>,
25 require_eigen_vector_t<T_theta>* = nullptr>
27 int size_theta = theta.size();
28 plain_type_t<T_theta> log_theta = log(theta);
29 plain_type_t<T_theta> log1m_theta = log1m(theta);
30
31 Eigen::Matrix<T_scalar, Eigen::Dynamic, Eigen::Dynamic> alpha(size_theta + 1,
32 y + 1);
33
34 // alpha[i, j] = log prob of j successes in first i trials
35 alpha(0, 0) = 0.0;
36 for (int i = 0; i < size_theta; ++i) {
37 // no success in i trials
38 alpha(i + 1, 0) = alpha(i, 0) + log1m_theta[i];
39
40 // 0 < j < i successes in i trials
41 for (int j = 0; j < std::min(y, i); ++j) {
42 alpha(i + 1, j + 1) = log_sum_exp(alpha(i, j) + log_theta[i],
43 alpha(i, j + 1) + log1m_theta[i]);
44 }
45
46 // i successes in i trials
47 if (i < y) {
48 alpha(i + 1, i + 1) = alpha(i, i) + log_theta(i);
49 }
50 }
51
52 return alpha.row(size_theta);
53}
54
55template <typename T_y, typename T_theta, require_vt_integral<T_y>* = nullptr>
56auto poisson_binomial_log_probs(const T_y& y, const T_theta& theta) {
57 using T_scalar = scalar_type_t<T_theta>;
58 size_t max_sizes = std::max(stan::math::size(y), size_mvt(theta));
59 std::vector<Eigen::Matrix<T_scalar, Eigen::Dynamic, 1>> result(max_sizes);
60 scalar_seq_view<T_y> y_vec(y);
61 vector_seq_view<T_theta> theta_vec(theta);
62
63 for (size_t i = 0; i < max_sizes; ++i) {
64 result[i] = poisson_binomial_log_probs(y_vec[i], theta_vec[i]);
65 }
66
67 return result;
68}
69
70} // namespace math
71} // namespace stan
72#endif
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
This class provides a low-cost wrapper for situations where you either need an Eigen Vector or RowVec...
int64_t size_mvt(const ScalarT &)
Provides the size of a multivariate argument.
Definition size_mvt.hpp:25
int64_t size(const T &m)
Returns the size (number of the elements) of a matrix_cl or var_value<matrix_cl<T>>.
Definition size.hpp:19
plain_type_t< T_theta > poisson_binomial_log_probs(int y, const T_theta &theta)
Returns the last row of the log probability matrix of the Poisson-Binomial distribution given the num...
fvar< T > log(const fvar< T > &x)
Definition log.hpp:15
fvar< T > log1m(const fvar< T > &x)
Definition log1m.hpp:12
fvar< T > log_sum_exp(const fvar< T > &x1, const fvar< T > &x2)
typename plain_type< T >::type plain_type_t
typename scalar_type< T >::type scalar_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...