Example #1
0
 def testShekelClass(self):
     
     S = Shekel5()
     
     # get 50 latin hypercube samples
     X = lhcSample(S.bounds, 50, seed=2)
     Y = [S.f(x) for x in X]
     
     hyper = [.2, .2, .2, .2]
     noise = 0.1
     
     gkernel = GaussianKernel_ard(hyper)
     # print gkernel.sf2
     GP = GaussianProcess(gkernel, X, Y, noise=noise)
     
     # let's take a look at the trained GP.  first, make sure variance at
     # the samples is determined by noise
     mu, sig2 = GP.posteriors(X)
     for m, s, y in zip(mu, sig2, Y):
         # print m, s
         self.failUnless(s < 1/(1+noise))
         self.failUnless(abs(m-y) < 2*noise)
     
     # now get some test samples and see how well we are fitting the function
     testX = lhcSample(S.bounds, 50, seed=3)
     testY = [S.f(x) for x in X]
     for tx, ty in zip(testX, testY):
         m, s = GP.posterior(tx)
         # prediction should be within one stdev of mean
         self.failUnless(abs(ty-m)/sqrt(s) < 1)
Example #2
0
    def testShekelClass(self):

        S = Shekel5()

        # get 50 latin hypercube samples
        X = lhcSample(S.bounds, 50, seed=2)
        Y = [S.f(x) for x in X]

        hyper = [.2, .2, .2, .2]
        noise = 0.1

        gkernel = GaussianKernel_ard(hyper)
        # print gkernel.sf2
        GP = GaussianProcess(gkernel, X, Y, noise=noise)

        # let's take a look at the trained GP.  first, make sure variance at
        # the samples is determined by noise
        mu, sig2 = GP.posteriors(X)
        for m, s, y in zip(mu, sig2, Y):
            # print m, s
            self.failUnless(s < 1 / (1 + noise))
            self.failUnless(abs(m - y) < 2 * noise)

        # now get some test samples and see how well we are fitting the function
        testX = lhcSample(S.bounds, 50, seed=3)
        testY = [S.f(x) for x in X]
        for tx, ty in zip(testX, testY):
            m, s = GP.posterior(tx)
            # prediction should be within one stdev of mean
            self.failUnless(abs(ty - m) / sqrt(s) < 1)
Example #3
0
    def testShekelGPPrior(self):
        
        # see how the GP works on the Shekel function
        S5 = Shekel5()

        pX = lhcSample(S5.bounds, 100, seed=8)
        pY = [S5.f(x) for x in pX]
        prior = RBFNMeanPrior()
        prior.train(pX, pY, S5.bounds, k=10, seed=103)
        
        hv = .1
        hyper = [hv, hv, hv, hv]
        gkernel = GaussianKernel_ard(hyper)
        X = lhcSample(S5.bounds, 10, seed=9)
        Y = [S5.f(x) for x in X]
        priorGP = GaussianProcess(gkernel, X, Y, prior=prior)
        nopriorGP = GaussianProcess(gkernel, X, Y, prior=None)
        
        
        S = lhcSample(S5.bounds, 1000, seed=10)
        nopriorErr = mean([(S5.f(x)-nopriorGP.mu(x))**2 for x in S])
        priorErr = mean([(S5.f(x)-priorGP.mu(x))**2 for x in S])
        
        # print '\nno prior Err =', nopriorErr
        # print 'prior Err =', priorErr
        self.failUnless(priorErr < nopriorErr*.8)
Example #4
0
 def testFromEmptyGP(self):
     # test a GP that has no data to start
     f = lambda x: float(sin(x*10)+x)
     kernel = GaussianKernel_iso(array([1.0]))
     GP = GaussianProcess(kernel)
     
     for x in arange(0., 1., .1):
         GP.addData(array([x]), f(x))
         
     for x in arange(1., 2., .1):
         GP.addData(array([x]), f(x))
     
     self.failUnlessEqual(len(GP.X), 20)
Example #5
0
    def test1DGP(self):
        f = lambda x: float(sin(x * 5.))
        X = lhcSample([[0., 1.]], 5, seed=25)
        Y = [f(x) for x in X]

        kernel = GaussianKernel_ard(array([1.0, 1.0]))
        GP = GaussianProcess(kernel, X=X, Y=Y)
Example #6
0
    def testShekelGPPrior(self):

        # see how the GP works on the Shekel function
        S5 = Shekel5()

        pX = lhcSample(S5.bounds, 100, seed=8)
        pY = [S5.f(x) for x in pX]
        prior = RBFNMeanPrior()
        prior.train(pX, pY, S5.bounds, k=10, seed=103)

        hv = .1
        hyper = [hv, hv, hv, hv]
        gkernel = GaussianKernel_ard(hyper)
        X = lhcSample(S5.bounds, 10, seed=9)
        Y = [S5.f(x) for x in X]
        priorGP = GaussianProcess(gkernel, X, Y, prior=prior)
        nopriorGP = GaussianProcess(gkernel, X, Y, prior=None)

        S = lhcSample(S5.bounds, 1000, seed=10)
        nopriorErr = mean([(S5.f(x) - nopriorGP.mu(x))**2 for x in S])
        priorErr = mean([(S5.f(x) - priorGP.mu(x))**2 for x in S])

        # print '\nno prior Err =', nopriorErr
        # print 'prior Err =', priorErr
        self.failUnless(priorErr < nopriorErr * .8)
Example #7
0
    def testFromEmptyGP(self):
        # test a GP that has no data to start
        f = lambda x: float(sin(x * 10) + x)
        kernel = GaussianKernel_iso(array([1.0]))
        GP = GaussianProcess(kernel)

        for x in arange(0., 1., .1):
            GP.addData(array([x]), f(x))

        for x in arange(1., 2., .1):
            GP.addData(array([x]), f(x))

        self.failUnlessEqual(len(GP.X), 20)
Example #8
0
    def testGPPrior(self):

        # see how GP works with the dataprior...
        def foo(x):
            return sum(sin(x * 20))

        bounds = [[0., 1.]]
        # train prior
        pX = lhcSample([[0., 1.]], 100, seed=6)
        pY = [foo(x) for x in pX]
        prior = RBFNMeanPrior()
        prior.train(pX, pY, bounds, k=10, seed=102)

        X = lhcSample([[0., 1.]], 2, seed=7)
        Y = [foo(x) for x in X]

        kernel = GaussianKernel_ard(array([.1]))
        GP = GaussianProcess(kernel, X, Y, prior=prior)
        GPnoprior = GaussianProcess(kernel, X, Y)

        S = arange(0, 1, .01)

        nopriorErr = mean([(foo(x) - GPnoprior.mu(x))**2 for x in S])
        priorErr = mean([(foo(x) - GP.mu(x))**2 for x in S])

        # print '\nno prior Err =', nopriorErr
        # print 'prior Err =', priorErr

        self.failUnless(priorErr < nopriorErr * .5)

        if False:
            figure(1)
            clf()
            plot(S, [prior.mu(x) for x in S], 'g-', alpha=0.3)
            plot(S, [GPnoprior.mu(x) for x in S], 'b-', alpha=0.3)
            plot(S, [GP.mu(x) for x in S], 'k-', lw=2)
            plot(X, Y, 'ko')
            show()
Example #9
0
    def testGPPrior(self):
        
        # see how GP works with the dataprior...
        def foo(x):
            return sum(sin(x*20))
        
        bounds = [[0., 1.]]
        # train prior
        pX = lhcSample([[0., 1.]], 100, seed=6)
        pY = [foo(x) for x in pX]
        prior = RBFNMeanPrior()
        prior.train(pX, pY, bounds, k=10, seed=102)
        
        X = lhcSample([[0., 1.]], 2, seed=7)
        Y = [foo(x) for x in X]
        
        kernel = GaussianKernel_ard(array([.1]))
        GP = GaussianProcess(kernel, X, Y, prior=prior)
        GPnoprior = GaussianProcess(kernel, X, Y)

        S = arange(0, 1, .01)

        nopriorErr = mean([(foo(x)-GPnoprior.mu(x))**2 for x in S])
        priorErr = mean([(foo(x)-GP.mu(x))**2 for x in S])
        
        # print '\nno prior Err =', nopriorErr
        # print 'prior Err =', priorErr
        
        self.failUnless(priorErr < nopriorErr*.5)
        
        if False:
            figure(1)
            clf()
            plot(S, [prior.mu(x) for x in S], 'g-', alpha=0.3)
            plot(S, [GPnoprior.mu(x) for x in S], 'b-', alpha=0.3)
            plot(S, [GP.mu(x) for x in S], 'k-', lw=2)
            plot(X, Y, 'ko')
            show()
Example #10
0
    def testTraining(self):

        # test that sequential training gives the same result as batch

        tf = Shekel5()
        X = lhcSample(tf.bounds, 25, seed=1)
        Y = [tf.f(x) for x in X]

        # GP1 adds all data during initialization
        GP1 = GaussianProcess(GaussianKernel_iso([.1]), X, Y, noise=.2)

        # GP2 adds data one at a time
        GP2 = GaussianProcess(GaussianKernel_iso([.1]), noise=.2)

        # GP3 uses addData()
        GP3 = GaussianProcess(GaussianKernel_iso([.1]), noise=.2)

        # GP4 adds using various methods
        GP4 = GaussianProcess(GaussianKernel_iso([.1]),
                              X[:10],
                              Y[:10],
                              noise=.2)

        for x, y in zip(X, Y):
            GP2.addData(x, y)

        for i in xrange(0, 25, 5):
            GP3.addData(X[i:i + 5], Y[i:i + 5])

        GP4.addData(X[10], Y[10])
        GP4.addData(X[11:18], Y[11:18])
        for i in xrange(18, 25):
            GP4.addData(X[i], Y[i])

        self.failUnless(all(GP1.R == GP2.R))
        self.failUnless(all(GP1.R == GP3.R))
        self.failUnless(all(GP1.R == GP4.R))

        testX = lhcSample(tf.bounds, 25, seed=2)
        for x in testX:
            mu1, s1 = GP1.posterior(x)
            mu2, s2 = GP2.posterior(x)
            mu3, s3 = GP3.posterior(x)
            mu4, s4 = GP4.posterior(x)
            self.failUnlessEqual(mu1, mu2)
            self.failUnlessEqual(mu1, mu3)
            self.failUnlessEqual(mu1, mu4)
            self.failUnlessEqual(s1, s2)
            self.failUnlessEqual(s1, s3)
            self.failUnlessEqual(s1, s4)
Example #11
0
 def testTraining(self):
     
     # test that sequential training gives the same result as batch
     
     tf = Shekel5()
     X = lhcSample(tf.bounds, 25, seed=1)
     Y = [tf.f(x) for x in X]
     
     # GP1 adds all data during initialization
     GP1 = GaussianProcess(GaussianKernel_iso([.1]), X, Y, noise=.2)
     
     # GP2 adds data one at a time
     GP2 = GaussianProcess(GaussianKernel_iso([.1]), noise=.2)
     
     # GP3 uses addData()
     GP3 = GaussianProcess(GaussianKernel_iso([.1]), noise=.2)
     
     # GP4 adds using various methods
     GP4 = GaussianProcess(GaussianKernel_iso([.1]), X[:10], Y[:10], noise=.2)
     
     for x, y in zip(X, Y):
         GP2.addData(x, y)
         
     for i in xrange(0, 25, 5):
         GP3.addData(X[i:i+5], Y[i:i+5])
     
     GP4.addData(X[10], Y[10])
     GP4.addData(X[11:18], Y[11:18])
     for i in xrange(18, 25):
         GP4.addData(X[i], Y[i])
     
     
     self.failUnless(all(GP1.R==GP2.R))
     self.failUnless(all(GP1.R==GP3.R))
     self.failUnless(all(GP1.R==GP4.R))
     
     testX = lhcSample(tf.bounds, 25, seed=2)
     for x in testX:
         mu1, s1 = GP1.posterior(x)
         mu2, s2 = GP2.posterior(x)
         mu3, s3 = GP3.posterior(x)
         mu4, s4 = GP4.posterior(x)
         self.failUnlessEqual(mu1, mu2)
         self.failUnlessEqual(mu1, mu3)
         self.failUnlessEqual(mu1, mu4)
         self.failUnlessEqual(s1, s2)
         self.failUnlessEqual(s1, s3)
         self.failUnlessEqual(s1, s4)