1 #ifndef STAN_MATH_PRIM_MAT_FUN_VARIANCE_HPP 2 #define STAN_MATH_PRIM_MAT_FUN_VARIANCE_HPP 7 #include <boost/math/tools/promotion.hpp> 22 inline typename boost::math::tools::promote_args<T>::type
variance(
23 const std::vector<T>& v) {
29 for (
size_t i = 0; i < v.size(); ++i) {
30 T diff = v[i] - v_mean;
31 sum_sq_diff += diff * diff;
33 return sum_sq_diff / (v.size() - 1);
42 template <
typename T,
int R,
int C>
43 inline typename boost::math::tools::promote_args<T>::type
variance(
44 const Eigen::Matrix<T, R, C>& m) {
49 typename boost::math::tools::promote_args<T>::type mn(
mean(m));
50 typename boost::math::tools::promote_args<T>::type sum_sq_diff(0);
51 for (
int i = 0; i < m.size(); ++i) {
52 typename boost::math::tools::promote_args<T>::type diff = m(i) - mn;
53 sum_sq_diff += diff * diff;
55 return sum_sq_diff / (m.size() - 1);
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.
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...