Stan Math Library  2.20.0
reverse mode automatic differentiation
bernoulli_lpmf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_BERNOULLI_LPMF_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_BERNOULLI_LPMF_HPP
3 
11 #include <cmath>
12 
13 namespace stan {
14 namespace math {
15 
28 template <bool propto, typename T_n, typename T_prob>
30  const T_prob& theta) {
31  static const char* function = "bernoulli_lpmf";
32  typedef
33  typename stan::partials_return_type<T_n, T_prob>::type T_partials_return;
34 
35  using std::log;
36 
37  if (size_zero(n, theta))
38  return 0.0;
39 
40  T_partials_return logp(0.0);
41 
42  check_bounded(function, "n", n, 0, 1);
43  check_finite(function, "Probability parameter", theta);
44  check_bounded(function, "Probability parameter", theta, 0.0, 1.0);
45  check_consistent_sizes(function, "Random variable", n,
46  "Probability parameter", theta);
47 
49  return 0.0;
50 
51  scalar_seq_view<T_n> n_vec(n);
52  scalar_seq_view<T_prob> theta_vec(theta);
53  size_t N = max_size(n, theta);
54  operands_and_partials<T_prob> ops_partials(theta);
55 
56  if (length(theta) == 1) {
57  size_t sum = 0;
58  for (size_t n = 0; n < N; n++) {
59  sum += value_of(n_vec[n]);
60  }
61  const T_partials_return theta_dbl = value_of(theta_vec[0]);
62  // avoid nans when sum == N or sum == 0
63  if (sum == N) {
64  logp += N * log(theta_dbl);
66  ops_partials.edge1_.partials_[0] += N / theta_dbl;
67  } else if (sum == 0) {
68  logp += N * log1m(theta_dbl);
70  ops_partials.edge1_.partials_[0] += N / (theta_dbl - 1);
71  } else {
72  const T_partials_return log_theta = log(theta_dbl);
73  const T_partials_return log1m_theta = log1m(theta_dbl);
74 
75  logp += sum * log_theta;
76  logp += (N - sum) * log1m_theta;
77 
79  ops_partials.edge1_.partials_[0] += sum / theta_dbl;
80  ops_partials.edge1_.partials_[0] += (N - sum) / (theta_dbl - 1);
81  }
82  }
83  } else {
84  for (size_t n = 0; n < N; n++) {
85  const int n_int = value_of(n_vec[n]);
86  const T_partials_return theta_dbl = value_of(theta_vec[n]);
87 
88  if (n_int == 1)
89  logp += log(theta_dbl);
90  else
91  logp += log1m(theta_dbl);
92 
94  if (n_int == 1)
95  ops_partials.edge1_.partials_[n] += 1.0 / theta_dbl;
96  else
97  ops_partials.edge1_.partials_[n] += 1.0 / (theta_dbl - 1);
98  }
99  }
100  }
101  return ops_partials.build(logp);
102 }
103 
104 template <typename T_y, typename T_prob>
105 inline typename return_type<T_prob>::type bernoulli_lpmf(const T_y& n,
106  const T_prob& theta) {
107  return bernoulli_lpmf<false>(n, theta);
108 }
109 
110 } // namespace math
111 } // namespace stan
112 #endif
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition: sum.hpp:20
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
bool size_zero(T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition: size_zero.hpp:18
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
return_type< T_prob >::type bernoulli_lpmf(const T_n &n, const T_prob &theta)
Returns the log PMF of the Bernoulli distribution.
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.
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.
fvar< T > log1m(const fvar< T > &x)
Definition: log1m.hpp:12
internal::ops_partials_edge< double, Op1 > edge1_

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