Automatic Differentiation
 
Loading...
Searching...
No Matches
poisson_lpmf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_POISSON_LPMF_HPP
2#define STAN_MATH_PRIM_PROB_POISSON_LPMF_HPP
3
21
22namespace stan {
23namespace math {
24
25// Poisson(n|lambda) [lambda > 0; n >= 0]
26template <bool propto, typename T_n, typename T_rate,
28 T_n, T_rate>* = nullptr>
29return_type_t<T_rate> poisson_lpmf(const T_n& n, const T_rate& lambda) {
30 using T_partials_return = partials_return_t<T_n, T_rate>;
31 using T_n_ref = ref_type_if_not_constant_t<T_n>;
32 using T_lambda_ref = ref_type_if_not_constant_t<T_rate>;
33 using std::isinf;
34 static constexpr const char* function = "poisson_lpmf";
35 check_consistent_sizes(function, "Random variable", n, "Rate parameter",
36 lambda);
37
38 T_n_ref n_ref = n;
39 T_lambda_ref lambda_ref = lambda;
40
41 decltype(auto) n_val = to_ref(as_value_column_array_or_scalar(n_ref));
42 decltype(auto) lambda_val
44
45 check_nonnegative(function, "Random variable", n_val);
46 check_nonnegative(function, "Rate parameter", lambda_val);
47
48 if (size_zero(n, lambda)) {
49 return 0.0;
50 }
52 return 0.0;
53 }
54 if (sum(promote_scalar<int>(isinf(lambda_val)))) {
55 return LOG_ZERO;
56 }
57
58 size_t N = max_size(n, lambda);
59 scalar_seq_view<decltype(n_val)> n_vec(n_val);
60 scalar_seq_view<decltype(lambda_val)> lambda_vec(lambda_val);
61 for (size_t i = 0; i < N; i++) {
62 if (lambda_vec[i] == 0 && n_vec[i] != 0) {
63 return LOG_ZERO;
64 }
65 }
66
67 auto ops_partials = make_partials_propagator(lambda_ref);
68
69 T_partials_return logp = stan::math::sum(multiply_log(n_val, lambda_val));
71 logp -= sum(lambda_val) * N / math::size(lambda);
72 }
74 logp -= sum(lgamma(n_val + 1.0)) * N / math::size(n);
75 }
76
78 partials<0>(ops_partials) = n_val / lambda_val - 1.0;
79 }
80
81 return ops_partials.build(logp);
82}
83
84template <typename T_n, typename T_rate>
85inline return_type_t<T_rate> poisson_lpmf(const T_n& n, const T_rate& lambda) {
86 return poisson_lpmf<false>(n, lambda);
87}
88
89} // namespace math
90} // namespace stan
91#endif
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
require_all_not_t< is_nonscalar_prim_or_rev_kernel_expression< std::decay_t< Types > >... > require_all_not_nonscalar_prim_or_rev_kernel_expression_t
Require none of the types satisfy is_nonscalar_prim_or_rev_kernel_expression.
return_type_t< T_rate_cl > poisson_lpmf(const T_n_cl &n, const T_rate_cl &lambda)
Returns the log PMF of the Poisson distribution.
size_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:18
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
static constexpr double LOG_ZERO
The natural logarithm of 0, .
Definition constants.hpp:68
fvar< T > multiply_log(const fvar< T > &x1, const fvar< T > &x2)
size_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:19
void check_nonnegative(const char *function, const char *name, const T_y &y)
Check if y is non-negative.
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition size_zero.hpp:19
auto as_value_column_array_or_scalar(T &&a)
Extract the value from an object and for eigen vectors and std::vectors convert to an eigen column ar...
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:22
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
fvar< T > lgamma(const fvar< T > &x)
Return the natural logarithm of the gamma function applied to the specified argument.
Definition lgamma.hpp:21
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
typename ref_type_if<!is_constant< T >::value, T >::type ref_type_if_not_constant_t
Definition ref_type.hpp:62
typename partials_return_type< Args... >::type partials_return_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Definition fvar.hpp:9
bool isinf(const stan::math::var &a)
Return 1 if the specified argument is positive infinity or negative infinity and 0 otherwise.
Definition std_isinf.hpp:16
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...