%run notebook_setup

If you have not already read it, you may want to start with the first tutorial: `Getting started with The Joker <1-Getting-started.ipynb>`__.

Continue generating samples with standard MCMC

When many prior samples are used with The Joker, and the sampler returns one sample, or the samples returned are within the same mode of the posterior, the posterior pdf is likely unimodal. In these cases, we can use standard MCMC methods to generate posterior samples, which will typically be much more efficient than The Joker itself. In this example, we will use pymc3 to “continue” sampling for data that are very constraining.

First, some imports we will need later:

import astropy.coordinates as coord
import astropy.table as at
from astropy.time import Time
import astropy.units as u
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
import corner
import pymc3 as pm
import exoplanet as xo

import thejoker as tj
WARNING (theano.tensor.blas): Using NumPy C-API based implementation for BLAS functions.
# set up a random number generator to ensure reproducibility
rnd = np.random.default_rng(seed=42)

Here we will again load some pre-generated data meant to represent well-sampled, precise radial velocity observations of a single luminous source with a single companion (we again downsample the data set here just for demonstration):

data_tbl = at.QTable.read('data.ecsv')
sub_tbl = data_tbl[rnd.choice(len(data_tbl), size=18, replace=False)]  # downsample data
data = tj.RVData.guess_from_table(sub_tbl, t0=data_tbl.meta['t0'])
_ = data.plot()
findfont: Font family ['serif'] not found. Falling back to DejaVu Sans.
findfont: Font family ['cursive'] not found. Falling back to DejaVu Sans.

We will use the default prior, but feel free to play around with these values:

prior = tj.JokerPrior.default(
    P_min=2*u.day, P_max=1e3*u.day,

The data above look fairly constraining: it would be hard to draw many distinct orbital solutions through the RV data plotted above. In cases like this, we will often only get back 1 or a few samples from The Joker even if we use a huge number of prior samples. Since we are only going to use the samples from The Joker to initialize standard MCMC, we will only use a moderate number of prior samples:

prior_samples = prior.sample(size=250_000,
joker = tj.TheJoker(prior, random_state=rnd)
joker_samples = joker.rejection_sample(data, prior_samples,
<JokerSamples [P, e, omega, M0, s, K, v0] (1 samples)>
QTable length=1
dradradkm / skm / skm / s
_ = tj.plot_rv_curves(joker_samples, data=data)

The sample that was returned by The Joker does look like it is a reasonable fit to the RV data, but to fully explore the posterior pdf we will use standard MCMC through pymc3. Here we will use the NUTS sampler, but you could also experiment with other backends (e.g., Metropolis-Hastings, or even emcee by following this blog post):

with prior.model:
    mcmc_init = joker.setup_mcmc(data, joker_samples)

    trace = pm.sample(tune=1000, draws=1000,
                      cores=1, chains=2)
Sequential sampling (2 chains in 1 job)
NUTS: [v0, K, P, M0, omega, e]
100.00% [2000/2000 00:23<00:00 Sampling chain 0, 0 divergences]
100.00% [2000/2000 00:15<00:00 Sampling chain 1, 0 divergences]
Sampling 2 chains for 1_000 tune and 1_000 draw iterations (2_000 + 2_000 draws total) took 39 seconds.

If you get warnings from running the sampler above, they usually indicate that we should run the sampler for many more steps to tune the sampler and for our main run, but let’s ignore that for now. With the MCMC traces in hand, we can summarize the properties of the chains using pymc3.summary:

pm.summary(trace, var_names=prior.par_names)
mean sd hdi_3% hdi_97% mcse_mean mcse_sd ess_mean ess_sd ess_bulk ess_tail r_hat
P 51.476 0.108 51.292 51.682 0.002 0.002 2177.0 2177.0 2179.0 1498.0 1.00
e 0.095 0.017 0.063 0.125 0.000 0.000 1381.0 1381.0 1385.0 1066.0 1.00
omega 0.342 0.140 0.097 0.605 0.005 0.004 661.0 592.0 717.0 766.0 1.00
M0 1.438 0.125 1.206 1.664 0.005 0.004 651.0 622.0 707.0 635.0 1.01
s 0.000 0.000 0.000 0.000 0.000 0.000 2000.0 2000.0 2000.0 2000.0 NaN
K -12.199 0.204 -12.560 -11.791 0.005 0.003 1703.0 1703.0 1705.0 1262.0 1.00
v0 -7.691 0.154 -8.013 -7.421 0.003 0.002 2413.0 2407.0 2419.0 1552.0 1.00

To convert the trace into a JokerSamples instance, we can use the TheJoker.trace_to_samples() method. Note here that the sign of K is arbitrary, so to compare to the true value, we also call wrap_K() to store only the absolute value of K (which also increases omega by π, to stay consistent):

mcmc_samples = joker.trace_to_samples(trace, data)
<JokerSamples [P, e, omega, M0, s, K, v0] (2000 samples)>

We can now compare the samples we got from MCMC to the true orbital parameters used to generate this data:

import pickle
with open('true-orbit.pkl', 'rb') as f:
    truth = pickle.load(f)

# make sure the angles are wrapped the same way
if np.median(mcmc_samples['omega']) < 0:
    truth['omega'] = coord.Angle(truth['omega']).wrap_at(np.pi*u.radian)

if np.median(mcmc_samples['M0']) < 0:
    truth['M0'] = coord.Angle(truth['M0']).wrap_at(np.pi*u.radian)
df = mcmc_samples.tbl.to_pandas()
truths = []
colnames = []
for name in df.columns:
    if name in truth:
_ = corner.corner(df[colnames], truths=truths)

Overall, it looks like we do recover the input parameters!