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