Likelihood-Free Markhov Chain Monte Carlo (LFMCMC)
Usage
LFMCMC(model = NULL)
run_lfmcmc(lfmcmc, params_init, n_samples, epsilon, seed = NULL)
set_observed_data(lfmcmc, observed_data)
set_proposal_fun(lfmcmc, fun)
use_proposal_norm_reflective(lfmcmc)
set_simulation_fun(lfmcmc, fun)
set_summary_fun(lfmcmc, fun)
set_kernel_fun(lfmcmc, fun)
use_kernel_fun_gaussian(lfmcmc)
get_mean_params(lfmcmc)
get_mean_stats(lfmcmc)
get_initial_params(lfmcmc)
get_current_proposed_params(lfmcmc)
get_current_accepted_params(lfmcmc)
get_current_proposed_stats(lfmcmc)
get_current_accepted_stats(lfmcmc)
get_observed_stats(lfmcmc)
get_all_sample_params(lfmcmc)
get_all_sample_stats(lfmcmc)
get_all_sample_acceptance(lfmcmc)
get_all_sample_drawn_prob(lfmcmc)
get_all_sample_kernel_scores(lfmcmc)
get_all_accepted_params(lfmcmc)
get_all_accepted_stats(lfmcmc)
get_all_accepted_kernel_scores(lfmcmc)
get_n_samples(lfmcmc)
get_n_stats(lfmcmc)
get_n_params(lfmcmc)
# S3 method for class 'epiworld_lfmcmc'
verbose_off(x)
set_params_names(lfmcmc, names)
set_stats_names(lfmcmc, names)
# S3 method for class 'epiworld_lfmcmc'
print(x, burnin = 0, ...)
Arguments
- model
A model of class epiworld_model or
NULL
(see details).- lfmcmc
LFMCMC model
- params_init
Initial model parameters, treated as double
- n_samples
Number of samples, treated as integer
- epsilon
Epsilon parameter, treated as double
- seed
Random engine seed
- observed_data
Observed data, treated as double.
- fun
A function (see details).
- x
LFMCMC model to print
- names
Character vector of names.
- burnin
Integer. Number of samples to discard as burnin before computing the summary.
- ...
Ignored
Value
The LFMCMC
function returns a model of class epiworld_lfmcmc.
The simulated model of class epiworld_lfmcmc.
use_kernel_fun_gaussian
: The LFMCMC model with kernel function set to gaussian.
get_mean_params
: The param means for the given lfmcmc model.
get_mean_stats
: The stats means for the given lfmcmc model.
The function
get_initial_params
returns the initial parameters for the given LFMCMC model.
The function
get_current_proposed_params
returns the proposed parameters for the next LFMCMC sample.
The function
get_current_accepted_params
returns the most recently accepted parameters (the current state of the LFMCMC)
The function
get_current_proposed_stats
returns the statistics from the simulation run with the proposed parameters
The function
get_current_accepted_stats
returns the statistics from the most recently accepted parameters
The function
get_observed_stats
returns the statistics for the observed data
The function
get_all_sample_params
returns a matrix of sample parameters for the given LFMCMC model. with the number of rows equal to the number of samples and the number of columns equal to the number of parameters.
The function
get_all_sample_stats
returns a matrix of statistics for the given LFMCMC model. with the number of rows equal to the number of samples and the number of columns equal to the number of statistics.
The function
get_all_sample_acceptance
returns a vector of boolean flags which indicate whether a given sample was accepted
The function
get_all_sample_drawn_prob
returns a vector of drawn probabilities for each sample
The function
get_all_sample_kernel_scores
returns a vector of kernel scores for each sample
The function
get_all_accepted_params
returns a matrix of accepted parameters for the given LFMCMC model. with the number of rows equal to the number of samples and the number of columns equal to the number of parameters.
The function
get_all_accepted_stats
returns a matrix of accepted statistics for the given LFMCMC model. with the number of rows equal to the number of samples and the number of columns equal to the number of statistics.
The function
get_all_accepted_kernel_scores
returns a vector of kernel scores for each accepted sample
The functions
get_n_samples
,get_n_stats
, andget_n_params
return the number of samples, statistics, and parameters for the given LFMCMC model, respectively.
The
verbose_on
andverbose_off
functions return the same model, howeververbose_off
returns the model with no progress bar.
set_params_names
: The lfmcmc model with the parameter names added.
set_stats_names
: The lfmcmc model with the stats names added.
Details
Performs a Likelihood-Free Markhov Chain Monte Carlo simulation. When
model
is not NULL
, the model uses the same random-number generator
engine as the model. Otherwise, when model
is NULL
, a new random-number
generator engine is created.
The functions passed to the LFMCMC object have different arguments depending on the object:
set_proposal_fun
: A vector of parameters and the model.set_simulation_fun
: A vector of parameters and the model.set_summary_fun
: A vector of simulated data and the model.set_kernel_fun
: A vector of simulated statistics, observed statistics, epsilon, and the model.
The verbose_on
and verbose_off
functions activate and deactivate printing
progress on screen, respectively. Both functions return the model (x
) invisibly.
Examples
## Setup an SIR model to use in the simulation
model_seed <- 122
model_sir <- ModelSIR(name = "COVID-19", prevalence = .1,
transmission_rate = .9, recovery_rate = .3)
agents_smallworld(
model_sir,
n = 1000,
k = 5,
d = FALSE,
p = 0.01
)
verbose_off(model_sir)
run(model_sir, ndays = 50, seed = model_seed)
## Setup LFMCMC
# Extract the observed data from the model
obs_data <- get_today_total(model_sir)
# Define the simulation function
simfun <- function(params, lfmcmc_obj) {
set_param(model_sir, "Recovery rate", params[1])
set_param(model_sir, "Transmission rate", params[2])
run(model_sir, ndays = 50)
res <- get_today_total(model_sir)
return(res)
}
# Define the summary function
sumfun <- function(dat, lfmcmc_obj) {
return(dat)
}
# Create the LFMCMC model
lfmcmc_model <- LFMCMC(model_sir) |>
set_simulation_fun(simfun) |>
set_summary_fun(sumfun) |>
use_proposal_norm_reflective() |>
use_kernel_fun_gaussian() |>
set_observed_data(obs_data)
## Run LFMCMC simulation
# Set initial parameters
par0 <- c(0.1, 0.5)
n_samp <- 2000
epsil <- 1.0
# Run the LFMCMC simulation
verbose_off(lfmcmc_model)
run_lfmcmc(
lfmcmc = lfmcmc_model,
params_init = par0,
n_samples = n_samp,
epsilon = epsil,
seed = model_seed
)
# Print the results
set_stats_names(lfmcmc_model, get_states(model_sir))
set_params_names(lfmcmc_model, c("Immune recovery", "Infectiousness"))
print(lfmcmc_model)
#> ___________________________________________
#>
#> LIKELIHOOD-FREE MARKOV CHAIN MONTE CARLO
#>
#> N Samples (total) : 2000
#> N Samples (after burn-in period) : 2000
#> Elapsed t : 1.00s
#>
#> Parameters:
#> -Immune recovery : 0.45 [ 0.14, 0.95] (initial : 0.10)
#> -Infectiousness : 0.85 [ 0.54, 1.00] (initial : 0.50)
#>
#> Statistics:
#> -Susceptible : 0.22 [ 0.00, 2.00] (Observed: 0.00)
#> -Infected : 0.07 [ 0.00, 1.00] (Observed: 0.00)
#> -Recovered : 995.71 [ 998.00, 1000.00] (Observed: 1000.00)
#> ___________________________________________
#>
get_mean_stats(lfmcmc_model)
#> [1] 0.2215 0.0655 995.7130
get_mean_params(lfmcmc_model)
#> [1] 0.4482147 0.8480773