Beispiel #1
0
def compare_pdf_Oscillator():

    smpl = np.genfromtxt("map_samples2D.txt")
    ndim = 2
    tf = 25
    nsteps = 1000
    u_init = [0, 0]
    noise = Noise([0, tf])
    lam = noise.get_eigenvalues(ndim)
    mean = np.zeros(ndim)
    cov = np.diag(lam)
    domain = [[-a, a] for a in 6.0 * np.sqrt(np.diag(cov))]
    inputs = GaussianInputs(mean, cov, domain)
    weights = inputs.pdf(smpl[:, 0:-1])
    x_d = np.linspace(-3, 3, 500)

    #weights = weights/weights
    pdf_scipy = stats.gaussian_kde(smpl[:, -1], weights=weights)
    pdf_numba = KDE_Numba(smpl[:, -1], weights=weights)

    pdf_kdepy = FFTKDE(bw=pdf_numba.bw).fit(smpl[:, -1], weights)

    plt.semilogy(x_d, pdf_scipy(x_d), lw=3)
    plt.semilogy(x_d, pdf_numba(x_d), '--')
    plt.semilogy(x_d, pdf_kdepy(x_d), '--', lw=0.5)
    plt.xlim(-3, 3)
    plt.ylim(1e-8, 1e2)
    plt.show()
Beispiel #2
0
def main():

    ndim = 2
    np.random.seed(3)

    tf = 25
    nsteps = 1000
    u_init = [0, 0]
    noise = Noise([0, tf])
    oscil = Oscillator(noise, tf, nsteps, u_init)
    my_map = BlackBox(map_def, args=(oscil, ))

    n_init = 4
    n_iter = 80

    mean, cov = np.zeros(ndim), np.ones(ndim)
    domain = [[-6, 6]] * ndim
    inputs = GaussianInputs(domain, mean, cov)
    X = inputs.draw_samples(n_init, "lhs")
    Y = my_map.evaluate(X)

    o = OptimalDesign(X,
                      Y,
                      my_map,
                      inputs,
                      fix_noise=True,
                      noise_var=0.0,
                      normalize_Y=True)
    m_list = o.optimize(n_iter,
                        acquisition="US",
                        num_restarts=10,
                        parallel_restarts=True)

    # Compute true pdf
    filename = "map_samples{:d}D.txt".format(ndim)
    try:
        smpl = np.genfromtxt(filename)
        pts = smpl[:, 0:-1]
        yy = smpl[:, -1]
    except:
        pts = inputs.draw_samples(n_samples=100, sample_method="grd")
        yy = my_map.evaluate(pts, parallel=True, include_noise=False)
        np.savetxt(filename, np.column_stack((pts, yy)))
    pdf = custom_KDE(yy, weights=inputs.pdf(pts))

    for ii in np.arange(0, n_iter + 1, 10):
        pb, pp, pm = model_pdf(m_list[ii], inputs, pts=pts)
        plot_pdf(pdf,
                 pb, [pm, pp],
                 filename="pdfs%.4d.pdf" % (ii),
                 xticks=[-3, 0, 3],
                 yticks=[-8, -3, 2])
        plot_smp(m_list[ii],
                 inputs,
                 n_init,
                 filename="smps%.4d.pdf" % (ii),
                 xticks=[-6, 0, 6],
                 yticks=[-5, 0, 5],
                 cmapticks=[-2, -1, 0, 1, 2])
Beispiel #3
0
def run(function, prefix):

    noise_var = 1e-3

    b = augment(function)(noise_var=noise_var, rescale_X=True)
    my_map, inputs, true_ymin, true_xmin = b.my_map, b.inputs, b.ymin, b.xmin

    # Use Gaussian prior
    domain = inputs.domain
    mean = np.zeros(inputs.input_dim) + 0.5
    cov = np.ones(inputs.input_dim) * 0.01
    inputs = GaussianInputs(domain, mean, cov)

    record_time = np.linspace(0, 15, 226)
    n_trials = 50
    n_jobs = 20

    pts = inputs.draw_samples(n_samples=int(1e5), sample_method="uni")
    yy = my_map.evaluate(pts, parallel=True, include_noise=False)
    y_list = [yy] * len(record_time)
    pt_list = [custom_KDE(yy, weights=inputs.pdf(pts))] * len(record_time)
    true_ymin_list = [true_ymin] * len(record_time)
    true_xmin_list = [true_xmin] * len(record_time)

    metric = [(rmse, dict(pts=pts, y_list=y_list, t_list=record_time)),
              (mll, dict(pts=pts, y_list=y_list, t_list=record_time)),
              (log_pdf, dict(pts=pts, pt_list=pt_list, t_list=record_time)),
              (distmin_model,
               dict(true_xmin_list=true_xmin_list, t_list=record_time)),
              (regret_tmap,
               dict(true_ymin_list=true_ymin_list,
                    tmap=my_map,
                    t_list=record_time))]

    X_pose = (0, 0, np.pi / 4)
    planner = PathPlanner(inputs.domain, look_ahead=0.2, turning_radius=0.02)

    acq_list = ["US_IW", "US_LW", "IVR_IW", "IVR_LW"]

    for acq in acq_list:
        print("Benchmarking " + acq)
        b = BenchmarkerPath(my_map, acq, planner, X_pose, record_time, inputs,
                            metric)
        result = b.run_benchmark(n_trials,
                                 n_jobs=n_jobs,
                                 filename=prefix + acq)
Beispiel #4
0
def main():

    ndim = 2
    tf = 25
    nsteps = 1000
    u_init = [0, 0]
    noise = Noise([0, tf])
    oscil = Oscillator(noise, tf, nsteps, u_init)

    mean, cov = np.zeros(ndim), np.ones(ndim)
    domain = [[-6, 6]] * ndim
    inputs = GaussianInputs(domain, mean, cov)

    prefix = "oscill_"
    noise_var = 1e-3
    my_map = BlackBox(map_def, args=(oscil, ), noise_var=noise_var)

    n_init = 3
    n_iter = 80
    n_trials = 100
    n_jobs = 30

    # Compute true pdf
    filename = "map_samples{:d}D.txt".format(ndim)
    try:
        smpl = np.genfromtxt(filename)
        pts = smpl[:, 0:-1]
        yy = smpl[:, -1]
    except:
        pts = inputs.draw_samples(n_samples=100, sample_method="grd")
        yy = my_map.evaluate(pts, parallel=True, include_noise=False)
        np.savetxt(filename, np.column_stack((pts, yy)))
    weights = inputs.pdf(pts)
    pt = custom_KDE(yy, weights=weights)

    metric = [("log_pdf", dict(pt=pt, pts=pts))]
    acq_list = ["US", "US_LW", "IVR_IW", "IVR_LW"]

    for acq in acq_list:
        print("Benchmarking " + acq)
        b = Benchmarker(my_map, acq, n_init, n_iter, inputs, metric)
        result = b.run_benchmark(n_trials, n_jobs, filename=prefix + acq)

    b = BenchmarkerLHS(my_map, n_init, n_iter, inputs, metric)
    result = b.run_benchmark(n_trials, n_jobs, filename=prefix + "LHS")
Beispiel #5
0
def plot_likelihood_ratio(function, n_GMM, filename):

    my_map, inputs = function.my_map, function.inputs
    mu = np.random.randn(inputs.input_dim)
    cov = 4*np.random.randn(inputs.input_dim)**2
    inputs = GaussianInputs(inputs.domain, mu=mu, cov=cov)

    ngrid = 10
    pts = inputs.draw_samples(n_samples=ngrid, sample_method="grd")
    ndim = pts.shape[-1]
    grd = pts.reshape( (ngrid,)*ndim + (ndim,) ).T
    X, Y = grd[0], grd[1]

    # Compute map
    yy = my_map.evaluate(pts)
    # Compute GPy model
    model = GPy.models.GPRegression(pts, yy, normalizer=True)

    likelihood = Likelihood(model, inputs)
    x_new = np.random.rand(2, inputs.input_dim) 
    print(likelihood._evaluate_raw(x_new))
    print(likelihood._jacobian_raw(x_new))

    g = GradientChecker(lambda x: likelihood._evaluate_gmm(x),
                        lambda x: likelihood._jacobian_gmm(x),
                        x_new, 'x')
    assert(g.checkgrad())


    yy = model.predict(pts)[0].flatten()
    dyy_dx, _ = model.predictive_gradients(pts)
    dyy_dx = dyy_dx[:,:,0]

    ZZ = yy.reshape( (ngrid,)*ndim ).T
        
    # Compute likelihood ratio
    x, y = custom_KDE(yy, weights=inputs.pdf(pts)).evaluate()
    fnTn = scipy.interpolate.interp1d(x, y)
    fx = inputs.pdf(pts).flatten()
    fy = fnTn(yy).flatten()
    w = fx/fy
    ZL = w.reshape( (ngrid,)*ndim ).T

    # Compute gradient of likelihood ratio
    dy_dx = np.gradient(y,x)
    fnTn_dx = scipy.interpolate.interp1d(x, dy_dx)
    tmp = -fx / fy**2 * fnTn_dx(yy) 
    dw_dx = tmp[:,None] * dyy_dx

    plt.figure()
    plt.plot(x,y)
    plt.plot(x,fnTn(x), '-.')
    from scipy.interpolate import InterpolatedUnivariateSpline
    spl = InterpolatedUnivariateSpline(x, y)
    plt.plot(x, spl(x), '--')

    plt.figure()
    plt.semilogy(x,dy_dx)
    plt.semilogy(x,fnTn_dx(x), '-.')
    plt.semilogy(x, spl.derivative()(x), '--')
    plt.show(); exit()