Automatic Differentiation
 
Loading...
Searching...
No Matches
sd.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_REV_SD_HPP
2#define STAN_MATH_OPENCL_REV_SD_HPP
3#ifdef STAN_OPENCL
4
10
11namespace stan {
12namespace math {
13
22template <typename T,
23 require_all_kernel_expressions_and_none_scalar_t<T>* = nullptr>
24inline var sd(const var_value<T>& A) {
25 if (A.size() == 1) {
26 return 0.0;
27 }
28 double A_mean = mean(A.val());
30 matrix_cl<double> sq_norm;
31 auto diff_expr = A.val() - A_mean;
32 auto sq_norm_expr = sum_2d(square(diff));
33 results(diff, sq_norm) = expressions(diff_expr, sq_norm_expr);
34
35 return make_callback_var(
36 sqrt(from_matrix_cl(sq_norm).sum() / (A.size() - 1.0)),
37 [A, diff](vari& res) mutable {
38 A.adj() += res.adj() / (res.val() * (A.size() - 1.0)) * diff;
39 });
40}
41
42} // namespace math
43} // namespace stan
44
45#endif
46#endif
A variant of matrix_cl that schedules its destructor to be called, so it can be used on the AD stack.
Represents an arithmetic matrix on the OpenCL device.
Definition matrix_cl.hpp:47
auto sum_2d(T &&a)
Two dimensional sum - reduction of a kernel generator expression.
results_cl< T_results... > results(T_results &&... results)
Deduces types for constructing results_cl object.
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
scalar_type_t< T > mean(const T &m)
Returns the sample mean (i.e., average) of the coefficients in the specified std vector,...
Definition mean.hpp:20
var_value< plain_type_t< T > > make_callback_var(T &&value, F &&functor)
Creates a new var initialized with a callback_vari with a given value and reverse-pass callback funct...
fvar< T > sqrt(const fvar< T > &x)
Definition sqrt.hpp:18
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:23
double sd(const T &a)
Returns the unbiased sample standard deviation of the coefficients in the specified std vector,...
Definition sd.hpp:26
fvar< T > square(const fvar< T > &x)
Definition square.hpp:12
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...