-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
96 changed files
with
3,204 additions
and
5,033 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,49 @@ | ||
#pragma once | ||
#include <numeric> | ||
#include <iostream> | ||
#include <Eigen/Dense> | ||
#include <autoppl/math/ess.hpp> | ||
|
||
namespace ppl { | ||
|
||
template <class ArrayType> | ||
double sample_average(const ArrayType& storage) { | ||
double sum = std::accumulate( | ||
storage.begin(), | ||
storage.end(), | ||
0.); | ||
return sum / (storage.size()); | ||
template <class Derived> | ||
inline auto mean(const Eigen::MatrixBase<Derived>& m) | ||
{ return m.colwise().mean(); } | ||
|
||
template <class Derived> | ||
inline auto sd(const Eigen::MatrixBase<Derived>& m) | ||
{ | ||
assert(m.rows() > 1); | ||
auto var = (m.rowwise() - ppl::mean(m)) | ||
.colwise().squaredNorm() / (m.rows() - 1); | ||
return var.array().sqrt().matrix(); | ||
} | ||
|
||
inline void summary(const std::string& header, | ||
const Eigen::MatrixXd& m, | ||
double warmup_time, | ||
double sampling_time) | ||
{ | ||
std::cout << "Warmup: " << warmup_time << std::endl; | ||
std::cout << "Sampling: " << sampling_time << std::endl; | ||
|
||
std::cout << header << std::endl; | ||
|
||
Eigen::MatrixXd mean = ppl::mean(m); | ||
std::cout << "Mean:\n" | ||
<< mean << std::endl; | ||
|
||
Eigen::MatrixXd sd = ppl::sd(m); | ||
std::cout << "SD:\n" | ||
<< sd << std::endl; | ||
|
||
Eigen::MatrixXd ess = ppl::math::ess(m); | ||
std::cout << "ESS:\n" | ||
<< ess << std::endl; | ||
|
||
Eigen::MatrixXd ess_per_s = ess / sampling_time; | ||
std::cout << "ESS/s:\n" | ||
<< ess_per_s << std::endl; | ||
} | ||
|
||
} // namespace ppl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#include <random> | ||
#include <benchmark/benchmark.h> | ||
#include "benchmark_utils.hpp" | ||
#include <autoppl/expression/expr_builder.hpp> | ||
#include <autoppl/expression/variable/data.hpp> | ||
#include <autoppl/expression/variable/param.hpp> | ||
#include <autoppl/mcmc/mh/mh.hpp> | ||
|
||
namespace ppl { | ||
|
||
static void BM_MHBayesNet(benchmark::State& state) { | ||
size_t n_samples = state.range(0); | ||
constexpr size_t n_data = 1000; | ||
|
||
std::bernoulli_distribution b(0.341); | ||
std::mt19937 gen(0); | ||
ppl::Data<util::disc_param_t, ppl::vec> y(n_data); | ||
|
||
ppl::Param<double> p1, p2, m1, m2, M1, M2; | ||
ppl::Param<int> w; | ||
auto model = ( | ||
m1 |= ppl::uniform(0., 1.), | ||
m2 |= ppl::uniform(0., 1.), | ||
M1 |= ppl::uniform(0., 1.), | ||
M2 |= ppl::uniform(0., 1.), | ||
p1 |= ppl::uniform(m1, M1), | ||
p2 |= ppl::uniform(m2, M2), | ||
w |= ppl::bernoulli(0.3 * p1), | ||
y |= ppl::bernoulli(w * p1 + (1-w) * p2) | ||
); | ||
|
||
for (size_t i = 0; i < n_data; ++i) { | ||
y.get()(i) = b(gen); | ||
} | ||
|
||
ppl::MCMCResult res; | ||
|
||
ppl::MHConfig config; | ||
config.warmup = n_samples; | ||
config.samples = n_samples; | ||
|
||
for (auto _ : state) { | ||
res = ppl::mh(model, config); | ||
} | ||
|
||
ppl::summary("m1, m2, M1, M2, p1, p2", | ||
res.cont_samples, | ||
res.warmup_time, | ||
res.sampling_time); | ||
ppl::summary("w", | ||
res.disc_samples.cast<double>(), | ||
res.warmup_time, | ||
res.sampling_time); | ||
} | ||
|
||
BENCHMARK(BM_MHBayesNet)->Arg(100000) | ||
->Arg(200000) | ||
->Arg(300000) | ||
; | ||
} // namespace ppl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.