#first optimise the function and get parameter errors from the conditionals guess_pars, err_pars = Infer.ConditionalErrors(LogLikelihood_iid_mf, guess_pars, err_pars, (Transit_aRs, time, flux), plot=0, opt=True) Infer.MCMC(LogLikelihood_iid_mf, guess_pars, (Transit_aRs, time, flux), chain_len, err_pars, n_chains=no_ch, adapt_limits=adapt_lims, glob_limits=glob_lims, thin=thin) par, par_err = Infer.AnalyseChains(conv / thin, n_chains=no_ch) #plot the chains and correlations #pylab.figure(2) #Infer.PlotChains(conv/thin,n_chains=no_ch,p=[0,2,3,4],labels=lab) pylab.figure(3) Infer.PlotCorrelations(conv / thin, n_chains=no_ch, p=np.where(np.array(par_err) > 0.)[0]) #importance sampling to get the evidence and re-estimate the mean/std of each parameter pylab.figure(4) m, K = Infer.NormalFromMCMC(conv / thin, n_chains=no_ch, plot=1) #get covariance matrix of MCMC chains Infer.ImportanceSamp(LogLikelihood_iid_mf, (Transit_aRs, time, flux), m, 2 * K, 10000) #importance sample
#create example with some noise/systematics tpar = [0,3.0,10,0.1,0.2,0.2,0.2,1.0,0.0] time = np.linspace(-0.1,0.1,300) flux = MF.Transit_aRs(tpar,time) + 0.001*np.sin(2*np.pi*40*time) + np.random.normal(0.,0.0005,time.size) #construct the GP gp = GeePea.GP(time,flux,p=tpar+[0.1,0.01,0.001],mf=MF.Transit_aRs) gp.opt() #optimise #run quick MCMC to test predictions: ch_len = 10000 lims = (0,5000,10) epar = [0,0,0,0.001,0,0,0,0,0,] + [0.001,0.01,0.001] Infer.MCMC_N(gp.logPosterior,gp.p,(),ch_len,epar,adapt_limits=lims,glob_limits=lims,chain_filenames=['test_chain']) p,perr = Infer.AnalyseChains(lims[1],chain_filenames=['test_chain']) X = Infer.GetSamples(5000,100,chain_filenames=['test_chain']) #get samples from the chains os.remove('test_chain.npy') #standard plot pylab.figure() gp.plot() #pylab.savefig('test.pdf') #density plot for single parameter set pylab.figure() f,ferr = gp.predict() GeePea.PlotDensity(time,f,ferr) pylab.plot(time,flux,'ro',ms=3) #density plot for sample of params
#get residuals resid = f - MF.Transit_aRs(p, t) #compare with MCMC fit - use LM values as inputs lims = (0, 4000, 4) MCMC_p = list(p) + [ wn, ] MCMC_pe = list(pe) + [ epar[-1], ] Infer.MCMC_N(MF.LogLikelihood_iid_mf, MCMC_p, (MF.Transit_aRs, t, f), 10000, MCMC_pe, adapt_limits=lims, glob_limits=lims, N=2) MCMC_p, MCMC_pe = Infer.AnalyseChains(lims[1], n_chains=2, N_obs=t.size) os.remove('MCMC_chain_1.npy') os.remove('MCMC_chain_2.npy') #plot the data pylab.plot(t, f, 'k.') pylab.plot(t, MF.Transit_aRs(gpar[:-1], t), 'g-') pylab.plot(t, MF.Transit_aRs(p, t), 'r-') pylab.plot(t, resid + 1.0 - 1.5 * np.ptp(MF.Transit_aRs(p, t)), 'k.') pylab.axhline(1.0 - 1.5 * np.ptp(MF.Transit_aRs(p, t)), color='r') pylab.plot(t, MF.Transit_aRs(MCMC_p[:-1], t), 'b-') raw_input()
chain_len = 40000 conv = 10000 thin = 10 no_ch = 2 lims = (1000, conv, 5) err_pars = np.array([0.001, 0.001, 0.0001] + [0.00001, 0, 0.2, 0.0003, 0.02, 0.0, 0.0, 0.001, 0.0001]) Infer.MCMC(MyGP.logPosterior, MyGP._pars, (), chain_len, err_pars, n_chains=no_ch, adapt_limits=lims, glob_limits=lims, thin=thin) MyGP.pars, par_err = Infer.AnalyseChains(conv / thin, n_chains=no_ch) pylab.figure(2) Infer.PlotCorrelations(conv / thin, n_chains=no_ch) #do an Affine Invariant MCMC n = 100 err_pars = 0.01 * np.array( [0.001, 0.001, 0.0001] + [0.00001, 0, 0.2, 0.0003, 0.02, 0.0, 0.0, 0.001, 0.0001]) Infer.AffInvMCMC(MyGP.logPosterior, MyGP._pars, (), n, chain_len / n, err_pars, n_chains=no_ch) MyGP.pars, par_err = Infer.AnalyseChains(conv, n_chains=no_ch)
gp.plot() #can also run an MCMC by using GP.logPosterior() ch = 10000 conv = 0.4 * ch lims = (0, conv, 10) Infer.MCMC_N(gp.logPosterior, gp.p, (), ch, gp.ep, N=2, adapt_limits=lims, glob_limits=lims) #Infer.AffInvMCMC(gp.logPosterior,gp.p,(),500,ch/500,gp.ep*0.01,n_chains=2) pylab.figure(2) Infer.PlotCorrelations(conv, n_chains=2, p=np.where(np.array(gp.ep) > 0)[0]) #pylab.savefig('Correlations.png') #get the parameters and uncertainties from the MCMC gp.p, gp.ep = Infer.AnalyseChains(conv, n_chains=2) #and plot pylab.figure(1) gp.plot() #delete the MCMC chains # if os.path.exists('MCMC_chain_1.npy'): os.remove('MCMC_chain_1.npy') # if os.path.exists('MCMC_chain_2.npy'): os.remove('MCMC_chain_2.npy') #raw_input()
par_guess, (time, ), f, fixed=~(np.array(epar[:-1]) > 0)) #fit with simple MCMC lims = [1000, 20000, 5] Infer.MCMC_N(MF.LogLikelihood_iid_mf, np.concatenate([par_guess, [ wn, ]]), (mf, time, f), 30000, epar, adapt_limits=lims, glob_limits=lims, N=2) MCMC_par, MCMC_epar = Infer.AnalyseChains(20000, n_chains=2) os.system('rm MCMC_chain_?.npy') #fit with nelder mead NM_par = Infer.Optimise(MF.LogLikelihood_iid_mf, np.concatenate([par_guess, [ wn, ]]), (mf, time, f), fixed=fp) #fit with brute force/Infer BR_par = Infer.Brute(MF.LogLikelihood_iid_mf, np.concatenate([par_guess, [ wn, ]]), (mf, time, f), epar,
if _e == 0.: bounds_mf[i] = None gp.opt_global(bounds=bounds_mf + bounds_hp) #and optimise #finally make a plot plt.figure(1) gp.plot() #can also run an MCMC by using GP.logPosterior() lims = (0, 10000, 4) Infer.MCMC_N(gp.logPosterior, gp.p, (), 20000, gp.ep, N=2, adapt_limits=lims, glob_limits=lims) #get the parameters and uncertainties from the MCMC gp.p, gp.ep = Infer.AnalyseChains(10000, n_chains=2) #and plot the correlations plt.figure(2) Infer.PlotCorrelations(10000, n_chains=2, p=np.where(np.array(gp.ep) > 0)[0]) #pylab.savefig('Correlations.png') #delete the MCMC chains if os.path.exists('MCMC_chain_1.npy'): os.remove('MCMC_chain_1.npy') if os.path.exists('MCMC_chain_2.npy'): os.remove('MCMC_chain_2.npy') raw_input()