Fix how the joint MultivariateGaussian prior is set when reading it from a file
Currently, if trying to read in and use a MultivariateGaussian
prior from a file it fails. For example, a regular file and JSON file of such a distribution could be created with:
import bilby
names = ["m", "c"]
mus = [[-5.0, -5.0], [5.0, 5.0]] # means of the two modes
corrcoefs = [
[[1.0, -0.7], [-0.7, 1.0]],
[[1.0, 0.7], [0.7, 1.0]],
] # correlation coefficients of the two modes
sigmas = [[1.5, 1.5], [2.1, 2.1]] # standard deviations of the two modes
weights = [1.0, 3.0] # relative weights of each mode
nmodes = 2
mvg = bilby.core.prior.MultivariateGaussianDist(
names, nmodes=2, mus=mus, corrcoefs=corrcoefs, sigmas=sigmas, weights=weights
)
priors = bilby.core.prior.PriorDict()
priors["m"] = bilby.core.prior.MultivariateGaussian(mvg, "m")
priors["c"] = bilby.core.prior.MultivariateGaussian(mvg, "c")
# write out to file
priors.to_file(".", "mvgprior")
# write out to JSON file
priors.to_json(".", "mvgprior")
Now, if we try and read in the regular file:
from bilby.core.prior import PriorDict
p = PriorDict()
p.from_file("mvgprior.prior")
it gives a NameError: name 'MultivariateGaussianDist' is not defined
. If we try and read in from the JSON file, it works, but if we draw samples form the prior we see that they are incorrect:
from matplotlib import pyplot as plt
p = PriorDict.from_json("mvgprior_prior.json")
samps = priors.sample(10000) # original prior dict
plt.scatter(samps["m"], samps["c"], label="Original")
nsamps = p.sample(10000) # read-in prior
plt.scatter(nsamps["m"], nsamps["c"], alpha=0.1, label="read-in")
plt.legend()
The reason for this problem is that within the MultivariateGaussian
priors the dist
attribute for each should point to the same object, but when read in from the JSON file it points to two different objects.
The MR fixes both the issue with not reading the regular file and the above one when reading from the JSON file.
Edited by Matthew Pitkin