Exemple #1
0
def test_adaptive_parzen_normal_estimator():
    """Test adaptive parzen estimator"""
    low = -1
    high = 5

    obs_mus = [1.2]
    mus, sigmas, weights = adaptive_parzen_estimator(
        obs_mus, low, high, prior_weight=1.0, equal_weight=False, flat_num=25
    )
    assert list(mus) == [1.2, 2]
    assert list(sigmas) == [3, 6]
    assert list(weights) == [1.0 / 2, 1.0 / 2]

    obs_mus = [3.4]
    mus, sigmas, weights = adaptive_parzen_estimator(
        obs_mus, low, high, prior_weight=0.5, equal_weight=False, flat_num=25
    )
    assert list(mus) == [2, 3.4]
    assert list(sigmas) == [6, 3]
    assert list(weights) == [0.5 / 1.5, 1.0 / 1.5]

    obs_mus = numpy.linspace(-1, 5, num=30, endpoint=False)
    mus, sigmas, weights = adaptive_parzen_estimator(
        obs_mus, low, high, prior_weight=1.0, equal_weight=False, flat_num=25
    )

    ramp = numpy.linspace(1.0 / 30, 1.0, num=30 - 25)
    full = numpy.ones(25 + 1)
    all_weights = numpy.concatenate([ramp, full])

    assert len(mus) == len(sigmas) == len(weights) == 30 + 1
    assert numpy.all(weights[: 30 - 25] == ramp / all_weights.sum())
    assert numpy.all(weights[30 - 25 :] == 1 / all_weights.sum())
    assert numpy.all(sigmas == 6 / 10)
Exemple #2
0
def test_adaptive_parzen_normal_estimator_weight():
    """Test the weight for the normal components"""
    obs_mus = numpy.linspace(-1, 5, num=30, endpoint=False)
    low = -1
    high = 5

    # equal weight
    mus, sigmas, weights = adaptive_parzen_estimator(obs_mus,
                                                     low,
                                                     high,
                                                     prior_weight=1.0,
                                                     equal_weight=True,
                                                     flat_num=25)
    assert numpy.all(weights == 1 / 31)
    assert numpy.all(sigmas == 6 / 10)

    # prior weight
    mus, sigmas, weights = adaptive_parzen_estimator(obs_mus,
                                                     low,
                                                     high,
                                                     prior_weight=0.5,
                                                     equal_weight=False,
                                                     flat_num=25)

    ramp = numpy.linspace(1.0 / 30, 1.0, num=30 - 25)
    full = numpy.ones(25 + 1)
    all_weights = (numpy.concatenate([ramp, full]))
    prior_pos = numpy.searchsorted(mus, 2)
    all_weights[prior_pos] = 0.5

    assert numpy.all(
        weights[:30 - 25] == (numpy.linspace(1.0 / 30, 1.0, num=30 - 25) /
                              all_weights.sum()))
    assert numpy.all(weights[33 - 25:prior_pos] == 1 / all_weights.sum())
    assert weights[prior_pos] == 0.5 / all_weights.sum()
    assert numpy.all(weights[prior_pos + 1:] == 1 / all_weights.sum())
    assert numpy.all(sigmas == 6 / 10)

    # full weights number
    mus, sigmas, weights = adaptive_parzen_estimator(obs_mus,
                                                     low,
                                                     high,
                                                     prior_weight=1.0,
                                                     equal_weight=False,
                                                     flat_num=15)

    ramp = numpy.linspace(1.0 / 30, 1.0, num=30 - 15)
    full = numpy.ones(15 + 1)
    all_weights = (numpy.concatenate([ramp, full]))
    prior_pos = numpy.searchsorted(mus, 2)
    all_weights[prior_pos] = 1.0

    assert numpy.all(
        weights[:30 - 15] == (numpy.linspace(1.0 / 30, 1.0, num=30 - 15) /
                              all_weights.sum()))
    assert numpy.all(weights[30 - 15:] == 1 / all_weights.sum())
    assert numpy.all(sigmas == 6 / 10)
Exemple #3
0
def test_adaptive_parzen_normal_estimator_sigma_clip():
    """Test that the magic clip of sigmas for parzen estimator"""
    low = -1
    high = 5

    obs_mus = numpy.linspace(-1, 5, num=8, endpoint=False)
    mus, sigmas, weights = adaptive_parzen_estimator(obs_mus,
                                                     low,
                                                     high,
                                                     prior_weight=1.0,
                                                     equal_weight=False,
                                                     flat_num=25)
    assert len(mus) == len(sigmas) == len(weights) == 8 + 1
    assert numpy.all(weights == 1 / 9)
    assert numpy.all(sigmas == 6 / 8)

    obs_mus = numpy.random.uniform(-1, 5, 30)
    mus, sigmas, weights = adaptive_parzen_estimator(obs_mus,
                                                     low,
                                                     high,
                                                     prior_weight=1.0,
                                                     equal_weight=False,
                                                     flat_num=25)

    assert len(mus) == len(sigmas) == len(weights) == 30 + 1
    assert numpy.all(weights[-25:] == weights[-1])
    assert numpy.all(sigmas <= 6) and numpy.all(sigmas >= 6 / 10)

    obs_mus = numpy.random.uniform(-1, 5, 400)
    mus, sigmas, weights = adaptive_parzen_estimator(obs_mus,
                                                     low,
                                                     high,
                                                     prior_weight=1.0,
                                                     equal_weight=False,
                                                     flat_num=25)

    assert len(mus) == len(sigmas) == len(weights) == 400 + 1
    assert numpy.all(weights[-25:] == weights[-1])
    assert numpy.all(sigmas <= 6) and numpy.all(sigmas >= 6 / 20)

    obs_mus = numpy.random.uniform(-1, 5, 10000)
    mus, sigmas, weights = adaptive_parzen_estimator(obs_mus,
                                                     low,
                                                     high,
                                                     prior_weight=1.0,
                                                     equal_weight=False,
                                                     flat_num=25)

    assert len(mus) == len(sigmas) == len(weights) == 10000 + 1
    assert numpy.all(weights[-25:] == weights[-1])
    assert numpy.all(sigmas <= 6) and numpy.all(sigmas >= 6 / 100)