Stan Math Library  2.20.0
reverse mode automatic differentiation
binomial_lcdf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_BINOMIAL_LCDF_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_BINOMIAL_LCDF_HPP
3 
14 #include <cmath>
15 
16 namespace stan {
17 namespace math {
18 
35 template <typename T_n, typename T_N, typename T_prob>
36 typename return_type<T_prob>::type binomial_lcdf(const T_n& n, const T_N& N,
37  const T_prob& theta) {
38  static const char* function = "binomial_lcdf";
40  T_partials_return;
41 
42  if (size_zero(n, N, theta))
43  return 0.0;
44 
45  T_partials_return P(0.0);
46 
47  check_nonnegative(function, "Population size parameter", N);
48  check_finite(function, "Probability parameter", theta);
49  check_bounded(function, "Probability parameter", theta, 0.0, 1.0);
50  check_consistent_sizes(function, "Successes variable", n,
51  "Population size parameter", N,
52  "Probability parameter", theta);
53 
54  scalar_seq_view<T_n> n_vec(n);
55  scalar_seq_view<T_N> N_vec(N);
56  scalar_seq_view<T_prob> theta_vec(theta);
57  size_t size = max_size(n, N, theta);
58 
59  using std::exp;
60  using std::log;
61  using std::pow;
62 
63  operands_and_partials<T_prob> ops_partials(theta);
64 
65  // Explicit return for extreme values
66  // The gradients are technically ill-defined,
67  // but treated as negative infinity
68  for (size_t i = 0; i < stan::length(n); i++) {
69  if (value_of(n_vec[i]) < 0)
70  return ops_partials.build(negative_infinity());
71  }
72 
73  for (size_t i = 0; i < size; i++) {
74  // Explicit results for extreme values
75  // The gradients are technically ill-defined, but treated as zero
76  if (value_of(n_vec[i]) >= value_of(N_vec[i])) {
77  continue;
78  }
79  const T_partials_return n_dbl = value_of(n_vec[i]);
80  const T_partials_return N_dbl = value_of(N_vec[i]);
81  const T_partials_return theta_dbl = value_of(theta_vec[i]);
82  const T_partials_return betafunc = beta(N_dbl - n_dbl, n_dbl + 1);
83  const T_partials_return Pi
84  = inc_beta(N_dbl - n_dbl, n_dbl + 1, 1 - theta_dbl);
85 
86  P += log(Pi);
87 
89  ops_partials.edge1_.partials_[i]
90  -= pow(theta_dbl, n_dbl) * pow(1 - theta_dbl, N_dbl - n_dbl - 1)
91  / betafunc / Pi;
92  }
93  return ops_partials.build(P);
94 }
95 
96 } // namespace math
97 } // namespace stan
98 #endif
void check_finite(const char *function, const char *name, const T_y &y)
Check if y is finite.
boost::math::tools::promote_args< double, typename partials_type< typename scalar_type< T >::type >::type, typename partials_return_type< T_pack... >::type >::type type
void check_bounded(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
Check if the value is between the low and high values, inclusively.
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
return_type< T_prob >::type binomial_lcdf(const T_n &n, const T_N &N, const T_prob &theta)
Returns the log CDF for the binomial distribution evaluated at the specified success, population size, and chance of success.
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.
fvar< T > inc_beta(const fvar< T > &a, const fvar< T > &b, const fvar< T > &x)
Definition: inc_beta.hpp:18
fvar< T > beta(const fvar< T > &x1, const fvar< T > &x2)
Return fvar with the beta function applied to the specified arguments and its gradient.
Definition: beta.hpp:51
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
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 size(const std::vector< T > &x)
Return the size of the specified standard vector.
Definition: size.hpp:17
fvar< T > pow(const fvar< T > &x1, const fvar< T > &x2)
Definition: pow.hpp:16
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.
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.