forcepho.fitting#

Methods in the forcepho.fitting module are used to optimize or sample from the posterior for source parameters given data.

fitting.py

Routines for fitting a Scene model to data, using older CPU-based likelihood methods.

class forcepho.fitting.Result(filename=None, **kwargs)#

A simple namespace for storing information about a run.

dump_to_h5(filename, verbose=False)#

Try to save attributes of the Result() as satasets or attributes in the given HDF5 file.

fill(region, active, fixed, model, big=None, bounds=None, patchID=None, step=None, stats=None)#
Parameters:
  • region (a region.Region object) – The region defining the patch; its parameters will be added to the result.

  • active (structured ndarray) – The active sources and their starting parameters.

  • fixed (structured ndarray) – The fixed sources and their parameters.

  • model (a model.PosteriorModel object) – Must contain patch and scene attributes

  • bounds (optional) – If given, a structured ndarrray of lower and upper bounds for each parameter of each source.

  • patchID (optional) – An integer giving the unique patch ID.

  • step (optional) – If supplied, a littlemcmc NUTS step obect. this contains the covariance matrix

  • stats (optional) – If supplied, a littlemcmc stats object.

Returns:

  • qcat (structured ndarray) – A structured array of the parameter values in the last sample of the chain.

  • block_covs (ndarray of shape (N_source, N_param, N_param)) – The covariance matrices for the sampling potential, extracted as block diagonal elements of the full N_source * N_param square covariance array. Note that it is in the units of the transformed, unconstrained sampling parameters. If the prior bounds change, the covariance matrix is no longer valid (or must be retransformed)

get_map(structured=False)#

Get the highest probability sample

Returns:

ymap

Return type:

ndarray of shape (1, n_dim) by default or structured ndararry of length (n_active,)

get_sample_cat(iteration)#

Get a sample of the scene parameters from the chain, as a structured arrays with one row for each source.

Parameters:

iteration (int) – The iteration of the chain for which to produce a catalog.

Returns:

sample – The parameters of each source at the specified iteration of the chain, as a structured array.

Return type:

structured ndarray of shape (n_active,)

read_from_h5(filename)#

Read attributes of the Results() object form an HDF5 file.

forcepho.fitting.optimize_fluxes(patcher, active, return_all=False)#

Do a simple wieghted least-squares to get the maximum likelihood fluxes, conditional on source shape parameters.

Returns:

  • fluxes (list of ndarrays) – List of ndarrays of shape (n_source,) giving the maximum likelihoood fluxes of each source. The list has same length and order as patcher.bandlist

  • precisions (list of ndarrays) – List of ndarrays of shape (n_source, n_source) giving the flux precision matrix in each band (i.e. the inverse of the flux covariance matrix)

forcepho.fitting.run_lmc(model, q, n_draws, adapt=False, full=False, weight=10, warmup=[10], trace=None, z_cov=None, max_treedepth=10, discard_tuned_samples=True, progressbar=False, random_seed=3735928559)#

Use the littlemcmc barebones NUTS algorithm to sample from the posterior.

Parameters:
  • model (The model object (instance of forcepho.model.Posterior)) – This should have the methods lnprob_and_grad and transform()

  • q (ndarray of shape (ndim,)) – The starting position in the constrained parameter space q

  • n_draws (int) – Number of posterior samples

  • warmup (list of ints) – Number of iterations in each warmup-phase, used to tune the step-size.

  • z_cov (optional, ndarray of shape (ndim, ndim)) – The mass matrix, in the unconstrained parameter space z

  • full (bool, optional (default, False)) – Whether to use the full covariance matrix instead of the diagonal.

  • adapt (bool, optional (default, False)) – If true, use a diagonol potential (mass_matrix) that adapts at every iteration for the first warmup round. Otherwise the covariance matrix will have off diagonal elements and only adapt between warmup rounds.

Returns:

  • chain (ndarray of shape (n_draws, n_dim)) – The posterior samples in the parameter space q (i.e. not the sampling space z)

  • step (littlemcmc.nuts.NUTS instance) – The stepper. See step.potential._cov for the covariance (mass) matrix of the step, in z units

  • stats (list of dicts) – Statistics for each sample

forcepho.fitting.run_opt(model, q, jac=True, callback=None, **extras)#

Simple BFGS optimization using scipy.optimize