Note

This tutorial was generated from an IPython notebook that can be downloaded here.

Fitting TESS data

theano version: 1.0.4
pymc3 version: 3.7
exoplanet version: 0.2.1

In this tutorial, we will reproduce the fits to the transiting planet in the Pi Mensae system discovered by Huang et al. (2018). The data processing and model are similar to the Case study: K2-24, putting it all together tutorial, but with a few extra bits like aperture selection and de-trending.

To start, we need to download the target pixel file:

import numpy as np
from astropy.io import fits
import matplotlib.pyplot as plt

tpf_url = "https://archive.stsci.edu/missions/tess/tid/s0001/0000/0002/6113/6679/tess2018206045859-s0001-0000000261136679-0120-s_tp.fits"
with fits.open(tpf_url) as hdus:
    tpf = hdus[1].data
    tpf_hdr = hdus[1].header

texp = tpf_hdr["FRAMETIM"] * tpf_hdr["NUM_FRM"]
texp /= 60.0 * 60.0 * 24.0
time = tpf["TIME"]
flux = tpf["FLUX"]
m = np.any(np.isfinite(flux), axis=(1, 2)) & (tpf["QUALITY"] == 0)
ref_time = 0.5 * (np.min(time[m])+np.max(time[m]))
time = np.ascontiguousarray(time[m] - ref_time, dtype=np.float64)
flux = np.ascontiguousarray(flux[m], dtype=np.float64)

mean_img = np.median(flux, axis=0)
plt.imshow(mean_img.T, cmap="gray_r")
plt.title("TESS image of Pi Men")
plt.xticks([])
plt.yticks([]);
../../_images/tess_4_0.png

Aperture selection

Next, we’ll select an aperture using a hacky method that tries to minimizes the windowed scatter in the lightcurve (something like the CDPP).

from scipy.signal import savgol_filter

# Sort the pixels by median brightness
order = np.argsort(mean_img.flatten())[::-1]

# A function to estimate the windowed scatter in a lightcurve
def estimate_scatter_with_mask(mask):
    f = np.sum(flux[:, mask], axis=-1)
    smooth = savgol_filter(f, 1001, polyorder=5)
    return 1e6 * np.sqrt(np.median((f / smooth - 1)**2))

# Loop over pixels ordered by brightness and add them one-by-one
# to the aperture
masks, scatters = [], []
for i in range(10, 100):
    msk = np.zeros_like(mean_img, dtype=bool)
    msk[np.unravel_index(order[:i], mean_img.shape)] = True
    scatter = estimate_scatter_with_mask(msk)
    masks.append(msk)
    scatters.append(scatter)

# Choose the aperture that minimizes the scatter
pix_mask = masks[np.argmin(scatters)]

# Plot the selected aperture
plt.imshow(mean_img.T, cmap="gray_r")
plt.imshow(pix_mask.T, cmap="Reds", alpha=0.3)
plt.title("selected aperture")
plt.xticks([])
plt.yticks([]);
../../_images/tess_6_0.png

This aperture produces the following light curve:

plt.figure(figsize=(10, 5))
sap_flux = np.sum(flux[:, pix_mask], axis=-1)
sap_flux = (sap_flux / np.median(sap_flux) - 1) * 1e3
plt.plot(time, sap_flux, "k")
plt.xlabel("time [days]")
plt.ylabel("relative flux [ppt]")
plt.title("raw light curve")
plt.xlim(time.min(), time.max());
../../_images/tess_8_0.png

The transit model in PyMC3

The transit model, initialization, and sampling are all nearly the same as the one in Case study: K2-24, putting it all together.

import exoplanet as xo
import pymc3 as pm
import theano.tensor as tt

def build_model(mask=None, start=None):
    if mask is None:
        mask = np.ones(len(x), dtype=bool)
    with pm.Model() as model:

        # Parameters for the stellar properties
        mean = pm.Normal("mean", mu=0.0, sd=10.0)
        u_star = xo.distributions.QuadLimbDark("u_star")

        # Stellar parameters from Huang et al (2018)
        M_star_huang = 1.094, 0.039
        R_star_huang = 1.10, 0.023
        BoundedNormal = pm.Bound(pm.Normal, lower=0, upper=3)
        m_star = BoundedNormal("m_star", mu=M_star_huang[0], sd=M_star_huang[1])
        r_star = BoundedNormal("r_star", mu=R_star_huang[0], sd=R_star_huang[1])

        # Orbital parameters for the planets
        logP = pm.Normal("logP", mu=np.log(bls_period), sd=1)
        t0 = pm.Normal("t0", mu=bls_t0, sd=1)
        logr = pm.Normal("logr", sd=1.0,
                         mu=0.5*np.log(1e-3*np.array(bls_depth))+np.log(R_star_huang[0]))
        r_pl = pm.Deterministic("r_pl", tt.exp(logr))
        ror = pm.Deterministic("ror", r_pl / r_star)
        b = xo.distributions.ImpactParameter("b", ror=ror)

        # This is the eccentricity prior from Kipping (2013):
        # https://arxiv.org/abs/1306.4982
        ecc = xo.distributions.eccentricity.kipping13("ecc", testval=0.1)
        omega = xo.distributions.Angle("omega")

        # Transit jitter & GP parameters
        logs2 = pm.Normal("logs2", mu=np.log(np.var(y[mask])), sd=10)
        logw0 = pm.Normal("logw0", mu=0, sd=10)
        logSw4 = pm.Normal("logSw4", mu=np.log(np.var(y[mask])), sd=10)

        # Tracking planet parameters
        period = pm.Deterministic("period", tt.exp(logP))

        # Orbit model
        orbit = xo.orbits.KeplerianOrbit(
            r_star=r_star, m_star=m_star,
            period=period, t0=t0, b=b,
            ecc=ecc, omega=omega)

        # Compute the model light curve using starry
        light_curves = xo.LimbDarkLightCurve(u_star).get_light_curve(
            orbit=orbit, r=r_pl, t=x[mask], texp=texp)*1e3
        light_curve = pm.math.sum(light_curves, axis=-1) + mean
        pm.Deterministic("light_curves", light_curves)

        # GP model for the light curve
        kernel = xo.gp.terms.SHOTerm(log_Sw4=logSw4, log_w0=logw0, Q=1/np.sqrt(2))
        gp = xo.gp.GP(kernel, x[mask], tt.exp(logs2) + tt.zeros(mask.sum()), J=2)
        pm.Potential("transit_obs", gp.log_likelihood(y[mask] - light_curve))
        pm.Deterministic("gp_pred", gp.predict())

        # Fit for the maximum a posteriori parameters, I've found that I can get
        # a better solution by trying different combinations of parameters in turn
        if start is None:
            start = model.test_point
        map_soln = xo.optimize(start=start, vars=[logs2, logSw4, logw0])
        map_soln = xo.optimize(start=map_soln, vars=[logr])
        map_soln = xo.optimize(start=map_soln, vars=[b])
        map_soln = xo.optimize(start=map_soln, vars=[logP, t0])
        map_soln = xo.optimize(start=map_soln, vars=[u_star])
        map_soln = xo.optimize(start=map_soln, vars=[logr])
        map_soln = xo.optimize(start=map_soln, vars=[b])
        map_soln = xo.optimize(start=map_soln, vars=[ecc, omega])
        map_soln = xo.optimize(start=map_soln, vars=[mean])
        map_soln = xo.optimize(start=map_soln, vars=[logs2, logSw4, logw0])
        map_soln = xo.optimize(start=map_soln)

    return model, map_soln

model0, map_soln0 = build_model()
optimizing logp for variables: [logw0, logSw4, logs2]
20it [00:00, 92.70it/s, logp=1.264290e+04]
message: Optimization terminated successfully.
logp: 12406.06185575557 -> 12642.89955925584
optimizing logp for variables: [logr]
207it [00:03, 58.24it/s, logp=1.268195e+04]
message: Desired error not necessarily achieved due to precision loss.
logp: 12642.899559255851 -> 12681.951246651677
optimizing logp for variables: [b, logr, r_star]
64it [00:01, 35.87it/s, logp=1.294934e+04]
message: Desired error not necessarily achieved due to precision loss.
logp: 12681.951246651677 -> 12949.339148287803
optimizing logp for variables: [t0, logP]
87it [00:00, 100.11it/s, logp=1.296374e+04]
message: Desired error not necessarily achieved due to precision loss.
logp: 12949.339148287796 -> 12963.740755133189
optimizing logp for variables: [u_star]
12it [00:01,  8.05it/s, logp=1.296662e+04]
message: Optimization terminated successfully.
logp: 12963.740755133182 -> 12966.624941258484
optimizing logp for variables: [logr]
9it [00:01,  7.05it/s, logp=1.296682e+04]
message: Optimization terminated successfully.
logp: 12966.624941258473 -> 12966.818870393334
optimizing logp for variables: [b, logr, r_star]
16it [00:01, 11.18it/s, logp=1.296694e+04]
message: Optimization terminated successfully.
logp: 12966.818870393334 -> 12966.944430577354
optimizing logp for variables: [omega, ecc]
23it [00:01, 14.07it/s, logp=1.298782e+04]
message: Optimization terminated successfully.
logp: 12966.944430577354 -> 12987.822019225596
optimizing logp for variables: [mean]
5it [00:01,  3.36it/s, logp=1.298785e+04]
message: Optimization terminated successfully.
logp: 12987.822019225618 -> 12987.850306123684
optimizing logp for variables: [logw0, logSw4, logs2]
70it [00:02, 31.16it/s, logp=1.299865e+04]
message: Optimization terminated successfully.
logp: 12987.850306123684 -> 12998.653500794804
optimizing logp for variables: [logSw4, logw0, logs2, omega, ecc, ecc_beta, ecc_alpha, b, logr, t0, logP, r_star, m_star, u_star, mean]
184it [00:03, 52.27it/s, logp=1.305483e+04]
message: Desired error not necessarily achieved due to precision loss.
logp: 12998.653500794808 -> 13054.83064951331

Here’s how we plot the initial light curve model:

def plot_light_curve(soln, mask=None):
    if mask is None:
        mask = np.ones(len(x), dtype=bool)

    fig, axes = plt.subplots(3, 1, figsize=(10, 7), sharex=True)

    ax = axes[0]
    ax.plot(x[mask], y[mask], "k", label="data")
    gp_mod = soln["gp_pred"] + soln["mean"]
    ax.plot(x[mask], gp_mod, color="C2", label="gp model")
    ax.legend(fontsize=10)
    ax.set_ylabel("relative flux [ppt]")

    ax = axes[1]
    ax.plot(x[mask], y[mask] - gp_mod, "k", label="de-trended data")
    for i, l in enumerate("b"):
        mod = soln["light_curves"][:, i]
        ax.plot(x[mask], mod, label="planet {0}".format(l))
    ax.legend(fontsize=10, loc=3)
    ax.set_ylabel("de-trended flux [ppt]")

    ax = axes[2]
    mod = gp_mod + np.sum(soln["light_curves"], axis=-1)
    ax.plot(x[mask], y[mask] - mod, "k")
    ax.axhline(0, color="#aaaaaa", lw=1)
    ax.set_ylabel("residuals [ppt]")
    ax.set_xlim(x[mask].min(), x[mask].max())
    ax.set_xlabel("time [days]")

    return fig

plot_light_curve(map_soln0);
../../_images/tess_20_0.png

As in the Case study: K2-24, putting it all together tutorial, we can do some sigma clipping to remove significant outliers.

mod = map_soln0["gp_pred"] + map_soln0["mean"] + np.sum(map_soln0["light_curves"], axis=-1)
resid = y - mod
rms = np.sqrt(np.median(resid**2))
mask = np.abs(resid) < 5 * rms

plt.figure(figsize=(10, 5))
plt.plot(x, resid, "k", label="data")
plt.plot(x[~mask], resid[~mask], "xr", label="outliers")
plt.axhline(0, color="#aaaaaa", lw=1)
plt.ylabel("residuals [ppt]")
plt.xlabel("time [days]")
plt.legend(fontsize=12, loc=3)
plt.xlim(x.min(), x.max());
../../_images/tess_22_0.png

And then we re-build the model using the data without outliers.

model, map_soln = build_model(mask, map_soln0)
plot_light_curve(map_soln, mask);
optimizing logp for variables: [logw0, logSw4, logs2]
15it [00:01, 10.71it/s, logp=1.374030e+04]
message: Optimization terminated successfully.
logp: 13709.337756067558 -> 13740.297129125755
optimizing logp for variables: [logr]
8it [00:01,  6.99it/s, logp=1.374032e+04]
message: Optimization terminated successfully.
logp: 13740.297129125758 -> 13740.317229932978
optimizing logp for variables: [b, logr, r_star]
29it [00:01, 23.25it/s, logp=1.374032e+04]
message: Desired error not necessarily achieved due to precision loss.
logp: 13740.317229932978 -> 13740.318317156272
optimizing logp for variables: [t0, logP]
15it [00:01, 13.41it/s, logp=1.374033e+04]
message: Optimization terminated successfully.
logp: 13740.31831715628 -> 13740.326927441381
optimizing logp for variables: [u_star]
8it [00:01,  5.34it/s, logp=1.374035e+04]
message: Optimization terminated successfully.
logp: 13740.326927441381 -> 13740.350391960192
optimizing logp for variables: [logr]
8it [00:01,  6.05it/s, logp=1.374035e+04]
message: Optimization terminated successfully.
logp: 13740.350391960188 -> 13740.353904909165
optimizing logp for variables: [b, logr, r_star]
13it [00:01, 11.31it/s, logp=1.374036e+04]
message: Optimization terminated successfully.
logp: 13740.353904909165 -> 13740.362784313404
optimizing logp for variables: [omega, ecc]
66it [00:01, 43.13it/s, logp=1.374036e+04]
message: Desired error not necessarily achieved due to precision loss.
logp: 13740.362784313404 -> 13740.362835329439
optimizing logp for variables: [mean]
5it [00:01,  3.75it/s, logp=1.374037e+04]
message: Optimization terminated successfully.
logp: 13740.362835329439 -> 13740.36600504063
optimizing logp for variables: [logw0, logSw4, logs2]
9it [00:01,  6.44it/s, logp=1.374037e+04]
message: Optimization terminated successfully.
logp: 13740.36600504063 -> 13740.366007623204
optimizing logp for variables: [logSw4, logw0, logs2, omega, ecc, ecc_beta, ecc_alpha, b, logr, t0, logP, r_star, m_star, u_star, mean]
91it [00:02, 37.29it/s, logp=1.374038e+04]
message: Desired error not necessarily achieved due to precision loss.
logp: 13740.3660076232 -> 13740.37793983133
../../_images/tess_24_1.png

Now that we have the model, we can sample:

np.random.seed(42)
with model:
    trace = pm.sample(tune=5000, draws=3000, start=map_soln, chains=4,
                      step=xo.get_dense_nuts_step(target_accept=0.9))
Multiprocess sampling (4 chains in 4 jobs)
NUTS: [logSw4, logw0, logs2, omega, ecc, ecc_beta, ecc_alpha, b, logr, t0, logP, r_star, m_star, u_star, mean]
Sampling 4 chains: 100%|██████████| 32000/32000 [2:38:18<00:00,  1.04s/draws]
There was 1 divergence after tuning. Increase target_accept or reparameterize.
The number of effective samples is smaller than 10% for some parameters.
pm.summary(trace, varnames=["logw0", "logSw4", "logs2", "omega", "ecc", "r_pl", "b", "t0", "logP", "r_star", "m_star", "u_star", "mean"])
mean sd mc_error hpd_2.5 hpd_97.5 n_eff Rhat
logw0 1.178893 0.137754 1.503798e-03 0.902732 1.441867 9690.786748 0.999896
logSw4 -2.101933 0.320000 3.549201e-03 -2.717470 -1.473023 10150.540106 0.999991
logs2 -4.382816 0.010474 1.117906e-04 -4.403522 -4.363071 10040.304524 1.000070
omega 0.654692 1.719355 3.692160e-02 -2.827989 3.138827 1857.782392 1.001326
ecc 0.222181 0.146238 3.251373e-03 0.000051 0.516797 1797.151414 1.000742
r_pl 0.017246 0.000664 1.493598e-05 0.016009 0.018551 1771.548874 1.000065
b 0.394967 0.219231 5.041584e-03 0.000013 0.738112 1635.492630 1.000577
t0 -1.197393 0.000760 2.305281e-05 -1.198772 -1.195969 823.500623 1.001729
logP 1.835410 0.000071 9.212764e-07 1.835269 1.835537 6989.158384 0.999957
r_star 1.098800 0.022756 1.865907e-04 1.052688 1.141015 10490.084092 0.999971
m_star 1.094947 0.039345 3.819506e-04 1.017792 1.170257 11120.496164 1.000554
u_star__0 0.202729 0.170785 2.572381e-03 0.000011 0.546532 4024.512054 1.000023
u_star__1 0.456836 0.277733 4.296571e-03 -0.093615 0.948256 3922.343483 0.999852
mean -0.000892 0.009125 9.200209e-05 -0.018995 0.017397 9475.726176 1.000000

Results

After sampling, we can make the usual plots. First, let’s look at the folded light curve plot:

# Compute the GP prediction
gp_mod = np.median(trace["gp_pred"] + trace["mean"][:, None], axis=0)

# Get the posterior median orbital parameters
p = np.median(trace["period"])
t0 = np.median(trace["t0"])

# Plot the folded data
x_fold = (x[mask] - t0 + 0.5*p) % p - 0.5*p
plt.plot(x_fold, y[mask] - gp_mod, ".k", label="data", zorder=-1000)

# Overplot the phase binned light curve
bins = np.linspace(-0.41, 0.41, 50)
denom, _ = np.histogram(x_fold, bins)
num, _ = np.histogram(x_fold, bins, weights=y[mask])
denom[num == 0] = 1.0
plt.plot(0.5*(bins[1:] + bins[:-1]), num / denom, "o", color="C2",
         label="binned")

# Plot the folded model
inds = np.argsort(x_fold)
inds = inds[np.abs(x_fold)[inds] < 0.3]
pred = trace["light_curves"][:, inds, 0]
pred = np.percentile(pred, [16, 50, 84], axis=0)
plt.plot(x_fold[inds], pred[1], color="C1", label="model")
art = plt.fill_between(x_fold[inds], pred[0], pred[2], color="C1", alpha=0.5,
                       zorder=1000)
art.set_edgecolor("none")

# Annotate the plot with the planet's period
txt = "period = {0:.5f} +/- {1:.5f} d".format(
    np.mean(trace["period"]), np.std(trace["period"]))
plt.annotate(txt, (0, 0), xycoords="axes fraction",
             xytext=(5, 5), textcoords="offset points",
             ha="left", va="bottom", fontsize=12)

plt.legend(fontsize=10, loc=4)
plt.xlim(-0.5*p, 0.5*p)
plt.xlabel("time since transit [days]")
plt.ylabel("de-trended flux")
plt.xlim(-0.15, 0.15);
../../_images/tess_29_0.png

And a corner plot of some of the key parameters:

import corner
import astropy.units as u
varnames = ["period", "b", "ecc", "r_pl"]
samples = pm.trace_to_dataframe(trace, varnames=varnames)

# Convert the radius to Earth radii
samples["r_pl"] = (np.array(samples["r_pl"]) * u.R_sun).to(u.R_earth).value

corner.corner(
    samples[["period", "r_pl", "b", "ecc"]],
    labels=["period [days]", "radius [Earth radii]", "impact param", "eccentricity"]);
../../_images/tess_31_0.png

These all seem consistent with the previously published values and an earlier inconsistency between this radius measurement and the literature has been resolved by fixing a bug in exoplanet.

Citations

As described in the Citing exoplanet & its dependencies tutorial, we can use exoplanet.citations.get_citations_for_model() to construct an acknowledgement and BibTeX listing that includes the relevant citations for this model.

with model:
    txt, bib = xo.citations.get_citations_for_model()
print(txt)
This research made use of textsf{exoplanet} citep{exoplanet} and its
dependencies citep{exoplanet:agol19, exoplanet:astropy13, exoplanet:astropy18,
exoplanet:exoplanet, exoplanet:foremanmackey17, exoplanet:foremanmackey18,
exoplanet:kipping13, exoplanet:kipping13b, exoplanet:luger18, exoplanet:pymc3,
exoplanet:theano}.
print("\n".join(bib.splitlines()[:10]) + "\n...")
@misc{exoplanet:exoplanet,
  author = {Daniel Foreman-Mackey and Ian Czekala and Eric Agol and
            Rodrigo Luger and Geert Barentsen and Tom Barclay},
   title = {dfm/exoplanet: exoplanet v0.2.0},
   month = aug,
    year = 2019,
     doi = {10.5281/zenodo.3359880},
     url = {https://doi.org/10.5281/zenodo.3359880}
}
...