Stan Math Library  2.20.0
reverse mode automatic differentiation
bernoulli_cdf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_BERNOULLI_CDF_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_BERNOULLI_CDF_HPP
3 
11 
12 namespace stan {
13 namespace math {
14 
27 template <typename T_n, typename T_prob>
29  const T_prob& theta) {
30  static const char* function = "bernoulli_cdf";
31  typedef
32  typename stan::partials_return_type<T_n, T_prob>::type T_partials_return;
33 
34  if (size_zero(n, theta))
35  return 1.0;
36 
37  T_partials_return P(1.0);
38 
39  check_finite(function, "Probability parameter", theta);
40  check_bounded(function, "Probability parameter", theta, 0.0, 1.0);
41  check_consistent_sizes(function, "Random variable", n,
42  "Probability parameter", theta);
43 
44  scalar_seq_view<T_n> n_vec(n);
45  scalar_seq_view<T_prob> theta_vec(theta);
46  size_t size = max_size(n, theta);
47 
48  operands_and_partials<T_prob> ops_partials(theta);
49 
50  // Explicit return for extreme values
51  // The gradients are technically ill-defined, but treated as zero
52  for (size_t i = 0; i < stan::length(n); i++) {
53  if (value_of(n_vec[i]) < 0)
54  return ops_partials.build(0.0);
55  }
56 
57  for (size_t i = 0; i < size; i++) {
58  // Explicit results for extreme values
59  // The gradients are technically ill-defined, but treated as zero
60  if (value_of(n_vec[i]) >= 1)
61  continue;
62 
63  const T_partials_return Pi = 1 - value_of(theta_vec[i]);
64 
65  P *= Pi;
66 
68  ops_partials.edge1_.partials_[i] += -1 / Pi;
69  }
70 
72  for (size_t i = 0; i < stan::length(theta); ++i)
73  ops_partials.edge1_.partials_[i] *= P;
74  }
75  return ops_partials.build(P);
76 }
77 
78 } // namespace math
79 } // namespace stan
80 #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
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_cdf(const T_n &n, const T_prob &theta)
Returns the CDF 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_

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