Automatic Differentiation
 
Loading...
Searching...
No Matches
variance.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_VARIANCE_HPP
2#define STAN_MATH_OPENCL_PRIM_VARIANCE_HPP
3#ifdef STAN_OPENCL
11
12namespace stan {
13namespace math {
14
23template <typename T,
24 require_all_kernel_expressions_and_none_scalar_t<T>* = nullptr>
25inline double variance(const T& a) {
26 check_nonzero_size("variance (OpenCL)", "a", a);
27 if (a.size() == 1) {
28 return 0.0;
29 }
31 return sum(square(a - mean(a))) / (a.size() - 1);
32 } else {
33 matrix_cl<double> a_eval;
35 results(a_eval, a_sum) = expressions(a, sum_2d(a));
36 return sum(square(a_eval - from_matrix_cl(a_sum).sum() / a.size()))
37 / (a.size() - 1);
38 }
39}
40
41} // namespace math
42} // namespace stan
43
44#endif
45#endif
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
double variance(const T &a)
Return the sample variance of the var_value matrix Raise domain error if size is not greater than zer...
Definition variance.hpp:25
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:22
void check_nonzero_size(const char *function, const char *name, const T_y &y)
Check if the specified matrix/vector is of non-zero size.
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 ...
Definition fvar.hpp:9