Stan Math Library  2.20.0
reverse mode automatic differentiation
binomial_lpmf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_BINOMIAL_LPMF_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_BINOMIAL_LPMF_HPP
3 
14 
15 namespace stan {
16 namespace math {
17 
35 template <bool propto, typename T_n, typename T_N, typename T_prob>
36 typename return_type<T_prob>::type binomial_lpmf(const T_n& n, const T_N& N,
37  const T_prob& theta) {
39  T_partials_return;
40 
41  static const char* function = "binomial_lpmf";
42 
43  if (size_zero(n, N, theta))
44  return 0.0;
45 
46  T_partials_return logp = 0;
47  check_bounded(function, "Successes variable", n, 0, N);
48  check_nonnegative(function, "Population size parameter", N);
49  check_finite(function, "Probability parameter", theta);
50  check_bounded(function, "Probability parameter", theta, 0.0, 1.0);
51  check_consistent_sizes(function, "Successes variable", n,
52  "Population size parameter", N,
53  "Probability parameter", theta);
54 
56  return 0.0;
57 
58  scalar_seq_view<T_n> n_vec(n);
59  scalar_seq_view<T_N> N_vec(N);
60  scalar_seq_view<T_prob> theta_vec(theta);
61  size_t size = max_size(n, N, theta);
62 
63  operands_and_partials<T_prob> ops_partials(theta);
64 
66  for (size_t i = 0; i < size; ++i)
67  logp += binomial_coefficient_log(N_vec[i], n_vec[i]);
68  }
69 
71  for (size_t i = 0; i < length(theta); ++i)
72  log1m_theta[i] = log1m(value_of(theta_vec[i]));
73 
74  for (size_t i = 0; i < size; ++i)
75  logp += multiply_log(n_vec[i], value_of(theta_vec[i]))
76  + (N_vec[i] - n_vec[i]) * log1m_theta[i];
77 
78  if (length(theta) == 1) {
79  T_partials_return temp1 = 0;
80  T_partials_return temp2 = 0;
81  for (size_t i = 0; i < size; ++i) {
82  temp1 += n_vec[i];
83  temp2 += N_vec[i] - n_vec[i];
84  }
86  ops_partials.edge1_.partials_[0]
87  += temp1 / value_of(theta_vec[0])
88  - temp2 / (1.0 - value_of(theta_vec[0]));
89  }
90  } else {
92  for (size_t i = 0; i < size; ++i)
93  ops_partials.edge1_.partials_[i]
94  += n_vec[i] / value_of(theta_vec[i])
95  - (N_vec[i] - n_vec[i]) / (1.0 - value_of(theta_vec[i]));
96  }
97  }
98 
99  return ops_partials.build(logp);
100 }
101 
102 template <typename T_n, typename T_N, typename T_prob>
103 inline typename return_type<T_prob>::type binomial_lpmf(const T_n& n,
104  const T_N& N,
105  const T_prob& theta) {
106  return binomial_lpmf<false>(n, N, theta);
107 }
108 
109 } // namespace math
110 } // namespace stan
111 #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
fvar< T > binomial_coefficient_log(const fvar< T > &x1, const fvar< T > &x2)
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...
return_type< T_prob >::type binomial_lpmf(const T_n &n, const T_N &N, const T_prob &theta)
Returns the log PMF for the binomial distribution evaluated at the specified success, population size, and chance of success.
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...
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
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.
fvar< T > multiply_log(const fvar< T > &x1, const fvar< T > &x2)
VectorBuilder allocates type T1 values to be used as intermediate values.
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.
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.