Automatic Differentiation
 
Loading...
Searching...
No Matches
poisson_log_lpmf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_POISSON_LOG_LPMF_HPP
2#define STAN_MATH_OPENCL_PRIM_POISSON_LOG_LPMF_HPP
3#ifdef STAN_OPENCL
4
11
12namespace stan {
13namespace math {
14
27template <bool propto, typename T_n_cl, typename T_log_rate_cl,
29 T_log_rate_cl>* = nullptr,
30 require_any_not_stan_scalar_t<T_n_cl, T_log_rate_cl>* = nullptr>
32 const T_log_rate_cl& alpha) {
33 static constexpr const char* function = "poisson_log_lpmf(OpenCL)";
34 using T_partials_return = partials_return_t<T_log_rate_cl>;
35 using std::isinf;
36 using std::isnan;
37 constexpr bool is_n_vector = !is_stan_scalar<T_n_cl>::value;
38
39 check_consistent_sizes(function, "Random variable", n, "Log rate parameter",
40 alpha);
41 const size_t N = is_n_vector ? math::size(n) : math::size(alpha);
42 if (N == 0) {
43 return 0.0;
44 }
46 return 0.0;
47 }
48
49 const auto& alpha_col = as_column_vector_or_scalar(alpha);
50 const auto& alpha_val = value_of(alpha_col);
51
52 T_partials_return logp(0.0);
53 auto ops_partials = make_partials_propagator(alpha_col);
54
55 auto check_n_nonnegative
56 = check_cl(function, "Random variable", n, "nonnegative");
57 auto n_nonnegativer = 0 <= n;
58 auto check_alpha_not_nan
59 = check_cl(function, "Log rate parameter", alpha_val, "not nan");
60 auto alpha_not_nan = !isnan(alpha_val);
61
62 auto return_log_zero
63 = colwise_max(cast<char>(isinf(alpha_val) && (alpha_val > 0 || n != 0)));
64 auto exp_alpha = exp(alpha_val);
65
66 auto logp1 = elt_multiply(n, alpha_val);
67 auto logp2 = static_select<include_summand<propto, T_log_rate_cl>::value>(
68 logp1 - exp_alpha, logp1);
70 logp2 - lgamma(n + 1.0), logp2));
71
72 auto deriv = n - exp_alpha;
73
74 matrix_cl<char> return_log_zero_cl;
75 matrix_cl<double> logp_cl;
76 matrix_cl<double> deriv_cl;
77
78 results(check_n_nonnegative, check_alpha_not_nan, return_log_zero_cl, logp_cl,
79 deriv_cl)
80 = expressions(n_nonnegativer, alpha_not_nan, return_log_zero, logp_expr,
82
83 if (from_matrix_cl(return_log_zero_cl).any()) {
84 return LOG_ZERO;
85 }
86
87 logp = sum(from_matrix_cl(logp_cl));
88
90 partials<0>(ops_partials) = deriv_cl;
91 }
92
93 return ops_partials.build(logp);
94}
95
96} // namespace math
97} // namespace stan
98#endif
99#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)
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.
auto colwise_max(T &&a)
Column wise max - reduction of a kernel generator expression.
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.
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
return_type_t< T_log_rate_cl > poisson_log_lpmf(const T_n_cl &n, const T_log_rate_cl &alpha)
Returns the log PMF of the Poisson log distribution.
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.
int64_t size(const T &m)
Returns the size (number of the elements) of a matrix_cl or var_value<matrix_cl<T>>.
Definition size.hpp:19
static constexpr double LOG_ZERO
The natural logarithm of 0, .
Definition constants.hpp:68
constexpr bool any(T x)
Return true if any values in the input are true.
Definition any.hpp:21
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
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 > 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
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 ...
bool isnan(const stan::math::var &a)
Checks if the given number is NaN.
Definition std_isnan.hpp:18
bool isinf(const stan::math::var &a)
Return 1 if the specified argument is positive infinity or negative infinity and 0 otherwise.
Definition std_isinf.hpp:16
Checks if decayed type is a var, fvar, or arithmetic.
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...