All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
sd.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_REV_MAT_FUN_SD_HPP
#define STAN_MATH_REV_MAT_FUN_SD_HPP
#include <stan/math/prim/mat/fun/Eigen.hpp>
#include <stan/math/prim/mat/fun/mean.hpp>
#include <stan/math/rev/core.hpp>
#include <stan/math/prim/scal/err/check_nonzero_size.hpp>
#include <boost/math/tools/promotion.hpp>
#include <cmath>
#include <vector>
namespace stan {
namespace math {
namespace { // anonymous
// if x.size() = N, and x[i] = x[j] =
// then lim sd(x) -> 0 [ d/dx[n] sd(x) ] = sqrt(N) / N
var calc_sd(size_t size,
const var* dtrs) {
using std::sqrt;
vari** varis
= reinterpret_cast<vari**>(ChainableStack::memalloc_
.alloc(size * sizeof(vari*)));
for (size_t i = 0; i < size; ++i)
varis[i] = dtrs[i].vi_;
double sum = 0.0;
for (size_t i = 0; i < size; ++i)
sum += dtrs[i].vi_->val_;
double mean = sum / size;
double sum_of_squares = 0;
for (size_t i = 0; i < size; ++i) {
double diff = dtrs[i].vi_->val_ - mean;
sum_of_squares += diff * diff;
}
double variance = sum_of_squares / (size - 1);
double sd = sqrt(variance);
double* partials
= reinterpret_cast<double*>(ChainableStack::memalloc_
.alloc(size * sizeof(double)));
if (sum_of_squares < 1e-20) {
double grad_limit = 1 / std::sqrt(static_cast<double>(size));
for (size_t i = 0; i < size; ++i)
partials[i] = grad_limit;
} else {
double multiplier = 1 / (sd * (size - 1));
for (size_t i = 0; i < size; ++i)
partials[i] = multiplier * (dtrs[i].vi_->val_ - mean);
}
return var(new stored_gradient_vari(sd, size,
varis, partials));
}
}
var sd(const std::vector<var>& v) {
stan::math::check_nonzero_size("sd", "v", v);
if (v.size() == 1) return 0;
return calc_sd(v.size(), &v[0]);
}
/*
* Return the sample standard deviation of the specified vector,
* row vector, or matrix. Raise domain error if size is not
* greater than zero.
*
* @tparam R number of rows
* @tparam C number of columns
* @param[in] m input matrix
* @return sample standard deviation of specified matrix
*/
template <int R, int C>
var sd(const Eigen::Matrix<var, R, C>& m) {
stan::math::check_nonzero_size("sd", "m", m);
if (m.size() == 1) return 0;
return calc_sd(m.size(), &m(0));
}
}
}
#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::math::sqrt
fvar< T > sqrt(const fvar< T > &x)
Definition: sqrt.hpp:15
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:32
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:31
stan::math::sd
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
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::e
double e()
Return the base of the natural logarithm.
Definition: constants.hpp:95
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:170

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