Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with conditional prior dict and log probabilities? #880

Open
KonstantinLeyde opened this issue Dec 13, 2024 · 0 comments
Open

Problem with conditional prior dict and log probabilities? #880

KonstantinLeyde opened this issue Dec 13, 2024 · 0 comments
Labels
bug Something isn't working priors

Comments

@KonstantinLeyde
Copy link

KonstantinLeyde commented Dec 13, 2024

If I define the following conditional prior dict, the log_prob raises an error:

from bilby.core.prior import ConditionalPriorDict, PowerLaw, ConditionalPowerLaw, Constraint
from bilby.gw.conversion import generate_mass_parameters

def secondary_condition_function(reference_params, mass_1_source):
    return dict(minimum=reference_params['minimum'], maximum=mass_1_source)

p = dict(
    alpha=1,
    beta=2,
    minimum=3,
    maximum=10,
)

prior = ConditionalPriorDict(
    {'mass_1_source': PowerLaw(
            alpha=-p["alpha"],
            minimum=p["minimum"],
            maximum=p["maximum"],
        ),
    "mass_2_source": ConditionalPowerLaw(
            secondary_condition_function,
            alpha=p["beta"],
            minimum=p["minimum"],
            maximum=p["maximum"],
    ),
    "mass_ratio": Constraint(minimum=0.125, maximum=1.0),
    },
conversion_function=lambda x: generate_mass_parameters(x, source=True),
)

s = prior.sample(size=5)


# gives an error
log_prob = prior.ln_prob(s)
print(log_prob.shape)

# # works as expected
# log_prob = prior.ln_prob(s, axis=0)
# print(log_prob.shape)

The error message I get is

    log_prob = prior.ln_prob(s)
  File "/opt/homebrew/Caskroom/mambaforge/base/envs/dingo-pop/lib/python3.10/site-packages/bilby/core/prior/dict.py", line 809, in ln_prob
    return self.check_ln_prob(sample, ln_prob)
  File "/opt/homebrew/Caskroom/mambaforge/base/envs/dingo-pop/lib/python3.10/site-packages/bilby/core/prior/dict.py", line 565, in check_ln_prob
    if self.evaluate_constraints(sample):
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

If I do not enforce the mass ratio constraint (and also delete the conversion_function), this works as expected (calculating the probability over all samples). Also, if I add axis=0, this returns the log probability for each sample, also not throwing an error. Is this behavior expected? Thanks!

@mj-will mj-will added bug Something isn't working priors labels Dec 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working priors
Projects
None yet
Development

No branches or pull requests

2 participants