1 #ifndef STAN_MATH_REV_MAT_FUN_VARIANCE_HPP
2 #define STAN_MATH_REV_MAT_FUN_VARIANCE_HPP
4 #include <boost/math/tools/promotion.hpp>
16 inline var calc_variance(
size_t size,
19 .
alloc(size *
sizeof(vari*)));
20 for (
size_t i = 0; i <
size; ++i)
21 varis[i] = dtrs[i].vi_;
23 for (
size_t i = 0; i <
size; ++i)
24 sum += dtrs[i].vi_->val_;
25 double mean = sum / size;
26 double sum_of_squares = 0;
27 for (
size_t i = 0; i <
size; ++i) {
28 double diff = dtrs[i].vi_->val_ -
mean;
29 sum_of_squares += diff * diff;
31 double variance = sum_of_squares / (size - 1);
34 .
alloc(size *
sizeof(
double)));
35 double two_over_size_m1 = 2 / (size - 1);
36 for (
size_t i = 0; i <
size; ++i)
37 partials[i] = two_over_size_m1 * (dtrs[i].vi_->val_ -
mean);
38 return var(
new stored_gradient_vari(variance, size,
53 if (v.size() == 1)
return 0;
54 return calc_variance(v.size(), &v[0]);
67 template <
int R,
int C>
70 if (m.size() == 1)
return 0;
71 return calc_variance(m.size(), &m(0));
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
static stack_alloc memalloc_
Independent (input) and dependent (output) variables for gradients.
bool check_nonzero_size(const char *function, const char *name, const T_y &y)
Return true if the specified matrix/vector is of non-zero size.
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...
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...
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...