Stan Math Library  2.20.0
reverse mode automatic differentiation
beta_lpdf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_BETA_LPDF_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_BETA_LPDF_HPP
3 
17 #include <cmath>
18 
19 namespace stan {
20 namespace math {
21 
40 template <bool propto, typename T_y, typename T_scale_succ,
41  typename T_scale_fail>
43  const T_y& y, const T_scale_succ& alpha, const T_scale_fail& beta) {
44  static const char* function = "beta_lpdf";
45 
46  typedef
48  T_partials_return;
49  using std::log;
50  check_positive_finite(function, "First shape parameter", alpha);
51  check_positive_finite(function, "Second shape parameter", beta);
52  check_not_nan(function, "Random variable", y);
53  check_consistent_sizes(function, "Random variable", y,
54  "First shape parameter", alpha,
55  "Second shape parameter", beta);
56  check_nonnegative(function, "Random variable", y);
57  check_less_or_equal(function, "Random variable", y, 1);
58 
59  if (size_zero(y, alpha, beta))
60  return 0;
62  return 0;
63 
64  T_partials_return logp(0);
65  scalar_seq_view<T_y> y_vec(y);
66  scalar_seq_view<T_scale_succ> alpha_vec(alpha);
67  scalar_seq_view<T_scale_fail> beta_vec(beta);
68  size_t N = max_size(y, alpha, beta);
69 
70  for (size_t n = 0; n < N; n++) {
71  const T_partials_return y_dbl = value_of(y_vec[n]);
72  if (y_dbl < 0 || y_dbl > 1)
73  return LOG_ZERO;
74  }
75 
77  beta);
78 
80  T_partials_return, T_y>
81  log_y(length(y));
83  T_partials_return, T_y>
84  log1m_y(length(y));
85 
86  for (size_t n = 0; n < length(y); n++) {
88  log_y[n] = log(value_of(y_vec[n]));
90  log1m_y[n] = log1m(value_of(y_vec[n]));
91  }
92 
94  T_scale_succ>
95  lgamma_alpha(length(alpha));
97  T_scale_succ>
98  digamma_alpha(length(alpha));
99  for (size_t n = 0; n < length(alpha); n++) {
101  lgamma_alpha[n] = lgamma(value_of(alpha_vec[n]));
103  digamma_alpha[n] = digamma(value_of(alpha_vec[n]));
104  }
105 
107  T_scale_fail>
108  lgamma_beta(length(beta));
110  T_scale_fail>
111  digamma_beta(length(beta));
112 
113  for (size_t n = 0; n < length(beta); n++) {
115  lgamma_beta[n] = lgamma(value_of(beta_vec[n]));
117  digamma_beta[n] = digamma(value_of(beta_vec[n]));
118  }
119 
121  T_partials_return, T_scale_succ, T_scale_fail>
122  lgamma_alpha_beta(max_size(alpha, beta));
123 
125  T_partials_return, T_scale_succ, T_scale_fail>
126  digamma_alpha_beta(max_size(alpha, beta));
127 
128  for (size_t n = 0; n < max_size(alpha, beta); n++) {
129  const T_partials_return alpha_beta
130  = value_of(alpha_vec[n]) + value_of(beta_vec[n]);
132  lgamma_alpha_beta[n] = lgamma(alpha_beta);
134  digamma_alpha_beta[n] = digamma(alpha_beta);
135  }
136 
137  for (size_t n = 0; n < N; n++) {
138  const T_partials_return y_dbl = value_of(y_vec[n]);
139  const T_partials_return alpha_dbl = value_of(alpha_vec[n]);
140  const T_partials_return beta_dbl = value_of(beta_vec[n]);
141 
143  logp += lgamma_alpha_beta[n];
145  logp -= lgamma_alpha[n];
147  logp -= lgamma_beta[n];
149  logp += (alpha_dbl - 1.0) * log_y[n];
151  logp += (beta_dbl - 1.0) * log1m_y[n];
152 
154  ops_partials.edge1_.partials_[n]
155  += (alpha_dbl - 1) / y_dbl + (beta_dbl - 1) / (y_dbl - 1);
157  ops_partials.edge2_.partials_[n]
158  += log_y[n] + digamma_alpha_beta[n] - digamma_alpha[n];
160  ops_partials.edge3_.partials_[n]
161  += log1m_y[n] + digamma_alpha_beta[n] - digamma_beta[n];
162  }
163  return ops_partials.build(logp);
164 }
165 
166 template <typename T_y, typename T_scale_succ, typename T_scale_fail>
168  const T_y& y, const T_scale_succ& alpha, const T_scale_fail& beta) {
169  return beta_lpdf<false>(y, alpha, beta);
170 }
171 
172 } // namespace math
173 } // namespace stan
174 #endif
void check_less_or_equal(const char *function, const char *name, const T_y &y, const T_high &high)
Check if y is less or equal to high.
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 > lgamma(const fvar< T > &x)
Return the natural logarithm of the gamma function applied to the specified argument.
Definition: lgamma.hpp:21
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
const double LOG_ZERO
Definition: constants.hpp:150
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.
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
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
return_type< T_y, T_scale_succ, T_scale_fail >::type beta_lpdf(const T_y &y, const T_scale_succ &alpha, const T_scale_fail &beta)
The log of the beta density for the specified scalar(s) given the specified sample size(s)...
Definition: beta_lpdf.hpp:42
boost::math::tools::promote_args< double, typename scalar_type< T >::type, typename return_type< Types_pack... >::type >::type type
Definition: return_type.hpp:36
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
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.
VectorBuilder allocates type T1 values to be used as intermediate values.
internal::ops_partials_edge< double, Op2 > edge2_
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, Op3 > edge3_
internal::ops_partials_edge< double, Op1 > edge1_
fvar< T > digamma(const fvar< T > &x)
Return the derivative of the log gamma function at the specified argument.
Definition: digamma.hpp:23

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