prospect.fitting#
- prospect.fitting.fit_model(obs, model, sps, noise=(None, None), lnprobfn=<function lnprobfn>, optimize=False, emcee=False, dynesty=True, **kwargs)#
Fit a model to observations using a number of different methods
- Parameters:
obs – The
obs
dictionary containing the data to fit to, which will be passed tolnprobfn
.model – An instance of the
prospect.models.SedModel
class containing the model parameterization and parameter state. It will be passed tolnprobfn
.sps – An instance of a
prospect.sources.SSPBasis
(sub-)class. Alternatively, anything with a compatibleget_spectrum()
can be used here. It will be passed tolnprobfn
noise – (optional, default: (None, None)) A tuple of NoiseModel objects for the spectroscopy and photometry respectively. Can also be (None, None) in which case simple chi-square will be used.
lnprobfn – (optional, default: lnprobfn) A posterior probability function that can take
obs
,model
,sps
, andnoise
as keywords. By default use thelnprobfn()
defined above.optimize –
(optional, default: False) If
True
, conduct a round of optimization before sampling from the posterior. The model state will be set to the best value at the end of optimization before continuing on to sampling or returning. Parameters controlling the optimization can be passed viakwargs
, includingmin_method
: ‘lm’ | ‘powell’nmin
: number of minimizations to do. Beyond the first, minimizations will be started from draws from the prior.min_opts
: dictionary of minimization options passed to the scipy.optimize.minimize method.
See
run_minimize()
for details.emcee –
(optional, default: False) If
True
, sample from the posterior using emcee. Additonal parameters controlling emcee can be passed via**kwargs
. These includeinitial_positions
: A set of initial positions for the walkershfile
: an open h5py.File file handle for writing result incrementally
Many additional emcee parameters can be provided here, see
run_emcee()
for details.dynesty – If
True
, sample from the posterior using dynesty. Additonal parameters controlling dynesty can be passed via**kwargs
. Seerun_dynesty()
for details.
- Returns output:
A dictionary with two keys,
"optimization"
and"sampling"
. The value of each of these is a 2-tuple with results in the first element and durations (in seconds) in the second element.
- prospect.fitting.lnprobfn(theta, model=None, obs=None, sps=None, noise=(None, None), residuals=False, nested=False, negative=False, verbose=False)#
Given a parameter vector and optionally a dictionary of observational ata and a model object, return the matural log of the posterior. This requires that an sps object (and if using spectra and gaussian processes, a NoiseModel) be instantiated.
- Parameters:
theta – Input parameter vector, ndarray of shape (ndim,)
model – SedModel model object, with attributes including
params
, a dictionary of model parameter state. It must also haveprior_product()
, andpredict()
methods defined.obs –
A dictionary of observational data. The keys should be
"wavelength"
(angstroms)"spectrum"
(maggies)"unc"
(maggies)"maggies"
(photometry in maggies)"maggies_unc"
(photometry uncertainty in maggies)"filters"
(sedpy.observate.FilterSet
or iterable ofsedpy.observate.Filter
)and optional spectroscopic
"mask"
and"phot_mask"
(same length asspectrum
andmaggies
respectively, True means use the data points)
sps – A
prospect.sources.SSPBasis
object or subclass thereof, or any object with aget_spectrum
method that will take a dictionary of model parameters and return a spectrum, photometry, and ancillary information.noise – (optional, default: (None, None)) A 2-element tuple of
prospect.likelihood.NoiseModel
objects.residuals – (optional, default: False) A switch to allow vectors of \(\chi\) values to be returned instead of a scalar posterior probability. This can be useful for least-squares optimization methods. Note that prior probabilities are not included in this calculation.
nested – (optional, default: False) If
True
, do not add the ln-prior probability to the ln-likelihood when computing the ln-posterior. For nested sampling algorithms the prior probability is incorporated in the way samples are drawn, so should not be included here.negative – (optiona, default: False) If
True
return the negative on the ln-probability for minimization purposes.
- Returns lnp:
Ln-probability, unless
residuals=True
in which case a vector of \(\chi\) values is returned.