-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from mrc-ide/mrc-5336
Deterministic filter-like device
- Loading branch information
Showing
8 changed files
with
393 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#pragma once | ||
|
||
#include <dust2/cpu.hpp> | ||
|
||
namespace dust2 { | ||
|
||
template <typename T> | ||
class unfilter { | ||
public: | ||
using real_type = typename T::real_type; | ||
using data_type = typename T::data_type; | ||
|
||
// We need to provide direct access to the model, because the user | ||
// will want to set parameters in, and pull out state, etc. | ||
dust_cpu<T> model; | ||
|
||
unfilter(dust_cpu<T> model_, | ||
real_type time_start, | ||
std::vector<real_type> time, | ||
std::vector<data_type> data) : | ||
model(model_), | ||
time_start_(time_start), | ||
time_(time), | ||
data_(data), | ||
n_particles_(model.n_particles()), | ||
n_groups_(model.n_groups()), | ||
ll_(n_particles_ * n_groups_, 0), | ||
ll_step_(n_particles_ * n_groups_, 0) { | ||
const auto dt = model_.dt(); | ||
for (size_t i = 0; i < time_.size(); i++) { | ||
const auto t0 = i == 0 ? time_start_ : time_[i - 1]; | ||
const auto t1 = time_[i]; | ||
step_.push_back(static_cast<size_t>(std::round((t1 - t0) / dt))); | ||
} | ||
} | ||
|
||
void run() { | ||
const auto n_times = step_.size(); | ||
|
||
model.set_time(time_start_); | ||
model.set_state_initial(); | ||
std::fill(ll_.begin(), ll_.end(), 0); | ||
|
||
auto it_data = data_.begin(); | ||
for (size_t i = 0; i < n_times; ++i, it_data += n_groups_) { | ||
model.run_steps(step_[i]); // just compute this at point of use? | ||
model.compare_data(it_data, ll_step_.begin()); | ||
for (size_t j = 0; j < ll_.size(); ++j) { | ||
ll_[j] += ll_step_[j]; | ||
} | ||
} | ||
} | ||
|
||
template <typename Iter> | ||
void last_log_likelihood(Iter iter) { | ||
std::copy(ll_.begin(), ll_.end(), iter); | ||
} | ||
|
||
private: | ||
real_type time_start_; | ||
std::vector<real_type> time_; | ||
std::vector<size_t> step_; | ||
std::vector<data_type> data_; | ||
size_t n_particles_; | ||
size_t n_groups_; | ||
std::vector<real_type> ll_; | ||
std::vector<real_type> ll_step_; | ||
}; | ||
|
||
} |
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,79 @@ | ||
#pragma once | ||
|
||
#include <dust2/r/helpers.hpp> | ||
#include <dust2/filter.hpp> | ||
|
||
namespace dust2 { | ||
namespace r { | ||
|
||
// TODO: this name must be changed! | ||
template <typename T> | ||
cpp11::sexp dust2_cpu_unfilter_alloc(cpp11::list r_pars, | ||
cpp11::sexp r_time_start, | ||
cpp11::sexp r_time, | ||
cpp11::sexp r_dt, | ||
cpp11::list r_data, | ||
cpp11::sexp r_n_particles, | ||
cpp11::sexp r_n_groups) { | ||
using real_type = typename T::real_type; | ||
using rng_state_type = typename T::rng_state_type; | ||
|
||
auto n_particles = to_size(r_n_particles, "n_particles"); | ||
auto n_groups = to_size(r_n_groups, "n_groups"); | ||
const bool grouped = n_groups > 0; | ||
const auto time_start = check_time(r_time_start, "time_start"); | ||
const auto time = check_time_sequence<real_type>(time_start, r_time, "time"); | ||
const auto dt = check_dt(r_dt); | ||
const auto shared = build_shared<T>(r_pars, n_groups); | ||
const auto internal = build_internal<T>(shared); | ||
const auto data = check_data<T>(r_data, time.size(), n_groups, "data"); | ||
|
||
// It's possible that we don't want to always really be | ||
// deterministic here? Though nooone can think of a case where | ||
// that's actually the behaviour wanted. For now let's go fully | ||
// deterministic. | ||
auto seed = mcstate::random::r::as_rng_seed<rng_state_type>(R_NilValue); | ||
const auto deterministic = true; | ||
|
||
// Then allocate the model; this pulls together almost all the data | ||
// we need. At this point we could have constructed the model out | ||
// of one that exists already on the R side, but I think that's | ||
// going to feel weirder overall. | ||
const auto model = dust2::dust_cpu<T>(shared, internal, time_start, dt, n_particles, | ||
seed, deterministic); | ||
|
||
auto obj = new unfilter<T>(model, time_start, time, data); | ||
cpp11::external_pointer<unfilter<T>> ptr(obj, true, false); | ||
|
||
cpp11::sexp r_n_state = cpp11::as_sexp(obj->model.n_state()); | ||
cpp11::sexp r_group_names = R_NilValue; | ||
if (grouped) { | ||
r_group_names = r_pars.attr("names"); | ||
} | ||
cpp11::sexp r_grouped = cpp11::as_sexp(grouped); | ||
|
||
return cpp11::writable::list{ptr, r_n_state, r_grouped, r_group_names}; | ||
} | ||
|
||
template <typename T> | ||
cpp11::sexp dust2_cpu_unfilter_run(cpp11::sexp ptr, cpp11::sexp r_pars, | ||
bool grouped) { | ||
auto *obj = | ||
cpp11::as_cpp<cpp11::external_pointer<unfilter<T>>>(ptr).get(); | ||
if (r_pars != R_NilValue) { | ||
update_pars(obj->model, cpp11::as_cpp<cpp11::list>(r_pars), grouped); | ||
} | ||
obj->run(); | ||
|
||
const auto n_groups = obj->model.n_groups(); | ||
const auto n_particles = obj->model.n_particles(); | ||
cpp11::writable::doubles ret(n_groups * n_particles); | ||
obj->last_log_likelihood(REAL(ret)); | ||
if (grouped && n_particles > 1) { | ||
set_array_dims(ret, {n_particles, n_groups}); | ||
} | ||
return ret; | ||
} | ||
|
||
} | ||
} |
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
Oops, something went wrong.