Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix deprecated functions #63

Merged
merged 1 commit into from
Jan 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Examples/Gaussian/Gaussian_Example.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ options = (Nsamples=2000, Nadapt=1000, delta=.8, Nd=Nd)
# Perform the benchmark
results = pbenchmark(samplers, simulateGaussian, Nreps; options...)

# replace 0s for log time
replace!(results[!,:gctime], 0.0=>eps())

# Save results
save(results, ProjDir)

Expand Down
4 changes: 2 additions & 2 deletions Models/Gaussian/Gaussian_Models.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@model AHMCGaussian(y, N) = begin
mu ~ Normal(0, 1)
sigma ~ Truncated(Cauchy(0, 5), 0, Inf)
sigma ~ Truncated(Cauchy(0, 5), 0.0, Inf)
y ~ MvNormal(Fill(mu, N), sigma) # use `Fill(mu, N)` is memory freiendly
end

Expand Down Expand Up @@ -40,7 +40,7 @@ function (problem::GaussianProb)(θ)
@unpack mu, sigma = θ
N = length(y)
logpdf(MvNormal(Fill(mu, N), sigma), y) + logpdf(Normal(0, 1), mu) +
logpdf(Truncated(Cauchy(0, 5), 0, Inf), sigma)
logpdf(Truncated(Cauchy(0, 5), 0.0, Inf), sigma)
end

# Define problem with data and inits.
Expand Down
5 changes: 3 additions & 2 deletions Models/Hierarchical_LNR/Hierarchical_LNR_Models.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ logpdf(d::LNR, data::Tuple) = logpdf(d, data...)
::Type{T1}=Array{Float64,2}) where {T,T1} = begin
Nr = 3
mu_g = T(undef,Nr)
sigma_g ~ Truncated(Cauchy(0, 1), 0, Inf)
sigma_g ~ Truncated(Cauchy(0, 1), 0.0, Inf)
mu_g ~ [Normal(0,3)]
mu = T1(undef,Nsub,Nr)
for s in 1:Nsub
for i in 1:Nr
mu[s,i] ~ Normal(mu_g[i], sigma_g)
end
end
sigma ~ Truncated(Cauchy(0, 1), 0, Inf)
sigma ~ Truncated(Cauchy(0, 1), 0.0, Inf)
for i in 1:N
data[i] ~ LNR(mu[idx[i],:], sigma, 0.0)
end
Expand Down Expand Up @@ -145,6 +145,7 @@ CmdStanConfig = Stanmodel(name="CmdStanLNR", model=CmdStanLNR, nchains=1,
@unpack v,A,k,tau=θ
d=LBA(ν=v, A=A, k=k, τ=tau)
minRT = minimum(x->x[2], data)

logpdf(d,data)+sum(logpdf.(TruncatedNormal(0, 3, 0, Inf), v)) +
logpdf(TruncatedNormal(.8, .4, 0, Inf),A)+logpdf(TruncatedNormal(.2, .3 ,0 ,Inf), k)+
logpdf(TruncatedNormal(.4, .1, 0, minRT), tau)
Expand Down
4 changes: 2 additions & 2 deletions Models/Hierarchical_Poisson/Hierarhical_Poisson_Models.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ end
@model AHMCpoisson(y, x, idx, N, Ns) = begin
a0 ~ Normal(0, 10)
a1 ~ Normal(0, 1)
a0_sig ~ Truncated(Cauchy(0, 1), 0, Inf)
a0_sig ~ Truncated(Cauchy(0, 1), 0.0, Inf)
a0s ~ MvNormal(zeros(Ns), a0_sig)
for i ∈ 1:N
λ = a0 + a0s[idx[i]] + a1 * x[i]
Expand Down Expand Up @@ -86,7 +86,7 @@ function (problem::PoissonProb)(θ)
@unpack y, x, idx, N, Ns = problem # extract the data
@unpack a0, a1, a0s, a0_sig = θ
LL = 0.0
LL += logpdf(Truncated(Cauchy(0, 1), 0, Inf), a0_sig)
LL += logpdf(Truncated(Cauchy(0, 1), 0.0, Inf), a0_sig)
LL += sum(logpdf(MvNormal(zeros(Ns), a0_sig), a0s))
LL += logpdf.(Normal(0, 10), a0)
LL += logpdf.(Normal(0, 1), a1)
Expand Down
14 changes: 7 additions & 7 deletions Models/LBA/LBA_Models.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
@model lbaModel(data, N, Nc, ::Type{T}=Float64) where {T} = begin
mn = minimum(x -> x[2], data)
tau ~ TruncatedNormal(0.4, 0.1, 0, mn)
A ~ TruncatedNormal(0.8, 0.4, 0, Inf)
k ~ TruncatedNormal(0.2, 0.3, 0, Inf)
tau ~ Truncated(Normal(0.4, 0.1), 0.0, mn)
A ~ Truncated(Normal(0.8, 0.4), 0.0, Inf)
k ~ Truncated(Normal(0.2, 0.3), 0.0, Inf)
v = Vector{T}(undef, Nc)
for i in 1:Nc
v[i] ~ TruncatedNormal(0, 3, 0, Inf)
v[i] ~ Truncated(Normal(0, 3), 0.0, Inf)
end
data ~ [LBA(; ν=v, τ=tau, A=A, k=k)]
end
Expand Down Expand Up @@ -156,9 +156,9 @@ CmdStanConfig = Stanmodel(name="CmdStanLBA", model=CmdStanLBA, nchains=1,
@unpack v,A,k,tau=θ
d=LBA(ν=v, A=A, k=k, τ=tau)
minRT = minimum(x->x[2], data)
logpdf(d,data)+sum(logpdf.(TruncatedNormal(0, 3, 0, Inf), v)) +
logpdf(TruncatedNormal(.8, .4, 0, Inf),A)+logpdf(TruncatedNormal(.2, .3 ,0 ,Inf), k)+
logpdf(TruncatedNormal(.4, .1, 0, minRT), tau)
logpdf(d,data)+sum(logpdf.(Truncated(Normal(0, 3), 0.0, Inf), v)) +
logpdf(Truncated(Normal(.8, .4), 0.0, Inf),A)+logpdf(Truncated(Normal(.2, .3) ,0.0 ,Inf), k)+
logpdf(Truncated(Normal(.4, .1), 0.0, minRT), tau)
end

function sampleDHMC(choice, rt, N, Nc, nsamples)
Expand Down
4 changes: 2 additions & 2 deletions Models/Linear_Regression/Linear_Regression_Models.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@model AHMCregression(x, y, Nd, Nc) = begin
B ~ MvNormal(zeros(Nc), 10)
B0 ~ Normal(0, 10)
sigma ~ Truncated(Cauchy(0, 5), 0, Inf)
sigma ~ Truncated(Cauchy(0, 5), 0.0, Inf)
mu = B0 .+ x * B
y ~ MvNormal(mu, sigma)
end
Expand Down Expand Up @@ -46,7 +46,7 @@ CmdStanConfig = Stanmodel(name="CmdStanRegression", model=CmdStanRegression, nch
@unpack B0,B,sigma = θ
μ = B0 .+x*B
logpdf(MvNormal(μ, sigma), y) + logpdf(Normal(0, 10), B0) +
loglikelihood(Normal(0, 10), B) + logpdf(Truncated(Cauchy(0, 5), 0, Inf), sigma)
loglikelihood(Normal(0, 10), B) + logpdf(Truncated(Cauchy(0, 5), 0.0, Inf), sigma)
end

# Define problem with data and inits.
Expand Down
64 changes: 32 additions & 32 deletions src/core_benchmarking.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ mutable struct DHMCNUTS{T1,T2} <: MCMCSampler
autodiff::Symbol
end

DHMCNUTS(model) = DHMCNUTS(model,0,:DHMCNUTS,:forward)
DHMCNUTS(model) = DHMCNUTS(model, 0, :DHMCNUTS, :forward)

"""
Primary function that performs mcmc benchmark repeatedly on a set of samplers
Expand All @@ -66,29 +66,29 @@ and records the results.
* `kwargs`: optional keyword arguments that are passed to modifyConfig!, updateResults! and
runSampler, providing flexibility in benchmark simulations.
"""
function benchmark!(samplers, results, csr̂, simulate, Nreps, chains;kwargs...)
function benchmark!(samplers, results, csr̂, simulate, Nreps, chains; kwargs...)
for rep in 1:Nreps
data = simulate(;kwargs...)
schains = Chains[]
for s in samplers
modifyConfig!(s;kwargs...)
modifyConfig!(s; kwargs...)
println("\nSampler: $(typeof(s))")
println("Simulation: $simulate")
println("Repetition: $rep of $Nreps\n")
foreach(x->println(x),kwargs)
performance = @timed runSampler(s, data;kwargs...)
performance = @timed runSampler(s, data; kwargs...)
push!(schains,performance[1])
allowmissing!(results)
results = updateResults!(s, performance, results;kwargs...)
savechain!(s ,chains, performance;kwargs...)
results = updateResults!(s, performance, results; kwargs...)
savechain!(s ,chains, performance; kwargs...)
end
csr̂=cross_samplerRhat!(schains, csr̂;kwargs...)
csr̂=cross_samplerRhat!(schains, csr̂; kwargs...)
end
return results,csr̂
end

function benchmark!(sampler::T, results, csr̂, simulate, Nreps, chains;kwargs...) where {T<:MCMCSampler}
return benchmark!((sampler, ), results, simulate, Nreps, chains;kwargs...)
function benchmark!(sampler::T, results, csr̂, simulate, Nreps, chains; kwargs...) where {T<:MCMCSampler}
return benchmark!((sampler, ), results, simulate, Nreps, chains; kwargs...)
end

"""
Expand All @@ -98,11 +98,11 @@ Runs the benchmarking procedure and returns the results
* `simulate`: model simulation function with keyword Nd
* `Nreps`: number of times the benchmark is repeated for each factor combination
"""
function benchmark(samplers, simulate, Nreps, chains=();kwargs...)
function benchmark(samplers, simulate, Nreps, chains=(); kwargs...)
results = DataFrame()
csr̂ = DataFrame()
for p in Permutation(kwargs)
results,csr̂=benchmark!(samplers, results, csr̂, simulate, Nreps, chains;p...)
results,csr̂=benchmark!(samplers, results, csr̂, simulate, Nreps, chains; p...)
end
return [results csr̂]
end
Expand All @@ -115,11 +115,11 @@ Runs the benchmarking procedure and returns the results
* `simulate`: model simulation function with keyword Nd
* `Nreps`: number of times the benchmark is repeated for each factor combination
"""
function pbenchmark(samplers, simulate, Nreps;kwargs...)
compile(samplers, simulate;kwargs...)
pfun(rep) = benchmark(samplers, simulate, rep, chains;kwargs...)
function pbenchmark(samplers, simulate, Nreps; kwargs...)
compile(samplers, simulate; kwargs...)
pfun(rep) = benchmark(samplers, simulate, rep, chains; kwargs...)
reps = setreps(Nreps)
presults = pmap(rep->pfun(rep),reps)
presults = pmap(rep->pfun(rep), reps)
return vcat(presults..., cols=:union)
end

Expand All @@ -130,13 +130,13 @@ parameter estimation
* `data`: data for benchmarking
* `Nchains`: number of chains ran serially. Default = 1
"""
function runSampler(s::AHMCNUTS, data;Nchains=1, kwargs...)
function runSampler(s::AHMCNUTS, data; Nchains=1, kwargs...)
f() = sample(s.model(data...), s.config, s.Nsamples; discard_adapt=false,
progress=false)
return reduce(chainscat, map(x->f(), 1:Nchains))
end

function runSampler(s::CmdStanNUTS, data;Nchains=1, kwargs...)
function runSampler(s::CmdStanNUTS, data; Nchains=1, kwargs...)
f() = stan(s.model, toDict(data), summary=false, s.dir)[2]
return reduce(chainscat, map(x->f(), 1:Nchains))
end
Expand All @@ -152,63 +152,63 @@ Update the results DataFrame on each iteration
* `performance`: includes MCMC Chain, execution time, and memory measurements
* `results`: DataFrame containing benchmark results
"""
function updateResults!(s::AHMCNUTS, performance, results;kwargs...)
function updateResults!(s::AHMCNUTS, performance, results; kwargs...)
chain = performance[1]
newDF = DataFrame()
chain=removeBurnin(chain;kwargs...)
chain=removeBurnin(chain; kwargs...)
df = MCMCChains.describe(chain)[1].df
addChainSummary!(newDF, chain,df,:ess)
addChainSummary!(newDF, chain,df,:r_hat)
addESStime!(newDF, chain, df, performance)
addHPD!(newDF, chain)
addMeans!(newDF, df)
permutecols!(newDF, sort!(names(newDF)))#ensure correct order
select!(newDF, sort!(names(newDF)))#ensure correct order
dfi=MCMCChains.describe(chain, sections=[:internals])[1]
newDF[!,:epsilon] = [dfi[:step_size,:mean][1]]
newDF[!,:tree_depth] = [dfi[:tree_depth, :mean][1]]
addPerformance!(newDF,performance)
newDF[!,:sampler] = [s.name]
addKW!(newDF;kwargs...)
addKW!(newDF; kwargs...)
return vcat(results, newDF, cols=:union)
end

function updateResults!(s::CmdStanNUTS, performance, results;kwargs...)
function updateResults!(s::CmdStanNUTS, performance, results; kwargs...)
chain = performance[1]
newDF = DataFrame()
chain=removeBurnin(chain;kwargs...)
chain=removeBurnin(chain; kwargs...)
df = MCMCChains.describe(chain)[1].df
addChainSummary!(newDF, chain, df, :ess)
addChainSummary!(newDF,chain,df,:r_hat)
addESStime!(newDF, chain, df, performance)
addHPD!(newDF, chain)
addMeans!(newDF, df)
permutecols!(newDF, sort!(names(newDF)))#ensure correct order
select!(newDF, sort!(names(newDF)))#ensure correct order
dfi=MCMCChains.describe(chain, sections=[:internals])[1]
newDF[!,:epsilon] = [dfi[:stepsize__, :mean][1]]
newDF[!,:tree_depth] = [dfi[:treedepth__, :mean][1]]
addPerformance!(newDF, performance)
newDF[!,:sampler] = [s.name]
addKW!(newDF;kwargs...)
addKW!(newDF; kwargs...)
return vcat(results, newDF, cols=:union)
end

function updateResults!(s::DHMCNUTS, performance, results;kwargs...)
function updateResults!(s::DHMCNUTS, performance, results; kwargs...)
chain = performance[1]
newDF = DataFrame()
chain = removeBurnin(chain;kwargs...)
chain = removeBurnin(chain; kwargs...)
df = MCMCChains.describe(chain)[1].df
addChainSummary!(newDF, chain, df, :ess)
addChainSummary!(newDF, chain, df, :r_hat)
addESStime!(newDF, chain, df, performance)
addHPD!(newDF, chain)
addMeans!(newDF, df)
permutecols!(newDF, sort!(names(newDF)))#ensure correct order
select!(newDF, sort!(names(newDF)))#ensure correct order
dfi=MCMCChains.describe(chain, sections=[:internals])[1]
newDF[!,:epsilon] = [dfi[:lf_eps, :mean][1]]
newDF[!,:tree_depth] = [dfi[:tree_depth, :mean][1]]
addPerformance!(newDF,performance)
newDF[!,:sampler] = [s.name]
addKW!(newDF;kwargs...)
addKW!(newDF; kwargs...)
return vcat(results, newDF, cols=:union)
end

Expand All @@ -220,13 +220,13 @@ acceptance rate and others depending on the specific sampler.
* `Nadapt`: number of adaption samples during warm up
* `delta`: target acceptance rate.
"""
function modifyConfig!(s::AHMCNUTS;Nsamples, Nadapt, delta, autodiff=:forward, kwargs...)
function modifyConfig!(s::AHMCNUTS; Nsamples, Nadapt, delta, autodiff=:forward, kwargs...)
s.config = Turing.NUTS(Nadapt, delta)
s.Nsamples = Nsamples
s.autodiff = autodiff == :reverse ? Turing.setadbackend(:reverse_diff) : Turing.setadbackend(:forward_diff)
end

function modifyConfig!(s::CmdStanNUTS;Nsamples, Nadapt, delta, kwargs...)
function modifyConfig!(s::CmdStanNUTS; Nsamples, Nadapt, delta, kwargs...)
s.model.num_samples = Nsamples-Nadapt
s.model.num_warmup = Nadapt
s.model.method.adapt.delta = delta
Expand All @@ -238,7 +238,7 @@ function modifyConfig!(s::CmdStanNUTS;Nsamples, Nadapt, delta, kwargs...)
end
end

function modifyConfig!(s::DHMCNUTS;Nsamples, autodiff=:forward, kwargs...)
function modifyConfig!(s::DHMCNUTS; Nsamples, autodiff=:forward, kwargs...)
s.Nsamples = Nsamples
s.autodiff = autodiff == :reverse ? :ReverseDiff : :ForwardDiff
end
Loading