Automatic Differentiation
 
Loading...
Searching...
No Matches
frechet_lpdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_FRECHET_LPDF_HPP
2#define STAN_MATH_OPENCL_PRIM_FRECHET_LPDF_HPP
3#ifdef STAN_OPENCL
4
13
14namespace stan {
15namespace math {
16
34template <
35 bool propto, typename T_y_cl, typename T_shape_cl, typename T_scale_cl,
37 T_scale_cl>* = nullptr,
38 require_any_not_stan_scalar_t<T_y_cl, T_shape_cl, T_scale_cl>* = nullptr>
40 const T_y_cl& y, const T_shape_cl& alpha, const T_scale_cl& sigma) {
41 using std::isfinite;
42 static constexpr const char* function = "frechet_lpdf(OpenCL)";
44
45 check_consistent_sizes(function, "Random variable", y, "Shape parameter",
46 alpha, "Scale parameter", sigma);
47 const size_t N = max_size(y, alpha, sigma);
48 if (N == 0) {
49 return 0.0;
50 }
51 if constexpr (!include_summand<propto, T_y_cl, T_shape_cl,
52 T_scale_cl>::value) {
53 return 0.0;
54 }
55
56 const auto& y_col = as_column_vector_or_scalar(y);
57 const auto& alpha_col = as_column_vector_or_scalar(alpha);
58 const auto& sigma_col = as_column_vector_or_scalar(sigma);
59
60 const auto& y_val = value_of(y_col);
61 const auto& alpha_val = value_of(alpha_col);
62 const auto& sigma_val = value_of(sigma_col);
63
64 auto ops_partials = make_partials_propagator(y_col, alpha_col, sigma_col);
65
66 auto check_y_positive
67 = check_cl(function, "Random variable", y_val, "positive");
68 auto y_positive_expr = 0 < y_val;
69 auto check_alpha_pos_finite
70 = check_cl(function, "Shape parameter", alpha_val, "positive finite");
71 auto alpha_pos_finite_expr = alpha_val > 0 && isfinite(alpha_val);
72 auto check_sigma_pos_finite
73 = check_cl(function, "Scale parameter", sigma_val, "positive finite");
74 auto sigma_pos_finite_expr = sigma_val > 0 && isfinite(sigma_val);
75
76 auto log_y_expr = log(y_val);
77 auto sigma_div_y_pow_alpha_expr
78 = pow(elt_divide(sigma_val, y_val), alpha_val);
79 auto log_sigma_expr = log(sigma_val);
80 auto logp1_expr = -sigma_div_y_pow_alpha_expr;
81 auto logp2_expr = static_select<include_summand<propto, T_shape_cl>::value>(
82 logp1_expr + log(alpha_val), logp1_expr);
83 auto logp3_expr
84 = static_select<include_summand<propto, T_y_cl, T_shape_cl>::value>(
85 logp2_expr - elt_multiply(alpha_val + 1.0, log_y_expr), logp2_expr);
86 auto logp_expr = colwise_sum(
88 logp3_expr + elt_multiply(alpha_val, log_sigma_expr), logp3_expr));
89
90 auto alpha_deriv_expr = elt_divide(1.0, alpha_val)
91 + elt_multiply(1 - sigma_div_y_pow_alpha_expr,
92 log_sigma_expr - log_y_expr);
93 auto y_deriv_expr = elt_divide(
94 elt_multiply(alpha_val, sigma_div_y_pow_alpha_expr) - alpha_val - 1,
95 y_val);
96 auto sigma_deriv_expr = elt_multiply(elt_divide(alpha_val, sigma_val),
97 1 - sigma_div_y_pow_alpha_expr);
98
99 matrix_cl<double> logp_cl;
100 matrix_cl<double> y_deriv_cl;
101 matrix_cl<double> alpha_deriv_cl;
102 matrix_cl<double> sigma_deriv_cl;
103
104 results(check_y_positive, check_alpha_pos_finite, check_sigma_pos_finite,
105 logp_cl, y_deriv_cl, alpha_deriv_cl, sigma_deriv_cl)
106 = expressions(y_positive_expr, alpha_pos_finite_expr,
107 sigma_pos_finite_expr, logp_expr, y_deriv_expr,
108 alpha_deriv_expr, sigma_deriv_expr);
109
110 T_partials_return logp = sum(from_matrix_cl(logp_cl));
111
112 if constexpr (is_autodiff_v<T_y_cl>) {
113 partials<0>(ops_partials) = std::move(y_deriv_cl);
114 }
115 if constexpr (is_autodiff_v<T_shape_cl>) {
116 partials<1>(ops_partials) = std::move(alpha_deriv_cl);
117 }
118 if constexpr (is_autodiff_v<T_scale_cl>) {
119 partials<2>(ops_partials) = std::move(sigma_deriv_cl);
120 }
121
122 return ops_partials.build(logp);
123}
124
125} // namespace math
126} // namespace stan
127#endif
128#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 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_shape_cl, T_scale_cl > frechet_lpdf(const T_y_cl &y, const T_shape_cl &alpha, const T_scale_cl &sigma)
The log of the frechet density for the specified scalar(s) given the specified sample stan::math::siz...
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.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
auto pow(const T1 &x1, const T2 &x2)
Definition pow.hpp:32
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
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.
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:23
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:20
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 ...
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...