edges.modeling.fitting

Fitting routines for models.

class edges.modeling.fitting.ModelFit(model: FixedLinearModel, ydata: ndarray, weights: ndarray | float = 1.0, method: Literal['lstsq', 'qr', 'alan-qrd'] = 'lstsq')[source]

A class representing a fit of model to data.

Parameters:
  • model (edges.modeling.core.FixedLinearModel) – The evaluatable model to fit to the data.

  • ydata (numpy.ndarray) – The values of the measured data.

  • weights (numpy.ndarray | float) – The weight of the measured data at each point. This corresponds to the variance of the measurement (not the standard deviation). This is appropriate if the weights represent the number of measurements going into each piece of data.

  • method (Literal[‘lstsq’, ‘qr’, ‘alan-qrd’]) – The method to solve the linear least squares problem. This can be either ‘lstsq’, ‘qr’ or ‘alan-qrd’. The ‘leastsq’ method uses the np.linalg.lstsq function, while the ‘qr’ method uses the np.linalg.solve function after scipy.linalg.qr. The ‘alan-qr’ method is a python-port of the QR decomposition algorithm found in Alan’s C Codebase.

Raises:

ValueError – If model_type is not str, or a subclass of Model.

property degrees_of_freedom: int[source]

The number of degrees of freedom of the fit.

evaluate(x: ndarray | None = None) ndarray[source]

Evaluate the best-fit model.

Parameters:

x (np.ndarray, optional) – The co-ordinates at which to evaluate the model. By default, use the input data co-ordinates.

Returns:

y (np.ndarray) – The best-fit model evaluated at x.

property fit: FixedLinearModel[source]

A model that has parameters set based on the best fit to this data.

classmethod from_file(path: str | Path | Group)

Load an HDF5 file as a given type.

get_sample(size: int | tuple[int] = 1)[source]

Generate a random sample from the posterior distribution.

property hessian[source]

The Hessian matrix of the linear parameters.

property model_parameters[source]

The best-fit model parameters.

property parameter_correlation: ndarray[source]

The correlation matrix of the parameters.

property parameter_covariance: ndarray[source]

The Covariance matrix of the parameters.

property reduced_weighted_chi2: float[source]

The weighted chi^2 divided by the degrees of freedom.

property residual: ndarray[source]

Residuals of data to model.

property weighted_chi2: float[source]

The chi^2 of the weighted fit.

property weighted_rms: float[source]

The weighted root-mean-square of the residuals.

write(path: str | Path | Group)

Write an attrs class to HDF5.