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);
73 return 0;
74 }
75
76 const auto& log_y = to_ref(log(y_val));
77 const auto& log1m_y = to_ref(log1m(y_val));
78
79 size_t N = max_size(y, alpha, beta);
80 T_partials_return logp(0);
82 logp -= sum(lgamma(alpha_val)) * N / max_size(alpha);
83 }
85 logp -= sum(lgamma(beta_val)) * N / max_size(beta);
86 }
88 logp += sum((alpha_val - 1.0) * log_y) * N / max_size(y, alpha);
89 }
91 logp += sum((beta_val - 1.0) * log1m_y) * N / max_size(y, beta);
92 }
93
94 auto ops_partials = make_partials_propagator(y_ref, alpha_ref, beta_ref);
96 edge<0>(ops_partials).partials_
97 = (alpha_val - 1) / y_val + (beta_val - 1) / (y_val - 1);
98 }
99
101 const auto& alpha_beta
102 = to_ref_if<!is_constant_all<T_scale_succ, T_scale_fail>::value>(
103 alpha_val + beta_val);
104 logp += sum(lgamma(alpha_beta)) * N / max_size(alpha, beta);
106 const auto& digamma_alpha_beta
107 = to_ref_if < !is_constant_all<T_scale_succ>::value
110 edge<1>(ops_partials).partials_
111 = log_y + digamma_alpha_beta - digamma(alpha_val);
112 }
114 edge<2>(ops_partials).partials_
115 = log1m_y + digamma_alpha_beta - digamma(beta_val);
116 }
117 }
118 }
119 return ops_partials.build(logp);
120}
121
122template <typename T_y, typename T_scale_succ, typename T_scale_fail>
124 const T_y& y, const T_scale_succ& alpha, const T_scale_fail& beta) {
125 return beta_lpdf<false>(y, alpha, beta);
126}
127
128} // namespace math
129} // namespace stan
130#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:15
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.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
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
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_constant< T >::value, T >::type ref_type_if_not_constant_t
Definition ref_type.hpp:62
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 ...
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...