Load your data, define your background model and source model as usual. This, you have to do using calls to PyXSpec functions. Look at the file examples/example_simplest.py for an instructive example on how to do the simplest analysis.
Create a list of prior transformations like in the example above, one line for each variable. These functions will help you with that.
BXA (Bayesian Xray Analysis) for Xspec
Copyright: Johannes Buchner (C) 20132014
Use for location variables (position) The uniform prior gives equal weight in nonlogarithmic scale.
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
Pass your own prior weighting transformation
See examples/example_simplest.py for a simple example. examples/example_advanced_priors.py introduces more complex and custom priors.
A convencience method is provided for you, called standard_analysis, which does everything. You need to specify a prefix, called outputfiles_basename where the files are stored.
Default analysis which produces nice plots:
Look at the source of this function to figure out how to do the individual parts. Copy them to your scripts and adapt them to your needs.
Run the Bayesian analysis with specified parameters+transformations.
If prior is None, uniform priors are used on the passed parameters. If parameters is also None, all thawed parameters are used.
Parameters: 


The remainder are multinest arguments (see PyMultiNest and MultiNest documentation!)
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 and without using const_efficiency_mode, otherwise it is not reliable.
Both method return a pymultinest.Analyzer object, which provides access to the results.
The example examples/example_custom_run.py shows how to customize the analysis (other plots)
This will allow you to create marginal plots, qq plots, plots of the spectra, etc.
For histograms (1d and 2d) of the marginal parameter distributions, use plot.marginal_plots.
Plotting of posterior parameter marginal distributions
Create marginal plots
analyzer: A instance of pymultinest.Analyzer
are plotted. set d=2 if you want to force a 2d matrix plot
For plotting the model parameters found against the data, use these functions.
Plot unconvolved model posterior predictions.
component_names: labels to use. Set to ‘ignore’ to skip plotting a component plot_args: matplotlib.pyplot.plot arguments for each component
Plot convolved model posterior predictions. Also returns data points for plotting.
component_names: labels to use. Set to ‘ignore’ to skip plotting a component plot_args: matplotlib.pyplot.plot arguments for each component
Binning routines for plotting
Bins the data for plotting. Using the gof module, computes a Poisson goodnessoffit range, i.e. ranges where the model must lie. This is done for multiple binning sizes simultaneously.
Returns:
Refer to the standard_analysis function as an example of how to use them.
The data points are adaptively binned to contain at least 20 counts. The error bars are created by asking: which model count rate can produce this amount of counts. In a Poisson process, the inverse incomplete gamma function provides this answer. The 10%90% probability range is used.
For all intents and purposes, you can ignore the colors.
The colors are intended to aid the discovery of discrepancies, by using a custom Goodness of Fit measure. In this procedure (gof module), a tree of the bins is built, i.e. in the first layer, every 2 bins are merged, in the second, every 4 bins are merged, etc. Then, the counts in the bins are compared against with the poisson process of the model. The worst case, i.e. the least likely probability over the whole tree is considered. That is, for each bin, the lowest probability of all its merges is kept. Finally, this is multiplied by the number of nodes in the tree (as more comparisons lead to more random chances).
Then, if the probability for the bin is below \(10^{2}\), the point is marked orange, and if it reaches below \(10^{6}\), it is marked red.
It is ok to ignore the colors, this computation is not used otherwise.
pymultinest.Analyzer.equal_weighted_posterior() provides access to the posterior samples (similar to a Markov Chain). Use these to propagate errors:
This preserves the structure of the uncertainty (multiple modes, degeneracies, etc.)
Continuing in Xspec: A chain file, compatible with Xspec chain commands is written for you into <outputfiles_basename>chain.fits. In Xspec, load it using “chain load”. This should set parameters, and compute flux estimates.
examples/model_compare.py shows an example of model selection. Keep in mind what model prior you would like to use.
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 secondbest 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!
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.
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
For the first point, QuantileQuantile plots provide a unbinned, less noisy alternative to residual plots.
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 misfits.
This example is almost a perfect fit! You can see a offset growing at 67 keV, which remains at higher energies. This indicates that the data has more counts than the model there.
As the growth is in a Sshape, it is probably a Gaussian (see its cumulative density function).
Refer to the appendix of the accompaning paper for more examples.
For Xspec, the qq function in the qq module allows you to create such plots easily.
Create a quantilequantile plot for model discovery (deviations in data from model).
The current data and model is used, so call set_best_fit(analyzer, transformations) before, to get the qq plot at the best fit.
or number of equally spaced markers between minimum+maximum. * annotate: add information to the plot
Refer to the accompaning paper, which gives an introduction and detailed discussion on the methodology.