Пример #1
0
    def power_spectrum(self, fs, p):
        p = self.to_params(p)

        ar_roots = self.ar_roots(p)
        ma_roots = self.ma_roots(p)

        ar_coefs = np.real(np.poly(ar_roots))

        ma_coefs = np.real(np.poly(ma_roots))
        ma_coefs /= ma_coefs[-1]
        ma_coefs = ma_coefs[::-1]

        s = par.bounded_values(p['logit_sigma'],
                               low=self.sigma_min,
                               high=self.sigma_max)

        sigma = s / np.sqrt(cm.carma_variance(1.0, ar_roots, ma_coefs))

        return cm.power_spectrum(fs, sigma, ar_coefs, ma_coefs)
Пример #2
0
    def _make_kalman_filter(self, p):
        p = self.to_params(p)

        ar_roots = self.ar_roots(p)
        ma_coefs = self.ma_poly(p)

        mu = par.bounded_values(p['logit_mu'],
                                low=self.mu_min,
                                high=self.mu_max)
        nu = par.bounded_values(p['logit_nu'],
                                low=self.nu_min,
                                high=self.nu_max)

        s = par.bounded_values(p['logit_sigma'],
                               low=self.sigma_min,
                               high=self.sigma_max)

        sigma = s / np.sqrt(cm.carma_variance(1.0, ar_roots, ma_coefs))
        sigmasq = sigma * sigma
        sigmasq = float(sigmasq)

        tv = cm.vecD()
        tv.extend(self.t)
        yv = cm.vecD()
        yv.extend(self.y - mu)
        dyv = cm.vecD()
        dyv.extend(self.dy * nu)
        arv = cm.vecC()
        arv.extend(ar_roots)
        mav = cm.vecD()
        mav.extend(ma_coefs)
        kfilter = cm.KalmanFilterp(tv, yv, dyv, sigmasq, arv, mav)

        kfilter.Filter()

        return kfilter
Пример #3
0
	def writeLC(self, Mask = None, Cadences = None):
		"""	Create a C-ARMA light curve with C-ARMA configuration supplied in the ConfigFile. 
		"""
		self.t = None
		self.y = None
		self.yerr = None
		self.Mask = None
		self.Cadences = None
		if not self.DateTime:
			##sigmay = 1.2e-9
			SigmaY = self.MAPoly[0]
			##ma_coefs = np.array([1.0, 5.834])
			MACoefs = np.array([Poly/SigmaY for Poly in self.MAPoly])

			SigSqr = SigmaY**2/cmcmc.carma_variance(1.0, self.ARRoots, ma_coefs = MACoefs)

			self.t = np.arange(0.0, self.T, self.dt)
			self.y = cmcmc.carma_process(self.t, SigSqr, self.ARRoots, ma_coefs = MACoefs)
			self.y += np.array(self.t.shape[0]*[self.baseFlux])
			if not Cadences:
				self.Cadences = np.arange(self.t.shape[0])
			else:
				self.Cadences = Cadences
			noise = np.random.normal(loc = 0.0, scale = self.noiseLvl, size = self.Cadences.shape[0])
			self.y += noise
			self.yerr = np.array(self.Cadences.shape[0]*[self.noiseLvl])

			numMasked = 0
			if Mask:
				self.Mask = np.array(Mask)
				for i in xrange(self.Mask.shape[0]):
					if self.Mask[i] == 0.0:
						self.t[i] = 0.0
						self.y[i] = 0.0
						self.yerr[i] = 1.3407807929942596e+154
						numMasked += 1
			else:
				self.Mask = np.array(self.Cadences.shape[0]*[1.0])

			self.LCFile = self.WorkingDirectory + self.prefix + "_LC.dat"
			outFile = open(self.LCFile, 'w')
			line = "ConfigFileHash: %s\n"%(self.ConfigFileHash)
			outFile.write(line)
			line = "numCadences: %d\n"%(self.t.shape[0])
			outFile.write(line)
			line = "numObservations: %d\n"%(self.t.shape[0] - numMasked)
			outFile.write(line)
			line = "meanFlux: %+17.16e\n"%(np.mean(self.y))
			outFile.write(line)
			line = "CadenceNum Mask t y yerr\n"
			outFile.write(line) 
			for i in xrange(self.Cadences.shape[0]-1):
				line = "%d %1.0f %+17.16e %+17.16e %+17.16e\n"%(self.Cadences[i], self.Mask[i], self.t[i], self.y[i], self.yerr[i])
				outFile.write(line)
			line = "%d %1.0f %+17.16e %+17.16e %+17.16e"%(self.Cadences[self.Cadences.shape[0]-1], self.Mask[self.Cadences.shape[0]-1], self.t[self.Cadences.shape[0]-1], self.y[self.Cadences.shape[0]-1], self.yerr[self.Cadences.shape[0]-1])
			outFile.write(line)
			outFile.close()

		else:
			self.LCFile = self.WorkingDirectory + self.prefix + "_LC.dat"
			inFile = open(self.LCFile, 'rb')
			words = inFile.readline().rstrip('\n').split()
			LCHash = words[0]
			if (LCHash == self.ConfigFileHash):
				line = inFile.readline()
				line = line.rstrip('\n')
				words = line.split()
				numCadences = int(words[1])
				self.t = np.array(numCadences*[0.0])
				self.y = np.array(numCadences*[0.0])
				self.yerr = np.array(numCadences*[0.0])
				self.Cadences = np.array(numCadences*[0.0])
				self.Mask = np.array(numCadences*[0.0])
				line = inFile.readline()
				line = inFile.readline()
				for i in xrange(numCadences):
					words = inFile.readline().rstrip('\n').split()
					self.t[i] = i*self.dt
					self.Cadences[i] = int(words[0])
					self.Mask[i] = float(words[1])
					self.y[i] = float(words[2])
					self.yerr[i] = float(words[3])
			else:
				print "Hash mismatch! The ConfigFile %s in WorkingDirectory %s has changed and no longer matches that used to make the light curve. Exiting!"%(self.ConfigFile, self.WorkingDirectory)

		if self.plotLC == True:
			fig1 = plt.figure(1, figsize=(fwid, fhgt))
			ax1 = fig1.add_subplot(gs[:,:])
			ax1.ticklabel_format(useOffset = False)
			ax1.plot(self.t, self.y)
			ax1.set_xlabel(r'$t$ (d)')
			ax1.set_ylabel(r'Flux')
			if self.JPG == True:
				fig1.savefig(self.WorkingDirectory + self.prefix + "_LC.jpg" , dpi = dpi)
			if self.PDF == True:
				fig1.savefig(self.WorkingDirectory + self.prefix + "_LC.pdf" , dpi = dpi)
			if self.EPS == True:
				fig1.savefig(self.WorkingDirectory + self.prefix + "_LC.eps" , dpi = dpi)
			if self.showFig == True:
				plt.show()
		return 0
Пример #4
0
def do_simulated_regular():

    # first generate some data assuming a CAR(5) 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-3 / 2.0)

    y = y0 + ysig * np.random.standard_normal(ny)

    data = (time, y, ysig)

    froot = base_dir + '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('CAR(5) Process')
    plt.savefig(froot + 'tseries.eps')

    ar_coef = np.poly(ar_roots)

    pool = mp.Pool(mp.cpu_count() - 1)

    args = []
    maxp = 8
    for p in xrange(1, maxp + 1):
        for q in xrange(p):
            args.append(((p, q), data))

    print "Running the CARMA MCMC samplers..."

    carma_run = pool.map(run_carma_sampler, args)

    dic = []
    pmodels = []
    qmodels = []
    for crun in carma_run:
        dic.append(crun.DIC())
        pmodels.append(crun.p)
        qmodels.append(crun.q)

    pmodels = np.array(pmodels)
    qmodels = np.array(qmodels)
    dic = np.array(dic)

    plt.clf()
    plt.subplot(111)
    for i in xrange(qmodels.max() + 1):
        plt.plot(pmodels[qmodels == i],
                 dic[qmodels == i],
                 label='q=' + str(i),
                 lw=2)

    plt.legend()
    plt.xlabel('p')
    plt.ylabel('DIC')
    print "DIC", dic
    plt.savefig(froot + 'dic.eps')

    carma = carma_run[np.argmin(dic)]

    print "order of best model is", carma.p

    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.plot_power_spectrum(
        percentile=95.0, sp=ax, doShow=False, color='SkyBlue')
    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_mid, '--b', lw=2)
    noise_level = 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.assess_fit(doShow=False)
    plt.savefig(froot + 'fit_quality.eps')
Пример #5
0
def do_simulated_irregular():

    # first generate some data assuming a CAR(5) process on a uniform grid
    sigmay = 2.3  # dispersion in lightcurve
    p = 5  # order of AR polynomial
    mu = 17.0  # mean of time series
    qpo_width = np.array([1.0 / 100.0, 1.0 / 300.0, 1.0 / 200.0])
    qpo_cent = np.array([1.0 / 5.0, 1.0 / 25.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 = 270
    time = np.empty(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 = mu + cm.carma_process(time, sigsqr, ar_roots, ma_coefs=ma_coefs)

    ysig = np.ones(ny) * y.std() / 5.0
    ysig = np.ones(ny) * 1e-6
    y0 = y.copy()
    y += ysig * np.random.standard_normal(ny)

    data = (time, y, ysig)

    #    car5_model = cm.CarmaMCMC(time, y, ysig, 5, 5000, doZcarma=True, nburnin=1000)
    #    zcar = car5_model.RunMCMC()

    froot = base_dir + 'car5_irregular_'

    plt.subplot(111)
    for i in xrange(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.xlim(time.min(), time.max())
    plt.xlabel('Time')
    plt.ylabel('CAR(5) Process')
    plt.savefig(froot + 'tseries.eps')

    ar_coef = np.poly(ar_roots)

    pool = mp.Pool(mp.cpu_count() - 1)

    args = []
    maxp = 9
    for p in xrange(1, maxp + 1):
        for q in xrange(p):
            args.append((p, q, data))

    print "Running the CARMA MCMC samplers..."

    carma_run = pool.map(run_carma_sampler, args)

    dic = []
    pmodels = []
    qmodels = []
    for crun in carma_run:
        dic.append(crun.DIC())
        pmodels.append(crun.p)
        qmodels.append(crun.q)

    plt.clf()
    plt.subplot(111)
    for i in xrange(qmodels.max() + 1):
        plt.plot(pmodels[qmodels == i],
                 dic[qmodels == i],
                 label='q=' + str(i),
                 lw=2)

    plt.legend()
    plt.xlabel('p')
    plt.ylabel('DIC')
    print "DIC", dic
    plt.savefig(froot + 'dic.eps')

    carma = carma_run[np.argmin(dic)]

    print "order of best model is", carma.p

    plt.clf()
    ax = plt.subplot(111)
    print 'Getting bounds on PSD...'
    psd_low, psd_hi, psd_mid, frequencies = carma.plot_power_spectrum(
        percentile=95.0, sp=ax, doShow=False, color='SkyBlue')
    psd = cm.power_spectrum(frequencies,
                            np.sqrt(sigsqr),
                            ar_coef,
                            ma_coefs=ma_coefs)
    ax.loglog(frequencies, psd_mid, '--b', lw=2)
    ax.loglog(frequencies, psd, 'k', lw=2)
    noise_level = np.mean(ysig**2) * dt.min()
    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.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.predict_lightcurve(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('CAR(5) Process')
    plt.xlim(time_predict.min(), time_predict.max())
    plt.legend()
    plt.savefig(froot + 'interp.eps')
Пример #6
0
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')
Пример #7
0
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()
Пример #8
0
import numpy as np
import matplotlib.pyplot as plt
from os import environ
from scipy.misc import comb
import carmcmc

# true values
p = 5  # order of AR polynomial
sigmay = 2.3
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 = carmcmc.get_ar_roots(qpo_width, qpo_cent)
# calculate moving average coefficients under z-transform of Belcher et al. (1994)
kappa = 3.0
ma_coefs = comb(p-1 * np.ones(p), np.arange(p)) / kappa ** np.arange(p)
sigsqr = sigmay ** 2 / carmcmc.carma_variance(1.0, ar_roots, ma_coefs=ma_coefs)

data_dir = environ['HOME'] + '/Projects/carma_pack/cpp_tests/data/'
fname = data_dir + 'zcarma5_mcmc.dat'
data = np.genfromtxt(data_dir + 'zcarma5_test.dat')

Zcarma = carmcmc.ZCarmaSample(data[:, 0], data[:, 1], data[:, 2], filename=fname)

Zcarma.assess_fit()

print "True value of log10(kappa) is: ", np.log10(kappa)
plt.hist(Zcarma.get_samples('kappa'), bins=100)
plt.show()

Zcarma.plot_parameter('kappa', doShow=True)
Пример #9
0
def do_simulated_regular():

    # first generate some data assuming a CAR(5) 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-3 / 2.0)

    y = y0 + ysig * np.random.standard_normal(ny)

    data = (time, y, ysig)

    froot = base_dir + '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('CAR(5) Process')
    plt.savefig(froot + 'tseries.eps')

    ar_coef = np.poly(ar_roots)

    pool = mp.Pool(mp.cpu_count()-1)

    args = []
    maxp = 8
    for p in xrange(1, maxp + 1):
        for q in xrange(p):
            args.append(((p, q), data))

    print "Running the CARMA MCMC samplers..."

    carma_run = pool.map(run_carma_sampler, args)

    dic = []
    pmodels = []
    qmodels = []
    for crun in carma_run:
        dic.append(crun.DIC())
        pmodels.append(crun.p)
        qmodels.append(crun.q)

    pmodels = np.array(pmodels)
    qmodels = np.array(qmodels)
    dic = np.array(dic)

    plt.clf()
    plt.subplot(111)
    for i in xrange(qmodels.max()+1):
        plt.plot(pmodels[qmodels == i], dic[qmodels == i], label='q=' + str(i), lw=2)

    plt.legend()
    plt.xlabel('p')
    plt.ylabel('DIC')
    print "DIC", dic
    plt.savefig(froot + 'dic.eps')

    carma = carma_run[np.argmin(dic)]

    print "order of best model is", carma.p

    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.plot_power_spectrum(percentile=95.0, sp=ax, doShow=False,
                                                                      color='SkyBlue')
    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_mid, '--b', lw=2)
    noise_level = 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.assess_fit(doShow=False)
    plt.savefig(froot + 'fit_quality.eps')
Пример #10
0
def do_simulated_irregular():

    # first generate some data assuming a CAR(5) process on a uniform grid
    sigmay = 2.3  # dispersion in lightcurve
    p = 5  # order of AR polynomial
    mu = 17.0  # mean of time series
    qpo_width = np.array([1.0/100.0, 1.0/300.0, 1.0/200.0])
    qpo_cent = np.array([1.0/5.0, 1.0/25.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 = 270
    time = np.empty(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 = mu + cm.carma_process(time, sigsqr, ar_roots, ma_coefs=ma_coefs)

    ysig = np.ones(ny) * y.std() / 5.0
    ysig = np.ones(ny) * 1e-6
    y0 = y.copy()
    y += ysig * np.random.standard_normal(ny)

    data = (time, y, ysig)

#    car5_model = cm.CarmaMCMC(time, y, ysig, 5, 5000, doZcarma=True, nburnin=1000)
#    zcar = car5_model.RunMCMC()

    froot = base_dir + 'car5_irregular_'

    plt.subplot(111)
    for i in xrange(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.xlim(time.min(), time.max())
    plt.xlabel('Time')
    plt.ylabel('CAR(5) Process')
    plt.savefig(froot + 'tseries.eps')

    ar_coef = np.poly(ar_roots)

    pool = mp.Pool(mp.cpu_count()-1)

    args = []
    maxp = 9
    for p in xrange(1, maxp + 1):
        for q in xrange(p):
            args.append((p, q, data))

    print "Running the CARMA MCMC samplers..."

    carma_run = pool.map(run_carma_sampler, args)

    dic = []
    pmodels = []
    qmodels = []
    for crun in carma_run:
        dic.append(crun.DIC())
        pmodels.append(crun.p)
        qmodels.append(crun.q)

    plt.clf()
    plt.subplot(111)
    for i in xrange(qmodels.max()+1):
        plt.plot(pmodels[qmodels == i], dic[qmodels == i], label='q=' + str(i), lw=2)

    plt.legend()
    plt.xlabel('p')
    plt.ylabel('DIC')
    print "DIC", dic
    plt.savefig(froot + 'dic.eps')

    carma = carma_run[np.argmin(dic)]

    print "order of best model is", carma.p

    plt.clf()
    ax = plt.subplot(111)
    print 'Getting bounds on PSD...'
    psd_low, psd_hi, psd_mid, frequencies = carma.plot_power_spectrum(percentile=95.0, sp=ax, doShow=False,
                                                                      color='SkyBlue')
    psd = cm.power_spectrum(frequencies, np.sqrt(sigsqr), ar_coef, ma_coefs=ma_coefs)
    ax.loglog(frequencies, psd_mid, '--b', lw=2)
    ax.loglog(frequencies, psd, 'k', lw=2)
    noise_level = np.mean(ysig ** 2) * dt.min()
    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.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.predict_lightcurve(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('CAR(5) Process')
    plt.xlim(time_predict.min(), time_predict.max())
    plt.legend()
    plt.savefig(froot + 'interp.eps')
import numpy as np
import matplotlib.pyplot as plt
from os import environ
from scipy.misc import comb
import carmcmc

# true values
p = 5  # order of AR polynomial
sigmay = 2.3
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 = carmcmc.get_ar_roots(qpo_width, qpo_cent)
# calculate moving average coefficients under z-transform of Belcher et al. (1994)
kappa = 3.0
ma_coefs = comb(p - 1 * np.ones(p), np.arange(p)) / kappa**np.arange(p)
sigsqr = sigmay**2 / carmcmc.carma_variance(1.0, ar_roots, ma_coefs=ma_coefs)

data_dir = environ['HOME'] + '/Projects/carma_pack/cpp_tests/data/'
fname = data_dir + 'zcarma5_mcmc.dat'
data = np.genfromtxt(data_dir + 'zcarma5_test.dat')

Zcarma = carmcmc.ZCarmaSample(data[:, 0],
                              data[:, 1],
                              data[:, 2],
                              filename=fname)

Zcarma.assess_fit()

print "True value of log10(kappa) is: ", np.log10(kappa)
plt.hist(Zcarma.get_samples('kappa'), bins=100)
plt.show()