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
16#include <cmath>
17
18namespace stan {
19namespace math {
20
39template <bool propto, typename T_y, typename T_scale_succ,
40 typename T_scale_fail,
42 T_y, T_scale_succ, T_scale_fail>* = nullptr>
44 const T_y& y, const T_scale_succ& alpha, const T_scale_fail& beta) {
46 using T_y_ref = ref_type_if_not_constant_t<T_y>;
49 static constexpr const char* function = "beta_lpdf";
50 check_consistent_sizes(function, "Random variable", y,
51 "First shape parameter", alpha,
52 "Second shape parameter", beta);
53 if (size_zero(y, alpha, beta)) {
54 return 0;
55 }
56
57 T_y_ref y_ref = y;
58 T_alpha_ref alpha_ref = alpha;
59 T_beta_ref beta_ref = beta;
60
61 decltype(auto) y_val = to_ref(as_value_column_array_or_scalar(y_ref));
62 decltype(auto) alpha_val = to_ref(as_value_column_array_or_scalar(alpha_ref));
63 decltype(auto) beta_val = to_ref(as_value_column_array_or_scalar(beta_ref));
64
65 check_positive_finite(function, "First shape parameter", alpha_val);
66 check_positive_finite(function, "Second shape parameter", beta_val);
67 check_bounded(function, "Random variable", value_of(y_val), 0, 1);
68 if constexpr (!include_summand<propto, T_y, T_scale_succ,
69 T_scale_fail>::value) {
70 return 0;
71 }
72
73 const auto& log_y = to_ref(log(y_val));
74 const auto& log1m_y = to_ref(log1m(y_val));
75
76 size_t N = max_size(y, alpha, beta);
77 T_partials_return logp(0);
79 logp -= sum(lgamma(alpha_val)) * N / max_size(alpha);
80 }
82 logp -= sum(lgamma(beta_val)) * N / max_size(beta);
83 }
85 logp += sum((alpha_val - 1.0) * log_y) * N / max_size(y, alpha);
86 }
88 logp += sum((beta_val - 1.0) * log1m_y) * N / max_size(y, beta);
89 }
90
91 auto ops_partials = make_partials_propagator(y_ref, alpha_ref, beta_ref);
92 if constexpr (is_autodiff_v<T_y>) {
93 edge<0>(ops_partials).partials_
94 = (alpha_val - 1) / y_val + (beta_val - 1) / (y_val - 1);
95 }
96
98 const auto& alpha_beta
99 = to_ref_if<is_any_autodiff_v<T_scale_succ, T_scale_fail>>(alpha_val
100 + beta_val);
101 logp += sum(lgamma(alpha_beta)) * N / max_size(alpha, beta);
102 if constexpr (is_any_autodiff_v<T_scale_succ, T_scale_fail>) {
103 const auto& digamma_alpha_beta
104 = to_ref_if<is_all_autodiff_v<T_scale_succ, T_scale_fail>>(
105 digamma(alpha_beta));
106 if constexpr (is_autodiff_v<T_scale_succ>) {
107 edge<1>(ops_partials).partials_
108 = log_y + digamma_alpha_beta - digamma(alpha_val);
109 }
110 if constexpr (is_autodiff_v<T_scale_fail>) {
111 edge<2>(ops_partials).partials_
112 = log1m_y + digamma_alpha_beta - digamma(beta_val);
113 }
114 }
115 }
116 return ops_partials.build(logp);
117}
118
119template <typename T_y, typename T_scale_succ, typename T_scale_fail>
121 const T_y& y, const T_scale_succ& alpha, const T_scale_fail& beta) {
122 return beta_lpdf<false>(y, alpha, beta);
123}
124
125} // namespace math
126} // namespace stan
127#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...