```
[1]:
```

```
%run notebook_setup
```

If you have not already read it, you may want to start with the first tutorial: Getting started with The Joker.

# Inferring calibration offsets between instruments¶

Also in addition to the default linear parameters (see Tutorial 1, or the documentation for `JokerSamples.default()`

), *The Joker* allows adding linear parameters to account for possible calibration offsets between instruments. For example, there may be an absolute velocity offset between two spectrographs. Below we will demonstrate how to simultaneously infer and marginalize over a constant velocity offset between two simulated surveys of the same “star”.

First, some imports we will need later:

```
[2]:
```

```
import astropy.table as at
import astropy.units as u
from astropy.visualization.units import quantity_support
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
import corner
import pymc3 as pm
import exoplanet as xo
import exoplanet.units as xu
import thejoker as tj
```

```
WARNING (theano.tensor.blas): Using NumPy C-API based implementation for BLAS functions.
```

```
[3]:
```

```
# set up a random state to ensure reproducibility
rnd = np.random.RandomState(seed=42)
```

The data for our two surveys are stored in two separate CSV files included with the documentation. We will load separate `RVData`

instances for the two data sets and append these objects to a list of datasets:

```
[4]:
```

```
data = []
for filename in ['data-survey1.ecsv', 'data-survey2.ecsv']:
tbl = at.QTable.read(filename)
_data = tj.RVData.guess_from_table(tbl, t0=tbl.meta['t0'])
data.append(_data)
```

In the plot below, the two data sets are shown in different colors:

```
[5]:
```

```
for d, color in zip(data, ['tab:blue', 'tab:red']):
_ = d.plot(color=color)
```

```
findfont: Font family ['serif'] not found. Falling back to DejaVu Sans.
findfont: Font family ['cursive'] not found. Falling back to DejaVu Sans.
```

To tell *The Joker* to handle additional linear parameters to account for offsets in absolute velocity, we must define a new parameter for the offset betwen survey 1 and survey 2 and specify a prior. Here we will assume a Gaussian prior on the offset, centered on 0, but with a 10 km/s standard deviation. We then pass this in to `JokerPrior.default()`

(all other parameters here use the default prior) through the `v0_offsets`

argument:

```
[6]:
```

```
with pm.Model() as model:
dv0_1 = xu.with_unit(pm.Normal('dv0_1', 0, 10),
u.km/u.s)
prior = tj.JokerPrior.default(
P_min=2*u.day, P_max=256*u.day,
sigma_K0=30*u.km/u.s,
sigma_v=100*u.km/u.s,
v0_offsets=[dv0_1])
```

The rest should look familiar: The code below is identical to previous tutorials, in which we generate prior samples and then rejection sample with *The Joker*:

```
[7]:
```

```
prior_samples = prior.sample(size=1_000_000,
random_state=rnd)
```

```
[8]:
```

```
joker = tj.TheJoker(prior, random_state=rnd)
joker_samples = joker.rejection_sample(data, prior_samples,
max_posterior_samples=128)
joker_samples
```

```
[8]:
```

```
<JokerSamples [P, e, omega, M0, s, K, v0, dv0_1] (1 samples)>
```

Note that the new parameter, `dv0_1`

, now appears in the returned samples above.

If we pass these samples in to the `plot_rv_curves`

function, the data from other surveys is, by default, shifted by the mean value of the offset before plotting:

```
[9]:
```

```
_ = tj.plot_rv_curves(joker_samples, data=data)
```

However, the above behavior can be disabled by setting `apply_mean_v0_offset=False`

. Note that with this set, the inferred orbit will not generally pass through data that suffer from a measurable offset:

```
[10]:
```

```
_ = tj.plot_rv_curves(joker_samples, data=data,
apply_mean_v0_offset=False)
```

As introduced in the previous tutorial, we can also continue generating samples by initializing and running standard MCMC:

```
[11]:
```

```
with prior.model:
mcmc_init = joker.setup_mcmc(data, joker_samples)
trace = pm.sample(
tune=1000, draws=1000,
start=mcmc_init,
step=xo.get_dense_nuts_step(target_accept=0.95))
```

```
Multiprocess sampling (4 chains in 4 jobs)
NUTS: [v0, K, P, M0, omega, e, dv0_1]
Sampling 4 chains, 0 divergences: 100%|██████████| 8000/8000 [00:21<00:00, 372.42draws/s]
The number of effective samples is smaller than 25% for some parameters.
```

```
[12]:
```

```
pm.summary(trace, var_names=prior.par_names)
```

```
[12]:
```

mean | sd | hpd_3% | hpd_97% | mcse_mean | mcse_sd | ess_mean | ess_sd | ess_bulk | ess_tail | r_hat | |
---|---|---|---|---|---|---|---|---|---|---|---|

P | 67.163 | 0.211 | 66.764 | 67.543 | 0.004 | 0.003 | 2312.0 | 2311.0 | 2332.0 | 2931.0 | 1.0 |

e | 0.138 | 0.017 | 0.107 | 0.170 | 0.000 | 0.000 | 1760.0 | 1621.0 | 1945.0 | 1638.0 | 1.0 |

omega | -2.090 | 0.262 | -2.553 | -1.602 | 0.008 | 0.006 | 965.0 | 965.0 | 980.0 | 1352.0 | 1.0 |

M0 | 1.910 | 0.298 | 1.391 | 2.467 | 0.010 | 0.007 | 960.0 | 930.0 | 976.0 | 1336.0 | 1.0 |

s | 0.000 | 0.000 | 0.000 | 0.000 | 0.000 | 0.000 | 4000.0 | 4000.0 | 4000.0 | 4000.0 | NaN |

K | 7.951 | 0.100 | 7.769 | 8.143 | 0.003 | 0.002 | 1354.0 | 1345.0 | 1462.0 | 1536.0 | 1.0 |

v0 | 40.226 | 0.274 | 39.729 | 40.742 | 0.009 | 0.006 | 943.0 | 943.0 | 975.0 | 1042.0 | 1.0 |

dv0_1 | 4.797 | 0.360 | 4.117 | 5.444 | 0.010 | 0.007 | 1222.0 | 1203.0 | 1235.0 | 1800.0 | 1.0 |

Here the true offset is 4.8 km/s, so it looks like we recover this value!

A full corner plot of the MCMC samples:

```
[13]:
```

```
mcmc_samples = joker.trace_to_samples(trace, data)
mcmc_samples.wrap_K()
```

```
[13]:
```

```
<JokerSamples [P, e, omega, M0, s, K, v0, dv0_1] (4000 samples)>
```

```
[14]:
```

```
df = mcmc_samples.tbl.to_pandas()
colnames = mcmc_samples.par_names
colnames.pop(colnames.index('s'))
_ = corner.corner(df[colnames])
```