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
11#include <vector>
12
13namespace stan {
14namespace math {
15namespace internal {
16
17inline var calc_variance(size_t size, const var* dtrs) {
19 double* partials
21
22 Eigen::Map<const vector_v> dtrs_map(dtrs, size);
23 Eigen::Map<vector_vi>(varis, size) = dtrs_map.vi();
24 vector_d dtrs_vals = dtrs_map.val();
25
26 vector_d diff = dtrs_vals.array() - dtrs_vals.mean();
27 double size_m1 = size - 1;
28 Eigen::Map<vector_d>(partials, size) = 2 * diff.array() / size_m1;
29 double variance = diff.squaredNorm() / size_m1;
30
31 return {new stored_gradient_vari(variance, size, varis, partials)};
32}
33
34} // namespace internal
35
43inline var variance(const std::vector<var>& v) {
44 check_nonzero_size("variance", "v", v);
45 if (v.size() == 1) {
46 return var{0.0};
47 }
48 return {internal::calc_variance(v.size(), &v[0])};
49}
50
61template <typename EigMat, require_eigen_vt<is_var, EigMat>* = nullptr>
62var variance(const EigMat& m) {
63 const auto& mat = to_ref(m);
64 check_nonzero_size("variance", "m", mat);
65 if (mat.size() == 1) {
66 return var{0.0};
67 }
68 return {internal::calc_variance(mat.size(), mat.data())};
69}
70
79template <typename Mat, require_var_matrix_t<Mat>* = nullptr>
80inline var variance(const Mat& x) {
81 check_nonzero_size("variance", "x", x);
82
83 if (x.size() == 1) {
84 return 0.0;
85 }
86
87 double mean = x.val().mean();
88 arena_t<promote_scalar_t<double, Mat>> arena_diff(x.rows(), x.cols());
89
90 double squaredNorm = 0.0;
91 for (Eigen::Index i = 0; i < arena_diff.size(); ++i) {
92 double diff = x.val().coeff(i) - mean;
93 arena_diff.coeffRef(i) = diff;
94 squaredNorm += diff * diff;
95 }
96
97 var res = squaredNorm / (x.size() - 1);
98
99 reverse_pass_callback([x, res, arena_diff]() mutable {
100 x.adj() += (2.0 * res.adj() / (x.size() - 1)) * arena_diff;
101 });
102
103 return res;
104}
105
106} // namespace math
107} // namespace stan
108#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:17
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_