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)
def test_sample_and_predict(): reg = GPARRegressor(replace=False, impute=False, linear=True, linear_scale=1., nonlinear=False, noise=1e-8, normalise_y=False) x = np.linspace(0, 5, 10) # Test checks. yield raises, ValueError, lambda: reg.sample(x) yield raises, RuntimeError, lambda: reg.sample(x, posterior=True) # Test that output is simplified correctly. yield isinstance, reg.sample(x, p=2), np.ndarray yield isinstance, reg.sample(x, p=2, num_samples=2), list # Test that it produces random samples. Not sure how to test correctness. yield ge, np.sum(np.abs(reg.sample(x, p=2) - reg.sample(x, p=2))), 1e-2 yield ge, 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.fit(x, y, iters=0) yield approx, y, np.mean(reg.sample(x, posterior=True, num_samples=20), axis=0), 4 yield approx, y, np.mean(reg.sample(x, latent=True, posterior=True, num_samples=20), axis=0), 4 # Test that prediction is around the data. yield approx, y, reg.predict(x, num_samples=20), 4 yield approx, y, reg.predict(x, latent=True, num_samples=20), 4 # Test that prediction is confident. _, lowers, uppers = reg.predict(x, num_samples=10, credible_bounds=True) yield ok, np.less_equal(uppers - lowers, 1e-3).all()
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()
y = f + noise * np.random.randn(n, 3) x_obs, y_obs = x[::8], y[::8] # Fit and predict GPAR. model = GPARRegressor(scale=0.1, linear=True, linear_scale=10., nonlinear=True, nonlinear_scale=0.1, noise=0.1, impute=True, replace=False, normalise_y=False) model.fit(x_obs, y_obs) means, lowers, uppers = \ model.predict(x, num_samples=100, credible_bounds=True, latent=True) # Fit and predict independent GPs: set markov=0. igp = GPARRegressor(scale=0.1, linear=True, linear_scale=10., nonlinear=True, nonlinear_scale=0.1, noise=0.1, markov=0, normalise_y=False) igp.fit(x_obs, y_obs) igp_means, igp_lowers, igp_uppers = \ igp.predict(x, num_samples=100, credible_bounds=True, latent=True) # Plot the result.
model = GPARRegressor(scale=0.1, linear=False, nonlinear=True, nonlinear_scale=0.5, impute=True, replace=True, noise=0.1, normalise_y=True) # Sample observations and discard some. y = model.sample(x, p=3) y_obs = y.copy() y_obs[np.random.permutation(100)[:25], 0] = np.nan y_obs[np.random.permutation(100)[:50], 1] = np.nan y_obs[np.random.permutation(100)[:75], 2] = np.nan # Fit model and predict. model.fit(x, y) means, lowers, uppers = \ model.predict(x, num_samples=200, latent=False, credible_bounds=True) # Plot the result. plt.figure(figsize=(8, 6)) for i in range(3): plt.subplot(3, 1, i + 1) plt.plot(x, means[:, i], label='Prediction', style='pred') plt.fill_between(x, lowers[:, i], uppers[:, i], style='pred') plt.scatter(x, y[:, i], label='Truth', style='test') plt.scatter(x, y_obs[:, i], label='Observations', style='train') plt.ylabel(f'Output {i + 1}') wbml.plot.tweak(legend=i == 0) plt.tight_layout() plt.show()
replace=True, noise=0.1, normalise_y=True, ) # Sample observations and discard some. y = model.sample(x, p=3) y_obs = y.copy() y_obs[np.random.permutation(100)[:25], 0] = np.nan y_obs[np.random.permutation(100)[:50], 1] = np.nan y_obs[np.random.permutation(100)[:75], 2] = np.nan # Fit model and predict. model.fit(x, y) means, lowers, uppers = model.predict(x, num_samples=200, latent=False, credible_bounds=True) # Plot the result. plt.figure(figsize=(8, 6)) for i in range(3): plt.subplot(3, 1, i + 1) plt.plot(x, means[:, i], label="Prediction", style="pred") plt.fill_between(x, lowers[:, i], uppers[:, i], style="pred") plt.scatter(x, y[:, i], label="Truth", style="test") plt.scatter(x, y_obs[:, i], label="Observations", style="train") plt.ylabel(f"Output {i + 1}") wbml.plot.tweak(legend=i == 0) plt.tight_layout()
# Fit and predict GPAR. model = GPARRegressor( scale=0.1, linear=True, linear_scale=10.0, nonlinear=True, nonlinear_scale=0.1, noise=0.1, impute=True, replace=False, normalise_y=False, ) model.fit(x_obs, y_obs) means, lowers, uppers = model.predict(x, num_samples=200, credible_bounds=True, latent=True) # Fit and predict independent GPs: set `markov=0` in GPAR. igp = GPARRegressor( scale=0.1, linear=True, linear_scale=10.0, nonlinear=True, nonlinear_scale=0.1, noise=0.1, markov=0, normalise_y=False, ) igp.fit(x_obs, y_obs) igp_means, igp_lowers, igp_uppers = igp.predict(x,