Automatic Differentiation
 
Loading...
Searching...
No Matches
pareto_type_2_lccdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_DOUBLE_PARETO_TYPE_2_LCCDF_HPP
2#define STAN_MATH_OPENCL_PRIM_DOUBLE_PARETO_TYPE_2_LCCDF_HPP
3#ifdef STAN_OPENCL
4
12
13namespace stan {
14namespace math {
15
30template <typename T_y_cl, typename T_loc_cl, typename T_scale_cl,
31 typename T_shape_cl,
33 T_y_cl, T_loc_cl, T_scale_cl, T_shape_cl>* = nullptr,
34 require_any_not_stan_scalar_t<T_y_cl, T_loc_cl, T_scale_cl,
35 T_shape_cl>* = nullptr>
37 const T_y_cl& y, const T_loc_cl& mu, const T_scale_cl& lambda,
38 const T_shape_cl& alpha) {
39 static constexpr const char* function = "pareto_type_2_lccdf(OpenCL)";
40 using T_partials_return
42 using std::isfinite;
43 using std::isnan;
44
45 check_consistent_sizes(function, "Random variable", y, "Location parameter",
46 mu, "Scale parameter", lambda, "Shape parameter",
47 alpha);
48 const size_t N = max_size(y, mu, lambda, alpha);
49 if (N == 0) {
50 return 1.0;
51 }
52
53 const auto& y_col = as_column_vector_or_scalar(y);
54 const auto& mu_col = as_column_vector_or_scalar(mu);
55 const auto& lambda_col = as_column_vector_or_scalar(lambda);
56 const auto& alpha_col = as_column_vector_or_scalar(alpha);
57
58 const auto& y_val = value_of(y_col);
59 const auto& mu_val = value_of(mu_col);
60 const auto& lambda_val = value_of(lambda_col);
61 const auto& alpha_val = value_of(alpha_col);
62
63 auto check_y_nonnegative
64 = check_cl(function, "Random variable", y_val, "nonnegative");
65 auto y_nonnegative_expr = 0 <= y_val;
66 auto check_lambda_positive_finite
67 = check_cl(function, "Scale parameter", lambda_val, "positive finite");
68 auto lambda_positive_finite_expr = 0 < lambda_val && isfinite(lambda_val);
69 auto check_alpha_positive_finite
70 = check_cl(function, "Shape parameter", alpha_val, "positive finite");
71 auto alpha_positive_finite_expr = 0 < alpha_val && isfinite(alpha_val);
72 auto diff = y_val - mu_val;
73 auto check_diff_nonnegative
74 = check_cl(function, "Random variable minus location parameter", diff,
75 "nonnegative");
76 auto diff_nonnegative_expr = 0 <= diff;
77
78 auto log_temp = log1p(elt_divide(diff, lambda_val));
79 auto lccdf_expr = colwise_sum(elt_multiply(alpha_val, log_temp));
80
81 auto mu_deriv = elt_divide(alpha_val, diff + lambda_val);
82 auto y_deriv = -mu_deriv;
83 auto lambda_deriv = elt_divide(elt_multiply(mu_deriv, diff), lambda_val);
84 auto alpha_deriv = -log_temp;
85
86 matrix_cl<double> lccdf_cl;
87 matrix_cl<double> y_deriv_cl;
88 matrix_cl<double> mu_deriv_cl;
89 matrix_cl<double> lambda_deriv_cl;
90 matrix_cl<double> alpha_deriv_cl;
91
92 results(check_y_nonnegative, check_lambda_positive_finite,
93 check_alpha_positive_finite, check_diff_nonnegative, lccdf_cl,
94 y_deriv_cl, mu_deriv_cl, lambda_deriv_cl, alpha_deriv_cl)
95 = expressions(y_nonnegative_expr, lambda_positive_finite_expr,
96 alpha_positive_finite_expr, diff_nonnegative_expr,
97 lccdf_expr, calc_if<!is_constant<T_y_cl>::value>(y_deriv),
101
102 T_partials_return lccdf = -from_matrix_cl(lccdf_cl).sum();
103
104 auto ops_partials
105 = make_partials_propagator(y_col, mu_col, lambda_col, alpha_col);
108 partials<0>(ops_partials) = std::move(y_deriv_cl);
109 }
111 partials<1>(ops_partials) = std::move(mu_deriv_cl);
112 }
114 partials<2>(ops_partials) = std::move(lambda_deriv_cl);
115 }
117 partials<3>(ops_partials) = std::move(alpha_deriv_cl);
118 }
119 }
120 return ops_partials.build(lccdf);
121}
122
123} // namespace math
124} // namespace stan
125#endif
126#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_loc_cl, T_scale_cl, T_shape_cl > pareto_type_2_lccdf(const T_y_cl &y, const T_loc_cl &mu, const T_scale_cl &lambda, const T_shape_cl &alpha)
Returns the pareto type 2 log complementaty cumulative density 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.
size_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:19
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
fvar< T > log1p(const fvar< T > &x)
Definition log1p.hpp:12
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 ...
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...