Skip to content

Instantly share code, notes, and snippets.

@yoneken
Last active December 18, 2015 02:39
Show Gist options
  • Save yoneken/5712517 to your computer and use it in GitHub Desktop.
Save yoneken/5712517 to your computer and use it in GitHub Desktop.
The variation calculated in normal boost is not an unbiased variance but a sample variance. See: http://mathworld.wolfram.com/Variance.html This change makes boost to calculate unbiased variances normally.
diff -ur /opt/local/include/boost/accumulators/statistics/covariance.hpp ../boost/accumulators/statistics/covariance.hpp
--- /opt/local/include/boost/accumulators/statistics/covariance.hpp 2013-05-31 08:41:19.000000000 +0900
+++ ../boost/accumulators/statistics/covariance.hpp 2013-06-05 19:09:33.000000000 +0900
@@ -141,7 +141,7 @@
template<typename Args>
void operator ()(Args const &args)
{
- std::size_t cnt = count(args);
+ cnt = count(args);
if (cnt > 1)
{
@@ -157,11 +157,12 @@
result_type result(dont_care) const
{
- return this->cov_;
+ return numeric::average(this->cov_ * cnt, cnt - 1.);
}
private:
result_type cov_;
+ std::size_t cnt;
};
} // namespace impl
diff -ur /opt/local/include/boost/accumulators/statistics/variance.hpp ../boost/accumulators/statistics/variance.hpp
--- /opt/local/include/boost/accumulators/statistics/variance.hpp 2013-05-31 08:41:19.000000000 +0900
+++ ../boost/accumulators/statistics/variance.hpp 2013-06-05 19:11:04.000000000 +0900
@@ -96,7 +96,7 @@
template<typename Args>
void operator ()(Args const &args)
{
- std::size_t cnt = count(args);
+ cnt = count(args);
if(cnt > 1)
{
@@ -110,11 +110,12 @@
result_type result(dont_care) const
{
- return this->variance;
+ return numeric::average(this->variance * cnt, cnt - 1);
}
private:
result_type variance;
+ std::size_t cnt;
};
} // namespace impl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment