aerosandbox.modeling.fitting#

Module Contents#

Classes#

FittedModel

A model that is fitted to data. Maps from R^N -> R^1.

class aerosandbox.modeling.fitting.FittedModel(model, x_data, y_data, parameter_guesses, parameter_bounds=None, residual_norm_type='L2', fit_type='best', weights=None, put_residuals_in_logspace=False, verbose=True)[source]#

Bases: aerosandbox.modeling.surrogate_model.SurrogateModel

A model that is fitted to data. Maps from R^N -> R^1.

You can evaluate this model at a given point by calling it just like a function, e.g.:

>>> my_fitted_model = FittedModel(...)  # See FittedModel.__init__ docstring for syntax
>>> y = my_fitted_model(x)
The input to the model (x in the example above) is of the type:
  • in the general N-dimensional case, a dictionary where: keys are variable names and values are float/array

  • in the case of a 1-dimensional input (R^1 -> R^1), a float/array.

If you’re not sure what the input type of my_fitted_model should be, just do:

>>> print(my_fitted_model) # Displays the valid input type to the model

The output of the model (y in the example above) is always a float or array.

See the docstring __init__ method of FittedModel for more details of how to instantiate and use FittedModel.

One might have expected a fitted model to be a literal Python function rather than a Python class - the benefit of having FittedModel as a class rather than a function is that you can easily save (pickle) classes including data (e.g. parameters, x_data, y_data), but you can’t do that with functions. And, because the FittedModel class has a __call__ method, you can basically still just think of it like a function.

Parameters:
  • model (Callable[[Union[aerosandbox.numpy.ndarray, Dict[str, aerosandbox.numpy.ndarray]], Dict[str, float]], aerosandbox.numpy.ndarray]) –

  • x_data (Union[aerosandbox.numpy.ndarray, Dict[str, aerosandbox.numpy.ndarray]]) –

  • y_data (aerosandbox.numpy.ndarray) –

  • parameter_guesses (Dict[str, float]) –

  • parameter_bounds (Dict[str, tuple]) –

  • residual_norm_type (str) –

  • fit_type (str) –

  • weights (aerosandbox.numpy.ndarray) –

  • put_residuals_in_logspace (bool) –

__call__(x)[source]#

Evaluates the surrogate model at some given input x.

The input x is of the type:
  • in the general N-dimensional case, a dictionary where keys are variable names and values are float/array.

  • in the case of a 1-dimensional input (R^1 -> R^2), a float/array.

goodness_of_fit(type='R^2')[source]#

Returns a metric of the goodness of the fit.

Parameters:

type

Type of metric to use for goodness of fit. One of:

  • ”R^2”: The coefficient of determination. Strictly speaking only mathematically rigorous to use this

for linear fits.

  • ”mean_absolute_error” or “mae” or “L1”: The mean absolute error of the fit.

  • ”root_mean_squared_error” or “rms” or “L2”: The root mean squared error of the fit.

  • ”max_absolute_error” or “Linf”: The maximum deviation of the fit from any of the data points.

Returns: The metric of the goodness of the fit.