---
title: "`EasyABC`: a `R` package to perform efficient approximate Bayesian computation sampling schemes"
author: "Franck Jabot, Thierry Faure, Nicolas Dumoulin, Carlo Albert"
date: "`r Sys.Date()`"
output: 
  html_document:
    toc: true
    number_sections: true
    toc_float: true
vignette: >
  %\VignetteIndexEntry{`EasyABC`: a `R` package to perform efficient approximate Bayesian computation sampling schemes}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
  \usepackage[utf8]{inputenc}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
    cache = TRUE,
    collapse = TRUE,
    comment = "#>"
)
```

# Summary
The aim of this vignette is to present the features of the `EasyABC` package.
Section [Overview of the package EasyABC] describes the different algorithms available in the package.
Section [Installation and requirements] details how to install the package and the formatting requirements.
Sections [The toy model] and [The trait model] present two detailed worked examples.

# Overview of the package EasyABC
[algorithms]: `EasyABC` enables to launch various ABC schemes and to retrieve the ouputs of the simulations, so as to perform post-processing treatments with the various R tools available. `EasyABC` is also able to launch the simulations on multiple cores of a multi-core computer.
Four main types of ABC schemes are available in EasyABC: the standard rejection algorithm of Pritchard et al. (1999), sequential schemes first proposed by Sisson et al. (2007), coupled to MCMC sequential schemes first proposed by Marjoram et al. (2003), and a Simulated Annealing algorithm (SABC) suggested in Albert et al. (2014).
Four different sequential algorithms are available: the ones of Beaumont et al. (2009), Drovandi and Pettitt (2011), Del Moral et al. (2012) and Lenormand et al. (2012).
Four different MCMC schemes are available: the ones of Marjoram et al. (2003), Wegmann et al. (2009a), a modification of Marjoram et al. (2003)'s algorithm in which the tolerance and proposal range are determined by the algorithm, following the modifications of Wegmann et al. (2009a).
Details on how to implement these various algorithms with `EasyABC` are given in the manual pages of each function and two examples are detailed in Sections [The toy model] and [The trait model]. We provide below a short presentation of each implemented algorithm.

## The standard rejection algorithm of Pritchard et al. (1999)
This sampling scheme consists in drawing the model parameters in the prior distributions, in using these model parameter values to launch a simulation and in repeating this two-step procedure `nb_simul` times.
At the end of the `nb_simul` simulations, the simulations closest to the target (or at a distance smaller than a tolerance threshold) in the space of the summary statistics are retained to form an approximate posterior distribution of the model parameters.

## Sequential algorithms
Sequential algorithms for ABC have first been proposed by Sisson et al. (2007). These algorithms aim at reducing the required number of simulations to reach a given quality of the posterior approximation.
The underlying idea of these algorithms is to spend more time in the areas of the parameter space where simulations are frequently close to the target.
Sequential algorithms consist in a first step of standard rejection ABC, followed by a number of steps where the sampling of the parameter space is not anymore performed according to the prior distributions of parameter values.
Various ways to perform this biased sampling have been proposed, and four of them are implemented in the package `EasyABC`.

## Coupled to MCMC algorithms
The idea of ABC-MCMC algorithms proposed by Marjoram et al. (2003) is to perform a Metropolis-Hastings algorithm to explore the parameter space, and in replacing the step of likelihood ratio computation by simulations of the model.
The original algorithm of Marjoram et al. (2003) is implemented in the method "Marjoram_original" in `EasyABC`.
Wegmann et al. (2009) later proposed a number of improvements to the original scheme of Marjoram et al. (2003): they proposed to perform a calibration step so that the algorithm automatically determines the tolerance threshold, the scaling of the summary statistics and the scaling of the jumps in the parameter space during the MCMC.
These improvements have been implemented in the method "Marjoram".
Wegmann et al. (2009) also proposed additional modifications, among which a PLS transformation of the summary statistics. The complete Wegmann et al. (2009)'s algorithm is implemented in the method "Wegmann".

## Simulated annealing
Inspired by Simulated Annealing algorithms used for optimization, the SABC algorithm from Albert et al. (2014) propagates an ensemble of particles in the product space of parameters and model outputs and continuously lowers the tolerance between model outputs and the data so that the parameter marginal converges to the posterior. The tolerance is lowered adaptively so as to minimize entropy production, which serves as a measure for computational waste.
In `EasyABC`, SABC is implemented in the function `SABC`.

# Installation and requirements

## Installing the package
A version of R greater than or equal to 2.15.0 is required. The package has been tested on Windows 32 and Linux, but not on Mac. To install the `EasyABC` package from `R`, simply type:
```{r eval=FALSE} 
install.packages("EasyABC")
```

Once the package is installed, it needs to be loaded in the current `R` session to be used:
```{r setup, include=FALSE}
library(EasyABC)
```

For online help on the package content, simply type:
```{r eval=FALSE}
help(package="EasyABC")
```
For online help on a particular command (such as the function `ABC_sequential`), simply type:
```{r eval=FALSE}
help(ABC_sequential)
```

## The simulation code - for use on a single core
Users need to develop a simulation code with minimal compatibility constraints. The code can either be a `R` function or a binary executable file.

If the code is a `R` function, its argument must be a vector of parameter values and it must return a vector of summary statistics. If the option `use_seed=TRUE` is chosen, the first parameter value passed to the simulation code corresponds to the seed value to be used by the simulation code to initialize the pseudo-random number generator.  The following parameters are the model parameters.

If the code is a binary executable file, it needs to read the parameter values in a file named 'input' in which each line contains one parameter value, and to output the summary statistics in a file named 'output' in which each summary statistics must be separated by a space or a tabulation.

If the code is a binary executable file, a wrapper `R` function named 'binary_model' is available to interface the executable file with the `R` functions of the `EasyABC` package (see section [The toy model] below).

Alternatively, users may prefer building a `R` function calling their binary executable file. A short tutorial is provided in [this section](#rc_link) to call a `C/C++` program.

*NB:* Currently, SABC ignores the `use_seed` option and requires a function, whose first argument is the parameter vector.

## The simulation code - for use with multiple cores

Users need to develop a simulation code with minimal compatibility constraints. The code can either be a `R` function or a binary executable file.

If the code is a `R` function, its argument must be a vector of parameter values and it must return a vector of summary statistics. The first parameter value passed to the simulation code corresponds to the seed value to be used by the simulation code to initialize the pseudo-random number generator. The following parameters are the model parameters. This means that the option `use_seed` must be turned to `TRUE` when using `EasyABC` with multiple cores.

If the code is a binary executable file, it needs to have as its single argument a positive integer `k`. It has to read the parameter values in a file named 'inputk' (where k is the integer passed as argument to the binary code: 'input1', 'input2'...) in which each line contains one parameter value, and to output the summary statistics in a file named 'outputk' (where k is the integer passed as argument to the binary code: 'output1', 'output2'...) in which each summary statistics must be separated by a space or a tabulation.
This construction avoids multiple cores to read/write in the same files.
If the code is a binary executable file, a wrapper `R` function named 'binary_model_cluster' is available to interface the executable file with the `R` functions of the `EasyABC` package (see section [The trait model] below).

Alternatively, users may prefer building a `R` function calling their binary executable file. A short tutorial is provided in [this section](#rc_link) to call a `C/C++` program.

*NB:* Currently, SABC does currently not support the usage of multiple cores.

## Management of pseudo-random number generators
To insure that stochastic simulations are independent, the simulation code must either possess an internal way of initializing the seeds of its pseudo-random number generators each time the simulation code is launched.

This can be achieved for instance by initializing the seed to the clock value.
It is often desirable though to have a way to re-run some analyses with similar seed values. 
`EasyABC` offers this possibility by default with the default option `use_seed=TRUE,seed_count=0` where `seed_count` can be any integer number.

If this option is chosen, a seed value is provided in the input file as a first (additional) parameter, and incremented by 1 at each call of the simulation code.

This means that the simulation code must be designed so that the first parameter is a seed initializing value.
In the worked example (Section [The trait model]), the simulation code `trait_model` makes use of this package option, and in the first example (Section [The toy model]), the way this option can be used with a simple `R` function is demonstrated.

*NB:* Note that when using multicores with the package functions (`n_cluster=x` with `x` larger than 1), the option `use_seed=TRUE` is forced, since the seed value is also used to distribute the tasks to each core.

## Encoding the prior distributions
A list encoding the prior distributions used for each model parameter must be supplied by the user.
Each element of the list corresponds to a model parameter and can be defined in two ways:

1. By using predefined prior distributions. In this case, the list element must be a vector whose first argument determines the type of prior distribution followed by the argument of the distribution function, possible values are:
    - "unif" for a uniform distribution on a segment, followed by two numbers the minimum and maximum values of the uniform distribution
    - "normal" for a normal distribution, followed by two numbers the mean and standard deviation of the normal distribution
    - "lognormal" for a lognormal distribution, followed by two numbers: the mean and standard deviation on the log scale of the lognormal distribution
    - "exponential" for an exponential distribution, followed by one number: the rate of the exponential distribution
```{r simple_prior}
my_prior=list(c("unif",0,1),c("normal",1,2))
```

*NB:* Note that a fixed variable can be passed to the simulation code by choosing for this fixed variable a uniform prior distribution and a trivial range (with equal lower and upper bounds). 
The `EasyABC` methods will not work properly if these fixed variables are passed with other types of prior distributions (like a normal distribution with a standard deviation equal to zero).

2. By providing the user-defined sampling and density function. In this case, each list element must be itself a list of two elements: the sampling function and the density function. For example, a uniform distribution can be defined using this approach with the following code (equivalent to `my_prior=list(c("unif",0,1))`):
```{r custom_prior}
my_prior=list(list(c("runif",1,0,1), c("dunif",0,1)))
```

*NB:* SABC requires the prior to be specified as a sampler and as a density (see the examples below).

## Adding constraints to prior distributions

To add constraints to prior distributions (for instance, parameter 1 < parameter 2), users need to use the parameter `prior_test` in the ABC functions of the package (see their online documentation).
This parameter `prior_test` will be evaluated as a logical expression, you can use all the logical operators including `"<"`, `">"`, ... to define whether a parameter set respects the constraint.
Each parameter should be designated with `"X1"`, `"X2"`, ... in the same order as in the prior definition.

Here is an example where the second parameter should be greater than the first one:
```{r, eval=FALSE}
prior = list(c("unif",0,1),c("unif",0,10))
ABC_rejection(model=a_model,prior=prior,nb_simul=3, prior_test="X2 > X1")
```

## The target summary statistics

A vector containing the summary statistics of the data must be supplied. The statistics must be in the same order as in the simulation outputs.
The function `SABC` allows for a semi-automatic generation of summary statistics according to Fearnhead et al. (2012).

## The option verbose

Intermediary results can be written in output files in the working directory. Users solely need to choose the option `verbose=TRUE` when launching the `EasyABC` functions (otherwise, the default value for `verbose` is `FALSE`).
Intermediary results consist in the progressive writing of simulation outputs for the functions `ABC_rejection` and `ABC_mcmc` and in the writing of intermediary results at the end of each step for the function `ABC_sequential`. Additional details are provided in the help files of the functions.

## Building a `R` function calling a `C/C++` program {#rc_link}

Users having a `C/C++` simulation code may wish to construct a `R` function calling their `C/C++` program, instead of using the provided wrappers (see sections [The simulation code - for use on a single core] and [The simulation code - for use with multiple cores]).
The procedure is abundantly described in the ['Writing R Extensions' manual](https://cran.r-project.org/doc/manuals/R-exts.html).

In short, this can be done by:

- Adapt your C/C++ program by wrapping your main method into a `extern "C" \{ … \}` block. Here is an excerpt of the source code of the trait model provided in this package, in the folder `src`:
```{r, eval=FALSE}
extern "C" {
    void trait_model(double *input,double *stat_to_return){
        // compute output and fill the array stat_to_return
    }
}
```

- Build your code into a binary library (.so under Linux or .dll under Windows) with the `R CMD SHLIB` command.
In our example, the command for compiling the trait model and the given output are:
```
$ R CMD SHLIB trait_model_rc.cpp
g++ -I/usr/share/R/include -DNDEBUG -fpic -O2 -pipe -g -c trait_model_rc.cpp
-o trait_model_rc.o
g++ -shared -o trait_model_rc.so trait_model_rc.o -L/usr/lib/R/lib -lR
```
- Load the builded library in your session with the `dyn.load` function.
```
> dyn.load("trait_model_rc.so")
```
- Use the `.C` function for calling your program, like we've done in our `trait_model` function:
```{r, eval=FALSE}
trait_model <- function(input=c(1,1,1,1,1,1)) {
    .C("trait_model",input=input,stat_to_return=array(0,4))$stat_to_return
}
```

Now, as our model will have two parameters with constant values (see examble [The trait model]), we can fix them as following:
```{r, eval=FALSE}
trait_model <- function(input=c(1,1,1,1,1,1)) {
    .C("trait_model",input=c(input[1], 500, input[2:3], 1, input[4:5]),
    stat_to_return=array(0,4))$stat_to_return
}
```

## Example of integration of an external program: `fastsimcoal`
This example is provided by an EasyABC user Albert Min-Shan Ko (currently at the Department of genetics, Max Planck Institute of Evolutionary Anthropology, Leipzig, Germany).
The purpose is to plug a third-party software related to population genetics into the EasyABC workflow.
This software needs input data in a given format, so the idea is to wrap the call to the `fastsimcoal` software into a script that will link EasyABC to `fastsimcoal`.

Here are the scripts as provided by courtesy of Albert Min-Shan Ko.

- First, a R script reformats the parameters to be used by `fastsimcoal` (here named `mod.input.r`).
```{r, eval=FALSE}
r<-read.table('input',head=F)
sink('mod.input')
cat(paste('1','p1','unif',round(r[1,],0),round(r[1,],0),sep='\t'))
cat('\n')
cat(paste('1','p2','unif',round(r[2,],0),round(r[2,],0),sep='\t'))
cat('\n')
cat(paste('1','p3','unif',round(r[3,],0),round(r[3,],0),sep='\t'))
sink()
```

- Second, a GNU Bash script (here names `run_sim.sh`) invokes the latter R script and builds a parameter file for `fastsimcoal` (`sim.est`), runs `fastsimcoal` and computes some summary statistics with the arlequin program.
```{bash, eval=FALSE}
#!/bin/bash
rm -fr sim
Rscript mod.input.r
cat <(sed -n 1p template.est) <(sed -n '1,3'p mod.input) \ 
<(sed -n '5,\$'p template.est) > sim.est
until [ -f sim/arl_output ]; do
./fastsimcoal -t sim.tpl -e sim.est -E1 -n1 -q
./arlsumstat sim/sim_1_1.arp sim/arl_output 1 0 run_silent
done
cat sim/arl_output > output
```

Then, the user can invoke EasyABC like this :
```{r, eval=FALSE}
prior=list(c("unif",500,1000),c("unif",100,500),c("unif",50,200))
ABC_sim<-ABC_rejection(model=binary_model('./run_sim.sh'),prior=prior,nb_simul=3)
```

## Example of integration of a java model
If your model runs with a Java Virtual Machine (can be written in Java, Scala, Groovy, …), you can of course use the `binary_model` wrapper to run the JVM within your model.
But, you can achieve a tighter integration that will simplify the process and save computing time.
This section propose to use the R package `rJava`.
Let's consider the toy model written in Java (in a file named Model.java):
```java
public class Model {
    public static double[] run(double[] x) {
        double[] result = new double[2];
        result[0] = x[0] + x[1];
        result[1] = x[0] * x[1];
        return result;
    }
}
```

We can compile it with the command: `javac Model.java` and then define our wrapper in R:
```{r, eval=FALSE}
mymodel <- function(x) {
    library("rJava")
    .jinit(classpath=".")
    result = .jcall(J("Model"),"[D","run",.jarray(x))
    result
}
```

Then, the user can invoke EasyABC like this :
```{r, eval=FALSE}
prior=list(c("unif",0,1),c("normal",1,2))
ABC_sim<-ABC_rejection(model=mymodel,prior=prior,nb_simul=3)
```

# A first worked example

## The toy model

We here consider a very simple stochastic model coded in the `R` language:
```{r toy_model}
toy_model<-function(x){
    c( x[1] + x[2] + rnorm(1,0,0.1) , x[1] * x[2] + rnorm(1,0,0.1) ) 
}
```

We will use two different types of prior distribution for the two model parameters ($x[1]$ and $x[2]$): a uniform distribution between 0 and 1 and a normal distribution with mean 1 and standard deviation 2.
```{r toy_prior}
toy_prior=list(c("unif",0,1),c("normal",1,2))
```

And we will consider an imaginary dataset of two summary statistics that the toy_model is aiming at fitting:
```{r sum_stat_obs}
sum_stat_obs=c(1.5,0.5)
```

## Performing a standard ABC-rejection procedure

A standard ABC-rejection procedure can be simply performed with the function `ABC_rejection`, in precising the number $n$ of simulations to be performed and the proportion of simulations which are to be retained $p$:
```{r ABC_rejection}
set.seed(1)
n=10
p=0.2
ABC_rej<-ABC_rejection(model=toy_model, prior=toy_prior, nb_simul=n,
summary_stat_target=sum_stat_obs, tol=p)
ABC_rej
```

Alternatively, `ABC_rejection` can be used to solely launch the simulations and to store the simulation outputs without performing the rejection step.
This option enables the user to make use of the `R` package `abc` (Csilléry et al. 2012) which offers an array of more sophisticated post-processing treatments than the simple rejection procedure:
```{r ABC_rejection2}
# Run the ABC rejection on the model
set.seed(1)
n=10
ABC_rej<-ABC_rejection(model=toy_model, prior=toy_prior, nb_simul=n)
ABC_rej
```

```{r abcinstall,eval=FALSE}
# Install if needed the "abc" package
install.packages("abc")
```

```{r abc}
# Post-process the simulations outputs
library(abc)
rej<-abc(sum_stat_obs, ABC_rej$param, ABC_rej$stats, tol=0.2, method="rejection")
# simulations selected:
rej$unadj.values
# their associated summary statistics:
rej$ss
# their normalized euclidean distance to the data summary statistics:
rej$dist
```

## Performing a sequential ABC scheme

Other functions of the `EasyABC` package are used in a very similar manner.
To perform the algorithm of Beaumont et al. (2009), one needs to specify the sequence of tolerance levels $tolerance\_tab$ and the number $nb\_simul$ of simulations to obtain below the tolerance level at each iteration:
```{r ABC_Beaumont}
n=10
tolerance=c(1.25,0.75)
ABC_Beaumont<-ABC_sequential(method="Beaumont", model=toy_model,
prior=toy_prior, nb_simul=n, summary_stat_target=sum_stat_obs,
tolerance_tab=tolerance)
ABC_Beaumont
```

To perform the algorithm of Drovandi and Pettitt (2011), one needs to specify four arguments: the initial number of simulations $nb\_simul$, the final tolerance level $tolerance\_tab$, the proportion $\alpha$ of best-fit simulations to update the tolerance level at each step, and the target proportion $c$ of unmoved particles during the MCMC jump.
Note that default values $alpha=0.5$ and $c=0.01$ are used if not specified, following Drovandi and Pettitt (2011).
```{r ABC_Drovandi}
n=10
tolerance=0.75
c_drov=0.7
ABC_Drovandi<-ABC_sequential(method="Drovandi", model=toy_model,
prior=toy_prior, nb_simul=n, summary_stat_target=sum_stat_obs,
tolerance_tab=tolerance, c=c_drov)
ABC_Drovandi
```

To perform the algorithm of Del Moral et al. (2012), one needs to specify five arguments: the initial number of simulations $nb\_simul$, the number $\alpha$ controlling the decrease in effective sample size of the particle set at each step, the number $M$ of simulations performed for each particle, the minimal effective sample size $nb\_threshold$ below which a resampling of particles is performed and the final tolerance level $tolerance\_target$.
Note that default values $alpha=0.5$, $M=1$ and $nb\_threshold=nb\_simul/2$ are used if not specified.
```{r ABC_Delmoral}
n=10
alpha_delmo=0.5
tolerance=0.75
ABC_Delmoral<-ABC_sequential(method="Delmoral", model=toy_model,
prior=toy_prior, nb_simul=n, summary_stat_target=sum_stat_obs,
alpha=alpha_delmo, tolerance_target=tolerance)
ABC_Delmoral
```

To perform the algorithm of Lenormand et al. (2012), one needs to specify three arguments: the initial number of simulations $nb\_simul$, the proportion $\alpha$ of best-fit simulations to update the tolerance level at each step, and the stopping criterion $p\_acc\_min$.
Note that default values $alpha=0.5$ and $p\_acc\_min=0.05$ are used if not specified, following Lenormand et al. (2012).
Also note that the method "Lenormand" is only supported with uniform prior distributions (since it performs a Latin Hypercube sampling at the beginning). Here, we therefore need to alter the prior distribution of the second model parameter:
```{r toy_prior2}
toy_prior2=list(c("unif",0,1),c("unif",0.5,1.5))
```

```{r ABC_Lenormand}
n=10
pacc=0.4
ABC_Lenormand<-ABC_sequential(method="Lenormand", model=toy_model,
prior=toy_prior2, nb_simul=10, summary_stat_target=sum_stat_obs,
p_acc_min=pacc)
ABC_Lenormand
```

## Performing a ABC-MCMC scheme

To perform the algorithm of Marjoram et al. (2003), one needs to specify five arguments: the number of sampled points $n\_rec$ in the Markov Chain, the number of chain points between two sampled points $n\_between\_sampling$, the maximal distance accepted between simulations and data $dist\_max$, a vector $tab\_normalization$ precising the scale of each summary statistics, and a vector $proposal\_range$ precising the maximal distances in each dimension of the parameter space for a jump of the MCMC.
All these arguments have default values (see the package help for the function `ABC_mcmc`), so that `ABC_mcmc` will work without user-defined values.
```{r ABC_Marjoram_original}
n=10
ABC_Marjoram_original<-ABC_mcmc(method="Marjoram_original", model=toy_model,
prior=toy_prior, summary_stat_target=sum_stat_obs, n_rec=n)
ABC_Marjoram_original
```

To perform the algorithm of Marjoram et al. (2003) in which some of the arguments ($dist\_max$, $tab\_normalization$ and $proposal\_range$) are automatically determined by the algorithm via an initial calibration step, one needs to specify three arguments: the number $n\_calibration$ of simulations to perform at the calibration step, the tolerance quantile $tolerance\_quantile$ to be used for the determination of $dist\_max$ and the scale factor $proposal\_phi$ to determine the proposal range.
These modifications are drawn from the algorithm of Wegmann et al. (2009a), without relying on PLS regressions.
The arguments are set by default to: $n\_calibration=10000$, $tolerance\_quantile=0.01$ and $proposal\_phi=1$.
This way of automatic determination of $dist\_max$, $tab\_normalization$ and $proposal\_range$ is strongly recommended, compared to the crude automatic determination proposed in the method `Marjoram_original`.
```{r ABC_Marjoram}
n=10
ABC_Marjoram<-ABC_mcmc(method="Marjoram", model=toy_model,
prior=toy_prior, summary_stat_target=sum_stat_obs, n_rec=n)
ABC_Marjoram
```

To perform the algorithm of Wegmann et al. (2009a), one needs to specify four arguments: the number $n\_calibration$ of simulations to perform at the calibration step, the tolerance quantile $tolerance\_quantile$ to be used for the determination of $dist\_max$, the scale factor $proposal\_phi$ to determine the proposal range and the number of components $numcomp$ to be used in PLS regressions.
The arguments are set by default to: $n\_calibration=10000$, $tolerance\_quantile=0.01$, $proposal\_phi=1$ and $numcomp=0$, this last default value encodes a choice of a number of PLS components equal to the number of summary statistics.
```{r ABC_Wegmann}
n=10
ABC_Wegmann<-ABC_mcmc(method="Wegmann", model=toy_model,
prior=toy_prior, summary_stat_target=sum_stat_obs, n_rec=n)
ABC_Wegmann
```

## Performing a SABC scheme

For the SABC algorithm by Albert et al. (2014) we need to provide the prior in the form of a sampler and a density:
```{r SABCPrior}
r.prior <- function()   c(runif(1,0,1),rnorm(1,1,2))
d.prior <- function(x)  dunif(x[1],0,1)*dnorm(x[2],1,2)
```

Furthermore, we need to specify the size of the ensemble, the number of simulations and the initial tolerance
```{r SABCParam}
n.sample <- 300
iter.max <- n.sample * 30
eps.init <- 2
```

Since, for this example, the prior carries relevant information, we choose the method "informative":
```{r SABC, include=FALSE}
ABC_Albert <-SABC(r.model = toy_model,
r.prior = r.prior,
d.prior = d.prior,
n.sample = n.sample,
eps.init = eps.init,
iter.max = iter.max,
method = "informative",
y = sum_stat_obs
)
```

An approximate posterior parameter sample is contained in `ABC_Albert\$E[,1:2]`, e.g.
```{r SABCPlot1}
plot(ABC_Albert$E[,1:2])
```

## Using multiple cores

The functions of the package `EasyABC` can launch the simulations on multiple cores of a computer: users have to indicate the number of cores they wish to use in the argument `n_cluster` of the functions, and they have to use the option `use_seed=TRUE`.
Users also need to design their code in a slightly different way so that it is compatible with the option `use_seed=TRUE` (see Section [The simulation code - for use with multiple cores] for additional details).
For the toy model above, the modifications needed are the following:
```{r toy_model_parallel}
toy_model_parallel<-function(x){
set.seed(x[1]) # so that each core is initialized with a different seed value.
c( x[2] + x[3] + rnorm(1,0,0.1) , x[2] * x[3] + rnorm(1,0,0.1) ) 
}
```

```{r ABC_rejection3}
set.seed(1)
n=10
p=0.2
ABC_rej<-ABC_rejection(model=toy_model_parallel, prior=toy_prior,
nb_simul=n, summary_stat_target=sum_stat_obs, tol=p, n_cluster=2,
use_seed=TRUE)
ABC_rej
```

# A second worked example

## The trait model

We turn now to a stochastic ecological model hereafter called `trait_model` to illustrate how to use `EasyABC` with models not initially coded in the `R` language.
`trait_model` represents the stochastic dynamics of an ecological community where each species is represented by a set of traits (i.e. characteristics) which determine its competitive ability.
A detailed description and analysis of the model can be found in Jabot (2010).
The model requires four parameters: an immigration rate $I$, and three additional parameters ($h$, $A$ and $\sigma$) describing the way traits determine species competitive ability.
The model additionnally requires two fixed variables: the total number of individuals in the local community $J$ and the number of traits used $n\_t$.

The model outputs four summary statistics: the species richness of the community $S$, its Shannon's index $H$, the mean of the trait value among individuals $MTV$ and the skewness of the trait value distribution $STV$.

*NB:* Three parameters ($I$, $A$ and $\sigma$) have non-uniform prior distributions: instead, their log-transformed values have a uniform prior distribution.
The simulation code `trait_model` therefore takes an exponential transform of the values proposed by `EasyABC` for these parameters at the beginning of each simulation.

In the following, we will use the values $J=500$ and $n\_t=1$, and uniform prior distributions for $ln(I)$ in $[3;5]$, $h$ in [-25;125], $ln(A)$ in $[ln(0.1);ln(5)]$ and $ln(\sigma)$ in $[ln(0.5);ln(25)]$. The simulation code `trait_model` reads sequentially $J$, $I$, $A$, $n\_t$, $h$ and $\sigma$.

*NB:* Note that the fixed variables $J$ and $n\_t$ have been fixed (see [this section for details](#rc_link)) into the function `trait_model`. But if it didn't, we would have included these constants in the prior list using uniform distributions with a trivial ranges, like `c("unif",500,500)` for example.

```{r trait_prior}
trait_prior=list(c("unif",3,5),c("unif",-2.3,1.6),
c("unif",-25,125), c("unif",-0.7,3.2))
```

We will consider an imaginary dataset whose summary statistics are $(S,H,MTV,STV) = (100,2.5,20,30000)$:
```{r sum_stat_obs2}
sum_stat_obs=c(100,2.5,20,30000)
```

## Performing a standard ABC-rejection procedure

A standard ABC-rejection procedure can be simply performed with the function `ABC_rejection`, in precising the number $n$ of simulations to be performed and the proportion $p$ of retained simulations.
Note that the option `use_seed=TRUE` is used, since `trait_model` requires a seed initializing value for its pseudo-random number generator:
```{r ABC_rejection4}
set.seed(1)
n=10
p=0.2
ABC_rej<-ABC_rejection(model=trait_model, prior=trait_prior, nb_simul=n,
summary_stat_target=sum_stat_obs, tol=p, use_seed=TRUE)
ABC_rej
```

Alternatively, `ABC_rejection` can be used to solely launch the simulations and to store the simulation outputs without performing the rejection step.
This option enables the user to make use of the `R` package `abc` (Csilléry et al. 2012) which offers an array of more sophisticated post-processing treatments than the simple rejection procedure:
```{r abcinstall2,eval=FALSE}
install.packages("abc")
```

```{r abc2}
library(abc)
set.seed(1)
n=10
p=0.2
ABC_rej<-ABC_rejection(model=trait_model, prior=trait_prior, nb_simul=n, use_seed=TRUE)
rej<-abc(sum_stat_obs, ABC_rej$param, ABC_rej$stats,
tol=0.2, method="rejection")
# simulations selected:
rej$unadj.values
# their associated summary statistics:
rej$ss
# their normalized euclidean distance to the data summary statistics:
rej$dist
```

Note that a simulation code `My_simulation_code` can be passed to the function `ABC_rejection` in several ways depending on its nature:

- if it is a `R` function \\
`ABC_rejection(My_simulation_code, prior, nb_simul,...)` 
-  if it is a binary executable file and a single core is used (see section [The simulation code - for use on a single core] for compatibility constraints)\\
`ABC_rejection(binary_model("./My_simulation_code"), prior, nb_simul, use_seed=TRUE,...)`
-  if it is a binary executable file and multiple cores are used (see section [The simulation code - for use with multiple cores] for compatibility constraints)\\
`ABC_rejection(binary_model_cluster("./My_simulation_code"), prior, nb_simul, n_cluster=2, use_seed=TRUE)`

## Performing a sequential ABC scheme

Other functions of the `EasyABC` package are used in a very similar manner.
To perform the algorithm of Beaumont et al. (2009), one needs to specify the sequence of tolerance levels $tolerance\_tab$ and the number $nb\_simul$ of simulations to obtain below the tolerance level at each iteration:
```{r ABC_Beaumont2}
n=10
tolerance=c(8,5)
ABC_Beaumont<-ABC_sequential(method="Beaumont", model=trait_model,
prior=trait_prior, nb_simul=n, summary_stat_target=sum_stat_obs,
tolerance_tab=tolerance, use_seed=TRUE)
ABC_Beaumont
```

To perform the algorithm of Drovandi and Pettitt (2011), one needs to specify four arguments: the initial number of simulations $nb\_simul$, the final tolerance level $tolerance\_tab$, the proportion $\alpha$ of best-fit simulations to update the tolerance level at each step, and the target proportion $c$ of unmoved particles during the MCMC jump.
Note that default values $alpha=0.5$ and $c=0.01$ are used if not specified, following Drovandi and Pettitt (2011).
```{r ABC_Drovandi2}
n=10
tolerance=3
c_drov=0.7
ABC_Drovandi<-ABC_sequential(method="Drovandi", model=trait_model,
prior=trait_prior, nb_simul=n, summary_stat_target=sum_stat_obs,
tolerance_tab=tolerance, c=c_drov, use_seed=TRUE)
ABC_Drovandi
```

To perform the algorithm of Del Moral et al. (2012), one needs to specify five arguments: the initial number of simulations $nb\_simul$, the number $\alpha$ controlling the decrease in effective sample size of the particle set at each step, the number $M$ of simulations performed for each particle, the minimal effective sample size $nb\_threshold$ below which a resampling of particles is performed and the final tolerance level $tolerance\_target$.
Note that default values $alpha=0.5$, $M=1$ and $nb\_threshold=nb\_simul/2$ are used if not specified.
```{r ABC_Delmoral2}
n=10
alpha_delmo=0.5
tolerance=3
ABC_Delmoral<-ABC_sequential(method="Delmoral", model=trait_model,
prior=trait_prior, nb_simul=n, summary_stat_target=sum_stat_obs,
alpha=alpha_delmo, tolerance_target=tolerance, use_seed=TRUE)
ABC_Delmoral
```

To perform the algorithm of Lenormand et al. (2012), one needs to specify three arguments: the initial number of simulations $nb\_simul$, the proportion $\alpha$ of best-fit simulations to update the tolerance level at each step, and the stopping criterion $p\_acc\_min$.
Note that default values $alpha=0.5$ and $p\_acc\_min=0.05$ are used if not specified, following Lenormand et al. (2012).
```{r ABC_Lenormand2}
n=10
pacc=0.4
ABC_Lenormand<-ABC_sequential(method="Lenormand", model=trait_model,
prior=trait_prior, nb_simul=n, summary_stat_target=sum_stat_obs,
p_acc_min=pacc, use_seed=TRUE)
ABC_Lenormand
```

## Performing a ABC-MCMC scheme

To perform the algorithm of Marjoram et al. (2003), one needs to specify five arguments: the number of sampled points $n\_obs$ in the Markov Chain, the number of chain points between two sampled points $n\_between\_sampling$, the maximal distance accepted between simulations and data $dist\_max$, a vector $tab\_normalization$ precising the scale of each summary statistics, and a vector $proposal\_range$ precising the maximal distances in each dimension of the parameter space for a jump of the MCMC.
All these arguments have default values (see the package help for the function `ABC_mcmc`), so that `ABC_mcmc` will work without user-defined values.
```{r ABC_Marjoram_original2}
n=10
ABC_Marjoram_original<-ABC_mcmc(method="Marjoram_original", model=trait_model,
prior=trait_prior, summary_stat_target=sum_stat_obs, n_rec=n, use_seed=TRUE)
ABC_Marjoram_original
```

To perform the algorithm of Marjoram et al. (2003) in which some of the arguments ($dist\_max$, $tab\_normalization$ and $proposal\_range$) are automatically determined by the algorithm via an initial calibration step, one needs to specify three arguments: the number $n\_calibration$ of simulations to perform at the calibration step, the tolerance quantile $tolerance\_quantile$ to be used for the determination of $dist\_max$ and the scale factor $proposal\_phi$ to determine the proposal range.
These modifications are drawn from the algorithm of Wegmann et al. (2009a), without relying on PLS regressions.
The arguments are set by default to: $n\_calibration=10000$, $tolerance\_quantile=0.01$ and $proposal\_phi=1$.
This way of automatic determination of $dist\_max$, $tab\_normalization$ and $proposal\_range$ is strongly recommended, compared to the crude automatic determination proposed in the method `Marjoram_original`.
```{r ABC_Marjoram2}
n=10
n_calib=10
tol_quant=0.2 
ABC_Marjoram<-ABC_mcmc(method="Marjoram", model=trait_model, prior=trait_prior,
summary_stat_target=sum_stat_obs,
n_rec=n, n_calibration=n_calib, tolerance_quantile=tol_quant, use_seed=TRUE)
ABC_Marjoram
```

To perform the algorithm of Wegmann et al. (2009a), one needs to specify four arguments: the number $n\_calibration$ of simulations to perform at the calibration step, the tolerance quantile $tolerance\_quantile$ to be used for the determination of $dist\_max$, the scale factor $proposal\_phi$ to determine the proposal range and the number of components $numcomp$ to be used in PLS regressions.
The arguments are set by default to: $n\_calibration=10000$, $tolerance\_quantile=0.01$, $proposal\_phi=1$ and $numcomp=0$, this last default value encodes a choice of a number of PLS components equal to the number of summary statistics.
```{r ABC_Wegmann2}
n=10
n_calib=10
tol_quant=0.2 
ABC_Wegmann<-ABC_mcmc(method="Wegmann", model=trait_model, prior=trait_prior,
summary_stat_target=sum_stat_obs,
n_rec=n, n_calibration=n_calib, tolerance_quantile=tol_quant, use_seed=TRUE)
ABC_Wegmann
```

## Performing a SABC scheme

For the SABC algorithm by Albert et al. (2014) we need to provide the prior in the form of a sampler and a density:
```{r SABCPrior2}
r.prior <- function() c(runif(1,3,5), runif(1,-2.3,1.6),
    runif(1,-25,125),runif(1,-0.7,3.2),1)
d.prior <- function(x) dunif(x[1],3,5) * dunif(x[2],-2.3,1.6) *
    dunif(x[3],-25,125) * dunif(x[4],-0.7,3.2)
```

Furthermore, we need to specify the size of the ensemble, the number of simulations and the initial tolerance
```{r SABCParam2}
n.sample <- 30
iter.max <- n.sample * 30
eps.init <- 20
```

Since, for this example, the prior is flat, we choose the method "noninformative":
```{r SABC2, include=FALSE}
ABC_Albert <-SABC(r.model = trait_model,
    r.prior = r.prior,
    d.prior = d.prior,
    n.sample = n.sample,
    eps.init = eps.init,
    iter.max = iter.max,
    method = "noninformative",
    y = sum_stat_obs
    )
```

We may plot the marginals of the posterior, using, e.g.,
```{r SABCPlot2}
hist(ABC_Albert$E[,3],breaks=100)
```

## Using multiple cores

The functions of the package `EasyABC` can launch the simulations on multiple cores of a computer: users only have to indicate the number of cores they wish to use in the argument `n_cluster` of the functions.
The compatibility constraints of the simulation code are slightly different when using multiple cores: please refer to section [The simulation code - for use with multiple cores] for more information.

# Troubleshooting and development

Please send comments, suggestions and bug reports to nicolas.dumoulin@inrae.fr or franck.jabot@inrae.fr.
Any new development of more efficient ABC schemes that could be included in the package is particularly welcome.

# Programming Acknowledgements

The `EasyABC` package makes use of a number of `R` tools, among which:

- the `R` package `lhs` (Carnell 2012) for latin hypercube sampling.
- the `R` package `MASS` (Venables and Ripley 2002) for boxcox transformation.
- the `R` package `mnormt` (Genz and Azzalini 2012) for multivariate normal generation.
- the `R` package `pls` (Mevik and Wehrens 2011) for partial least square regression.
- the `R` script for the Wegmann et al. (2009a)'s algorithm drawn from the `ABCtoolbox` documentation (Wegmann et al. 2009b).
We thank Sylvie Huet, Albert Ko, Matteo Fasiolo and Wim Delva for their suggestions and inputs in the development of this version.

# References

Albert C., K\"unsch HR., Scheidegger A. (2014) A Simulated Annealing Approach to Approximate Bayes Computations. *Stat. Comput.*, 1--16, arXiv:1208.2157 [stat.CO].

Beaumont, M. A., Cornuet, J., Marin, J., and Robert, C. P. (2009) Adaptive approximate Bayesian computation. *Biometrika*,**96**, 983--990.

Carnell, R. (2012) lhs: Latin Hypercube Samples. R package version 0.10. https://doi.org/10.32614/CRAN.package.lhs

Csilléry, K., Fran\c cois, O., and Blum, M.G.B. (2012) abc: an r package for approximate bayesian computation (abc). *Methods in Ecology and Evolution*, **3**, 475--479.

Del Moral, P., Doucet, A., and Jasra, A. (2012) An adaptive sequential Monte Carlo method for approximate Bayesian computation. *Statistics and Computing*, **22**, 1009--1020.

Drovandi, C. C. and Pettitt, A. N. (2011) Estimation of parameters for macroparasite population evolution using approximate Bayesian computation. *Biometrics*, **67**, 225--233.

Fearnhead, P. and Prangle, D. (2012) Constructing summary statistics for approximate Bayesian computation: semiautomatic approximate Bayesian computation. *J.Roy. Stat. Soc.: Series B* **74.3**, 419-474.

Genz, A., and Azzalini, A. (2012) mnormt: The multivariate normal and t distributions. R package version 1.4-5. https://doi.org/10.32614/CRAN.package.mnormt

Jabot, F. (2010) A stochastic dispersal-limited trait-based model of community dynamics. *Journal of Theoretical Biology*, **262**, 650--661.

Lenormand, M., Jabot, F., Deffuant G. (2012) Adaptive approximate Bayesian computation for complex models. http://arxiv.org/pdf/1111.1308.pdf

Marjoram, P., Molitor, J., Plagnol, V. and Tavaré, S. (2003) Markov chain Monte Carlo without likelihoods. *PNAS*, **100**, 15324--15328.

Mevik, B.-H., and Wehrens, R. (2011) pls: Partial Least Squares and Principal Component regression. R package version 2.3-0. https://doi.org/10.32614/CRAN.package.pls

Pritchard, J.K., and M.T. Seielstad and A. Perez-Lezaun and M.W. Feldman (1999) Population growth of human Y chromosomes: a study of Y chromosome microsatellites. *Molecular Biology and Evolution*, **16**, 1791--1798.

Sisson, S.A., Fan, Y., and Tanaka, M.M. (2007) Sequential Monte Carlo without likelihoods. *PNAS*, **104**, 1760--1765.

Venables, W.N., and Ripley, B.D. (2002) Modern Applied Statistics with S. Fourth Edition. Springer, New York.

Wegmann, D., Leuenberger, C. and Excoffier, L. (2009a) Efficient approximate Bayesian computation coupled with Markov chain Monte Carlo without likelihood. *Genetics*, **182**, 1207-1218.

Wegmann, D., Leuenberger, C. and Excoffier, L. (2009b) Using ABCtoolbox. https://cmpg.unibe.ch/software/ABCtoolbox/ABCtoolbox_manual.pdf
