Skip to content

MAINT: tweaks to prior constraints#1096

Open
mj-will wants to merge 6 commits into
bilby-dev:mainfrom
mj-will:improve-prior-constraints
Open

MAINT: tweaks to prior constraints#1096
mj-will wants to merge 6 commits into
bilby-dev:mainfrom
mj-will:improve-prior-constraints

Conversation

@mj-will

@mj-will mj-will commented May 20, 2026

Copy link
Copy Markdown
Collaborator

Some tweaks to prior contraints

  • Raise an error if a constraint is present but the parameter key is missing. Currently, it's possible to have constraints that are silently ignored, e.g. because the conversion function doesn't generate them.
  • Remove an unreachable if statement. Prior to Change probability initialization to use np.ones_like #1028, evaluate_constraints returned 1.0 if there were no valid constraints and there was a check for this where the normalization was set to 1. I've removed this for now. Alternatively we could return 1.0 if all values are true but the while should do this anyway.

@mj-will mj-will added the priors label May 20, 2026
@mj-will mj-will added this to the 3.0.0 milestone May 20, 2026
@mj-will

mj-will commented Jun 22, 2026

Copy link
Copy Markdown
Collaborator Author

I think we may need to have an option where this doesn't raise an error since it is called in sample_subset_constrained and there are cases there where it shouldn't fail. E.g. if evaluating the usual GW prior and the masses aren't present.

@ColmTalbot

Copy link
Copy Markdown
Collaborator

I think we may need to have an option where this doesn't raise an error since it is called in sample_subset_constrained and there are cases there where it shouldn't fail. E.g. if evaluating the usual GW prior and the masses aren't present.

We could add this to the CBCPriorDict?

@mj-will mj-will force-pushed the improve-prior-constraints branch from d0a2ebf to b0c3259 Compare June 29, 2026 14:15
@mj-will

mj-will commented Jun 29, 2026

Copy link
Copy Markdown
Collaborator Author

I think we may need to have an option where this doesn't raise an error since it is called in sample_subset_constrained and there are cases there where it shouldn't fail. E.g. if evaluating the usual GW prior and the masses aren't present.

We could add this to the CBCPriorDict?

I've added strict option, let me know what you think.

@mj-will mj-will force-pushed the improve-prior-constraints branch from b0c3259 to a7e2493 Compare July 1, 2026 12:26

@ColmTalbot ColmTalbot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this is a hard condition on the sample, prob, and ln_prob methods.
I don't know that the latter two are correctly handled.
For example, the following is currently valid, but I suspect will fail under this change.
Is that right?

import bilby

priors = bilby.gw.prior.BBHPriorDict
priors.ln_prob(dict(a_1=0.3, tilt_1=0.7))

Comment thread bilby/core/prior/dict.py
def _estimate_normalization(self, keys, min_accept, sampling_chunk):
samples = self.sample_subset(keys=keys, size=sampling_chunk)
keep = np.atleast_1d(self.evaluate_constraints(samples))
if len(keep) == 1:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why has this been removed?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding was that this was a hacky check for if there aren't any constraints. Prior to #1028, evaluate_constraints would return 1.0 if there weren't any constraints, so this would skip the rest of this function.

After the changes 1028, that only happens if the sampling_chunk=1 is, so it's not clear to me this actually serves it's intended purpose.

Looking at this again, I think the lines above this may be redundant as well and this whole thing could be replaced with a has_constraints (or similar) property.

@mj-will

mj-will commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator Author

It looks like this is a hard condition on the sample, prob, and ln_prob methods. I don't know that the latter two are correctly handled. For example, the following is currently valid, but I suspect will fail under this change. Is that right?

import bilby

priors = bilby.gw.prior.BBHPriorDict
priors.ln_prob(dict(a_1=0.3, tilt_1=0.7))

Yes, this fails but I think it would fail before as well since the conversion function would fail with a KeyError due to the masses missing.

You raise a good point though, I had assumed ln_prob should only called with a complete sample but I don't know if that's actually the case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants