1 #ifndef STAN__AGRAD__REV__MATRIX__VARIANCE_HPP
2 #define STAN__AGRAD__REV__MATRIX__VARIANCE_HPP
5 #include <boost/math/tools/promotion.hpp>
19 var calc_variance(
size_t size,
22 for (
size_t i = 0; i <
size; ++i)
23 varis[i] = dtrs[i].vi_;
25 for (
size_t i = 0; i <
size; ++i)
26 sum += dtrs[i].vi_->val_;
27 double mean = sum / size;
28 double sum_of_squares = 0;
29 for (
size_t i = 0; i <
size; ++i) {
30 double diff = dtrs[i].vi_->val_ -
mean;
31 sum_of_squares += diff * diff;
33 double variance = sum_of_squares / (size - 1);
34 double* partials = (
double*)
memalloc_.
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));
memory::stack_alloc memalloc_
fvar< T > sum(const Eigen::Matrix< fvar< T >, R, C > &m)
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...
void * alloc(size_t len)
Return a newly allocated block of memory of the appropriate size managed by the stack allocator...
Independent (input) and dependent (output) variables for gradients.
int size(const std::vector< T > &x)
var variance(const std::vector< var > &v)
Return the sample variance of the specified standard vector.
bool check_nonzero_size(const char *function, const T_y &y, const char *name, T_result *result)
Return true if the specified matrix/vector is of non-zero size.