1#ifndef STAN_MATH_REV_FUN_VARIANCE_HPP
2#define STAN_MATH_REV_FUN_VARIANCE_HPP
22 Eigen::Map<const vector_v> dtrs_map(dtrs,
size);
23 Eigen::Map<vector_vi>(varis,
size) = dtrs_map.vi();
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;
61template <
typename EigMat, require_eigen_vt<is_var, EigMat>* =
nullptr>
63 const auto& mat =
to_ref(m);
65 if (mat.size() == 1) {
79template <
typename Mat, require_var_matrix_t<Mat>* =
nullptr>
87 double mean = x.val().mean();
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;
97 var res = squaredNorm / (x.size() - 1);
100 x.adj() += (2.0 * res.adj() / (x.size() - 1)) * arena_diff;
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>>.
var calc_variance(size_t size, const var *dtrs)
scalar_type_t< T > mean(const T &m)
Returns the sample mean (i.e., average) of the coefficients in the specified std vector,...
double variance(const T &a)
Return the sample variance of the var_value matrix Raise domain error if size is not greater than zer...
Eigen::Matrix< double, Eigen::Dynamic, 1 > vector_d
Type for (column) vector of double values.
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.
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_