Stan Math Library
2.20.0
reverse mode automatic differentiation
stan
math
prim
mat
fun
welford_var_estimator.hpp
Go to the documentation of this file.
1
#ifndef STAN_MATH_PRIM_MAT_FUN_WELFORD_VAR_ESTIMATOR_HPP
2
#define STAN_MATH_PRIM_MAT_FUN_WELFORD_VAR_ESTIMATOR_HPP
3
4
#include <
stan/math/prim/mat/fun/Eigen.hpp
>
5
#include <vector>
6
7
namespace
stan
{
8
namespace
math {
9
10
class
welford_var_estimator
{
11
public
:
12
explicit
welford_var_estimator
(
int
n)
13
:
m_
(
Eigen
::VectorXd::Zero(n)),
m2_
(
Eigen
::VectorXd::Zero(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) {
24
++
num_samples_
;
25
26
Eigen::VectorXd delta(q -
m_
);
27
m_
+= delta /
num_samples_
;
28
m2_
+= delta.cwiseProduct(q -
m_
);
29
}
30
31
int
num_samples
() {
return
num_samples_
; }
32
33
void
sample_mean
(Eigen::VectorXd&
mean
) { mean =
m_
; }
34
35
void
sample_variance
(Eigen::VectorXd&
var
) {
36
if
(
num_samples_
> 1)
37
var =
m2_
/ (
num_samples_
- 1.0);
38
}
39
40
protected
:
41
double
num_samples_
;
42
Eigen::VectorXd
m_
;
43
Eigen::VectorXd
m2_
;
44
};
45
46
}
// namespace math
47
}
// namespace stan
48
#endif
stan::math::welford_var_estimator::num_samples
int num_samples()
Definition:
welford_var_estimator.hpp:31
stan::math::welford_var_estimator::num_samples_
double num_samples_
Definition:
welford_var_estimator.hpp:41
stan::math::welford_var_estimator::welford_var_estimator
welford_var_estimator(int n)
Definition:
welford_var_estimator.hpp:12
stan
Definition:
log_sum_exp.hpp:8
stan::math::var
Independent (input) and dependent (output) variables for gradients.
Definition:
var.hpp:33
Eigen
(Expert) Numerical traits for algorithmic differentiation variables.
Definition:
Eigen_NumTraits.hpp:9
stan::math::welford_var_estimator::sample_mean
void sample_mean(Eigen::VectorXd &mean)
Definition:
welford_var_estimator.hpp:33
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:21
Eigen.hpp
stan::math::welford_var_estimator::add_sample
void add_sample(const Eigen::VectorXd &q)
Definition:
welford_var_estimator.hpp:23
stan::math::welford_var_estimator
Definition:
welford_var_estimator.hpp:10
stan::math::welford_var_estimator::m2_
Eigen::VectorXd m2_
Definition:
welford_var_estimator.hpp:43
stan::math::welford_var_estimator::sample_variance
void sample_variance(Eigen::VectorXd &var)
Definition:
welford_var_estimator.hpp:35
stan::math::welford_var_estimator::m_
Eigen::VectorXd m_
Definition:
welford_var_estimator.hpp:42
stan::math::welford_var_estimator::restart
void restart()
Definition:
welford_var_estimator.hpp:17
[
Stan Home Page
]
© 2011–2018, Stan Development Team.