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 }
52 return 0.0;
53 }
54
55 const auto& y_col = as_column_vector_or_scalar(y);
56 const auto& alpha_col = as_column_vector_or_scalar(alpha);
57 const auto& sigma_col = as_column_vector_or_scalar(sigma);
58
59 const auto& y_val = value_of(y_col);
60 const auto& alpha_val = value_of(alpha_col);
61 const auto& sigma_val = value_of(sigma_col);
62
63 auto ops_partials = make_partials_propagator(y_col, alpha_col, sigma_col);
64
65 auto check_y_positive
66 = check_cl(function, "Random variable", y_val, "positive");
67 auto y_positive_expr = 0 < y_val;
68 auto check_alpha_pos_finite
69 = check_cl(function, "Shape parameter", alpha_val, "positive finite");
70 auto alpha_pos_finite_expr = alpha_val > 0 && isfinite(alpha_val);
71 auto check_sigma_pos_finite
72 = check_cl(function, "Scale parameter", sigma_val, "positive finite");
73 auto sigma_pos_finite_expr = sigma_val > 0 && isfinite(sigma_val);
74
75 auto log_y_expr = log(y_val);
76 auto sigma_div_y_pow_alpha_expr
77 = pow(elt_divide(sigma_val, y_val), alpha_val);
78 auto log_sigma_expr = log(sigma_val);
79 auto logp1_expr = -sigma_div_y_pow_alpha_expr;
80 auto logp2_expr = static_select<include_summand<propto, T_shape_cl>::value>(
81 logp1_expr + log(alpha_val), logp1_expr);
82 auto logp3_expr
83 = static_select<include_summand<propto, T_y_cl, T_shape_cl>::value>(
84 logp2_expr - elt_multiply(alpha_val + 1.0, log_y_expr), logp2_expr);
85 auto logp_expr = colwise_sum(
87 logp3_expr + elt_multiply(alpha_val, log_sigma_expr), logp3_expr));
88
89 auto alpha_deriv_expr = elt_divide(1.0, alpha_val)
90 + elt_multiply(1 - sigma_div_y_pow_alpha_expr,
91 log_sigma_expr - log_y_expr);
92 auto y_deriv_expr = elt_divide(
93 elt_multiply(alpha_val, sigma_div_y_pow_alpha_expr) - alpha_val - 1,
94 y_val);
95 auto sigma_deriv_expr = elt_multiply(elt_divide(alpha_val, sigma_val),
96 1 - sigma_div_y_pow_alpha_expr);
97
98 matrix_cl<double> logp_cl;
99 matrix_cl<double> y_deriv_cl;
100 matrix_cl<double> alpha_deriv_cl;
101 matrix_cl<double> sigma_deriv_cl;
102
103 results(check_y_positive, check_alpha_pos_finite, check_sigma_pos_finite,
104 logp_cl, y_deriv_cl, alpha_deriv_cl, sigma_deriv_cl)
105 = expressions(y_positive_expr, alpha_pos_finite_expr,
106 sigma_pos_finite_expr, logp_expr, y_deriv_expr,
107 alpha_deriv_expr, sigma_deriv_expr);
108
109 T_partials_return logp = sum(from_matrix_cl(logp_cl));
110
112 partials<0>(ops_partials) = std::move(y_deriv_cl);
113 }
115 partials<1>(ops_partials) = std::move(alpha_deriv_cl);
116 }
118 partials<2>(ops_partials) = std::move(sigma_deriv_cl);
119 }
120
121 return ops_partials.build(logp);
122}
123
124} // namespace math
125} // namespace stan
126#endif
127#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: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.
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 ...
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...