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
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
11
12namespace stan {
13namespace math {
14
25template <typename T_theta, typename T_scalar = scalar_type_t<T_theta>,
26 require_vector_t<T_theta>* = nullptr>
27Eigen::Matrix<T_scalar, -1, 1> poisson_binomial_log_probs(
28 int y, const T_theta& theta) {
29 int size_theta = theta.size();
30 plain_type_t<T_theta> log_theta = log(theta);
31 plain_type_t<T_theta> log1m_theta = log1m(theta);
32
33 Eigen::Matrix<T_scalar, Eigen::Dynamic, Eigen::Dynamic> alpha(y + 1,
34 size_theta + 1);
35
36 // alpha[i, j] = log prob of j successes in first i trials
37 alpha(0, 0) = 0.0;
38 for (int i = 0; i < size_theta; ++i) {
39 // no success in i trials
40 alpha(0, i + 1) = alpha(0, i) + log1m_theta[i];
41
42 // 0 < j < i successes in i trials
43 for (int j = 0; j < std::min(y, i); ++j) {
44 alpha(j + 1, i + 1) = log_mix(theta[i], alpha(j, i), alpha(j + 1, i));
45 }
46
47 // i successes in i trials
48 if (i < y) {
49 alpha(i + 1, i + 1) = alpha(i, i) + log_theta[i];
50 }
51 }
52
53 return alpha.col(size_theta);
54}
55
56template <typename T_y, typename T_theta, require_vt_integral<T_y>* = nullptr>
57auto poisson_binomial_log_probs(const T_y& y, const T_theta& theta) {
58 using T_scalar = scalar_type_t<T_theta>;
59 size_t max_sizes = std::max(stan::math::size(y), size_mvt(theta));
60 std::vector<Eigen::Matrix<T_scalar, Eigen::Dynamic, 1>> result(max_sizes);
61 scalar_seq_view<T_y> y_vec(y);
62 vector_seq_view<T_theta> theta_vec(theta);
63
64 for (size_t i = 0; i < max_sizes; ++i) {
65 result[i] = poisson_binomial_log_probs(y_vec[i], theta_vec[i]);
66 }
67
68 return result;
69}
70
71} // namespace math
72} // namespace stan
73#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
Eigen::Matrix< T_scalar, -1, 1 > 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:18
fvar< T > log_mix(const fvar< T > &theta, const fvar< T > &lambda1, const fvar< T > &lambda2)
Return the log mixture density with specified mixing proportion and log densities and its derivative ...
Definition log_mix.hpp:98
fvar< T > log1m(const fvar< T > &x)
Definition log1m.hpp:12
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 ...