Skip to content

Commit

Permalink
Adding object to summary function
Browse files Browse the repository at this point in the history
  • Loading branch information
gvegayon committed Dec 12, 2024
1 parent 3535438 commit affa7b8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
11 changes: 5 additions & 6 deletions src/lfmcmc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,13 @@ SEXP set_summary_fun_cpp(
LFMCMCSummaryFun<TData_default> fun_call = [fun](
std::vector< epiworld_double >& res,
const TData_default& dat,
LFMCMC<TData_default>*
LFMCMC<TData_default>* lfmcmc_obj
) -> void {

auto dat_int = cpp11::doubles(dat);
auto res_tmp = cpp11::doubles(fun(dat_int));
WrapLFMCMC(lfmcmc_ptr)(lfmcmc_obj, false);
auto res_tmp = cpp11::doubles(fun(dat_int, lfmcmc_ptr));


if (res.size() == 0u)
res.resize(res_tmp.size());
Expand Down Expand Up @@ -185,10 +187,7 @@ SEXP set_kernel_fun_cpp(
auto sim_stats_doubles = cpp11::doubles(simulated_stats);
auto obs_stats_doubles = cpp11::doubles(observed_stats);

cpp11::external_pointer<LFMCMC<TData_default>> lfmcmc_ptr(
lfmcmc_obj,
false
);
WrapLFMCMC(lfmcmc_ptr)(lfmcmc_obj, false);

return cpp11::as_cpp<epiworld_double>(
fun(sim_stats_doubles, obs_stats_doubles, epsilon, lfmcmc_ptr)
Expand Down
5 changes: 3 additions & 2 deletions vignettes/likelihood-free-mcmc.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ For our example, since the population distribution is already a summary of the d
With more complicated use cases, you might instead compute summary statistics such as the mean or standard deviation.

```{r sumfun}
summary_fun <- function(data) {
summary_fun <- function(data, model) {
return(data)
}
```
Expand All @@ -126,8 +126,9 @@ proposal_fun <- function(old_params) {
The **kernel function** effectively scores the results of the latest simulation run against the observed data, by comparing the summary statistics from `summary_fun` for each.
LFMCMC uses the kernel score and the Hastings Ratio to determine whether or not to accept the parameters for that run.
In our example, since `summary_fun` simply passes the data through, `simulated_stats` and `observed_stats` are our simulated and observed data respectively.

```{r kernfun}
kernel_fun <- function(simulated_stats, observed_stats, epsilon) {
kernel_fun <- function(simulated_stats, observed_stats, epsilon, model) {
dnorm(sqrt(sum((simulated_stats - observed_stats)^2)))
}
```
Expand Down

0 comments on commit affa7b8

Please sign in to comment.