Stan Math Library  2.12.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 <boost/math/tools/promotion.hpp>
7 #include <stan/math/rev/core.hpp>
9 #include <vector>
10 
11 namespace stan {
12  namespace math {
13 
14  namespace {
15 
16  inline var calc_variance(size_t size,
17  const var* dtrs) {
18  vari** varis = reinterpret_cast<vari**>(ChainableStack::memalloc_
19  .alloc(size * sizeof(vari*)));
20  for (size_t i = 0; i < size; ++i)
21  varis[i] = dtrs[i].vi_;
22  double sum = 0.0;
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;
30  }
31  double variance = sum_of_squares / (size - 1);
32  double* partials
33  = reinterpret_cast<double*>(ChainableStack::memalloc_
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,
39  varis, partials));
40  }
41 
42  }
43 
51  inline var variance(const std::vector<var>& v) {
52  check_nonzero_size("variance", "v", v);
53  if (v.size() == 1) return 0;
54  return calc_variance(v.size(), &v[0]);
55  }
56 
57  /*
58  * Return the sample variance of the specified vector, row vector,
59  * or matrix. Raise domain error if size is not greater than
60  * zero.
61  *
62  * @tparam R number of rows
63  * @tparam C number of columns
64  * @param[in] m input matrix
65  * @return sample variance of specified matrix
66  */
67  template <int R, int C>
68  var variance(const Eigen::Matrix<var, R, C>& m) {
69  check_nonzero_size("variance", "m", m);
70  if (m.size() == 1) return 0;
71  return calc_variance(m.size(), &m(0));
72  }
73 
74  }
75 }
76 #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
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:30
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...
Definition: variance.hpp:24
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:23
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
Definition: size.hpp:17
void * alloc(size_t len)
Return a newly allocated block of memory of the appropriate size managed by the stack allocator...

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