Automatic Differentiation
 
Loading...
Searching...
No Matches
pareto_type_2_lpdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_PARETO_TYPE_2_LPDF_HPP
2#define STAN_MATH_OPENCL_PRIM_PARETO_TYPE_2_LPDF_HPP
3#ifdef STAN_OPENCL
4
12
13namespace stan {
14namespace math {
15
33template <bool propto, typename T_y_cl, typename T_loc_cl, typename T_scale_cl,
34 typename T_shape_cl,
36 T_y_cl, T_loc_cl, T_scale_cl, T_shape_cl>* = nullptr,
37 require_any_not_stan_scalar_t<T_y_cl, T_loc_cl, T_scale_cl,
38 T_shape_cl>* = nullptr>
40 const T_y_cl& y, const T_loc_cl& mu, const T_scale_cl& lambda,
41 const T_shape_cl& alpha) {
42 static constexpr const char* function = "pareto_type_2_lpdf(OpenCL)";
43 using T_partials_return
45 using std::isfinite;
46 using std::isnan;
47
48 check_consistent_sizes(function, "Random variable", y, "Location parameter",
49 mu, "Scale parameter", alpha, "Shape parameter",
50 alpha);
51 const size_t N = max_size(y, mu, lambda, alpha);
52 if (N == 0) {
53 return 0.0;
54 }
55 if (!include_summand<propto, T_y_cl, T_loc_cl, T_scale_cl,
56 T_shape_cl>::value) {
57 return 0.0;
58 }
59
60 const auto& y_col = as_column_vector_or_scalar(y);
61 const auto& mu_col = as_column_vector_or_scalar(mu);
62 const auto& lambda_col = as_column_vector_or_scalar(lambda);
63 const auto& alpha_col = as_column_vector_or_scalar(alpha);
64
65 const auto& y_val = value_of(y_col);
66 const auto& mu_val = value_of(mu_col);
67 const auto& lambda_val = value_of(lambda_col);
68 const auto& alpha_val = value_of(alpha_col);
69
70 auto y_minus_mu = y_val - mu_val;
71 auto check_y_ge_mu
72 = check_cl(function, "Random variable minus location parameter",
73 y_minus_mu, "greater or equal than zero");
74 auto y_ge_mu = y_minus_mu >= 0;
75 auto check_lambda_positive_finite
76 = check_cl(function, "Scale parameter", lambda_val, "positive finite");
77 auto lambda_positive_finite = isfinite(lambda_val) && lambda_val > 0;
78 auto check_alpha_positive_finite
79 = check_cl(function, "Shape parameter", alpha_val, "positive finite");
80 auto alpha_positive_finite = isfinite(alpha_val) && alpha_val > 0;
81
82 auto log1p_scaled_diff = log1p(elt_divide(y_minus_mu, lambda_val));
83
84 auto logp1 = static_select<include_summand<propto, T_shape_cl>::value>(
85 log(alpha_val), constant(0, N, 1));
86 auto logp2 = static_select<include_summand<propto, T_scale_cl>::value>(
87 logp1 - log(lambda_val), logp1);
88 auto logp_expr = colwise_sum(
89 static_select<include_summand<propto, T_y_cl, T_loc_cl, T_scale_cl,
90 T_shape_cl>::value>(
91 logp2 - elt_multiply(alpha_val + 1.0, log1p_scaled_diff), logp2));
92
93 auto inv_sum = elt_divide(1.0, lambda_val + y_minus_mu);
94 auto alpha_div_sum = elt_multiply(alpha_val, inv_sum);
95
96 auto deriv_y_mu = inv_sum + alpha_div_sum;
97 auto deriv_lambda
98 = elt_divide(elt_multiply(alpha_div_sum, y_minus_mu), lambda_val)
99 - inv_sum;
100 auto deriv_alpha = elt_divide(1.0, alpha_val) - log1p_scaled_diff;
101
102 matrix_cl<double> logp_cl;
103 matrix_cl<double> y_deriv_cl;
104 matrix_cl<double> mu_deriv_cl;
105 matrix_cl<double> lambda_deriv_cl;
106 matrix_cl<double> alpha_deriv_cl;
107
108 results(check_y_ge_mu, check_lambda_positive_finite,
109 check_alpha_positive_finite, logp_cl, y_deriv_cl, mu_deriv_cl,
110 lambda_deriv_cl, alpha_deriv_cl)
111 = expressions(y_ge_mu, lambda_positive_finite, alpha_positive_finite,
112 logp_expr,
113 calc_if<!is_constant<T_y_cl>::value>(-deriv_y_mu),
117
118 T_partials_return logp = sum(from_matrix_cl(logp_cl));
119
120 auto ops_partials
121 = make_partials_propagator(y_col, mu_col, lambda_col, alpha_col);
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 return ops_partials.build(logp);
135}
136
137} // namespace math
138} // namespace stan
139
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.
elt_divide_< as_operation_cl_t< T_a >, as_operation_cl_t< T_b > > elt_divide(T_a &&a, T_b &&b)
auto constant(const T a, int rows, int cols)
Matrix of repeated values in kernel generator expressions.
Definition constant.hpp:130
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_lpdf(const T_y_cl &y, const T_loc_cl &mu, const T_scale_cl &lambda, const T_shape_cl &alpha)
Returns the log PMF of the Pareto type 2 distribution.
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
fvar< T > log(const fvar< T > &x)
Definition log.hpp:15
T1 static_select(T1 &&a, T2 &&b)
Returns one of the arguments that can be of different type, depending on the compile time condition.
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:22
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 ...
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...