Automatic Differentiation
 
Loading...
Searching...
No Matches
std_normal_lccdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_STD_NORMAL_LCCDF_HPP
2#define STAN_MATH_OPENCL_PRIM_STD_NORMAL_LCCDF_HPP
3#ifdef STAN_OPENCL
4
12
13namespace stan {
14namespace math {
15
24template <typename T_y_cl,
25 require_all_prim_or_rev_kernel_expression_t<T_y_cl>* = nullptr,
26 require_any_not_stan_scalar_t<T_y_cl>* = nullptr>
28 static constexpr const char* function = "std_normal_lccdf(OpenCL)";
29 using T_partials_return = partials_return_t<T_y_cl>;
30 using std::isfinite;
31 using std::isnan;
32
33 const size_t N = math::size(y);
34 if (N == 0) {
35 return 1.0;
36 }
37
38 const auto& y_col = as_column_vector_or_scalar(y);
39 const auto& y_val = value_of(y_col);
40
41 auto check_y_not_nan
42 = check_cl(function, "Random variable", y_val, "not NaN");
43 auto y_not_nan_expr = !isnan(y_val);
44
45 auto scaled_y = y_val * INV_SQRT_TWO;
46 auto one_m_erf
47 = select(y_val < -37.5, 2.0,
48 select(y_val < -5.0, 2.0 - erfc(-scaled_y),
49 select(y_val > 8.25, 0.0, 1.0 - erf(scaled_y))));
50 auto lccdf_expr = colwise_sum(log(one_m_erf));
51 auto y_deriv = -select(
52 y_val > 8.25, INFTY,
53 SQRT_TWO_OVER_SQRT_PI * elt_divide(exp(-square(scaled_y)), one_m_erf));
54
55 matrix_cl<double> lccdf_cl;
56 matrix_cl<double> y_deriv_cl;
57
58 results(check_y_not_nan, lccdf_cl, y_deriv_cl)
59 = expressions(y_not_nan_expr, lccdf_expr,
61
62 T_partials_return lccdf = from_matrix_cl(lccdf_cl).sum() + LOG_HALF * N;
63
64 auto ops_partials = make_partials_propagator(y_col);
65
67 partials<0>(ops_partials) = std::move(y_deriv_cl);
68 }
69 return ops_partials.build(lccdf);
70}
71
72} // namespace math
73} // namespace stan
74#endif
75#endif
Represents an arithmetic matrix on the OpenCL device.
Definition matrix_cl.hpp:47
select_< as_operation_cl_t< T_condition >, as_operation_cl_t< T_then >, as_operation_cl_t< T_else > > select(T_condition &&condition, T_then &&then, T_else &&els)
Selection operation on kernel generator expressions.
Definition select.hpp:148
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 > std_normal_lccdf(const T_y_cl &y)
Returns the log standard normal complementary cumulative distribution function.
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_HALF
The natural logarithm of 0.5, .
Definition constants.hpp:92
static constexpr double SQRT_TWO_OVER_SQRT_PI
The square root of 2 divided by the square root of , .
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:18
fvar< T > erf(const fvar< T > &x)
Definition erf.hpp:16
static constexpr double INV_SQRT_TWO
The value of 1 over the square root of 2, .
fvar< T > erfc(const fvar< T > &x)
Definition erfc.hpp:16
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
static constexpr double INFTY
Positive infinity.
Definition constants.hpp:46
fvar< T > square(const fvar< T > &x)
Definition square.hpp:12
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
Metaprogramming struct to detect whether a given type is constant in the mathematical sense (not the ...