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

  • 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


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



Perform all required computations


Find matching keys and indices between the model and the data


Find the differences between the sim and the data


Compute the goodness-of-fit


Compute the weighted goodness-of-fit


Compute the final mismatch


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.

  • 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()


Figure object