def gpmapasrecc(optstate, **para): if para["onlyafter"] > len(optstate.y) or not len(optstate.y) % para["everyn"] == 0: return [sp.NaN for i in para["lb"]], {"didnotrun": True} logger.info("gpmapas reccomender") d = len(para["lb"]) x = sp.hstack([sp.vstack(optstate.x), sp.vstack([e["xa"] for e in optstate.ev])]) y = sp.vstack(optstate.y) s = sp.vstack([e["s"] for e in optstate.ev]) dx = [e["d"] for e in optstate.ev] MAP = GPdc.searchMAPhyp(x, y, s, dx, para["mprior"], para["sprior"], para["kindex"]) logger.info("MAPHYP {}".format(MAP)) G = GPdc.GPcore(x, y, s, dx, GPdc.kernel(para["kindex"], d + 1, MAP)) def directwrap(xq, y): xq.resize([1, d]) xe = sp.hstack([xq, sp.array([[0.0]])]) # print xe a = G.infer_m(xe, [[sp.NaN]]) return (a[0, 0], 0) [xmin, ymin, ierror] = DIRECT.solve( directwrap, para["lb"], para["ub"], user_data=[], algmethod=1, volper=para["volper"], logfilename="/dev/null" ) logger.info("reccsearchresult: {}".format([xmin, ymin, ierror])) return [i for i in xmin], {"MAPHYP": MAP, "ymin": ymin}
def ojf(x, s, d, override=False): #print "called ojf: "+str(x) try: x = x.flatten(0) except: pass xlow = [-2., -2.] xupp = [2., 2.] xthis = [ xlow[i] + 0.5 * (xin + 1) * (xupp[i] - xlow[i]) for i, xin in enumerate(x) ] hyp = [10**i for i in xthis] print hyp t0 = time.clock() llk = sp.clip( GPdc.GP_LKonly(X, Y, S, D, GPdc.kernel(GPdc.MAT52, 1, sp.array(hyp))).plk(pm, ps), -1e60, 1e60) t1 = time.clock() if llk < -1.: out = sp.log(-llk) + 1. else: out = -llk print "--->llk: {0} {1} t: {2}".format(llk, out, t1 - t0) return [out, t1 - t0]
def genbiasedmat52ojf(d, lb, ub, sls): # s normalised to 0 exact, 1 from ESutils import gen_dataset nt = 20 [X, Y, S, D] = gen_dataset(nt, d + 1, [0.0] + lb, [1.0] + ub, GPdc.MAT52, sp.array([1.5] + [0.30] * d + [sls])) G = GPdc.GPcore(X, Y, S, D, GPdc.kernel(GPdc.MAT52, d + 1, sp.array([1.5] + [0.30] * d + [sls]))) def ojf(x, **ev): # print "\nojfinput: {} : {}".format(x,ev) dx = ev["d"] s = ev["s"] if ev["s"] > 0: noise = sp.random.normal(scale=sp.sqrt(ev["s"])) else: noise = 0 xa = ev["xa"] x = sp.array(x) xfull = sp.hstack([x, xa]) return G.infer_m(xfull, [dx])[0, 0] + noise, 1.0, dict() def dirwrap(x, y): z = G.infer_m(sp.hstack(sp.array(x) + [0.0]), [[sp.NaN]])[0, 0] return (z, 0) [xmin, ymin, ierror] = DIRECT.solve(dirwrap, lb, ub, user_data=[], algmethod=1, maxf=89000, logfilename="/dev/null") logger.info("generated function xmin {} ymin {}".format(xmin, ymin)) return ojf, xmin, ymin
def genmat52ojf(d, lb, ub): from ESutils import gen_dataset nt = 14 [X, Y, S, D] = gen_dataset(nt, d, lb, ub, GPdc.MAT52, sp.array([1.5] + [0.55] * d)) G = GPdc.GPcore(X, Y, S, D, GPdc.kernel(GPdc.MAT52, d, sp.array([1.5] + [0.55] * d))) def ojf(x, **ev): dx = ev["d"] s = ev["s"] if ev["s"] > 0: noise = sp.random.normal(scale=sp.sqrt(ev["s"])) else: noise = 0 return G.infer_m(sp.array(x), [dx])[0, 0] + noise, 1.0, dict() def dirwrap(x, y): z = G.infer_m(x, [[sp.NaN]])[0, 0] # z = obj(x,0.,[sp.NaN]) return (z, 0) [xmin, ymin, ierror] = DIRECT.solve(dirwrap, lb, ub, user_data=[], algmethod=1, maxf=89000, logfilename="/dev/null") logger.info("generated function xmin {} ymin {} {}".format(xmin, ymin, ierror)) return ojf, xmin, ymin
def gensquexpdraw(d, lb, ub, ignores=-1): nt = 14 [X, Y, S, D] = ESutils.gen_dataset(nt, d, lb, ub, GPdc.SQUEXP, sp.array([1.5] + [0.30] * d)) G = GPdc.GPcore(X, Y, S, D, GPdc.kernel(GPdc.SQUEXP, d, sp.array([1.5] + [0.30] * d))) def obj(x, s, d, override=False): #print [x,s,d] if ignores > 0: s = ignores if s == 0. or override: noise = 0. else: noise = sp.random.normal(scale=sp.sqrt(s)) print "EVAL WITH NOISE: " + str(noise) + "FROM S= " + str(s) return [G.infer_m(x, [d])[0, 0] + noise, 1.] def dirwrap(x, y): z = G.infer_m(x, [[sp.NaN]])[0, 0] #z = obj(x,0.,[sp.NaN]) return (z, 0) [xmin, ymin, ierror] = DIRECT.solve(dirwrap, lb, ub, user_data=[], algmethod=1, maxf=89000, logfilename='/dev/null') return [obj, xmin, ymin]
def EIMAPaq(optstate,persist,ev=None, ub = None, lb=None, nrandinit=None, mprior=None,sprior=None,kindex = None,directmaxiter=None): para = copy.deepcopy(para) if persist==None: persist = {'n':0,'d':len(ub)} n = persist['n'] d = persist['d'] if n<nrandinit: persist['n']+=1 return randomaq(optstate,persist,ev=ev,lb=lb,ub=ub) logger.info('EIMAPaq') #logger.debug(sp.vstack([e[0] for e in optstate.ev])) #raise x=sp.vstack(optstate.x) y=sp.vstack(optstate.y) s= sp.vstack([e['s'] for e in optstate.ev]) dx=[e['d'] for e in optstate.ev] MAP = GPdc.searchMAPhyp(x,y,s,dx,mprior,sprior, kindex) logger.info('MAPHYP {}'.format(MAP)) G = GPdc.GPcore(x,y,s,dx,GPdc.kernel(kindex,d,MAP)) def directwrap(xq,y): xq.resize([1,d]) a = G.infer_lEI(xq,[ev['d']]) return (-a[0,0],0) [xmin,ymin,ierror] = DIRECT.solve(directwrap,lb,ub,user_data=[], algmethod=0, maxf = directmaxiter, logfilename='/dev/null') #logger.debug([xmin,ymin,ierror]) persist['n']+=1 return [i for i in xmin],ev,persist,{'MAPHYP':MAP,'logEImin':ymin,'DIRECTmessage':ierror}
def gensquexpIPdraw(d,lb,ub,sl,su,sfn,sls,cfn): #axis = 0 value = sl #d dimensional objective +1 for s nt=25 #print sp.hstack([sp.array([[sl]]),lb]) #print sp.hstack([sp.array([[su]]),ub]) [X,Y,S,D] = ESutils.gen_dataset(nt,d+1,sp.hstack([sp.array([[sl]]),lb]).flatten(),sp.hstack([sp.array([[su]]),ub]).flatten(),GPdc.SQUEXP,sp.array([1.5]+[sls]+[0.30]*d)) G = GPdc.GPcore(X,Y,S,D,GPdc.kernel(GPdc.SQUEXP,d+1,sp.array([1.5]+[sls]+[0.30]*d))) def obj(x,s,d,override=False): x = x.flatten() if sfn(x)==0. or override: noise = 0. else: noise = sp.random.normal(scale=sp.sqrt(sfn(x))) return [G.infer_m(x,[d])[0,0]+noise,cfn(x)] def dirwrap(x,y): z = obj(sp.array([[sl]+[i for i in x]]),sl,[sp.NaN],override=True) return (z,0) [xmin0,ymin0,ierror] = DIRECT.solve(dirwrap,lb,ub,user_data=[], algmethod=1, maxf=89000, logfilename='/dev/null') lb2 = xmin0-sp.ones(d)*1e-4 ub2 = xmin0+sp.ones(d)*1e-4 [xmin,ymin,ierror] = DIRECT.solve(dirwrap,lb2,ub2,user_data=[], algmethod=1, maxf=89000, logfilename='/dev/null') #print "RRRRR"+str([xmin0,xmin,ymin0,ymin,xmin0-xmin,ymin0-ymin]) return [obj,xmin,ymin]
def bounds(Xs,Ys,ns=100): #use a gp to infer mean and bounds on sets of x/y data that have diffent x #f,a = plt.subplots(2) #for i in xrange(len(Ys)): # a[0].plot(Xs[i],Ys[i]) X = sp.hstack(Xs) np = X.size Y = sp.hstack(Ys) X.resize([np,1]) Y.resize([np,1]) #a[1].plot(X,Y,'r.') np = X.size S = sp.zeros(np) D = [[sp.NaN]]*np ki = GPdc.MAT52CS mprior = sp.array([1.,2.,1.]) sprior = sp.array([2.,2.,2.]) #MAPH = GPdc.searchMAPhyp(X,Y,S,D,mprior,sprior, ki,mx=500) MAPH = sp.array([0.5,5.,0.3]) g = GPdc.GPcore(X,Y,S,D,GPdc.kernel(ki,1,MAPH)) sup = sp.linspace(min(X),max(X),ns) [m,V] = g.infer_diag_post(sup,[[sp.NaN]]*ns) std = sp.sqrt(V+MAPH[2]) #plt.fill_between(sup.flatten(),(m-std).flatten(),(m+std).flatten(),facecolor='lightblue',edgecolor='lightblue',alpha=0.5) #a[1].plot(sup,m.flatten(),'b') return [sup,m,std]
def gpmapasrecc(optstate, **para): if para['onlyafter'] > len( optstate.y) or not len(optstate.y) % para['everyn'] == 0: return [sp.NaN for i in para['lb']], {'didnotrun': True} logger.info('gpmapas reccomender') d = len(para['lb']) x = sp.hstack( [sp.vstack(optstate.x), sp.vstack([e['xa'] for e in optstate.ev])]) y = sp.vstack(optstate.y) s = sp.vstack([e['s'] for e in optstate.ev]) dx = [e['d'] for e in optstate.ev] MAP = GPdc.searchMAPhyp(x, y, s, dx, para['mprior'], para['sprior'], para['kindex']) logger.info('MAPHYP {}'.format(MAP)) G = GPdc.GPcore(x, y, s, dx, GPdc.kernel(para['kindex'], d + 1, MAP)) def directwrap(xq, y): xq.resize([1, d]) xe = sp.hstack([xq, sp.array([[0.]])]) #print xe a = G.infer_m(xe, [[sp.NaN]]) return (a[0, 0], 0) [xmin, ymin, ierror] = DIRECT.solve(directwrap, para['lb'], para['ub'], user_data=[], algmethod=1, volper=para['volper'], logfilename='/dev/null') logger.info('reccsearchresult: {}'.format([xmin, ymin, ierror])) return [i for i in xmin], {'MAPHYP': MAP, 'ymin': ymin}
def ojf(x,s,d,override=False): #print "called ojf: "+str(x) try: x=x.flatten(0) except: pass xlow = [-2.,-2.] xupp = [2.,2.] xthis = [xlow[i]+0.5*(xin+1)*(xupp[i]-xlow[i]) for i,xin in enumerate(x)] hyp = [10**i for i in xthis] print hyp t0=time.clock() llk = sp.clip(GPdc.GP_LKonly(X,Y,S,D,GPdc.kernel(GPdc.MAT52,1,sp.array(hyp))).plk(pm,ps),-1e60,1e60) t1=time.clock() if llk<-1.: out = sp.log(-llk)+1. else: out = -llk print "--->llk: {0} {1} t: {2}".format(llk,out,t1-t0) return [out,t1-t0]
def bounds(Xs, Ys, ns=100): #use a gp to infer mean and bounds on sets of x/y data that have diffent x #f,a = plt.subplots(2) #for i in xrange(len(Ys)): # a[0].plot(Xs[i],Ys[i]) X = sp.hstack(Xs) np = X.size Y = sp.hstack(Ys) X.resize([np, 1]) Y.resize([np, 1]) #a[1].plot(X,Y,'r.') np = X.size S = sp.zeros(np) D = [[sp.NaN]] * np ki = GPdc.MAT52CS mprior = sp.array([1., 2., 1.]) sprior = sp.array([2., 2., 2.]) #MAPH = GPdc.searchMAPhyp(X,Y,S,D,mprior,sprior, ki,mx=500) MAPH = sp.array([0.5, 5., 0.3]) g = GPdc.GPcore(X, Y, S, D, GPdc.kernel(ki, 1, MAPH)) sup = sp.linspace(min(X), max(X), ns) [m, V] = g.infer_diag_post(sup, [[sp.NaN]] * ns) std = sp.sqrt(V + MAPH[2]) #plt.fill_between(sup.flatten(),(m-std).flatten(),(m+std).flatten(),facecolor='lightblue',edgecolor='lightblue',alpha=0.5) #a[1].plot(sup,m.flatten(),'b') return [sup, m, std]
def genmat52ojf(d, lb, ub): from ESutils import gen_dataset nt = 14 [X, Y, S, D] = gen_dataset(nt, d, lb, ub, GPdc.MAT52, sp.array([1.5] + [0.55] * d)) G = GPdc.GPcore(X, Y, S, D, GPdc.kernel(GPdc.MAT52, d, sp.array([1.5] + [0.55] * d))) def ojf(x, **ev): dx = ev['d'] s = ev['s'] if ev['s'] > 0: noise = sp.random.normal(scale=sp.sqrt(ev['s'])) else: noise = 0 return G.infer_m(sp.array(x), [dx])[0, 0] + noise, 1., dict() def dirwrap(x, y): z = G.infer_m(x, [[sp.NaN]])[0, 0] #z = obj(x,0.,[sp.NaN]) return (z, 0) [xmin, ymin, ierror] = DIRECT.solve(dirwrap, lb, ub, user_data=[], algmethod=1, maxf=89000, logfilename='/dev/null') logger.info('generated function xmin {} ymin {} {}'.format( xmin, ymin, ierror)) return ojf, xmin, ymin
def run_search(self): MAP = GPdc.searchMAPhyp(self.X, self.Y, self.S, self.D, self.mprior, self.sprior, self.kindex) try: del (self.G) except: pass self.G = GPdc.GPcore(self.X, self.Y, self.S, self.D, GPdc.kernel(self.kindex, self.d, MAP)) def directwrap(x, y): x.resize([1, self.d]) a = self.G.infer_lEI(x, [[sp.NaN]]) #print [x,a] #print G.infer_diag_post(x,[[sp.NaN]]) return (-a[0, 0], 0) [xmin, ymin, ierror] = DIRECT.solve(directwrap, self.lb, self.ub, user_data=[], algmethod=1, volper=self.volper, logfilename='/dev/null') return [xmin, self.s, [sp.NaN]]
def makeG(X,Y,S,D,kindex,mprior,sprior,nh): #draw hyps based on plk #print "RRRRRRRRRRRRRR"+str([X,Y,S,D,kindex,mprior,sprior,nh]) H = ESutils.drawhyp_plk(X,Y,S,D,kindex,mprior,sprior,nh) G = GPdc.GPcore(X,Y,S,D,[GPdc.kernel(kindex,X.shape[1],i) for i in H]) return G
def gen_dataset(nt, d, lb, ub, kindex, hyp, s=1e-9): X = draw_support(d, lb, ub, nt, SUPPORT_UNIFORM) D = [[sp.NaN]] * (nt) kf = GPdc.kernel(kindex, d, hyp) Kxx = GPdc.buildKsym_d(kf, X, D) Y = spl.cholesky(Kxx, lower=True) * sp.matrix(sps.norm.rvs( 0, 1., nt)).T + sp.matrix(sps.norm.rvs(0, sp.sqrt(s), nt)).T S = sp.matrix([s] * nt).T return [X, Y, S, D]
def gen_dataset(nt, d, lb, ub, kindex, hyp, s=1e-9): X = draw_support(d, lb, ub, nt, SUPPORT_UNIFORM) D = [[sp.NaN]] * (nt) kf = GPdc.kernel(kindex, d, hyp) Kxx = GPdc.buildKsym_d(kf, X, D) Y = ( spl.cholesky(Kxx, lower=True) * sp.matrix(sps.norm.rvs(0, 1.0, nt)).T + sp.matrix(sps.norm.rvs(0, sp.sqrt(s), nt)).T ) S = sp.matrix([s] * nt).T return [X, Y, S, D]
def train_costest(self): print self.X[:,0] X = self.X[:,0].copy().reshape([len(self.C),1]) C = sp.array(self.C) D = [[sp.NaN]]*len(self.C) S = sp.zeros([len(self.C),1]) lbc = sp.array([-3.,-2.,-6.]) ubc = sp.array([3.,2.,1.]) MLEC = GPdc.searchMLEhyp(X,sp.log(C),S,D,lbc,ubc,GPdc.SQUEXPCS,mx=10000) print "MLEC "+str(MLEC) self.ce = GPdc.GPcore(X.copy(),sp.log(C),S,D,GPdc.kernel(GPdc.SQUEXPCS,1,sp.array(MLEC))) #self.ce = GPdc.GPcore(X,C,S,D,GPdc.kernel(GPdc.SQUEXPCS,1,sp.array([1.,0.3,1e-3]))) #self.ce.printc() return
def EIMAPaq(optstate, persist, ev=None, ub=None, lb=None, nrandinit=None, mprior=None, sprior=None, kindex=None, directmaxiter=None): para = copy.deepcopy(para) if persist == None: persist = {'n': 0, 'd': len(ub)} n = persist['n'] d = persist['d'] if n < nrandinit: persist['n'] += 1 return randomaq(optstate, persist, ev=ev, lb=lb, ub=ub) logger.info('EIMAPaq') #logger.debug(sp.vstack([e[0] for e in optstate.ev])) #raise x = sp.vstack(optstate.x) y = sp.vstack(optstate.y) s = sp.vstack([e['s'] for e in optstate.ev]) dx = [e['d'] for e in optstate.ev] MAP = GPdc.searchMAPhyp(x, y, s, dx, mprior, sprior, kindex) logger.info('MAPHYP {}'.format(MAP)) G = GPdc.GPcore(x, y, s, dx, GPdc.kernel(kindex, d, MAP)) def directwrap(xq, y): xq.resize([1, d]) a = G.infer_lEI(xq, [ev['d']]) return (-a[0, 0], 0) [xmin, ymin, ierror] = DIRECT.solve(directwrap, lb, ub, user_data=[], algmethod=0, maxf=directmaxiter, logfilename='/dev/null') #logger.debug([xmin,ymin,ierror]) persist['n'] += 1 return [i for i in xmin], ev, persist, { 'MAPHYP': MAP, 'logEImin': ymin, 'DIRECTmessage': ierror }
def run_search(self): MAP = GPdc.searchMAPhyp(self.X,self.Y,self.S,self.D,self.mprior,self.sprior, self.kindex) try: del(self.G) except: pass self.G = GPdc.GPcore(self.X,self.Y,self.S,self.D,GPdc.kernel(self.kindex,self.d,MAP)) def directwrap(x,y): x.resize([1,self.d]) a = self.G.infer_LCB(x,[[sp.NaN]],1.)[0,0] return (a,0) [xmin,ymin,ierror] = DIRECT.solve(directwrap,self.lb,self.ub,user_data=[], algmethod=1, volper=self.volper, logfilename='/dev/null') return [xmin,self.s,[sp.NaN]]
def llks(self,hy,sub): t0 = time.clock() Xs = sp.empty([sub,self.d]) Ys = sp.empty([sub,1]) Ss = sp.zeros([sub,1]) Ds = [] ix = npr.choice(range(self.n),size=sub, replace=False) for i,x in enumerate(ix): Xs[i,:] = self.X[x,:] Ys[i,:] = self.Y[x,:] Ds.append(self.D[x]) t1 = time.clock() r = GPdc.GP_LKonly(Xs,Ys,Ss,Ds,GPdc.kernel(self.ki,self.d,hy)).llk() t2=time.clock() #print [t1-t0,t2-t1] return [r,t2-t0]
def gensquexpIPdraw(d, lb, ub, sl, su, sfn, sls, cfn): #axis = 0 value = sl #d dimensional objective +1 for s nt = 25 #print sp.hstack([sp.array([[sl]]),lb]) #print sp.hstack([sp.array([[su]]),ub]) [X, Y, S, D] = ESutils.gen_dataset(nt, d + 1, sp.hstack([sp.array([[sl]]), lb]).flatten(), sp.hstack([sp.array([[su]]), ub]).flatten(), GPdc.SQUEXP, sp.array([1.5] + [sls] + [0.30] * d)) G = GPdc.GPcore( X, Y, S, D, GPdc.kernel(GPdc.SQUEXP, d + 1, sp.array([1.5] + [sls] + [0.30] * d))) def obj(x, s, d, override=False): x = x.flatten() if sfn(x) == 0. or override: noise = 0. else: noise = sp.random.normal(scale=sp.sqrt(sfn(x))) return [G.infer_m(x, [d])[0, 0] + noise, cfn(x)] def dirwrap(x, y): z = obj(sp.array([[sl] + [i for i in x]]), sl, [sp.NaN], override=True) return (z, 0) [xmin0, ymin0, ierror] = DIRECT.solve(dirwrap, lb, ub, user_data=[], algmethod=1, maxf=89000, logfilename='/dev/null') lb2 = xmin0 - sp.ones(d) * 1e-4 ub2 = xmin0 + sp.ones(d) * 1e-4 [xmin, ymin, ierror] = DIRECT.solve(dirwrap, lb2, ub2, user_data=[], algmethod=1, maxf=89000, logfilename='/dev/null') #print "RRRRR"+str([xmin0,xmin,ymin0,ymin,xmin0-xmin,ymin0-ymin]) return [obj, xmin, ymin]
def llks(self, hy, sub): t0 = time.clock() Xs = sp.empty([sub, self.d]) Ys = sp.empty([sub, 1]) Ss = sp.zeros([sub, 1]) Ds = [] ix = npr.choice(range(self.n), size=sub, replace=False) for i, x in enumerate(ix): Xs[i, :] = self.X[x, :] Ys[i, :] = self.Y[x, :] Ds.append(self.D[x]) t1 = time.clock() r = GPdc.GP_LKonly(Xs, Ys, Ss, Ds, GPdc.kernel(self.ki, self.d, hy)).llk() t2 = time.clock() #print [t1-t0,t2-t1] return [r, t2 - t0]
def f(loghyp): ub = hm + 1.8 * hs lb = hm - 1.8 * hs if all(loghyp < ub) and all(loghyp > lb): r = GPdc.GP_LKonly(X, Y, S, D, GPdc.kernel(ki, X.shape[1], [10 ** i for i in loghyp])).plk(hm, hs) if sp.isnan(r): class MJMError(Exception): pass print "nan from GPLKonly with input" print [X, Y, S, D, ki, hm, hs, n, burn, subsam] raise MJMError("nan from GPLKonly with input") else: r = -1e99 # print [loghyp, r] return r
def f(loghyp): ub = hm + 1.8 * hs lb = hm - 1.8 * hs if all(loghyp < ub) and all(loghyp > lb): r = GPdc.GP_LKonly( X, Y, S, D, GPdc.kernel(ki, X.shape[1], [10**i for i in loghyp])).plk(hm, hs) if sp.isnan(r): class MJMError(Exception): pass print 'nan from GPLKonly with input' print[X, Y, S, D, ki, hm, hs, n, burn, subsam] raise MJMError('nan from GPLKonly with input') else: r = -1e99 #print [loghyp, r] return r
def ojfa(x, s, d, override=False): #print "called ojf: "+str(x) try: x = x.flatten(0) except: pass xlow = [-2., -2.] xupp = [2., 2.] xthis = [ xlow[i] + 0.5 * (xin + 1) * (xupp[i] - xlow[i]) for i, xin in enumerate(x[1:]) ] hyp = [10**i for i in xthis] #hyp = [10**i for i in x.flatten()[1:]] print hyp t0 = time.clock() sub = x.flatten()[0] npts = int((1. - 0.9 * sub) * n) if override: npts = n print "subsampling {0} of {1} at x[0]={2}".format(npts, n, x.flatten()[0]) ps = npr.choice(range(n), size=npts, replace=False) Xd = sp.vstack([X[i] for i in ps]) Yd = sp.vstack([Y[i] for i in ps]) Sd = sp.vstack([S[i] for i in ps]) Dd = [[sp.NaN]] * npts llk = GPdc.GP_LKonly(Xd, Yd, Sd, Dd, GPdc.kernel(GPdc.MAT52, 1, sp.array(hyp))).plk(pm, ps) t1 = time.clock() if llk < -1.: out = sp.log(-llk) + 1. else: out = -llk print "--->llk: {0} {1} t: {2}".format(llk, out, t1 - t0) return [out, t1 - t0]
def gensquexpdraw(d,lb,ub,ignores=-1): nt=14 [X,Y,S,D] = ESutils.gen_dataset(nt,d,lb,ub,GPdc.SQUEXP,sp.array([1.5]+[0.30]*d)) G = GPdc.GPcore(X,Y,S,D,GPdc.kernel(GPdc.SQUEXP,d,sp.array([1.5]+[0.30]*d))) def obj(x,s,d,override=False): #print [x,s,d] if ignores>0: s=ignores if s==0. or override: noise = 0. else: noise = sp.random.normal(scale=sp.sqrt(s)) print "EVAL WITH NOISE: "+str(noise) + "FROM S= "+str(s) return [G.infer_m(x,[d])[0,0]+noise,1.] def dirwrap(x,y): z = G.infer_m(x,[[sp.NaN]])[0,0] #z = obj(x,0.,[sp.NaN]) return (z,0) [xmin,ymin,ierror] = DIRECT.solve(dirwrap,lb,ub,user_data=[], algmethod=1, maxf=89000, logfilename='/dev/null') return [obj,xmin,ymin]
def train_costest(self): print self.X[:, 0] X = self.X[:, 0].copy().reshape([len(self.C), 1]) C = sp.array(self.C) D = [[sp.NaN]] * len(self.C) S = sp.zeros([len(self.C), 1]) lbc = sp.array([-3., -2., -6.]) ubc = sp.array([3., 2., 1.]) MLEC = GPdc.searchMLEhyp(X, sp.log(C), S, D, lbc, ubc, GPdc.SQUEXPCS, mx=10000) print "MLEC " + str(MLEC) self.ce = GPdc.GPcore(X.copy(), sp.log(C), S, D, GPdc.kernel(GPdc.SQUEXPCS, 1, sp.array(MLEC))) #self.ce = GPdc.GPcore(X,C,S,D,GPdc.kernel(GPdc.SQUEXPCS,1,sp.array([1.,0.3,1e-3]))) #self.ce.printc() return
def ojfa(x,s,d,override=False): #print "called ojf: "+str(x) try: x=x.flatten(0) except: pass xlow = [-2.,-2.] xupp = [2.,2.] xthis = [xlow[i]+0.5*(xin+1)*(xupp[i]-xlow[i]) for i,xin in enumerate(x[1:])] hyp = [10**i for i in xthis] #hyp = [10**i for i in x.flatten()[1:]] print hyp t0=time.clock() sub = x.flatten()[0] npts = int((1.-0.9*sub)*n) if override: npts=n print "subsampling {0} of {1} at x[0]={2}".format(npts,n,x.flatten()[0]) ps = npr.choice(range(n),size=npts, replace=False) Xd = sp.vstack([X[i] for i in ps]) Yd = sp.vstack([Y[i] for i in ps]) Sd = sp.vstack([S[i] for i in ps]) Dd = [[sp.NaN]]*npts llk = GPdc.GP_LKonly(Xd,Yd,Sd,Dd,GPdc.kernel(GPdc.MAT52,1,sp.array(hyp))).plk(pm,ps) t1=time.clock() if llk<-1.: out = sp.log(-llk)+1. else: out = -llk print "--->llk: {0} {1} t: {2}".format(llk,out,t1-t0) return [out,t1-t0]
def genbiasedmat52ojf(d, lb, ub, sls): #s normalised to 0 exact, 1 from ESutils import gen_dataset nt = 20 [X, Y, S, D] = gen_dataset(nt, d + 1, [0.] + lb, [1.] + ub, GPdc.MAT52, sp.array([1.5] + [0.30] * d + [sls])) G = GPdc.GPcore( X, Y, S, D, GPdc.kernel(GPdc.MAT52, d + 1, sp.array([1.5] + [0.30] * d + [sls]))) def ojf(x, **ev): #print "\nojfinput: {} : {}".format(x,ev) dx = ev['d'] s = ev['s'] if ev['s'] > 0: noise = sp.random.normal(scale=sp.sqrt(ev['s'])) else: noise = 0 xa = ev['xa'] x = sp.array(x) xfull = sp.hstack([x, xa]) return G.infer_m(xfull, [dx])[0, 0] + noise, 1., dict() def dirwrap(x, y): z = G.infer_m(sp.hstack(sp.array(x) + [0.]), [[sp.NaN]])[0, 0] return (z, 0) [xmin, ymin, ierror] = DIRECT.solve(dirwrap, lb, ub, user_data=[], algmethod=1, maxf=89000, logfilename='/dev/null') logger.info('generated function xmin {} ymin {}'.format(xmin, ymin)) return ojf, xmin, ymin
from scipy import stats as sps from matplotlib import pyplot as plt import GPdc #test on a single point nt=3 X = ESutils.draw_support(1, sp.array([-1.]),sp.array([1.]),nt,ESutils.SUPPORT_UNIFORM) D = [[sp.NaN]]*(nt) hyp = sp.array([1.5,0.15]) kf = GPdc.gen_sqexp_k_d(hyp) Kxx = GPdc.buildKsym_d(kf,X,D) Y = spl.cholesky(Kxx,lower=True)*sp.matrix(sps.norm.rvs(0,1.,nt)).T+sp.matrix(sps.norm.rvs(0,1e-3,nt)).T S = sp.matrix([1e-6]*nt).T G = GPdc.GPcore(X,Y,S,D,GPdc.kernel(GPdc.SQUEXP,2,hyp)) ng = 40 A = sp.empty([ng,ng]) print 'startimage1' sup = sp.logspace(-3,2,ng) for i,hi in enumerate(sup): for j,hj in enumerate(sup): A[i,j] = GPdc.GP_LKonly(X,Y,S,D,GPdc.kernel(GPdc.SQUEXP,2,sp.array([hi,hj]))).plk(sp.array([0.,-1.]),sp.array([1.,1.])) A = -sp.log10(-A+sp.amax(A)+1.) plt.figure() plt.contour(sp.log10(sup),sp.log10(sup),A,30) print 'draw hyps 1' X = ESutils.drawhyp_plk(X,Y,S,D,GPdc.SQUEXP,sp.array([0.,-1.]),sp.array([1.,1.]),200) plt.plot(sp.log10(X[:,1]),sp.log10(X[:,0]),'b.')
#lb = sp.array([0.,0.,-4.,-0.5*float(nf),float(nf)]) #ub = sp.array([4.,3.,3.,0.,1.5*float(nf)]) #MLEH = GPdc.searchMLEhyp(X,H,sp.zeros([np,1]),[[sp.NaN]]*(np),lb,ub,GPdc.SQUEXPPS,mx=10000) #G = GPdc.GPcore(X.copy(),H,sp.zeros([np,1]),[[sp.NaN]]*(np),GPdc.kernel(GPdc.SQUEXPPS,1,sp.array(MLEH))) lb = sp.array([0., 0., -4., -1., -3.]) ub = sp.array([4., 3., 3., 0.5, 3.]) MLEH = GPdc.searchMLEhyp(1. / float(nf) * X, H, sp.zeros([np, 1]), [[sp.NaN]] * (np), lb, ub, GPdc.SQUEXPBS, mx=10000) G = GPdc.GPcore(1. / float(nf) * X.copy(), H, sp.zeros([np, 1]), [[sp.NaN]] * (np), GPdc.kernel(GPdc.SQUEXPBS, 1, sp.array(MLEH))) [m, v] = G.infer_diag(1. / float(nf) * X, [[sp.NaN]] * (np)) S = sp.empty([np, 1]) for i in xrange(np): S[i, 0] = MLEH[2] * ((1. / float(nf) * X[i, 0])**(MLEH[3] * MLEH[4])) * ( (1. - 1. / float(nf) * X[i, 0])**(MLEH[3] * (1. - MLEH[4]))) lbc = sp.array([-4., 0., -6.]) ubc = sp.array([2., 3., 0.]) MLEC = GPdc.searchMLEhyp(X, sp.log(T), sp.zeros([np, 1]), [[sp.NaN]] * (np), lbc, ubc,
ns = 200 sup = sp.linspace(-1, 1, 200) #points are a high noise obs, a low noise obs, a derivative obs and a second derivative obs X = sp.array([[-0.8], [-0.25], [0.25], [0.8]]) Y = sp.array([[0.3], [-0.2], [2.5], [50.]]) S = sp.array([[1e-1], [1e-6], [1e-6], [1e-6]]) D = [[sp.NaN], [sp.NaN], [0], [0, 0]] a[0].plot([-0.8, -0.8], [0.3 - sp.sqrt(1e-1), 0.3 + sp.sqrt(1e-1)], 'r') a[0].plot([-0.8], [0.3], 'ro') a[0].plot([-0.25], [-0.2], 'ro') a[1].plot([0.25], [2.5], 'ro') a[2].plot([0.8], [50], 'ro') k = GPdc.kernel(GPdc.SQUEXP, 1, sp.array([0.5, 0.2])) K = sp.empty([4, 4]) for i in xrange(4): for j in xrange(i, 4): K[i, j] = K[j, i] = k(X[i, :], X[j, :], D[i], D[j]) K[i, i] += 1e-6 g = GPdc.GPcore(X, Y, S, D, GPdc.kernel(GPdc.SQUEXP, 1, sp.array([0.5, 0.2]))) #g.printc() C = g.get_cho() print C print spl.cho_solve((C, True), sp.eye(4)).dot(K) for i, d in enumerate([[sp.NaN], [0], [0, 0]]): m, v = g.infer_diag(sup, [d] * ns) vl = (m - sp.sqrt(v)).flatten()
import ctypes as ct import scipy as sp from scipy import stats as sps from scipy import linalg as spl from matplotlib import pyplot as plt import os libGP = ct.cdll.LoadLibrary(os.path.join(os.path.dirname(os.path.realpath(__file__)),'../../../dist/Release/GNU-Linux/libGPshared.so')) ctpd = ct.POINTER(ct.c_double) import GPdc ni = 50 kf = GPdc.kernel(GPdc.SQUEXP,1,sp.array([1.3,0.3])) X = sp.matrix(sp.linspace(-1,1,ni)).T D = [[sp.NaN]]*ni Kxx = GPdc.buildKsym_d(kf,X,D) tmp = spl.cholesky(Kxx,lower=True) Ch = sp.vstack([tmp[i,:] for i in xrange(ni)]) #hack/force row major storage z = 5 b = sp.empty([ni,z]) libGP.drawcov(Ch.ctypes.data_as(ctpd),ct.c_int(ni),b.ctypes.data_as(ctpd),ct.c_int(z))
# To change this license header, choose License Headers in Project Properties. # To change this template file, choose Tools | Templates # and open the template in the editor. from scipy import stats as sps from scipy import linalg as spl import scipy as sp from matplotlib import pyplot as plt import GPdc ni = 100 kf = GPdc.kernel(GPdc.SQUEXP,2,sp.array([1.3,0.3,0.2])) X = sp.random.uniform(-1,1,size=[ni,2]) D = [[sp.NaN]]*ni Kxx = GPdc.buildKsym_d(kf,X,D) s = 1e-2 Y = spl.cholesky(Kxx,lower=True)*sp.matrix(sps.norm.rvs(0,1.,ni)).T+sp.matrix(sps.norm.rvs(0,s,ni)).T S = sp.ones(ni)*s print Y MLEHYP = GPdc.searchMLEhyp(X,Y,S,D,sp.array([2.,2.,2.]),sp.array([-2.,-2.,-2.]), GPdc.SQUEXP) print MLEHYP MAPHYP = GPdc.searchMAPhyp(X,Y,S,D,sp.array([0.,0.,0.]),sp.array([1.,1.,1.]), GPdc.SQUEXP) print MAPHYP
#X = sp.matrix([[0.,-9.],[0.2,5.],[0.4,12.],[0.6,3.],[0.8,9.]]) #Y = sp.matrix([0.2,0.1,0.,-0.15,-0.3]).T D = [[sp.NaN]] * (X.shape[0]) S = sp.matrix([0.005**2] * X.shape[0]).T f0 = plt.figure() a0 = plt.subplot(111) a0.plot(sp.array(X[:, 0]).flatten(), Y, 'g.') lb = sp.array([-2., -1., -2.]) ub = sp.array([2., 1., 2.]) MLEH = GPdc.searchMLEhyp(X, Y, S, D, lb, ub, GPdc.LIN1, mx=10000) print MLEH G = GPdc.GPcore(X, Y, S, D, GPdc.kernel(GPdc.LIN1, 2, MLEH)) print G.llk() np = 180 sup = sp.linspace(-1, 1, np) Dp = [[sp.NaN]] * np Xp = sp.vstack([sp.array([i, 1.]) for i in sup]) [m, v] = G.infer_diag(Xp, Dp) a0.plot(sup, m.flatten()) sq = sp.sqrt(v) a0.fill_between(sup, sp.array(m - 1. * sq).flatten(), sp.array(m + 1. * sq).flatten(), facecolor='lightblue',
#Y = sp.matrix([0.2,0.1,0.,-0.15,-0.3]).T D = [[sp.NaN]]*(X.shape[0]) S = sp.matrix([0.005**2]*X.shape[0]).T f0 = plt.figure() a0 = plt.subplot(111) a0.plot(sp.array(X[:,0]).flatten(),Y,'g.') lb = sp.array([-2.,-1.,-2.]) ub = sp.array([2.,1.,2.]) MLEH = GPdc.searchMLEhyp(X,Y,S,D,lb,ub,GPdc.LIN1,mx=10000) print MLEH G = GPdc.GPcore(X,Y,S,D,GPdc.kernel(GPdc.LIN1,2,MLEH)) print G.llk() np=180 sup = sp.linspace(-1,1,np) Dp = [[sp.NaN]]*np Xp = sp.vstack([sp.array([i,1.]) for i in sup]) [m,v] = G.infer_diag(Xp,Dp) a0.plot(sup,m.flatten()) sq = sp.sqrt(v) a0.fill_between(sup, sp.array(m-1.*sq).flatten(), sp.array(m+1.*sq).flatten(), facecolor='lightblue',edgecolor='lightblue') plt.show()
from matplotlib import pyplot as plt import GPdc #test on a single point nt = 3 X = ESutils.draw_support(1, sp.array([-1.]), sp.array([1.]), nt, ESutils.SUPPORT_UNIFORM) D = [[sp.NaN]] * (nt) hyp = sp.array([1.5, 0.15]) kf = GPdc.gen_sqexp_k_d(hyp) Kxx = GPdc.buildKsym_d(kf, X, D) Y = spl.cholesky(Kxx, lower=True) * sp.matrix(sps.norm.rvs( 0, 1., nt)).T + sp.matrix(sps.norm.rvs(0, 1e-3, nt)).T S = sp.matrix([1e-6] * nt).T G = GPdc.GPcore(X, Y, S, D, GPdc.kernel(GPdc.SQUEXP, 2, hyp)) ng = 40 A = sp.empty([ng, ng]) print 'startimage1' sup = sp.logspace(-3, 2, ng) for i, hi in enumerate(sup): for j, hj in enumerate(sup): A[i, j] = GPdc.GP_LKonly(X, Y, S, D, GPdc.kernel(GPdc.SQUEXP, 2, sp.array([hi, hj]))).plk( sp.array([0., -1.]), sp.array([1., 1.])) A = -sp.log10(-A + sp.amax(A) + 1.) plt.figure()
from scipy import stats as sps from scipy import linalg as spl import scipy as sp from matplotlib import pyplot as plt import ESutils import GPdc nt = 82 X = sp.matrix(sp.linspace(-1, 1, nt)).T D = [[sp.NaN]] * (nt) hyp0 = sp.array([1.5, 0.15]) hyp1 = sp.array([1.5, 0.05]) hyp2 = sp.array([1.5, 0.20]) kf = GPdc.kernel(GPdc.SQUEXP, 1, hyp0) Kxx = GPdc.buildKsym_d(kf, X, D) Y = spl.cholesky(Kxx, lower=True) * sp.matrix(sps.norm.rvs( 0, 1., nt)).T + sp.matrix(sps.norm.rvs(0, 1e-3, nt)).T S = sp.matrix([1e-6] * nt).T G = GPdc.GPcore(X, Y, S, D, [ GPdc.kernel(GPdc.SQUEXP, 1, hyp0), GPdc.kernel(GPdc.SQUEXP, 1, hyp1), GPdc.kernel(GPdc.SQUEXP, 1, hyp2), GPdc.kernel(GPdc.SQUEXP, 1, hyp0) ]) #G.printc()
S*=0. f0 = plt.figure() a0 = plt.subplot(111) a0.plot(sp.array(X[:,0]).flatten(),Y,'g.') lb = sp.array([-2.,-2.,-9]) ub = sp.array([2.,2.,-1]) MLEH = GPdc.searchMLEhyp(X,Y,S,D,lb,ub,GPdc.SQUEXPCS,mx=10000) mprior = sp.array([0.,-1.,-5.]) sprior = sp.array([1.,1.,3.]) MAPH = GPdc.searchMAPhyp(X,Y,S,D,mprior,sprior,GPdc.SQUEXPCS,mx=10000) print "MLEH: "+str(MLEH) print "MAPH: "+str(MAPH) G = GPdc.GPcore(X,Y,S,D,GPdc.kernel(GPdc.SQUEXPCS,1,sp.array(MLEH))) print G.llk() np=180 sup = sp.linspace(-1,1,np) Dp = [[sp.NaN]]*np Xp = sp.vstack([sp.array([i]) for i in sup]) [m,v] = G.infer_diag(Xp,Dp) a0.plot(sup,m.flatten()) sq = sp.sqrt(v) a0.fill_between(sup, sp.array(m-1.*sq).flatten(), sp.array(m+1.*sq).flatten(), facecolor='lightblue',edgecolor='lightblue')
from scipy import stats as sps from scipy import linalg as spl import scipy as sp from matplotlib import pyplot as plt import ESutils import GPdc nt=82 X = sp.matrix(sp.linspace(-1,1,nt)).T D = [[sp.NaN]]*(nt) hyp0 = sp.array([1.5,0.15]) hyp1 = sp.array([1.5,0.05]) hyp2 = sp.array([1.5,0.20]) kf = GPdc.kernel(GPdc.SQUEXP,1,hyp0) Kxx = GPdc.buildKsym_d(kf,X,D) Y = spl.cholesky(Kxx,lower=True)*sp.matrix(sps.norm.rvs(0,1.,nt)).T+sp.matrix(sps.norm.rvs(0,1e-3,nt)).T S = sp.matrix([1e-6]*nt).T G = GPdc.GPcore(X,Y,S,D,[GPdc.kernel(GPdc.SQUEXP,1,hyp0),GPdc.kernel(GPdc.SQUEXP,1,hyp1),GPdc.kernel(GPdc.SQUEXP,1,hyp2),GPdc.kernel(GPdc.SQUEXP,1,hyp0)]) #G.printc() np=100 sup = sp.linspace(-1,1,np) Dp = [[sp.NaN]]*np Xp = sp.vstack([sp.array([i]) for i in sup]) m = G.infer_m(Xp,Dp)
X = sp.array([sp.linspace(0,nf,np)]).T H = sp.empty([np,1]) T = sp.empty([np,1]) for i in xrange(np): [H[i,0],T[i,0]] = L.llks(sp.array([1.3,0.13,0.2,1e-2]),int(X[i,0])) #lb = sp.array([0.,0.,-4.,-0.5*float(nf),float(nf)]) #ub = sp.array([4.,3.,3.,0.,1.5*float(nf)]) #MLEH = GPdc.searchMLEhyp(X,H,sp.zeros([np,1]),[[sp.NaN]]*(np),lb,ub,GPdc.SQUEXPPS,mx=10000) #G = GPdc.GPcore(X.copy(),H,sp.zeros([np,1]),[[sp.NaN]]*(np),GPdc.kernel(GPdc.SQUEXPPS,1,sp.array(MLEH))) lb = sp.array([0.,0.,-4.,-1.,-3.]) ub = sp.array([4.,3.,3.,0.5,3.]) MLEH = GPdc.searchMLEhyp(1./float(nf)*X,H,sp.zeros([np,1]),[[sp.NaN]]*(np),lb,ub,GPdc.SQUEXPBS,mx=10000) G = GPdc.GPcore(1./float(nf)*X.copy(),H,sp.zeros([np,1]),[[sp.NaN]]*(np),GPdc.kernel(GPdc.SQUEXPBS,1,sp.array(MLEH))) [m,v] = G.infer_diag(1./float(nf)*X,[[sp.NaN]]*(np)) S = sp.empty([np,1]) for i in xrange(np): S[i,0] = MLEH[2]*((1./float(nf)*X[i,0])**(MLEH[3]*MLEH[4]))* ((1.-1./float(nf)*X[i,0])**(MLEH[3]*(1.-MLEH[4]))) lbc = sp.array([-4.,0.,-6.]) ubc = sp.array([2.,3.,0.]) MLEC = GPdc.searchMLEhyp(X,sp.log(T),sp.zeros([np,1]),[[sp.NaN]]*(np),lbc,ubc,GPdc.SQUEXPCS,mx=10000) C = GPdc.GPcore(X.copy(),sp.log(T),sp.zeros([np,1]),[[sp.NaN]]*(np),GPdc.kernel(GPdc.SQUEXPCS,1,sp.array(MLEC))) f,a = plt.subplots(2) a[0].plot(X.flatten(),H.flatten(),'g.') s = sp.sqrt(v).flatten()
Kxx = GPdc.buildKsym_d(kf,X,D) Y = spl.cholesky(Kxx,lower=True)*sp.matrix(sps.norm.rvs(0,1.,nt)).T+sp.matrix(sps.norm.rvs(0,1e-3,nt)).T S = sp.matrix([1e-6]*nt).T f0 = plt.figure() a0 = plt.subplot(111) a1=a0.twinx() a0.plot(sp.array(X[:,0]).flatten(),Y,'g.') lb = sp.array([-2.,-2.]) ub = sp.array([2.,2.]) MLEH = GPdc.searchMLEhyp(X,Y,S,D,lb,ub,GPdc.SQUEXP,mx=10000) print MLEH G = GPdc.GPcore(X,Y,S,D,GPdc.kernel(GPdc.SQUEXP,1,MLEH)) print G.llk() np=180 sup = sp.linspace(-1,1,np) Dp = [[sp.NaN]]*np Xp = sp.vstack([sp.array([i]) for i in sup]) [m,v] = G.infer_diag(Xp,Dp) a0.plot(sup,m.flatten()) sq = sp.sqrt(v) a0.fill_between(sup, sp.array(m-2.*sq).flatten(), sp.array(m+2.*sq).flatten(), facecolor='lightblue',edgecolor='lightblue') lcb = G.infer_LCB(Xp,Dp,2.) a0.plot(sup,sp.array(lcb).flatten(),'g')
from scipy import linalg as spl from matplotlib import pyplot as plt import GPdc import OPTutils import ESutils import DIRECT #base dimension d = 2 kindex = GPdc.MAT52 nt = 34 lb = sp.array([0.] + [-1.] * d) ub = sp.array([5.] + [1.] * d) Htrue = sp.array([1.4, 4.] + [0.25] * d) [X, Y, S, D] = ESutils.gen_dataset(nt, d + 1, lb, ub, kindex, Htrue, s=1e-8) G = GPdc.GPcore(X, Y, S, D, GPdc.kernel(kindex, d + 1, Htrue)) def ojfaugnn(x): return G.infer_m(x, [[sp.NaN]])[0, 0] def opt_ip(s): def dwrap(x, y): X = sp.hstack([[s], x]) return (ojfaugnn(X), 0) [xm, ym, ierror] = DIRECT.solve(dwrap, lb[1:], ub[1:], user_data=[],
nt=34 X = ESutils.draw_support(2, sp.array([-1.,-1.]),sp.array([1.,1.]),nt,ESutils.SUPPORT_UNIFORM) D = [[sp.NaN]]*(nt) hyp = sp.array([1.5,0.25,0.25]) kf = GPdc.gen_sqexp_k_d(hyp) Kxx = GPdc.buildKsym_d(kf,X,D) Y = spl.cholesky(Kxx,lower=True)*sp.matrix(sps.norm.rvs(0,1.,nt)).T+sp.matrix(sps.norm.rvs(0,1e-3,nt)).T S = sp.matrix([1e-6]*nt).T lb = sp.array([-2.,-2.,-2.]) ub = sp.array([2.,2.,2.]) #MLEH = GPdc.searchMLEhyp(X,Y,S,D,lb,ub,GPdc.SQUEXP,mx=10000) G = GPdc.GPcore(X,Y,S,D,GPdc.kernel(GPdc.SQUEXP,2,sp.array([1.5,0.15,0.15]))) #np=180 #sup = sp.linspace(-1,1,np) #Dp = [[sp.NaN]]*np #Xp = sp.vstack([sp.array([i]) for i in sup]) #[m,v] = G.infer_diag(Xp,Dp) #a0.plot(sup,m) #sq = sp.sqrt(v) #a0.fill_between(sup, sp.array(m-2.*sq).flatten(), sp.array(m+2.*sq).flatten(), facecolor='lightblue',edgecolor='lightblue') Z = ESutils.draw_support(G, sp.array([-1.,-1.]),sp.array([1.,1.]),500,ESutils.SUPPORT_SLICEEI) np=30 R = ESutils.draw_min(G,Z,500) Z2 = ESutils.draw_support(G, sp.array([-1.,-1.]),sp.array([1.,1.]),500,ESutils.SUPPORT_LAPAPR,para=np) R2 = ESutils.draw_min(G,Z2,500)
ns = 200 sup = sp.linspace(-1,1,200) #points are a high noise obs, a low noise obs, a derivative obs and a second derivative obs X = sp.array([[-0.8],[-0.25],[0.25],[0.8]]) Y = sp.array([[0.3],[-0.2],[2.5],[50.]]) S = sp.array([[1e-1],[1e-6],[1e-6],[1e-6]]) D = [[sp.NaN],[sp.NaN],[0],[0,0]] a[0].plot([-0.8,-0.8],[0.3-sp.sqrt(1e-1),0.3+sp.sqrt(1e-1)],'r') a[0].plot([-0.8],[0.3],'ro') a[0].plot([-0.25],[-0.2],'ro') a[1].plot([0.25],[2.5],'ro') a[2].plot([0.8],[50],'ro') k= GPdc.kernel(GPdc.SQUEXP,1,sp.array([0.5,0.2])) K = sp.empty([4,4]) for i in xrange(4): for j in xrange(i,4): K[i,j] =K[j,i] = k(X[i,:],X[j,:],D[i],D[j]) K[i,i]+=1e-6 g = GPdc.GPcore(X,Y,S,D,GPdc.kernel(GPdc.SQUEXP,1,sp.array([0.5,0.2]))) #g.printc() C= g.get_cho() print C print spl.cho_solve((C,True),sp.eye(4)).dot(K) for i,d in enumerate([[sp.NaN],[0],[0,0]]): m,v = g.infer_diag(sup,[d]*ns)
import ctypes as ct import scipy as sp from scipy import stats as sps from scipy import linalg as spl from matplotlib import pyplot as plt import os libGP = ct.cdll.LoadLibrary( os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../../dist/Release/GNU-Linux/libGPshared.so')) ctpd = ct.POINTER(ct.c_double) import GPdc ni = 50 kf = GPdc.kernel(GPdc.SQUEXP, 1, sp.array([1.3, 0.3])) X = sp.matrix(sp.linspace(-1, 1, ni)).T D = [[sp.NaN]] * ni Kxx = GPdc.buildKsym_d(kf, X, D) tmp = spl.cholesky(Kxx, lower=True) Ch = sp.vstack([tmp[i, :] for i in xrange(ni)]) #hack/force row major storage z = 5 b = sp.empty([ni, z]) libGP.drawcov(Ch.ctypes.data_as(ctpd), ct.c_int(ni), b.ctypes.data_as(ctpd), ct.c_int(z)) #print b
print "X" Y = spl.cholesky(Kxx,lower=True)*sp.matrix(sps.norm.rvs(0,1.,nt)).T+sp.matrix(sps.norm.rvs(0,1e-3,nt)).T S = sp.matrix([1e-6]*nt).T f0,a0 = plt.subplots(1) a0.plot(sp.array(X[:,0]).flatten(),Y,'g.') lb = sp.array([-2.,-2.]) ub = sp.array([2.,2.]) MLEH = GPdc.searchMLEhyp(X,Y,S,D,lb,ub,GPdc.SQUEXP,mx=10000) print "X" print MLEH mp = sp.array([0.,-1.]) sb = sp.array([1.,1.]) #MAPH = GPdc.searchMAPhyp(X,Y,S,D,mp,sb,GPdc.SQUEXP,mx=10000) G = GPdc.GPcore(X,Y,S,D,GPdc.kernel(GPdc.SQUEXP,1,MLEH)) print "X" print Y.shape G.printc() print G.llk() np=180 sup = sp.linspace(-1,1,np) Dp = [[sp.NaN]]*np Xp = sp.vstack([sp.array([i]) for i in sup]) [m,v] = G.infer_diag(Xp,Dp) a0.plot(sup,m.flatten()) sq = sp.sqrt(v) a0.fill_between(sup, sp.array(m-1.*sq).flatten(), sp.array(m+1.*sq).flatten(), facecolor='lightblue',edgecolor='lightblue')
#!/usr/bin/env python2 #encoding: UTF-8 #test the 1d periodic matern5/2 kernel import sys sys.path.append("./..") import scipy as sp from matplotlib import pyplot as plt import seaborn as sns import GPdc kp = GPdc.kernel(GPdc.MAT52, 1, sp.array([0.75, 0.1])) kq = GPdc.kernel(GPdc.MAT52PER, 1, sp.array([0.75, 0.1, 4.5])) kr = GPdc.kernel(GPdc.MAT52PPT, 1, sp.array([0.75, 0.1, 4.5, 15.])) ks = GPdc.kernel(GPdc.DEV, 1, sp.array([0.75, 0.1, 4.5, 0.5, 0.4, 10.])) #support ns = 1201 xax = sp.array([sp.linspace(-15, 15, ns)]).T d0 = [[sp.NaN]] x0 = sp.array([[0.]]) p = sp.empty(ns) q = sp.empty(ns) r = sp.empty(ns) s = sp.empty(ns) for i in xrange(ns): p[i] = kp(x0, xax[i, :], d0, d0) q[i] = kq(x0, xax[i, :], d0, d0) + 1 r[i] = kr(x0, xax[i, :], d0, d0) + 2 s[i] = ks(x0, xax[i, :], d0, d0) + 3
#!/usr/bin/env python2 # encoding: UTF-8 # test the 1d periodic matern5/2 kernel import sys sys.path.append("./..") import scipy as sp from matplotlib import pyplot as plt import seaborn as sns import GPdc kp = GPdc.kernel(GPdc.MAT52, 1, sp.array([0.75, 0.1])) kq = GPdc.kernel(GPdc.MAT52PER, 1, sp.array([0.75, 0.1, 4.5])) kr = GPdc.kernel(GPdc.MAT52PPT, 1, sp.array([0.75, 0.1, 4.5, 15.0])) ks = GPdc.kernel(GPdc.DEV, 1, sp.array([0.75, 0.1, 4.5, 0.5, 0.4, 10.0])) # support ns = 1201 xax = sp.array([sp.linspace(-15, 15, ns)]).T d0 = [[sp.NaN]] x0 = sp.array([[0.0]]) p = sp.empty(ns) q = sp.empty(ns) r = sp.empty(ns) s = sp.empty(ns) for i in xrange(ns): p[i] = kp(x0, xax[i, :], d0, d0) q[i] = kq(x0, xax[i, :], d0, d0) + 1
from scipy import linalg as spl from matplotlib import pyplot as plt import GPdc import OPTutils import ESutils import DIRECT #base dimension d = 2 kindex = GPdc.MAT52 nt = 34 lb = sp.array([0.]+[-1.]*d) ub = sp.array([5.]+[1.]*d) Htrue = sp.array([1.4,4.]+[0.25]*d) [X,Y,S,D] = ESutils.gen_dataset(nt,d+1,lb,ub,kindex,Htrue, s=1e-8) G = GPdc.GPcore(X,Y,S,D,GPdc.kernel(kindex,d+1,Htrue)) def ojfaugnn(x): return G.infer_m(x,[[sp.NaN]])[0,0] def opt_ip(s): def dwrap(x,y): X = sp.hstack([[s],x]) return (ojfaugnn(X),0) [xm,ym,ierror] = DIRECT.solve(dwrap,lb[1:],ub[1:], user_data=[], algmethod=1, maxf=12000, logfilename='/dev/null') print "DIRECT found: " +str([xm,ym,ierror]) return xm mintrue = opt_ip(0.) minaug = sp.hstack([[0.],mintrue])
f,ax = plt.subplots(n,sharex=True) ns=200 sup = sp.linspace(-6,6,ns) for i in xrange(n): ax[i].plot(sup,sps.norm.pdf(sup,loc=m[i],scale=sp.sqrt(v[i,i])),'b') ax[i].plot(sup,sps.norm.pdf(sup,loc=mu[i],scale=sp.sqrt(vu[i,i])),'r') ax[i].twinx().plot(sup,sps.norm.cdf(Z[i]*(sup-Y[i])/max(F[i],1e-20))*sps.norm.pdf(sup,loc=m[i],scale=sp.sqrt(v[i,i])),'g') #est on a gp import ESutils import GPdc nt=5 X = sp.matrix(sp.linspace(-1,1,nt)).T D = [[sp.NaN]]*(nt) hyp = sp.array([1.5,0.15]) kf = GPdc.kernel(GPdc.SQUEXP,1,hyp) Kxx = GPdc.buildKsym_d(kf,X,D) Y = spl.cholesky(Kxx,lower=True)*sp.matrix(sps.norm.rvs(0,1.,nt)).T+sp.matrix(sps.norm.rvs(0,1e-3,nt)).T S = sp.matrix([1e-2]*nt).T g = GPdc.GPcore(X,Y,S,D,kf) f,a = plt.subplots(2) Xe = sp.array([-0.25,0.25]) De = [[sp.NaN]]*2 [m0,V0] = g.infer_full(Xe,De) Ye = sp.array([2.,-2.]) Ze = sp.array([1.,-1.]) Fe = sp.array([(1e-6)**2,(1e-6)**2]) mt,vt = eprop.expectation_prop(m0,V0,Ye,Ze,Fe,20) print D+De
ax[i].plot(sup, sps.norm.pdf(sup, loc=mu[i], scale=sp.sqrt(vu[i, i])), "r") ax[i].twinx().plot( sup, sps.norm.cdf(Z[i] * (sup - Y[i]) / max(F[i], 1e-20)) * sps.norm.pdf(sup, loc=m[i], scale=sp.sqrt(v[i, i])), "g", ) # est on a gp import ESutils import GPdc nt = 5 X = sp.matrix(sp.linspace(-1, 1, nt)).T D = [[sp.NaN]] * (nt) hyp = sp.array([1.5, 0.15]) kf = GPdc.kernel(GPdc.SQUEXP, 1, hyp) Kxx = GPdc.buildKsym_d(kf, X, D) Y = spl.cholesky(Kxx, lower=True) * sp.matrix(sps.norm.rvs(0, 1.0, nt)).T + sp.matrix(sps.norm.rvs(0, 1e-3, nt)).T S = sp.matrix([1e-2] * nt).T g = GPdc.GPcore(X, Y, S, D, kf) f, a = plt.subplots(2) Xe = sp.array([-0.25, 0.25]) De = [[sp.NaN]] * 2 [m0, V0] = g.infer_full(Xe, De) Ye = sp.array([2.0, -2.0]) Ze = sp.array([1.0, -1.0]) Fe = sp.array([(1e-6) ** 2, (1e-6) ** 2]) mt, vt = eprop.expectation_prop(m0, V0, Ye, Ze, Fe, 20) print D + De
S *= 0. f0 = plt.figure() a0 = plt.subplot(111) a0.plot(sp.array(X[:, 0]).flatten(), Y, 'g.') lb = sp.array([-2., -2., -9]) ub = sp.array([2., 2., -1]) MLEH = GPdc.searchMLEhyp(X, Y, S, D, lb, ub, GPdc.SQUEXPCS, mx=10000) mprior = sp.array([0., -1., -5.]) sprior = sp.array([1., 1., 3.]) MAPH = GPdc.searchMAPhyp(X, Y, S, D, mprior, sprior, GPdc.SQUEXPCS, mx=10000) print "MLEH: " + str(MLEH) print "MAPH: " + str(MAPH) G = GPdc.GPcore(X, Y, S, D, GPdc.kernel(GPdc.SQUEXPCS, 1, sp.array(MLEH))) print G.llk() np = 180 sup = sp.linspace(-1, 1, np) Dp = [[sp.NaN]] * np Xp = sp.vstack([sp.array([i]) for i in sup]) [m, v] = G.infer_diag(Xp, Dp) a0.plot(sup, m.flatten()) sq = sp.sqrt(v) a0.fill_between(sup, sp.array(m - 1. * sq).flatten(), sp.array(m + 1. * sq).flatten(),
f0 = plt.figure() a0 = plt.subplot(111) a0.plot(sp.array(X[:,0]).flatten(),Y,'g.') lb = sp.array([-2.,-2.,-9,-2.,-2.]) ub = sp.array([2.,2.,-1,2.,2.]) MLEH = GPdc.searchMLEhyp(X,Y,S,D,lb,ub,GPdc.SQUEXPPS,mx=20000) mprior = sp.array([0.,-1.,-5.,-0.5,0.5]) sprior = sp.array([1.,1.,3.,1.,1.]) MAPH = GPdc.searchMAPhyp(X,Y,S,D,mprior,sprior,GPdc.SQUEXPPS,mx=20000) print "MLEH: "+str(MLEH) print "MAPH: "+str(MAPH) G = GPdc.GPcore(X,Y,S,D,GPdc.kernel(GPdc.SQUEXPPS,1,sp.array(MAPH))) print G.llk() np=180 sup = sp.linspace(-1,1,np) Dp = [[sp.NaN]]*np Xp = sp.vstack([sp.array([i]) for i in sup]) [m,v] = G.infer_diag(Xp,Dp) sq = sp.sqrt(v) S = sp.empty([np,1]) for i in xrange(np): S[i,0] = -MAPH[2]*(Xp[i,0]-MAPH[3])*(Xp[i,0]-MAPH[4])
#X = sp.matrix([[0.,-9.],[0.2,5.],[0.4,12.],[0.6,3.],[0.8,9.]]) #Y = sp.matrix([0.2,0.1,0.,-0.15,-0.3]).T D = [[sp.NaN]]*X.shape[0] S = sp.matrix([0.005**2]*X.shape[0]).T f0 = plt.figure() a0 = plt.subplot(111) a0.plot(sp.array(X[:,0]).flatten(),Y,'g.') lb = sp.array([-2.,-1.,-2.,-2.,-2.,-2.,-2.]) ub = sp.array([2.,1.,2.,2.,2.,2.,2.]) MLEH = GPdc.searchMLEhyp(X,Y,S,D,lb,ub,GPdc.LINSQUEXPXSQUEXP,mx=10000) print "xxx" GPdc.kernel(GPdc.LINSQUEXPXSQUEXP,2,MLEH) print "yyyy" print MLEH G = GPdc.GPcore(X,Y,S,D,GPdc.kernel(GPdc.LINSQUEXPXSQUEXP,2,MLEH)) print G.llk() print "zzz" np=180 sup = sp.linspace(-1,1,np) Dp = [[sp.NaN]]*np Xp = sp.vstack([sp.array([i,0.2]) for i in sup]) [m,v] = G.infer_diag(Xp,Dp) print "wwww" a0.plot(sup,m) sq = sp.sqrt(v)
def traincfn(x, c): n = x.size g = GPdc.GPcore(x, c, sp.array([1e-1] * n), [[sp.NaN]] * n, GPdc.kernel(GPdc.MAT52, 1, [1., 0.2])) return cfnobj(g)