Esempio n. 1
0
def sample_gp_with_slice( gp, params ):
  logposterior_generating_function_wrt_params = progapy.gp.generate_gp_logposterior_using_params
  
  p = gp.get_params()
  thetas = np.zeros( (params["N"],len(gp.get_params())))
  
  if params.has_key("ids"):
    ids = params["ids"]
  else:
    ids = range(len(gp.get_params()))
      
  cur_p = gp.get_params()
  L,R,stepsizes     = gp.get_range_of_params()

  for n in range(params["N"]):
    for i in ids:
      
      X, logprob = slice_sample( logposterior_generating_function_wrt_params(gp, idx=i), \
                               i, \
                               cur_p[i], \
                               L[i], \
                               R[i], \
                               stepsizes[i], \
                               params["nbrSteps"], \
                               params["MODE"] )
      cur_p[i] = X[-1]
    thetas[n,:] = cur_p
  if params.has_key("set_to_last_sample"):
    if params["set_to_last_sample"]:
      gp.set_params(thetas[-1])
    else:
      gp.set_params(p)
  else:
    gp.set_params(p)
  return thetas
Esempio n. 2
0
def drawhyp_plk(X, Y, S, D, ki, hm, hs, n, burn=80, subsam=5):
    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

    X = slice.slice_sample(f, hm, n, 0.05 * hs, burn=burn, subsam=subsam)
    return 10 ** X
Esempio n. 3
0
def drawhyp_plk(X, Y, S, D, ki, hm, hs, n, burn=80, subsam=5):
    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

    X = slice.slice_sample(f, hm, n, 0.05 * hs, burn=burn, subsam=subsam)
    return 10**X
Esempio n. 4
0
def draw_support(g, lb, ub, n, method, para=1.0):

    # para is the std confidence bound
    if type(g) is int:
        d = g
    else:
        d = g.D
    # print 'Draw support input GP with d={} lb {} ub {}'.format(d,lb,ub)
    if method == SUPPORT_UNIFORM:
        print "Drawing support using uniform:"
        X = sp.random.uniform(size=[n, d])
        for i in xrange(d):
            X[:, i] *= ub[i] - lb[i]
            X[:, i] += lb[i]
    elif method == SUPPORT_LAPAPR:
        print "Drawing support using lapapr:"
        # start with 4 times as many points as needed
        # print 'a'
        para = 5 * int(para)
        over = 4
        Xsto = sp.random.uniform(size=[over * para, d])
        for i in xrange(d):
            Xsto[:, i] *= ub[i] - lb[i]
            Xsto[:, i] += lb[i]
        # eval mean at the points
        # print 'b'
        fs = sp.empty(para * over)
        for i in xrange(para * over):
            fs[i] = g.infer_m_post(Xsto[i, :], [[sp.NaN]])[0, 0]
        # print 'mean at random draws {}'.format(fs)
        Xst = sp.empty([2 * para, d])
        # keep the lowest
        # print 'c'
        for i in xrange(para):
            j = fs.argmin()
            Xst[i, :] = Xsto[j, :]
            fs[j] = 1e99
        # minimize the posterior mean from each start
        # print 'd'
        def f(x):
            y = g.infer_m_post(sp.array(x), [[sp.NaN]])[0, 0]
            bound = 0
            r = max(abs(x))
            if r > 1:
                # print 'offedge {}'.format(x)
                bound = (1e3 * (r - 1)) ** 6
            return y + bound

        for i in xrange(para):
            res = spomin(f, Xst[i, :], method="Nelder-Mead", options={"xtol": 0.0001, "maxfev": 2000})
            if not res.success:

                class MJMError(Exception):
                    pass

                print res
                if not res.status == 2:
                    raise MJMError("failed in opt in support lapapr")
                else:
                    print "warn, lapapr opt did not fully vconverge "
            Xst[i + int(para), :] = res.x

        # find endpoints that are unique
        # print 'e'
        Xst
        unq = [Xst[0 + para, :]]
        for i in xrange(para):
            tmp = []
            for xm in unq:
                tmp.append(abs((xm - Xst[i + para, :])).max())

            if min(tmp) > 0.0002:
                unq.append(Xst[i + para, :])
        # get the alligned gaussian approx of pmin
        # print 'f'
        # print unq
        cls = []
        for xm in unq:
            ls = []
            for i in xrange(d):

                vg = g.infer_diag_post(xm, [[i]])[1][0, 0]
                gg = g.infer_diag_post(xm, [[i, i]])[0][0, 0]
                ls.append(sp.sqrt(vg) / gg)

            cls.append(ls)
        # print 'g'
        X = mnv.rvs(size=n, mean=[0.0] * d)
        if d == 1:
            X.resize([n, 1])
        neach = int(n / len(unq))
        for i in xrange(len(unq)):

            for j in xrange(d):
                X[i * neach : (i + 1) * neach, j] *= min(2.0, cls[i][j])
                X[i * neach : (i + 1) * neach, j] += unq[i][j]

        sp.clip(X, -1, 1, out=X)
        if True:
            # print 'inits'
            # print Xst

            # print 'cls'
            # print cls

            np = para
            # print 'para{}'.format(para)
            # print Xst.shape
            n = 200
            x_ = sp.linspace(-1, 1, n)
            y_ = sp.linspace(-1, 1, n)
            z_ = sp.empty([n, n])
            s_ = sp.empty([n, n])
            for i in xrange(n):
                for j in xrange(n):
                    m_, v_ = g.infer_diag_post(sp.array([y_[j], x_[i]]), [[sp.NaN]])
                    z_[i, j] = m_[0, 0]
                    s_[i, j] = sp.sqrt(v_[0, 0])
            fig, ax = plt.subplots(nrows=2, ncols=1, figsize=(10, 20))
            CS = ax[0].contour(x_, y_, z_, 20)
            ax[0].clabel(CS, inline=1, fontsize=10)
            CS = ax[1].contour(x_, y_, s_, 20)
            ax[0].axis([-1.0, 1.0, -1.0, 1.0])
            ax[1].clabel(CS, inline=1, fontsize=10)
            for i in xrange(np):
                ax[0].plot([Xst[i, 0], Xst[i + np, 0]], [Xst[i, 1], Xst[i + np, 1]], "b.-")
            for j in xrange(len(unq)):
                x = unq[j]
                ax[0].plot(x[0], x[1], "ro")
                xp = [x[0] + cls[j][0], x[0], x[0] - cls[j][0], x[0], x[0] + cls[j][0]]
                yp = [x[1], x[1] + cls[j][1], x[1], x[1] - cls[j][1], x[1]]
                ax[0].plot(xp, yp, "r-")
            fig.savefig(
                os.path.join(
                    os.path.expanduser("~"),
                    "Dropbox/debugoutput",
                    "drawlapapr" + time.strftime("%d_%m_%y_%H:%M:%S") + ".pdf",
                )
            )
    elif method == SUPPORT_SLICELCB:

        def f(x):
            if all(x > lb) and all(x < ub):
                try:
                    return -g.infer_LCB_post(sp.array(x), [[sp.NaN]], para)[0, 0]
                except:
                    g.infer_LCB_post(sp.array(x), [[sp.NaN]], para)[0, 0]
                    g.printc()
                    raise
            else:
                return -1e99

        print "Drawing support using slice sample over LCB:"
        X = slice.slice_sample(f, 0.5 * (ub + lb), n, 0.1 * (ub - lb))

    elif method == SUPPORT_SLICEEI:

        def f(x):
            if all(x > lb) and all(x < ub):
                try:
                    ei = g.infer_EI(sp.array(x), [[sp.NaN]])[0, 0]
                    # print ei
                    return sp.log(ei)
                except:
                    # ei=g.infer_EI(sp.array(x),[[sp.NaN]])[0,0]
                    g.printc()
                    raise
            else:
                return -1e99

        print "Drawing support using slice sample over EI:"
        X = slice.slice_sample(f, 0.5 * (ub + lb), n, 0.1 * (ub - lb))

    elif method == SUPPORT_SLICEPM:

        def f(x):
            if all(x > lb) and all(x < ub):
                [m, v] = g.infer_diag_post(sp.vstack([sp.array(x)] * d), [[i] for i in xrange(d)])
                p = 0.0
                for i in xrange(d):
                    p += -0.5 * (m[0, i] ** 2) / v[0, i]
                ym = g.infer_m_post(sp.array(x), [[sp.NaN]])[0, 0]
                if not sp.isfinite(p):
                    print [m, V, p]
                    # raise ValueError
                return -10 * ym + 0.01 * p
            else:
                return -1e99

        if False:
            A = sp.empty([100, 100])
            sup = sp.linspace(-0.999, 0.999, 100)
            for i in xrange(100):
                for j in xrange(100):
                    print sp.array([sup[i], sup[j]])
                    A[99 - j, i] = f([sup[i], sup[j]])
                    print A[99 - j, i]
            print A
            plt.figure()
            plt.imshow(A)
            plt.figure()
        print "Drawing support using slice sample over PM:"
        X = slice.slice_sample(f, 0.5 * (ub + lb), n, 0.1 * (ub - lb))
    else:
        raise RuntimeError("draw_support method invalid")
    return X
Esempio n. 5
0
#!/usr/bin/env python2
#encoding: UTF-8

# 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 matplotlib import pyplot as plt
import slice
import scipy as sp
def llk(x):
    return sp.log( sp.exp(-(0.5*(x-1)**2)/0.5**2)+sp.exp(-(0.5*(x+2)**2)/0.25**2))

S = slice.slice_sample(llk,sp.array([0.]),5000,sp.array([0.05]))
#print S
plt.hist(sp.array(S).flatten(),bins=30)

def llk2(x):
    return sp.log( sp.exp(-0.5*((x[0]+1.5)**2+(x[1]-1.5)**2)/0.5**2)+sp.exp(-0.5*(((x[0]-1.5)**2)/0.75**2+((x[1]+1.5)**2)/0.25**2)))

S = slice.slice_sample(llk2,sp.array([0.,0.]),10000,sp.array([0.15,0.15]))
A = sp.zeros([20,20])
for i in xrange(S.shape[0]):
    a1 = int((S[i,0]+3)*20/6)
    a2 = int((S[i,1]+3)*20/6)
    try:
        A[a1,a2]+=1
    except:
        pass
#plt.figure()
#for i in xrange(A.shape[0]):
#    plt.plot(S[i,0],S[i,1],'r.')
Esempio n. 6
0
def draw_support(g, lb, ub, n, method, para=1.):

    #para is the std confidence bound
    if (type(g) is int):
        d = g
    else:
        d = g.D
    #print 'Draw support input GP with d={} lb {} ub {}'.format(d,lb,ub)
    if method == SUPPORT_UNIFORM:
        print "Drawing support using uniform:"
        X = sp.random.uniform(size=[n, d])
        for i in xrange(d):
            X[:, i] *= ub[i] - lb[i]
            X[:, i] += lb[i]
    elif method == SUPPORT_LAPAPR:
        print "Drawing support using lapapr:"
        #start with 4 times as many points as needed
        #print 'a'
        para = 5 * int(para)
        over = 4
        Xsto = sp.random.uniform(size=[over * para, d])
        for i in xrange(d):
            Xsto[:, i] *= ub[i] - lb[i]
            Xsto[:, i] += lb[i]
        #eval mean at the points
        #print 'b'
        fs = sp.empty(para * over)
        for i in xrange(para * over):
            fs[i] = g.infer_m_post(Xsto[i, :], [[sp.NaN]])[0, 0]
        #print 'mean at random draws {}'.format(fs)
        Xst = sp.empty([2 * para, d])
        #keep the lowest
        #print 'c'
        for i in xrange(para):
            j = fs.argmin()
            Xst[i, :] = Xsto[j, :]
            fs[j] = 1e99
        #minimize the posterior mean from each start
        #print 'd'
        def f(x):
            y = g.infer_m_post(sp.array(x), [[sp.NaN]])[0, 0]
            bound = 0
            r = max(abs(x))
            if r > 1:
                #print 'offedge {}'.format(x)
                bound = (1e3 * (r - 1))**6
            return y + bound

        for i in xrange(para):
            res = spomin(f,
                         Xst[i, :],
                         method='Nelder-Mead',
                         options={
                             'xtol': 0.0001,
                             'maxfev': 2000
                         })
            if not res.success:

                class MJMError(Exception):
                    pass

                print res
                if not res.status == 2:
                    raise MJMError('failed in opt in support lapapr')
                else:
                    print "warn, lapapr opt did not fully vconverge "
            Xst[i + int(para), :] = res.x

        #find endpoints that are unique
        #print 'e'
        Xst
        unq = [Xst[0 + para, :]]
        for i in xrange(para):
            tmp = []
            for xm in unq:
                tmp.append(abs((xm - Xst[i + para, :])).max())

            if min(tmp) > 0.0002:
                unq.append(Xst[i + para, :])
        #get the alligned gaussian approx of pmin
        #print 'f'
        #print unq
        cls = []
        for xm in unq:
            ls = []
            for i in xrange(d):

                vg = g.infer_diag_post(xm, [[i]])[1][0, 0]
                gg = g.infer_diag_post(xm, [[i, i]])[0][0, 0]
                ls.append(sp.sqrt(vg) / gg)

            cls.append(ls)
        #print 'g'
        X = mnv.rvs(size=n, mean=[0.] * d)
        if d == 1:
            X.resize([n, 1])
        neach = int(n / len(unq))
        for i in xrange(len(unq)):

            for j in xrange(d):
                X[i * neach:(i + 1) * neach, j] *= min(2., cls[i][j])
                X[i * neach:(i + 1) * neach, j] += unq[i][j]

        sp.clip(X, -1, 1, out=X)
        if True:
            #print 'inits'
            #print Xst

            #print 'cls'
            #print cls

            np = para
            #print 'para{}'.format(para)
            #print Xst.shape
            n = 200
            x_ = sp.linspace(-1, 1, n)
            y_ = sp.linspace(-1, 1, n)
            z_ = sp.empty([n, n])
            s_ = sp.empty([n, n])
            for i in xrange(n):
                for j in xrange(n):
                    m_, v_ = g.infer_diag_post(sp.array([y_[j], x_[i]]),
                                               [[sp.NaN]])
                    z_[i, j] = m_[0, 0]
                    s_[i, j] = sp.sqrt(v_[0, 0])
            fig, ax = plt.subplots(nrows=2, ncols=1, figsize=(10, 20))
            CS = ax[0].contour(x_, y_, z_, 20)
            ax[0].clabel(CS, inline=1, fontsize=10)
            CS = ax[1].contour(x_, y_, s_, 20)
            ax[0].axis([-1., 1., -1., 1.])
            ax[1].clabel(CS, inline=1, fontsize=10)
            for i in xrange(np):
                ax[0].plot([Xst[i, 0], Xst[i + np, 0]],
                           [Xst[i, 1], Xst[i + np, 1]], 'b.-')
            for j in xrange(len(unq)):
                x = unq[j]
                ax[0].plot(x[0], x[1], 'ro')
                xp = [
                    x[0] + cls[j][0], x[0], x[0] - cls[j][0], x[0],
                    x[0] + cls[j][0]
                ]
                yp = [x[1], x[1] + cls[j][1], x[1], x[1] - cls[j][1], x[1]]
                ax[0].plot(xp, yp, 'r-')
            fig.savefig(
                os.path.join(
                    os.path.expanduser('~'), 'Dropbox/debugoutput',
                    'drawlapapr' + time.strftime('%d_%m_%y_%H:%M:%S') +
                    '.pdf'))
    elif method == SUPPORT_SLICELCB:

        def f(x):
            if all(x > lb) and all(x < ub):
                try:
                    return -g.infer_LCB_post(sp.array(x), [[sp.NaN]], para)[0,
                                                                            0]
                except:
                    g.infer_LCB_post(sp.array(x), [[sp.NaN]], para)[0, 0]
                    g.printc()
                    raise
            else:
                return -1e99

        print "Drawing support using slice sample over LCB:"
        X = slice.slice_sample(f, 0.5 * (ub + lb), n, 0.1 * (ub - lb))

    elif method == SUPPORT_SLICEEI:

        def f(x):
            if all(x > lb) and all(x < ub):
                try:
                    ei = g.infer_EI(sp.array(x), [[sp.NaN]])[0, 0]
                    #print ei
                    return sp.log(ei)
                except:
                    #ei=g.infer_EI(sp.array(x),[[sp.NaN]])[0,0]
                    g.printc()
                    raise
            else:
                return -1e99

        print "Drawing support using slice sample over EI:"
        X = slice.slice_sample(f, 0.5 * (ub + lb), n, 0.1 * (ub - lb))

    elif method == SUPPORT_SLICEPM:

        def f(x):
            if all(x > lb) and all(x < ub):
                [m, v] = g.infer_diag_post(sp.vstack([sp.array(x)] * d),
                                           [[i] for i in xrange(d)])
                p = 0.
                for i in xrange(d):
                    p += -0.5 * (m[0, i]**2) / v[0, i]
                ym = g.infer_m_post(sp.array(x), [[sp.NaN]])[0, 0]
                if not sp.isfinite(p):
                    print[m, V, p]
                    #raise ValueError
                return -10 * ym + 0.01 * p
            else:
                return -1e99

        if False:
            A = sp.empty([100, 100])
            sup = sp.linspace(-0.999, 0.999, 100)
            for i in xrange(100):
                for j in xrange(100):
                    print sp.array([sup[i], sup[j]])
                    A[99 - j, i] = f([sup[i], sup[j]])
                    print A[99 - j, i]
            print A
            plt.figure()
            plt.imshow(A)
            plt.figure()
        print "Drawing support using slice sample over PM:"
        X = slice.slice_sample(f, 0.5 * (ub + lb), n, 0.1 * (ub - lb))
    else:
        raise RuntimeError("draw_support method invalid")
    return X
Esempio n. 7
0
# 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 matplotlib import pyplot as plt
import slice
import scipy as sp


def llk(x):
    return sp.log(
        sp.exp(-(0.5 * (x - 1)**2) / 0.5**2) + sp.exp(-(0.5 *
                                                        (x + 2)**2) / 0.25**2))


S = slice.slice_sample(llk, sp.array([0.]), 5000, sp.array([0.05]))
#print S
plt.hist(sp.array(S).flatten(), bins=30)


def llk2(x):
    return sp.log(
        sp.exp(-0.5 * ((x[0] + 1.5)**2 + (x[1] - 1.5)**2) / 0.5**2) +
        sp.exp(-0.5 * (((x[0] - 1.5)**2) / 0.75**2 +
                       ((x[1] + 1.5)**2) / 0.25**2)))


S = slice.slice_sample(llk2, sp.array([0., 0.]), 10000, sp.array([0.15, 0.15]))
A = sp.zeros([20, 20])
for i in xrange(S.shape[0]):
    a1 = int((S[i, 0] + 3) * 20 / 6)