Loading [MathJax]/extensions/TeX/AMSsymbols.js
Stan Math Library
5.0.0
Automatic Differentiation
▼
Stan Math Library
Overview
▼
Contributor Guides
Developer Guide
Adding New Functions
Adding New Distributions
Common Pitfalls
Using requires for general overloads
Reverse Mode Types
Testing Automatic Differentiation Functions
Testing New Distributions
Add New Functions With Known Gradients
Adding New OpenCL Functions
Windows Development Tips
▼
Internal Docs
►
Probability Distributions
►
OpenCL
►
Compressed Sparse Row matrix format.
►
Type Traits
►
Available requires<> for overloading.
►
Eigen expressions
►
arena_matrix <br>
►
real <br>
►
Parallelism
(External Link) Stan Language Docs
(External Link) Stan Discourse
►
Stan Math Library Docs
►
Class List
•
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Modules
Pages
Loading...
Searching...
No Matches
welford_var_estimator.hpp
Go to the documentation of this file.
1
#ifndef STAN_MATH_PRIM_FUN_WELFORD_VAR_ESTIMATOR_HPP
2
#define STAN_MATH_PRIM_FUN_WELFORD_VAR_ESTIMATOR_HPP
3
4
#include <
stan/math/prim/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
41
protected
:
42
double
num_samples_
;
43
Eigen::VectorXd
m_
;
44
Eigen::VectorXd
m2_
;
45
};
46
47
}
// namespace math
48
}
// namespace stan
49
#endif
Eigen.hpp
stan::math::var_value< double >
stan::math::welford_var_estimator::welford_var_estimator
welford_var_estimator(int n)
Definition
welford_var_estimator.hpp:12
stan::math::welford_var_estimator::m2_
Eigen::VectorXd m2_
Definition
welford_var_estimator.hpp:44
stan::math::welford_var_estimator::restart
void restart()
Definition
welford_var_estimator.hpp:17
stan::math::welford_var_estimator::sample_variance
void sample_variance(Eigen::VectorXd &var)
Definition
welford_var_estimator.hpp:35
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::num_samples_
double num_samples_
Definition
welford_var_estimator.hpp:42
stan::math::welford_var_estimator::m_
Eigen::VectorXd m_
Definition
welford_var_estimator.hpp:43
stan::math::welford_var_estimator::num_samples
int num_samples()
Definition
welford_var_estimator.hpp:31
stan::math::welford_var_estimator::sample_mean
void sample_mean(Eigen::VectorXd &mean)
Definition
welford_var_estimator.hpp:33
stan::math::welford_var_estimator
Definition
welford_var_estimator.hpp:10
Eigen
(Expert) Numerical traits for algorithmic differentiation variables.
Definition
Eigen_NumTraits.hpp:11
stan::math::mean
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
stan
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Definition
unit_vector_constrain.hpp:15
stan
math
prim
fun
welford_var_estimator.hpp
[
Stan Home Page
]
© 2011–2019, Stan Development Team.