BXA/Sherpa

Define your background model and source model as usual in sherpa. Freeze the parameters you do not want to investigate. Make sure you set the parameter minimum and maximum values to appropriate (a priori reasonable) values.

Defining priors

# you can use automatic priors (uniform priors everywhere, within the parameters range)
# for this, all scale parameters (those with ampl or norm in their name)
# are converted to log-parameters
parameters = auto_reparametrize()
prior = bxa.create_prior_function(parameters)

Alternatively (advanced), define priors manually:

# get parameters
parameters = [param1, param2, param3]
# or just get all that are not linked or frozen
parameters = [for p in get_model().pars if not p.frozen and p.link is None]

priors = []
priors += [bxa.create_jeffreys_prior_for(param1)]
priors += [bxa.create_uniform_prior_for(param2)]
priors += [lambda x: x**2] # custom prior transformation (rarely desired)
priorfunction = bxa.create_prior_function(priors = priors)
bxa.sherpa.create_jeffreys_prior_for(parameter)

Use for scale variables (order of magnitude) The Jeffreys prior gives equal weight to each order of magnitude between the minimum and maximum value. Flat in logarithmic scale.

Parameters:parameter – Parameter to create a prior for. E.g. xspowerlaw.mypowerlaw.norm

It is usually easier to create an ancillary parameter, and link the actual parameter, like so:

from sherpa.models.parameter import Parameter
lognorm = Parameter(modelname='mycomponent', name='lognorm', val=-5, min=-4*2, max=0)
powerlaw.norm = 10**lognorm
bxa.sherpa.create_uniform_prior_for(parameter)

Use for location variables (position) The uniform prior gives equal weight in non-logarithmic scale.

Parameters:parameter – Parameter to create a prior for. E.g. xspowerlaw.mypowerlaw.PhoIndex
bxa.sherpa.create_prior_function(priors=, []parameters=None)

Combine the prior transformations into a single function for pymultinest.

Parameters:
  • priors – individual prior transforms to combine into one function. If priors is empty, uniform priors are used on all passed parameters
  • parameters – If priors is empty, specify the list of parameters. Uniform priors will be created for them.

Running the analysis

You need to specify a prefix, called outputfiles_basename where the files are stored.

# see the pymultinest documentation for all options
bxa.nested_run(prior = priorfunction, parameters = parameters,
             resume = True, verbose = True,
             outputfiles_basename = "testbxa_")
bxa.sherpa.nested_run(id=None, otherids=(), prior=None, parameters=None, sampling_efficiency=0.3, evidence_tolerance=0.5, n_live_points=400, outputfiles_basename='chains/', **kwargs)[source]

Run the Bayesian analysis with specified parameters+transformations.

Parameters:
  • id – See the sherpa documentation of calc_stat.
  • otherids – See the sherpa documentation of calc_stat.
  • prior – prior function created with create_prior_function.
  • parameters – List of parameters to analyse.
  • outputfiles_basename – prefix for output filenames.

If prior is None, uniform priors are used on the passed parameters. If parameters is also None, all thawed parameters are used.

The remainder are multinest arguments (see PyMultiNest and MultiNest documentation!) n_live_points: 400 are often enough

For quick results, use sampling_efficiency = 0.8, n_live_points = 50, evidence_tolerance = 5. The real results must be estimated with sampling_efficiency = 0.3, otherwise it is not reliable.

bxa.sherpa.set_best_fit(id=None, otherids=(), parameters=None, outputfiles_basename='chains/')[source]

Sets model to the best fit values.

Marginal plots

Plot and analyse the results. PyMultiNest comes in handy at this point to produce a number of plots and summaries.

On the shell, run:

$ multinest_marginals.py "testbxa_"

The multinest_marginals.py utility is installed with PyMultiNest, for instance into ~/.local/bin/.

Error propagation

pymultinest.Analyzer.equal_weighted_posterior() provides access to the posterior samples (similar to a Markov Chain). Use these to propagate errors:

  • For every row in the chain, compute the quantity of interest
  • Then, make a histogram of the results, or compute mean and standard deviations.

This preserves the structure of the uncertainty (multiple modes, degeneracies, etc.)

You can also access the output directly and compute other quantities:

import pymultinest
analyzer = pymultinest.analyse.Analyzer(n_params = len(parameters),
        outputfiles_basename = 'testbxa_')

chain = analyzer.get_equal_weighted_posterior()

print chain

BXA also allows you to compute the fluxes corresponding to the parameter estimation, giving the correct probability distribution on the flux. With distance information (fixed value or distribution), you can later infer the correct luminosity distribution.

dist = pyblocxs.mn.get_distribution_with_fluxes(lo=2, hi=10,
        parameters = parameters, outputfiles_basename = 'testbxa_')
numpy.savetxt(out + prefix + "dist.txt", dist)
bxa.sherpa.get_distribution_with_fluxes(id=None, otherids=(), lo=None, hi=None, parameters=None, outputfiles_basename='chains/')[source]

Returns an array of equally weighted posterior samples (parameter values) with two additional columns: the photon fluxes and the energy fluxes.

The values will be correctly distributed according to the analysis run before.

Model comparison

examples/model_compare.py shows an example of model selection. Keep in mind what model prior you would like to use.

  • Case 1: Multiple models, want to find one best one to use from there on:
    • follow examples/model_compare.py, and pick the model with the highest evidence
  • Case 2: Simpler and more complex models, want to find out which complexity is justified:
    • follow examples/model_compare.py, and keep the models above a certain threshold
  • Case 3: Multiple models which could be correct, only interested in a parameter
    • Marginalize over the models: Use the posterior samples from each model, and weigh them by the relative probability of the models (weight = exp(lnZ))

Example output:

jbuchner@ds42 $ python model_compare.py absorbed- line- simplest-

Model comparison
****************

model simplest- : log10(Z) = -1632.7  XXX ruled out
model absorbed- : log10(Z) =    -7.5  XXX ruled out
model line-     : log10(Z) =     0.0    <-- GOOD

The last, most likely model was used as normalization.
Uniform model priors are assumed, with a cut of log10(30) to rule out models.

jbuchner@ds42 $

Here, the probability of the second-best model, “absorbed”, is \(10^7.5\) times less likely than the model “line”. As this exceeds our threshold (by a lot!) we can claim the detection of an iron line!

Experiment design

We want to to evaluate whether a planned experiment can detect features or constrain parameters, i.e. determine the discriminatory power of future configurations/surveys/missions.

For this, simulate a few spectra using the appropriate response.

  • Case 1: Can the experiment constrain the parameters?
    • Analyse and check what fraction of the posterior samples lie inside/outside the region of interest.
  • Case 2: Can the experiment distinguish between two models?
    • Model selection as above.
  • Case 3: Which sources (redshift range, luminosity, etc) can be distinguished?
    • Compute a grid of spectra. Do model selection at each point in the grid.

Model discovery

Is the model the right one? Is there more in the data? These questions can not be answered in a statistical way, but what we can do is

  1. generate ideas on what models could fit better
  2. test those models for significance with model selection

For the first point, Quantile-Quantile plots provide a unbinned, less noisy alternative to residual plots.

_images/absorbed-qq_model_deviations.png _images/absorbed-convolved_posterior.png

QQ plot example (left), with the corresponding spectrum for comparison (right).

In these plots, for each energy the number of counts observed with lower energy are plotted on one axis, while the predicted are on the other axis. If model and data agree perfectly, this would be a straight line. Deviances are indications of possible mis-fits.

This example is almost a perfect fit! You can see a offset growing at 6-7 keV, which remains at higher energies. This indicates that the data has more counts than the model there.

As the growth is in a S-shape, it is probably a Gaussian (see its cumulative density function).

Refer to the appendix of the accompaning paper for more examples.

The qq function in the qq module allows you to create such plots easily, by exporting the cumulative functions into a file.

bxa.sherpa.qq.qq_export(id=None, bkg=False, outfile='qq.txt', elow=None, ehigh=None)[source]

Export Q-Q plot into a file for plotting.

Parameters:
  • id – spectrum id to use (see get_bkg_plot/get_data_plot)
  • bkg – whether to use get_bkg_plot or get_data_plot
  • outfile – filename to write results into
  • elow – low energy limit
  • ehigh – low energy limit

Example:

qq.qq_export('bg', outfile='my_bg_qq', elow=0.2, ehigh=10)

Refer to the accompaning paper, which gives an introduction and detailed discussion on the methodology.

Table Of Contents

Previous topic

Welcome to BXA’s documentation!

Next topic

BXA/Xspec

This Page