Example #1
0
    def query_server(self, req):
        bbx = (req.point.x-2, req.point.y-2, req.point.z-2,
               req.point.x+2, req.point.y+2, req.point.z+2)
        y = np.empty((0))
        x = np.empty((0,3))
        hits = self.idx.intersection(bbx, objects=True)
        nearest = self.idx.nearest(bbx, 1, objects=True)

        for item in hits:
            y = np.append(y, [item.object.gain], axis=0)
            x = np.append(x, [[item.object.position.x, item.object.position.y, item.object.position.z]], axis = 0)

        yaw = 0
        for item in nearest:
            yaw = item.object.yaw

        if y.shape[0] == 0:
            response = QueryResponse()
            response.mu = 0
            response.sigma = 1
            return response

        xstar = np.array([[req.point.x, req.point.y, req.point.z]])

        mean, sigma = gp.gp(y, x, xstar, self.hyperparam, gp.sqexpkernel)

        response = QueryResponse()
        response.mu = mean
        response.sigma = sigma
        response.yaw = yaw

        return response
Example #2
0
    def evaluate(self, event):
        y = np.empty((0))
        x = np.empty((0, 3))
        xstar = np.empty((0, 3))
        hits = self.idx.intersection(self.bbx, objects=True)
        for item in hits:
            y = np.append(y, [item.object.gain], axis=0)
            x = np.append(x, [[
                item.object.position.x, item.object.position.y,
                item.object.position.z
            ]],
                          axis=0)

        xt = np.arange(self.min[0], self.max[0], self.resolution)
        yt = np.arange(self.min[1], self.max[1], self.resolution)
        zt = [1]

        for xx in xt:
            for yy in yt:
                for zz in zt:
                    xstar = np.append(xstar, [[xx, yy, zz]], axis=0)

        mean, sigma = gp.gp(y, x, xstar, self.hyperparam, gp.sqexpkernel)
        mean_markers = MarkerArray()
        sigma_markers = MarkerArray()
        for id, pts in enumerate(zip(xstar, mean, sigma)):
            mean_markers.markers.append(
                self.np_array_to_marker(id, pts[0], pts[1], max(1 - pts[2],
                                                                0)))
            # sigma_markers.markers.append(self.np_array_to_marker(id, pts[0], pts[2] * 2))

        self.mean_pub.publish(mean_markers)
        self.sigma_pub.publish(sigma_markers)
    def __init__(self, X, y, N_experts, method='poe', beta=None):
        assert N_experts < X.shape[
            0], "you can't have more experts than data points"
        self.method = method
        self.N_experts = N_experts
        self.X = X
        self.y = y
        self.N = X.shape[0]  #Number of training points
        self.M = int(self.N / N_experts)
        self.bcm = False
        #add code to randomly shuffle X and y first
        if method == 'poe':
            self.setBeta(np.ones((N_experts)))
            self.predict = self.predict_poe
        elif method == 'gpoe':
            self.setBeta(beta)
            self.predict = self.predict_poe
        elif method == 'bcm':
            self.bcm = True
            self.setBeta(np.ones((N_experts)))
            self.predict = self.predict_poe
        elif method == 'rbcm':
            self.bcm = True
            self.setBeta(beta)
            self.predict = self.predict_rbcm
        else:
            raise ValueError(
                'Distributing method must be poe, gpoe, bcm, or rbcm')

        self.experts = []
        for i in range(N_experts):
            X_expert = X[i * self.M:(i + 1) * self.M, :]
            y_expert = y[i * self.M:(i + 1) * self.M, :]
            sigma2_n = np.random.random()
            sigma2_s = np.random.random()
            length_scale = np.random.uniform(0, 2)  #how to best initialise?
            params = {}
            params['sigma2_noise'] = 1.0
            params['sigma2_signal'] = 2.0
            params['length_scale'] = 1.0
            an_expert = gp(X_expert, y_expert, "rbf", params)
            # OPTIMIZE HYPERPARAMS
            self.experts.append(an_expert)
Example #4
0
    u, indices = np.unique(np.around(nlmlarr, decimals=3), return_index=True)
    mi = np.argmin(u)
    nlml = u[mi]
    hypdict = hypdictarr[indices[mi]]

    print '############################ Model 1 - Periodic ############################'
    print '                          Number of trials = %3i' % ntrial
    print '             Number of unique convergences = %3i' % len(u)
    print 'Best log marginal likelihood for the model = %.5f' % -nlml
    print '                                 Best mean = %.5f' % hypdict['mean']
    print '                                Best sigma = %.5f' % hypdict['sigma']
    print '                      Best hyperparameters =', np.exp(
        hypdict['hyp'])

    fm, fe, fc, al, nlml = gp.gp(hypdict, covar, time[-delta * points:],
                                 obs[-delta * points:], timegrid)

    fe = np.sqrt(fe**2 + hypdict['sigma']**2)

    w, h = mp.figaspect(0.5)
    fig = mp.figure(figsize=(w, h), facecolor='w')
    mp.fill_between(timegrid, fm + 2 * fe, y2=fm - 2 * fe, color='0.75')
    mp.plot(time, obs, '.')
    mp.plot(timegrid, fm, 'k')
    mp.xlabel('Day in july 2011')
    mp.ylabel('Tide Height (m)')
    mp.axis([20, 30, 0.5, 5])
    mp.savefig('tideheight1.eps')
    mp.clf()

    #Model 2
    plotter(z,ym,ys2,x,y,[-1.9, 1.9, -0.9, 3.9])'''
    ###########################################################
    covfunc = [ ['kernels.covSEiso'] ]
    ## SET (hyper)parameters
    hyp2 = hyperParameters()

    hyp2.cov = np.array([0.0,0.0])
    hyp2.lik = np.array([np.log(0.1)])
    #vargout = min_wrapper(hyp2,gp,'CG',inffunc,[],covfunc,likfunc,x,y,None,None,True)
    #hyp2 = vargout[0]
    hyp2.cov = np.array([-0.993396880620537,0.685943441677086])
    hyp2.lik = np.array([-1.902546786026883])
    #vargout = gp(hyp2,inffunc,[],covfunc,likfunc,x,y,None,None,False)
    #print "nlml2 = ",vargout[0]

    vargout = gp(hyp2,inffunc,[],covfunc,likfunc,x,y,z)
    ym = vargout[0]; ys2 = vargout[1]
    m  = vargout[2]; s2  = vargout[3]
    ## Plot results
    plotter(z,ym,ys2,x,y,[-1.9, 1.9, -0.9, 3.9])
    ###########################################################
    '''covfunc = [ ['kernels.covSEiso'] ]
    hyp = hyperParameters()

    hyp.cov = np.array([0.0,0.0])
    hyp.mean = np.array([0.0,0.0])
    hyp.lik = np.array([np.log(0.1)])

    vargout = min_wrapper(hyp,gp,'BFGS',inffunc,meanfunc,covfunc,likfunc,x,y,None,None,True)
    hyp = vargout[0]
    hyp.mean = np.array([1.1919,1.4625])
Example #6
0
    meanfunc = [ ['means.meanSum'], [ ['means.meanLinear'] , ['means.meanConst'] ] ]
    covfunc  = [ ['kernels.covMatern'] ]
    inffunc  = ['inf.infExact']
    likfunc  = ['lik.likGauss']

    ## SET (hyper)parameters
    hyp = hyperParameters()

    hyp.cov = np.array([np.log(0.25),np.log(1.0),np.log(3.0)])
    hyp.mean = np.array([0.5,1.0])
    sn = 0.1; hyp.lik = np.array([np.log(sn)])

    #_________________________________
    # STANDARD GP:
    ## PREDICTION 
    vargout = gp(hyp,inffunc,meanfunc,covfunc,likfunc,x,y,None,None,False)
    print "nlml = ",vargout[0]
    vargout = gp(hyp,inffunc,meanfunc,covfunc,likfunc,x,y,z)
    ym = vargout[0]; ys2 = vargout[1]
    m  = vargout[2]; s2 = vargout[3]
    ## Plot results
    plotter(z,ym,ys2,x,y,[-1.9, 1.9, -0.9, 3.9])
    
    print "pre" + str(x)
    x = np.delete(x, 0, 0)
    x = np.delete(x, 5, 0)
    print "post" + str(x)
    y = np.delete(y, 0, 0)
    y = np.delete(y, 5, 0)
    vargout = gp(hyp,inffunc,meanfunc,covfunc,likfunc,x,y,None,None,False)
    print "nlml = ",vargout[0]
Example #7
0
    start_heading = NORTH
    grid_size = 10
    max_steps = pow(grid_size, 2) * 2
    harsh = False
    fitness_function = fitness(start_position, start_heading,
                               grid_size, max_steps, harsh)
    selection_function = gp.truncation_selection(keep)
    operations = [(0.10, gp.mutate(functools.partial(random_program, 5))),
                  (0.70, gp.crossover)]

    step_seq = itertools.chain(itertools.repeat(5, 5),
                               itertools.repeat(100, 5),
                               itertools.repeat(1000))
    g = 0
    while True:
        steps = step_seq.next()
        termination_condition = lambda ep, g: g == steps
        population = gp.gp(population, fitness_function, lambda x,y: cmp(y,x),
                           selection_function, operations,
                           termination_condition)
        g += steps
        path = run(population[0], start_position, start_heading,
                   grid_size, max_steps, harsh)
        print 'generation: %d' % g
        fitness = fitness_function(population[0])
        print 'fitness:    %r (%.2f)' % (fitness,
                                         len(set(path)) / pow(grid_size, 2.0)) 
        print 'length:     %d' % len(population[0])
        print
        watchworm.process(grid_size, path)
Example #8
0
    ### STANDARD GP (example 2)                                  ###
    ###----------------------------------------------------------###
    ### USE another covariance function
    covfunc = [ ['kernels.covSEiso'] ]
    
    ### SET (hyper)parameters
    hyp2 = hyperParameters()
    hyp2.cov = np.array([-1.0,0.0])
    hyp2.mean = np.array([0.5,1.0])
    hyp2.lik = np.array([np.log(0.1)])

    ### PREDICTION
    import time
    t0 = time.time()
    print 'prediction'
    vargout = gp(hyp2,inffunc,meanfunc,covfunc,likfunc,x,y,xstar)
    ym = vargout[0]; ys2 = vargout[1]; m  = vargout[2]; s2 = vargout[3]
    
    print time.time() - t0
    
    
    ### PLOT results
    plotter(xstar,ym,ys2,x,y,[-2, 2, -0.9, 3.9])
    
    ### GET negative log marginal likelihood
    [nlml, post] = gp(hyp2,inffunc,meanfunc,covfunc,likfunc,x,y,None,None,False)
    print "nlml2 = ", nlml


    ###----------------------------------------------------------###
    ### STANDARD GP (example 3)                                  ###
Example #9
0
def show_1d(X,y,Xs,ym,ys):
    plt.fill(np.concatenate([Xs, Xs[::-1]]),
             np.concatenate([ym - 1.96*ys,(ym + 1.96*ys)[::-1]]),
             alpha=0.25, fc='k', ec='k', label='95% confidence interval')
    plt.plot(X,y,'b+',ms=10)
    plt.plot(Xs,ym,'b',lw=2)
    plt.grid()
    plt.xlabel('input X')
    plt.ylabel('output y')

def f(x): return x * np.sin(x)
X = np.atleast_2d([1., 3., 5., 6., 7., 8.]).T
y = f(X).ravel()
Xs = np.atleast_2d(np.linspace(0, 10, 2007)).T

from sklearn import gaussian_process
gp = gaussian_process.GaussianProcess(corr='cubic', theta0=1e-2, thetaL=1e-4, thetaU=1e-1, random_start=100)
gp.fit(X,y)
ymn,ys2 = gp.predict(Xs, eval_MSE=True); ysd = np.sqrt(ys2)

from gp import GaussianProcess as gp
gp = gp(X=X,y=y)
post = gp.inference(X,y)
fmu,fs2,ymu,ys2,lp = gp.predict(X,y,Xs)


f0 = plt.figure(0); plt.clf()
show_1d(X,y,Xs,ymn,ysd)
f1 = plt.figure(1); plt.clf()
show_1d(X,y,Xs,ymu,ys2)
Example #10
0
import gp
import numpy as np
import matplotlib.pyplot as plt

hyperparam = gp.HyperParam(l=0.3, sigma_f=1, sigma_n=0.1)

x = np.array([[0.4, 0.4], [-0.6, -0.6]])
y = np.array([0.719, -0.044])

xstar = np.empty([0, 2])
xi = np.arange(-1, 1, 0.01)
for i in xi:
    xstar = np.vstack((xstar, i * np.hstack([1, 1])))

mu, sigma2 = gp.gp(y, x, xstar, hyperparam, gp.sqexpkernel)
sigma = np.sqrt(sigma2)

plt.plot(xi, mu)
plt.fill_between(xi,
                 mu - 2 * sigma,
                 mu + 2 * sigma,
                 facecolor='#D3D3D3',
                 color="#808080")
plt.show()
    pc = plt.contour(t1, t2, np.reshape(p2/(p1+p2), (t1.shape[0],t1.shape[1]) ))
    fig.colorbar(pc)
    plt.grid()
    plt.axis([-4, 4, -4, 4])
    plt.show()

    meanfunc = [ ['means.meanConst'] ] 
    covfunc  = [ ['kernels.covSEard'] ]   
    likfunc = [ ['lik.likErf'] ]
    inffunc = [ ['inf.infEP'] ]

    hyp = hyperParameters()
    hyp.mean = np.array([-2.842117459073954])
    hyp.cov  = np.array([0.051885508906388,0.170633324977413,1.218386482861781])

    vargout = gp(hyp, inffunc, meanfunc, covfunc, likfunc, x, y, t, np.ones((n,1)) )
    a = vargout[0]; b = vargout[1]; c = vargout[2]; d = vargout[3]; lp = vargout[4]

    fig = plt.figure()
    plt.plot(x1[:,0], x1[:,1], 'b+', markersize = 12)
    plt.plot(x2[:,0], x2[:,1], 'r+', markersize = 12)
    pc = plt.contour(t1, t2, np.reshape(np.exp(lp), (t1.shape[0],t1.shape[1]) ))
    fig.colorbar(pc)
    plt.grid()
    plt.axis([-4, 4, -4, 4])
    plt.show()

    u1,u2 = np.meshgrid(np.linspace(-2,2,5),np.linspace(-2,2,5))
    u = np.array(zip(np.reshape(u2,(np.prod(u2.shape),)),np.reshape(u1,(np.prod(u1.shape),)))) 
    del u1, u2
    nu = u.shape[0]
Example #12
0
            kss = self.cov(Xs[idx], diag=True)  # self variance
            Ks = self.cov(Xs[idx], X)  # cross-covariances
            ms = self.mean(Xs[idx])

            al, sW, L, C = self.post.alpha, self.post.sW, self.post.L, self.post.C
            fmu[idx] = ms + np.dot(Ks, al)
            if L == None:
                fs2[idx] = kss + np.sum(Ks * np.dot(Ks, L), axis=1)
            else:
                V = np.linalg.solve(L, sW * Ks.T)
                fs2[idx] = kss - np.sum(V * V, axis=0)
            if ys == 0: yi = 0
            else: yi = ys[idx]
            lp[idx], ymu[idx], ys2[idx] = self.lik.pred(yi, fmu[idx], fs2[idx])
            na += nb

        return fmu, fs2, ymu, ys2, lp


if __name__ == "__main__":

    def f(x):
        return x * np.sin(x)

    X = np.atleast_2d([1., 3., 5., 6., 7., 8.]).T
    y = f(X).ravel()
    Xs = np.atleast_2d(np.linspace(0, 10, 2007)).T
    from gp import GaussianProcess as gp
    gp = gp(mean=1.0 * one())
    post, nlZ, dnlZ = gp.inference(X, y, deriv=True)
    fmu, fs2, ymu, ys2, lp = gp.predict(X, y, Xs)
Example #13
0
    covfunc = [['kernels.covSEard']]
    likfunc = [['lik.likErf']]
    inffunc = [['inf.infEP']]

    hyp = hyperParameters()

    hyp.mean = np.array([0.])
    hyp.cov = np.array([0.0, 0.0, 0.0])
    vargout = min_wrapper(hyp, gp, 'CG', inffunc, meanfunc, covfunc, likfunc,
                          x, y, None, None, True)
    hyp = vargout[0]

    #hyp.mean = np.array([-2.842117459073954])
    #hyp.cov  = np.array([0.051885508906388,0.170633324977413,1.218386482861781])

    vargout = gp(hyp, inffunc, meanfunc, covfunc, likfunc, x, y, t,
                 np.ones((n, 1)))
    a = vargout[0]
    b = vargout[1]
    c = vargout[2]
    d = vargout[3]
    lp = vargout[4]

    fig = plt.figure()
    plt.plot(x1[:, 0], x1[:, 1], 'b+', markersize=12)
    plt.plot(x2[:, 0], x2[:, 1], 'r+', markersize=12)
    pc = plt.contour(t1, t2, np.reshape(np.exp(lp),
                                        (t1.shape[0], t1.shape[1])))
    fig.colorbar(pc)
    plt.grid()
    plt.axis([-4, 4, -4, 4])
    plt.show()
Example #14
0

def f(x):
    return x * np.sin(x)


X = np.atleast_2d([1., 3., 5., 6., 7., 8.]).T
y = f(X).ravel()
Xs = np.atleast_2d(np.linspace(0, 10, 2007)).T

from sklearn import gaussian_process
gp = gaussian_process.GaussianProcess(corr='cubic',
                                      theta0=1e-2,
                                      thetaL=1e-4,
                                      thetaU=1e-1,
                                      random_start=100)
gp.fit(X, y)
ymn, ys2 = gp.predict(Xs, eval_MSE=True)
ysd = np.sqrt(ys2)

from gp import GaussianProcess as gp
gp = gp(X=X, y=y)
post = gp.inference(X, y)
fmu, fs2, ymu, ys2, lp = gp.predict(X, y, Xs)

f0 = plt.figure(0)
plt.clf()
show_1d(X, y, Xs, ymn, ysd)
f1 = plt.figure(1)
plt.clf()
show_1d(X, y, Xs, ymu, ys2)
Example #15
0
    u, indices = np.unique(np.around(nlmlarr, decimals=3), return_index=True)
    mi = np.argmin(u)
    nlml = u[mi]
    hypdict = hypdictarr[indices[mi]]

    print '############################ Model 1 - Periodic ############################'
    print '                          Number of trials = %3i' % ntrial
    print '             Number of unique convergences = %3i' % len(u)
    print 'Best log marginal likelihood for the model = %.5f' % -nlml
    print '                                 Best mean = %.5f' % hypdict['mean']
    print '                                Best sigma = %.5f' % hypdict['sigma']
    print '                      Best hyperparameters =', np.exp(
        hypdict['hyp'])

    fm, fe, fc, al, nlml = gp.gp(hypdict, covar, time, obs, timegrid)
    print nlml
    fe = np.sqrt(fe**2 + hypdict['sigma']**2)

    w, h = mp.figaspect(0.5)
    fig = mp.figure(figsize=(w, h), facecolor='w')
    mp.fill_between(timegrid, fm + 2 * fe, y2=fm - 2 * fe, color='0.75')
    mp.plot(time, obs, '.')
    mp.plot(timegrid, fm, 'k')
    mp.xlabel('Day in july 2011')
    mp.ylabel('Tide Height (m)')
    mp.axis([20, 30, 0.5, 5])
    mp.savefig('tideheight1.eps')
    mp.clf()

    #Model 2
Example #16
0
import matplotlib.pyplot as plt

cwdata = sio.loadmat('cw1d.mat')
X = np.array(cwdata['x'])
Y = np.array(cwdata['y'])

# log(sf), log(sl), log(sn)
hyp_se = [0., -1.0, 0.]
# hyp = [1., 0.01, 10**-1]
# hyp = [1., 0.01, -1]

hyp_prod = [1, 1, 1, 0]

k_se = kernels.covSEardJ(1)
d_se = gp.gp(k_se, X, Y)
d_se.sample_prior(hyp_se, X)

k_prod = kernels.covPeriodicJ(1)
d_prod = gp.gp(k_prod, X, Y)
d_prod.sample_prior(hyp_prod, X)

# Print out the likelihood for a few different hyperparameters
print "Start optimisation"

def opt_callback(x, dx=None, f=None):
    # time.sleep(0.5)
    opt_callback.i += + 1
#     if ((opt_callback.i % 100) == 0):
    if (True):
        sys.stdout.write(str(x) + ' ')
Example #17
0
    meanfunc = [ ['means.meanSum'], [ ['means.meanLinear'] , ['means.meanConst'] ] ]
    covfunc  = [ ['kernels.covMatern'] ]
    inffunc  = ['inf.infExact']
    likfunc  = ['lik.likGauss']

    ## SET (hyper)parameters
    hyp = hyperParameters()

    hyp.cov = np.array([np.log(0.25),np.log(1.0),np.log(3.0)])
    hyp.mean = np.array([0.5,1.0])
    sn = 0.1; hyp.lik = np.array([np.log(sn)])

    #_________________________________
    # STANDARD GP:
    ## PREDICTION 
    vargout = gp(hyp,inffunc,meanfunc,covfunc,likfunc,x,y,None,None,False)
    print "nlml = ",vargout[0]
    vargout = gp(hyp,inffunc,meanfunc,covfunc,likfunc,x,y,z)
    ym = vargout[0]; ys2 = vargout[1]
    m  = vargout[2]; s2 = vargout[3]
    ## Plot results
    plotter(z,ym,ys2,x,y,[-1.9, 1.9, -0.9, 3.9])
    ###########################################################
    covfunc = [ ['kernels.covSEiso'] ]
    ## SET (hyper)parameters
    hyp2 = hyperParameters()

    hyp2.cov = np.array([-1.0,0.0])
    hyp2.lik = np.array([np.log(0.1)])
    vargout = min_wrapper(hyp2,gp,'SCG',inffunc,[],covfunc,likfunc,x,y,None,None,True)
    hyp2 = vargout[0]
Example #18
0
    ## SET (hyper)parameters
    hyp = hyperParameters()

    ## SET (hyper)parameters for covariance and mean
    hyp.cov = np.array([np.log(67.), np.log(66.), np.log(1.3)])
    hyp.mean = np.array([])

    sn = 0.1
    hyp.lik = np.array([np.log(sn)])

    print 'Initial mean = ',hyp.mean
    print 'Initial covariance = ',hyp.cov
    print 'Initial liklihood = ',hyp.lik

    [nlml, post] = gp(hyp,inffunc,meanfunc,covfunc,likfunc,x,y,None,None,False)
    print 'Initial negative log marginal likelihood = ',nlml
    
    ##----------------------------------------------------------##
    ## STANDARD GP (prediction)                                 ##
    ##----------------------------------------------------------## 
    vargout = gp(hyp,inffunc,meanfunc,covfunc,likfunc,x,y,xs)
    ym = vargout[0]; ys2 = vargout[1]
    m  = vargout[2]; s2  = vargout[3]

    HousingPlotter(ym,ys2,ys)

    ##----------------------------------------------------------##
    ## STANDARD GP (training)                                   ##
    ## OPTIMIZE HYPERPARAMETERS                                 ##
    ##----------------------------------------------------------##
Example #19
0
#figure 1

def func(x,sigma,seed=None):
  np.random.seed(seed=seed)
  return (-(1-x)**2 + x + 1.1)/2 + sigma*np.random.randn(len(x))

covar      = cv.SE    #Covariance matrix to be used
x_training = np.array([0.1, 0.31, 0.35, 0.5, 0.9, 1.1])
x_grid     = np.arange(0,1.55,0.05)
x_test     = 1.4
sigma      = 0.1
y          = func(x_training,sigma,seed=23)
f          = func(x_grid,0)
hypdict    = {'hyp':[0,0],'mean':0,'sigma':sigma}
f_test,f_test_sigma,fcovar,alpha,nlml = gp.gp(hypdict,cv.SE,x_training,y,x_test)
f_grid,f_grid_sigma,fcovar,alpha,nlml = gp.gp(hypdict,cv.SE,x_training,y,x_grid)


mp.errorbar(x_training,y,fmt='o',yerr=sigma)
mp.annotate('?',(1.4,1.0))
mp.axis([0, 1.5, 0, 1.5])
mp.xlabel('x')
mp.ylabel('y')
mp.savefig('fig1.eps')
mp.clf()

#figure 2

mp.fill_between(x_grid,f_grid+2*f_grid_sigma,y2=f_grid-2*f_grid_sigma,color='0.75')
mp.errorbar(x_training,y,fmt='.',yerr=sigma)
Example #20
0
        ymu,ys2 = np.zeros(ns),np.zeros(ns)
        lp = np.zeros(ns)
        while na<ns:
            idx = np.arange(na,min(na+nb,ns))
            kss = self.cov(Xs[idx],diag=True)                   # self variance
            Ks = self.cov(Xs[idx],X)                        # cross-covariances
            ms = self.mean(Xs[idx])
            
            al,sW,L,C = self.post.alpha,self.post.sW,self.post.L,self.post.C
            fmu[idx] = ms + np.dot(Ks,al)
            if L==None:
                fs2[idx] = kss + np.sum(Ks*np.dot(Ks,L),axis=1)
            else:
                V = np.linalg.solve(L,sW*Ks.T)
                fs2[idx] = kss - np.sum(V*V,axis=0)
            if ys==0: yi = 0
            else:     yi = ys[idx]
            lp[idx],ymu[idx],ys2[idx] = self.lik.pred(yi,fmu[idx],fs2[idx])
            na += nb

        return fmu,fs2,ymu,ys2,lp

if __name__ == "__main__":
    def f(x): return x * np.sin(x)
    X = np.atleast_2d([1., 3., 5., 6., 7., 8.]).T
    y = f(X).ravel()
    Xs = np.atleast_2d(np.linspace(0, 10, 2007)).T
    from gp import GaussianProcess as gp
    gp = gp(mean=1.0*one())
    post,nlZ,dnlZ = gp.inference(X,y,deriv=True)
    fmu,fs2,ymu,ys2,lp = gp.predict(X,y,Xs)
Example #21
0
    ## SET (hyper)parameters for covariance and mean 
    hyp.cov = np.array([np.log(67.), np.log(66.), np.log(1.3), np.log(1.0), np.log(2.4), np.log(90.), np.log(2.4), \
                np.log(1.2), np.log(0.66), np.log(0.78), np.log(1.6/12.), np.log(0.18), np.log(0.19)])
    hyp.mean = np.array([])

    sn = 0.1
    hyp.lik = np.array([np.log(sn)])

  
    ##----------------------------------------------------------##
    ## STANDARD GP (prediction)                                 ##
    ##----------------------------------------------------------## 
    xs = np.arange(2004+1./24.,2024-1./24.,1./12.)     # TEST POINTS
    xs = xs.reshape(len(xs),1)

    vargout = gp(hyp,inffunc,meanfunc,covfunc,likfunc,x,y,xs)
    ym = vargout[0]; ys2 = vargout[1]
    m  = vargout[2]; s2  = vargout[3]
    plotter(xs,ym,ys2,x,y)
    
    ##----------------------------------------------------------##
    ## STANDARD GP (training)                                   ##
    ## OPTIMIZE HYPERPARAMETERS                                 ##
    ##----------------------------------------------------------##
    ## -> parameter training using (off the shelf) conjugent gradient (CG) optimization (NOTE: SCG is faster)
    from time import clock
    t0 = clock()
    vargout = min_wrapper(hyp,gp,'SCG',inffunc,meanfunc,covfunc,likfunc,x,y,None,None,True)
    t1 = clock()

    hyp = vargout[0]
Example #22
0
    ## SET (hyper)parameters
    hyp = hyperParameters()

    ## SET (hyper)parameters for covariance and mean
    hyp.cov = np.array([np.log(67.), np.log(66.), np.log(1.3), np.log(1.0), np.log(2.4), np.log(90.), np.log(2.4), \
                np.log(1.2), np.log(0.66), np.log(0.78), np.log(1.6/12.), np.log(0.18), np.log(0.19)])
    hyp.mean = np.array([])

    sn = 0.1
    hyp.lik = np.array([np.log(sn)])

    #_________________________________
    # STANDARD GP:
    ### TEST POINTS
    xs = np.arange(2004+1./24.,2024-1./24.,1./12.)
    xs = xs.reshape(len(xs),1)

    vargout = gp(hyp,inffunc,meanfunc,covfunc,likfunc,x,y,xs)
    ym = vargout[0]; ys2 = vargout[1]
    m  = vargout[2]; s2  = vargout[3]
    plotter(xs,ym,ys2,x,y)
    
    #vargout = min_wrapper(hyp,gp,'CG',inffunc,meanfunc,covfunc,likfunc,x,y,None,None,True)
    #hyp = vargout[0]
    #vargout = gp(hyp,inffunc,meanfunc,covfunc,likfunc,x,y,xs)
    #ym = vargout[0]; ys2 = vargout[1]
    #m  = vargout[2]; s2  = vargout[3]
    #plotter(xs,ym,ys2,x,y)