Automatic Differentiation
 
Loading...
Searching...
No Matches
exp_mod_normal_lpdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_EXP_MOD_NORMAL_LPDF_HPP
2#define STAN_MATH_OPENCL_PRIM_EXP_MOD_NORMAL_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_inv_scale_cl,
36 T_y_cl, T_loc_cl, T_scale_cl, T_inv_scale_cl>* = nullptr,
37 require_any_not_stan_scalar_t<T_y_cl, T_loc_cl, T_scale_cl,
38 T_inv_scale_cl>* = nullptr>
40 const T_y_cl& y, const T_loc_cl& mu, const T_scale_cl& sigma,
41 const T_inv_scale_cl& lambda) {
42 static constexpr const char* function = "exp_mod_normal_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", sigma, "Inv_scale paramter",
50 lambda);
51 const size_t N = max_size(y, mu, sigma, lambda);
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_inv_scale_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& sigma_col = as_column_vector_or_scalar(sigma);
63 const auto& lambda_col = as_column_vector_or_scalar(lambda);
64
65 const auto& y_val = value_of(y_col);
66 const auto& mu_val = value_of(mu_col);
67 const auto& sigma_val = value_of(sigma_col);
68 const auto& lambda_val = value_of(lambda_col);
69
70 auto check_y_not_nan
71 = check_cl(function, "Random variable", y_val, "not_nan");
72 auto y_not_nan_expr = !isnan(y_val);
73 auto check_mu_finite
74 = check_cl(function, "Location parameter", mu_val, "finite");
75 auto mu_finite_expr = isfinite(mu_val);
76 auto check_sigma_positive_finite
77 = check_cl(function, "Scale parameter", sigma_val, "positive finite");
78 auto sigma_positive_finite_expr = isfinite(sigma_val) && sigma_val > 0;
79 auto check_lambda_positive_finite = check_cl(function, "Inv_scale parameter",
80 lambda_val, "positive finite");
81 auto lambda_positive_finite_expr = isfinite(lambda_val) && lambda_val > 0;
82
83 auto inv_sigma_expr = elt_divide(1.0, sigma_val);
84 auto sigma_sq_expr = elt_multiply(sigma_val, sigma_val);
85 auto lambda_sigma_sq_expr = elt_multiply(lambda_val, sigma_sq_expr);
86 auto mu_minus_y_expr = mu_val - y_val;
87 auto inner_term_expr = elt_multiply(mu_minus_y_expr + lambda_sigma_sq_expr,
88 INV_SQRT_TWO * inv_sigma_expr);
89 auto erfc_calc_expr = erfc(inner_term_expr);
90 auto logp1_expr
91 = elt_multiply(lambda_val, mu_minus_y_expr + 0.5 * lambda_sigma_sq_expr)
92 + log(erfc_calc_expr);
93 auto logp_expr = colwise_sum(
95 logp1_expr + log(lambda_val), logp1_expr));
96
97 auto deriv_logerfc_expr
99 * exp(-elt_multiply(inner_term_expr, inner_term_expr)),
100 erfc_calc_expr);
101 auto deriv_expr
102 = lambda_val + elt_multiply(deriv_logerfc_expr, inv_sigma_expr);
103 auto deriv_sigma_expr
104 = elt_multiply(sigma_val, elt_multiply(lambda_val, lambda_val))
105 + elt_multiply(
106 deriv_logerfc_expr,
107 (lambda_val - elt_divide(mu_minus_y_expr, sigma_sq_expr)));
108 auto deriv_lambda_expr = elt_divide(1.0, lambda_val) + lambda_sigma_sq_expr
109 + mu_minus_y_expr
110 + elt_multiply(deriv_logerfc_expr, sigma_val);
111
112 matrix_cl<double> logp_cl;
113 matrix_cl<double> y_deriv_cl;
114 matrix_cl<double> mu_deriv_cl;
115 matrix_cl<double> sigma_deriv_cl;
116 matrix_cl<double> lambda_deriv_cl;
117
118 results(check_y_not_nan, check_mu_finite, check_sigma_positive_finite,
119 check_lambda_positive_finite, logp_cl, y_deriv_cl, mu_deriv_cl,
120 sigma_deriv_cl, lambda_deriv_cl)
121 = expressions(
122 y_not_nan_expr, mu_finite_expr, sigma_positive_finite_expr,
123 lambda_positive_finite_expr, logp_expr,
124 calc_if<!is_constant<T_y_cl>::value>(-deriv_expr),
126 calc_if<!is_constant<T_scale_cl>::value>(deriv_sigma_expr),
127 calc_if<!is_constant<T_inv_scale_cl>::value>(deriv_lambda_expr));
128
129 T_partials_return logp = sum(from_matrix_cl(logp_cl));
131 logp -= LOG_TWO * N;
132 }
133
134 auto ops_partials
135 = make_partials_propagator(y_col, mu_col, sigma_col, lambda_col);
137 partials<0>(ops_partials) = std::move(y_deriv_cl);
138 }
140 partials<1>(ops_partials) = std::move(mu_deriv_cl);
141 }
143 partials<2>(ops_partials) = std::move(sigma_deriv_cl);
144 }
146 partials<3>(ops_partials) = std::move(lambda_deriv_cl);
147 }
148 return ops_partials.build(logp);
149}
150
151} // namespace math
152} // namespace stan
153
154#endif
155#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.
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_inv_scale_cl > exp_mod_normal_lpdf(const T_y_cl &y, const T_loc_cl &mu, const T_scale_cl &sigma, const T_inv_scale_cl &lambda)
Returns the log PMF of the exp mod normal distribution.
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.
static constexpr double SQRT_TWO_OVER_SQRT_PI
The square root of 2 divided by the square root of , .
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
static constexpr double INV_SQRT_TWO
The value of 1 over the square root of 2, .
static constexpr double LOG_TWO
The natural logarithm of 2, .
Definition constants.hpp:80
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 > erfc(const fvar< T > &x)
Definition erfc.hpp:15
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.
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:13
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 ...
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...