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)
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])
def log_pdf(m_list, inputs, pts=None, pt_list=None, clip=True, t_list=None): """Log-error between estimated pdf and true pdf. Parameters ---------- m_list : list A list of GPy models generated by `OptimalDesign`. inputs : instance of `Inputs` The input space. pts : array_like Randomly sampled points used for KDE of the GP model. pt_list : list of instances of `FFTKDE` The true pdf for time instants corresponding to `record_time`. clip : boolean, optional Whether or not to clip the pdf values below machine-precision. Returns ------- res : list A list containing the values of the log-error for each model in `m_list`. The log-error is defined as e = \int | log(pdf_{GP}) - log(pdf_{true}) | dy """ res = np.zeros(len(m_list)) for ii, model in enumerate(m_list): time = t_list[ii] aug_pts = np.hstack((pts, time * np.ones((pts.shape[0], 1)))) mu = model.predict(aug_pts)[0].flatten() ww = inputs.pdf(pts) pb = custom_KDE(mu, weights=ww) pt = pt_list[ii] x_min = min(pb.data.min(), pt.data.min()) x_max = max(pb.data.max(), pt.data.max()) rang = x_max - x_min x_eva = np.linspace(x_min - 0.01 * rang, x_max + 0.01 * rang, 1024) yb, yt = pb.evaluate(x_eva), pt.evaluate(x_eva) log_yb, log_yt = np.log(yb), np.log(yt) if clip: # Clip to machine-precision np.clip(log_yb, -14, None, out=log_yb) np.clip(log_yt, -14, None, out=log_yt) log_diff = np.abs(log_yb - log_yt) noInf = np.isfinite(log_diff) res[ii] = np.trapz(log_diff[noInf], x_eva[noInf]) return res
def run(function, prefix): noise_var = 1e-3 b = function(noise_var=noise_var, rescale_X=True) my_map, inputs, true_ymin, true_xmin = b.my_map, b.inputs, b.ymin, b.xmin # domain = inputs.domain # mean = np.zeros(inputs.input_dim) + 0.5 # cov = np.ones(inputs.input_dim)*0.01 # inputs = GaussianInputs(domain, mean, cov) n_iter = 100 n_trials = 100 n_jobs = 40 pts = inputs.draw_samples(n_samples=int(1e5), sample_method="uni") yy = my_map.evaluate(pts, parallel=True, include_noise=False) pt = custom_KDE(yy, weights=inputs.pdf(pts)) X_pose = (0, 0, np.pi / 4) planner = PathPlanner(inputs.domain, look_ahead=0.2, turning_radius=0.02, record_step=4, n_frontier=150, fov=0.75 * np.pi) metric = [("rmse", dict(pts=pts, yy=yy)), ("log_pdf", dict(pt=pt, pts=pts)), ("distmin_model", dict(true_xmin=true_xmin)), ("regret_tmap", dict(true_ymin=true_ymin, tmap=my_map)), ("regret_obs", dict(true_ymin=true_ymin))] acq_list = ["US", "US_LW", "IVR", "IVR_LW"] for acq in acq_list: print("Benchmarking " + acq) b = BenchmarkerPath(my_map, acq, planner, X_pose, n_iter, inputs, metric=metric) result = b.run_benchmark(n_trials, n_jobs=n_jobs, filename=prefix + acq)
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)
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")
def main(): ndim = 8 noise_var = 1e-2 my_map = BlackBox(map_def, noise_var=noise_var) inputs = CustomInputs([ [0,1] ] * ndim) np.random.seed(3) 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=int(1e6), sample_method="uni") 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) prefix = "gmm2_noise2" n_init = ndim + 1 n_iter = 160 n_trials = 100 n_jobs = 15 metric = [ ("log_pdf", dict(pt=pt, pts=pts)) ] acq_list = ["US", "US_LW", "IVR", "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")
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()