Stan Math Library  2.14.0
reverse mode automatic differentiation
sd.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_MAT_FUN_SD_HPP
2 #define STAN_MATH_REV_MAT_FUN_SD_HPP
3 
7 #include <stan/math/rev/core.hpp>
8 #include <boost/math/tools/promotion.hpp>
9 #include <cmath>
10 #include <vector>
11 
12 namespace stan {
13  namespace math {
14 
15  namespace { // anonymous
16 
17  // if x.size() = N, and x[i] = x[j] =
18  // then lim sd(x) -> 0 [ d/dx[n] sd(x) ] = sqrt(N) / N
19 
20  var calc_sd(size_t size,
21  const var* dtrs) {
22  using std::sqrt;
23  vari** varis
24  = reinterpret_cast<vari**>(ChainableStack::memalloc_
25  .alloc(size * sizeof(vari*)));
26  for (size_t i = 0; i < size; ++i)
27  varis[i] = dtrs[i].vi_;
28  double sum = 0.0;
29  for (size_t i = 0; i < size; ++i)
30  sum += dtrs[i].vi_->val_;
31  double mean = sum / size;
32  double sum_of_squares = 0;
33  for (size_t i = 0; i < size; ++i) {
34  double diff = dtrs[i].vi_->val_ - mean;
35  sum_of_squares += diff * diff;
36  }
37  double variance = sum_of_squares / (size - 1);
38  double sd = sqrt(variance);
39  double* partials
40  = reinterpret_cast<double*>(ChainableStack::memalloc_
41  .alloc(size * sizeof(double)));
42  if (sum_of_squares < 1e-20) {
43  double grad_limit = 1 / std::sqrt(static_cast<double>(size));
44  for (size_t i = 0; i < size; ++i)
45  partials[i] = grad_limit;
46  } else {
47  double multiplier = 1 / (sd * (size - 1));
48  for (size_t i = 0; i < size; ++i)
49  partials[i] = multiplier * (dtrs[i].vi_->val_ - mean);
50  }
51  return var(new stored_gradient_vari(sd, size,
52  varis, partials));
53  }
54 
55  }
56 
64  inline var sd(const std::vector<var>& v) {
65  check_nonzero_size("sd", "v", v);
66  if (v.size() == 1) return 0;
67  return calc_sd(v.size(), &v[0]);
68  }
69 
70  /*
71  * Return the sample standard deviation of the specified vector,
72  * row vector, or matrix. Raise domain error if size is not
73  * greater than zero.
74  *
75  * @tparam R number of rows
76  * @tparam C number of columns
77  * @param[in] m input matrix
78  * @return sample standard deviation of specified matrix
79  */
80  template <int R, int C>
81  var sd(const Eigen::Matrix<var, R, C>& m) {
82  check_nonzero_size("sd", "m", m);
83  if (m.size() == 1) return 0;
84  return calc_sd(m.size(), &m(0));
85  }
86 
87  }
88 }
89 #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.
fvar< T > sqrt(const fvar< T > &x)
Definition: sqrt.hpp:14
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:30
boost::math::tools::promote_args< T >::type sd(const std::vector< T > &v)
Returns the unbiased sample standard deviation of the coefficients in the specified column vector...
Definition: sd.hpp:22
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
double e()
Return the base of the natural logarithm.
Definition: constants.hpp:94
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.