Skip to content

Instantly share code, notes, and snippets.

@ony
Created November 26, 2014 23:40
Show Gist options
  • Save ony/d26213ee20664b00e35b to your computer and use it in GitHub Desktop.
Save ony/d26213ee20664b00e35b to your computer and use it in GitHub Desktop.
Bootstrapping in C++ using variadic templates for parallel estimators application
template <typename T>
struct estimate { T mean, lbound, ubound, stdev; }
template <typename T, typename... Rs>
std::tuple<estimate<Rs>...> bootstrap(std::vector<T> &sample, size_t resamples,
std::function<Rs(std::vector<T>)>... estimators)
{
const size_t n = sample.size();
// re-sample and estimate our sample
std::default_random_engine rng;
std::uniform_int_distribution<size_t> index_dist(0, n - 1);
std::vector<std::tuple<Rs...>> esample;
esample.reserve(resamples);
for (size_t i = 0; i < resamples; ++i)
{
std::vector<T> bsample;
bsample.reserve(n);
for (size_t j = 0; j < n; ++j)
{
bsample.emplace_back(sample[index_dist(rng)]);
}
esample.empalce_back(estimators(bsample)...);
}
// lets calculate mean
std::tuple<Rs...> emean = {Rs(0)...};
for (const auto &evalue : esample)
{
// XXX: what to put here?
// emean += evalue; <-- element-by-element
}
return {}; // TODO:
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment