Automatic Differentiation
 
Loading...
Searching...
No Matches
beta_lpdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_BETA_LPDF_HPP
2#define STAN_MATH_OPENCL_PRIM_BETA_LPDF_HPP
3#ifdef STAN_OPENCL
4
15
16namespace stan {
17namespace math {
18
37template <bool propto, typename T_y_cl, typename T_scale_succ_cl,
38 typename T_scale_fail_cl,
40 T_y_cl, T_scale_succ_cl, T_scale_fail_cl>* = nullptr,
41 require_any_not_stan_scalar_t<T_y_cl, T_scale_succ_cl,
42 T_scale_fail_cl>* = nullptr>
44 const T_y_cl& y, const T_scale_succ_cl& alpha,
45 const T_scale_fail_cl& beta) {
46 using std::isfinite;
47 static constexpr const char* function = "beta_lpdf(OpenCL)";
48 using T_partials_return
50
51 check_consistent_sizes(function, "Random variable", y,
52 "First shape parameter", alpha,
53 "Second shape parameter", beta);
54 const size_t N = max_size(y, alpha, beta);
55 if (N == 0) {
56 return 0.0;
57 }
58 if (!include_summand<propto, T_y_cl, T_scale_succ_cl,
59 T_scale_fail_cl>::value) {
60 return 0.0;
61 }
62
63 const auto& y_col = as_column_vector_or_scalar(y);
64 const auto& alpha_col = as_column_vector_or_scalar(alpha);
65 const auto& beta_col = as_column_vector_or_scalar(beta);
66
67 const auto& y_val = value_of(y_col);
68 const auto& alpha_val = value_of(alpha_col);
69 const auto& beta_val = value_of(beta_col);
70
71 auto ops_partials = make_partials_propagator(y_col, alpha_col, beta_col);
72
73 auto check_alpha_pos_finite = check_cl(function, "First shape parameter",
74 alpha_val, "positive finite");
75 auto alpha_pos_finite = alpha_val > 0 && isfinite(alpha_val);
76 auto check_beta_pos_finite = check_cl(function, "Second shape parameter",
77 beta_val, "positive finite");
78 auto beta_pos_finite = beta_val > 0 && isfinite(beta_val);
79 auto check_y_bounded
80 = check_cl(function, "Random variable", y_val, "in the interval [0, 1]");
81 auto y_bounded = 0 <= y_val && y_val <= 1;
82
83 auto log_y_expr = log(y_val);
84 auto log1m_y_expr = log1p(-y_val);
85 auto alpha_beta_expr = alpha_val + beta_val;
86
87 auto zero_expr
88 = as_operation_cl(0); // simplifiy the kernel by only using one zero
89 auto logp_expr = colwise_sum(
91 -lgamma(alpha_val), zero_expr)
93 -lgamma(beta_val), zero_expr)
95 elt_multiply((alpha_val - 1.0), log_y_expr), zero_expr)
97 elt_multiply((beta_val - 1.0), log1m_y_expr), zero_expr)
100 lgamma(alpha_beta_expr), zero_expr));
101
102 auto y_deriv_expr = calc_if<!is_constant<T_y_cl>::value>(
103 elt_divide((alpha_val - 1), y_val)
104 + elt_divide((beta_val - 1), (y_val - 1)));
105 auto digamma_alpha_beta_expr = digamma(alpha_beta_expr);
106 auto alpha_deriv_expr = calc_if<!is_constant<T_scale_succ_cl>::value>(
107 log_y_expr + digamma_alpha_beta_expr - digamma(alpha_val));
108 auto beta_deriv_expr = calc_if<!is_constant<T_scale_fail_cl>::value>(
109 log1m_y_expr + digamma_alpha_beta_expr - digamma(beta_val));
110
111 matrix_cl<double> logp_cl;
112 matrix_cl<double> y_deriv_cl;
113 matrix_cl<double> alpha_deriv_cl;
114 matrix_cl<double> beta_deriv_cl;
115
116 results(check_alpha_pos_finite, check_beta_pos_finite, check_y_bounded,
117 logp_cl, y_deriv_cl, alpha_deriv_cl, beta_deriv_cl)
118 = expressions(alpha_pos_finite, beta_pos_finite, y_bounded, logp_expr,
119 y_deriv_expr, alpha_deriv_expr, beta_deriv_expr);
120
121 T_partials_return logp = sum(from_matrix_cl(logp_cl));
122
124 partials<0>(ops_partials) = std::move(y_deriv_cl);
125 }
127 partials<1>(ops_partials) = std::move(alpha_deriv_cl);
128 }
130 partials<2>(ops_partials) = std::move(beta_deriv_cl);
131 }
132
133 return ops_partials.build(logp);
134}
135
136} // namespace math
137} // namespace stan
138#endif
139#endif
Represents an arithmetic matrix on the OpenCL device.
Definition matrix_cl.hpp:47
elt_multiply_< as_operation_cl_t< T_a >, as_operation_cl_t< T_b > > elt_multiply(T_a &&a, T_b &&b)
isfinite_< as_operation_cl_t< T > > isfinite(T &&a)
auto check_cl(const char *function, const char *var_name, T &&y, const char *must_be)
Constructs a check on opencl matrix or expression.
Definition check_cl.hpp:219
results_cl< T_results... > results(T_results &&... results)
Deduces types for constructing results_cl object.
auto as_column_vector_or_scalar(T &&a)
as_column_vector_or_scalar of a kernel generator expression.
elt_divide_< as_operation_cl_t< T_a >, as_operation_cl_t< T_b > > elt_divide(T_a &&a, T_b &&b)
auto colwise_sum(T &&a)
Column wise sum - reduction of a kernel generator expression.
expressions_cl< T_expressions... > expressions(T_expressions &&... expressions)
Deduces types for constructing expressions_cl object.
T_operation && as_operation_cl(T_operation &&a)
Converts any valid kernel generator expression into an operation.
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
auto from_matrix_cl(const T &src)
Copies the source matrix that is stored on the OpenCL device to the destination Eigen matrix.
Definition copy.hpp:61
require_all_t< is_prim_or_rev_kernel_expression< std::decay_t< Types > >... > require_all_prim_or_rev_kernel_expression_t
Require type satisfies is_prim_or_rev_kernel_expression.
require_any_not_t< is_stan_scalar< std::decay_t< Types > >... > require_any_not_stan_scalar_t
Require at least one of the types do not satisfy is_stan_scalar.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
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
T1 static_select(T1 &&a, T2 &&b)
Returns one of the arguments that can be of different type, depending on the compile time condition.
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
fvar< T > log1p(const fvar< T > &x)
Definition log1p.hpp:12
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 > 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.
fvar< T > digamma(const fvar< T > &x)
Return the derivative of the log gamma function at the specified argument.
Definition digamma.hpp:23
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 ...
Metaprogramming struct to detect whether a given type is constant in the mathematical sense (not the ...
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...