Stan Math Library  2.20.0
reverse mode automatic differentiation
bernoulli_lccdf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_BERNOULLI_LCCDF_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_BERNOULLI_LCCDF_HPP
3 
11 #include <cmath>
12 
13 namespace stan {
14 namespace math {
15 
28 template <typename T_n, typename T_prob>
30  const T_prob& theta) {
31  static const char* function = "bernoulli_lccdf";
32  typedef
33  typename stan::partials_return_type<T_n, T_prob>::type T_partials_return;
34 
35  if (size_zero(n, theta))
36  return 0.0;
37 
38  T_partials_return P(0.0);
39 
40  check_finite(function, "Probability parameter", theta);
41  check_bounded(function, "Probability parameter", theta, 0.0, 1.0);
42  check_consistent_sizes(function, "Random variable", n,
43  "Probability parameter", theta);
44 
45  scalar_seq_view<T_n> n_vec(n);
46  scalar_seq_view<T_prob> theta_vec(theta);
47  size_t size = max_size(n, theta);
48 
49  using std::log;
50  operands_and_partials<T_prob> ops_partials(theta);
51 
52  // Explicit return for extreme values
53  // The gradients are technically ill-defined, but treated as zero
54  for (size_t i = 0; i < stan::length(n); i++) {
55  if (value_of(n_vec[i]) < 0)
56  return ops_partials.build(0.0);
57  }
58 
59  for (size_t i = 0; i < size; i++) {
60  // Explicit results for extreme values
61  // The gradients are technically ill-defined, but treated as zero
62  if (value_of(n_vec[i]) >= 1) {
63  return ops_partials.build(negative_infinity());
64  } else {
65  const T_partials_return Pi = value_of(theta_vec[i]);
66 
67  P += log(Pi);
68 
70  ops_partials.edge1_.partials_[i] += 1 / Pi;
71  }
72  }
73 
74  return ops_partials.build(P);
75 }
76 } // namespace math
77 } // namespace stan
78 #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
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
return_type< T_prob >::type bernoulli_lccdf(const T_n &n, const T_prob &theta)
Returns the log CCDF of the Bernoulli distribution.
bool size_zero(T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition: size_zero.hpp:18
boost::math::tools::promote_args< double, typename scalar_type< T >::type, typename return_type< Types_pack... >::type >::type type
Definition: return_type.hpp:36
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
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.