1 #ifndef STAN_MATH_REV_MAT_FUN_SD_HPP 2 #define STAN_MATH_REV_MAT_FUN_SD_HPP 21 vari** varis =
reinterpret_cast<vari**
>(
23 for (
size_t i = 0; i <
size; ++i)
24 varis[i] = dtrs[i].vi_;
26 for (
size_t i = 0; i <
size; ++i)
27 sum += dtrs[i].vi_->
val_;
28 double mean = sum / size;
29 double sum_of_squares = 0;
30 for (
size_t i = 0; i <
size; ++i) {
32 sum_of_squares += diff * diff;
34 double variance = sum_of_squares / (size - 1);
35 double sd =
sqrt(variance);
36 double* partials =
reinterpret_cast<double*
>(
38 if (sum_of_squares < 1
e-20) {
39 double grad_limit = 1 /
std::sqrt(static_cast<double>(size));
40 for (
size_t i = 0; i <
size; ++i)
41 partials[i] = grad_limit;
43 double multiplier = 1 / (sd * (size - 1));
44 for (
size_t i = 0; i <
size; ++i)
45 partials[i] = multiplier * (dtrs[i].vi_->
val_ -
mean);
59 inline var sd(
const std::vector<var>& v) {
76 template <
int R,
int C>
77 var sd(
const Eigen::Matrix<var, R, C>& m) {
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
var calc_sd(size_t size, const var *dtrs)
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 > sqrt(const fvar< T > &x)
The variable implementation base class.
static STAN_THREADS_DEF AutodiffStackStorage * instance_
Independent (input) and dependent (output) variables for gradients.
A var implementation that stores the daughter variable implementation pointers and the partial deriva...
const double val_
The value of this variable.
boost::math::tools::promote_args< T >::type sd(const std::vector< T > &v)
Returns the unbiased sample standard deviation of the coefficients in the specified column vector...
boost::math::tools::promote_args< T >::type variance(const std::vector< T > &v)
Returns the sample variance (divide by length - 1) of the coefficients in the specified standard vect...
vari * vi_
Pointer to the implementation of this variable.
boost::math::tools::promote_args< T >::type mean(const std::vector< T > &v)
Returns the sample mean (i.e., average) of the coefficients in the specified standard vector...
double e()
Return the base of the natural logarithm.
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
void * alloc(size_t len)
Return a newly allocated block of memory of the appropriate size managed by the stack allocator...