示例#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()
示例#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])
示例#3
0
def run(casename):

    fh = Dataset("../data/" + casename + ".nc", mode="r")
    lons = fh.variables["lon"][:]
    lats = fh.variables["lat"][:]
    depths = fh.variables["elevation"][:]

    # Rescale data and flip depths to have anomalies as minima

    lons = MinMaxScaler().fit_transform(lons.reshape(-1, 1))
    lats = MinMaxScaler().fit_transform(lats.reshape(-1, 1))
    tmp = StandardScaler().fit_transform(depths.reshape(-1, 1))
    depths = -tmp.reshape(depths.shape)
    if casename in ["cuba", "challenger", "izu", "izu2", "izu3"]:
        depths = -depths

    domain = [[0, 1], [0, 1]]
    inputs = UniformInputs(domain)
    inputs = GaussianInputs(domain, [0.5, 0.5], [0.01, 0.01])
    noise_var = 0.0

    interpolant = interpolate.interp2d(lons, lats, depths, kind="cubic")
    my_map = BlackBox(map_def, args=(interpolant, ), noise_var=noise_var)
    idx_max = np.unravel_index(depths.argmin(), depths.shape)
    true_xmin = [lons[idx_max[1]], lats[idx_max[0]]]
    true_ymin = np.min(depths)

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

    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=casename + "_" + acq)
示例#4
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)
示例#5
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")
示例#6
0
def main():

    ndim = 3
    np.random.seed(2)

    tf = 4000
    dt = 0.01
    nsteps = int(tf/dt)
    u_init = [0, 0.01, 0.01]
    pre = PRE(tf, nsteps, u_init)
    u, t = pre.solve()
    plot_observable(t, u[:,2], ylabel="z",
                    xticks=[0,2000,4000], yticks=[-0.5,0,0.5,1])
    plot_trajectory(t, u)

    # Do PCA
    iostep = 100
    tsnap = t[::iostep]
    usnap = u[::iostep]
    lam, psi, usnap_mean = comp_pca(usnap, ndim)
    print("PCA Eigenvalues:", lam)

    n_init = 3
    n_iter = 50

    mean = np.zeros(ndim)
    cov = np.diag(lam)
    a_bnds = 4.0
    domain = [ [-a, a] for a in a_bnds*np.sqrt(np.diag(cov)) ]
    inputs = GaussianInputs(domain, mean, cov)
    my_map = BlackBox(map_def, args=(psi,usnap_mean))

    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="EI",
                        num_restarts=10,
                        parallel_restarts=True)
示例#7
0
def main():

    # Generate data for PCA
    tf = 2000
    dt = 0.01
    nsteps = int(tf / dt)
    u_init = [0, 0.01, 0.01]
    pre = PRE(tf, nsteps, u_init)
    u, t = pre.solve()

    # Do PCA
    ndim = 2
    iostep = 100
    tsnap = t[::iostep]
    usnap = u[::iostep]
    lam, psi, usnap_mean = comp_pca(usnap, ndim)
    print("PCA Eigenvalues:", lam)

    # Set up black box
    mean = np.zeros(ndim)
    cov = np.diag(lam)
    a_bnds = 4.0
    domain = [[-a, a] for a in a_bnds * np.sqrt(np.diag(cov))]
    inputs = GaussianInputs(domain, mean, cov)

    noise_var = 1e-3
    my_map = BlackBox(map_def, args=(psi, usnap_mean), noise_var=noise_var)

    # Do Benchmark
    prefix = "prec2d_"

    n_init = 3
    n_iter = 100
    n_trials = 100
    n_jobs = 40

    metric = [("regret_tmap", dict(tmap=my_map, true_ymin=0.0)),
              ("regret_obs", dict(true_ymin=0.0))]

    acq_list = [
        "EI", "PI", "LCB", "LCB_LW", "IVR", "IVR_BO", "IVR_LW", "IVR_LWBO"
    ]

    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)
示例#8
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()
示例#9
0
def main():

    np.random.seed(2)
    casename = "izu3"

    fh = Dataset("../benchmarks/bathymetry/data/" + casename + ".nc", mode="r")
    lons = fh.variables["lon"][:]
    lats = fh.variables["lat"][:]
    depths = fh.variables["elevation"][:]

# Rescale data and flip depths to have anomalies as minima

    lons = MinMaxScaler().fit_transform(lons.reshape(-1, 1))
    lats = MinMaxScaler().fit_transform(lats.reshape(-1, 1))
    tmp = StandardScaler().fit_transform(depths.reshape(-1, 1))
    depths = -tmp.reshape(depths.shape)
    if casename in ["cuba", "challenger", "izu", "izu2"]: depths = -depths

    domain = [ [0, 1], [0, 1] ]
   #inputs = UniformInputs(domain)
    inputs = GaussianInputs(domain, [0.5,0.5], [0.01,0.01])
    noise_var = 0.0

    interpolant = interpolate.interp2d(lons, lats, depths, kind="cubic")
    my_map = BlackBox(map_def, args=(interpolant,), noise_var=noise_var)

# Set up path-planner

    record_time = np.linspace(0,2,31)
   #record_time = np.linspace(0,4,61)
   #record_time = np.linspace(0,10,151)
    my_map.kwargs["time"] = record_time[0]
    X_pose = (0,0,np.pi/4); X = X_pose[0:2]
    Y_pose = my_map.evaluate(X); print(Y_pose); 

    p = PathPlanner(inputs.domain, 
                    look_ahead=0.2, 
                    turning_radius=0.02)

# Run mission

  # o = OptimalPathStatic(X_pose, Y_pose, my_map, inputs)
    o = OptimalPath(X_pose, Y_pose, my_map, inputs)#, static=True)
    m_list, p_list, s_list = o.optimize(record_time,
                                        path_planner=p, 
                                        acquisition="US")

# Plot stuff

    ngrid = 50
    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]
    yy = my_map.evaluate(pts, include_noise=False)
    ZZt = yy.reshape( (ngrid,)*ndim ).T 

    r_list = [ np.array(p.make_itinerary(path,1000)[0]) for path in p_list ]
    for ii in range(0, len(m_list)):
        model = m_list[ii]
        stamp = model.X[-1,-1]*np.ones(len(pts))
        yy = model.predict( np.hstack((pts, stamp[:,None])) )[0]
        ZZ = yy.reshape( (ngrid,)*ndim ).T

        fig = plt.figure(figsize=(10,4))
        plt.subplot(121)
        plt.contourf(X, Y, ZZ);
        for rr in r_list[0:ii+1]:
            plt.plot(rr[:,0], rr[:,1], 'r-')
        plt.plot(model.X[:,0],model.X[:,1],'ro')
        plt.xlim(0,1); plt.ylim(0,1)
        plt.axis('equal');

        plt.subplot(122)
        plt.contourf(X, Y, ZZt);
        plt.xlim(0,1); plt.ylim(0,1)
        plt.axis('equal');
        plt.show();