Analysis Results

3ML stores the results of a fit in a container we call an “Analysis Result” (AR). The structure of this object is designed to be useable in a live sense within an active analysis (python script, ipython interactive shell, jupyter notebook) as well as storable as a FITS file for saving results for later.

The structure is nearly the same between MLE and Bayesian analyses in order to make a seamless functionality between all analyses.

Let’s take a look at what we can do with an AR. First, we will simulate some data.

[3]:
gen_function = Line(a=2, b=0) + Gaussian(F=30.0, mu=25.0, sigma=1)

# Generate a dataset using the line and a gaussian.
# constant 20% error

x = np.linspace(0, 50, 50)

xy = XYLike.from_function(
    "sim_data", function=gen_function, x=x, yerr=0.2 * gen_function(x)
)

fig = xy.plot()
../_images/notebooks_Analysis_results_showcase_4_0.png

MLE Results

First we will demonstrate how AR’s work for an MLE analysis on our synthetic data. As we will see, most of the functionality exists in the Bayesian AR’s as well.

Let’s do a simple likelihood maximization of our data and model.

[4]:
fitfun = Line() + Gaussian()

fitfun.b_1.bounds = (-10, 10.0)
fitfun.a_1.bounds = (-100, 100.0)
fitfun.F_2 = 25.0
fitfun.F_2.bounds = (1e-3, 200.0)
fitfun.mu_2 = 25.0
fitfun.mu_2.bounds = (0.0, 100.0)
fitfun.sigma_2.bounds = (1e-3, 10.0)

model = Model(PointSource("fake", 0.0, 0.0, fitfun))

data = DataList(xy)

jl = JointLikelihood(model, DataList(xy))
_ = jl.fit()
Best fit values:

result unit
parameter
fake.spectrum.main.composite.a_1 1.84 +/- 0.11 1 / (cm2 keV s)
fake.spectrum.main.composite.b_1 (4 +/- 4) x 10^-3 1 / (cm2 keV2 s)
fake.spectrum.main.composite.F_2 (3.6 +/- 0.4) x 10 1 / (cm2 s)
fake.spectrum.main.composite.mu_2 (2.487 +/- 0.010) x 10 keV
fake.spectrum.main.composite.sigma_2 1.03 +/- 0.09 keV

Correlation matrix:

1.00-0.85-0.040.01-0.10
-0.851.00-0.00-0.020.00
-0.04-0.001.000.14-0.16
0.01-0.020.141.000.10
-0.100.00-0.160.101.00

Values of -log(likelihood) at the minimum:

-log(likelihood)
sim_data 21.713302
total 21.713302

Values of statistical measures:

statistical measures
AIC 54.790240
BIC 62.986719

We can get our errors as always, but the results cannot be propagated (error propagation assumes Gaussian errors, i.e., symmetric errors) In this case though errors are pretty symmetric, so we are likely in the case where the MLE is actually normally distributed.

[5]:
jl.get_errors()
result unit
parameter
fake.spectrum.main.composite.a_1 1.84 +/- 0.11 1 / (cm2 keV s)
fake.spectrum.main.composite.b_1 (4 +/- 4) x 10^-3 1 / (cm2 keV2 s)
fake.spectrum.main.composite.F_2 (3.6 +/- 0.4) x 10 1 / (cm2 s)
fake.spectrum.main.composite.mu_2 (2.487 +/- 0.010) x 10 keV
fake.spectrum.main.composite.sigma_2 1.03 -0.09 +0.10 keV
[5]:
value negative_error positive_error error unit
fake.spectrum.main.composite.a_1 1.840082 -0.113876 0.113455 0.113665 1 / (cm2 keV s)
fake.spectrum.main.composite.b_1 0.004130 -0.003834 0.003855 0.003844 1 / (cm2 keV2 s)
fake.spectrum.main.composite.F_2 36.072569 -3.986934 3.995320 3.991127 1 / (cm2 s)
fake.spectrum.main.composite.mu_2 24.871071 -0.102293 0.100501 0.101397 keV
fake.spectrum.main.composite.sigma_2 1.031617 -0.091045 0.095699 0.093372 keV

We need to get the AnalysisResults object that is created after a fit is performed. The AR object is a member of the JointLikelihood object

[6]:
ar = jl.results

We can display the results of the analysis. Note, when a fit is performed, the post display is actaully from the internal AR.

[7]:
ar.display()
Best fit values:

result unit
parameter
fake.spectrum.main.composite.a_1 1.84 +/- 0.11 1 / (cm2 keV s)
fake.spectrum.main.composite.b_1 (4 +/- 4) x 10^-3 1 / (cm2 keV2 s)
fake.spectrum.main.composite.F_2 (3.6 +/- 0.4) x 10 1 / (cm2 s)
fake.spectrum.main.composite.mu_2 (2.487 +/- 0.010) x 10 keV
fake.spectrum.main.composite.sigma_2 1.03 +/- 0.09 keV

Correlation matrix:

1.00-0.85-0.040.01-0.10
-0.851.00-0.00-0.020.00
-0.04-0.001.000.14-0.16
0.01-0.020.141.000.10
-0.100.00-0.160.101.00

Values of -log(likelihood) at the minimum:

-log(likelihood)
sim_data 21.713302
total 21.713302

Values of statistical measures:

statistical measures
AIC 54.790240
BIC 62.986719

By default, the equal tail intervals are displayed. We can instead display highest posterior densities (equal in the MLE case)

[8]:
ar.display("hpd")
Best fit values:

result unit
parameter
fake.spectrum.main.composite.a_1 1.84 +/- 0.11 1 / (cm2 keV s)
fake.spectrum.main.composite.b_1 (4 +/- 4) x 10^-3 1 / (cm2 keV2 s)
fake.spectrum.main.composite.F_2 (3.6 +/- 0.4) x 10 1 / (cm2 s)
fake.spectrum.main.composite.mu_2 (2.487 +/- 0.010) x 10 keV
fake.spectrum.main.composite.sigma_2 1.03 +/- 0.09 keV

Correlation matrix:

1.00-0.85-0.040.01-0.10
-0.851.00-0.00-0.020.00
-0.04-0.001.000.14-0.16
0.01-0.020.141.000.10
-0.100.00-0.160.101.00

Values of -log(likelihood) at the minimum:

-log(likelihood)
sim_data 21.713302
total 21.713302

Values of statistical measures:

statistical measures
AIC 54.790240
BIC 62.986719

The AR stores several properties from the analysis:

[9]:
ar.analysis_type
[9]:
'MLE'
[10]:
ar.covariance_matrix
[10]:
array([[ 1.29193761e-02, -3.69698837e-04, -1.73782236e-02,
         1.44915086e-04, -1.03524082e-03],
       [-3.69698837e-04,  1.47775746e-05, -1.66749229e-05,
        -8.01053811e-06,  1.20949994e-06],
       [-1.73782236e-02, -1.66749229e-05,  1.59319880e+01,
         5.77892527e-02, -6.10072684e-02],
       [ 1.44915086e-04, -8.01053811e-06,  5.77892527e-02,
         1.01671267e-02,  9.48394613e-04],
       [-1.03524082e-03,  1.20949994e-06, -6.10072684e-02,
         9.48394613e-04,  8.62144657e-03]])
[11]:
ar.get_point_source_flux(1 * u.keV, 0.1 * u.MeV)
[11]:
flux low bound hi bound
fake: total 1.842162149595543e-05 erg / (cm2 s) 1.7070505364293698e-05 erg / (cm2 s) 1.9768146935594637e-05 erg / (cm2 s)
[12]:
ar.optimized_model
[12]:
Model summary:

N
Point sources 1
Extended sources 0
Particle sources 0


Free parameters (5):

value min_value max_value unit
fake.spectrum.main.composite.a_1 1.840082 -100.0 100.0 keV-1 s-1 cm-2
fake.spectrum.main.composite.b_1 0.00413 -10.0 10.0 s-1 cm-2 keV-2
fake.spectrum.main.composite.F_2 36.072569 0.001 200.0 s-1 cm-2
fake.spectrum.main.composite.mu_2 24.871071 0.0 100.0 keV
fake.spectrum.main.composite.sigma_2 1.031617 0.001 10.0 keV


Fixed parameters (2):
(abridged. Use complete=True to see all fixed parameters)


Linked parameters (0):

(none)

Independent variables:

(none)

Saving results to disk

The beauty of the analysis result is that all of this information can be written to disk and restored at a later time. The statistical parameters, best-fit model, etc. can all be recovered.

AR’s are stored as a structured FITS file. We write the AR like this:

[13]:
ar.write_to("test_mle.fits", overwrite=True)
WARNING: VerifyWarning: Card is too long, comment will be truncated. [astropy.io.fits.card]

The FITS file can be examines with any normal FITS reader.

[14]:
import astropy.io.fits as fits
[15]:
ar_fits = fits.open("test_mle.fits")
ar_fits.info()
Filename: test_mle.fits
No.    Name      Ver    Type      Cards   Dimensions   Format
  0  PRIMARY       1 PrimaryHDU       6   ()
  1  ANALYSIS_RESULTS    1 BinTableHDU     36   5R x 8C   [36A, D, D, D, D, 16A, 5D, D]

However, to easily pull the results back into the 3ML framework, we use the \({\tt load\_analysis\_results}\) function:

[16]:
ar_reloaded = load_analysis_results("test_mle.fits")
[17]:
ar_reloaded.get_statistic_frame()
[17]:
-log(likelihood)
sim_data 21.713302
total 21.713302

You can get a DataFrame with the saved results:

[18]:
ar_reloaded.get_data_frame()
[18]:
value negative_error positive_error error unit
fake.spectrum.main.composite.a_1 1.840082 -0.110500 0.116179 0.113340 1 / (cm2 keV s)
fake.spectrum.main.composite.b_1 0.004130 -0.003948 0.003749 0.003849 1 / (cm2 keV2 s)
fake.spectrum.main.composite.F_2 36.072569 -3.899603 3.949046 3.924325 1 / (cm2 s)
fake.spectrum.main.composite.mu_2 24.871071 -0.101014 0.100074 0.100544 keV
fake.spectrum.main.composite.sigma_2 1.031617 -0.092561 0.092997 0.092779 keV

Analysis Result Sets

When doing time-resolved analysis or analysing a several objects, we can save several AR’s is a set. This is achieved with the analysis result set. We can pass an array of AR’s to the set and even set up descriptions for the different entries.

[19]:
from threeML.analysis_results import AnalysisResultsSet

analysis_set = AnalysisResultsSet([ar, ar_reloaded])

# index as time bins
analysis_set.set_bins("testing", [-1, 1], [3, 5], unit="s")

# write to disk
analysis_set.write_to("analysis_set_test.fits", overwrite=True)
WARNING: VerifyWarning: Card is too long, comment will be truncated. [astropy.io.fits.card]
[20]:
analysis_set = load_analysis_results("analysis_set_test.fits")
[21]:
analysis_set[0].display()
Best fit values:

result unit
parameter
fake.spectrum.main.composite.a_1 1.84 +/- 0.11 1 / (cm2 keV s)
fake.spectrum.main.composite.b_1 (4 +/- 4) x 10^-3 1 / (cm2 keV2 s)
fake.spectrum.main.composite.F_2 (3.6 +/- 0.4) x 10 1 / (cm2 s)
fake.spectrum.main.composite.mu_2 (2.487 +/- 0.010) x 10 keV
fake.spectrum.main.composite.sigma_2 1.03 +/- 0.09 keV

Correlation matrix:

1.00-0.85-0.040.01-0.10
-0.851.00-0.00-0.020.00
-0.04-0.001.000.14-0.16
0.01-0.020.141.000.10
-0.100.00-0.160.101.00

Values of -log(likelihood) at the minimum:

-log(likelihood)
sim_data 21.713302
total 21.713302

Values of statistical measures:

statistical measures
AIC 54.790240
BIC 62.986719

Error propagation

In 3ML, we propagate errors for MLE reults via sampling of the covariance matrix instead of Taylor exanding around the maximum of the likelihood and computing a jacobain. Thus, we can achieve non-linear error propagation.

You can use the results for propagating errors non-linearly for analytical functions:

[22]:
p1 = ar.get_variates("fake.spectrum.main.composite.b_1")
p2 = ar.get_variates("fake.spectrum.main.composite.a_1")

print("Propagating a+b, with a and b respectively:")
print(p1)
print(p2)

print("\nThis is the result (with errors):")
res = p1 + p2
print(res)

print(res.equal_tail_interval())
Propagating a+b, with a and b respectively:
equal-tail: (4 +/- 4) x 10^-3, hpd: (4 +/- 4) x 10^-3
equal-tail: 1.84 -0.12 +0.11, hpd: 1.84 -0.13 +0.10

This is the result (with errors):
equal-tail: 1.84 +/- 0.11, hpd: 1.84 -0.13 +0.09
(1.7318664659471747, 1.953129399961454)

The propagation accounts for covariances. For example this has error of zero (of course) since there is perfect covariance.

[23]:
print("\nThis is 50 * a/a:")
print(50 * p1 / p1)

This is 50 * a/a:
equal-tail: (5.0 +/- 0) x 10, hpd: (5.0 +/- 0) x 10

You can use arbitrary (np) functions

[24]:
print("\nThis is arcsinh(b + 5*) / np.log10(b) (why not?)")
print(np.arcsinh(p1 + 5 * p2) / np.log10(p2))

This is arcsinh(b + 5*) / np.log10(b) (why not?)
equal-tail: (1.10 -0.08 +0.10) x 10, hpd: (1.10 -0.09 +0.08) x 10

Errors can become asymmetric. For example, the ratio of two gaussians is asymmetric notoriously:

[25]:
print("\nRatio a/b:")
print(p2 / p1)

Ratio a/b:
equal-tail: (3.3 -1.9 +5) x 10^2, hpd: (3.3 -2.3 +4) x 10^2

You can always use it with arbitrary functions:

[26]:
def my_function(x, a, b):

    return b * x ** a


print("\nPropagating using a custom function:")
print(my_function(2.3, p1, p2))

Propagating using a custom function:
equal-tail: 1.85 +/- 0.11, hpd: 1.85 +/- 0.11

This is an example of an error propagation to get the plot of the model with its errors (which are propagated without assuming linearity on parameters)

[27]:
def go(fitfun, ar, model):

    fig, ax = plt.subplots()

    # Gather the parameter variates

    arguments = {}

    for par in fitfun.parameters.values():

        if par.free:

            this_name = par.name

            this_variate = ar.get_variates(par.path)

            # Do not use more than 1000 values (would make computation too slow for nothing)

            if len(this_variate) > 1000:

                this_variate = np.random.choice(this_variate, size=1000)

            arguments[this_name] = this_variate

    # Prepare the error propagator function

    pp = ar.propagate(
        ar.optimized_model.fake.spectrum.main.shape.evaluate_at, **arguments
    )

    # You can just use it as:

    print(pp(5.0))

    # Make the plot

    energies = np.linspace(0, 50, 100)

    low_curve = np.zeros_like(energies)
    middle_curve = np.zeros_like(energies)
    hi_curve = np.zeros_like(energies)

    free_parameters = model.free_parameters

    p = tqdm(total=len(energies), desc="Propagating errors")

    with use_astromodels_memoization(False):
        for i, e in enumerate(energies):
            this_flux = pp(e)

            low_bound, hi_bound = this_flux.equal_tail_interval()

            low_curve[i], middle_curve[i], hi_curve[i] = (
                low_bound,
                this_flux.median,
                hi_bound,
            )

            p.update(1)

    ax.plot(energies, middle_curve, "--", color="black")
    ax.fill_between(energies, low_curve, hi_curve, alpha=0.5, color="blue")
[28]:
go(fitfun, ar, model)
equal-tail: 1.86 +/- 0.12, hpd: 1.86 -0.12 +0.11
../_images/notebooks_Analysis_results_showcase_47_2.png

Bayesian Analysis Results

Analysis Results work exactly the same under Bayesian analysis.

Let’s run the analysis first.

[29]:

for parameter in ar.optimized_model:

    model[parameter.path].value = parameter.value

model.fake.spectrum.main.composite.a_1.set_uninformative_prior(Uniform_prior)
model.fake.spectrum.main.composite.b_1.set_uninformative_prior(Uniform_prior)
model.fake.spectrum.main.composite.F_2.set_uninformative_prior(Log_uniform_prior)
model.fake.spectrum.main.composite.mu_2.set_uninformative_prior(Uniform_prior)
model.fake.spectrum.main.composite.sigma_2.set_uninformative_prior(Log_uniform_prior)

bs = BayesianAnalysis(model, data)
bs.set_sampler("emcee")
bs.sampler.setup(n_iterations=1000, n_burn_in=100, n_walkers=20)
samples = bs.sample()
Maximum a posteriori probability (MAP) point:

result unit
parameter
fake.spectrum.main.composite.a_1 1.85 -0.11 +0.12 1 / (cm2 keV s)
fake.spectrum.main.composite.b_1 (4.4 +/- 3.5) x 10^-3 1 / (cm2 keV2 s)
fake.spectrum.main.composite.F_2 (3.0 -0.4 +0.8) x 10 1 / (cm2 s)
fake.spectrum.main.composite.mu_2 (2.16 +0.30 +0.33) x 10 keV
fake.spectrum.main.composite.sigma_2 (9.1 -0.7 +2.1) x 10^-1 keV

Values of -log(posterior) at the minimum:

-log(posterior)
sim_data -23.328875
total -23.328875

Values of statistical measures:

statistical measures
AIC 58.021386
BIC 66.217864
DIC -1324.562763
PDIC -1390.776554

Again, we grab the results from the BayesianAnalysis object:

[30]:
ar2 = bs.results

We can write and read the results to/from a file:

[31]:
ar2.write_to("test_bayes.fits", overwrite=True)
WARNING: VerifyWarning: Card is too long, comment will be truncated. [astropy.io.fits.card]
[32]:
ar2_reloaded = load_analysis_results("test_bayes.fits")

The AR holds the posterior samples from the analysis. We can see the saved and live reults are the same:

[33]:
np.allclose(ar2_reloaded.samples, ar2.samples)
[33]:
True

NOTE: MLE AR’s store samples as well. These are the samples from the covariance matrix

We can examine the marginal distributions of the parameters:

[34]:
fig = ar2.corner_plot()
WARNING:root:Too few points to create valid contours
WARNING:root:Too few points to create valid contours
../_images/notebooks_Analysis_results_showcase_58_1.png

We can return pandas DataFrames with equal tail or HPD results.

[35]:
ar2.get_data_frame("equal tail")
[35]:
value negative_error positive_error error unit
fake.spectrum.main.composite.a_1 1.849097 -0.114337 0.117314 0.115826 1 / (cm2 keV s)
fake.spectrum.main.composite.b_1 0.004436 -0.003470 0.003456 0.003463 1 / (cm2 keV2 s)
fake.spectrum.main.composite.F_2 30.103833 -4.216555 8.041789 6.129172 1 / (cm2 s)
fake.spectrum.main.composite.mu_2 21.597592 2.996411 3.346791 3.171601 keV
fake.spectrum.main.composite.sigma_2 0.914750 -0.074593 0.207847 0.141220 keV
[36]:
ar2.get_data_frame("hpd")
[36]:
value negative_error positive_error error unit
fake.spectrum.main.composite.a_1 1.849097 -0.126893 0.101776 0.114335 1 / (cm2 keV s)
fake.spectrum.main.composite.b_1 0.004436 -0.003612 0.003285 0.003449 1 / (cm2 keV2 s)
fake.spectrum.main.composite.F_2 30.103833 -0.484722 9.424861 4.954791 1 / (cm2 s)
fake.spectrum.main.composite.mu_2 21.597592 3.110023 3.381043 3.245533 keV
fake.spectrum.main.composite.sigma_2 0.914750 -0.004134 0.244438 0.124286 keV

Error propagation operates the same way. Internally, the process is the same as the MLE results, however, the samples are those of the posterior rather than the (assumed) covariance matrix.

[37]:
p1 = ar2.get_variates("fake.spectrum.main.composite.b_1")
p2 = ar2.get_variates("fake.spectrum.main.composite.a_1")

print(p1)
print(p2)

res = p1 + p2

print(res)
equal-tail: (4.3 -3.4 +3.5) x 10^-3, hpd: (4.3 -3.5 +3.4) x 10^-3
equal-tail: 1.85 -0.11 +0.12, hpd: 1.85 -0.13 +0.10
equal-tail: 1.85 -0.11 +0.12, hpd: 1.85 -0.13 +0.10

To demonstrate how the two objects (MLE and Bayes) are the same, we see that our plotting function written for the MLE result works on our Bayesian results seamlessly.

[38]:
go(fitfun, ar2, model)
equal-tail: 1.88 -0.12 +0.13, hpd: 1.88 -0.15 +0.09
../_images/notebooks_Analysis_results_showcase_65_2.png
[ ]: