Skip to content

Instantly share code, notes, and snippets.

@nanis
Last active December 7, 2016 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nanis/a3c0106f7f8d615fc1505f0972b56bf9 to your computer and use it in GitHub Desktop.
Save nanis/a3c0106f7f8d615fc1505f0972b56bf9 to your computer and use it in GitHub Desktop.
Simple C++ program to demonstrate boost's variance accumulator
#include <iomanip>
#include <iostream>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/variance.hpp>
#include <boost/accumulators/statistics/stats.hpp>
namespace bacc = boost::accumulators;
static void
demo(const double base)
{
bacc::accumulator_set<double, bacc::stats<bacc::tag::variance(bacc::lazy)> > acc;
std::cout << std::setprecision(15) << base << '\n';
acc(base + 4);
acc(base + 7);
acc(base + 13);
acc(base + 16);
std::cout << bacc::variance(acc) << '\n';
return;
}
int
main(int argc, char *argv[])
{
demo(0);
demo(1e1);
demo(1e3);
demo(1e5);
demo(1e8);
demo(1e9);
return 0;
}
@nanis
Copy link
Author

nanis commented Dec 7, 2016

On OS X 10.11.6 compiled with

$ g++ -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

$ g++ var.cpp -o var -I/opt/local/include

I get the output:

0
22.5
10
22.5
1000
22.5
100000
22.5
100000000
22
1000000000
-128

which is really bad.

@nanis
Copy link
Author

nanis commented Dec 7, 2016

Confirmed on Windows as well

C:\...\var> cl -Ic:\opt\include -EHsc var.cpp             
Microsoft (R) C/C++ Optimizing Compiler Version 18.00.31101 for x64   
Copyright (C) Microsoft Corporation.  All rights reserved.            
                                                                      
var.cpp                                                               
Microsoft (R) Incremental Linker Version 12.00.31101.0                
Copyright (C) Microsoft Corporation.  All rights reserved.            
                                                                      
/out:var.exe                                                          
var.obj                                                               
                                                                      
C:\...\var> var   
0         
22.5      
10        
22.5      
1000      
22.5      
100000    
22.5      
100000000 
22        
1000000000
-128

@nanis
Copy link
Author

nanis commented Dec 7, 2016

*** MUST NOT USE lazy***

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment