Пример #1
0
def test_HistoryMatching_set_expectations():
    "test the set_expectations method of HistoryMatching"

    # correct functionality

    expectations = (np.array([0.]), np.array([0.1]), np.array([[2.]]))

    hm = HistoryMatching()
    hm.set_expectations(expectations)

    for a, b in zip(hm.expectations, expectations):
        assert_allclose(a, b)

    # confirm ability to reset expectations

    hm.set_expectations(None)
    assert hm.expectations == None

    # verify derivatives can be None

    expectations = (np.array([0.]), np.array([0.1]), None)

    hm = HistoryMatching()
    hm.set_expectations(expectations)

    for a, b in zip(hm.expectations[:-1], expectations[:-1]):
        assert_allclose(a, b)
    assert hm.expectations[2] is None

    # negative variance

    expectations = (np.array([0.]), np.array([-0.1]), np.array([[2.]]))

    with pytest.raises(AssertionError):
        hm.set_expectations(expectations)

    # bad values for expectations

    expectations = (np.array([0.1]), 1., 1.)
    hm = HistoryMatching()

    with pytest.raises(TypeError):
        hm.set_expectations(expectations)

    expectations = (np.array([0.1]), np.array([1., 2.]), 1.)

    with pytest.raises(TypeError):
        hm.set_expectations(expectations)

    expectations = (np.array([1.]), np.array([1., 2.]), None)

    with pytest.raises(ValueError):
        hm.set_expectations(expectations)
Пример #2
0
def test_sanity_checks():
    "test basic functioning of HistoryMatching"

    # Create a gaussian process
    x_training = np.array([[0.], [10.], [20.], [30.], [43.], [50.]])

    y_training = get_y_simulated_1D(x_training)

    gp = GaussianProcess(x_training, y_training)
    np.random.seed(47)
    gp.learn_hyperparameters()

    # Define observation and implausibility threshold
    obs = [-0.8, 0.0004]

    # Coords to predict
    n_rand = 2000
    x_predict_min = -3
    x_predict_max = 53
    x_predict = np.random.rand(n_rand)
    x_predict = np.sort(x_predict, axis=0)
    x_predict *= (x_predict_max - x_predict_min)
    x_predict += x_predict_min
    x_predict = x_predict[:, None]

    coords = x_predict

    expectations = gp.predict(coords)

    # Create History Matching Instance
    print("---TEST INPUTS---")
    print("No Args")
    hm = HistoryMatching()
    hm.status()

    print("Obs Only a - list")
    hm = HistoryMatching(obs=obs)
    hm.status()

    print("Obs only b - single-element list")
    hm = HistoryMatching(obs=[3.])
    hm.status()

    print("Obs only c - single-value")
    hm = HistoryMatching(obs=3.)
    hm.status()

    print("gp Only")
    hm = HistoryMatching(gp=gp)
    hm.status()

    print("Coords only a - 2d ndarray")
    hm = HistoryMatching(coords=coords)
    hm.status()

    print("Coords only b - 1d ndarray")
    hm = HistoryMatching(coords=np.random.rand(n_rand))
    hm.status()

    print("Coords only c - list")
    hm = HistoryMatching(coords=[a for a in range(n_rand)])
    hm.status()

    print("Expectation only")
    hm = HistoryMatching(expectations=expectations)
    hm.status()

    print("Threshold Only")
    hm = HistoryMatching(threshold=3.)
    hm.status()

    print("---TEST ASSIGNMENT---")
    print("Assign gp")
    hm = HistoryMatching(obs)
    hm.status()
    hm.set_gp(gp)
    hm.status()

    print("Assign Obs")
    hm = HistoryMatching(gp)
    hm.status()
    hm.set_obs(obs)
    hm.status()

    print("Assign Coords")
    hm = HistoryMatching()
    hm.status()
    hm.set_coords(coords)
    hm.status()

    print("Assign Expectations")
    hm = HistoryMatching()
    hm.status()
    hm.set_expectations(expectations)
    hm.status()

    print("Assign Threshold")
    hm = HistoryMatching()
    hm.status()
    hm.set_threshold(3.)
    hm.status()

    print("---TEST IMPLAUSABILIY---")
    print("implausibility test a - no vars")
    hm = HistoryMatching(obs=obs, gp=gp, coords=coords)
    I = hm.get_implausibility()

    print("implausibility test b - single value")
    hm = HistoryMatching(obs=obs, gp=gp, coords=coords)
    I = hm.get_implausibility(7.)