-
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 #2 from mrc-ide/mrc-5326
Comparison with data
- Loading branch information
Showing
9 changed files
with
424 additions
and
5 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,111 @@ | ||
#include <dust2/common.hpp> | ||
|
||
namespace { | ||
inline double with_default(double default_value, cpp11::sexp value) { | ||
return value == R_NilValue ? default_value : cpp11::as_cpp<double>(value); | ||
} | ||
} | ||
|
||
class sir { | ||
public: | ||
sir() = delete; | ||
|
||
using real_type = double; | ||
|
||
struct shared_state { | ||
real_type N; | ||
real_type I0; | ||
real_type beta; | ||
real_type gamma; | ||
real_type exp_noise; | ||
}; | ||
|
||
using internal_state = dust2::no_internal_state; | ||
|
||
struct data_type { | ||
real_type incidence; | ||
}; | ||
|
||
using rng_state_type = mcstate::random::generator<real_type>; | ||
|
||
static size_t size(const shared_state& shared) { | ||
return 5; | ||
} | ||
|
||
static void initial(real_type time, | ||
real_type dt, | ||
const shared_state& shared, | ||
internal_state& internal, | ||
rng_state_type& rng_state, | ||
real_type * state_next) { | ||
state_next[0] = shared.N - shared.I0; | ||
state_next[1] = shared.I0; | ||
state_next[2] = 0; | ||
state_next[3] = 0; | ||
state_next[4] = 0; | ||
} | ||
|
||
// The main update function, converting state to state_next | ||
static void update(real_type time, | ||
real_type dt, | ||
const real_type * state, | ||
const shared_state& shared, | ||
internal_state& internal, | ||
rng_state_type& rng_state, | ||
real_type * state_next) { | ||
const auto S = state[0]; | ||
const auto I = state[1]; | ||
const auto R = state[2]; | ||
const auto cases_cumul = state[3]; | ||
// const auto cases_inc = state[4]; | ||
const auto p_SI = 1 - mcstate::math::exp(-shared.beta * I / shared.N * dt); | ||
const auto p_IR = 1 - mcstate::math::exp(-shared.gamma * dt); | ||
const auto n_SI = mcstate::random::binomial<real_type>(rng_state, S, p_SI); | ||
const auto n_IR = mcstate::random::binomial<real_type>(rng_state, I, p_IR); | ||
state_next[0] = S - n_SI; | ||
state_next[1] = I + n_SI - n_IR; | ||
state_next[2] = R + n_IR; | ||
state_next[3] = cases_cumul + n_SI; | ||
// state_next[4] = (time % shared.freq == 0) ? n_SI : (cases_inc + n_SI); | ||
state_next[4] = n_SI; | ||
} | ||
|
||
static shared_state build_shared(cpp11::list pars) { | ||
const real_type I0 = with_default(10, pars["I0"]); | ||
const real_type N = with_default(1000, pars["N"]); | ||
|
||
const real_type beta = with_default(0.2, pars["beta"]); | ||
const real_type gamma = with_default(0.1, pars["gamma"]); | ||
|
||
const real_type exp_noise = with_default(1e6, pars["exp_noise"]); | ||
|
||
return shared_state{N, I0, beta, gamma, exp_noise}; | ||
} | ||
|
||
static internal_state build_internal(cpp11::list pars) { | ||
return sir::internal_state{}; | ||
} | ||
|
||
static data_type build_data(cpp11::sexp r_data) { | ||
auto data = static_cast<cpp11::list>(r_data); | ||
return data_type{cpp11::as_cpp<real_type>(data["incidence"])}; | ||
} | ||
|
||
static real_type compare_data(const real_type time, | ||
const real_type dt, | ||
const real_type * state, | ||
const data_type& data, | ||
const shared_state& shared, | ||
internal_state& internal, | ||
rng_state_type& rng_state) { | ||
const auto incidence_observed = data.incidence; | ||
if (std::isnan(data.incidence)) { | ||
return 0; | ||
} | ||
const auto noise = | ||
mcstate::random::exponential(rng_state, shared.exp_noise); | ||
const auto incidence_modelled = state[4]; | ||
const auto lambda = incidence_modelled + noise; | ||
return mcstate::density::poisson(incidence_observed, lambda, true); | ||
} | ||
}; |
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
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,58 @@ | ||
// Generated by dust2 (version 0.1.0) - do not edit | ||
#include <cpp11.hpp> | ||
#include <dust2/r/cpu.hpp> | ||
|
||
// first declarations all go here, with their decorators, once we get | ||
// this bit sorted. | ||
|
||
#include "../inst/examples/sir.cpp" | ||
|
||
[[cpp11::register]] | ||
SEXP dust2_cpu_sir_alloc(cpp11::list r_pars, | ||
cpp11::sexp r_time, | ||
cpp11::sexp r_dt, | ||
cpp11::sexp r_n_particles, | ||
cpp11::sexp r_n_groups, | ||
cpp11::sexp r_seed, | ||
cpp11::sexp r_deterministic) { | ||
return dust2::r::dust2_cpu_alloc<sir>(r_pars, r_time, r_dt, | ||
r_n_particles, r_n_groups, | ||
r_seed, r_deterministic); | ||
} | ||
|
||
[[cpp11::register]] | ||
SEXP dust2_cpu_sir_run_steps(cpp11::sexp ptr, cpp11::sexp r_n_steps) { | ||
return dust2::r::dust2_cpu_run_steps<sir>(ptr, r_n_steps); | ||
} | ||
|
||
[[cpp11::register]] | ||
SEXP dust2_cpu_sir_state(cpp11::sexp ptr, bool grouped) { | ||
return dust2::r::dust2_cpu_state<sir>(ptr, grouped); | ||
} | ||
|
||
[[cpp11::register]] | ||
SEXP dust2_cpu_sir_time(cpp11::sexp ptr) { | ||
return dust2::r::dust2_cpu_time<sir>(ptr); | ||
} | ||
|
||
[[cpp11::register]] | ||
SEXP dust2_cpu_sir_set_state_initial(cpp11::sexp ptr) { | ||
return dust2::r::dust2_cpu_set_state_initial<sir>(ptr); | ||
} | ||
|
||
[[cpp11::register]] | ||
SEXP dust2_cpu_sir_set_state(cpp11::sexp ptr, cpp11::sexp r_state) { | ||
return dust2::r::dust2_cpu_set_state<sir>(ptr, r_state); | ||
} | ||
|
||
[[cpp11::register]] | ||
SEXP dust2_cpu_sir_rng_state(cpp11::sexp ptr) { | ||
return dust2::r::dust2_cpu_rng_state<sir>(ptr); | ||
} | ||
|
||
[[cpp11::register]] | ||
SEXP dust2_cpu_sir_compare_data(cpp11::sexp ptr, | ||
cpp11::sexp r_data, | ||
bool grouped) { | ||
return dust2::r::dust2_cpu_compare_data<sir>(ptr, r_data, grouped); | ||
} |
Oops, something went wrong.