Random Variates

When we perform a fit or load and analysis result, the parmeters of our model become distributions in the AnalysisResults object. These are actaully instantiactions of the RandomVaraiates class.

While we have covered most of the functionality of RandomVariates in the AnalysisResults section, we want to highlight a few of the details here.

import warnings

import numpy as np

{'divide': 'warn', 'over': 'warn', 'under': 'ignore', 'invalid': 'warn'}
import matplotlib.pyplot as plt
from threeML import *
from jupyterthemes import jtplot

%matplotlib inline
jtplot.style(context="talk", fscale=1, ticks=True, grid=False)

Let’s load back our fit of the line + gaussian from the AnalysisResults section.

ar = load_analysis_results("test_mle.fits")

When we display our fit, we can see the parameter paths of the model. What if we want specific information on a parameter(s)?

Best fit values:

result unit
fake.spectrum.main.composite.a_1 2.03 +/- 0.11 1 / (cm2 keV s)
fake.spectrum.main.composite.b_1 (-1 +/- 4) x 10^-3 1 / (cm2 keV2 s)
fake.spectrum.main.composite.F_2 (2.6 +/- 0.4) x 10 1 / (cm2 s)
fake.spectrum.main.composite.mu_2 (2.485 +/- 0.014) x 10 keV
fake.spectrum.main.composite.sigma_2 1.11 +/- 0.11 keV
Correlation matrix:

Values of -log(likelihood) at the minimum:

sim_data 15.147862
total 15.147862
Values of statistical measures:

statistical measures
AIC 41.659361
BIC 49.855840

Let’s take a look at the normalization of the gaussian. To access the parameter, we take the parameter path, and we want to get the variates:

norm = ar.get_variates("fake.spectrum.main.composite.F_2")

Now, norm is a RandomVariate.


This is essentially a wrapper around numpy NDArray with a few added properties. It is an array of samples. In the MLE case, they are samples from the covariance matrix (this is not at all a marginal distribution, but the parameter “knows” about the entire fit, i.e., it is not a profile) and in the Bayesian case, these are samples from the posterior (this is a marginal).

The output representation for an RV are its 68% equal-tail and HPD uncertainties.

equal-tail: (2.6 +/- 0.4) x 10, hpd: (2.6 +/- 0.4) x 10

We can access these directly, and to any desired confidence level.

(18.241009915544332, 34.09791806021883)
(23.611795945918463, 28.923712740626446)

As stated above, the RV is made from samples. We can histogram them to show this explicitly.

fig, ax = plt.subplots()

ax.hist(norm.samples, bins=50, ec="k", fc="w", lw=1.2)
Text(0.5, 0, 'norm')

We can easily transform the RV through propagation.

log_norm = np.log10(norm)
equal-tail: 1.42 -0.07 +0.06, hpd: 1.42 -0.06 +0.07
fig, ax = plt.subplots()

ax.hist(log_norm.samples, bins=50, ec="k", fc="w", lw=1.2)
ax.set_xlabel("log norm")
Text(0.5, 0, 'log norm')
.. note:: Some operations will destroy the RV by accessing only its NDArray substructure. For example, using an RV with astropy units will return an array of samples with the given units.