Fitting observational data: fit
This section describes fitting observational data using the fit
class. Check out the third iPython notebook example for a quick-start guide and the fourth for some more advanced options.
API documentation: fit
- class bagpipes.fit(galaxy, fit_instructions, run='.', time_calls=False, n_posterior=500)
Top-level class for fitting models to observational data.
Interfaces with MultiNest or nautilus to sample from the posterior distribution of a fitted_model object. Performs loading and saving of results.
- Parameters:
galaxy (bagpipes.galaxy) – A galaxy object containing the photomeric and/or spectroscopic data you wish to fit.
fit_instructions (dict) – A dictionary containing instructions on the kind of model which should be fitted to the data.
run (string - optional) – The subfolder into which outputs will be saved, useful e.g. for fitting more than one model configuration to the same data.
time_calls (bool - optional) – Whether to print information on the average time taken for likelihood calls.
n_posterior (int - optional) – How many equally weighted samples should be generated from the posterior once fitting is complete. Default is 500.
- fit(verbose=False, n_live=400, use_MPI=True, sampler='multinest', n_eff=0, discard_exploration=False, n_networks=4, pool=1)
Fit the specified model to the input galaxy data.
- Parameters:
verbose (bool - optional) – Set to True to get progress updates from the sampler.
n_live (int - optional) – Number of live points: reducing speeds up the code but may lead to unreliable results.
sampler (string - optional) – The sampler to use. Available options are “multinest” and “nautilus”.
n_eff (float - optional) – Target minimum effective sample size. Only used by nautilus.
discard_exploration (bool - optional) – Whether to discard the exploration phase to get more accurate results. Only used by nautilus.
n_networks (int - optional) – Number of neural networks. Only used by nautilus.
pool (int - optional) – Pool size used for parallelization. Only used by nautilus. MultiNest is parallelized with MPI.
The fit_instructions dictionary
The two arguments passed to the fit
class are a galaxy
object (described in the loading observational data section) and the fit_instructions
dictionary, which contains instructions on the model to be fitted to the data.
This is very similar to the model_components dictionary, however some additional options are available so that as well as being fixed, parameters can be fitted and prior probability density functions specified. A complete guide to the fit_instructions
dictionary is provided here.
Running the sampler
The MultiNest nested sampling algorithm can be run in order to sample from the posterior distribution using the fit
method of the fit
class. Nested sampling is similar to MCMC with a few key differences, for example no initial starting parameters are necessary.
Obtaining fitting results
The main output of the code is a set of samples from the posterior probability distribution for the model parameters. The code will also calculate samples for a series of derived quantites, e.g. the living stellar mass, ongoing star-formation rate etc. Samples are stored in the fit.posterior.samples dictionary. More information is available in the third iPython notebook example.
Saved outputs
The code saves basic output quantities needed to reconstruct the fit results without re-running the sampler as a hdf5 file under pipes/posterior/<ID>.h5
. When the same fit is run again the results of the previous sampler run will be loaded by default, and you will not be able to re-fit the data. If you want to start over you’ll need to delete the saved file or change the run (see below).
Making output plots
Bagpipes can provide several standard plots. These are saved under the pipes/plots/
folder.
These can be generated with:
fit.plot_spectrum_posterior() # Shows the input and fitted spectrum/photometry
fit.plot_sfh_posterior() # Shows the fitted star-formation history
fit.plot_1d_posterior() # Shows 1d posterior probability distributions
fit.plot_corner() # Shows 1d and 2d posterior probability distributions
You may find some of the functions available under pipes.plotting helpful when generating your own custom plots
The run keyword argument
Often we will want to fit a series of different models to data, changing star-formation histories, parameter limits, priors etc. In order to quickly switch between different fitting runs without deleting output posteriors we can specify the run
keyword argument of fit
. This will cause all outputs in pipes/posterior/
and pipes/plots/
to be saved into a further subdirectory with the name passed as run
, e.g. pipes/posterior/<run>/
.