Automatic Differentiation
 
Loading...
Searching...
No Matches
logistic_lpdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_LOGISTIC_LPDF_HPP
2#define STAN_MATH_OPENCL_PRIM_LOGISTIC_LPDF_HPP
3#ifdef STAN_OPENCL
4
15
16namespace stan {
17namespace math {
18
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 = "logistic_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_finite = check_cl(function, "Random variable", y_val, "finite");
67 auto y_finite = isfinite(y_val);
68 auto check_mu_finite
69 = check_cl(function, "Location parameter", mu_val, "finite");
70 auto mu_finite = isfinite(mu_val);
71 auto check_sigma_pos_finite
72 = check_cl(function, "Scale parameter", sigma_val, "positive finite");
73 auto sigma_pos_finite = sigma_val > 0.0 && isfinite(sigma_val);
74
75 auto inv_sigma = elt_divide(1.0, sigma_val);
76 auto y_minus_mu = y_val - mu_val;
77 auto y_minus_mu_div_sigma = elt_multiply(y_minus_mu, inv_sigma);
78
79 auto logp1 = -y_minus_mu_div_sigma - 2.0 * log1p_exp(-y_minus_mu_div_sigma);
80 auto logp_expr
82 logp1 - log(sigma_val), logp1));
83
84 auto y_deriv = elt_multiply(
85 elt_divide(2.0, 1.0 + exp(y_minus_mu_div_sigma)) - 1.0, inv_sigma);
86 auto exp_mu_div_sigma = exp(elt_multiply(mu_val, inv_sigma));
87 auto mu_deriv = elt_multiply(
88 1.0
89 - 2.0
90 * elt_divide(
91 exp_mu_div_sigma,
92 exp_mu_div_sigma + exp(elt_multiply(y_val, inv_sigma))),
93 inv_sigma);
94 auto sigma_deriv
95 = elt_multiply(-elt_multiply(y_deriv, y_minus_mu) - 1.0, inv_sigma);
96
97 matrix_cl<double> logp_cl;
98 matrix_cl<double> y_deriv_cl;
99 matrix_cl<double> mu_deriv_cl;
100 matrix_cl<double> sigma_deriv_cl;
101
102 results(check_mu_finite, check_sigma_pos_finite, check_y_finite, logp_cl,
103 y_deriv_cl, mu_deriv_cl, sigma_deriv_cl)
104 = expressions(mu_finite, sigma_pos_finite, y_finite, logp_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(mu_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)
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 > logistic_lpdf(const T_y_cl &y, const T_loc_cl &mu, const T_scale_cl &sigma)
The log of a logistic density for y with the specified location and scale parameters.
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
T1 static_select(T1 &&a, T2 &&b)
Returns one of the arguments that can be of different type, depending on the compile time condition.
fvar< T > log1p_exp(const fvar< T > &x)
Definition log1p_exp.hpp:13
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 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 ...
Definition fvar.hpp:9
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...