Stan Math Library  2.20.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 
4 #include <stan/math/rev/meta.hpp>
7 #include <stan/math/rev/core.hpp>
8 #include <cmath>
9 #include <vector>
10 
11 namespace stan {
12 namespace math {
13 
14 namespace internal {
15 
16 // if x.size() = N, and x[i] = x[j] =
17 // then lim sd(x) -> 0 [ d/dx[n] sd(x) ] = sqrt(N) / N
18 
19 inline var calc_sd(size_t size, const var* dtrs) {
20  using std::sqrt;
21  vari** varis = reinterpret_cast<vari**>(
22  ChainableStack::instance_->memalloc_.alloc(size * sizeof(vari*)));
23  for (size_t i = 0; i < size; ++i)
24  varis[i] = dtrs[i].vi_;
25  double sum = 0.0;
26  for (size_t i = 0; i < size; ++i)
27  sum += dtrs[i].vi_->val_;
28  double mean = sum / size;
29  double sum_of_squares = 0;
30  for (size_t i = 0; i < size; ++i) {
31  double diff = dtrs[i].vi_->val_ - mean;
32  sum_of_squares += diff * diff;
33  }
34  double variance = sum_of_squares / (size - 1);
35  double sd = sqrt(variance);
36  double* partials = reinterpret_cast<double*>(
37  ChainableStack::instance_->memalloc_.alloc(size * sizeof(double)));
38  if (sum_of_squares < 1e-20) {
39  double grad_limit = 1 / std::sqrt(static_cast<double>(size));
40  for (size_t i = 0; i < size; ++i)
41  partials[i] = grad_limit;
42  } else {
43  double multiplier = 1 / (sd * (size - 1));
44  for (size_t i = 0; i < size; ++i)
45  partials[i] = multiplier * (dtrs[i].vi_->val_ - mean);
46  }
47  return var(new stored_gradient_vari(sd, size, varis, partials));
48 }
49 
50 } // namespace internal
51 
59 inline var sd(const std::vector<var>& v) {
60  check_nonzero_size("sd", "v", v);
61  if (v.size() == 1)
62  return 0;
63  return internal::calc_sd(v.size(), &v[0]);
64 }
65 
66 /*
67  * Return the sample standard deviation of the specified vector,
68  * row vector, or matrix. Raise domain error if size is not
69  * greater than zero.
70  *
71  * @tparam R number of rows
72  * @tparam C number of columns
73  * @param[in] m input matrix
74  * @return sample standard deviation of specified matrix
75  */
76 template <int R, int C>
77 var sd(const Eigen::Matrix<var, R, C>& m) {
78  check_nonzero_size("sd", "m", m);
79  if (m.size() == 1)
80  return 0;
81  return internal::calc_sd(m.size(), &m(0));
82 }
83 
84 } // namespace math
85 } // namespace stan
86 #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
var calc_sd(size_t size, const var *dtrs)
Definition: sd.hpp:19
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:13
The variable implementation base class.
Definition: vari.hpp:30
static STAN_THREADS_DEF AutodiffStackStorage * instance_
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:33
A var implementation that stores the daughter variable implementation pointers and the partial deriva...
const double val_
The value of this variable.
Definition: vari.hpp:38
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:20
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:22
vari * vi_
Pointer to the implementation of this variable.
Definition: var.hpp:45
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:21
double e()
Return the base of the natural logarithm.
Definition: constants.hpp:87
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–2018, Stan Development Team.