# Bumps 0.7.6 documentation

## fitproblem - Interface between models and fitters

`Fitness` | Manage parameters, data, and theory function evaluation.
`FitProblem` | Return a fit problem instance for the fitness function(s).
`load_problem` | Load a problem definition from a python script file.
`BaseFitProblem` | See `FitProblem()`
`MultiFitProblem` | Weighted fits for multiple models.

Interface between the models and the fitters.

`Fitness` defines the interface that model evaluators can follow. These models can be bundled together into a `FitProblem()` and sent to `bumps.fitters.FitDriver` for optimization and uncertainty analysis.

class `bumps.fitproblem.``Fitness`

  Bases: `object`

  Manage parameters, data, and theory function evaluation.

  See Complex models for a detailed explanation.

  `nllf`()

    Return the negative log likelihood value of the current parameter set.

  `numpoints`()

    Return the number of data points.

  `parameters`()

    Return the parameters in the model.

    Model parameters are a hierarchical structure of lists and dictionaries.

  `plot`(view='linear')

    Plot the model to the current figure. You only get one figure, but you can make it as complex as you want. This will be saved as a png on the server, and composed onto a results web page.

  `residuals`()

    Return residuals for current theory minus data.

    Used for Levenburg-Marquardt, and for plotting.

  `restore_data`()

    Restore the original data in the model (after resynth).

  `resynth_data`()

    Generate fake data based on uncertainties in the real data. For Monte Carlo resynth-refit uncertainty analysis. Bootstrapping?

  `save`(basename)

    Save the model to a file based on basename+extension. This will point to a path to a directory on a remote machine; don’t make any assumptions about information stored on the server. Return the set of files saved so that the monitor software can make a pretty web page.

  `update`()

    Called when parameters have been updated. Any cached values will need to be cleared and the model reevaluated.

`bumps.fitproblem.``FitProblem`(*args, **kw)

  Return a fit problem instance for the fitness function(s).

  For an individual model:

  > fitness is a `Fitness` instance.

  For a set of models:

  > models is a sequence of `Fitness` instances.

  > weights is an optional scale factor for each model

  > freevars is `parameter.FreeVariables` instance defining the per-model parameter assignments. See Free Variables for details.

  Additional parameters:

  > name name of the problem

  > constraints is a function which returns the negative log likelihood of seeing the parameters independent from the fitness function. Use this for example to check for feasible regions of the search space, or to add constraints that cannot be easily calculated per parameter. Ideally, the constraints nllf will increase as you go farther from the feasible region so that the fit will be directed toward feasible values.

  > soft_limit is the constraints function cutoff, beyond which the penalty_nllf will be used and fitness nllf will not be calculated.

  > penalty_nllf is the nllf to use for fitness when constraints is greater than soft_limit.

  Total nllf is the sum of the parameter nllf, the constraints nllf and the depending on whether constraints is greater than soft_limit, either the fitness nllf or the penalty nllf.

`bumps.fitproblem.``load_problem`(filename, options=None)

  Load a problem definition from a python script file.

  sys.argv is set to `[file] + options` within the context of the script.

  The user must define `problem=FitProblem(...)` within the script.

  Raises ValueError if the script does not define problem.

class `bumps.fitproblem.``BaseFitProblem`(fitness, name=None, constraints=<function no_constraints>, penalty_nllf=1000000.0, soft_limit=inf, partial=False)

  Bases: `object`

  See `FitProblem()`

  `bounds`()
  `chisq`()

    Return sum squared residuals normalized by the degrees of freedom.

    In the context of a composite fit, the reduced chisq on the individual models only considers the points and the fitted parameters within the individual model.

    Note that this does not include cost factors due to constraints on the parameters, such as sample_offset ~ N(0,0.01).

  `chisq_str`()
  `constraints_nllf`()

    Returns the cost of all constraints.

  `cov`()
  `getp`()

    Returns the current value of the parameter vector.

  `labels`()
  `model_nllf`()

    Negative log likelihood of seeing data given model.

  `model_parameters`()

    Parameters associated with the model.

  `model_points`()

    Number of data points associated with the model.

  `model_reset`()

    Prepare for the fit.

    This sets the parameters and the bounds properties that the solver is expecting from the fittable object. We also compute the degrees of freedom so that we can return a normalized fit likelihood.

    If the set of fit parameters changes, then model_reset must be called.

  `model_update`()

    Update the model according to the changed parameters.

  `nllf`(pvec=None)

    Compute the cost function for a new parameter set p.

    Note that this is not simply the sum-squared residuals, but instead is the negative log likelihood of seeing the data given the model plus the negative log likelihood of seeing the model. The individual likelihoods are scaled by 1/max(P) so that normalization constants can be ignored.

    The model is not actually calculated if the parameter nllf plus the constraint nllf are bigger than soft_limit, but instead it is assigned a value of penalty_nllf.

  `parameter_nllf`()

    Returns negative log likelihood of seeing parameters p.

  `parameter_residuals`()

    Returns negative log likelihood of seeing parameters p.

  `plot`(p=None, fignum=None, figfile=None, view=None)
  `randomize`(n=None)

    Generates a random model.

    randomize() sets the model to a random value.

    randomize(n) returns a population of n random models.

    For indefinite bounds, the random population distribution is centered on initial value of the parameter, or 1. if the initial parameter is not finite.

  `residuals`()

    Return the model residuals.

  `restore_data`()

    Restore original data after resynthesis.

  `resynth_data`()

    Resynthesize data with noise from the uncertainty estimates.

  `save`(basename)
  `setp`(pvec)

    Set a new value for the parameters into the model. If the model is valid, calls model_update to signal that the model should be recalculated.

    Returns True if the value is valid and the parameters were set, otherwise returns False.

  `show`()
  `simulate_data`(noise=None)

    Simulate data with added noise

  `stderr`()
  `summarize`()
  `valid`(pvec)

class `bumps.fitproblem.``MultiFitProblem`(models, weights=None, name=None, constraints=<function no_constraints>, soft_limit=inf, penalty_nllf=1000000.0, freevars=None)

  Bases: `bumps.fitproblem.BaseFitProblem`

  Weighted fits for multiple models.

  `bounds`()
  `chisq`()

    Return sum squared residuals normalized by the degrees of freedom.

    In the context of a composite fit, the reduced chisq on the individual models only considers the points and the fitted parameters within the individual model.

    Note that this does not include cost factors due to constraints on the parameters, such as sample_offset ~ N(0,0.01).

  `chisq_str`()
  `constraints_nllf`()

    Return the cost function for all constraints

  `cov`()
  `getp`()

    Returns the current value of the parameter vector.

  `labels`()
  `model_nllf`()

    Return cost function for all data sets

  `model_parameters`()

    Return parameters from all models

  `model_points`()

    Return number of points in all models

  `model_reset`()

    Prepare for the fit.

    This sets the parameters and the bounds properties that the solver is expecting from the fittable object. We also compute the degrees of freedom so that we can return a normalized fit likelihood.

    If the set of fit parameters changes, then model_reset must be called.

  `model_update`()

    Let all models know they need to be recalculated

  `models`

    Iterate over models, with free parameters set from model values

  `nllf`(pvec=None)

    Compute the cost function for a new parameter set p.

    Note that this is not simply the sum-squared residuals, but instead is the negative log likelihood of seeing the data given the model plus the negative log likelihood of seeing the model. The individual likelihoods are scaled by 1/max(P) so that normalization constants can be ignored.

    The model is not actually calculated if the parameter nllf plus the constraint nllf are bigger than soft_limit, but instead it is assigned a value of penalty_nllf.

  `parameter_nllf`()

    Returns negative log likelihood of seeing parameters p.

  `parameter_residuals`()

    Returns negative log likelihood of seeing parameters p.

  `plot`(p=None, fignum=1, figfile=None, view=None)
  `randomize`(n=None)

    Generates a random model.

    randomize() sets the model to a random value.

    randomize(n) returns a population of n random models.

    For indefinite bounds, the random population distribution is centered on initial value of the parameter, or 1. if the initial parameter is not finite.

  `residuals`()
  `restore_data`()

    Restore original data after resynthesis.

  `resynth_data`()

    Resynthesize data with noise from the uncertainty estimates.

  `save`(basename)
  `set_active_model`(i)

    Use free parameters from model i

  `setp`(pvec)

    Set a new value for the parameters into the model. If the model is valid, calls model_update to signal that the model should be recalculated.

    Returns True if the value is valid and the parameters were set, otherwise returns False.

  `show`()
  `simulate_data`(noise=None)

    Simulate data with added noise

  `stderr`()
  `summarize`()
  `valid`(pvec)