Stan Math Library  2.20.0
reverse mode automatic differentiation
variance.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_MAT_FUN_VARIANCE_HPP
2 #define STAN_MATH_REV_MAT_FUN_VARIANCE_HPP
3 
4 #include <stan/math/rev/meta.hpp>
6 #include <stan/math/rev/core.hpp>
8 #include <vector>
9 
10 namespace stan {
11 namespace math {
12 
13 namespace internal {
14 
15 inline var calc_variance(size_t size, const var* dtrs) {
17  for (size_t i = 0; i < size; ++i)
18  varis[i] = dtrs[i].vi_;
19  double sum = 0.0;
20  for (size_t i = 0; i < size; ++i)
21  sum += dtrs[i].vi_->val_;
22  double mean = sum / size;
23  double sum_of_squares = 0;
24  double reciprocal_size_m1 = 1.0 / (size - 1);
25  double two_over_size_m1 = 2.0 * reciprocal_size_m1;
26  double* partials
28  for (size_t i = 0; i < size; ++i) {
29  double diff = dtrs[i].vi_->val_ - mean;
30  sum_of_squares += diff * diff;
31  partials[i] = two_over_size_m1 * diff;
32  }
33  double variance = sum_of_squares * reciprocal_size_m1;
34  return var(new stored_gradient_vari(variance, size, varis, partials));
35 }
36 
37 } // namespace internal
38 
46 inline var variance(const std::vector<var>& v) {
47  check_nonzero_size("variance", "v", v);
48  if (v.size() == 1)
49  return 0;
50  return internal::calc_variance(v.size(), &v[0]);
51 }
52 
53 /*
54  * Return the sample variance of the specified vector, row vector,
55  * or matrix. Raise domain error if size is not greater than
56  * zero.
57  *
58  * @tparam R number of rows
59  * @tparam C number of columns
60  * @param[in] m input matrix
61  * @return sample variance of specified matrix
62  */
63 template <int R, int C>
64 var variance(const Eigen::Matrix<var, R, C>& m) {
65  check_nonzero_size("variance", "m", m);
66  if (m.size() == 1)
67  return 0;
68  return internal::calc_variance(m.size(), &m(0));
69 }
70 
71 } // namespace math
72 } // namespace stan
73 #endif
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition: sum.hpp:20
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.
The variable implementation base class.
Definition: vari.hpp:30
static STAN_THREADS_DEF AutodiffStackStorage * instance_
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:33
A var implementation that stores the daughter variable implementation pointers and the partial deriva...
const double val_
The value of this variable.
Definition: vari.hpp:38
var calc_variance(size_t size, const var *dtrs)
Definition: variance.hpp:15
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...
Definition: variance.hpp:22
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:45
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...
Definition: mean.hpp:21
T * alloc_array(size_t n)
Allocate an array on the arena of the specified size to hold values of the specified template paramet...
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
Definition: size.hpp:17

     [ Stan Home Page ] © 2011–2018, Stan Development Team.