Example #1
0
def test_condition_and_fit():
    reg = GPARRegressor(replace=False, impute=False,
                        normalise_y=True, transform_y=squishing_transform)
    x = np.linspace(0, 5, 10)
    y = reg.sample(x, p=2)

    # Test that data is correctly normalised.
    reg.condition(x, y)
    approx(B.mean(reg.y, axis=0), B.zeros(reg.p))
    approx(B.std(reg.y, axis=0), B.ones(reg.p))

    # Test that data is correctly normalised if it has an output with zero
    # variance.
    y_pathological = y.copy()
    y_pathological[:, 0] = 1
    reg.condition(x, y_pathological)
    assert (~B.isnan(reg.y)).numpy().all()

    # Test transformation and normalisation of outputs.
    z = torch.linspace(-1, 1, 10, dtype=torch.float64)
    z = B.stack(z, 2 * z, axis=1)
    allclose(reg._untransform_y(reg._transform_y(z)), z)
    allclose(reg._unnormalise_y(reg._normalise_y(z)), z)

    # Test that fitting runs without issues.
    vs = reg.vs.copy(detach=True)
    reg.fit(x, y, fix=False)
    reg.vs = vs
    reg.fit(x, y, fix=True)

    # TODO: Remove this once greedy search is implemented.
    with pytest.raises(NotImplementedError):
        reg.fit(x, y, greedy=True)
Example #2
0
def test_sample_and_predict(x, w):
    # Use output transform to ensure that is handled correctly.
    reg = GPARRegressor(
        replace=False,
        impute=False,
        linear=True,
        linear_scale=1.0,
        nonlinear=False,
        noise=1e-8,
        normalise_y=False,
        transform_y=squishing_transform,
    )

    # Test checks.
    with pytest.raises(ValueError):
        reg.sample(x, w)
    with pytest.raises(RuntimeError):
        reg.sample(x, w, posterior=True)

    # Test that output is simplified correctly.
    assert isinstance(reg.sample(x, w, p=2), np.ndarray)
    assert isinstance(reg.sample(x, w, p=2, num_samples=2), list)

    # Test that it produces random samples. Not sure how to test correctness.
    all_different(reg.sample(x, w, p=2), reg.sample(x, w, p=2))
    all_different(reg.sample(x, w, p=2, latent=True),
                  reg.sample(x, w, p=2, latent=True))

    # Test that mean of posterior samples are around the data.
    y = reg.sample(x, w, p=2)
    reg.condition(x, y, w)
    approx(y,
           np.mean(reg.sample(x, w, posterior=True, num_samples=100), axis=0),
           atol=5e-2)
    approx(
        y,
        np.mean(reg.sample(x, w, latent=True, posterior=True, num_samples=100),
                axis=0),
        atol=5e-2,
    )

    # Test that prediction is around the data.
    approx(y, reg.predict(x, w, num_samples=100), atol=5e-2)
    approx(y, reg.predict(x, w, latent=True, num_samples=100), atol=5e-2)

    # Test that prediction is confident.
    _, lowers, uppers = reg.predict(x,
                                    w,
                                    num_samples=100,
                                    credible_bounds=True)
    approx(uppers, lowers, atol=5e-2)
Example #3
0
def test_logpdf(x, w):
    # Sample some data from a "sensitive" GPAR.
    reg = GPARRegressor(
        replace=False,
        impute=False,
        nonlinear=True,
        nonlinear_scale=0.1,
        linear=True,
        linear_scale=10.0,
        noise=1e-2,
        normalise_y=False,
    )
    y = reg.sample(x, w, p=2, latent=True)

    # Extract models.
    gpar = _construct_gpar(reg, reg.vs, B.shape(B.uprank(x))[1], 2)
    f1, e1 = gpar.layers[0]()
    f2, e2 = gpar.layers[1]()

    # Test computation under prior.
    x1 = x
    x2 = B.concat(B.uprank(x), y[:, 0:1], axis=1)
    if w is not None:
        x1 = WeightedUnique(x1, w[:, 0])
        x2 = WeightedUnique(x2, w[:, 1])
    logpdf1 = (f1 + e1)(x1).logpdf(y[:, 0])
    logpdf2 = (f2 + e2)(x2).logpdf(y[:, 1])
    approx(reg.logpdf(x, y, w), logpdf1 + logpdf2, atol=1e-6)

    # Test computation under posterior.
    post1 = f1.measure | ((f1 + e1)(x1), y[:, 0])
    post2 = f2.measure | ((f2 + e2)(x2), y[:, 1])
    e1_post = GP(e1.mean, e1.kernel, measure=post1)
    e2_post = GP(e2.mean, e2.kernel, measure=post2)
    logpdf1 = (post1(f1) + e1_post)(x1).logpdf(y[:, 0])
    logpdf2 = (post2(f2) + e2_post)(x2).logpdf(y[:, 1])
    with pytest.raises(RuntimeError):
        reg.logpdf(x, y, w, posterior=True)
    reg.condition(x, y, w)
    approx(reg.logpdf(x, y, w, posterior=True), logpdf1 + logpdf2, atol=1e-6)

    # Test that sampling missing gives a stochastic estimate.
    y[::2, 0] = np.nan
    all_different(
        reg.logpdf(x, y, w, sample_missing=True),
        reg.logpdf(x, y, w, sample_missing=True),
    )
Example #4
0
def test_logpdf(x, w):
    # Sample some data from a "sensitive" GPAR.
    reg = GPARRegressor(
        replace=False,
        impute=False,
        nonlinear=True,
        nonlinear_scale=0.1,
        linear=True,
        linear_scale=10.0,
        noise=1e-2,
        normalise_y=False,
    )
    y = reg.sample(x, w, p=2, latent=True)

    # Extract models.
    gpar = _construct_gpar(reg, reg.vs, B.shape(B.uprank(x))[1], 2)
    f1, noise1 = gpar.layers[0]()
    f2, noise2 = gpar.layers[1]()

    if w is not None:
        noise1 = noise1 / w[:, 0]
        noise2 = noise2 / w[:, 1]

    # Test computation under prior.
    x1 = x
    x2 = B.concat(B.uprank(x), y[:, 0:1], axis=1)
    logpdf1 = f1(x1, noise1).logpdf(y[:, 0])
    logpdf2 = f2(x2, noise2).logpdf(y[:, 1])
    approx(reg.logpdf(x, y, w), logpdf1 + logpdf2, atol=1e-6)

    # Test computation under posterior.
    f1_post = f1 | (f1(x1, noise1), y[:, 0])
    f2_post = f2 | (f2(x2, noise2), y[:, 1])
    logpdf1 = f1_post(x1, noise1).logpdf(y[:, 0])
    logpdf2 = f2_post(x2, noise2).logpdf(y[:, 1])
    with pytest.raises(RuntimeError):
        reg.logpdf(x, y, w, posterior=True)
    reg.condition(x, y, w)
    approx(reg.logpdf(x, y, w, posterior=True), logpdf1 + logpdf2, atol=1e-6)

    # Test that sampling missing gives a stochastic estimate.
    y[::2, 0] = np.nan
    all_different(
        reg.logpdf(x, y, w, sample_missing=True),
        reg.logpdf(x, y, w, sample_missing=True),
    )
Example #5
0
def test_sample_and_predict():
    # Use output transform to ensure that is handled correctly.
    reg = GPARRegressor(replace=False, impute=False,
                        linear=True, linear_scale=1., nonlinear=False,
                        noise=1e-8, normalise_y=False,
                        transform_y=squishing_transform)
    x = np.linspace(0, 5, 5)

    # Test checks.
    with pytest.raises(ValueError):
        reg.sample(x)
    with pytest.raises(RuntimeError):
        reg.sample(x, posterior=True)

    # Test that output is simplified correctly.
    assert isinstance(reg.sample(x, p=2), np.ndarray)
    assert isinstance(reg.sample(x, p=2, num_samples=2), list)

    # Test that it produces random samples. Not sure how to test correctness.
    assert np.sum(np.abs(reg.sample(x, p=2) - reg.sample(x, p=2))) >= 1e-2
    assert np.sum(np.abs(reg.sample(x, p=2, latent=True) -
                         reg.sample(x, p=2, latent=True))) >= 1e-3

    # Test that mean of posterior samples are around the data.
    y = reg.sample(x, p=2)
    reg.condition(x, y)
    approx(y, np.mean(reg.sample(x,
                                 posterior=True,
                                 num_samples=100), axis=0), digits=3)
    approx(y, np.mean(reg.sample(x,
                                 latent=True,
                                 posterior=True,
                                 num_samples=100), axis=0), digits=3)

    # Test that prediction is around the data.
    approx(y, reg.predict(x, num_samples=100), digits=3)
    approx(y, reg.predict(x, latent=True, num_samples=100), digits=3)

    # Test that prediction is confident.
    _, lowers, uppers = reg.predict(x, num_samples=100, credible_bounds=True)
    assert np.less_equal(uppers - lowers, 1e-2).all()
Example #6
0
def test_logpdf():
    # Sample some data from a "sensitive" GPAR.
    reg = GPARRegressor(replace=False, impute=False,
                        nonlinear=True, nonlinear_scale=0.1,
                        linear=True, linear_scale=10.,
                        noise=1e-4, normalise_y=False)
    x = np.linspace(0, 5, 10)
    y = reg.sample(x, p=2, latent=True)

    # Extract models.
    gpar = _construct_gpar(reg, reg.vs, 1, 2)
    f1, e1 = gpar.layers[0]()
    f2, e2 = gpar.layers[1]()

    # Test computation under prior.
    logpdf1 = (f1 + e1)(tensor(x)).logpdf(tensor(y[:, 0]))
    x_stack = np.concatenate([x[:, None], y[:, 0:1]], axis=1)
    logpdf2 = (f2 + e2)(tensor(x_stack)).logpdf(tensor(y[:, 1]))
    approx(reg.logpdf(x, y), logpdf1 + logpdf2, digits=6)

    # Test computation under posterior.
    e1_post = GP(e1.kernel, e1.mean, graph=e1.graph)
    e2_post = GP(e2.kernel, e2.mean, graph=e2.graph)
    f1_post = f1 | ((f1 + e1)(tensor(x)), tensor(y[:, 0]))
    f2_post = f2 | ((f2 + e2)(tensor(x_stack)), tensor(y[:, 1]))
    logpdf1 = (f1_post + e1_post)(tensor(x)).logpdf(tensor(y[:, 0]))
    logpdf2 = (f2_post + e2_post)(tensor(x_stack)).logpdf(tensor(y[:, 1]))
    with pytest.raises(RuntimeError):
        reg.logpdf(x, y, posterior=True)
    reg.condition(x, y)
    approx(reg.logpdf(x, y, posterior=True), logpdf1 + logpdf2, digits=6)

    # Test that sampling missing gives a stochastic estimate.
    y[::2, 0] = np.nan
    assert np.abs(reg.logpdf(x, y, sample_missing=True) -
                  reg.logpdf(x, y, sample_missing=True)) >= 1e-3