def test_regression_without_noise(): """Test regression without noise with MVN.""" random_state = check_random_state(0) n_samples = 10 x = np.linspace(0, 1, n_samples)[:, np.newaxis] y = 3 * x + 1 samples = np.hstack((x, y)) mvn = MVN(random_state=random_state) mvn.from_samples(samples) assert_array_almost_equal(mvn.mean, np.array([0.5, 2.5]), decimal=2) pred, cov = mvn.predict(np.array([0]), x) mse = np.sum((y - pred) ** 2) / n_samples assert_less(mse, 1e-10) assert_less(cov[0, 0], 1e-10)
def test_regression_without_noise(): """Test regression without noise with MVN.""" random_state = check_random_state(0) n_samples = 10 x = np.linspace(0, 1, n_samples)[:, np.newaxis] y = 3 * x + 1 samples = np.hstack((x, y)) mvn = MVN(random_state=random_state) mvn.from_samples(samples) assert_array_almost_equal(mvn.mean, np.array([0.5, 2.5]), decimal=2) pred, cov = mvn.predict(np.array([0]), x) mse = np.sum((y - pred)**2) / n_samples assert_less(mse, 1e-10) assert_less(cov[0, 0], 1e-10)
def test_regression_with_2d_input(): """Test regression with MVN and two-dimensional input.""" random_state = check_random_state(0) n_samples = 100 x = np.linspace(0, 1, n_samples)[:, np.newaxis] y = 3 * x + 1 noise = random_state.randn(n_samples, 1) * 0.01 y += noise samples = np.hstack((x, x[::-1], y)) mvn = MVN(random_state=random_state) mvn.from_samples(samples) assert_array_almost_equal(mvn.mean, np.array([0.5, 0.5, 2.5]), decimal=2) x_test = np.hstack((x, x[::-1])) pred, cov = mvn.predict(np.array([0, 1]), x_test) mse = np.sum((y - pred)**2) / n_samples assert_less(mse, 1e-3) assert_less(cov[0, 0], 0.01)
def test_regression_with_2d_input(): """Test regression with MVN and two-dimensional input.""" random_state = check_random_state(0) n_samples = 100 x = np.linspace(0, 1, n_samples)[:, np.newaxis] y = 3 * x + 1 noise = random_state.randn(n_samples, 1) * 0.01 y += noise samples = np.hstack((x, x[::-1], y)) mvn = MVN(random_state=random_state) mvn.from_samples(samples) assert_array_almost_equal(mvn.mean, np.array([0.5, 0.5, 2.5]), decimal=2) x_test = np.hstack((x, x[::-1])) pred, cov = mvn.predict(np.array([0, 1]), x_test) mse = np.sum((y - pred) ** 2) / n_samples assert_less(mse, 1e-3) assert_less(cov[0, 0], 0.01)
from gmr import MVN, GMM, plot_error_ellipses if __name__ == "__main__": random_state = check_random_state(0) n_samples = 10 X = np.ndarray((n_samples, 2)) X[:, 0] = np.linspace(0, 2 * np.pi, n_samples) X[:, 1] = 1 - 3 * X[:, 0] + random_state.randn(n_samples) mvn = MVN(random_state=0) mvn.from_samples(X) X_test = np.linspace(0, 2 * np.pi, 100) mean, covariance = mvn.predict(np.array([0]), X_test[:, np.newaxis]) plt.figure(figsize=(10, 5)) plt.subplot(1, 2, 1) plt.title("Linear: $p(Y | X) = \mathcal{N}(\mu_{Y|X}, \Sigma_{Y|X})$") plt.scatter(X[:, 0], X[:, 1]) y = mean.ravel() s = covariance.ravel() plt.fill_between(X_test, y - s, y + s, alpha=0.2) plt.plot(X_test, y, lw=2) n_samples = 100 X = np.ndarray((n_samples, 2)) X[:, 0] = np.linspace(0, 2 * np.pi, n_samples) X[:, 1] = np.sin(X[:, 0]) + random_state.randn(n_samples) * 0.1
from gmr.utils import check_random_state from gmr import MVN, GMM, plot_error_ellipses random_state = check_random_state(0) n_samples = 10 X = np.ndarray((n_samples, 2)) X[:, 0] = np.linspace(0, 2 * np.pi, n_samples) X[:, 1] = 1 - 3 * X[:, 0] + random_state.randn(n_samples) mvn = MVN(random_state=0) mvn.from_samples(X) X_test = np.linspace(0, 2 * np.pi, 100) mean, covariance = mvn.predict(np.array([0]), X_test[:, np.newaxis]) plt.figure(figsize=(10, 5)) plt.subplot(1, 2, 1) plt.title("Linear: $p(Y | X) = \mathcal{N}(\mu_{Y|X}, \Sigma_{Y|X})$") plt.scatter(X[:, 0], X[:, 1]) y = mean.ravel() s = covariance.ravel() plt.fill_between(X_test, y - s, y + s, alpha=0.2) plt.plot(X_test, y, lw=2) n_samples = 100 X = np.ndarray((n_samples, 2)) X[:, 0] = np.linspace(0, 2 * np.pi, n_samples) X[:, 1] = np.sin(X[:, 0]) + random_state.randn(n_samples) * 0.1