Automatic Differentiation
 
Loading...
Searching...
No Matches
beta_proportion_lpdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_BETA_PROPORTION_LPDF_HPP
2#define STAN_MATH_OPENCL_PRIM_BETA_PROPORTION_LPDF_HPP
3#ifdef STAN_OPENCL
4
12
13namespace stan {
14namespace math {
15
38template <bool propto, typename T_y_cl, typename T_loc_cl, typename T_prec_cl,
40 T_prec_cl>* = nullptr,
41 require_any_not_stan_scalar_t<T_y_cl, T_loc_cl, T_prec_cl>* = nullptr>
43 const T_y_cl& y, const T_loc_cl& mu, const T_prec_cl& kappa) {
44 static constexpr const char* function = "beta_proportion_lpdf(OpenCL)";
45 using T_partials_return = partials_return_t<T_y_cl, T_loc_cl, T_prec_cl>;
46 using std::isfinite;
47
48 check_consistent_sizes(function, "Random variable", y, "Location parameter",
49 mu, "Precision parameter", kappa);
50 const size_t N = max_size(y, mu, kappa);
51 if (N == 0) {
52 return 0.0;
53 }
55 return 0.0;
56 }
57
58 const auto& y_col = as_column_vector_or_scalar(y);
59 const auto& mu_col = as_column_vector_or_scalar(mu);
60 const auto& kappa_col = as_column_vector_or_scalar(kappa);
61
62 const auto& y_val = value_of(y_col);
63 const auto& mu_val = value_of(mu_col);
64 const auto& kappa_val = value_of(kappa_col);
65
66 auto check_y_bounded
67 = check_cl(function, "Random variable", y_val, "in the interval [0, 1]");
68 auto y_bounded_expr = 0 <= y_val && y_val <= 1;
69 auto check_mu_bounded = check_cl(function, "Location parameter", mu_val,
70 "in the interval (0, 1)");
71 auto mu_bounded_expr = 0 < mu_val && mu_val < 1;
72 auto check_kappa_positive_finite = check_cl(
73 function, "Precision parameter", kappa_val, "in the interval [0, 1]");
74 auto kappa_positive_finite = 0 < kappa_val && isfinite(kappa_val);
75
76 auto log_y_expr = log(y_val);
77 auto log1m_y_expr = log1p(-y_val);
78 auto mukappa_expr = elt_multiply(mu_val, kappa_val);
79 auto logp_expr = colwise_sum(
80 elt_multiply(mukappa_expr - 1, log_y_expr)
81 + elt_multiply(kappa_val - mukappa_expr - 1, log1m_y_expr)
83 lgamma(kappa_val), 0)
85 lgamma(mukappa_expr) + lgamma(kappa_val - mukappa_expr), 0));
86 auto y_deriv_expr = elt_divide(mukappa_expr - 1, y_val)
87 + elt_divide(kappa_val - mukappa_expr - 1, y_val - 1);
88 auto digamma_mukappa_expr = digamma(mukappa_expr);
89 auto digamma_kappa_mukappa_expr = digamma(kappa_val - mukappa_expr);
90 auto mu_deriv_expr = elt_multiply(kappa_val, digamma_kappa_mukappa_expr
91 - digamma_mukappa_expr
92 + log_y_expr - log1m_y_expr);
93 auto kappa_deriv_expr
94 = digamma(kappa_val)
95 + elt_multiply(mu_val, log_y_expr - digamma_mukappa_expr)
96 + elt_multiply(1 - mu_val, log1m_y_expr - digamma_kappa_mukappa_expr);
97
98 matrix_cl<double> logp_cl;
99 matrix_cl<double> y_deriv_cl;
100 matrix_cl<double> mu_deriv_cl;
101 matrix_cl<double> kappa_deriv_cl;
102
103 results(check_y_bounded, check_mu_bounded, check_kappa_positive_finite,
104 logp_cl, y_deriv_cl, mu_deriv_cl, kappa_deriv_cl)
105 = expressions(y_bounded_expr, mu_bounded_expr, kappa_positive_finite,
106 logp_expr,
107 calc_if<!is_constant<T_y_cl>::value>(y_deriv_expr),
108 calc_if<!is_constant<T_loc_cl>::value>(mu_deriv_expr),
109 calc_if<!is_constant<T_prec_cl>::value>(kappa_deriv_expr));
110
111 T_partials_return logp = sum(from_matrix_cl(logp_cl));
112
113 auto ops_partials = make_partials_propagator(y_col, mu_col, kappa_col);
115 partials<0>(ops_partials) = std::move(y_deriv_cl);
116 }
118 partials<1>(ops_partials) = std::move(mu_deriv_cl);
119 }
121 partials<2>(ops_partials) = std::move(kappa_deriv_cl);
122 }
123
124 return ops_partials.build(logp);
125}
126
127} // namespace math
128} // namespace stan
129#endif
130#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)
calc_if_< true, as_operation_cl_t< T > > calc_if(T &&a)
Definition calc_if.hpp:121
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.
return_type_t< T_y_cl, T_loc_cl, T_prec_cl > beta_proportion_lpdf(const T_y_cl &y, const T_loc_cl &mu, const T_prec_cl &kappa)
The log of the beta density for specified y, location, and precision: beta_proportion_lpdf(y | mu,...
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.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
size_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:19
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 > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:22
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 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 ...
Definition fvar.hpp:9
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...