Checks if the provided exposure model can be solved in advance of the main simulation in fewer function calls than would be expected in the main simulation. Note that there is some overhead to checking for pre-computation, so this may not be quicker and may actually slow down the simulation. However, in some cases where many individuals share identical exposure probabilities, running this function can lead to massive speed ups.
Usage
precomputation_checks(
N,
times,
exposure_ids,
groups,
exposure_model,
foe_pars,
demography,
VERBOSE,
check_correct = FALSE,
...
)
Arguments
- N
integer number of individuals in the simulation
- times
vector of time periods in the simulation
- exposure_ids
integer vector of unique exposure IDs matching the
biomarker_map
- groups
integer vector of unique groups matching entries in
demography
- exposure_model
function pointer to the desired exposure model
- foe_pars
object (usually an array, but may be a list or other class) corresponding to the
exposure_model
inputs- demography
tibble of demography variables used for the simulation
- VERBOSE
(optional) if specified, an integer giving the frequency at which updates should be printed, as well as dictating whether progress messages are printed. Defaults to
NULL
- check_correct
if TRUE, computes the entire exposure probability array as would be done in
runserosim
with no pre-computation. This is usually quite slow, but can be used to check that the pre-computed exposure probability array is correct.- ...
other inputs to the
exposure_model
Value
a list containing: 1) a boolean set to TRUE
if precomputation was successful; 2) a 3D array matching the dimensions of N
, times
and exposure_ids
giving the individual probability of exposure in each time period
Examples
times <- seq(1,100,by=1)
N <- 100
n_exposure_ids <- 2
n_groups <- 2
demography <- generate_pop_demography(N=N, times=times,prob_removal=0,
aux=list(Group=list(name="group",options=c(1,2),proportion=c(0.5,0.5))))
#> Joining with `by = join_by(i)`
foe_pars <- array(runif(n_exposure_ids*length(times)*n_groups),dim=c(n_groups,
length(times),n_exposure_ids))
res <- precomputation_checks(N, times=times, exposure_ids=1:2,groups=1:2,
exposure_model_simple_FOE,foe_pars=foe_pars, demography=demography,VERBOSE=10,check_correct=TRUE)
#> Run time can be reduced by pre-computation! Pre-computation would require a maximum of 501 calls to exposure_model as opposed to 20000
#>
#> Checking if exposure model can be vectorized...
#>
#> Solving the model for one individual without vectorization would take: 9.822845e-05 seconds
#>
#> Solving the model for one individual with vectorization would take: 1.096725e-05 seconds
#>
#> Exposure model can be vectorized!
#>
#> Precomputing exposure probabilities...
#>
#> Checking if pre-computed exposure probabilities are the same as calculating individually...
#>
#> Individual: 10
#>
#> Individual: 20
#>
#> Individual: 30
#>
#> Individual: 40
#>
#> Individual: 50
#>
#> Individual: 60
#>
#> Individual: 70
#>
#> Individual: 80
#>
#> Individual: 90
#>
#> Individual: 100
#>
#> Pre-computed exposure probabilities are all correct
#>