Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
variance.hpp
Go to the documentation of this file.
1 #ifndef STAN__AGRAD__REV__MATRIX__VARIANCE_HPP
2 #define STAN__AGRAD__REV__MATRIX__VARIANCE_HPP
3 
4 #include <vector>
5 #include <boost/math/tools/promotion.hpp>
8 #include <stan/agrad/rev/var.hpp>
12 
13 namespace stan {
14 
15  namespace agrad {
16 
17  namespace { // anonymous
18 
19  var calc_variance(size_t size,
20  const var* dtrs) {
21  vari** varis = (vari**) memalloc_.alloc(size * sizeof(vari*));
22  for (size_t i = 0; i < size; ++i)
23  varis[i] = dtrs[i].vi_;
24  double sum = 0.0;
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;
32  }
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,
39  varis, partials));
40  }
41 
42  }
43 
51  var variance(const std::vector<var>& v) {
52  stan::math::check_nonzero_size("variance(%1%)",v,"v",(double*)0);
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  stan::math::check_nonzero_size("variance(%1%)",m,"m",(double*)0);
70  if (m.size() == 1) return 0;
71  return calc_variance(m.size(), &m(0));
72  }
73 
74  }
75 }
76 
77 #endif
memory::stack_alloc memalloc_
Definition: var_stack.cpp:16
fvar< T > sum(const Eigen::Matrix< fvar< T >, R, C > &m)
Definition: sum.hpp:14
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
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.
Definition: var.hpp:27
int size(const std::vector< T > &x)
Definition: size.hpp:11
var variance(const std::vector< var > &v)
Return the sample variance of the specified standard vector.
Definition: variance.hpp:51
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.

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