Skip to content
Snippets Groups Projects

Fix how the joint MultivariateGaussian prior is set when reading it from a file

Merged Matthew David Pitkin requested to merge matthew-pitkin/bilby:fix_mvg into master
All threads resolved!

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()

Figure_1

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 David Pitkin

Merge request reports

Pipeline #397476 passed

Pipeline passed for d98d6262 on matthew-pitkin:fix_mvg

Approved by

Merged by Colm TalbotColm Talbot 2 years ago (May 17, 2022 7:57pm UTC)

Merge details

  • Changes merged into master with 7e832e0b (commits were squashed).
  • Deleted the source branch.

Pipeline #398380 passed

Pipeline passed for 7e832e0b on master

Test coverage 62.00% from 0 jobs

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
  • Colm Talbot
  • Colm Talbot
  • added 1 commit

    • 03591c16 - dict.py: deepcopy dictionary

    Compare with previous version

  • added 1 commit

    • 123125ae - joint_test.py: add unit test for from_repr of a MultivariateGaussianDist

    Compare with previous version

  • added 1 commit

    • 300e0091 - dict.py: remove superfluous if statement

    Compare with previous version

  • Colm Talbot resolved all threads

    resolved all threads

  • Colm Talbot approved this merge request

    approved this merge request

  • added 1 commit

    • d98d6262 - dict.py: fix test failure due to deepcopy

    Compare with previous version

  • Sylvia Biscoveanu approved this merge request

    approved this merge request

  • Colm Talbot resolved all threads

    resolved all threads

  • Colm Talbot approved this merge request

    approved this merge request

  • merged

  • Colm Talbot mentioned in commit 7e832e0b

    mentioned in commit 7e832e0b

  • Please register or sign in to reply
    Loading