-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes DE and refactor all algorithms (#6)
* refactor all algorithms * tune tolerances * finally fixed bias in ABCDE * tune examples * regen plot * change version
- Loading branch information
1 parent
2652cd3
commit 05a3c55
Showing
10 changed files
with
426 additions
and
425 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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,24 @@ | ||
|
||
|
||
""" | ||
ABC(plan, α_target; nparticles = 100, parallel = false) | ||
Classical ABC rejection algorithm. | ||
# Arguments: | ||
- `plan`: a plan built using the function ABCplan. | ||
- `α_target`: target acceptance rate for ABC rejection algorithm, `nparticles/α` will be sampled and only the best `nparticles` will be retained. | ||
- `nparticles`: number of samples from the approximate posterior that will be returned | ||
- `parallel`: when set to `true` multithreaded parallelism is enabled | ||
""" | ||
function ABC(plan::ABCplan, α_target; | ||
nparticles=100, parallel=false) | ||
@extract_params plan prior distance simulation data params | ||
@assert 0<α_target<=1 "α_target is the acceptance rate, and must be properly set between 0 - 1." | ||
simparticles=ceil(Int,nparticles/α_target) | ||
particles,distances=sample_plan(plan,simparticles,parallel) | ||
idx=sortperm(distances)[1:nparticles] | ||
(particles=particles[idx], | ||
distances=distances[idx], | ||
ϵ=distances[idx[end]]) | ||
end |
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,98 @@ | ||
""" | ||
deperturb(prior::Distribution, sample, r1, r2, γ) | ||
Function for `ABCDE` whose purpose is computing `sample + γ (r1 - r2) + ϵ` (the perturbation function of differential evolution) in a way suited to the prior. | ||
# Arguments: | ||
- `prior` | ||
- `sample` | ||
- `r1` | ||
- `r2` | ||
""" | ||
deperturb | ||
|
||
function deperturb(prior::Factored,sample,r1,r2,γ) | ||
deperturb.(prior.p,sample,r1,r2,γ) | ||
end | ||
|
||
function de_ϵ(sample,r1,r2,γ) | ||
γ*(abs(r1-r2)+abs(sample-r2)+abs(r1-sample))/100 | ||
end | ||
|
||
function deperturb(prior::ContinuousUnivariateDistribution,sample,r1,r2,γ) | ||
p = (r2-r1)*γ + randn()*de_ϵ(sample,r1,r2,γ) | ||
sample + p | ||
end | ||
|
||
function deperturb(prior::DiscreteUnivariateDistribution,sample::T,r1,r2,γ) where T | ||
p = (r2-r1)*γ + randn()*max(de_ϵ(sample,r1,r2,γ),0.5) | ||
sp=sign(p) | ||
ap=abs(p) | ||
intp=floor(ap) | ||
floatp=ap-intp | ||
pprob=(intp+ifelse(rand()>floatp,oftype(p,0),oftype(p,1)))*sp | ||
sample + round(T,pprob) | ||
end | ||
|
||
""" | ||
ABCDE(plan, ϵ_target; nparticles=100, generations=500, parallel=false, verbose=true) | ||
A sequential monte carlo algorithm inspired by differential evolution, very efficient (simpler version of B.M.Turner 2012, https://doi.org/10.1016/j.jmp.2012.06.004) | ||
# Arguments: | ||
- `plan`: a plan built using the function ABCplan. | ||
- `ϵ_target`: maximum acceptable distance between simulated datasets and the target dataset | ||
- `nparticles`: number of samples from the approximate posterior that will be returned | ||
- `generations`: total of simulations per particle | ||
- `parallel`: when set to `true` multithreaded parallelism is enabled | ||
- `verbose`: when set to `true` verbosity is enabled | ||
""" | ||
function ABCDE(plan::ABCplan, ϵ_target; nparticles=100, generations=500, parallel=false, verbose=true) | ||
@extract_params plan prior distance simulation data params | ||
θs,Δs=sample_plan(plan,nparticles,parallel) | ||
γ = 2.38/sqrt(2*length(prior)) | ||
iters=0 | ||
complete=1-sum(Δs.>ϵ_target)/nparticles | ||
while iters<generations | ||
iters+=1 | ||
nθs=identity.(θs) | ||
nΔs=identity.(Δs) | ||
@cthreads parallel for i in 1:nparticles | ||
s=i | ||
if Δs[i]>ϵ_target | ||
s=rand(1:nparticles) | ||
end | ||
a=s | ||
while a==s | ||
a=rand(1:nparticles) | ||
end | ||
b=a | ||
while b==a || b==s | ||
b=rand(1:nparticles) | ||
end | ||
θp=deperturb(prior,θs[s],θs[a],θs[b],γ) | ||
w_prior=pdf(prior,θp)/pdf(prior,θs[i]) | ||
rand() > min(1,w_prior) && continue | ||
xp=simulation(θp,params) | ||
dp=distance(xp,data) | ||
if dp <= max(ϵ_target, Δs[i]) | ||
nΔs[i]=dp | ||
nθs[i]=θp | ||
end | ||
end | ||
θs=nθs | ||
Δs=nΔs | ||
ncomplete=1-sum(Δs.>ϵ_target)/nparticles | ||
if verbose && (ncomplete!=complete || complete>=(nparticles-1)/nparticles) | ||
@info "Finished run:" completion=ncomplete nsim=iters*nparticles range_ϵ=extrema(Δs) | ||
end | ||
complete=ncomplete | ||
end | ||
conv=maximum(Δs)<=ϵ_target | ||
if verbose | ||
@info "End:" completion=complete converged=conv nsim=generations*nparticles range_ϵ=extrema(Δs) | ||
end | ||
θs,Δs,conv | ||
end | ||
|
||
export ABCDE |
Oops, something went wrong.