Пример #1
0
def test_specific_points_x_prime_to_x_pi_pi(convention, input, expected):
    """Test specific points on a sphere.

    Order is (x, y, z) to (ra/az, dec/zen, r) using [-pi, pi] for ra/az.

    Test:
    - (+/-1, 0, 0) and all permutations
    - (0, 0, 0)
    - (+/-1, +-1, sqrt(2)) a point exactly 2 away from the origin
    """
    parameters = ['a', 'b']
    if convention == 'ra-dec':
        prior_bounds = {'a': [-np.pi, np.pi], 'b': [-np.pi / 2, np.pi / 2]}
    else:
        prior_bounds = {'a': [-np.pi, np.pi], 'b': [0, np.pi]}
    reparam = AnglePair(
        parameters=parameters,
        prior_bounds=prior_bounds,
        convention=convention,
    )

    x_prime = parameters_to_live_point(input, reparam.prime_parameters)
    x = parameters_to_live_point([0, 0, 0], reparam.parameters)
    log_j = 0

    out, _, _ = reparam.inverse_reparameterise(x, x_prime, log_j)

    np.testing.assert_equal(out[reparam.parameters[0]], expected[0])
    np.testing.assert_equal(out[reparam.parameters[1]], expected[1])
    np.testing.assert_equal(out[reparam.parameters[2]], expected[2])
Пример #2
0
def test_consume_sample_reject(sampler, live_points):
    """Test the default behaviour of consume sample"""
    sampler.live_points = live_points
    reject_sample = parameters_to_live_point((-0.5,), ['x'])
    reject_sample['logL'] = -0.5
    new_sample = parameters_to_live_point((0.5,), ['x'])
    new_sample['logL'] = 0.5
    sampler.yield_sample = MagicMock()
    sampler.yield_sample.return_value = \
        iter([(1, reject_sample), (1, new_sample)])

    sampler.insert_live_point = MagicMock(return_value=0)
    sampler.check_state = MagicMock()

    NestedSampler.consume_sample(sampler)

    sampler.insert_live_point.assert_called_once_with(new_sample)
    sampler.check_state.assert_called_once()

    assert sampler.nested_samples == [live_points[0]]
    assert sampler.logLmin == 0.0
    assert sampler.rejected == 1
    assert sampler.accepted == 1
    assert sampler.block_acceptance == 0.5
    assert sampler.acceptance_history == [0.5]
    assert sampler.mean_block_acceptance == 0.5
    assert sampler.insertion_indices == [0]
Пример #3
0
def test_yield_sample_reject(sampler, old_sample):
    """Test function when sample is rejected and a new sample is drawn"""
    new_samples = [
        parameters_to_live_point([6, 6], names=sampler.model.names),
        parameters_to_live_point([1, 1], names=sampler.model.names)
    ]

    sampler.proposal.draw = MagicMock(side_effect=new_samples)
    sampler.proposal.populated = True

    count, next_sample = next(NestedSampler.yield_sample(sampler, old_sample))

    assert count == 2
    assert next_sample == new_samples[1]
    sampler.proposal.draw.assert_called_with(old_sample)
Пример #4
0
def test_parameters_to_live_point(live_point):
    """
    Test function that produces a single live point given the parameter
    values for the live point as a live or an array
    """
    x = lp.parameters_to_live_point([1., 2., 3.], ['x', 'y', 'z'])
    np.testing.assert_array_equal(live_point, x)
Пример #5
0
def test_periodic_parameter(value, output_x, output_y):
    """Test a generic periodic parameter"""
    parameters = ['a']
    prior_bounds = {'a': [-1.0, 1.0]}
    reparam = Angle(parameters=parameters,
                    prior_bounds=prior_bounds,
                    scale=None)
    x = parameters_to_live_point((value, ), parameters)
    x_prime = parameters_to_live_point((np.nan, np.nan),
                                       reparam.prime_parameters)
    log_j = np.zeros(x.size)
    x_out, x_prime_out, log_j_out = reparam.reparameterise(x, x_prime, log_j)

    np.testing.assert_array_equal(x_out, x)

    if output_x:
        assert x_prime_out[reparam.x] == output_x
    if output_y:
        assert x_prime_out[reparam.y] == output_y
Пример #6
0
def test_x_prime_log_prior():
    """Test the x prime prior"""
    reparam = create_autospec(AnglePair)
    reparam.prime_parameters = ['x', 'y', 'z']
    reparam.has_prime_prior = True
    x = parameters_to_live_point([1, 1, 1], reparam.prime_parameters)
    # Should be -1.5 * log(2pi) - 1.5
    expected = -1.5 * (1 + np.log(2 * np.pi))
    out = AnglePair.x_prime_log_prior(reparam, x)
    np.testing.assert_equal(out, expected)
Пример #7
0
def test_yield_sample_accept(sampler, old_sample):
    """Test function when sample is accepted"""
    new_sample = parameters_to_live_point([1, 1], names=sampler.model.names)
    sampler.proposal.draw = MagicMock(return_value=new_sample)

    count, next_sample = next(NestedSampler.yield_sample(sampler, old_sample))

    assert count == 1
    sampler.proposal.draw.assert_called_once_with(old_sample)
    assert next_sample == new_sample
    assert sampler.logLmax == sampler.model.log_likelihood(new_sample)
Пример #8
0
def test_yield_sample_not_populated(sampler, old_sample):
    """Test function when sample is rejected and a new sample is not drawn"""
    new_sample = parameters_to_live_point([6, 6], names=sampler.model.names)

    sampler.proposal.draw = MagicMock(return_value=new_sample)
    sampler.proposal.populated = False

    count, next_sample = next(NestedSampler.yield_sample(sampler, old_sample))

    assert count == 1
    assert old_sample == next_sample
    sampler.proposal.draw.assert_called_once_with(old_sample)
Пример #9
0
def test_log_prior():
    """Assert log_prior calls the log pdf of a chi distribution."""
    reparam = create_autospec(AnglePair)
    reparam.parameters = ['a', 'b', 'r']
    reparam.has_prior = True
    x = parameters_to_live_point([0, 0, 2.0], reparam.parameters)
    reparam.chi = MagicMock()
    reparam.chi.logpdf = MagicMock(return_value=1.0)
    out = AnglePair.log_prior(reparam, x)
    reparam.chi.assert_not_called()
    reparam.chi.logpdf.assert_called_once_with(x['r'])
    assert out == 1.0
Пример #10
0
def old_sample(model):
    return parameters_to_live_point([5, 5], names=model.names)
Пример #11
0
def test_empty_parameters_to_live_point(empty_live_point):
    """
    Test behaviour when an empty parameter is parsed
    """
    np.testing.assert_array_equal(
        empty_live_point, lp.parameters_to_live_point([], ['x', 'y', 'z']))