Fit#

class Fit(sim, weights=None, keys=None, custom=None, compute=True, verbose=False, die=True, label=None, **kwargs)[source]#

Bases: Analyzer

A class for calculating the fit between the model and the data. Note the following terminology is used here:

  • fit: nonspecific term for how well the model matches the data

  • difference: the absolute numerical differences between the model and the data (one time series per result)

  • goodness-of-fit: the result of passing the difference through a statistical function, such as mean squared error

  • loss: the goodness-of-fit for each result multiplied by user-specified weights (one time series per result)

  • mismatches: the sum of all the losses (a single scalar value per time series)

  • mismatch: the sum of the mismatches – this is the value to be minimized during calibration

Parameters:
  • sim (Sim) – the sim object

  • weights (dict) – the relative weight to place on each result (by default: 10 for deaths, 5 for diagnoses, 1 for everything else)

  • keys (list) – the keys to use in the calculation

  • custom (dict) – a custom dictionary of additional data to fit; format is e.g. {‘my_output’:{‘data’:[1,2,3], ‘sim’:[1,2,4], ‘weights’:2.0}}

  • compute (bool) – whether to compute the mismatch immediately

  • verbose (bool) – detail to print

  • die (bool) – whether to raise an exception if no data are supplied

  • label (str) – the label for the analyzer

  • kwargs (dict) – passed to cv.compute_gof() – see this function for more detail on goodness-of-fit calculation options

Example:

sim = cv.Sim(datafile='my-data-file.csv')
sim.run()
fit = sim.compute_fit()
fit.plot()

Methods

compute()[source]#

Perform all required computations

reconcile_inputs()[source]#

Find matching keys and indices between the model and the data

compute_diffs(absolute=False)[source]#

Find the differences between the sim and the data

compute_gofs(**kwargs)[source]#

Compute the goodness-of-fit

compute_losses()[source]#

Compute the weighted goodness-of-fit

compute_mismatch(use_median=False)[source]#

Compute the final mismatch

summarize()[source]#

Print out results from the fit

plot(keys=None, width=0.8, fig_args=None, axis_args=None, plot_args=None, date_args=None, do_show=None, fig=None, **kwargs)[source]#

Plot the fit of the model to the data. For each result, plot the data and the model; the difference; and the loss (weighted difference). Also plots the loss as a function of time.

Parameters:
  • keys (list) – which keys to plot (default, all)

  • width (float) – bar width

  • fig_args (dict) – passed to pl.figure()

  • axis_args (dict) – passed to pl.subplots_adjust()

  • plot_args (dict) – passed to pl.plot()

  • date_args (dict) – passed to cv.plotting.reset_ticks() (handle date format, rotation, etc.)

  • do_show (bool) – whether to show the plot

  • fig (fig) – if supplied, use this figure to plot in

  • kwargs (dict) – passed to cv.options.with_style()

Returns:

Figure object