Automatic Differentiation
 
Loading...
Searching...
No Matches
log_mix.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_LOG_MIX_HPP
2#define STAN_MATH_OPENCL_PRIM_LOG_MIX_HPP
3#ifdef STAN_OPENCL
4
8
9namespace stan {
10namespace math {
11
36template <typename T_theta_cl, typename T_lambda_cl,
38 T_lambda_cl>* = nullptr>
39inline auto log_mix(const T_theta_cl& theta, const T_lambda_cl& lambda) {
40 static constexpr const char* function = "log_mix(OpenCL)";
42 using std::isfinite;
43 using std::isnan;
44
45 check_consistent_sizes(function, "theta", theta, "lambda", lambda);
46 const size_t N = max_size(theta, lambda);
47 if (N == 0) {
48 return T_return(0.0);
49 }
50
51 const auto& theta_col = as_column_vector_or_scalar(theta);
52
53 const auto& theta_val = value_of(theta_col);
54 const auto& lambda_val = value_of(lambda);
55
56 auto check_lambda_not_nan
57 = check_cl(function, "lambda", lambda_val, "not NaN");
58 auto lambda_not_nan = !isnan(lambda_val);
59 auto check_theta_bounded
60 = check_cl(function, "theta", theta_val, "in the interval[0, 1]");
61 auto theta_bounded = 0.0 <= theta_val && theta_val <= 1.0;
62
63 auto theta_bc = rowwise_broadcast(theta_val);
64 auto lambda_p_log_theta_expr = lambda_val + log(theta_bc);
65 matrix_cl<double> lambda_p_log_theta;
66 matrix_cl<double> lambda_p_log_theta_colwise_max;
67 if (theta.cols() == lambda.cols()) {
68 results(check_lambda_not_nan, check_theta_bounded, lambda_p_log_theta,
69 lambda_p_log_theta_colwise_max)
70 = expressions(lambda_not_nan, theta_bounded, lambda_p_log_theta_expr,
71 colwise_max(lambda_p_log_theta_expr));
72 } else {
73 results(check_lambda_not_nan, lambda_p_log_theta,
74 lambda_p_log_theta_colwise_max)
75 = expressions(lambda_not_nan, lambda_p_log_theta_expr,
76 colwise_max(lambda_p_log_theta_expr));
77 check_theta_bounded = theta_bounded;
78 }
79 while (lambda_p_log_theta_colwise_max.rows() > 1) {
80 lambda_p_log_theta_colwise_max
81 = colwise_max(lambda_p_log_theta_colwise_max).eval();
82 }
84 lambda_p_log_theta - colwise_broadcast(lambda_p_log_theta_colwise_max)));
85 while (sum_exp.rows() > 1) {
86 sum_exp = colwise_sum(sum_exp).eval();
87 }
88
89 auto logp_vec_expr = transpose(lambda_p_log_theta_colwise_max + log(sum_exp));
90 matrix_cl<double> logp_vec;
91 matrix_cl<double> logp_sum;
92 results(logp_vec, logp_sum) = expressions(
94 colwise_sum(logp_vec_expr));
95
96 auto ops_partials = make_partials_propagator(theta_col, lambda);
98 auto derivs_expr = exp(lambda_val - colwise_broadcast(transpose(logp_vec)));
100 auto lambda_deriv_expr = elt_multiply(derivs_expr, theta_bc);
101 matrix_cl<double> derivs;
102 matrix_cl<double> lambda_deriv;
103 results(derivs, lambda_deriv)
105 lambda_deriv_expr);
106
107 partials<1>(ops_partials) = std::move(lambda_deriv);
109 partials<0>(ops_partials) = rowwise_sum(derivs);
110 }
111 } else if (!is_constant<T_theta_cl>::value) {
112 partials<0>(ops_partials) = rowwise_sum(derivs_expr);
113 }
114 }
115 return ops_partials.build(sum(from_matrix_cl(logp_sum)));
116}
117} // namespace math
118} // namespace stan
119#endif
120#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)
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 transpose(Arg &&a)
Transposes a kernel generator expression.
auto rowwise_broadcast(T &&a)
Broadcast an expression in rowwise dimmension.
auto rowwise_sum(T &&a)
Rowwise sum reduction of a kernel generator expression.
auto colwise_max(T &&a)
Column wise max - reduction of a kernel generator expression.
calc_if_< true, as_operation_cl_t< T > > calc_if(T &&a)
Definition calc_if.hpp:121
auto colwise_broadcast(T &&a)
Broadcast an expression in colwise dimmension.
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
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.
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
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 > log_mix(const fvar< T > &theta, const fvar< T > &lambda1, const fvar< T > &lambda2)
Return the log mixture density with specified mixing proportion and log densities and its derivative ...
Definition log_mix.hpp:98
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:13
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...