Esempio n. 1
0
    def __init__(self, config, batch_size):
        self.batch_size = batch_size
        self.config = config

        self.constraints_gaussian = gaussian_constraint_combined(
            config, batch_size=self.batch_size)
        self.constraints_poisson = poisson_constraint_combined(
            config, batch_size=self.batch_size)

        self.viewer_aux = ParamViewer(
            (self.batch_size or 1, self.config.npars),
            self.config.par_map,
            self.config.auxdata_order,
        )

        assert self.constraints_gaussian.batch_size == self.batch_size
        assert self.constraints_poisson.batch_size == self.batch_size

        indices = []
        if self.constraints_gaussian.has_pdf():
            indices.append(self.constraints_gaussian._normal_data)
        if self.constraints_poisson.has_pdf():
            indices.append(self.constraints_poisson._poisson_data)
        if self.has_pdf():
            self.constraints_tv = _TensorViewer(indices, self.batch_size)
Esempio n. 2
0
def test_batched_constraints(backend):
    config = MockConfig(
        par_order=['pois1', 'pois2', 'norm1', 'norm2'],
        par_map={
            'pois1': {
                'paramset':
                constrained_by_poisson(
                    name='pois1',
                    is_scalar=False,
                    n_parameters=1,
                    inits=[1.0],
                    bounds=[[0, 10]],
                    auxdata=[12],
                    factors=[12],
                    fixed=False,
                ),
                'slice':
                slice(0, 1),
                'auxdata': [1],
            },
            'pois2': {
                'paramset':
                constrained_by_poisson(
                    name='pois2',
                    is_scalar=False,
                    n_parameters=2,
                    inits=[1.0] * 2,
                    bounds=[[0, 10]] * 2,
                    auxdata=[13, 14],
                    factors=[13, 14],
                    fixed=False,
                ),
                'slice':
                slice(1, 3),
            },
            'norm1': {
                'paramset':
                constrained_by_normal(
                    name='norm1',
                    is_scalar=False,
                    n_parameters=2,
                    inits=[0] * 2,
                    bounds=[[0, 10]] * 2,
                    auxdata=[0, 0],
                    sigmas=[1.5, 2.0],
                    fixed=False,
                ),
                'slice':
                slice(3, 5),
            },
            'norm2': {
                'paramset':
                constrained_by_normal(
                    name='norm2',
                    is_scalar=False,
                    n_parameters=3,
                    inits=[0] * 3,
                    bounds=[[0, 10]] * 3,
                    auxdata=[0, 0, 0],
                    fixed=False,
                ),
                'slice':
                slice(5, 8),
            },
        },
    )
    suggested_pars = [1.0] * 3 + [0.0] * 5  # 2 pois 5 norm
    constraint = poisson_constraint_combined(config)
    result = default_backend.astensor(
        pyhf.tensorlib.tolist(
            constraint.logpdf(
                pyhf.tensorlib.astensor(config.auxdata),
                pyhf.tensorlib.astensor(suggested_pars),
            )))
    assert np.isclose(
        result,
        sum([
            default_backend.poisson_logpdf(data, rate)
            for data, rate in zip([12, 13, 14], [12, 13, 14])
        ]),
    )
    assert result.shape == ()

    suggested_pars = [1.1] * 3 + [0.0] * 5  # 2 pois 5 norm
    constraint = poisson_constraint_combined(config)
    result = default_backend.astensor(
        pyhf.tensorlib.tolist(
            constraint.logpdf(
                pyhf.tensorlib.astensor(config.auxdata),
                pyhf.tensorlib.astensor(suggested_pars),
            )))
    assert np.isclose(
        result,
        sum([
            default_backend.poisson_logpdf(data, rate)
            for data, rate in zip([12, 13, 14], [12 * 1.1, 13 * 1.1, 14 * 1.1])
        ]),
    )
    assert result.shape == ()

    constraint = poisson_constraint_combined(config, batch_size=10)
    result = constraint.logpdf(
        pyhf.tensorlib.astensor(config.auxdata),
        pyhf.tensorlib.astensor([suggested_pars] * 10),
    )
    assert result.shape == (10, )

    suggested_pars = [
        [1.1, 1.2, 1.3] + [0.0] * 5,  # 2 pois 5 norm
        [0.7, 0.8, 0.9] + [0.0] * 5,  # 2 pois 5 norm
        [0.4, 0.5, 0.6] + [0.0] * 5,  # 2 pois 5 norm
    ]
    constraint = poisson_constraint_combined(config, batch_size=3)
    result = default_backend.astensor(
        pyhf.tensorlib.tolist(
            constraint.logpdf(
                pyhf.tensorlib.astensor(config.auxdata),
                pyhf.tensorlib.astensor(suggested_pars),
            )))
    assert np.all(
        np.isclose(
            result,
            np.sum(
                [
                    [
                        default_backend.poisson_logpdf(data, rate)
                        for data, rate in zip([12, 13, 14],
                                              [12 * 1.1, 13 * 1.2, 14 * 1.3])
                    ],
                    [
                        default_backend.poisson_logpdf(data, rate)
                        for data, rate in zip([12, 13, 14],
                                              [12 * 0.7, 13 * 0.8, 14 * 0.9])
                    ],
                    [
                        default_backend.poisson_logpdf(data, rate)
                        for data, rate in zip([12, 13, 14],
                                              [12 * 0.4, 13 * 0.5, 14 * 0.6])
                    ],
                ],
                axis=1,
            ),
        ))
    assert result.shape == (3, )

    suggested_pars = [1.0] * 3 + [0.0] * 5  # 2 pois 5 norm
    constraint = gaussian_constraint_combined(config, batch_size=1)
    result = default_backend.astensor(
        pyhf.tensorlib.tolist(
            constraint.logpdf(
                pyhf.tensorlib.astensor(config.auxdata),
                pyhf.tensorlib.astensor(suggested_pars),
            )))
    assert np.isclose(
        result[0],
        sum([
            default_backend.normal_logpdf(data, mu, sigma) for data, mu, sigma
            in zip([0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1.5, 2.0, 1.0, 1.0, 1.0])
        ]),
    )
    assert result.shape == (1, )

    suggested_pars = [1.0] * 3 + [1, 2, 3, 4, 5]  # 2 pois 5 norm
    constraint = gaussian_constraint_combined(config, batch_size=1)
    result = default_backend.astensor(
        pyhf.tensorlib.tolist(
            constraint.logpdf(
                pyhf.tensorlib.astensor(config.auxdata),
                pyhf.tensorlib.astensor(suggested_pars),
            )))
    assert np.isclose(
        result[0],
        sum([
            default_backend.normal_logpdf(data, mu, sigma) for data, mu, sigma
            in zip([0, 0, 0, 0, 0], [1, 2, 3, 4, 5], [1.5, 2.0, 1.0, 1.0, 1.0])
        ]),
    )
    assert result.shape == (1, )

    suggested_pars = [
        [1.0] * 3 + [1, 2, 3, 4, 5],  # 2 pois 5 norm
        [1.0] * 3 + [-1, -2, -3, -4, -5],  # 2 pois 5 norm
        [1.0] * 3 + [-1, -2, 0, 1, 2],  # 2 pois 5 norm
    ]
    constraint = gaussian_constraint_combined(config, batch_size=3)
    result = default_backend.astensor(
        pyhf.tensorlib.tolist(
            constraint.logpdf(
                pyhf.tensorlib.astensor(config.auxdata),
                pyhf.tensorlib.astensor(suggested_pars),
            )))
    assert np.all(
        np.isclose(
            result,
            np.sum(
                [
                    [
                        default_backend.normal_logpdf(data, mu, sigma)
                        for data, mu, sigma in
                        zip([0, 0, 0, 0, 0], [1, 2, 3, 4, 5],
                            [1.5, 2.0, 1.0, 1.0, 1.0])
                    ],
                    [
                        default_backend.normal_logpdf(data, mu, sigma)
                        for data, mu, sigma in zip(
                            [0, 0, 0, 0, 0],
                            [-1, -2, -3, -4, -5],
                            [1.5, 2.0, 1.0, 1.0, 1.0],
                        )
                    ],
                    [
                        default_backend.normal_logpdf(data, mu, sigma)
                        for data, mu, sigma in zip(
                            [0, 0, 0, 0, 0],
                            [-1, -2, 0, 1, 2],
                            [1.5, 2.0, 1.0, 1.0, 1.0],
                        )
                    ],
                ],
                axis=1,
            ),
        ))
    assert result.shape == (3, )

    constraint = gaussian_constraint_combined(config, batch_size=10)
    result = constraint.logpdf(
        pyhf.tensorlib.astensor(config.auxdata),
        pyhf.tensorlib.astensor([suggested_pars] * 10),
    )
    assert result.shape == (10, )