Automatic Differentiation
 
Loading...
Searching...
No Matches
binomial_logit_lpmf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_BINOMIAL_LOGIT_LPMF_HPP
2#define STAN_MATH_OPENCL_PRIM_BINOMIAL_LOGIT_LPMF_HPP
3#ifdef STAN_OPENCL
4
12
13namespace stan {
14namespace math {
15
29template <bool propto, typename T_n_cl, typename T_N_cl, typename T_prob_cl,
31 T_prob_cl>* = nullptr,
33 T_n_cl, T_N_cl, T_prob_cl>* = nullptr>
35 const T_N_cl N,
36 const T_prob_cl& alpha) {
37 static constexpr const char* function = "binomial_logit_lpmf(OpenCL)";
38 using T_partials_return = partials_return_t<T_prob_cl>;
39 using std::isfinite;
40
41 check_consistent_sizes(function, "Successes variable", n,
42 "Population size parameter", N,
43 "Probability parameter", alpha);
44 const size_t siz = max_size(n, N, alpha);
45 if (siz == 0) {
46 return 0.0;
47 }
49 return 0.0;
50 }
51
52 const auto& alpha_col = as_column_vector_or_scalar(alpha);
53 const auto& alpha_val = value_of(alpha_col);
54
55 auto check_n_bounded
56 = check_cl(function, "Successes variable", n, "in the interval [0, N]");
57 auto n_bounded = 0 <= n && n <= N;
58 auto check_N_nonnegative
59 = check_cl(function, "Population size variable", n, "nonnegative");
60 auto N_nonnegative = N >= 0;
61 auto check_alpha_finite
62 = check_cl(function, "Probability parameter", alpha_val, "finite");
63 auto alpha_finite = isfinite(alpha_val);
64
65 auto log_inv_logit_alpha = log_inv_logit(alpha_val);
66 auto log1m_inv_logit_alpha = log1m_inv_logit(alpha_val);
67 auto n_diff = N - n;
68 auto logp_expr1 = elt_multiply(n, log_inv_logit_alpha)
69 + elt_multiply(n_diff, log1m_inv_logit_alpha);
70 auto logp_expr
71 = static_select<include_summand<propto, T_n_cl, T_N_cl>::value>(
72 logp_expr1 + binomial_coefficient_log(N, n), logp_expr1);
73 auto alpha_deriv = n - elt_multiply(N, exp(log_inv_logit_alpha));
74
75 matrix_cl<double> logp_cl;
76 matrix_cl<double> alpha_deriv_cl;
77
78 results(check_n_bounded, check_N_nonnegative, check_alpha_finite, logp_cl,
79 alpha_deriv_cl)
80 = expressions(n_bounded, N_nonnegative, alpha_finite, logp_expr,
81 calc_if<is_autodiff_v<T_prob_cl>>(alpha_deriv));
82
83 T_partials_return logp = sum(from_matrix_cl(logp_cl));
84 auto ops_partials = make_partials_propagator(alpha_col);
85 if constexpr (is_autodiff_v<T_prob_cl>) {
86 partials<0>(ops_partials) = std::move(alpha_deriv_cl);
87 }
88
89 return ops_partials.build(logp);
90}
91
92} // namespace math
93} // namespace stan
94#endif
95#endif
Represents an arithmetic matrix on the OpenCL device.
Definition matrix_cl.hpp:47
require_any_t< is_nonscalar_prim_or_rev_kernel_expression< std::decay_t< Types > >... > require_any_nonscalar_prim_or_rev_kernel_expression_t
Require any of the types satisfy is_nonscalar_prim_or_rev_kernel_expression.
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.
binomial_coefficient_log_< as_operation_cl_t< T1 >, as_operation_cl_t< T2 > > binomial_coefficient_log(T1 &&a, T2 &&b)
auto as_column_vector_or_scalar(T &&a)
as_column_vector_or_scalar of a kernel generator expression.
calc_if_< true, as_operation_cl_t< T > > calc_if(T &&a)
Definition calc_if.hpp:121
expressions_cl< T_expressions... > expressions(T_expressions &&... expressions)
Deduces types for constructing expressions_cl object.
return_type_t< T_prob_cl > binomial_logit_lpmf(const T_n_cl &n, const T_N_cl N, const T_prob_cl &alpha)
Binomial log PMF in logit parametrization.
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.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
fvar< T > log_inv_logit(const fvar< T > &x)
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
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_inv_logit(const fvar< T > &x)
Return the natural logarithm of one minus the inverse logit of the specified argument.
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:15
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...