Skip to content

Instantly share code, notes, and snippets.

@cjameshuff
Created November 26, 2012 18:51
Show Gist options
  • Save cjameshuff/4149893 to your computer and use it in GitHub Desktop.
Save cjameshuff/4149893 to your computer and use it in GitHub Desktop.
template<typename T>
std::tuple<double, double, double, double> stat_test(int ntrials, const T & fn)
{
double m = 0, m2 = 0, s = 0, mn = DBL_MAX, mx = -DBL_MAX;
int n = 0;
for(int j = 0; j < ntrials; ++j)
{
double x = fn();
mn = std::min(mn, x);
mx = std::min(mx, x);
++n;
double m_old = m;
m += (x - m)/n;
m2 += (x - m)*(x - m_old);
}
s = m2/(n - 1);
return std::make_tuple(mn, mx, m, s);
}
template<typename T>
void print_stat_test(int ntrials, const T & fn)
{
double mn, mx, m, s;
std::tie(mn, mx, m, s) = stat_test(ntrials, fn);
cout << fstring("m: %.16f, s: %.16f\n")% m % s;
}
int samps = 50;
int ntrials = 1000;
print_stat_test(ntrials, [&](){return IntegrateMC(testfn, samps, 0, 1);});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment