# 15 initial values for the 7 hod and 8 cosmo params p0 = np.full(nparams, 0.1) k1 = kernels.ExpSquaredKernel(p0, ndim=len(p0)) k2 = kernels.Matern32Kernel(p0, ndim=len(p0)) k3 = kernels.ConstantKernel(0.1, ndim=len(p0)) #k4 = kernels.WhiteKernel(0.1, ndim=len(p0)) k5 = kernels.ConstantKernel(0.1, ndim=len(p0)) kernel = k2 + k5 #kernel = np.var(y)*k1 ppt = pp[j] gp = george.GP(kernel, mean=np.mean(y), solver=george.BasicSolver) #gp = george.GP(kernel, solver=george.BasicSolver) gp.compute(rr, yerr) #gp.kernel.vector = ppt gp.set_parameter_vector(ppt) gp.compute(rr, yerr) gps.append(gp) if fixed_hod: #HH_test = range(hod, hod+1) CC_test = range(0, 7) # TODO: add more tests, for now just did first 10 hod
def lnlike_gp(params, wl, flux, fluxerror): a, tau = np.exp(params[:2]) gp = george.GP(a * kernels.Matern32Kernel(tau)) gp.compute(wl, fluxerror) return gp.lnlikelihood(flux - model(params[2:], wl))
def run_vs_3paramFit(): args = parse_args() ext = args.output_file_extension from pygp.canvas import Canvas xMinFit = 300 # Getting data points xRaw, yRaw, xerrRaw, yerrRaw = getDataPoints(args.input_file, 'dijetgamma_g85_2j65', 'Zprime_mjj_var') xSig, ySig, xerrSig, yerrSig = getDataPoints(args.signal_file, 'dijetgamma_g85_2j65', 'Zprime_mjj_var') #Data processing, cutting out the not desired range x, y, xerr, yerr = dataCut(xMinFit, 1500, 0, xRaw, yRaw, xerrRaw, yerrRaw) xFit, yFit, xerrFit, yerrFit = dataCut(xMinFit, 1500, 0, xRaw, yRaw, xerrRaw, yerrRaw) # make an evently spaced x t = np.linspace(np.min(x), np.max(x), 500) #calculating the log-likihood and minimizing for the gp lnProb = logLike_minuit(x, y, xerr) # # idecay = np.random.random() * 0.64 # ilength = np.random.random() * 5e5 # ipower = np.random.random() * 1.0 # isub = np.random.random() * 1.0 # 'amp': 5701461179.0, # 'p0': 0.23, # 'p1': 0.46, # 'p2': 0.89 # #dan crap initial guess->returns infiinity print("prob:", lnProb(5701461179.0, 0.64, 5e5, 1.0, 1.0, 0.23, 0.46, 0.89)) print( "prob:", lnProb(7894738685.23, 91.507809530688036, 152892.47486888882, 0.77936430405302681, 393.57106174440003, 0.46722747265429021, 0.92514297129196166, 1.7803224928740065)) #def __call__(self, Amp, decay, length, power, sub, p0, p1, p2): min_likelihood, best_fit = fit_gp_minuit(10, lnProb) fit_pars = [best_fit[x] for x in FIT3_PARS] #making the GP kargs = {x: y for x, y in best_fit.items() if x not in FIT3_PARS} kernel_new = get_kernel(**kargs) print(kernel_new.get_parameter_names()) #making the kernel gp_new = george.GP(kernel_new, mean=Mean(fit_pars), fit_mean=True) gp_new.compute(x, yerr) mu, cov = gp_new.predict(y, t) mu_x, cov_x = gp_new.predict(y, x) # calculating the fit function value #GP compute minimizes the log likelihood of the best_fit function best = [best_fit[x] for x in FIT3_PARS] print("best param meghan GP minmization:", best) meanFromGPFit = Mean(best) fit_meanM = meanFromGPFit.get_value( x, xerr) #so this is currently shit. can't get the xErr thing working print("fit_meanM:", fit_meanM) #fit_mean_smooth = gp_new.mean.get_value(t) #----3 param fit function in a different way lnProb = logLike_3ff(xFit, yFit, xerrFit) minimumLLH, best_fit_params = fit_3ff(100, lnProb) fit_mean = model_3param(xFit, best_fit_params, xerrFit) ##----4 param fit function #lnProb = logLike_4ff(xFit,yFit,xerrFit) #minimumLLH, best_fit_params = fit_4ff(100, lnProb) #fit_mean = model_4param(xFit, best_fit_params, xerrFit) #calculating significance signif = significance(mu_x, y, cov_x, yerr) initialCutPos = np.argmax(x > xMinFit) #sigFit = (fit_mean - y[initialCutPos:]) / np.sqrt(np.diag(cov_x[initialCutPos:]) + yerr[initialCutPos:]**2) sigFit = significance(fit_mean, y[initialCutPos:], cov_x[initialCutPos:], yerr[initialCutPos:]) #sigit = (mu_x) std = np.sqrt(np.diag(cov)) ext = args.output_file_extension title = "test" with Canvas(f'%s{ext}' % title) as can: can.ax.errorbar(x, y, yerr=yerr, fmt='.') can.ax.set_yscale('log') # can.ax.set_ylim(1, can.ax.get_ylim()[1]) can.ax.plot(t, mu, '-r') can.ax.plot(xFit, fit_mean, '-b') #this only works with the xErr part of Mean commented out #can.ax.plot(x, fit_meanM, '.g') # can.ax.plot(t, fit_mean_smooth, '--b') #can.ax.fill_between(t, mu - std, mu + std, #facecolor=(0, 1, 0, 0.5), #zorder=5, label='err = 1') #can.ratio.stem(x, signif, markerfmt='.', basefmt=' ') can.ratio.stem(xFit, sigFit, markerfmt='.', basefmt=' ') can.ratio.axhline(0, linewidth=1, alpha=0.5) can.ratio2.stem(x, signif, markerfmt='.', basefmt=' ') can.save(title)
def create_testcase07(): import george bjd0 = bjd_obs - Tref err = bjd0 * 0 + instrument['RV_precision'] gp_pams = np.zeros(4) gp_pams[0] = np.log(activity['Hamp_RV1']) * 2 gp_pams[1] = np.log(activity['Pdec']) * 2 gp_pams[2] = 1. / (2 * activity['Oamp']**2) gp_pams[3] = np.log(activity['Prot']) kernel = np.exp(gp_pams[0]) * \ george.kernels.ExpSquaredKernel(metric=np.exp(gp_pams[1])) * \ george.kernels.ExpSine2Kernel(gamma=gp_pams[2], log_period=gp_pams[3]) gp = george.GP(kernel) gp.compute(bjd0, err) prediction1 = gp.sample(bjd0) gp_pams[0] = np.log(activity['Hamp_RV2']) * 2 kernel = np.exp(gp_pams[0]) * \ george.kernels.ExpSquaredKernel(metric=np.exp(gp_pams[1])) * \ george.kernels.ExpSine2Kernel(gamma=gp_pams[2], log_period=gp_pams[3]) gp = george.GP(kernel) gp.compute(bjd0, err) prediction2 = gp.sample(bjd0) y_pla = kp.kepler_RV_T0P(bjd0, planet_b['f'], planet_b['P'], planet_b['K'], planet_b['e'], planet_b['o']) mod_pl1 = np.random.normal(y_pla + prediction1 + instrument['RV_offset1'], instrument['RV_precision']) mod_pl2 = np.random.normal(y_pla + prediction2 + instrument['RV_offset2'], instrument['RV_precision']) Tcent_b = np.random.normal( np.arange(0, 1) * planet_b['P'] + kp.kepler_Tcent_T0P( planet_b['P'], planet_b['f'], planet_b['e'], planet_b['o']) + Tref, instrument['T0_precision']) fileout = open('TestCase07_RV_dataset1.dat', 'w') for b, r in zip(bjd_obs, mod_pl1): fileout.write('{0:f} {1:.2f} {2:.2f} {3:d} {4:d} {5:d}\n'.format( b, r, instrument['RV_precision'], 0, 0, -1)) fileout.close() fileout = open('TestCase07_RV_dataset2.dat', 'w') for b, r in zip(bjd_obs, mod_pl2): fileout.write('{0:f} {1:.2f} {2:.2f} {3:d} {4:d} {5:d}\n'.format( b, r, instrument['RV_precision'], 0, 0, -1)) fileout.close() fileout = open('TestCase07_Tcent_b.dat', 'w') for i_Tc, v_Tc in enumerate(Tcent_b): fileout.write('{0:d} {1:.4f} {2:.4f} {3:d}\n'.format( i_Tc, v_Tc, instrument['T0_precision'], 0)) fileout.close()
idx_params = np.array(eparams.split(',')).astype('int') X = X[idx_params,:] # Sace other inputs: ld_law = args.ldlaw pmean = np.double(args.pmean) psd = np.double(args.psd) n_live_points = int(args.nlive) # Cook the george kernel: import george kernel = np.var(f)*george.kernels.ExpSquaredKernel(np.ones(X.shape[0]),ndim=X.shape[0],axes=range(X.shape[0])) # Cook jitter term jitter = george.modeling.ConstantModel(np.log((200.*1e-6)**2.)) # Wrap GP object to compute likelihood gp = george.GP(kernel, mean=0.0,fit_mean=False,white_noise=jitter,fit_white_noise=True) #print gp.get_parameter_names(),gp.get_parameter_vector() #print dir(gp) #sys.exit() gp.compute(X.T) # Define transit-related functions: def reverse_ld_coeffs(ld_law, q1, q2): if ld_law == 'quadratic': coeff1 = 2.*np.sqrt(q1)*q2 coeff2 = np.sqrt(q1)*(1.-2.*q2) elif ld_law=='squareroot': coeff1 = np.sqrt(q1)*(1.-2.*q2) coeff2 = 2.*np.sqrt(q1)*q2 elif ld_law=='logarithmic': coeff1 = 1.-np.sqrt(q1)*q2
def train(self, X, y, do_optimize=True, **kwargs): X_norm, _, _ = normalization.zero_one_normalization( X[:, :-1], self.lower, self.upper) s_ = self.basis_func(X[:, -1])[:, None] self.X = np.concatenate((X_norm, s_), axis=1) if self.normalize_output: # Normalize output to have zero mean and unit standard deviation self.y, self.y_mean, self.y_std = normalization.zero_mean_unit_var_normalization( y) else: self.y = y # Use the mean of the data as mean for the GP mean = np.mean(self.y, axis=0) self.gp = george.GP(self.kernel, mean=mean) if do_optimize: # We have one walker for each hyperparameter configuration sampler = emcee.EnsembleSampler(self.n_hypers, len(self.kernel) + 1, self.loglikelihood) # Do a burn-in in the first iteration if not self.burned: # Initialize the walkers by sampling from the prior if self.prior is None: self.p0 = np.random.rand(self.n_hypers, len(self.kernel.pars) + 1) else: self.p0 = self.prior.sample_from_prior(self.n_hypers) # Run MCMC sampling self.p0, _, _ = sampler.run_mcmc(self.p0, self.burnin_steps, rstate0=self.rng) self.burned = True # Start sampling pos, _, _ = sampler.run_mcmc(self.p0, self.chain_length, rstate0=self.rng) # Save the current position, it will be the start point in # the next iteration self.p0 = pos # Take the last samples from each walker self.hypers = sampler.chain[:, -1] else: if self.hypers is None: self.hypers = self.gp.kernel[:].tolist() self.hypers.append(self.noise) self.hypers = [self.hypers] self.models = [] for sample in self.hypers: # Instantiate a GP for each hyperparameter configuration kernel = deepcopy(self.kernel) kernel.set_parameter_vector(sample[:-1]) noise = np.exp(sample[-1]) model = FabolasGP(kernel, basis_function=self.basis_func, normalize_output=self.normalize_output, noise=noise, lower=self.lower, upper=self.upper, rng=self.rng) model.train(X, y, do_optimize=False) self.models.append(model) self.is_trained = True
k2=np.std(y) / 100, w2=0., e2=0.4, offset1=0., offset2=0.) kwargs = dict(**truth) kwargs["bounds"] = dict(P1=(7.5, 8.5), k1=(0, 0.1), w1=(-2 * np.pi, 2 * np.pi), e1=(0, 0.9), tau2=(-50, 50), k2=(0, 0.2), w2=(-2 * np.pi, 2 * np.pi), e2=(0, 0.9)) mean_model = Model(**kwargs) gp = george.GP(kernel, mean=mean_model, fit_mean=True) gp.compute(t, yerr) def lnprob2(p): gp.set_parameter_vector(p) return gp.log_likelihood(y, quiet=True) + gp.log_prior() #============================================================================== # MCMC #============================================================================== import emcee initial = gp.get_parameter_vector() names = gp.get_parameter_names()
0.1 ]) k1 = ExpSquaredKernel(p0, ndim=len(p0)) k2 = Matern32Kernel(p0, ndim=len(p0)) k3 = ConstantKernel(0.1, ndim=len(p0)) k4 = WhiteKernel(0.1, ndim=len(p0)) k5 = ConstantKernel(0.1, ndim=len(p0)) kernel = k1 * k5 + k2 + k3 + k4 ppt = pp[j] if j == 0: if HODLR == True: gp0 = george.GP(kernel, mean=np.mean(y), solver=george.HODLRSolver) else: gp0 = george.GP(kernel, mean=np.mean(y), solver=george.BasicSolver) gp0.compute(rr, yerr) gp0.kernel.vector = ppt gp0.compute(rr, yerr) if j == 1: if HODLR == True: gp1 = george.GP(kernel, mean=np.mean(y), solver=george.HODLRSolver) else: gp1 = george.GP(kernel, mean=np.mean(y), solver=george.BasicSolver) gp1.compute(rr, yerr) gp1.kernel.vector = ppt
def neo_update_kernel(theta, params): gp = george.GP(mean=0.0, fit_mean=False, white_noise=jitt) pass
def defaultGP(theta, y, order=None, white_noise=-12, fitAmp=False): """ Basic utility function that initializes a simple GP with an ExpSquaredKernel. This kernel works well in many applications as it effectively enforces a prior on the smoothness of the function and is infinitely differentiable. Parameters ---------- theta : array Design points y : array Data to condition GP on, e.g. the lnlike + lnprior at each design point, theta. order : int, optional Order of PolynomialKernel to add to ExpSquaredKernel. Defaults to None, that is, no PolynomialKernel is added and the GP only uses the ExpSquaredKernel white_noise : float, optional From george docs: "A description of the logarithm of the white noise variance added to the diagonal of the covariance matrix". Defaults to ln(white_noise) = -12. Note: if order is not None, you might need to set the white_noise to a larger value for the computation to be numerically stable, but this, as always, depends on the application. fitAmp : bool, optional Whether or not to include an amplitude term. Defaults to False. Returns ------- gp : george.GP Gaussian process with initialized kernel and factorized covariance matrix. """ # Tidy up the shapes and determine dimensionality theta = np.asarray(theta).squeeze() y = np.asarray(y).squeeze() if theta.ndim <= 1: ndim = 1 else: ndim = theta.shape[-1] # Guess initial metric, or scale length of the covariances (must be > 0) initialMetric = np.fabs(np.random.randn(ndim)) # Create kernel: We'll model coveriances in loglikelihood space using a # ndim-dimensional Squared Expoential Kernel kernel = george.kernels.ExpSquaredKernel(metric=initialMetric, ndim=ndim) # Include an amplitude term? if fitAmp: kernel = np.var(y) * kernel # Add a linear regression kernel of order order? # Use a meh guess for the amplitude and for the scale length (log(gamma^2)) if order is not None: kernel = kernel + (np.var(y)/10.0) * george.kernels.LinearKernel(log_gamma2=initialMetric[0], order=order, bounds=None, ndim=ndim) # Create GP and compute the kernel, aka factor the covariance matrix gp = george.GP(kernel=kernel, fit_mean=True, mean=np.median(y), white_noise=white_noise, fit_white_noise=False) gp.compute(theta) return gp
# Load the dataset. data = sm.datasets.get_rdataset("co2").data t = np.array(data.time) y = np.array(data.co2) # Initialize the kernel. k1 = 66.0**2 * kernels.ExpSquaredKernel(67.0**2) k2 = 2.4**2 * kernels.ExpSquaredKernel(90**2) \ * kernels.ExpSine2Kernel(2.0 / 1.3**2, 1.0) k3 = 0.66**2 * kernels.RationalQuadraticKernel(0.78, 1.2**2) k4 = 0.18**2 * kernels.ExpSquaredKernel(1.6**2) + kernels.WhiteKernel(0.19) kernel = k1 + k2 + k3 + k4 # Set up the Gaussian process. gp = george.GP(kernel, mean=np.mean(y)) s = time.time() gp.compute(t) gp.lnlikelihood(y) print(time.time() - s) # Define the probabilistic model. def lnprob(p): # Trivial prior: uniform in the log. if np.any((-10 > p) + (p > 10)): return -np.inf lnprior = 0.0 # Update the kernel and compute the lnlikelihood.
# l = np.where((fc >= fmin + j * df) & (fc < fmin + (j+1) * df))[0] # np.random.shuffle(l) # l = l[:nperbin] # sel[i,l] = True sel = np.ones(flux.shape, bool) ##################### # Rolls Royce model # ##################### t0 = clock() a0 = 0.71 r0 = 17.0 kernel = a0**2 * george.kernels.Matern52Kernel(r0**2) gp = george.GP(kernel, mean=1.0) lwav_shift = np.copy(lwav_corr) wav_shift = np.copy(wav_corr) ndat = sel.sum() nseg = 1 n = ndat / nseg while (ndat / nseg) > nmax: nseg += 1 n = ndat / nseg print nseg, n def lnprob2(p): npar = len(p)
def fit_gaussian_process(data, fix_scale=True, length_scale=20.): """Fit photometric observations with a gaussian regression Args: data (Table): Data table from sndata (format_sncosmo=True) fix_scale (bool): Whether to fix the scale while fitting length_scale (float): The initial length scale to use for the fit. Returns: A Gaussian process conditioned on the object's light-curve. """ if isinstance(data, Table): data = data.to_pandas() # Format data. Not included in original Avocado code fluxes = data['flux'] flux_errors = data['fluxerr'] wavelengths = data['band'].map(get_effective_wavelength) times = data['time'] # Use the highest signal-to-noise observation to estimate the scale. # Include an error floor so that for very high # signal-to-noise observations we pick the maximum flux value. signal_to_noises = (np.abs(fluxes) / np.sqrt(flux_errors**2 + (1e-2 * np.max(fluxes))**2)) scale = np.abs(fluxes[signal_to_noises.idxmax()]) # Construct gaussian process kernel = get_kernel(scale, fix_scale, length_scale) gp = george.GP(kernel) # Compute the covariance matrix x_data = np.vstack([times, wavelengths]).T gp.compute(x_data, flux_errors) # Define log likelihood calculations def neg_ln_like(p): gp.set_parameter_vector(p) return -gp.log_likelihood(fluxes) def grad_neg_ln_like(p): gp.set_parameter_vector(p) return -gp.grad_log_likelihood(fluxes) bounds = [(0, np.log(1000**2))] if not fix_scale: bounds = [(-30, 30)] + bounds fit_result = minimize( neg_ln_like, gp.get_parameter_vector(), jac=grad_neg_ln_like, bounds=bounds, ) if not fit_result.success: warn("GP fit failed! Using guessed GP parameters. ") gp.set_parameter_vector(fit_result.x) # Wrap the output function to return error instead of variance # Not included in original Avocado code def out_func(*args, **kwargs): p, pv = partial(gp.predict, fluxes)(*args, **kwargs) return p, np.sqrt(pv) return out_func
def run(self, dataSlice, slicePoint=None): """ Args: dataSlice (ndarray): Values passed to metric by the slicer, which the metric will use to calculate metric values at each slicePoint. slicePoint (Dict): Dictionary of slicePoint metadata passed to each metric. Returns: float: Interpolated static-probes Figure-of-Merit. """ # Chop off any outliers good_pix = np.where(dataSlice[self.col] > 0)[0] # Calculate area and med depth from area = hp.nside2pixarea(self.nside, degrees=True) * np.size(good_pix) median_depth = np.median(dataSlice[self.col][good_pix]) # FoM is calculated at the following values parameters = dict( area=np.array([ 7623.22, 14786.3, 9931.47, 8585.43, 17681.8, 15126.9, 9747.99, 8335.08, 9533.42, 18331.3, 12867.8, 17418.9, 19783.1, 12538.8, 15260., 16540.7, 19636.8, 11112.7, 10385.5, 16140.2, 18920.1, 17976.2, 11352., 9214.77, 16910.7, 11995.6, 16199.8, 14395.1, 8133.86, 13510.5, 19122.3, 15684.5, 12014.8, 14059.7, 10919.3, 13212.7 ]), depth=np.array([ 25.3975, 26.5907, 25.6702, 26.3726, 26.6691, 24.9882, 25.0814, 26.4247, 26.5088, 25.5596, 25.3288, 24.8035, 24.8792, 25.609, 26.2385, 25.0351, 26.7692, 26.5693, 25.8799, 26.3009, 25.5086, 25.4219, 25.8305, 26.2953, 26.0183, 25.26, 25.7903, 25.1846, 26.7264, 26.0507, 25.6996, 25.2256, 24.9383, 26.1144, 25.9464, 26.1878 ]), shear_m=np.array([ 0.00891915, 0.0104498, 0.0145972, 0.0191916, 0.00450246, 0.00567828, 0.00294841, 0.00530922, 0.0118632, 0.0151849, 0.00410151, 0.0170622, 0.0197331, 0.0106615, 0.0124445, 0.00994507, 0.0136251, 0.0143491, 0.0164314, 0.016962, 0.0186608, 0.00945903, 0.0113246, 0.0155225, 0.00800846, 0.00732104, 0.00649453, 0.00243976, 0.0125932, 0.0182587, 0.00335859, 0.00682287, 0.0177269, 0.0035219, 0.00773304, 0.0134886 ]), sigma_z=np.array([ 0.0849973, 0.0986032, 0.0875521, 0.0968222, 0.0225239, 0.0718278, 0.0733675, 0.0385274, 0.0425549, 0.0605867, 0.0178555, 0.0853407, 0.0124119, 0.0531027, 0.0304032, 0.0503145, 0.0132213, 0.0941765, 0.0416444, 0.0668198, 0.063227, 0.0291332, 0.0481633, 0.0595606, 0.0818742, 0.0472518, 0.0270185, 0.0767401, 0.0219945, 0.0902663, 0.0779705, 0.0337666, 0.0362358, 0.0692429, 0.0558841, 0.0150457 ]), sig_delta_z=np.array([ 0.0032537, 0.00135316, 0.00168787, 0.00215043, 0.00406031, 0.00222358, 0.00334993, 0.00255186, 0.00266499, 0.00159226, 0.00183664, 0.00384965, 0.00427765, 0.00314377, 0.00456113, 0.00347868, 0.00487938, 0.00418152, 0.00469911, 0.00367598, 0.0028009, 0.00234161, 0.00194964, 0.00200982, 0.00122739, 0.00310886, 0.00275168, 0.00492736, 0.00437241, 0.00113931, 0.00104864, 0.00292328, 0.00452082, 0.00394114, 0.00150756, 0.003613 ]), sig_sigma_z=np.array([ 0.00331909, 0.00529541, 0.00478151, 0.00437497, 0.00443062, 0.00486333, 0.00467423, 0.0036723, 0.00426963, 0.00515357, 0.0054553, 0.00310132, 0.00305971, 0.00406327, 0.00594293, 0.00348709, 0.00562526, 0.00396025, 0.00540537, 0.00500447, 0.00318595, 0.00460592, 0.00412137, 0.00336418, 0.00524988, 0.00390092, 0.00498349, 0.0056667, 0.0036384, 0.00455861, 0.00554822, 0.00381061, 0.0057615, 0.00357705, 0.00590572, 0.00422393 ]), FOM=np.array([ 11.708, 33.778, 19.914, 19.499, 41.173, 17.942, 12.836, 26.318, 25.766, 28.453, 28.832, 14.614, 23.8, 21.51, 27.262, 20.539, 39.698, 19.342, 17.103, 25.889, 25.444, 32.048, 24.611, 23.747, 32.193, 18.862, 34.583, 14.54, 23.31, 25.812, 39.212, 25.078, 14.339, 24.12, 24.648, 29.649 ])) # Standardizing data df_unscaled = pd.DataFrame(parameters) X_params = [ 'area', 'depth', 'shear_m', 'sigma_z', 'sig_delta_z', 'sig_sigma_z' ] scalerX = StandardScaler() scalerY = StandardScaler() X = df_unscaled.drop('FOM', axis=1) X = pd.DataFrame(scalerX.fit_transform(df_unscaled.drop('FOM', axis=1)), columns=X_params) Y = pd.DataFrame(scalerY.fit_transform( np.array(df_unscaled['FOM']).reshape(-1, 1)), columns=['FOM']) # Building Gaussian Process based emulator kernel = kernels.ExpSquaredKernel(metric=[1, 1, 1, 1, 1, 1], ndim=6) gp = george.GP(kernel, mean=Y['FOM'].mean()) gp.compute(X) def neg_ln_lik(p): gp.set_parameter_vector(p) return -gp.log_likelihood(Y['FOM']) def grad_neg_ln_like(p): gp.set_parameter_vector(p) return -gp.grad_log_likelihood(Y['FOM']) result = minimize(neg_ln_lik, gp.get_parameter_vector(), jac=grad_neg_ln_like) gp.set_parameter_vector(result.x) # Survey parameters to predict FoM at to_pred = np.array([[ area, median_depth, self.shear_m, self.sigma_z, self.sig_delta_z, self.sig_sigma_z ]]) to_pred = scalerX.transform(to_pred) predSFOM = gp.predict(Y['FOM'], to_pred, return_cov=False) predFOM = scalerY.inverse_transform(list(predSFOM)) return predFOM[0]
#tic = time.time() #args = (m,mNoisy,w,sigma,t) #f = lnprob(initial,*args) #print '1 exec took %0.2f' % (time.time() - tic) # best fit noise model #p = initial p = res.x m.set_all(*p[2:]) fit = m.source mBroad = m.broaden(sigma) tic = time.time() a, tau = np.exp(p[:2]) k = a * kernels.Matern32Kernel(tau, ndim=2) gp = george.GP(k) #gp.compute(t, 1/np.sqrt(w)) gp.compute(t, mBroad.flatten() / mBroad.sum() / np.sqrt(w)) mu = gp.predict(mNoisy.flatten() - mBroad.flatten(), t, mean_only=True) print 'one exec took %0.2fs' % (time.time() - tic) # figures plt.figure() plt.imshow(mu.reshape(fit.shape)) #plt.subplot(121); plt.imshow(mTrue); plt.title('true') #plt.subplot(122); plt.imshow(fit); plt.title('fit') plt.figure() vmin, vmax = 0, mNoisy.max() plt.subplot(122) plt.imshow(mNoisy, vmin=0, vmax=vmax)
fig, axes = pl.subplots(nrow, ncol, sharex=True, sharey=True, figsize=(12, 8)) fig.subplots_adjust(left=0.05, right=0.95, top=0.95, bottom=0.05, wspace=0.025, hspace=0.025) for i, ax in enumerate(axes.flatten()): ax.set_xticklabels([]) ax.set_yticklabels([]) # The time array time = np.linspace(0, 1, tlen) # The intrinsic fluxes (stellar variability + a little white noise) gp_stellar = george.GP(astr**2 * george.kernels.Matern32Kernel(tstr**2)) truths = gp_stellar.sample(time, size=nflx) truths += awht * np.random.randn(nflx, tlen) # The systematic components gp_sys = george.GP(asys**2 * george.kernels.Matern32Kernel(tsys**2)) systematics = gp_sys.sample(time, size=nsys) fluxes = np.zeros_like(truths) for i in range(nflx): fluxes[i] = truths[i] + np.dot(np.random.randn(nsys), systematics) # Subtract the median fluxes -= np.nanmedian(fluxes, axis=1).reshape(-1, 1) # Assign constant errors (for now) errors = awht * np.ones_like(fluxes)
s_obs = np.zeros((nsim, npix)) istart = np.random.uniform(0, dx, nsim).astype(int) for i in np.arange(nsim): x_obs[i, :] = x_degraded[istart[i]:istart[i] + npix * dx:dx] y_obs[i, :] = y_degraded[istart[i]:istart[i] + npix * dx:dx] sigma = 0.01 * np.sqrt(y_obs[i, :]) s_obs[i, :] = sigma noise = np.random.normal(0, 1, npix) * sigma y_obs[i, :] += noise print 'observed spectra' pl.plot(x_obs.T, y_obs.T, '.') pl.draw() raw_input('continue?') k = 1.0 * george.kernels.ExpSquaredKernel(1.0) gp = george.GP(k) x = x_obs[4, :].flatten() y = y_obs[4, :].flatten() s = s_obs[4, :].flatten() ss = np.argsort(x) x = x[ss] y = y[ss] s = s[ss] gp.compute(x, yerr=s) gp.optimize(x, y, yerr=s) print np.sqrt(gp.kernel.pars) gp.compute(x, yerr=s) mu, cov = gp.predict(y, x_true) err = np.sqrt(np.diag(cov)) print 'conditioned on one observation only' pl.plot(x_true, mu, 'r-')
plt.plot(freqs[1], split_vals_plot[1], label='Real', color='blue') #plt.plot(freqs[1],mean_model.get_value_plot(None)[1], label = 'First Guess',color = 'red', linestyle = '-.') plt.plot(freqs[2], split_vals_plot[2], label='Real', color='green') #plt.plot(freqs[2],mean_model.get_value_plot(None)[2], label = 'First Guess', color = 'green', linestyle = '-.') plt.legend(loc='best') plt.title('Actual Rotation Profile splitting values') plt.show() #gp = george.GP(0.001 * kernels.ExpSquaredKernel(metric=([1,0],[0,1]), metric_bounds = [(0,7),(None,None),(-5,-2)], ndim = 2), mean=mean_model) #gp = george.GP(1 * kernels.ExpSquaredKernel(metric=np.array([[1,0],[0,100]]), metric_bounds=[(-5, 5), (None, None), (-5, -2.3)], ndim=2), mean=mean_model) gp = george.GP(1 * kernels.ExpSquaredKernel(metric=1e-3 * np.eye(2), ndim=2, metric_bounds=[(-7, 5), (None, None), (-5, -1.5)]), mean=mean_model) #gp = george.GP(1 * kernels.ExpSquaredKernel(metric=1e-5 * np.eye(2), ndim=2, metric_bounds=[(-10, 10), (None, None), (-6, -2.3)])) gp.set_parameter('kernel:k2:metric:L_0_1', 0) gp.freeze_parameter('kernel:k2:metric:L_0_1') #this is the part where im confused #gp.compute(flatx) gp.compute(xvals, e_split_vals) def log_prior(p):
xsinx = lambda x: -x*np.sin(x) # Plot and show the function. t = np.linspace(0, 10, 500) # Return 500 evenly spaced numbers over range [0, 10] plt.plot(t, xsinx(t)) plt.ylabel("y") plt.xlabel("x") plt.show() # # Gaussian Process (with zero knowledge). # # Set up a gaussian process. The main ingredient of a gaussian process is the # kernel. It describes how correlated each point is with every other. kernel = ExpSquaredKernel(1.0) gp = george.GP(kernel) # Here we draw plot to show what kind of function we have when the gaussian # process have no knowledge of our objective value of our function. We draw # 10 samples below: fig, ax = plt.subplots() for pred in gp.sample(t, 10): ax.plot(t, pred) ax.set_ylim(-9, 7) ax.set_xlabel("x") ax.set_ylabel("y") plt.show() # If we choose a different kernel, the functions we sample will end up looking # different. matern_kernel = Matern32Kernel(1.0)
print("") x = np.array(x) y_fof2 = np.array(y_fof2) mean_fof2 = np.mean(y_fof2) y_fof2 -= mean_fof2 y_mufd = np.array(y_mufd) mean_mufd = np.mean(y_mufd) y_mufd -= mean_mufd y_hmf2 = np.array(y_hmf2) mean_hmf2 = np.mean(y_hmf2) y_hmf2 -= mean_hmf2 sigma = np.array(sigma) gp = george.GP(kernel) gp.compute(x, sigma + 1e-3) tm = np.array(range(last_tm - (86400 * 2), last_tm + (86400 * 7) + 1, 300)) pred_fof2, sd_fof2 = gp.predict(y_fof2, tm / 86400., return_var=True) pred_fof2 += mean_fof2 sd_fof2 = np.sqrt(sd_fof2) pred_mufd, sd_mufd = gp.predict(y_mufd, tm / 86400., return_var=True) pred_mufd += mean_mufd sd_mufd = np.sqrt(sd_mufd) pred_hmf2, sd_hmf2 = gp.predict(y_hmf2, tm / 86400., return_var=True) pred_hmf2 += mean_hmf2 sd_hmf2 = np.sqrt(sd_hmf2) for i in range(len(tm)):
def create_testcase06(): import george bjd0 = photometry['phot_bjd'] - Tref err = bjd0 * 0 + photometry['phot_precision'] """ Conversion of the physical parameter to the internally defined parameter to be passed to george """ gp_pams = np.zeros(4) gp_pams[0] = np.log(activity['Hamp_PH']) * 2 gp_pams[1] = np.log(activity['Pdec']) * 2 gp_pams[2] = 1. / (2 * activity['Oamp']**2) gp_pams[3] = np.log(activity['Prot']) kernel = np.exp(gp_pams[0]) * \ george.kernels.ExpSquaredKernel(metric=np.exp(gp_pams[1])) * \ george.kernels.ExpSine2Kernel(gamma=gp_pams[2], log_period=gp_pams[3]) gp = george.GP(kernel) gp.compute(bjd0, err) prediction = gp.sample(bjd0) obs_photometry = np.random.normal(prediction, photometry['phot_precision']) fileout = open('TestCase06_photometry.dat', 'w') for b, p in zip(photometry['phot_bjd'], obs_photometry): fileout.write('{0:14f} {1:14f} {2:14f} {3:5d} {4:5d} {5:5d} \n'.format( b, p, photometry['phot_precision'], 0, 0, -1)) fileout.close() bjd0 = bjd_obs - Tref err = bjd0 * 0 + instrument['RV_precision'] gp_pams[0] = np.log(activity['Hamp_RV1']) * 2 kernel = np.exp(gp_pams[0]) * \ george.kernels.ExpSquaredKernel(metric=np.exp(gp_pams[1])) * \ george.kernels.ExpSine2Kernel(gamma=gp_pams[2], log_period=gp_pams[3]) gp = george.GP(kernel) gp.compute(bjd0, err) prediction = gp.sample(bjd0) y_pla = kp.kepler_RV_T0P(bjd0, planet_b['f'], planet_b['P'], planet_b['K'], planet_b['e'], planet_b['o']) + instrument['RV_offset1'] mod_pl = np.random.normal(y_pla + prediction, instrument['RV_precision']) Tcent_b = np.random.normal( np.arange(0, 1) * planet_b['P'] + kp.kepler_Tcent_T0P( planet_b['P'], planet_b['f'], planet_b['e'], planet_b['o']) + Tref, instrument['T0_precision']) fileout = open('TestCase06_Tcent_b.dat', 'w') for i_Tc, v_Tc in enumerate(Tcent_b): fileout.write('{0:d} {1:.4f} {2:.4f} {3:d}\n'.format( i_Tc, v_Tc, instrument['T0_precision'], 0)) fileout.close() fileout = open('TestCase06_RV.dat', 'w') for b, r in zip(bjd_obs, mod_pl): fileout.write('{0:f} {1:.2f} {2:.2f} {3:d} {4:d} {5:d}\n'.format( b, r, instrument['RV_precision'], 0, 0, -1)) fileout.close()
def gp_train(kernel, lctrain): gp = george.GP(kernel) gp.compute(lctrain.x, lctrain.yerr**2) return gp
#y_ = 1 + d import george from george import kernels t = t_[use_for_pred] y = y_[use_for_pred] var_y = np.var(y) k1 = var_y * kernels.ExpSquaredKernel(metric=1) k2 = var_y * kernels.ExpSquaredKernel(metric=1) \ * kernels.ExpSine2Kernel(gamma=100, log_period=0.0) kernel = k1 + k2 #kernel = k1 gp = george.GP(kernel, white_noise=np.log(var_y), fit_white_noise=True) gp.compute(t) pn = 'kernel:k2:k2:log_period' if pn in gp.get_parameter_names(): gp.freeze_parameter(pn) #gp.freeze_parameter('kernel:k2:k2:log_period') import scipy.optimize as op def nll(p): gp.set_parameter_vector(p) ll = gp.log_likelihood(y, quiet=True) return -ll if np.isfinite(ll) else 1e25
# For each bin, make GP predictions of the bin centroid. parameter_names = list(results[f"models/{model_name}/gp_model"].keys())[2:] gps = {} for parameter_name in parameter_names: X = results[f"models/{model_name}/gp_model/{parameter_name}/X"][()] Y = results[f"models/{model_name}/gp_model/{parameter_name}/Y"][()] attrs = results[f"models/{model_name}/gp_model/{parameter_name}"].attrs metric = np.var(X, axis=0) kernel = george.kernels.ExpSquaredKernel(metric=metric, ndim=metric.size) gp = george.GP(kernel, mean=np.mean(Y), fit_mean=True, white_noise=np.log(np.std(Y)), fit_white_noise=True) for p in gp.parameter_names: gp.set_parameter(p, attrs[p]) gp.compute(X) gps[parameter_name] = (gp, Y) # Now make predictions at each centroid. mg = np.array(np.meshgrid(*centroids)).reshape((len(lns), -1)).T gp_predictions = np.empty((mg.shape[0], 4, 2)) gp_predictions[:] = np.nan for i, parameter_name in enumerate(parameter_names):
d_harps1=0., d_harps2=0.) kwargs = dict(**truth) kwargs["bounds"] = dict(P1=(12.0, 13.0), k1=(0, 1.), w1=(-2 * np.pi, 2 * np.pi), e1=(0.7, 0.95), P2=(35, 200), k2=(0, 2.), w2=(-2 * np.pi, 2 * np.pi), e2=(0., 0.5)) mean_model = Model(**kwargs) gp = george.GP(kernel, mean=mean_model, fit_mean=True, white_noise=np.log(0.5**2), fit_white_noise=True) # gp.freeze_parameter('kernel:k2:k1:log_constant') gp.compute(x, yerr) lnp1 = gp.log_likelihood(y) def lnprob2(p): gp.set_parameter_vector(p) return gp.log_likelihood(y, quiet=True) + gp.log_prior() #============================================================================== # MCMC #==============================================================================
# Other inputs: n_live_points = int(args.nlive) jitter = george.modeling.ConstantModel(np.log((200. * 1e-6)**2.)) # create dictionaries to store kernels and gp classes for individual nights kernels = {} gps = {} for key in nights: kernels[key] = np.var(nights[key][1]) * george.kernels.ExpSquaredKernel( np.ones(nights[key][2].shape[0]), ndim=(nights[key][2].shape[0]), axes=range(nights[key][2].shape[0])) # Wrap GP object to compute likelihood gps[key] = george.GP(kernels[key], mean=0.0, fit_mean=False, white_noise=jitter, fit_white_noise=True) gps[key].compute(nights[key][2].T) # Now define MultiNest priors and log-likelihood: def prior(cube): # Prior on "median flux" is uniform: cube[0] = utils.transform_uniform(cube[0], -2., 2.) # Pior on the log-jitter term (note this is the log VARIANCE, not sigma); from 0.01 to 100 ppm: cube[1] = utils.transform_uniform(cube[1], np.log((0.01e-3)**2), np.log((100e-3)**2)) pcounter = 2 # Prior on coefficients of comparison stars: if compfilename is not None:
def train(self, X, y, do_optimize=True, **kwargs): """ Performs MCMC sampling to sample hyperparameter configurations from the likelihood and trains for each sample a GP on X and y Parameters ---------- X: np.ndarray (N, D) Input data points. The dimensionality of X is (N, D), with N as the number of points and D is the number of features. y: np.ndarray (N,) The corresponding target values. do_optimize: boolean If set to true we perform MCMC sampling otherwise we just use the hyperparameter specified in the kernel. """ if self.normalize_input: # Normalize input to be in [0, 1] self.X, self.lower, self.upper = normalization.zero_one_normalization(X, self.lower, self.upper) else: self.X = X if self.normalize_output: # Normalize output to have zero mean and unit standard deviation self.y, self.y_mean, self.y_std = normalization.zero_mean_unit_var_normalization(y) if self.y_std == 0: raise ValueError("Cannot normalize output. All targets have the same value") else: self.y = y # Use the mean of the data as mean for the GP self.mean = np.mean(self.y, axis=0) self.gp = george.GP(self.kernel, mean=self.mean) if do_optimize: # We have one walker for each hyperparameter configuration sampler = emcee.EnsembleSampler(self.n_hypers, len(self.kernel.pars) + 1, self.loglikelihood) sampler.random_state = self.rng.get_state() # Do a burn-in in the first iteration if not self.burned: # Initialize the walkers by sampling from the prior if self.prior is None: self.p0 = self.rng.rand(self.n_hypers, len(self.kernel.pars) + 1) else: self.p0 = self.prior.sample_from_prior(self.n_hypers) # Run MCMC sampling self.p0, _, _ = sampler.run_mcmc(self.p0, self.burnin_steps, rstate0=self.rng) self.burned = True # Start sampling pos, _, _ = sampler.run_mcmc(self.p0, self.chain_length, rstate0=self.rng) # Save the current position, it will be the start point in # the next iteration self.p0 = pos # Take the last samples from each walker self.hypers = sampler.chain[:, -1] else: self.hypers = self.gp.kernel[:].tolist() self.hypers.append(self.noise) self.hypers = [self.hypers] self.models = [] for sample in self.hypers: # Instantiate a GP for each hyperparameter configuration kernel = deepcopy(self.kernel) kernel.pars = np.exp(sample[:-1]) noise = np.exp(sample[-1]) model = GaussianProcess(kernel, normalize_output=self.normalize_output, normalize_input=self.normalize_input, noise=noise, lower=self.lower, upper=self.upper, rng=self.rng) model.train(X, y, do_optimize=False) self.models.append(model) self.is_trained = True
def PLD_then_GP(q, id, kernel, kinit, kbounds, maxfun, datadir): ''' ''' global data if data is None: data = GetData(id, data_type='bkg', datadir=datadir) qd = data[q] if qd['time'] == []: return None # t/y for all chunks t_all = np.array([], dtype=float) y_all = np.array([], dtype=float) # Solve the (linear) PLD problem for this quarter fpix = np.array([x for y in qd['fpix'] for x in y]) fsum = np.sum(fpix, axis=1) init = np.array([np.median(fsum)] * fpix.shape[1]) def pm(y, *x): return np.sum(fpix * np.outer(1. / fsum, x), axis=1) x, _ = curve_fit(pm, None, fsum, p0=init) # Here's our detrended data y = [] t = [] e = [] for time, fpix, perr in zip(qd['time'], qd['fpix'], qd['perr']): # The pixel model fsum = np.sum(fpix, axis=1) pixmod = np.sum(fpix * np.outer(1. / fsum, x), axis=1) # The errors X = np.ones_like(time) + pixmod / fsum B = X.reshape(len(time), 1) * perr - x * perr / fsum.reshape( len(time), 1) yerr = np.sum(B**2, axis=1)**0.5 # Append to our arrays y.append(fsum - pixmod) t.append(time) e.append(yerr) # Now we solve for the best GP params res = fmin_l_bfgs_b(NegLnLikeGP, kinit, approx_grad=False, args=(t, y, e, kernel), bounds=kbounds, m=10, factr=1.e1, pgtol=1e-05, maxfun=maxfun) # Finally, detrend the data kernel.pars = res[0] gp = george.GP(kernel) for ti, yi, ei in zip(t, y, e): gp.compute(ti, ei) mu, _ = gp.predict(yi, ti) y_all = np.append(y_all, yi - mu) t_all = np.append(t_all, ti) return t_all, y_all
def run_vs_SearchPhase_UA2Fit(): args = parse_args() ext = args.output_file_extension from pygp.canvas import Canvas # Getting data points xRaw, yRaw, xerrRaw, yerrRaw = getDataPoints(args.input_file, 'dijetgamma_g85_2j65', 'Zprime_mjj_var') #Data processing, cutting out the not desired range x, y, xerr, yerr = dataCut(0, 1500, 0, xRaw, yRaw, xerrRaw, yerrRaw) xMinFit = 303 xMaxFit = 1500 xFit, yFit, xerrFit, yerrFit = dataCut(xMinFit, xMaxFit, 0, xRaw, yRaw, xerrRaw, yerrRaw) # make an evently spaced x t = np.linspace(np.min(x), np.max(x), 500) #calculating the log-likihood and minimizing for the gp lnProb = logLike_minuit(x, y, xerr) min_likelihood, best_fit = fit_gp_minuit(20, lnProb) fit_pars = [best_fit[x] for x in FIT3_PARS] #making the GP kargs = {x: y for x, y in best_fit.items() if x not in FIT3_PARS} kernel_new = get_kernel(**kargs) print(kernel_new.get_parameter_names()) #making the kernel gp_new = george.GP(kernel_new, mean=Mean(fit_pars), fit_mean=True) gp_new.compute(x, yerr) mu, cov = gp_new.predict(y, t) mu_x, cov_x = gp_new.predict(y, xFit) # calculating the fit function value #GP compute minimizes the log likelihood of the best_fit function best = [best_fit[x] for x in FIT3_PARS] print("best param meghan GP minmization:", best) meanFromGPFit = Mean(best) fit_meanM = meanFromGPFit.get_value( x, xerr) #so this is currently shit. can't get the xErr thing working print("fit_meanM:", fit_meanM) #fit results from search phase best_fit_params = (1.0779, 2.04035, 58.5769, -157.945) fit_mean = model_UA2(xFit, best_fit_params, xerrFit) ##----4 param fit function #lnProb = logLike_4ff(xFit,yFit,xerrFit) #minimumLLH, best_fit_params = fit_4ff(100, lnProb) #fit_mean = model_4param(xFit, best_fit_params, xerrFit) #calculating significance signif = significance(mu_x, y, cov_x, yerr) initialCutPos = np.argmax(x > xMinFit) finalCutPos = np.argmin(x < xMaxFit) #sigFit = (fit_mean - y[initialCutPos:]) / np.sqrt(np.diag(cov_x[initialCutPos:]) + yerr[initialCutPos:]**2) sigFit = significance(fit_mean, y[initialCutPos:], cov_x[initialCutPos:], yerr[initialCutPos:]) #sigit = (mu_x) #std = np.sqrt(np.diag(cov)) ext = args.output_file_extension title = "compareGPvsSearchPhaseUA2" with Canvas(f'%s{ext}' % title) as can: can.ax.errorbar(x, y, yerr=yerr, fmt='.') can.ax.set_yscale('log') # can.ax.set_ylim(1, can.ax.get_ylim()[1]) can.ax.plot(t, mu, '-r') can.ax.plot(xFit, fit_mean, '-b') #this only works with the xErr part of Mean commented out #can.ax.plot(x, fit_meanM, '.g') # can.ax.plot(t, fit_mean_smooth, '--b') #can.ax.fill_between(t, mu - std, mu + std, #facecolor=(0, 1, 0, 0.5), #zorder=5, label='err = 1') #can.ratio.stem(x, signif, markerfmt='.', basefmt=' ') can.ratio.stem(xFit, sigFit, markerfmt='.', basefmt=' ') can.ratio.axhline(0, linewidth=1, alpha=0.5) can.save(title)
def predict_Q_data(input_dir_2, filename4="training.txt"): nu, lam_squared, stokesQ, stokesU = read_data(input_dir_2, filename4) nu_R = nu stokesQ_R = stokesQ lam_R = lam_squared # Squared exponential kernel k1 = 0.3**2 * kernels.ExpSquaredKernel(0.02**2) # periodic covariance kernel with exponential component toallow decay away from periodicity k2 = 0.6**2 * kernels.ExpSquaredKernel(0.5**2) * kernels.ExpSine2Kernel( gamma=2 / 2.5**2, log_period=0.0) ###vary gamma value to widen the funnel # rational quadratic kernel for medium term irregularities. k3 = 0.3**2 * kernels.RationalQuadraticKernel(log_alpha=np.log(0.1), metric=0.1**2) # noise kernel: includes correlated noise & uncorrelated noise k4 = 0.18**2 * kernels.ExpSquaredKernel(1.6**2) kernel = k1 + k2 #+k3 + k4 gp = george.GP(kernel, mean=np.mean(stokesQ), fit_mean=True, white_noise=np.log(0.02**2), fit_white_noise=True) #gp = george.GP(kernel) gp.compute(lam_R) # range of times for prediction: #x = np.linspace(0.18, 0.21998, 360) x = x_values(input_dir_2) # calculate expectation and variance at each point: mu_initial, cov = gp.predict(stokesQ_R, x) #mu, cov = gp.predict(stokesQ_2Gz, x, return_var = True) std_initial = np.sqrt(np.diag(cov)) print("Initial prediction of missing Q values: \n {}".format(mu_initial)) # Define the objective function (negative log-likelihood in this case). def nll(p): gp.set_parameter_vector(p) ll = gp.log_likelihood(stokesQ_R, quiet=True) return -ll if np.isfinite(ll) else 1e25 # And the gradient of the objective function. def grad_nll(p): gp.set_parameter_vector(p) return -gp.grad_log_likelihood(stokesQ_R, quiet=True) gp.compute(lam_R) p0 = gp.get_parameter_vector() results = op.minimize(nll, p0, jac=grad_nll, method="L-BFGS-B") # run optimization: #results = op.minimize(nll, p0, jac=grad_nll) gp.set_parameter_vector(results.x) x = x_values(input_dir_2) mu_optimized, cov = gp.predict(stokesQ_R, x) std_optimized = np.sqrt(np.diag(cov)) print("Final prediction of missing Q values: \n {}".format(mu_optimized)) return nu_R, lam_R, stokesQ_R, mu_initial, std_initial, mu_optimized, std_optimized