Stan Math Library: stan/math/rev/mat/fun/variance.hpp Source File

| Stan Math Library 2.10.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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>
5 #include <stan/math/prim/mat/fun/Eigen.hpp>
6 #include <stan/math/prim/mat/fun/mean.hpp>
7 #include <stan/math/rev/core.hpp>
8 #include <stan/math/prim/arr/err/check_nonzero_size.hpp>
9 #include <vector>
10
11 namespace stan {
12
13 namespace math {
14
15 namespace { // anonymous
16
17 var calc_variance(size_t size,
18 const var* dtrs) {
19 vari** varis = reinterpret_cast<vari**>(ChainableStack::memalloc_
20 .alloc(size * sizeof(vari*)));
21 for (size_t i = 0; i < size; ++i)
22 varis[i] = dtrs[i].vi_;
23 double sum = 0.0;
24 for (size_t i = 0; i < size; ++i)
25 sum += dtrs[i].vi_->val_;
26 double mean = sum / size;
27 double sum_of_squares = 0;
28 for (size_t i = 0; i < size; ++i) {
29 double diff = dtrs[i].vi_->val_ - mean;
30 sum_of_squares += diff * diff;
31 }
32 double variance = sum_of_squares / (size - 1);
33 double* partials
34 = reinterpret_cast<double*>(ChainableStack::memalloc_
35 .alloc(size * sizeof(double)));
36 double two_over_size_m1 = 2 / (size - 1);
37 for (size_t i = 0; i < size; ++i)
38 partials[i] = two_over_size_m1 * (dtrs[i].vi_->val_ - mean);
39 return var(new stored_gradient_vari(variance, size,
40 varis, partials));
41 }
42
43 }
44
52 var variance(const std::vector<var>& v) {
53 stan::math::check_nonzero_size("variance", "v", v);
54 if (v.size() == 1) return 0;
55 return calc_variance(v.size(), &v[0]);
56 }
57
58 /*
59 * Return the sample variance of the specified vector, row vector,
60 * or matrix. Raise domain error if size is not greater than
61 * zero.
62 *
63 * @tparam R number of rows
64 * @tparam C number of columns
65 * @param[in] m input matrix
66 * @return sample variance of specified matrix
67 */
68 template <int R, int C>
69 var variance(const Eigen::Matrix<var, R, C>& m) {
70 stan::math::check_nonzero_size("variance", "m", m);
71 if (m.size() == 1) return 0;
72 return calc_variance(m.size(), &m(0));
73 }
74
75 }
76 }
77
78 #endif
stan::math::sum
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition: sum.hpp:20
core.hpp
stan
Definition: log_sum_exp.hpp:8
stan::math::AutodiffStackStorage::memalloc_
static stack_alloc memalloc_
Definition: autodiffstackstorage.hpp:16
stan::math::var
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:31
stan::math::check_nonzero_size
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.
Definition: check_nonzero_size.hpp:30
stan::math::variance
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
mean.hpp
stan::math::mean
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
Eigen.hpp
stan::math::size
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
Definition: size.hpp:17
check_nonzero_size.hpp
stan::math::stack_alloc::alloc
void * alloc(size_t len)
Return a newly allocated block of memory of the appropriate size managed by the stack allocator...
Definition: stack_alloc.hpp:166

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