Automatic Differentiation
 
Loading...
Searching...
No Matches
welford_covar_estimator.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_WELFORD_COVAR_ESTIMATOR_HPP
2#define STAN_MATH_PRIM_FUN_WELFORD_COVAR_ESTIMATOR_HPP
3
5#include <vector>
6
7namespace stan {
8namespace math {
9
11 public:
13 : m_(Eigen::VectorXd::Zero(n)), m2_(Eigen::MatrixXd::Zero(n, n)) {
14 restart();
15 }
16
17 void restart() {
18 num_samples_ = 0;
19 m_.setZero();
20 m2_.setZero();
21 }
22
23 void add_sample(const Eigen::VectorXd& q) {
25
26 Eigen::VectorXd delta(q - m_);
27 m_ += delta / num_samples_;
28 m2_ += (q - m_) * delta.transpose();
29 }
30
31 int num_samples() { return num_samples_; }
32
33 void sample_mean(Eigen::VectorXd& mean) { mean = m_; }
34
35 void sample_covariance(Eigen::MatrixXd& covar) {
36 if (num_samples_ > 1) {
37 covar = m2_ / (num_samples_ - 1.0);
38 }
39 }
40
41 protected:
43 Eigen::VectorXd m_;
44 Eigen::MatrixXd m2_;
45};
46
47} // namespace math
48} // namespace stan
49#endif
void add_sample(const Eigen::VectorXd &q)
void sample_covariance(Eigen::MatrixXd &covar)
(Expert) Numerical traits for algorithmic differentiation variables.
scalar_type_t< T > mean(const T &m)
Returns the sample mean (i.e., average) of the coefficients in the specified std vector,...
Definition mean.hpp:20
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Definition fvar.hpp:9