I am running multiple simulations with Turing and would like to extract some of the summary statistics from each chain. Below, I have created a simple example of what I am trying to achieve. For some reason, I cannot get the summary statistics for each chain. Sometimes it fails on the first chain. Other times it fails on the 9th or 10th. It seems like the summary information is not being computed consistently.
function getSummary(ch)
chain = ch[1001:end,:,:]
return chain.info.hashedsummary.x[2].summaries[1].value
end
using Turing
N=50
θ = .6
k = rand(Binomial(N,θ))
@model model(N,k) = begin
θ ~ Beta(5,5)
k~ Binomial(N,θ)
end
Nsamples = 2000
Nadapt = 1000
δ = .85
specs = NUTS(Nsamples,Nadapt,δ)
chain = map(x->sample(model(N,k),specs),1:20)
x = []
for (i,v) in enumerate(chain)
println(i)
push!(x,getSummary(v))
end
Error message:
1
2
3
4
5
6
7
8
9
BoundsError: attempt to access 0-element Array{MCMCChains.ChainSummary,1} at index [1]
getindex at array.jl:729 [inlined]
getSummary(::Chains{Union{Missing, Float64},Float64,NamedTuple{(:internals, :parameters),Tuple{Array{String,1},Array{String,1}}},NamedTuple{(:hashedsummary,),Tuple{Base.RefValue{Tuple{UInt64,MCMCChains.ChainSummaries}}}}}) at untitled-30c8ef2a1c6a84d0135df67b323151eb:3
top-level scope at untitled-30c8ef2a1c6a84d0135df67b323151eb:23 [inlined]
top-level scope at none:0
However, the following works:
chain[9]
Object of type Chains, with data of type 2000×7×1 Array{Union{Missing, Float64},3}
Log evidence = 0.0
Iterations = 1:2000
Thinning interval = 1
Chains = 1
Samples per chain = 2000
internals = elapsed, epsilon, eval_num, lf_eps, lf_num, lp
parameters = θ
parameters
Mean SD Naive SE MCSE ESS
θ 0.6497 0.0635 0.0014 0.0026 603.6478