Automatic Differentiation
 
Loading...
Searching...
No Matches
poisson_lpmf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_POISSON_LPMF_HPP
2#define STAN_MATH_OPENCL_PRIM_POISSON_LPMF_HPP
3#ifdef STAN_OPENCL
4
11
12namespace stan {
13namespace math {
14
27template <
28 bool propto, typename T_n_cl, typename T_rate_cl,
29 require_all_prim_or_rev_kernel_expression_t<T_n_cl, T_rate_cl>* = nullptr,
30 require_any_not_stan_scalar_t<T_n_cl, T_rate_cl>* = nullptr>
32 const T_rate_cl& lambda) {
33 static constexpr const char* function = "poisson_lpmf(OpenCL)";
34 using T_partials_return = partials_return_t<T_rate_cl>;
35 using std::isinf;
36 constexpr bool is_n_vector = !is_stan_scalar<T_n_cl>::value;
37
38 check_consistent_sizes(function, "Random variable", n, "Rate parameter",
39 lambda);
40 const size_t N = is_n_vector ? math::size(n) : math::size(lambda);
41 if (N == 0) {
42 return 0.0;
43 }
45 return 0.0;
46 }
47
48 const auto& lambda_col = as_column_vector_or_scalar(lambda);
49 const auto& lambda_val = value_of(lambda_col);
50
51 T_partials_return logp(0.0);
52 auto ops_partials = make_partials_propagator(lambda_col);
53
54 auto check_n_nonnegative
55 = check_cl(function, "Random variable", n, "nonnegative");
56 auto n_nonnegative = 0 <= n;
57 auto check_lambda_nonnegative
58 = check_cl(function, "Log rate parameter", lambda_val, "nonnegative");
59 auto lambda_nonnegative = 0.0 <= lambda_val;
60
61 auto return_log_zero = colwise_max(
62 cast<char>(isinf(lambda_val) || (lambda_val == 0 && n != 0)));
63
64 auto logp1 = multiply_log(n, lambda_val);
65 auto logp2 = static_select<include_summand<propto, T_rate_cl>::value>(
66 logp1 - lambda_val, logp1);
68 logp2 - lgamma(n + 1.0), logp2));
69
70 auto deriv = elt_divide(n, lambda_val) - 1.0;
71
72 matrix_cl<char> return_log_zero_cl;
73 matrix_cl<double> logp_cl;
74 matrix_cl<double> deriv_cl;
75
76 results(check_n_nonnegative, check_lambda_nonnegative, return_log_zero_cl,
77 logp_cl, deriv_cl)
78 = expressions(n_nonnegative, lambda_nonnegative, return_log_zero,
79 logp_expr,
81
82 if (from_matrix_cl(return_log_zero_cl).any()) {
83 return LOG_ZERO;
84 }
85
86 logp = sum(from_matrix_cl(logp_cl));
87
89 partials<0>(ops_partials) = deriv_cl;
90 }
91
92 return ops_partials.build(logp);
93}
94
95} // namespace math
96} // namespace stan
97#endif
98#endif
Represents an arithmetic matrix on the OpenCL device.
Definition matrix_cl.hpp:47
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_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.
return_type_t< T_rate_cl > poisson_lpmf(const T_n_cl &n, const T_rate_cl &lambda)
Returns the log PMF of the Poisson distribution.
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
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
fvar< T > multiply_log(const fvar< T > &x1, const fvar< T > &x2)
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.
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 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...