Stan Math Library  2.20.0
reverse mode automatic differentiation
poisson_lccdf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_POISSON_LCCDF_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_POISSON_LCCDF_HPP
3 
12 #include <cmath>
13 #include <limits>
14 
15 namespace stan {
16 namespace math {
17 
18 template <typename T_n, typename T_rate>
20  const T_rate& lambda) {
21  static const char* function = "poisson_lccdf";
22  typedef
23  typename stan::partials_return_type<T_n, T_rate>::type T_partials_return;
24 
25  if (size_zero(n, lambda))
26  return 0.0;
27 
28  T_partials_return P(0.0);
29 
30  check_not_nan(function, "Rate parameter", lambda);
31  check_nonnegative(function, "Rate parameter", lambda);
32  check_consistent_sizes(function, "Random variable", n, "Rate parameter",
33  lambda);
34 
35  scalar_seq_view<T_n> n_vec(n);
36  scalar_seq_view<T_rate> lambda_vec(lambda);
37  size_t size = max_size(n, lambda);
38 
39  using std::exp;
40  using std::log;
41 
42  operands_and_partials<T_rate> ops_partials(lambda);
43 
44  // Explicit return for extreme values
45  // The gradients are technically ill-defined, but treated as neg infinity
46  for (size_t i = 0; i < stan::length(n); i++) {
47  if (value_of(n_vec[i]) < 0)
48  return ops_partials.build(0.0);
49  }
50 
51  for (size_t i = 0; i < size; i++) {
52  // Explicit results for extreme values
53  // The gradients are technically ill-defined, but treated as zero
54  if (value_of(n_vec[i]) == std::numeric_limits<int>::max())
55  return ops_partials.build(negative_infinity());
56 
57  const T_partials_return n_dbl = value_of(n_vec[i]);
58  const T_partials_return lambda_dbl = value_of(lambda_vec[i]);
59  const T_partials_return log_Pi = log(gamma_p(n_dbl + 1, lambda_dbl));
60 
61  P += log_Pi;
62 
64  ops_partials.edge1_.partials_[i] += exp(
65  n_dbl * log(lambda_dbl) - lambda_dbl - lgamma(n_dbl + 1) - log_Pi);
66  }
67  return ops_partials.build(P);
68 }
69 
70 } // namespace math
71 } // namespace stan
72 #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
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:12
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.
size_t length(const std::vector< T > &x)
Returns the length of the provided std::vector.
Definition: length.hpp:16
bool size_zero(T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition: size_zero.hpp:18
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
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:11
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 max(const std::vector< int > &x)
Returns the maximum coefficient in the specified column vector.
Definition: max.hpp:21
fvar< T > gamma_p(const fvar< T > &x1, const fvar< T > &x2)
Definition: gamma_p.hpp:15
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_lccdf(const T_n &n, const T_rate &lambda)
internal::ops_partials_edge< double, Op1 > edge1_
double negative_infinity()
Return negative infinity.
Definition: constants.hpp:115

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