Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sd.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 <cmath>
5 #include <vector>
6 #include <boost/math/tools/promotion.hpp>
9 #include <stan/agrad/rev/var.hpp>
10 #include <stan/agrad/rev/vari.hpp>
13 
14 namespace stan {
15 
16  namespace agrad {
17 
18  namespace { // anonymous
19 
20  // if x.size() = N, and x[i] = x[j] =
21  // then lim sd(x) -> 0 [ d/dx[n] sd(x) ] = sqrt(N) / N
22 
23  var calc_sd(size_t size,
24  const var* dtrs) {
25  using std::sqrt;
26  vari** varis = (vari**) memalloc_.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 = (double*) memalloc_.alloc(size * sizeof(double));
41  if (sum_of_squares < 1e-20) {
42  double grad_limit = 1 / std::sqrt((double)size);
43  for (size_t i = 0; i < size; ++i)
44  partials[i] = grad_limit;
45  } else {
46  double multiplier = 1 / (sd * (size - 1));
47  for (size_t i = 0; i < size; ++i)
48  partials[i] = multiplier * (dtrs[i].vi_->val_ - mean);
49  }
50  return var(new stored_gradient_vari(sd, size,
51  varis, partials));
52  }
53 
54  }
55 
63  var sd(const std::vector<var>& v) {
64  stan::math::check_nonzero_size("sd(%1%)", v,"v",(double*)0);
65  if (v.size() == 1) return 0;
66  return calc_sd(v.size(), &v[0]);
67  }
68 
69  /*
70  * Return the sample standard deviation of the specified vector,
71  * row vector, or matrix. Raise domain error if size is not
72  * greater than zero.
73  *
74  * @tparam R number of rows
75  * @tparam C number of columns
76  * @param[in] m input matrix
77  * @return sample standard deviation of specified matrix
78  */
79  template <int R, int C>
80  var sd(const Eigen::Matrix<var,R,C>& m) {
81  stan::math::check_nonzero_size("sd(%1%)", m,"m",(double*)0);
82  if (m.size() == 1) return 0;
83  return calc_sd(m.size(), &m(0));
84  }
85 
86  }
87 }
88 
89 #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
fvar< T > sqrt(const fvar< T > &x)
Definition: sqrt.hpp:15
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
var sd(const std::vector< var > &v)
Return the sample standard deviation of the specified standard vector.
Definition: sd.hpp:63
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
double e()
Return the base of the natural logarithm.
Definition: constants.hpp:86
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.