In this tutorial, we will demonstrate how to fit radial velocity observations of an exoplanetary system using exoplanet. We will follow the getting started tutorial from the excellent RadVel package where they fit for the parameters of the two planets in the K2-24 system.
First, let’s download the data from RadVel:
[3]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
url = "https://raw.githubusercontent.com/California-Planet-Search/radvel/master/example_data/epic203771098.csv"
data = pd.read_csv(url, index_col=0)
x = np.array(data.t)
y = np.array(data.vel)
yerr = np.array(data.errvel)
# Compute a reference time that will be used to normalize the trends model
x_ref = 0.5 * (x.min() + x.max())
# Also make a fine grid that spans the observation window for plotting purposes
t = np.linspace(x.min() - 5, x.max() + 5, 1000)
plt.errorbar(x, y, yerr=yerr, fmt=".k")
plt.xlabel("time [days]")
_ = plt.ylabel("radial velocity [m/s]")
Now, we know the periods and transit times for the planets from the K2 light curve, so let’s start by using the exoplanet.estimate_semi_amplitude()
function to estimate the expected RV semi-amplitudes for the planets.
[4]:
import exoplanet as xo
periods = [20.8851, 42.3633]
period_errs = [0.0003, 0.0006]
t0s = [2072.7948, 2082.6251]
t0_errs = [0.0007, 0.0004]
Ks = xo.estimate_semi_amplitude(periods, x, y, yerr, t0s=t0s)
print(Ks, "m/s")
[5.05069163 5.50983542] m/s
Now that we have the data and an estimate of the initial values for the parameters, let’s start defining the probabilistic model in PyMC3 (take a look at A quick intro to PyMC3 for exoplaneteers if you’re new to PyMC3). First, we’ll define our priors on the parameters:
[5]:
import pymc3 as pm
import theano.tensor as tt
with pm.Model() as model:
# Gaussian priors based on transit data (from Petigura et al.)
t0 = pm.Normal("t0", mu=np.array(t0s), sd=np.array(t0_errs), shape=2)
P = pm.Bound(pm.Normal, lower=0)(
"P",
mu=np.array(periods),
sd=np.array(period_errs),
shape=2,
testval=np.array(periods),
)
# Wide log-normal prior for semi-amplitude
logK = pm.Bound(pm.Normal, lower=0)(
"logK", mu=np.log(Ks), sd=10.0, shape=2, testval=np.log(Ks)
)
# Eccentricity & argument of periasteron
ecc = xo.distributions.UnitUniform(
"ecc", shape=2, testval=np.array([0.1, 0.1])
)
omega = xo.distributions.Angle("omega", shape=2)
# Jitter & a quadratic RV trend
logs = pm.Normal("logs", mu=np.log(np.median(yerr)), sd=5.0)
trend = pm.Normal("trend", mu=0, sd=10.0 ** -np.arange(3)[::-1], shape=3)
# Then we define the orbit
orbit = xo.orbits.KeplerianOrbit(period=P, t0=t0, ecc=ecc, omega=omega)
# And a function for computing the full RV model
def get_rv_model(t, name=""):
# First the RVs induced by the planets
vrad = orbit.get_radial_velocity(t, K=tt.exp(logK))
pm.Deterministic("vrad" + name, vrad)
# Define the background model
A = np.vander(t - x_ref, 3)
bkg = pm.Deterministic("bkg" + name, tt.dot(A, trend))
# Sum over planets and add the background to get the full model
return pm.Deterministic("rv_model" + name, tt.sum(vrad, axis=-1) + bkg)
# Define the RVs at the observed times
rv_model = get_rv_model(x)
# Also define the model on a fine grid as computed above (for plotting)
rv_model_pred = get_rv_model(t, name="_pred")
# Finally add in the observation model. This next line adds a new contribution
# to the log probability of the PyMC3 model
err = tt.sqrt(yerr ** 2 + tt.exp(2 * logs))
pm.Normal("obs", mu=rv_model, sd=err, observed=y)
Now, we can plot the initial model:
[6]:
plt.errorbar(x, y, yerr=yerr, fmt=".k")
with model:
plt.plot(t, xo.eval_in_model(model.vrad_pred), "--k", alpha=0.5)
plt.plot(t, xo.eval_in_model(model.bkg_pred), ":k", alpha=0.5)
plt.plot(t, xo.eval_in_model(model.rv_model_pred), label="model")
plt.legend(fontsize=10)
plt.xlim(t.min(), t.max())
plt.xlabel("time [days]")
plt.ylabel("radial velocity [m/s]")
_ = plt.title("initial model")
In this plot, the background is the dotted line, the individual planets are the dashed lines, and the full model is the blue line.
It doesn’t look amazing so let’s fit for the maximum a posterior parameters.
[7]:
with model:
map_soln = xo.optimize(start=model.test_point, vars=[trend])
map_soln = xo.optimize(start=map_soln)
optimizing logp for variables: [trend]
12it [00:04, 2.94it/s, logp=-6.484820e+01]
message: Optimization terminated successfully.
logp: -79.73266285618772 -> -64.84820262331459
optimizing logp for variables: [trend, logs, omega, ecc, logK, P, t0]
135it [00:00, 273.79it/s, logp=-1.427676e+01]
message: Desired error not necessarily achieved due to precision loss.
logp: -64.84820262331459 -> -14.27676026238062
[8]:
plt.errorbar(x, y, yerr=yerr, fmt=".k")
plt.plot(t, map_soln["vrad_pred"], "--k", alpha=0.5)
plt.plot(t, map_soln["bkg_pred"], ":k", alpha=0.5)
plt.plot(t, map_soln["rv_model_pred"], label="model")
plt.legend(fontsize=10)
plt.xlim(t.min(), t.max())
plt.xlabel("time [days]")
plt.ylabel("radial velocity [m/s]")
_ = plt.title("MAP model")
That looks better.
Now that we have our model set up and a good estimate of the initial parameters, let’s start sampling. There are substantial covariances between some of the parameters so we’ll use the init="adapt_full"
argument.
[9]:
np.random.seed(42)
with model:
trace = pm.sample(
tune=4000,
draws=4000,
cores=2,
chains=2,
target_accept=0.95,
init="adapt_full",
)
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_full...
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [trend, logs, omega, ecc, logK, P, t0]
Sampling 2 chains for 4_000 tune and 4_000 draw iterations (8_000 + 8_000 draws total) took 85 seconds.
There was 1 divergence after tuning. Increase `target_accept` or reparameterize.
After sampling, it’s always a good idea to do some convergence checks. First, let’s check the number of effective samples and the Gelman-Rubin statistic for our parameters of interest:
[10]:
pm.summary(
trace, varnames=["trend", "logs", "omega", "ecc", "t0", "logK", "P"]
)
[10]:
mean | sd | hdi_3% | hdi_97% | mcse_mean | mcse_sd | ess_mean | ess_sd | ess_bulk | ess_tail | r_hat | |
---|---|---|---|---|---|---|---|---|---|---|---|
trend[0] | 0.001 | 0.001 | -0.000 | 0.002 | 0.000 | 0.000 | 5096.0 | 5061.0 | 5197.0 | 4833.0 | 1.0 |
trend[1] | -0.038 | 0.023 | -0.080 | 0.004 | 0.000 | 0.000 | 6299.0 | 6185.0 | 6311.0 | 4930.0 | 1.0 |
trend[2] | -1.962 | 0.800 | -3.480 | -0.481 | 0.010 | 0.007 | 6387.0 | 6385.0 | 6411.0 | 5299.0 | 1.0 |
logs | 1.035 | 0.227 | 0.622 | 1.474 | 0.004 | 0.003 | 3677.0 | 3545.0 | 3684.0 | 4440.0 | 1.0 |
omega[0] | -0.297 | 0.773 | -1.383 | 1.327 | 0.016 | 0.015 | 2232.0 | 1286.0 | 2573.0 | 1763.0 | 1.0 |
omega[1] | -0.693 | 2.125 | -3.134 | 2.887 | 0.042 | 0.029 | 2605.0 | 2605.0 | 3882.0 | 6559.0 | 1.0 |
ecc[0] | 0.237 | 0.113 | 0.000 | 0.413 | 0.002 | 0.001 | 3376.0 | 3376.0 | 2871.0 | 1695.0 | 1.0 |
ecc[1] | 0.205 | 0.148 | 0.000 | 0.477 | 0.003 | 0.002 | 2368.0 | 2204.0 | 2404.0 | 2715.0 | 1.0 |
t0[0] | 2072.795 | 0.001 | 2072.794 | 2072.796 | 0.000 | 0.000 | 8108.0 | 8108.0 | 8109.0 | 5299.0 | 1.0 |
t0[1] | 2082.625 | 0.000 | 2082.624 | 2082.626 | 0.000 | 0.000 | 7178.0 | 7178.0 | 7173.0 | 5066.0 | 1.0 |
logK[0] | 1.542 | 0.264 | 1.024 | 1.976 | 0.007 | 0.005 | 1484.0 | 1484.0 | 2278.0 | 1230.0 | 1.0 |
logK[1] | 1.568 | 0.233 | 1.134 | 1.992 | 0.005 | 0.003 | 2482.0 | 2482.0 | 3422.0 | 2204.0 | 1.0 |
P[0] | 20.885 | 0.000 | 20.885 | 20.886 | 0.000 | 0.000 | 7939.0 | 7939.0 | 7958.0 | 4883.0 | 1.0 |
P[1] | 42.363 | 0.001 | 42.362 | 42.364 | 0.000 | 0.000 | 8408.0 | 8408.0 | 8398.0 | 5225.0 | 1.0 |
It looks like everything is pretty much converged here. Not bad for 14 parameters and about a minute of runtime…
Then we can make a corner plot of any combination of the parameters. For example, let’s look at period, semi-amplitude, and eccentricity:
[11]:
import corner
samples = pm.trace_to_dataframe(trace, varnames=["P", "logK", "ecc", "omega"])
_ = corner.corner(samples)
Finally, let’s plot the plosterior constraints on the RV model and compare those to the data:
[12]:
plt.errorbar(x, y, yerr=yerr, fmt=".k")
# Compute the posterior predictions for the RV model
pred = np.percentile(trace["rv_model_pred"], [16, 50, 84], axis=0)
plt.plot(t, pred[1], color="C0", label="model")
art = plt.fill_between(t, pred[0], pred[2], color="C0", alpha=0.3)
art.set_edgecolor("none")
plt.legend(fontsize=10)
plt.xlim(t.min(), t.max())
plt.xlabel("time [days]")
plt.ylabel("radial velocity [m/s]")
_ = plt.title("posterior constraints")
It might be also be interesting to look at the phased plots for this system. Here we’ll fold the dataset on the median of posterior period and then overplot the posterior constraint on the folded model orbits.
[13]:
for n, letter in enumerate("bc"):
plt.figure()
# Get the posterior median orbital parameters
p = np.median(trace["P"][:, n])
t0 = np.median(trace["t0"][:, n])
# Compute the median of posterior estimate of the background RV
# and the contribution from the other planet. Then we can remove
# this from the data to plot just the planet we care about.
other = np.median(trace["vrad"][:, :, (n + 1) % 2], axis=0)
other += np.median(trace["bkg"], axis=0)
# Plot the folded data
x_fold = (x - t0 + 0.5 * p) % p - 0.5 * p
plt.errorbar(x_fold, y - other, yerr=yerr, fmt=".k")
# Compute the posterior prediction for the folded RV model for this
# planet
t_fold = (t - t0 + 0.5 * p) % p - 0.5 * p
inds = np.argsort(t_fold)
pred = np.percentile(trace["vrad_pred"][:, inds, n], [16, 50, 84], axis=0)
plt.plot(t_fold[inds], pred[1], color="C0", label="model")
art = plt.fill_between(
t_fold[inds], pred[0], pred[2], color="C0", alpha=0.3
)
art.set_edgecolor("none")
plt.legend(fontsize=10)
plt.xlim(-0.5 * p, 0.5 * p)
plt.xlabel("phase [days]")
plt.ylabel("radial velocity [m/s]")
plt.title("K2-24{0}".format(letter))
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.
[14]:
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:arviz, exoplanet:astropy13, exoplanet:astropy18,
exoplanet:exoplanet, exoplanet:pymc3, exoplanet:theano}.
[15]:
print("\n".join(bib.splitlines()[:10]) + "\n...")
@misc{exoplanet:exoplanet,
author = {Daniel Foreman-Mackey and Arjun Savel and Rodrigo Luger and
Ian Czekala and Eric Agol and Adrian Price-Whelan and
Emily Gilbert and Timothy D. Brandt and Tom Barclay and Luke Bouma},
title = {exoplanet-dev/exoplanet v0.4.4},
month = jan,
year = 2021,
doi = {10.5281/zenodo.1998447},
url = {https://doi.org/10.5281/zenodo.1998447}
...