Automatic Differentiation
 
Loading...
Searching...
No Matches
multi_normal_cholesky_lpdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_MULTI_NORMAL_CHOLESKY_LPDF_HPP
2#define STAN_MATH_OPENCL_PRIM_MULTI_NORMAL_CHOLESKY_LPDF_HPP
3#ifdef STAN_OPENCL
4
15
16namespace stan {
17namespace math {
18
39template <bool propto, typename T_y_cl, typename T_loc_cl, typename T_covar_cl,
41 T_covar_cl>* = nullptr>
43 const T_y_cl& y, const T_loc_cl& mu, const T_covar_cl& L) {
44 static constexpr const char* function = "multi_normal_cholesky_lpdf(OpenCL)";
45
46 check_consistent_sizes(function, "y", y, "mu", mu);
47 check_square(function, "covariance parameter", L);
48 check_size_match(function, "Size of random variable", y.rows(),
49 "rows of covariance parameter", L.rows());
50
51 if (max_size(y, mu, L) == 0) {
52 return 0.0;
53 }
55 return 0.0;
56 }
57
58 const auto& y_val = value_of(y);
59 const auto& mu_val = value_of(mu);
60 const auto& L_val_eval = eval(value_of(L));
61
62 int L_size = L_val_eval.rows();
63 int N_cases = std::max(y_val.cols(), mu_val.cols());
64
65 double logp = 0;
67 logp += NEG_LOG_SQRT_TWO_PI * L_size * N_cases;
68 }
69
70 matrix_cl<double> L_lower(L_val_eval.buffer(), L_val_eval.rows(),
71 L_val_eval.cols(), matrix_cl_view::Lower);
73
74 auto check_mu_finite
75 = check_cl(function, "Location parameter", mu_val, "finite");
76 auto mu_finite = isfinite(mu_val);
77 auto check_y_not_nan
78 = check_cl(function, "Random variable", y_val, "not nan");
79 auto y_not_nan = !isnan(y_val);
80
81 auto sum_log_diag_inv_L = colwise_sum(log(diagonal(inv_L)));
82 auto y_mu_diff
84
85 matrix_cl<double> y_mu_diff_cl;
86 matrix_cl<double> sum_log_diag_inv_L_cl;
87
88 if (y_val.cols() == 1 && mu_val.cols() == 1) {
89 results(check_mu_finite, check_y_not_nan, y_mu_diff_cl,
90 sum_log_diag_inv_L_cl)
91 = expressions(mu_finite, y_not_nan, y_mu_diff,
93 sum_log_diag_inv_L));
94 } else if (y_val.cols() == 1) {
95 results(check_y_not_nan, sum_log_diag_inv_L_cl) = expressions(
97 sum_log_diag_inv_L));
98 results(check_mu_finite, y_mu_diff_cl) = expressions(mu_finite, y_mu_diff);
99 } else if (mu_val.cols() == 1) {
100 results(check_mu_finite, sum_log_diag_inv_L_cl) = expressions(
102 sum_log_diag_inv_L));
103 results(check_y_not_nan, y_mu_diff_cl) = expressions(y_not_nan, y_mu_diff);
104 } else {
105 sum_log_diag_inv_L_cl = calc_if<include_summand<propto, T_covar_cl>::value>(
106 sum_log_diag_inv_L);
107 results(check_mu_finite, check_y_not_nan, y_mu_diff_cl)
108 = expressions(mu_finite, y_not_nan, y_mu_diff);
109 }
110
112 logp += sum(from_matrix_cl(sum_log_diag_inv_L_cl)) * N_cases;
113 }
114
115 matrix_cl<double> half = transpose(inv_L * y_mu_diff_cl);
116 matrix_cl<double> scaled_diff = transpose(half * inv_L);
117 logp -= 0.5 * dot_self(half);
118
119 auto ops_partials = make_partials_propagator(y, mu, L);
120
122 if (y_val.cols() == 1) {
123 partials<0>(ops_partials) = -rowwise_sum(scaled_diff);
124 } else {
125 partials<0>(ops_partials) = -scaled_diff;
126 }
127 }
129 if (mu_val.cols() == 1) {
130 partials<1>(ops_partials) = rowwise_sum(scaled_diff);
131 } else {
132 partials<1>(ops_partials) = scaled_diff;
133 }
134 }
136 partials<2>(ops_partials) = scaled_diff * half - N_cases * transpose(inv_L);
137 }
138
139 return ops_partials.build(logp);
140}
141
142} // namespace math
143} // namespace stan
144#endif
145#endif
Represents an arithmetic matrix on the OpenCL device.
Definition matrix_cl.hpp:47
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 transpose(Arg &&a)
Transposes a kernel generator expression.
auto rowwise_sum(T &&a)
Rowwise sum 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_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 diagonal(T &&a)
Diagonal of a kernel generator expression.
Definition diagonal.hpp:136
return_type_t< T_y_cl, T_loc_cl, T_covar_cl > multi_normal_cholesky_lpdf(const T_y_cl &y, const T_loc_cl &mu, const T_covar_cl &L)
The log of the multivariate normal density for the given y, mu, and a Cholesky factor L of the varian...
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
void check_square(const char *function, const char *name, const T_y &y)
Check if the specified matrix is square.
T eval(T &&arg)
Inputs which have a plain_type equal to the own time are forwarded unmodified (for Eigen expressions ...
Definition eval.hpp:20
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
const double NEG_LOG_SQRT_TWO_PI
The value of minus the natural logarithm of the square root of , .
auto rowwise_optional_broadcast(T &&a)
Broadcast an expression in rowwise dimmension if the number of columns equals to 1.
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
auto dot_self(const T &a)
Returns squared norm of a vector or matrix.
Definition dot_self.hpp:21
void check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Check if the provided sizes match.
Eigen::Matrix< value_type_t< T1 >, T1::RowsAtCompileTime, T2::ColsAtCompileTime > mdivide_left_tri_low(const T1 &A, const T2 &b)
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Definition fvar.hpp:9
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...