Automatic Differentiation
 
Loading...
Searching...
No Matches
std_normal_lpdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_STD_NORMAL_LPDF_HPP
2#define STAN_MATH_OPENCL_PRIM_STD_NORMAL_LPDF_HPP
3#ifdef STAN_OPENCL
4
12
13namespace stan {
14namespace math {
15
29template <bool propto, typename T_y_cl,
30 require_all_prim_or_rev_kernel_expression_t<T_y_cl>* = nullptr,
31 require_any_not_stan_scalar_t<T_y_cl>* = nullptr>
32inline return_type_t<T_y_cl> std_normal_lpdf(const T_y_cl& y) {
33 static constexpr const char* function = "std_normal_lpdf(OpenCL)";
34 using T_partials_return = partials_return_t<T_y_cl>;
35 using std::isfinite;
36 using std::isnan;
37
38 const size_t N = math::size(y);
39 if (N == 0) {
40 return 0.0;
41 }
43 return 0.0;
44 }
45
46 const auto& y_col = as_column_vector_or_scalar(y);
47 const auto& y_val = value_of(y_col);
48
49 auto check_y_not_nan
50 = check_cl(function, "Random variable", y_val, "not NaN");
51 auto y_not_nan = !isnan(y_val);
52
53 auto logp_expr = colwise_sum(elt_multiply(y_val, y_val));
54
55 auto y_deriv = -y_val;
56
57 matrix_cl<double> logp_cl;
58 matrix_cl<double> y_deriv_cl;
59
60 results(check_y_not_nan, logp_cl, y_deriv_cl) = expressions(
61 y_not_nan, logp_expr, calc_if<!is_constant<T_y_cl>::value>(y_deriv));
62
63 T_partials_return logp = sum(from_matrix_cl(logp_cl)) * -0.5;
64
66 logp += NEG_LOG_SQRT_TWO_PI * N;
67 }
68
69 auto ops_partials = make_partials_propagator(y_col);
70
72 partials<0>(ops_partials) = std::move(y_deriv_cl);
73 }
74 return ops_partials.build(logp);
75}
76
77} // namespace math
78} // namespace stan
79#endif
80#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.
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 > std_normal_lpdf(const T_y_cl &y)
The log of the normal density for the specified scalar(s) given a location of 0 and a scale of 1.
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
size_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:18
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
const double NEG_LOG_SQRT_TWO_PI
The value of minus the natural logarithm of the square root of , .
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:22
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 ...
Definition fvar.hpp:9
bool isnan(const stan::math::var &a)
Checks if the given number is NaN.
Definition std_isnan.hpp:18
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...