Ejemplo n.º 1
0
def test_normal_logEI():
    #rng = np.random.RandomState(123)

    N = 2000
    thresh = np.linspace(-10, 50, N)
    #N = 100
    #thresh = np.linspace(37, 38, N)
    mean = thresh * 0
    var = thresh * 0 + 1

    s_t, s_m, s_v = theano.tensor.dvectors('tmv')

    fn = theano.function([s_t, s_m, s_v],
                         gpr_math.s_normal_logEI(s_t, s_m, s_v))

    if 0:
        #print zip(thresh, fn(thresh, mean, var))
        #print 
        a = theano.tensor.dvector()
        y = s_t ** 2 * a[2] + s_t * a[1] + a[0]
        cost = ((y - gpr_math.s_normal_logEI(s_t, s_m, s_v)) ** 2).sum()
        da = theano.grad(cost, a)
        foo = theano.function([a, s_t, s_m, s_v], [cost, da])
        res = scipy.optimize.minimize(foo, [0, -1, -1], jac=True,
                                      args=(thresh, mean, var),
                                      method='L-BFGS-B')
        print res.x

    from hyperopt.criteria import logEI_gaussian
    if 0:
        import matplotlib.pyplot as plt
        y = fn(thresh, mean, var)
        z = logEI_gaussian(mean, var, thresh)
        plt.plot(thresh, y)
        plt.plot(thresh, z)
        plt.show()

    # -- the gpr_math logEI uses a quadratic approximation for very
    #    hopeless points, which gives the right derivative, but the
    #    slightly wrong value
    assert np.allclose(logEI_gaussian(mean, var, thresh),
                       fn(thresh, mean, var),
                       atol=1e-3, rtol=1e-4)

    if 0:
        d_t = theano.grad(gpr_math.s_normal_logEI(s_t, s_m, s_v).sum(), s_t)
        d_fn = theano.function([s_t, s_m, s_v], d_t)

        import matplotlib.pyplot as plt
        plt.plot(thresh, d_fn(thresh, mean, var))
        plt.show()
Ejemplo n.º 2
0
def test_normal_logEI_elemwise():
    rng = np.random.RandomState(123)

    N = 2000
    thresh = np.linspace(-50, 500, N)
    #N = 100
    #thresh = np.linspace(37, 38, N)
    mean = thresh * 0
    var = 1e-1 + rng.rand(N)
    sigma = np.sqrt(var)

    s_t, s_m, s_v = theano.tensor.dvectors('tmv')

    fn = theano.function([s_t, s_m, s_v],
                         normal_logEI_diff_sigma_elemwise(
                             s_t - s_m,
                             theano.tensor.sqrt(s_v)))

    my = fn(thresh, mean, var)
    ref = logEI_gaussian(mean, var, thresh)
    for xi, myv, spv in zip(thresh, my, ref):
        print xi, 'my', myv, 'sp', spv, 'diff', (myv - spv)

    assert np.any(thresh / sigma > 34)
    assert np.all(np.isfinite(my))
    assert np.allclose(my[thresh/sigma < 34], ref[thresh/sigma < 34])
    assert np.allclose(my, ref, rtol=.1)
Ejemplo n.º 3
0
def test_log_ei():
    for mean, var in [(0, 1), (-4, 9)]:
        thresholds = np.arange(-5, 30, 0.25) * np.sqrt(var) + mean

        ei = np.asarray(
            [crit.EI_gaussian(mean, var, thresh) for thresh in thresholds])
        nlei = np.asarray(
            [crit.logEI_gaussian(mean, var, thresh) for thresh in thresholds])
        naive = np.log(ei)
        # import matplotlib.pyplot as plt
        # plt.plot(thresholds, ei, label='ei')
        # plt.plot(thresholds, nlei, label='nlei')
        # plt.plot(thresholds, naive, label='naive')
        # plt.legend()
        # plt.show()

        # -- assert that they match when the threshold isn't too high
        assert np.allclose(nlei, naive)
Ejemplo n.º 4
0
def test_log_ei():
    for mean, var in [(0, 1), (-4, 9)]:
        thresholds = np.arange(-5, 30, .25) * np.sqrt(var) + mean

        ei = np.asarray(
            [crit.EI_gaussian(mean, var, thresh)
             for thresh in thresholds])
        nlei = np.asarray(
            [crit.logEI_gaussian(mean, var, thresh)
             for thresh in thresholds])
        naive = np.log(ei)
        #import matplotlib.pyplot as plt
        #plt.plot(thresholds, ei, label='ei')
        #plt.plot(thresholds, nlei, label='nlei')
        #plt.plot(thresholds, naive, label='naive')
        #plt.legend()
        #plt.show()

        # -- assert that they match when the threshold isn't too high
        assert np.allclose(nlei, naive)
Ejemplo n.º 5
0
def test_log_ei_range():
    assert np.all(
        np.isfinite([
            crit.logEI_gaussian(0, 1, thresh)
            for thresh in [-500, 0, 50, 100, 500, 5000]
        ]))
Ejemplo n.º 6
0
def test_log_ei_range():
    assert np.all(
        np.isfinite(
            [crit.logEI_gaussian(0, 1, thresh)
             for thresh in [-500, 0, 50, 100, 500, 5000]]))