def run_carmcmc(date, mag, mag_err): # Maximum order of the autoregressive polynomial MAX_ORDER_AR = 6 # Number of samples from the posterior to generate N_SAMPLES = 20000 model = cm.CarmaModel(date, mag, mag_err) model.choose_order(MAX_ORDER_AR, njobs=-1) sample = model.run_mcmc(N_SAMPLES) psd_low, psd_hi, psd_mid, frequencies = sample.plot_power_spectrum( percentile=95.0, nsamples=5000) dt = t[1:] - t[:-1] noise_level_mean = 2.0 * np.mean(dt) * np.mean(yerr**2) noise_level_median = 2.0 * np.median(dt) * np.median(yerr**2) print "psd" print psd_mid print "frec" print frequencies params = {param: sample.get_samples(param) for param in sample.parameters} params['p'] = model.p params['q'] = model.q return params
def run_car1(t, y_obs, ysig,nsamples): t = t - t[0] model = cm.CarmaModel(t, y_obs, ysig, p=1, q=0) sample = model.run_mcmc(nsamples) log_omega=sample.get_samples('log_omega') tau=np.exp(-1.0*log_omega) sigma=sample.get_samples('sigma') #ax = plt.subplot(111) #psd_low, psd_hi, psd_mid, frequencies = sample.plot_power_spectrum(percentile=95.0, sp=ax, doShow=False, color='SkyBlue', nsamples=5000) #plt.show() #compute the mode of the distributions of tau and sigma nt, bins=np.histogram(tau,30) binst = (bins[1:] + bins[0:-1])*0.5 elemt = np.argmax(nt) mode_tau=binst[elemt] ns, bins=np.histogram(sigma,30) binss = (bins[1:] + bins[0:-1])*0.5 elems = np.argmax(ns) mode_sigma=binss[elems] tau_mc=(mode_tau,np.percentile(tau, 50),np.percentile(tau, 50)-np.percentile(tau, 15.865),np.percentile(tau, 84.135)-np.percentile(tau, 50)) sigma_mc=(mode_sigma,np.percentile(sigma, 50),np.percentile(sigma, 50)-np.percentile(sigma, 15.865),np.percentile(sigma, 84.135)-np.percentile(sigma, 50)) return (np.array(tau_mc),np.array(sigma_mc))
def drw(jd, mag, errmag): model = cm.CarmaModel(jd, mag, errmag, p=1, q=0) sample = model.run_mcmc(10000) log_omega=sample.get_samples('log_omega') tau=np.exp(-1.0*log_omega) sigma=sample.get_samples('sigma') tau_mc=(np.percentile(tau, 50),np.percentile(tau, 50)-np.percentile(tau, 15.865),np.percentile(tau, 84.135)-np.percentile(tau, 50)) sigma_mc=(np.percentile(sigma, 50),np.percentile(sigma, 50)-np.percentile(sigma, 15.865),np.percentile(sigma, 84.135)-np.percentile(sigma, 50)) #tau_mc,sigma_mc=[-99,-99,-99], [-99,-99,-99] return(tau_mc[0],tau_mc[1],tau_mc[2],sigma_mc[0],sigma_mc[1],sigma_mc[2])
def carma_order(date, mag, mag_err, maxp, aic_file): #function to calculate the order p and q of the CARMA(p,q) process # maxp: Maximum value allowed for p, maximun value for q is by default p-1 date = date - date[0] model = cm.CarmaModel(date, mag, mag_err) MAP, pqlist, AIC_list = model.choose_order(maxp, njobs=1) # convert lists to a numpy arrays, easier to manipulate #the results of the AIC test are stored for future references. pqarray = np.array(pqlist) pmodels = pqarray[:, 0] qmodels = pqarray[:, 1] AICc = np.array(AIC_list) np.savetxt(aic_file, np.transpose([pmodels, qmodels, AICc]), header='p q AICc') pparam = model.p qparam = model.q return (pparam, qparam)
def testFull(self): path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../../cpp_tests/data/car5_test.dat") xv, yv, dyv = np.loadtxt(path, unpack=True) nSample = 100 nBurnin = 10 nThin = 1 pModel = 3 qModel = 1 carma1 = carmcmc.CarmaModel(xv, yv, dyv, p=1, q=0) post1 = carma1.run_mcmc(nSample, nburnin=nBurnin, nthin=nThin) carmap = carmcmc.CarmaModel(xv, yv, dyv, p=pModel, q=0) postp = carmap.run_mcmc(nSample, nburnin=nBurnin, nthin=nThin) carmapqo = carmcmc.CarmaModel(xv, yv, dyv, pModel, qModel) postpqo = carmapqo.run_mcmc(nSample, nburnin=nBurnin, nthin=nThin) carmapqe = carmcmc.CarmaModel(xv, yv, dyv, pModel + 1, qModel) postpqe = carmapqe.run_mcmc(nSample, nburnin=nBurnin, nthin=nThin) # cpp_tests of yamcmcpp samplers.py post1.effective_samples("sigma") postp.effective_samples("ar_roots") postpqo.effective_samples("ma_coefs") postpqe.effective_samples("ar_coefs") post1.plot_trace("sigma") postp.plot_trace("sigma") postpqo.plot_trace("sigma") postpqe.plot_trace("sigma") post1.plot_1dpdf("mu") postp.plot_1dpdf("mu") postpqo.plot_1dpdf("mu") postpqe.plot_1dpdf("mu") postp.plot_1dpdf("psd_centroid") postpqo.plot_1dpdf("psd_width") postpqe.plot_1dpdf("psd_width") post1.plot_2dpdf("sigma", "var") postp.plot_2dpdf("sigma", "var", pindex1=0, pindex2=0) postpqo.plot_2dpdf("sigma", "var", pindex1=1, pindex2=1) postpqe.plot_2dpdf("sigma", "var", pindex1=2, pindex2=2) post1.plot_2dkde("sigma", "var") postp.plot_2dkde("sigma", "var", pindex1=0, pindex2=0) postpqo.plot_2dkde("sigma", "var", pindex1=1, pindex2=1) postpqe.plot_2dkde("sigma", "var", pindex1=2, pindex2=2) postp.plot_autocorr("psd_centroid") postpqo.plot_autocorr("psd_centroid") postpqe.plot_autocorr("psd_centroid") postp.plot_parameter("psd_centroid") postpqo.plot_parameter("psd_centroid") postpqe.plot_parameter("psd_centroid") postp.posterior_summaries("psd_width") postpqo.posterior_summaries("psd_width") postpqe.posterior_summaries("psd_width") post1.posterior_summaries("sigma") postp.posterior_summaries("sigma") postpqo.posterior_summaries("sigma") postpqe.posterior_summaries("sigma") # cpp_tests of carma_pack carma_pack.py post1.plot_power_spectrum(percentile=95.0, doShow=False) postp.plot_power_spectrum(percentile=95.0, doShow=False) postpqo.plot_power_spectrum(percentile=95.0, doShow=False) postpqe.plot_power_spectrum(percentile=95.0, doShow=False) for bestfit in ["map", "median", "mean"]: post1.assess_fit(nplot=1000, bestfit=bestfit, doShow=False) postp.assess_fit(nplot=1000, bestfit=bestfit, doShow=False) postpqo.assess_fit(nplot=1000, bestfit=bestfit, doShow=False) postpqe.assess_fit(nplot=1000, bestfit=bestfit, doShow=False)
#export LD_LIBRARY_PATH=/home/felipe/astro/Software/boost/lib:${LD_LIBRARY_PATH} #export LD_LIBRARY_PATH=/home/felipe/astro/Software/armadillo/lib:${LD_LIBRARY_PATH} import os import numpy as np from scipy.optimize import minimize import scipy import carmcmc as cm os.chdir('/home/felipe/astro/IrregularAR/PackageIAR_Py') from FunctionsIATS import IARg_sample,IAR_phi_gamma,gentime,IAR_gamma np.random.seed(6713) sT=gentime(n=300) y,sT =IARg_sample(0.9,300,sT,1,1) phi,mu,sigma,ll=IAR_gamma(y,sT) ysig = np.zeros(300) carma_model = cm.CarmaModel(sT, y, ysig) pmax = 1 MLE, pqlist, AICc_list = carma_model.choose_order(pmax) carma_sample = carma_model.run_mcmc(50000) print carma_sample.parameters carma_sample.get_samples('log_omega')
def do_simulated_irregular_nonstationary(): # generate first half of lightcurve assuming a CARMA(5,3) process on a uniform grid sigmay1 = 2.3 # dispersion in lightcurve p = 5 # order of AR polynomial qpo_width1 = np.array([1.0/100.0, 1.0/300.0, 1.0/200.0]) qpo_cent1 = np.array([1.0/5.0, 1.0/25.0]) ar_roots1 = cm.get_ar_roots(qpo_width1, qpo_cent1) ma_coefs1 = np.zeros(p) ma_coefs1[0] = 1.0 ma_coefs1[1] = 4.5 ma_coefs1[2] = 1.25 sigsqr1 = sigmay1 ** 2 / cm.carma_variance(1.0, ar_roots1, ma_coefs=ma_coefs1) ny = 270 time = np.zeros(ny) dt = np.random.uniform(1.0, 3.0, ny) time[0:90] = np.cumsum(dt[0:90]) time[90:2*90] = 180 + time[90-1] + np.cumsum(dt[90:2*90]) time[2*90:] = 180 + time[2*90-1] + np.cumsum(dt[2*90:]) y = cm.carma_process(time, sigsqr1, ar_roots1, ma_coefs=ma_coefs1) # first generate some data assuming a CARMA(5,3) process on a uniform grid sigmay2 = 4.5 # dispersion in lightcurve p = 5 # order of AR polynomial qpo_width2 = np.array([1.0/100.0, 1.0/100.0, 1.0/500.0]) qpo_cent2 = np.array([1.0/5.0, 1.0/50.0]) ar_roots2 = cm.get_ar_roots(qpo_width2, qpo_cent2) ma_coefs2 = np.zeros(p) ma_coefs2[0] = 1.0 ma_coefs2[1] = 4.5 ma_coefs2[2] = 1.25 sigsqr2 = sigmay2 ** 2 / cm.carma_variance(1.0, ar_roots2, ma_coefs=ma_coefs2) ny = 270 time2 = np.zeros(ny) dt = np.random.uniform(1.0, 3.0, ny) time2[0:90] = np.cumsum(dt[0:90]) time2[90:2*90] = 180 + time2[90-1] + np.cumsum(dt[90:2*90]) time2[2*90:] = 180 + time2[2*90-1] + np.cumsum(dt[2*90:]) time = np.append(time, time.max() + 180 + time2) y2 = cm.carma_process(time2, sigsqr2, ar_roots2, ma_coefs=ma_coefs2) y = np.append(y, y2) ysig = np.ones(len(y)) * y.std() / 8.0 # ysig = np.ones(ny) * 1e-6 y0 = y.copy() y += ysig * np.random.standard_normal(len(y)) froot = base_dir + 'plots/car5_nonstationary_' plt.subplot(111) for i in xrange(6): plt.plot(time[90*i:90*(i+1)], y0[90*i:90*(i+1)], 'k', lw=2) plt.plot(time[90*i:90*(i+1)], y[90*i:90*(i+1)], 'bo') plt.xlim(time.min(), time.max()) plt.xlabel('Time') plt.ylabel('Non-Stationary Process') plt.savefig(froot + 'tseries.eps') plt.show() ar_coef1 = np.poly(ar_roots1) ar_coef2 = np.poly(ar_roots2) print 'Getting maximum-likelihood estimates...' carma_model = cm.CarmaModel(time, y, ysig) pmax = 7 MAP, pqlist, AIC_list = carma_model.choose_order(pmax, njobs=1) # convert lists to a numpy arrays, easier to manipulate pqarray = np.array(pqlist) pmodels = pqarray[:, 0] qmodels = pqarray[:, 1] AICc = np.array(AIC_list) plt.clf() plt.subplot(111) for i in xrange(qmodels.max()+1): plt.plot(pmodels[qmodels == i], AICc[qmodels == i], 's-', label='q=' + str(i), lw=2) plt.legend() plt.xlabel('p') plt.ylabel('AICc(p,q)') plt.xlim(0, pmodels.max() + 1) plt.savefig(froot + 'aic.eps') plt.close() nsamples = 50000 carma_sample = carma_model.run_mcmc(nsamples) carma_sample.add_mle(MAP) cPickle.dump(carma_sample, open(data_dir + 'nonstationary.pickle', 'wb')) plt.clf() ax = plt.subplot(111) print 'Getting bounds on PSD...' psd_low, psd_hi, psd_mid, frequencies = carma_sample.plot_power_spectrum(percentile=95.0, sp=ax, doShow=False, color='SkyBlue', nsamples=5000) psd_mle = cm.power_spectrum(frequencies, carma_sample.mle['sigma'], carma_sample.mle['ar_coefs'], ma_coefs=np.atleast_1d(carma_sample.mle['ma_coefs'])) psd1 = cm.power_spectrum(frequencies, np.sqrt(sigsqr1), ar_coef1, ma_coefs=ma_coefs1) psd2 = cm.power_spectrum(frequencies, np.sqrt(sigsqr2), ar_coef2, ma_coefs=ma_coefs2) # ax.loglog(frequencies, psd_mle, '--b', lw=2) ax.loglog(frequencies, psd1, 'k', lw=2) ax.loglog(frequencies, psd2, '--k', lw=2) dt = np.median(time[1:] - time[0:-1]) noise_level = 2.0 * dt * np.mean(ysig ** 2) ax.loglog(frequencies, np.ones(frequencies.size) * noise_level, color='grey', lw=2) ax.set_ylim(bottom=noise_level / 100.0) ax.annotate("Measurement Noise Level", (3.0 * ax.get_xlim()[0], noise_level / 2.5)) ax.set_xlabel('Frequency') ax.set_ylabel('Power Spectral Density') plt.savefig(froot + 'psd.eps') print 'Assessing the fit quality...' carma_sample.assess_fit(doShow=False) plt.savefig(froot + 'fit_quality.eps') # compute the marginal mean and variance of the predicted values nplot = 1028 time_predict = np.linspace(time.min(), 1.25 * time.max(), nplot) time_predict = time_predict[1:] predicted_mean, predicted_var = carma_sample.predict(time_predict, bestfit='map') predicted_low = predicted_mean - np.sqrt(predicted_var) predicted_high = predicted_mean + np.sqrt(predicted_var) # plot the time series and the marginal 1-sigma error bands plt.clf() plt.subplot(111) plt.fill_between(time_predict, predicted_low, predicted_high, color='cyan') plt.plot(time_predict, predicted_mean, '-b', label='Predicted') plt.plot(time[0:90], y0[0:90], 'k', lw=2, label='True') plt.plot(time[0:90], y[0:90], 'bo') for i in xrange(1, 3): plt.plot(time[90*i:90*(i+1)], y0[90*i:90*(i+1)], 'k', lw=2) plt.plot(time[90*i:90*(i+1)], y[90*i:90*(i+1)], 'bo') plt.xlabel('Time') plt.ylabel('CARMA(5,3) Process') plt.xlim(time_predict.min(), time_predict.max()) plt.legend() plt.savefig(froot + 'interp.eps')
def make_sampler_plots(time, y, ysig, pmax, file_root, title, do_mags=False, njobs=-1): froot = base_dir + 'plots/' + file_root # clean data dt = time[1:] - time[0:-1] if np.sum(dt <= 0) > 0: time = time[dt > 0] y = y[dt > 0] ysig = ysig[dt > 0] good = np.where(np.isfinite(time))[0] time = time[good] y = y[good] ysig = ysig[good] good = np.where(np.isfinite(y))[0] time = time[good] y = y[good] ysig = ysig[good] good = np.where(np.isfinite(ysig))[0] time = time[good] y = y[good] ysig = ysig[good] print 'Getting maximum-likelihood estimates...' carma_model = cm.CarmaModel(time, y, ysig) MAP, pqlist, AIC_list = carma_model.choose_order(pmax, njobs=njobs) # convert lists to a numpy arrays, easier to manipulate pqarray = np.array(pqlist) pmodels = pqarray[:, 0] qmodels = pqarray[:, 1] AICc = np.array(AIC_list) plt.clf() plt.subplot(111) for i in xrange(qmodels.max()+1): plt.plot(pmodels[qmodels == i], AICc[qmodels == i], 's-', label='q=' + str(i), lw=2) plt.legend(loc='best') plt.xlabel('p') plt.ylabel('AICc(p,q)') plt.xlim(0, pmodels.max() + 1) plt.title(title) plt.savefig(froot + 'aic.eps') plt.close() # make sure to change these back!!!! # carma_model.p = 7 # carma_model.q = 3 nsamples = 50000 carma_sample = carma_model.run_mcmc(nsamples) carma_sample.add_mle(MAP) ax = plt.subplot(111) print 'Getting bounds on PSD...' psd_low, psd_hi, psd_mid, frequencies = carma_sample.plot_power_spectrum(percentile=95.0, sp=ax, doShow=False, color='SkyBlue', nsamples=5000) psd_mle = cm.power_spectrum(frequencies, carma_sample.mle['sigma'], carma_sample.mle['ar_coefs'], ma_coefs=np.atleast_1d(carma_sample.mle['ma_coefs'])) ax.loglog(frequencies, psd_mle, '--b', lw=2) dt = time[1:] - time[0:-1] noise_level = 2.0 * np.median(dt) * np.mean(ysig ** 2) ax.loglog(frequencies, np.ones(frequencies.size) * noise_level, color='grey', lw=2) ax.set_ylim(bottom=noise_level / 100.0) do_s82 = True if do_s82: ax.annotate("Measurement Noise Level", (3.0e-2, 1e-2)) else: ax.annotate("Measurement Noise Level", (3.0 * ax.get_xlim()[0], noise_level / 2.5)) ax.set_xlabel('Frequency [1 / day]') if do_mags: ax.set_ylabel('Power Spectral Density [mag$^2$ day]') else: ax.set_ylabel('Power Spectral Density [flux$^2$ day]') plt.title(title) plt.savefig(froot + 'psd.eps') print 'Assessing the fit quality...' fig = carma_sample.assess_fit(doShow=False) ax_again = fig.add_subplot(2, 2, 1) ax_again.set_title(title) if do_mags: ylims = ax_again.get_ylim() ax_again.set_ylim(ylims[1], ylims[0]) ax_again.set_ylabel('magnitude') else: ax_again.set_ylabel('ln Flux') plt.savefig(froot + 'fit_quality.eps') return carma_sample
def do_simulated_regular(): # first generate some data assuming a CARMA(5,3) process on a uniform grid sigmay = 2.3 # dispersion in lightcurve p = 5 # order of AR polynomial qpo_width = np.array([1.0/100.0, 1.0/100.0, 1.0/500.0]) qpo_cent = np.array([1.0/5.0, 1.0/50.0]) ar_roots = cm.get_ar_roots(qpo_width, qpo_cent) ma_coefs = np.zeros(p) ma_coefs[0] = 1.0 ma_coefs[1] = 4.5 ma_coefs[2] = 1.25 sigsqr = sigmay ** 2 / cm.carma_variance(1.0, ar_roots, ma_coefs=ma_coefs) ny = 1028 time = np.arange(0.0, ny) y0 = cm.carma_process(time, sigsqr, ar_roots, ma_coefs=ma_coefs) ysig = np.ones(ny) * np.sqrt(1e-2) # ysig = np.ones(ny) * np.sqrt(1e-6) y = y0 + ysig * np.random.standard_normal(ny) froot = base_dir + 'plots/car5_regular_' plt.subplot(111) plt.plot(time, y0, 'k-') plt.plot(time, y, '.') plt.xlim(time.min(), time.max()) plt.xlabel('Time') plt.ylabel('CARMA(5,3) Process') plt.savefig(froot + 'tseries.eps') ar_coef = np.poly(ar_roots) print 'Getting maximum-likelihood estimates...' carma_model = cm.CarmaModel(time, y, ysig) pmax = 7 MAP, pqlist, AIC_list = carma_model.choose_order(pmax, njobs=-1) # convert lists to a numpy arrays, easier to manipulate pqarray = np.array(pqlist) pmodels = pqarray[:, 0] qmodels = pqarray[:, 1] AICc = np.array(AIC_list) plt.clf() plt.subplot(111) for i in xrange(qmodels.max()+1): plt.plot(pmodels[qmodels == i], AICc[qmodels == i], 's-', label='q=' + str(i), lw=2) plt.legend() plt.xlabel('p') plt.ylabel('AICc(p,q)') plt.xlim(0, pmodels.max() + 1) plt.savefig(froot + 'aic.eps') plt.close() nsamples = 50000 carma_sample = carma_model.run_mcmc(nsamples) carma_sample.add_mle(MAP) plt.subplot(111) pgram, freq = plt.psd(y) plt.clf() ax = plt.subplot(111) print 'Getting bounds on PSD...' psd_low, psd_hi, psd_mid, frequencies = carma_sample.plot_power_spectrum(percentile=95.0, sp=ax, doShow=False, color='SkyBlue', nsamples=5000) psd_mle = cm.power_spectrum(frequencies, carma_sample.mle['sigma'], carma_sample.mle['ar_coefs'], ma_coefs=np.atleast_1d(carma_sample.mle['ma_coefs'])) ax.loglog(freq / 2.0, pgram, 'o', color='DarkOrange') psd = cm.power_spectrum(frequencies, np.sqrt(sigsqr), ar_coef, ma_coefs=ma_coefs) ax.loglog(frequencies, psd, 'k', lw=2) ax.loglog(frequencies, psd_mle, '--b', lw=2) noise_level = 2.0 * np.mean(ysig ** 2) ax.loglog(frequencies, np.ones(frequencies.size) * noise_level, color='grey', lw=2) ax.set_ylim(bottom=noise_level / 100.0) ax.annotate("Measurement Noise Level", (3.0 * ax.get_xlim()[0], noise_level / 2.5)) ax.set_xlabel('Frequency') ax.set_ylabel('Power Spectral Density') plt.savefig(froot + 'psd.eps') print 'Assessing the fit quality...' carma_sample.assess_fit(doShow=False) plt.savefig(froot + 'fit_quality.eps') pfile = open(data_dir + froot + '.pickle', 'wb') cPickle.dump(carma_sample, pfile) pfile.close()
for dimNum in xrange(P + Q + 1): ntg.Chain[dimNum, walkerNum, stepNum] = float(words[dimNum]) ntg.LnPosterior[walkerNum, stepNum] = float(words[P + Q + 1]) chainFile.close() fileName = args.name.split('.')[0] + '_' + args.cC + '_g.dat' cmcmcChainFilePath = os.path.join(args.pwd, fileName) try: chainFile = open(cmcmcChainFilePath, 'r') except IOError: NSAMPLES = NWALKERS * NSTEPS / 2 NBURNIN = NWALKERS * NSTEPS / 2 if carma_pack: carma_model_g = cmcmc.CarmaModel(mock_sdss0g.t, mock_sdss0g.y, mock_sdss0g.yerr, p=P, q=Q) print "Starting cmcmc fitting..." startCMCMC = time.time() carma_sample_g = carma_model_g.run_mcmc(NSAMPLES, nburnin=NBURNIN) stopCMCMC = time.time() timeCMCMC = stopCMCMC - startCMCMC print "CMCMC took %4.3f s = %4.3f min = %4.3f hrs" % ( timeCMCMC, timeCMCMC / 60.0, timeCMCMC / 3600.0) ar_poly_g = carma_sample_g.get_samples('ar_coefs') ma_poly_g = carma_sample_g.get_samples('ma_coefs') sigma_g = carma_sample_g.get_samples('sigma') logpost_g = carma_sample_g.get_samples('logpost') cmcmcChain_g = np.zeros((P + Q + 1, NSAMPLES)) cmcmcLnPosterior_g = np.zeros(NSAMPLES)
parser.add_argument('--ntemp', default=10, type=int, metavar='N', help='number of temperatures') args = parser.parse_args() data = np.loadtxt(args.input) times, tind = np.unique(data[:, 0], return_index=True) data = data[tind, :] model = cm.CarmaModel(data[:, 0], data[:, 1], data[:, 2], p=args.p, q=args.q) thin = 1 nsamp = 10 * args.neff out, ext = os.path.splitext(args.output) outtemp = out + '.TEMP' + ext while True: sample = model.run_mcmc(nsamp, nthin=thin, nburnin=thin * nsamp / 2, tmax=args.tmax, ntemperatures=args.ntemp)
def run_CARMA(time, y, ysig, maxp, nsamples, aic_file, carma_sample_file, psd_file, psd_plot, fit_quality_plot, pl_plot, do_mags=True): #to calculate the order p and q of the CARMA(p,q) process, then run CARMA for values of p and q already calculated #function to calculate the order p and q of the CARMA(p,q) process # maxp: Maximum value allowed for p, maximun value for q is by default p-1 time = time - time[0] model = cm.CarmaModel(time, y, ysig) MAP, pqlist, AIC_list = model.choose_order(maxp, njobs=1) # convert lists to a numpy arrays, easier to manipulate #the results of the AIC test are stored for future references. pqarray = np.array(pqlist) pmodels = pqarray[:, 0] qmodels = pqarray[:, 1] AICc = np.array(AIC_list) np.savetxt(aic_file, np.transpose([pmodels, qmodels, AICc]), header='p q AICc') p = model.p q = model.q #running the sampler carma_model = cm.CarmaModel(time, y, ysig, p=p, q=q) carma_sample = carma_model.run_mcmc(nsamples) carma_sample.add_mle(MAP) #getting the PSD ax = plt.subplot(111) print 'Getting bounds on PSD...' psd_low, psd_hi, psd_mid, frequencies = carma_sample.plot_power_spectrum( percentile=95.0, sp=ax, doShow=False, color='SkyBlue', nsamples=5000) psd_mle = cm.power_spectrum(frequencies, carma_sample.mle['sigma'], carma_sample.mle['ar_coefs'], ma_coefs=np.atleast_1d( carma_sample.mle['ma_coefs'])) #saving the psd np.savetxt(psd_file, np.transpose([frequencies, psd_low, psd_hi, psd_mid, psd_mle]), header='frequencies psd_low psd_hi psd_mid psd_mle') ax.loglog(frequencies, psd_mle, '--b', lw=2) dt = time[1:] - time[0:-1] noise_level = 2.0 * np.median(dt) * np.mean(ysig**2) mean_noise_level = noise_level median_noise_level = 2.0 * np.median(dt) * np.median(ysig**2) ax.loglog(frequencies, np.ones(frequencies.size) * noise_level, color='grey', lw=2) ax.loglog(frequencies, np.ones(frequencies.size) * median_noise_level, color='green', lw=2) ax.set_ylim(bottom=noise_level / 100.0) ax.annotate("Measurement Noise Level", (3.0 * ax.get_xlim()[0], noise_level / 2.5)) ax.set_xlabel('Frequency [1 / day]') if do_mags: ax.set_ylabel('Power Spectral Density [mag$^2$ day]') else: ax.set_ylabel('Power Spectral Density [flux$^2$ day]') #plt.title(title) plt.savefig(psd_plot) plt.close('all') print 'Assessing the fit quality...' fig = carma_sample.assess_fit(doShow=False) ax_again = fig.add_subplot(2, 2, 1) #ax_again.set_title(title) if do_mags: ylims = ax_again.get_ylim() ax_again.set_ylim(ylims[1], ylims[0]) ax_again.set_ylabel('magnitude') else: ax_again.set_ylabel('ln Flux') plt.savefig(fit_quality_plot) pfile = open(carma_sample_file, 'wb') cPickle.dump(carma_sample, pfile) pfile.close() params = { param: carma_sample.get_samples(param) for param in carma_sample.parameters } params['p'] = model.p params['q'] = model.q print "fitting bending power-law" nf = np.where(psd_mid >= median_noise_level) psdfreq = frequencies[nf] psd_low = psd_low[nf] psd_hi = psd_hi[nf] psd_mid = psd_mid[nf] A, v_bend, a_low, a_high, blpfit = fit_BendingPL(psdfreq, psd_mid) pl_init = models.BrokenPowerLaw1D(amplitude=2, x_break=0.002, alpha_1=1, alpha_2=2) fit = LevMarLSQFitter() pl = fit(pl_init, psdfreq, psd_mid) amplitude = pl.amplitude.value x_break = pl.x_break.value alpha_1 = pl.alpha_1.value alpha_2 = pl.alpha_2.value print amplitude, x_break, alpha_1, alpha_2 print "BendingPL fit parameters = ", A, v_bend, a_low, a_high print "BrokenPL fit parameters = ", amplitude, x_break, alpha_1, alpha_2 plt.clf() plt.subplot(111) plt.loglog(psdfreq, psd_mid, color='green') plt.fill_between(psdfreq, psd_low, psd_hi, facecolor='green', alpha=0.3) plt.plot(psdfreq, blpfit, 'r--', lw=2) plt.plot(psdfreq, pl(psdfreq), 'k--', lw=2) plt.savefig(pl_plot) plt.close('all') return (params, mean_noise_level, median_noise_level, A, v_bend, a_low, a_high, amplitude, x_break, alpha_1, alpha_2)
words = line.rstrip('\n').split() for dimNum in xrange(P + Q + 1): ntg.Chain[dimNum, walkerNum, stepNum] = float(words[dimNum]) ntg.LnPosterior[walkerNum, stepNum] = float(words[P + Q + 1]) chainFile.close() fileName = args.name.split('.')[0] + '_' + args.cC + '_g.dat' cmcmcChain_g = os.path.join(args.pwd, fileName) try: chainFile = open(cmcmcChain_g, 'r') except IOError: NSAMPLES = NWALKERS*NSTEPS/2 NBURNIN = NWALKERS*NSTEPS/2 if carma_pack: chainFile = open(cmcmcChain_g, 'w') carma_model_g = cmcmc.CarmaModel(sdss0g.t - sdss0g.startT, sdss0g.y, sdss0g.yerr, p=P, q=Q) carma_sample_g = carma_model_g.run_mcmc(NSAMPLES, nburnin=NBURNIN) ar_poly_g = carma_sample_g.get_samples('ar_coefs') ma_poly_g = carma_sample_g.get_samples('ma_coefs') sigma_g = carma_sample_g.get_samples('sigma') logpost_g = carma_sample_g.get_samples('logpost') cmcmcChain_g = np.zeros((P + Q + 1, NSAMPLES)) cmcmcLnPosterior_g = np.zeros(NSAMPLES) line = '%d %d %d\n'%(P, Q, NSAMPLES) chainFile.write(line) for sampleNum in xrange(NSAMPLES): for j in xrange(P): cmcmcChain_g[j, sampleNum] = ar_poly_g[sampleNum, j + 1] for j in xrange(Q + 1): cmcmcChain_g[j + P, sampleNum] = ma_poly_g[sampleNum, j]*sigma_g[sampleNum, 0] cmcmcLnPosterior_g[sampleNum] = logpost_g[sampleNum, 0]