Automatic Differentiation
 
Loading...
Searching...
No Matches
variance.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUN_VARIANCE_HPP
2#define STAN_MATH_REV_FUN_VARIANCE_HPP
3
10#include <vector>
11
12namespace stan {
13namespace math {
14namespace internal {
15
16inline var calc_variance(size_t size, const var* dtrs) {
18 double* partials
20
21 Eigen::Map<const vector_v> dtrs_map(dtrs, size);
22 Eigen::Map<vector_vi>(varis, size) = dtrs_map.vi();
23 vector_d dtrs_vals = dtrs_map.val();
24
25 vector_d diff = dtrs_vals.array() - dtrs_vals.mean();
26 double size_m1 = size - 1;
27 Eigen::Map<vector_d>(partials, size) = 2 * diff.array() / size_m1;
28 double variance = diff.squaredNorm() / size_m1;
29
30 return {new stored_gradient_vari(variance, size, varis, partials)};
31}
32
33} // namespace internal
34
42inline var variance(const std::vector<var>& v) {
43 check_nonzero_size("variance", "v", v);
44 if (v.size() == 1) {
45 return var{0.0};
46 }
47 return {internal::calc_variance(v.size(), &v[0])};
48}
49
60template <typename EigMat, require_eigen_vt<is_var, EigMat>* = nullptr>
61var variance(const EigMat& m) {
62 const auto& mat = to_ref(m);
63 check_nonzero_size("variance", "m", mat);
64 if (mat.size() == 1) {
65 return var{0.0};
66 }
67 return {internal::calc_variance(mat.size(), mat.data())};
68}
69
78template <typename Mat, require_var_matrix_t<Mat>* = nullptr>
79inline var variance(const Mat& x) {
80 check_nonzero_size("variance", "x", x);
81
82 if (x.size() == 1) {
83 return 0.0;
84 }
85
86 double mean = x.val().mean();
87 arena_t<promote_scalar_t<double, Mat>> arena_diff(x.rows(), x.cols());
88
89 double squaredNorm = 0.0;
90 for (Eigen::Index i = 0; i < arena_diff.size(); ++i) {
91 double diff = x.val().coeff(i) - mean;
92 arena_diff.coeffRef(i) = diff;
93 squaredNorm += diff * diff;
94 }
95
96 var res = squaredNorm / (x.size() - 1);
97
98 reverse_pass_callback([x, res, arena_diff]() mutable {
99 x.adj() += (2.0 * res.adj() / (x.size() - 1)) * arena_diff;
100 });
101
102 return res;
103}
104
105} // namespace math
106} // namespace stan
107#endif
T * alloc_array(size_t n)
Allocate an array on the arena of the specified size to hold values of the specified template paramet...
A var implementation that stores the daughter variable implementation pointers and the partial deriva...
int64_t size(const T &m)
Returns the size (number of the elements) of a matrix_cl or var_value<matrix_cl<T>>.
Definition size.hpp:19
var calc_variance(size_t size, const var *dtrs)
Definition variance.hpp:16
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
Eigen::Matrix< double, Eigen::Dynamic, 1 > vector_d
Type for (column) vector of double values.
Definition typedefs.hpp:24
void reverse_pass_callback(F &&functor)
Puts a callback on the autodiff stack to be called in reverse pass.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
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.
constexpr auto & partials(internal::partials_propagator< Types... > &x) noexcept
Access the partials for an edge of an partials_propagator
typename internal::arena_type_impl< std::decay_t< T > >::type arena_t
Determines a type that can be used in place of T that does any dynamic allocations on the AD stack.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
static thread_local AutodiffStackStorage * instance_