def testKalmanp(self): pModel = 4 qModel = 0 sampler = carmcmc.run_mcmc_carma(self.nSample, self.nBurnin, self.xdata, self.ydata, self.dydata, pModel, qModel, self.nWalkers, False, self.nThin) psampler = carmcmc.CarmaSample(np.array(self.xdata), np.array(self.ydata), np.array(self.dydata), sampler) sigsqr = (psampler._samples["sigma"][0]**2)[0] ma_coefs = carmcmc.vecD() ma_coefs0 = psampler._samples["ma_coefs"][0] if len(ma_coefs0) != pModel: ma_coefs0 = np.append(ma_coefs0, np.zeros(pModel - qModel - 1)) ma_coefs.extend(ma_coefs0) omega = carmcmc.vecC() for i in range(psampler.p): omega.append(psampler._samples["ar_roots"][0][i]) #import pdb; pdb.set_trace() kfilter = carmcmc.KalmanFilterp(self.xdata, self.ydata, self.dydata, sigsqr, omega, ma_coefs) kfilter.Filter() pred0 = kfilter.Predict(self.xdata[0]) # evaluate at data point val0 = pred0.first var0 = pred0.second predN = kfilter.Predict(self.xdata[-1] + 1) # extrapolate valN = predN.first varN = predN.second self.assertTrue(varN > var0)
def doit(args): pModel = int(args[0]) x, y, dy = args[1] nSample = 10000 nBurnin = 1000 nThin = 1 nWalkers = 10 # Should not have to do this... xv = carmcmc.vecD() xv.extend(x) yv = carmcmc.vecD() yv.extend(y) dyv = carmcmc.vecD() dyv.extend(dy) if pModel == 1: sampler = carmcmc.run_mcmc_car1(nSample, nBurnin, xv, yv, dyv, nWalkers, nThin) samplep = carmcmc.CarSample1(x, y, dy, sampler) else: sampler = carmcmc.run_mcmc_carma(nSample, nBurnin, xv, yv, dyv, pModel, 0, nWalkers, False, nThin) samplep = carmcmc.CarmaSample(x, y, dy, sampler) dic = samplep.DIC() print "DIC", pModel, dic return samplep
def csample_from_files(datafile, chainfile, p, q): data = np.loadtxt(datafile) times, tind = np.unique(data[:, 0], return_index=True) data = data[tind, :] chain = np.loadtxt(chainfile) assert chain.shape[1] == p + q + 5, 'dimension mismatch' return cm.CarmaSample(data[:, 0], data[:, 1], data[:, 2], None, q=q, trace=chain[:, :-2], loglike=chain[:, -2], logpost=chain[:, -1])
def testCarpq(self, pModel=3, qModel=2): sampler = carmcmc.run_mcmc_carma(self.nSample, self.nBurnin, self.xdata, self.ydata, self.dydata, pModel, qModel, self.nWalkers, False, self.nThin) psampler = carmcmc.CarmaSample(np.array(self.xdata), np.array(self.ydata), np.array(self.dydata), sampler) self.assertEqual(psampler.p, pModel + qModel) psamples = np.array(sampler.getSamples()) ploglikes = np.array(sampler.GetLogLikes()) sample0 = carmcmc.vecD() sample0.extend(psamples[0]) logprior0 = sampler.getLogPrior(sample0) loglike0 = sampler.getLogDensity(sample0) # OK, this is where I truly test that sampler is of class CARp and not CAR1 self.assertAlmostEqual(ploglikes[0], loglike0)
doShow=False) ar_coef = np.poly(ar_roots) true_psd = carmcmc.power_spectrum(freq, np.sqrt(sigsqr), ar_coef, ma_coefs=ma_coefs) plt.loglog(freq, true_psd, 'r', lw=2) plt.xlabel('Frequency') plt.ylabel('PSD, ZCARMA(5)') plt.show() fname = data_dir + 'carma_mcmc.dat' Carma = carmcmc.CarmaSample(data[:, 0], data[:, 1], data[:, 2], filename=fname, q=p - 1) Carma.assess_fit() print "True values of MA coefs are:", ma_coefs Carma.plot_1dpdf('ma_coefs', doShow=True) Carma.posterior_summaries('ma_coefs') print '' print "True values of log_widths are", np.log(qpo_width) Carma.posterior_summaries('log_width') print '' print "True values of log_centroids are", np.log(qpo_cent) Carma.posterior_summaries('log_centroid') psd_low, psd_high, psd_mid, freq = Carma.plot_power_spectrum(percentile=95.0,