bat / BAT.jl

A Bayesian Analysis Toolkit in Julia
Other
212 stars 30 forks source link

Order of Samples not preserved in IO via HDF5.jl #355

Closed lmh91 closed 2 years ago

lmh91 commented 2 years ago

The example from the test set "hdf5" fails, if the order of the parameters in the prior is not alphabetically sorted.

MWE

I just changed a and b in example_posterior:


using Test
using BAT
using StableRNGs
using Distributions
using ValueShapes

import HDF5

function BAT.example_posterior()
    rng = StableRNGs.StableRNG(0x4cf83495c736cac2)
    prior = NamedTupleDist(
        b = [4.2, 3.3], # switched with `a`
        a = Exponential(),  
        c = Normal(1, 3),
        d = [Weibull(), Weibull()],
        e = Beta(),
        f = MvNormal([0.3, -2.9], [1.7 0.5; 0.5 2.3])
    )
    n = totalndof(varshape(prior))
    A = randn(rng, n, n)
    likelihood = varshape(prior)(MvNormal(A * A'))
    PosteriorDensity(likelihood, prior)
end

mktempdir() do tmp_datadir
    results_filename = joinpath(tmp_datadir, "results.hdf5")
    samples = bat_sample(BAT.example_posterior(), MCMCSampling(nsteps = 1000, strict = false)).result
    bat_write(results_filename, samples)
    samples2 = bat_read(results_filename).result
    @test samples == samples2
end