Skip to content

Fix constrained prior sampling

Colm Talbot requested to merge constrained-prior-sampling into master

The following simple example was failing due to inconsistent use of iteration parameter names.

I think the issue is that currently, this method assumes that the constraint parameters aren't added to the original dictionary.

This MR fixes that and simplifies some of the other logic in sampling from a constrained space.

def convert_x_y_to_z(parameters):
    """
    Function to convert between sampled parameters and constraint parameter.

    Parameters
    ----------
    parameters: dict
        Dictionary containing sampled parameter values, 'x', 'y'.

    Returns
    -------
    dict: Dictionary with constraint parameter 'z' added.
    """
    parameters['z'] = parameters['x'] - parameters['y']
    return parameters


from bilby.core.prior import PriorDict, Uniform, Constraint

priors = PriorDict(conversion_function=convert_x_y_to_z)
priors['x'] = Uniform(minimum=0, maximum=10)
priors['y'] = Uniform(minimum=0, maximum=10)
priors['z'] = Constraint(minimum=0, maximum=10)
priors.sample(10)

Merge request reports

Loading