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>
17 var calc_variance(
size_t size,
20 .
alloc(size *
sizeof(vari*)));
21 for (
size_t i = 0; i <
size; ++i)
22 varis[i] = dtrs[i].vi_;
24 for (
size_t i = 0; i <
size; ++i)
25 sum += dtrs[i].vi_->val_;
26 double mean = sum / size;
27 double sum_of_squares = 0;
28 for (
size_t i = 0; i <
size; ++i) {
29 double diff = dtrs[i].vi_->val_ -
mean;
30 sum_of_squares += diff * diff;
32 double variance = sum_of_squares / (size - 1);
35 .
alloc(size *
sizeof(
double)));
36 double two_over_size_m1 = 2 / (size - 1);
37 for (
size_t i = 0; i <
size; ++i)
38 partials[i] = two_over_size_m1 * (dtrs[i].vi_->val_ -
mean);
39 return var(
new stored_gradient_vari(variance, size,
54 if (v.size() == 1)
return 0;
55 return calc_variance(v.size(), &v[0]);
68 template <
int R,
int C>
71 if (m.size() == 1)
return 0;
72 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...