Automatic Differentiation
 
Loading...
Searching...
No Matches
std_normal_lcdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_STD_NORMAL_LCDF_HPP
2#define STAN_MATH_OPENCL_PRIM_STD_NORMAL_LCDF_HPP
3#ifdef STAN_OPENCL
4
13
14namespace stan {
15namespace math {
16namespace internal {
17constexpr char std_normal_lcdf_opencl_func[] = "std_normal_lcdf(OpenCL)";
18} // namespace internal
27template <const char* func = internal::std_normal_lcdf_opencl_func,
28 typename T_y_cl,
31inline return_type_t<T_y_cl> std_normal_lcdf(const T_y_cl& y) {
32 static constexpr const char* function = func;
33 using std::isfinite;
34 using std::isnan;
35
36 const size_t N = math::size(y);
37 if (N == 0) {
38 return 1.0;
39 }
40
41 const auto& y_col = as_column_vector_or_scalar(y);
42 const auto& y_val = value_of(y_col);
43
44 auto check_y_not_nan
45 = check_cl(function, "Random variable", y_val, "not NaN");
46 auto y_not_nan_expr = !isnan(y_val);
47
48 auto scaled_y = y_val * INV_SQRT_TWO;
49 auto lcdf_expr = colwise_sum(std_normal_lcdf_scaled_impl(scaled_y));
50 auto dnlcdf = std_normal_lcdf_dscaled_impl(scaled_y);
51 auto y_deriv = dnlcdf * INV_SQRT_TWO;
52
53 matrix_cl<double> lcdf_cl;
54 matrix_cl<double> y_deriv_cl;
55
56 results(check_y_not_nan, lcdf_cl, y_deriv_cl) = expressions(
57 y_not_nan_expr, lcdf_expr, calc_if<is_autodiff_v<T_y_cl>>(y_deriv));
58
59 double lcdf = from_matrix_cl(lcdf_cl).sum();
60
61 auto ops_partials = make_partials_propagator(y_col);
62
63 if constexpr (is_autodiff_v<T_y_cl>) {
64 partials<0>(ops_partials) = std::move(y_deriv_cl);
65 }
66 return ops_partials.build(lcdf);
67}
68
69} // namespace math
70} // namespace stan
71#endif
72#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.
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.
std_normal_lcdf_dscaled_impl_< as_operation_cl_t< T > > std_normal_lcdf_dscaled_impl(T &&a)
std_normal_lcdf_scaled_impl_< as_operation_cl_t< T > > std_normal_lcdf_scaled_impl(T &&a)
return_type_t< T_y_cl > std_normal_lcdf(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
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.
require_any_not_t< is_stan_scalar< std::decay_t< Types > >... > require_any_not_stan_scalar_t
Require at least one of the types do not satisfy is_stan_scalar.
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
constexpr char std_normal_lcdf_opencl_func[]
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
static constexpr double INV_SQRT_TWO
The value of 1 over the square root of 2, .
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
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