Stan Math Library  2.20.0
reverse mode automatic differentiation
poisson_lpmf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_POISSON_LPMF_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_POISSON_LPMF_HPP
3 
14 #include <limits>
15 
16 namespace stan {
17 namespace math {
18 
19 // Poisson(n|lambda) [lambda > 0; n >= 0]
20 template <bool propto, typename T_n, typename T_rate>
22  const T_rate& lambda) {
23  typedef
24  typename stan::partials_return_type<T_n, T_rate>::type T_partials_return;
25 
26  static const char* function = "poisson_lpmf";
27 
28  if (size_zero(n, lambda))
29  return 0.0;
30 
31  T_partials_return logp(0.0);
32 
33  check_nonnegative(function, "Random variable", n);
34  check_not_nan(function, "Rate parameter", lambda);
35  check_nonnegative(function, "Rate parameter", lambda);
36  check_consistent_sizes(function, "Random variable", n, "Rate parameter",
37  lambda);
38 
40  return 0.0;
41 
42  scalar_seq_view<T_n> n_vec(n);
43  scalar_seq_view<T_rate> lambda_vec(lambda);
44  size_t size = max_size(n, lambda);
45 
46  for (size_t i = 0; i < size; i++)
47  if (is_inf(lambda_vec[i]))
48  return LOG_ZERO;
49  for (size_t i = 0; i < size; i++)
50  if (lambda_vec[i] == 0 && n_vec[i] != 0)
51  return LOG_ZERO;
52 
53  operands_and_partials<T_rate> ops_partials(lambda);
54 
55  for (size_t i = 0; i < size; i++) {
56  if (!(lambda_vec[i] == 0 && n_vec[i] == 0)) {
58  logp -= lgamma(n_vec[i] + 1.0);
60  logp += multiply_log(n_vec[i], value_of(lambda_vec[i]))
61  - value_of(lambda_vec[i]);
62  }
63 
65  ops_partials.edge1_.partials_[i]
66  += n_vec[i] / value_of(lambda_vec[i]) - 1.0;
67  }
68  return ops_partials.build(logp);
69 }
70 
71 template <typename T_n, typename T_rate>
72 inline typename return_type<T_rate>::type poisson_lpmf(const T_n& n,
73  const T_rate& lambda) {
74  return poisson_lpmf<false>(n, lambda);
75 }
76 
77 } // namespace math
78 } // namespace stan
79 #endif
boost::math::tools::promote_args< double, typename partials_type< typename scalar_type< T >::type >::type, typename partials_return_type< T_pack... >::type >::type type
fvar< T > lgamma(const fvar< T > &x)
Return the natural logarithm of the gamma function applied to the specified argument.
Definition: lgamma.hpp:21
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition: value_of.hpp:17
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...
Definition: conjunction.hpp:14
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
This template builds partial derivatives with respect to a set of operands.
bool size_zero(T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition: size_zero.hpp:18
const double LOG_ZERO
Definition: constants.hpp:150
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
void check_nonnegative(const char *function, const char *name, const T_y &y)
Check if y is non-negative.
boost::math::tools::promote_args< double, typename scalar_type< T >::type, typename return_type< Types_pack... >::type >::type type
Definition: return_type.hpp:36
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
size_t max_size(const T1 &x1, const T2 &x2)
Definition: max_size.hpp:9
T_return_type build(double value)
Build the node to be stored on the autodiff graph.
int is_inf(const fvar< T > &x)
Returns 1 if the input&#39;s value is infinite and 0 otherwise.
Definition: is_inf.hpp:20
fvar< T > multiply_log(const fvar< T > &x1, const fvar< T > &x2)
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
Definition: size.hpp:17
void check_consistent_sizes(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)
Check if the dimension of x1 is consistent with x2.
return_type< T_rate >::type poisson_lpmf(const T_n &n, const T_rate &lambda)
internal::ops_partials_edge< double, Op1 > edge1_

     [ Stan Home Page ] © 2011–2018, Stan Development Team.