Automatic Differentiation
 
Loading...
Searching...
No Matches
pareto_type_2_cdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_DOUBLE_PARETO_TYPE_2_CDF_HPP
2#define STAN_MATH_OPENCL_PRIM_DOUBLE_PARETO_TYPE_2_CDF_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_cdf(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 summed = lambda_val + diff;
79 auto temp = elt_divide(summed, lambda_val);
80 auto p1_pow_alpha = pow(temp, -alpha_val);
81 auto cdf_expr = colwise_prod(1.0 - p1_pow_alpha);
82
83 auto inv_cdf_n = elt_divide(1.0, 1.0 - p1_pow_alpha);
84 auto y_deriv1 = elt_multiply(elt_divide(p1_pow_alpha, summed),
85 elt_multiply(alpha_val, inv_cdf_n));
86 auto lambda_deriv1 = elt_multiply(elt_divide(diff, -lambda_val), y_deriv1);
87 auto alpha_deriv1
88 = elt_multiply(elt_multiply(log(temp), p1_pow_alpha), inv_cdf_n);
89
90 matrix_cl<double> cdf_cl;
91 matrix_cl<double> y_deriv_cl;
92 matrix_cl<double> mu_deriv_cl;
93 matrix_cl<double> lambda_deriv_cl;
94 matrix_cl<double> alpha_deriv_cl;
95
96 results(check_y_nonnegative, check_lambda_positive_finite,
97 check_alpha_positive_finite, check_diff_nonnegative, cdf_cl,
98 mu_deriv_cl, lambda_deriv_cl, alpha_deriv_cl)
100 y_nonnegative_expr, lambda_positive_finite_expr,
101 alpha_positive_finite_expr, diff_nonnegative_expr, cdf_expr,
105
106 T_partials_return cdf = (from_matrix_cl(cdf_cl)).prod();
107
108 auto ops_partials
109 = make_partials_propagator(y_col, mu_col, lambda_col, alpha_col);
111 auto y_deriv = mu_deriv_cl * cdf;
112 auto mu_deriv = -y_deriv;
113 auto lambda_deriv = lambda_deriv_cl * cdf;
114 auto alpha_deriv = alpha_deriv_cl * cdf;
115
116 results(y_deriv_cl, mu_deriv_cl, lambda_deriv_cl, alpha_deriv_cl)
121
123 partials<0>(ops_partials) = std::move(y_deriv_cl);
124 }
126 partials<1>(ops_partials) = std::move(mu_deriv_cl);
127 }
129 partials<2>(ops_partials) = std::move(lambda_deriv_cl);
130 }
132 partials<3>(ops_partials) = std::move(alpha_deriv_cl);
133 }
134 }
135 return ops_partials.build(cdf);
136}
137
138} // namespace math
139} // namespace stan
140#endif
141#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.
auto colwise_prod(T &&a)
Column wise product - reduction 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
expressions_cl< T_expressions... > expressions(T_expressions &&... expressions)
Deduces types for constructing expressions_cl object.
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
return_type_t< T_y_cl, T_loc_cl, T_scale_cl, T_shape_cl > pareto_type_2_cdf(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 cumulative density function.
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
value_type_t< T > prod(const T &m)
Calculates product of given kernel generator expression elements.
Definition prod.hpp:21
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
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
fvar< T > pow(const fvar< T > &x1, const fvar< T > &x2)
Definition pow.hpp:19
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...