Post · 6 min read· pp. 794–799

aerosandbox.modeling

Submodules

Classes

FittedModelA model that is fitted to data. Maps from R^N -> R^1.
InterpolatedModelA model that is interpolated to structured (i.e., gridded) N-dimensional data. Maps from R^N -> R^1.
UnstructuredInterpolatedModelA model that is interpolated to unstructured (i.e., point cloud) N-dimensional data. Maps from R^N -> R^1.

Functions

black_box(function[, n_in, n_out, fd_method, fd_step, ...])Wraps a function as a black box, allowing it to be used in AeroSandbox / CasADi optimization problems.

Package Contents

class aerosandbox.modeling.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) : Bases: aerosandbox.modeling.surrogate_model.SurrogateModel

code
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.:

```default

my_fitted_model = FittedModel(...) # See FittedModel.init docstring for syntax y = my_fitted_model(x)

code

    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:

    ```default
>>> print(my_fitted_model) # Displays the valid input type to the model
code
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*) –

model

x\_data

y\_data

parameters

parameter\_guesses

parameter\_bounds *= None*

residual\_norm\_type *= 'L2'*

fit\_type *= 'best'*

weights

put\_residuals\_in\_logspace *= False*

\_\_call\_\_(*x*)
:   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'*)
:   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.

        > <https://en.wikipedia.org/wiki/Coefficient_of_determination>

        - ”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.

class aerosandbox.modeling.InterpolatedModel(x_data_coordinates, y_data_structured, method='bspline', fill_value=np.nan) : Bases: aerosandbox.modeling.surrogate_model.SurrogateModel

code
A model that is interpolated to structured (i.e., gridded) N-dimensional 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.:

```default

y = my_interpolated_model(x)

code

    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), it can optionally just be a float/array.

    If you’re not sure what the input type of my\_interpolated\_model should be, just do:

    ```default
>>> print(my_interpolated_model) # Displays the valid input type to the model
code
The output of the model (y in the example above) is always a float or array.

See the docstring \_\_init\_\_ method of InterpolatedModel for more details of how to instantiate and use InterpolatedModel.

One might have expected a interpolated model to be a literal Python function rather than a Python class - the
benefit of having InterpolatedModel 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
InterpolatedModel class has a \_\_call\_\_ method, you can basically still just think of it like a function.

Parameters:
:   - **x\_data\_coordinates** (*Union**[**aerosandbox.numpy.ndarray**,* *Dict**[**str**,* *aerosandbox.numpy.ndarray**]**]*) –
    - **y\_data\_structured** (*aerosandbox.numpy.ndarray*) –
    - **method** (*str*) –

x\_data\_coordinates

x\_data\_coordinates\_values

y\_data\_structured

method *= 'bspline'*

fill\_value

x\_data

y\_data

\_\_call\_\_(*x*)
:   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.

class aerosandbox.modeling.UnstructuredInterpolatedModel(x_data, y_data, x_data_resample=10, resampling_interpolator=interpolate.RBFInterpolator, resampling_interpolator_kwargs=None, fill_value=np.nan, interpolated_model_kwargs=None) : Bases: aerosandbox.modeling.interpolation.InterpolatedModel

code
A model that is interpolated to unstructured (i.e., point cloud) N-dimensional 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.:

```default

y = my_interpolated_model(x)

code

    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), it can optionally just be a float/array.

    If you’re not sure what the input type of my\_interpolated\_model should be, just do:

    ```default
>>> print(my_interpolated_model) # Displays the valid input type to the model
code
The output of the model (y in the example above) is always a float or array.

See the docstring \_\_init\_\_ method of InterpolatedModel for more details of how to instantiate and use UnstructuredInterpolatedModel.

Parameters:
:   - **x\_data** (*Union**[**aerosandbox.numpy.ndarray**,* *Dict**[**str**,* *aerosandbox.numpy.ndarray**]**]*) –
    - **y\_data** (*aerosandbox.numpy.ndarray*) –
    - **x\_data\_resample** (*Union**[**int**,* *Dict**[**str**,* *Union**[**int**,* *aerosandbox.numpy.ndarray**]**]**]*) –
    - **resampling\_interpolator** (*object*) –
    - **resampling\_interpolator\_kwargs** (*Dict**[**str**,* *Any**]*) –
    - **interpolated\_model\_kwargs** (*Dict**[**str**,* *Any**]*) –

x\_data\_raw\_unstructured

y\_data\_raw

aerosandbox.modeling.black_box(function, n_in=None, n_out=1, fd_method='central', fd_step=None, fd_step_iter=None) : Wraps a function as a black box, allowing it to be used in AeroSandbox / CasADi optimization problems.

code
Obtains gradients via finite differences. Assumes that the function’s Jacobian is fully dense, always.

Parameters:
:   - **function** (*Callable**[**[**Any**]**,* *float**]*) –
    - **n\_in** (*int*) –
    - **n\_out** (*int*) –
    - **fd\_method** (*str*) – One of:
      - ‘forward’
      - ‘backward’
      - ‘central’
      - ‘smoothed’
    - **fd\_step** (*Optional**[**float**]*) –
    - **fd\_step\_iter** (*Optional**[**bool**]*) –

Return type:
:   Callable[[Any], float]

Returns: