Automatic Differentiation
 
Loading...
Searching...
No Matches
chi_square_lpdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_CHI_SQUARE_LPDF_HPP
2#define STAN_MATH_OPENCL_PRIM_CHI_SQUARE_LPDF_HPP
3#ifdef STAN_OPENCL
4
10
11namespace stan {
12namespace math {
13
34template <
35 bool propto, typename T_y_cl, typename T_dof_cl,
36 require_all_prim_or_rev_kernel_expression_t<T_y_cl, T_dof_cl>* = nullptr,
37 require_any_not_stan_scalar_t<T_y_cl, T_dof_cl>* = nullptr>
39 const T_dof_cl& nu) {
40 static constexpr const char* function = "chi_square_lpdf(OpenCL)";
41 using T_partials_return = partials_return_t<T_y_cl, T_dof_cl>;
42 using std::isfinite;
43
44 check_consistent_sizes(function, "Random variable", y,
45 "Degrees of freedom parameter", nu);
46 const size_t N = max_size(y, nu);
47 if (N == 0) {
48 return 0.0;
49 }
51 return 0.0;
52 }
53
54 const auto& y_col = as_column_vector_or_scalar(y);
55 const auto& nu_col = as_column_vector_or_scalar(nu);
56
57 const auto& y_val = value_of(y_col);
58 const auto& nu_val = value_of(nu_col);
59
60 auto check_y_nonnegative
61 = check_cl(function, "Random variable", y_val, "nonnegative");
62 auto y_nonnegative_expr = 0 <= y_val;
63 auto check_nu_positive_finite = check_cl(
64 function, "Degrees of freedom parameter", nu_val, "positive finite");
65 auto nu_positive_finite_expr = 0 < nu_val && isfinite(nu_val);
66
67 auto log_y_expr = log(y_val);
68 auto half_nu_expr = 0.5 * nu_val;
69
70 auto logp1_expr = elt_multiply(half_nu_expr - 1, log_y_expr);
71 auto logp2_expr = static_select<include_summand<propto, T_dof_cl>::value>(
72 logp1_expr - nu_val * HALF_LOG_TWO - lgamma(half_nu_expr), logp1_expr);
73 auto logp_expr
75 logp2_expr - 0.5 * y_val, logp2_expr));
76
77 auto y_deriv_expr = elt_divide(half_nu_expr - 1, y_val) - 0.5;
78 auto nu_deriv_expr
79 = (log_y_expr - digamma(half_nu_expr)) * 0.5 - HALF_LOG_TWO;
80
81 matrix_cl<double> logp_cl;
82 matrix_cl<double> y_deriv_cl;
83 matrix_cl<double> nu_deriv_cl;
84
85 results(check_y_nonnegative, check_nu_positive_finite, logp_cl, y_deriv_cl,
86 nu_deriv_cl)
87 = expressions(y_nonnegative_expr, nu_positive_finite_expr, logp_expr,
88 calc_if<!is_constant<T_y_cl>::value>(y_deriv_expr),
89 calc_if<!is_constant<T_dof_cl>::value>(nu_deriv_expr));
90
91 T_partials_return logp = sum(from_matrix_cl(logp_cl));
92
93 auto ops_partials = make_partials_propagator(y_col, nu_col);
94
96 partials<0>(ops_partials) = std::move(y_deriv_cl);
97 }
99 partials<1>(ops_partials) = std::move(nu_deriv_cl);
100 }
101 return ops_partials.build(logp);
102}
103
104} // namespace math
105} // namespace stan
106
107#endif
108#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_dof_cl > chi_square_lpdf(const T_y_cl &y, const T_dof_cl &nu)
The log of a chi-squared density for y with the specified degrees of freedom parameter.
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.
static constexpr double HALF_LOG_TWO
The value of half the natural logarithm 2, .
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 > 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
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:20
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 ...
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...