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>
34return_type_t<T_prob_cl> binomial_logit_lpmf(const T_n_cl& n, const T_N_cl N,
35 const T_prob_cl& alpha) {
36 static constexpr const char* function = "binomial_logit_lpmf(OpenCL)";
37 using T_partials_return = partials_return_t<T_prob_cl>;
38 using std::isfinite;
39
40 check_consistent_sizes(function, "Successes variable", n,
41 "Population size parameter", N,
42 "Probability parameter", alpha);
43 const size_t siz = max_size(n, N, alpha);
44 if (siz == 0) {
45 return 0.0;
46 }
48 return 0.0;
49 }
50
51 const auto& alpha_col = as_column_vector_or_scalar(alpha);
52 const auto& alpha_val = value_of(alpha_col);
53
54 auto check_n_bounded
55 = check_cl(function, "Successes variable", n, "in the interval [0, N]");
56 auto n_bounded = 0 <= n && n <= N;
57 auto check_N_nonnegative
58 = check_cl(function, "Population size variable", n, "nonnegative");
59 auto N_nonnegative = N >= 0;
60 auto check_alpha_finite
61 = check_cl(function, "Probability parameter", alpha_val, "finite");
62 auto alpha_finite = isfinite(alpha_val);
63
64 auto log_inv_logit_alpha = log_inv_logit(alpha_val);
65 auto log1m_inv_logit_alpha = log1m_inv_logit(alpha_val);
66 auto n_diff = N - n;
67 auto logp_expr1 = elt_multiply(n, log_inv_logit_alpha)
68 + elt_multiply(n_diff, log1m_inv_logit_alpha);
69 auto logp_expr
70 = static_select<include_summand<propto, T_n_cl, T_N_cl>::value>(
71 logp_expr1 + binomial_coefficient_log(N, n), logp_expr1);
72 auto alpha_deriv = n - elt_multiply(N, exp(log_inv_logit_alpha));
73
74 matrix_cl<double> logp_cl;
75 matrix_cl<double> alpha_deriv_cl;
76
77 results(check_n_bounded, check_N_nonnegative, check_alpha_finite, logp_cl,
78 alpha_deriv_cl)
79 = expressions(n_bounded, N_nonnegative, alpha_finite, logp_expr,
81
82 T_partials_return logp = sum(from_matrix_cl(logp_cl));
83 auto ops_partials = make_partials_propagator(alpha_col);
85 partials<0>(ops_partials) = std::move(alpha_deriv_cl);
86 }
87
88 return ops_partials.build(logp);
89}
90
91} // namespace math
92} // namespace stan
93#endif
94#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:13
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...