Automatic Differentiation
 
Loading...
Searching...
No Matches
beta_lpdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_BETA_LPDF_HPP
2#define STAN_MATH_PRIM_PROB_BETA_LPDF_HPP
3
20#include <cmath>
21
22namespace stan {
23namespace math {
24
43template <bool propto, typename T_y, typename T_scale_succ,
44 typename T_scale_fail,
46 T_y, T_scale_succ, T_scale_fail>* = nullptr>
48 const T_y& y, const T_scale_succ& alpha, const T_scale_fail& beta) {
50 using T_y_ref = ref_type_if_not_constant_t<T_y>;
53 static constexpr const char* function = "beta_lpdf";
54 check_consistent_sizes(function, "Random variable", y,
55 "First shape parameter", alpha,
56 "Second shape parameter", beta);
57 if (size_zero(y, alpha, beta)) {
58 return 0;
59 }
60
61 T_y_ref y_ref = y;
62 T_alpha_ref alpha_ref = alpha;
63 T_beta_ref beta_ref = beta;
64
65 decltype(auto) y_val = to_ref(as_value_column_array_or_scalar(y_ref));
66 decltype(auto) alpha_val = to_ref(as_value_column_array_or_scalar(alpha_ref));
67 decltype(auto) beta_val = to_ref(as_value_column_array_or_scalar(beta_ref));
68
69 check_positive_finite(function, "First shape parameter", alpha_val);
70 check_positive_finite(function, "Second shape parameter", beta_val);
71 check_bounded(function, "Random variable", value_of(y_val), 0, 1);
72 if constexpr (!include_summand<propto, T_y, T_scale_succ,
73 T_scale_fail>::value) {
74 return 0;
75 }
76
77 const auto& log_y = to_ref(log(y_val));
78 const auto& log1m_y = to_ref(log1m(y_val));
79
80 size_t N = max_size(y, alpha, beta);
81 T_partials_return logp(0);
83 logp -= sum(lgamma(alpha_val)) * N / max_size(alpha);
84 }
86 logp -= sum(lgamma(beta_val)) * N / max_size(beta);
87 }
89 logp += sum((alpha_val - 1.0) * log_y) * N / max_size(y, alpha);
90 }
92 logp += sum((beta_val - 1.0) * log1m_y) * N / max_size(y, beta);
93 }
94
95 auto ops_partials = make_partials_propagator(y_ref, alpha_ref, beta_ref);
96 if constexpr (is_autodiff_v<T_y>) {
97 edge<0>(ops_partials).partials_
98 = (alpha_val - 1) / y_val + (beta_val - 1) / (y_val - 1);
99 }
100
102 const auto& alpha_beta
103 = to_ref_if<is_any_autodiff_v<T_scale_succ, T_scale_fail>>(alpha_val
104 + beta_val);
105 logp += sum(lgamma(alpha_beta)) * N / max_size(alpha, beta);
106 if constexpr (is_any_autodiff_v<T_scale_succ, T_scale_fail>) {
107 const auto& digamma_alpha_beta
108 = to_ref_if<is_all_autodiff_v<T_scale_succ, T_scale_fail>>(
109 digamma(alpha_beta));
110 if constexpr (is_autodiff_v<T_scale_succ>) {
111 edge<1>(ops_partials).partials_
112 = log_y + digamma_alpha_beta - digamma(alpha_val);
113 }
114 if constexpr (is_autodiff_v<T_scale_fail>) {
115 edge<2>(ops_partials).partials_
116 = log1m_y + digamma_alpha_beta - digamma(beta_val);
117 }
118 }
119 }
120 return ops_partials.build(logp);
121}
122
123template <typename T_y, typename T_scale_succ, typename T_scale_fail>
125 const T_y& y, const T_scale_succ& alpha, const T_scale_fail& beta) {
126 return beta_lpdf<false>(y, alpha, beta);
127}
128
129} // namespace math
130} // namespace stan
131#endif
require_all_not_t< is_nonscalar_prim_or_rev_kernel_expression< std::decay_t< Types > >... > require_all_not_nonscalar_prim_or_rev_kernel_expression_t
Require none of the types satisfy is_nonscalar_prim_or_rev_kernel_expression.
return_type_t< T_y_cl, T_scale_succ_cl, T_scale_fail_cl > beta_lpdf(const T_y_cl &y, const T_scale_succ_cl &alpha, const T_scale_fail_cl &beta)
The log of the beta density for the specified scalar(s) given the specified sample stan::math::size(s...
Definition beta_lpdf.hpp:43
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition size_zero.hpp:19
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:18
fvar< T > log(const fvar< T > &x)
Definition log.hpp:18
auto as_value_column_array_or_scalar(T &&a)
Extract the value from an object and for eigen vectors and std::vectors convert to an eigen column ar...
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
fvar< T > lgamma(const fvar< T > &x)
Return the natural logarithm of the gamma function applied to the specified argument.
Definition lgamma.hpp:21
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:23
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:18
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:20
fvar< T > log1m(const fvar< T > &x)
Definition log1m.hpp:12
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
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
fvar< T > digamma(const fvar< T > &x)
Return the derivative of the log gamma function at the specified argument.
Definition digamma.hpp:23
typename ref_type_if< is_autodiff_v< T >, T >::type ref_type_if_not_constant_t
Definition ref_type.hpp:63
typename partials_return_type< Args... >::type partials_return_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...