Correlated Gaussian
This example will explore a highly-correlated Gaussian using Models.CorrelatedGaussian
. This model uses a conjuage Gaussian prior, see the docstring for the mathematical definition.
Setup
For this example, you'll need to add the following packages
julia>]add Distributions MCMCChains Measurements NestedSamplers StatsBase StatsPlots
Define model
using NestedSamplers
# set up a 4-dimensional Gaussian
D = 4
model, logz = Models.CorrelatedGaussian(D)
let's take a look at a couple of parameters to see what the likelihood surface looks like
using StatsPlots
θ1 = range(-1, 1, length=1000)
θ2 = range(-1, 1, length=1000)
logf = [model.loglike([t1, t2, 0, 0]) for t2 in θ2, t1 in θ1]
heatmap(
θ1, θ2, exp.(logf),
aspect_ratio=1,
xlims=extrema(θ1),
ylims=extrema(θ2),
xlabel="θ1",
ylabel="θ2"
)
Sample
using MCMCChains
using StatsBase
# using single Ellipsoid for bounds
# using Gibbs-style slicing for proposing new points
sampler = Nested(D, 50D;
bounds=Bounds.Ellipsoid,
proposal=Proposals.Slice()
)
names = ["θ_$i" for i in 1:D]
chain, state = sample(model, sampler; dlogz=0.01, param_names=names)
# resample chain using statistical weights
chain_resampled = sample(chain, Weights(vec(chain[:weights])), length(chain));
Results
chain_resampled
Chains MCMC chain (2350×5×1 Array{Float64, 3}):
Iterations = 1:2350
Number of chains = 1
Samples per chain = 2350
parameters = θ_4, θ_1, θ_2, θ_3
internals = weights
Summary Statistics
parameters mean std naive_se mcse ess rhat
Symbol Float64 Float64 Float64 Float64 Float64 Float64
θ_1 1.5516 0.5344 0.0110 0.0134 2200.7829 1.0010
θ_2 1.5807 0.5107 0.0105 0.0103 2273.9140 1.0012
θ_3 1.5702 0.5353 0.0110 0.0128 2151.7091 1.0025
θ_4 1.5758 0.5044 0.0104 0.0128 2163.6311 1.0027
Quantiles
parameters 2.5% 25.0% 50.0% 75.0% 97.5%
Symbol Float64 Float64 Float64 Float64 Float64
θ_1 0.5364 1.1896 1.5638 1.9298 2.5273
θ_2 0.5894 1.2055 1.5788 1.9714 2.4899
θ_3 0.4824 1.2044 1.5810 1.9622 2.5383
θ_4 0.5462 1.2371 1.5836 1.9230 2.5612
corner(chain_resampled)
using Measurements
logz_est = state.logz ± state.logzerr
diff = logz_est - logz
print("logz: ", logz, "\nestimate: ", logz_est, "\ndiff: ", diff)