Automatic Differentiation
 
Loading...
Searching...
No Matches
lognormal_lpdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_LOGNORMAL_LPDF_HPP
2#define STAN_MATH_OPENCL_PRIM_LOGNORMAL_LPDF_HPP
3#ifdef STAN_OPENCL
4
14
15namespace stan {
16namespace math {
17
35template <
36 bool propto, typename T_y_cl, typename T_loc_cl, typename T_scale_cl,
38 T_scale_cl>* = nullptr,
39 require_any_not_stan_scalar_t<T_y_cl, T_loc_cl, T_scale_cl>* = nullptr>
41 const T_y_cl& y, const T_loc_cl& mu, const T_scale_cl& sigma) {
42 using std::isfinite;
43 static constexpr const char* function = "lognormal_lpdf(OpenCL)";
45
46 check_consistent_sizes(function, "Random variable", y, "Location parameter",
47 mu, "Scale parameter", sigma);
48 const size_t N = max_size(y, mu, sigma);
49 if (N == 0) {
50 return 0.0;
51 }
53 return 0.0;
54 }
55
56 const auto& y_col = as_column_vector_or_scalar(y);
57 const auto& mu_col = as_column_vector_or_scalar(mu);
58 const auto& sigma_col = as_column_vector_or_scalar(sigma);
59
60 const auto& y_val = value_of(y_col);
61 const auto& mu_val = value_of(mu_col);
62 const auto& sigma_val = value_of(sigma_col);
63
64 auto ops_partials = make_partials_propagator(y_col, mu_col, sigma_col);
65
66 auto check_y_nonnegative
67 = check_cl(function, "Random variable", y_val, "nonnegative");
68 auto y_nonnegative = 0 <= y_val;
69 auto check_mu_finite
70 = check_cl(function, "Location parameter", mu_val, "finite");
71 auto mu_finite = isfinite(mu_val);
72 auto check_sigma_pos_finite
73 = check_cl(function, "Scale parameter", sigma_val, "positive finite");
74 auto sigma_pos_finite = sigma_val > 0 && isfinite(sigma_val);
75
76 auto any_y_zero = colwise_max(cast<char>(y_val == 0.0));
77 auto inv_sigma = elt_divide(1.0, sigma_val);
78 auto inv_sigma_sq = elt_multiply(inv_sigma, inv_sigma);
79 auto log_y = log(y_val);
80 auto logy_m_mu = log_y - mu_val;
81 auto logy_m_mu_div_sigma = elt_multiply(logy_m_mu, inv_sigma_sq);
82
83 auto logp1
84 = -0.5 * elt_multiply(elt_multiply(logy_m_mu, logy_m_mu), inv_sigma_sq);
85 auto logp2 = static_select<include_summand<propto, T_scale_cl>::value>(
86 logp1 - log(sigma_val), logp1);
87 auto logp_expr
89 logp2 - log_y, logp2));
90
91 auto y_deriv_expr = elt_divide(-(1.0 + logy_m_mu_div_sigma), y_val);
92 auto sigma_deriv_expr = elt_multiply(
93 elt_multiply(logy_m_mu_div_sigma, logy_m_mu) - 1.0, inv_sigma);
94
95 matrix_cl<char> any_y_zero_cl;
96 matrix_cl<double> logp_cl;
97 matrix_cl<double> y_deriv_cl;
98 matrix_cl<double> mu_deriv_cl;
99 matrix_cl<double> sigma_deriv_cl;
100
101 results(check_y_nonnegative, check_mu_finite, check_sigma_pos_finite,
102 any_y_zero_cl, logp_cl, y_deriv_cl, mu_deriv_cl, sigma_deriv_cl)
103 = expressions(y_nonnegative, mu_finite, sigma_pos_finite, any_y_zero,
104 logp_expr,
105 calc_if<!is_constant<T_y_cl>::value>(y_deriv_expr),
106 calc_if<!is_constant<T_loc_cl>::value>(logy_m_mu_div_sigma),
107 calc_if<!is_constant<T_scale_cl>::value>(sigma_deriv_expr));
108
109 if (from_matrix_cl(any_y_zero_cl).any()) {
110 return LOG_ZERO;
111 }
112
113 T_partials_return logp
114 = sum(from_matrix_cl(logp_cl)) + N * NEG_LOG_SQRT_TWO_PI;
115
117 partials<0>(ops_partials) = std::move(y_deriv_cl);
118 }
120 partials<1>(ops_partials) = std::move(mu_deriv_cl);
121 }
123 partials<2>(ops_partials) = std::move(sigma_deriv_cl);
124 }
125
126 return ops_partials.build(logp);
127}
128
129} // namespace math
130} // namespace stan
131#endif
132#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_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_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_loc_cl, T_scale_cl > lognormal_lpdf(const T_y_cl &y, const T_loc_cl &mu, const T_scale_cl &sigma)
The log of the lognormal density for the specified scalar(s) given the specified sample stan::math::s...
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.
static constexpr double LOG_ZERO
The natural logarithm of 0, .
Definition constants.hpp:68
constexpr bool any(T x)
Return true if any values in the input are true.
Definition any.hpp:21
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 , .
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...