def plot(self, ax, cmap=None, cbar=True): if self.input_dim == 1: [b1, b2] = self.bounds thetas = create_grid(self) discrs = self.noiseless_f(thetas) ax.plot(thetas, discrs) elif self.input_dim == 2: if cmap is None: cmap = cm.binary thetas = create_grid(self) t1 = np.unique(thetas[:, 0]) t2 = np.unique(thetas[:, 1]) discrs = self.noiseless_f(thetas) cbar = ax.contourf(t1, t2, discrs.reshape(100, 100), cmap=cmap) if cbar: dloc = make_axes_locatable(ax) cax = dloc.append_axes('bottom', '10%', pad=0.25) plt.colorbar(cbar, cax=cax, orientation='horizontal') else: print('No plotting for 3D+ sims')
def __init__(self, model, acqs, sim, h_mult=0.05, verbose=False, eta=1.0, rng=None): super(ExplorerFIPR, self).__init__(model, 'ExplorerFIPR', acqs, verbose, rng) self.sim = sim self.eta = float(eta) self.probs = {name: [] for name in self.acq_names} self.gains = np.zeros(self.K) self.iter = 0 self.h_mult = float(h_mult) self.past_gains = [np.zeros(self.K)] self.past_rewards = [] if self.input_dim <= 2: self.use_grid = True self.thetas = create_grid(self.sim) else: self.use_grid = False self.thetas = []
def __init__(self, model, acqs, sim, verbose=False, rng=None): super(GoldStandard, self).__init__(model, 'GoldStandard', acqs, verbose, rng) self.sim = sim if self.sim.input_dim > 2: raise NotImplementedError('not implemented for 3D+ sims') self.choices = [] self.reducs = {a_n: [] for a_n in self.acq_names} self.thetas = create_grid(self.sim) discrs = self.sim.noiseless_f(self.thetas) self.h = discrs.min() + 0.035 * (discrs.max() - discrs.min()) self.pdf_true = norm.cdf((h - discrs) / self.sim.obs_noise) self.pdf_true /= np.sum(self.pdf_true)
def _get_ground_truth(self, h, n_grid_pts, n_local_pts, c_spr): """ Loads/calculates ground truth, depending on the simulator. """ out = dict() # Bacterial infections simulator (true, not interpolated) if isinstance(self.sim, BacterialInfectionsSimulator): bac_res = pkl.load(open('BAC_DATA/BACTERIAL_RESULTS.p', 'rb')).reshape(-1, 4) if isinstance(self.sim, BacterialInfections2D): k_dim = self.sim.known_dim k_prm = self.sim.known_param full_data = bac_res[np.where(abs(bac_res[:, k_dim] - k_prm) < 0.0001), :].squeeze() full_data = np.delete(full_data, obj=k_dim, axis=1).reshape(-1, 3) else: full_data = bac_res if len(full_data) < n_grid_pts**2: warnings.warn('Only %d BactInf data points available...' % len(full_data)) grid_thetas = full_data[:n_grid_pts**2, :-1] grid_pdf_true = kde.calculate_bact_inf_pdf(self.sim, full_data[:n_grid_pts**2, :], h) local_thetas = full_data[:n_local_pts**2, :-1] local_pdf_true = kde.calculate_bact_inf_pdf(self.sim, full_data[:n_local_pts**2, :], h) multiple_minima = False out['grid_thetas'] = grid_thetas out['grid_pdf_true'] = grid_pdf_true out['local_thetas'] = local_thetas out['local_pdf_true'] = local_pdf_true out['multiple_minima'] = multiple_minima return out # ======================================================= # 1 or 2 dimensions => create grid if self.input_dim in [1,2]: grid_thetas = kde.create_grid(self.sim, n_grid_pts) grid_pdf_true = kde.calculate_true_pdf(self.sim, h, grid_thetas) # Gaussian => sample from it elif self.sim.name[:20] == 'MultivariateGaussian': n_grid_pts = max(n_grid_pts, 10000) print('Sampling %d points for MC estimate of TV/KL' % n_grid_pts) grid_thetas = self.sim.sample(n_samples=n_grid_pts, cov_spread=c_spr) grid_pdf_true = self.sim.pdf(grid_thetas) # Other 3D+ => use MCMC to sample points for ground truth else: n_samps = int(n_grid_pts / 50) grid_thetas = np.zeros((0, self.input_dim)) for i in range(50): print('MCMC iter %d' % i) samples = sample_mcmc(self.sim, h, burnin=200, n_samples=n_samps, progress_bar=True) grid_thetas = np.vstack([grid_thetas, samples]) if self.ipy: clear_output() grid_pdf_true = kde.calculate_true_pdf(self.sim, h, grid_thetas) # Create local_thetas as grid around minima local_thetas = kde.create_grid(self.sim, n_local_pts, local=True) if type(local_thetas) == list: # <- if simulator has multiple global minima multiple_minima = True local_pdf_true = [] for local_t in local_thetas: local_pdf_true__ = kde.calculate_true_pdf(self.sim, h, local_t) local_pdf_true.append(local_pdf_true__) else: multiple_minima = False local_pdf_true = kde.calculate_true_pdf(self.sim, h, local_thetas) out['grid_thetas'] = grid_thetas out['grid_pdf_true'] = grid_pdf_true out['local_thetas'] = local_thetas out['local_pdf_true'] = local_pdf_true out['multiple_minima'] = multiple_minima return out