Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add config for mh, unify mcmc result, add bayes net example with mh
- Loading branch information
1 parent
5e59e69
commit c96953f
Showing
19 changed files
with
240 additions
and
122 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
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
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,6 +1,6 @@ | ||
#pragma once | ||
#include "util/traits/traits.hpp" | ||
#include "expression/expr_builder.hpp" | ||
#include "mcmc/mh.hpp" | ||
#include "mcmc/mh/mh.hpp" | ||
#include "mcmc/hmc/nuts/nuts.hpp" | ||
#include "math/ess.hpp" |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#pragma once | ||
#include <cstddef> | ||
#include <autoppl/mcmc/sampler_tools.hpp> | ||
|
||
namespace ppl { | ||
|
||
struct ConfigBase | ||
{ | ||
size_t warmup = 1000; | ||
size_t samples = 1000; | ||
size_t seed = mcmc::random_seed(); | ||
}; | ||
|
||
} // 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
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#pragma once | ||
#include <autoppl/mcmc/config_base.hpp> | ||
|
||
namespace ppl { | ||
|
||
struct MHConfig : ConfigBase | ||
{ | ||
double sigma = 1.0; | ||
double alpha = 0.25; | ||
}; | ||
|
||
} // namespace ppl |
Oops, something went wrong.