Skip to content

Fitting

fit = runner.fit()

fit() samples the posterior of the model parameters given the prepared image, the PSF, the priors and the noise model, and writes the results to fits/<model>/ in the run directory. This page covers the choices that define a fit: the model, the priors and the sampler.

The model

fit:
  models: [single_sersic]

fit.models lists the morphology models to fit; each is fitted separately, in its own directory. With several models, fit() returns a dictionary of fit products by model name. The available models are:

Model Describes
single_sersic one Sérsic profile: the baseline description of a galaxy
double_sersic two Sérsic profiles with a common centre
sersic_point_source a Sérsic profile and an unresolved nucleus
bulge_disk a Sérsic bulge and an exponential disc
boxy_sersic a Sérsic profile with boxy or discy isophotes
core_sersic a Sérsic profile with a depleted core
bulge_broken_disk a bulge and a disc whose slope changes at a break radius
bulge_disk_bar a bulge, a disc and a bar
bulge_disk_ring a bulge, a disc and a ring
bulge_disk_spiral a bulge and a disc with two spiral arms

Each page of the Models section lists the parameters of the model, their units and their default priors.

The priors

Every sampled parameter has a prior. The defaults are wide uniform ranges in pixels, except for the fluxes, which have none and must always be declared:

fit:
  priors:
    single_sersic:
      flux: {type: log_uniform, lower: 1.0e3, upper: 1.0e7}
      re: {type: uniform, lower: 2, upper: 60}       # pixels
      n: {type: uniform, lower: 0.5, upper: 6}
      theta: {type: uniform, lower: -pi/2, upper: pi/2}
      x0: {type: uniform, lower: -5, upper: 5, relative_to: target_x}
      y0: {type: uniform, lower: -5, upper: 5, relative_to: target_y}
Type Keys
uniform lower, upper
log_uniform lower, upper; uniform in the logarithm, for a scale such as a flux or a radius spanning decades
normal mean, sigma
beta alpha, beta, and optionally lower and upper

Angles can be written as pi, pi/2, -pi/2 or -pi. relative_to: target_x or target_y centres a uniform prior on the target position, so x0 above may lie within 5 pixels of the position given in data.target_position. The fit stops before it writes anything when the centre that the preparation measures on the target lies outside the priors of x0 and y0. Parameters not listed keep their defaults, which every model page gives.

The residual background has its own priors, under fit.background: see Sky and Noise.

The noise model

fit:
  likelihood:
    type: correlated_gaussian
    effective_gain:
      source: measured_sky

The likelihood is a Gaussian that describes the noise of the sky, the Poisson noise of the galaxy and the correlation of neighbouring sky pixels. The only choice it asks of you is the effective gain, explained in Sky and Noise.

The sampler

fit:
  sampler:
    name: nautilus          # nautilus, dynesty or pocomc
    execution:
      processes: auto       # number of parallel processes
    init:                   # options of the sampler, with their native names
      n_live: 2000
      seed: 42
      filepath: auto        # keep a checkpoint, so an interrupted fit resumes
    run:
      n_eff: 10000
Sampler What it is
nautilus (default) Nautilus, nested sampling boosted by neural networks
dynesty Dynesty, static or, with mode: dynamic, dynamic nested sampling
pocomc pocoMC, preconditioned Monte Carlo with normalizing flows

The options under init and run are passed unchanged to the sampler, with the names and defaults of its own documentation: for Nautilus, n_live live points, 2000 by default, and n_eff, the effective number of posterior samples at which it stops, 10000 by default. The one exception is Nautilus's discard_exploration, which BayMorph sets to true unless you declare it. BayMorph owns the likelihood, the priors and the parameters, which cannot be overridden there, and execution owns the parallel pool and the progress output. processes: auto uses the available processors minus two, at most ten. A seed makes the run reproducible.

pocoMC accepts only uniform, normal and beta priors: a fit with a log_uniform prior runs with Nautilus or Dynesty.

All three samplers return the same result: weighted posterior samples and the Bayesian evidence. The choice of sampler changes how the posterior is computed, never the scientific problem. Nested Sampling explains how they work.

How long a fit takes

The cost grows with the number of pixels in the cutout and with the number of parameters. On an Apple M2 Pro with ten processes, the simulated example of the Quick Start takes about a minute and a half, and a single Sérsic fit of a 251 x 251 px cutout with an empirical PSF took about four minutes with n_live: 300. A first look with fewer live points is a good way to check a configuration before a full run.

Comparing models

The evidence of each fit measures how well its model explains the image, penalized by the volume of its prior. Two models fitted to the same prepared image, with the same mask, PSF and noise model, can be compared by the difference of their log-evidences, the logarithm of the Bayes factor. With fit.comparison.enabled: true, which requires exactly fit.models: [single_sersic, double_sersic], BayMorph computes that Bayes factor and writes it to model_comparison/bayes_factor.json; other models are compared from the log-evidences of their fits. BayMorph ranks models; it never picks one for you. Bayesian Inference explains how to read a Bayes factor.