Skip to content

Commit

Permalink
Merge pull request #63 from itsdfish/auto-juliaformatter-pr
Browse files Browse the repository at this point in the history
Automatic JuliaFormatter.jl run
  • Loading branch information
itsdfish authored Mar 25, 2024
2 parents 844fb50 + d1a8a0e commit 73dde6b
Show file tree
Hide file tree
Showing 31 changed files with 625 additions and 602 deletions.
20 changes: 10 additions & 10 deletions Examples/Binomial_ABC.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ prior_loglike(θ) = logpdf(Beta(1, 1), θ)

sample_prior() = rand(Beta(1, 1))

bounds = ((0,1),)
bounds = ((0, 1),)
names = (,)

N = 10
k = rand(Binomial(N, .5))
data = (N = N,k = k)
k = rand(Binomial(N, 0.5))
data = (N = N, k = k)

function loglike(data, θ)
(;N,k) = data
(; N, k) = data
n_sim = 10^4
counter(_) = rand(Binomial(N, θ)) == k ? 1 : 0
cnt = mapreduce(counter, +, 1:n_sim)
Expand All @@ -23,14 +23,14 @@ end

# loglike(θ, data) = logpdf(Binomial(data.N, θ), data.k)

model = DEModel(;
sample_prior,
prior_loglike,
loglike,
model = DEModel(;
sample_prior,
prior_loglike,
loglike,
data,
names
)

de = DE(;sample_prior, bounds, burnin=1000, Np=3, σ=.01)
de = DE(; sample_prior, bounds, burnin = 1000, Np = 3, σ = 0.01)
n_iter = 2000
chains = sample(model, de, MCMCThreads(), n_iter, progress=true)
chains = sample(model, de, MCMCThreads(), n_iter, progress = true)
21 changes: 9 additions & 12 deletions Examples/Discrete_Example.jl
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
# Note: poor performance



# this may not be working properly


cd(@__DIR__)
using DifferentialEvolutionMCMC, Random, StatsBase, Distributions

N = 30
# Parameters for each cluster, we assume that each cluster is Gaussian distributed in the example.
μs = [-3.5, 0.0]
# Construct the data points.
data = mapreduce(c -> rand(MvNormal([μs[c], μs[c]], 1.), N), hcat, 1:2)
data = mapreduce(c -> rand(MvNormal([μs[c], μs[c]], 1.0), N), hcat, 1:2)

priors = (
idx = (Categorical([.5,.5]),N),
μs = (Normal(0, 1),2),
idx = (Categorical([0.5, 0.5]), N),
μs = (Normal(0, 1), 2)
)

bounds = ((1,2),(0.0,Inf))
bounds = ((1, 2), (0.0, Inf))

function loglike(data, idx, μs)
LL = 0.0
for (i,id) in enumerate(idx)
LL += logpdf(MvNormal([μs[id], μs[id]], 1.0), data[:,i])
for (i, id) in enumerate(idx)
LL += logpdf(MvNormal([μs[id], μs[id]], 1.0), data[:, i])
end
return LL
end

model = DEModel(priors=priors, model=loglike, data=data)
model = DEModel(priors = priors, model = loglike, data = data)

de = DE(bounds=bounds, burnin=1000, priors=priors, Np=15)
de = DE(bounds = bounds, burnin = 1000, priors = priors, Np = 15)
n_iter = 2000
@elapsed chains = sample(model, de, n_iter, progress=true)
@elapsed chains = sample(model, de, n_iter, progress = true)
18 changes: 9 additions & 9 deletions Examples/Gaussian_Example.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ end
function sample_prior()
μ = rand(Normal(0, 1))
σ = rand(truncated(Cauchy(0, 1), 0, Inf))
return [μ,σ]
return [μ, σ]
end

# likelihood function
Expand All @@ -34,24 +34,24 @@ data = rand(Normal(0.0, 1.0), 50)
# configure sampler
###################################################################################
# parameter names
names = (,)
names = (, )
# parameter bounds
bounds = ((-Inf,Inf),(0.0,Inf))
bounds = ((-Inf, Inf), (0.0, Inf))

# model object
model = DEModel(;
sample_prior,
prior_loglike,
loglike,
model = DEModel(;
sample_prior,
prior_loglike,
loglike,
data,
names
)

# DEMCMC sampler object
de = DE(;sample_prior, bounds, burnin = 1000, Np = 6)
de = DE(; sample_prior, bounds, burnin = 1000, Np = 6)
# number of interations per particle
n_iter = 2000
###################################################################################
# estimate parameters
###################################################################################
chains = sample(model, de, MCMCThreads(), n_iter, progress=true)
chains = sample(model, de, MCMCThreads(), n_iter, progress = true)
19 changes: 9 additions & 10 deletions Examples/Guassian_Example_Vector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ end
function sample_prior()
μ = rand(Normal(0, 1))
σ = rand(truncated(Cauchy(0, 1), 0, Inf))
return [μ,σ]
return [μ, σ]
end

bounds = ((-Inf,Inf),(0.0,Inf))
bounds = ((-Inf, Inf), (0.0, Inf))

data = rand(Normal(0.0, 1.0), 50)

Expand All @@ -25,17 +25,16 @@ function loglike(data, θ...)
return sum(logpdf.(Normal(μ, σ), data))
end

names = (,)
names = (, )

model = DEModel(;
sample_prior,
prior_loglike,
loglike,
model = DEModel(;
sample_prior,
prior_loglike,
loglike,
data,
names
)


de = DE(;sample_prior, bounds, burnin=1000, Np=6)
de = DE(; sample_prior, bounds, burnin = 1000, Np = 6)
n_iter = 2000
chains = sample(model, de, MCMCThreads(), n_iter, progress=true)
chains = sample(model, de, MCMCThreads(), n_iter, progress = true)
43 changes: 21 additions & 22 deletions Examples/Hierarchical_Example.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ end
# likelihood function
function loglike(data, μβ0, σβ0, β0, σ)
LL = 0.0
for s in 1:length(data)
μ = μβ0 + β0[s]
for s = 1:length(data)
μ = μβ0 + β0[s]
y = data[s] .- μ
LL += sum(logpdf.(Normal(0, σ), y))
end
Expand All @@ -55,29 +55,29 @@ n_subj = 50
σβ0 = 1.0
# subject intercept deviations
β0 = rand(Normal(0.0, σβ0), n_subj)
σ = .5
σ = 0.5

sim(μβ0, β0, σ, n_data) = rand(Normal(μβ0 + β0, σ), n_data)
data = [sim(μβ0, β0[i], σ, n_data) for i in 1:n_subj]
data = [sim(μβ0, β0[i], σ, n_data) for i = 1:n_subj]
###################################################################################
# configure sampler
###################################################################################
# parameter bounds
bounds = (
(-Inf,Inf),
(0.0,Inf),
(-Inf,Inf),
(0.0,Inf),
(-Inf, Inf),
(0.0, Inf),
(-Inf, Inf),
(0.0, Inf)
)

# parameter names
names = (:μβ0, :σβ0, :β0, )

# model object
model = DEModel(;
sample_prior,
prior_loglike,
loglike,
sample_prior,
prior_loglike,
loglike,
data,
names
)
Expand All @@ -86,8 +86,8 @@ model = DEModel(;
# update hyper parameters first
# update lower level parameters second
blocks = [
[true,true,fill(false, n_subj),true],
[false,false,fill(true, n_subj),false],
[true, true, fill(false, n_subj), true],
[false, false, fill(true, n_subj), false]
]
blocks = as_union.(blocks)

Expand All @@ -97,36 +97,35 @@ blocks = as_union.(blocks)
# push!(blocks, subj_blocks...)
# blocks = as_union.(blocks)


# use block updating on each iteration
blocking_on = x -> true

# sampler object
de = DE(;
sample_prior,
bounds,
bounds,
sample = resample,
burnin = 20_000,
burnin = 20_000,
n_initial = (n_subj + 1) * 4,
Np = 3,
n_groups = 2,
θsnooker = 0.1,
blocking_on,
blocks,
blocks
#generate_proposal = variable_gamma
)
###################################################################################
# estimate parameters
###################################################################################
n_iter = 40_000
chains = sample(model, de, MCMCThreads(), n_iter, progress=true)
chains = sample(model, de, MCMCThreads(), n_iter, progress = true)
###################################################################################
# Stan
###################################################################################
using StanSample
tmpdir = pwd() * "/tmp"

idx = repeat(1:n_subj, inner=n_data)
idx = repeat(1:n_subj, inner = n_data)
y = vcat(data...)
#xrep = repeat(x, inner=n_subj)
stan_data = Dict(
Expand All @@ -135,13 +134,13 @@ stan_data = Dict(
"n" => n_data * n_subj,
"idx" => idx,
"y" => y,
"x" => fill(0.0, n_data * n_subj),
"x" => fill(0.0, n_data * n_subj)
)
###################################################################################################
# Define Model
###################################################################################################
# load the Stan model file
stream = open("temp.stan","r")
stream = open("temp.stan", "r")
stan_model = read(stream, String)
close(stream)
stan_model = SampleModel("temp", stan_model, tmpdir)
Expand All @@ -159,4 +158,4 @@ stan_sample(
)
samples = read_samples(stan_model, :mcmcchains)
#rhats = filter(!isnan, MCMCChains.ess_rhat(samples).nt.rhat)
#mean(rhats .> 1.01)
#mean(rhats .> 1.01)
2 changes: 1 addition & 1 deletion Examples/KDE.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using KernelDensity, Distributions, Interpolations
import KernelDensity: kernel_dist
kernel_dist(::Type{Epanechnikov}, w::Float64) = Epanechnikov(0.0, w)
kernel(data) = kde(data; kernel=Epanechnikov)
kernel(data) = kde(data; kernel = Epanechnikov)
19 changes: 9 additions & 10 deletions Examples/KDE_Example.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,27 @@ end
function sample_prior()
μ = rand(Normal(0, 1))
σ = rand(truncated(Cauchy(0, 1), 0, Inf))
return [μ,σ]
return [μ, σ]
end

# parameter names
names = (,)
names = (, )
# parameter bounds
bounds = ((-Inf,Inf),(0.0,Inf))
bounds = ((-Inf, Inf), (0.0, Inf))

data = rand(Normal(0.0, 1.0), 50)


# model object
model = DEModel(;
sample_prior,
prior_loglike,
loglike,
model = DEModel(;
sample_prior,
prior_loglike,
loglike,
data,
names
)

# DEMCMC sampler object
de = DE(;sample_prior, bounds, burnin = 1000, Np = 6)
de = DE(; sample_prior, bounds, burnin = 1000, Np = 6)
# number of interations per particle
n_iter = 2000
chains = sample(model, de, MCMCThreads(), n_iter, progress=true)
chains = sample(model, de, MCMCThreads(), n_iter, progress = true)
22 changes: 11 additions & 11 deletions Examples/Multivariate_Guassian_Example.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ data = rand(MvNormal(μs, 1.0 * I), n_d)
function sample_prior()
μ = rand(Normal(0, 1), n_μ)
σ = rand(truncated(Cauchy(0, 1), 0, Inf))
return as_union([μ,σ])
return as_union([μ, σ])
end

# returns prior log likelihood
Expand All @@ -33,30 +33,30 @@ function loglike(data, μs, σ)
end

# upper and lower bounds of parameters
bounds = ((-Inf,Inf),(0.0,Inf))
bounds = ((-Inf, Inf), (0.0, Inf))
# parameter names
names = (,)
names = (, )

# model object
model = DEModel(;
sample_prior,
prior_loglike,
loglike,
model = DEModel(;
sample_prior,
prior_loglike,
loglike,
data,
names
)

# DEMCMC sampler
de = DE(;
sample_prior,
bounds,
bounds,
sample = resample,
burnin = 5000,
burnin = 5000,
n_initial = (n_μ + 1) * 4,
Np = 3,
n_groups = 1,
θsnooker = 0.1,
θsnooker = 0.1
)
# sample from the posterior distribution
n_iter = 50_000
chains = sample(model, de, MCMCThreads(), n_iter, progress=true)
chains = sample(model, de, MCMCThreads(), n_iter, progress = true)
Loading

0 comments on commit 73dde6b

Please sign in to comment.