1#ifndef STAN_MATH_REV_FUN_SD_HPP
2#define STAN_MATH_REV_FUN_SD_HPP
27template <
typename T, require_eigen_st<is_var, T>* =
nullptr>
43 Eigen::Map<T_vi> varis_map(varis, x.rows(), x.cols());
44 Eigen::Map<T_d> partials_map(
partials, x.rows(), x.cols());
47 T_d dtrs_val = x.val();
48 double mean = dtrs_val.mean();
49 T_d diff = dtrs_val.array() -
mean;
50 double sum_of_squares = diff.squaredNorm();
51 double size_m1 = x.size() - 1;
52 double sd =
sqrt(sum_of_squares / size_m1);
54 if (sum_of_squares < 1
e-20) {
55 partials_map.fill(
inv_sqrt(
static_cast<double>(x.size())));
57 partials_map = diff.array() / (
sd * size_m1);
70template <
typename T, require_var_matrix_t<T>* =
nullptr>
78 auto arena_diff =
to_arena((x.val().array() - x.val().mean()).matrix());
79 double sum_of_squares = arena_diff.squaredNorm();
80 double sd = std::sqrt(sum_of_squares / (x.size() - 1));
83 x.adj() += (res.adj() / (res.val() * (x.size() - 1))) * arena_diff;
96template <
typename T, require_std_vector_st<is_var, T>* =
nullptr>
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...
scalar_type_t< T > mean(const T &m)
Returns the sample mean (i.e., average) of the coefficients in the specified std vector,...
static constexpr double e()
Return the base of the natural logarithm.
typename promote_scalar_type< std::decay_t< T >, std::decay_t< S > >::type promote_scalar_t
arena_t< T > to_arena(const T &a)
Converts given argument into a type that either has any dynamic allocation on AD stack or schedules i...
fvar< T > sqrt(const fvar< T > &x)
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
double sd(const T &a)
Returns the unbiased sample standard deviation of the coefficients in the specified std vector,...
fvar< T > inv_sqrt(const fvar< T > &x)
internal::callback_vari< plain_type_t< T >, F > * make_callback_vari(T &&value, F &&functor)
Creates a new vari with given value and a callback that implements the reverse pass (chain).
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
static thread_local AutodiffStackStorage * instance_