Beispiel #1
0
    def _fit_gp(X, covX, t):
        xx, xy, yy = covX[0, 0], covX[0, 1], covX[1, 1]

        # Perform hyper-parameter optimization with different
        # initial points and choose the GP with best model evidence
        theta0 = np.array((t.std(), sqrt(xx), sqrt(yy), xy, .01))

        from gp import GaussianProcess, sqexp2D_covariancef
        best_gp = GaussianProcess.fit(X, t, sqexp2D_covariancef, theta0)

        from sklearn.model_selection import ParameterSampler
        from scipy.stats import uniform
        grid = {
            'sigmaf': uniform(0.1 * t.std(), 10 * t.std()),
            'cov_xx': uniform(50, 2000),
            'cov_yy': uniform(50, 2000),
            'noise_prec': uniform(0.1, 10)
        }

        for i, sample in enumerate(ParameterSampler(grid, n_iter=500)):
            sys.stdout.write('iter: %d/500 evidence: %.4f\r' %
                             (i, best_gp.model_evidence()))
            sys.stdout.flush()

            theta0 = np.array((sample['sigmaf'], sample['cov_xx'],
                               sample['cov_yy'], 0, sample['noise_prec']))
            gp = GaussianProcess.fit(X, t, sqexp2D_covariancef, theta0)
            if gp.model_evidence() > best_gp.model_evidence():
                best_gp = gp

        return best_gp
Beispiel #2
0
    def _fit_gp(X, covX, t):
        xx, xy, yy = covX[0,0], covX[0,1], covX[1,1]

        # Perform hyper-parameter optimization with different
        # initial points and choose the GP with best model evidence
        theta0 = np.array(( t.std(), sqrt(xx), sqrt(yy), xy, 10. ))
        best_gp = GaussianProcess.fit(X, t, sqexp2D_covariancef, theta0)

        for tau in xrange(50, 800, 100):
            theta0 = np.array(( t.std(), tau, tau, 0, 10. ))
            gp = GaussianProcess.fit(X, t, sqexp2D_covariancef, theta0)
            if gp.model_evidence() > best_gp.model_evidence():
                best_gp = gp

        return best_gp
Beispiel #3
0
class TestGP:
    def setUp(self):
        self.gp = GaussianProcess()

    def test_sparse(self):
        N = 1000
        x = 1000*np.random.rand(N)
        y = np.sin(x) + 0.1*np.random.randn(N)

        # do it using dense algebra
        Kxx = self.gp.K(x,x).todense()
        alpha = np.linalg.solve(Kxx,y)

        # sparse algebra
        self.gp.fit(x,y)
        assert np.linalg.norm(alpha-self.gp._alpha) < 1e-10
import numpy as np
from matplotlib import pyplot
from gp import GaussianProcess, sqexp1D_covariancef


if __name__ == "__main__":
    # --------------------------------------
    x = (np.random.random(100) - 0.5) * 2
    t = np.array(map(lambda a: a ** 2 + 0.05 * np.random.randn(), x))
    t = t - np.mean(t)

    # Find the best set of hyper-parameters
    theta0 = [np.std(t), 1, 10]
    gp = GaussianProcess.fit(x, t, sqexp1D_covariancef, theta0)

    # Plot predictions and samples from the Gaussian Process
    q = np.arange(-1.2, 1.2, 0.051)
    mean, cov = gp.predict(q, cov=True)
    assert (mean == gp.predict(q)).all()

    sig_bnd = np.sqrt(np.diag(cov))

    for s in np.random.multivariate_normal(mean, cov, 5):
        pyplot.plot(q, s, "y-")

    pyplot.plot(x, t, ".")
    pyplot.plot(q, mean, "r-")
    pyplot.plot(q, mean + 2 * sig_bnd, "k-")
    pyplot.plot(q, mean - 2 * sig_bnd, "k-")
    pyplot.show(block=True)
Beispiel #5
0
from gp import GaussianProcess, sqexp2D_covariancef

if __name__ == "__main__":
    #--------------------------------------
    # sample training data
    x = np.array([
        np.array([i, j]) for i in np.arange(-1, 1, 0.2)
        for j in np.arange(-1, 1, 0.2)
    ])
    t = np.array(
        [r[0]**2 + r[1]**2 + r[0] * r[1] + 0.1 * np.random.randn() for r in x])
    t = t - np.mean(t)

    # Find the best set of hyper-parameters
    theta0 = [np.std(t), 1, 1, 0, 10]
    gp = GaussianProcess.fit(x, t, sqexp2D_covariancef, theta0)
    print gp.covf.theta

    # Plot predictions and samples from the Gaussian Process
    q = np.array([
        np.array([i, j]) for i in np.arange(-1.1, 1.1, 0.2)
        for j in np.arange(-1.1, 1.1, 0.2)
    ])
    mean, cov = gp.predict(q, cov=True)
    assert (mean == gp.predict(q)).all()

    sig_bnd = np.sqrt(np.diag(cov))

    mlab.points3d(x[:, 0], x[:, 1], t, t, scale_mode='none', scale_factor=0.01)

    pts = mlab.points3d(q[:, 0],