Skip to content

fit.metrics

Metrics of a fit.

The metrics are calculated for a parameter set on an initialized OptimizationProblem, i.e., from the data of the fit mappings and the predictions of the model for the parameters, see FitMetrics. The functions which do the arithmetic work on plain arrays and are used on their own as well.

The names of the columns follow the convention of population pharmacokinetics:

DV      the measured value (dependent variable)
PRED    prediction of the population parameters, i.e., the parameter set
        which is shared by all fit mappings
IPRED   prediction of the individual parameters, i.e., the parameter set of
        the fit mapping. Without individual parameters IPRED is PRED
RES     DV - PRED
IRES    DV - IPRED
NRES    IRES / mean(DV) of the fit mapping, the normalized residual, so the
        residuals of curves of different magnitude are comparable
IWRES   the residual of the cost of the optimization problem, i.e., IRES
        as the `ResidualType` of the settings defines it, weighted and
        with the loss function applied. The cost of the training data is
        `0.5 * sum(IWRES**2)`

The data of a fit spans orders of magnitude, so the absolute metrics MSE, RMSE and R2 are dominated by the curves with the largest values and a curve of small values is missed by a factor of 20 at an absolute error which is still tiny. NRMSE, the root mean square of NRES, is the scale free metric which reads the same for every curve, and RMSE_w is the root mean square of IWRES, i.e., what the fit minimizes.

Note the sign: the residuals of OptimizationProblem.residuals are prediction - data, the residuals here are data - prediction.

FitMetrics dataclass

FitMetrics(
    problem, parameter_set, population_parameter_set=None
)

Metrics of a parameter set on an optimization problem.

The metrics are calculated from the data of the fit mappings and the predictions of the model, see the module docstring for the names.

Attributes:

Name Type Description
problem OptimizationProblem

initialized optimization problem, it provides the data.

parameter_set ParameterSet

parameters the predictions are calculated for, they give IPRED.

population_parameter_set ParameterSet | None

parameters shared by all fit mappings, they give PRED. Without them PRED is IPRED, which is the case of a deterministic fit of a single parameter set.

n_parameters property

n_parameters

Number of fitted parameters, the k of the AIC.

datapoints_df

datapoints_df()

Get the table of the data points with their predictions.

Returns:

Type Description
DataFrame

DataFrame with one row per data point and the columns experiment,

DataFrame

mapping, kind, x, DV, PRED, IPRED, RES, IRES,

DataFrame

NRES and IWRES, see the module.

mappings_df

mappings_df()

Get the metrics of every fit mapping.

Returns:

Type Description
DataFrame

DataFrame with one row per fit mapping and the columns experiment,

DataFrame

mapping, kind, n, MSE, RMSE, NRMSE, RMSE_w and R2.

summary

summary(kind=None)

Get the metrics over the data points of the problem.

MSE, RMSE, R2, AIC and BIC are the unweighted metrics of the data and the predictions, i.e., they are dominated by the fit mappings with the largest values. The two information criteria differ in how they penalize a parameter, 2 against ln(n), so the BIC prefers the smaller model of two which describe the data equally well. NRMSE is the root mean square of the residuals normalized by the mean of their curve, so every curve counts the same whatever its magnitude, and RMSE_w is the root mean square of the residuals of the cost, so a parameter set can have a larger RMSE and smaller weighted residuals than another one, which is what the weighting is for.

cost is the objective the optimization minimizes. It is defined on the training data alone, so it is only reported for the training data and is nan for the other kinds.

Parameters:

Name Type Description Default
kind MappingKind | None

only the data points of the fit mappings of this kind, all data points if None.

None

Returns:

Type Description
dict[str, Any]

Dictionary with the id of the parameter set, the kind, the number

dict[str, Any]

of data points n, the number of parameters k, the cost,

dict[str, Any]

MSE, RMSE, NRMSE, RMSE_w, R2, AIC and BIC.

Raises:

Type Description
ValueError

if the problem has no data points of the kind.

summary_df

summary_df()

Get the metrics per kind of fit mapping.

A fit is evaluated on its training data, on its validation data and on the outliers it dropped, so there is a row for every kind the problem has. There is no row over all data points: it pools the data a fit was fitted on with the data it dropped, which is not a number to read. summary() gives the metrics over all data points where they are wanted.

Returns:

Type Description
DataFrame

DataFrame with one row per kind, see summary.

cost

cost()

Get the cost of the parameter set, i.e., the objective of the fit.

report

report()

Get the metrics as text, per kind of fit mapping and per mapping.

sse

sse(residuals)

Sum of Squared Errors (SSE) of the residuals.

Parameters:

Name Type Description Default
residuals ArrayLike

residuals of the fit.

required

Returns:

Type Description
float

Sum of the squared residuals.

mse

mse(residuals)

Mean Squared Error (MSE) of the residuals.

Parameters:

Name Type Description Default
residuals ArrayLike

residuals of the fit.

required

Returns:

Type Description
float

Mean of the squared residuals.

Raises:

Type Description
ValueError

if no residuals are given.

rmse

rmse(residuals)

Root Mean Squared Error (RMSE) of the residuals.

Parameters:

Name Type Description Default
residuals ArrayLike

residuals of the fit.

required

Returns:

Type Description
float

Square root of the mean squared error.

rmse_from_mse

rmse_from_mse(mse)

Root Mean Squared Error (RMSE) from the mean squared error.

Parameters:

Name Type Description Default
mse float

mean squared error of the fit.

required

Returns:

Type Description
float

Square root of the mean squared error.

Raises:

Type Description
ValueError

if the mean squared error is negative.

aic

aic(residuals, k)

Akaike Information Criterion (AIC) of the residuals.

Parameters:

Name Type Description Default
residuals ArrayLike

residuals of the fit.

required
k int

number of fitted parameters.

required

Returns:

Type Description
float

Akaike information criterion.

aic_from_mse

aic_from_mse(mse, n, k)

Akaike Information Criterion (AIC) from the mean squared error.

The AIC is calculated for a least squares fit with normally distributed residuals, i.e., AIC = n * ln(MSE) + 2 * k up to an additive constant. Only differences of the AIC between models fitted on the same data are meaningful.

Parameters:

Name Type Description Default
mse float

mean squared error of the fit.

required
n int

number of data points.

required
k int

number of fitted parameters.

required

Returns:

Type Description
float

Akaike information criterion.

Raises:

Type Description
ValueError

if the mean squared error or the number of data points is not positive.

bic

bic(residuals, k)

Bayesian Information Criterion (BIC) of the residuals.

Parameters:

Name Type Description Default
residuals ArrayLike

residuals of the fit.

required
k int

number of fitted parameters.

required

Returns:

Type Description
float

Bayesian information criterion.

bic_from_mse

bic_from_mse(mse, n, k)

Bayesian Information Criterion (BIC) from the mean squared error.

The BIC is calculated for a least squares fit with normally distributed residuals, i.e., BIC = n * ln(MSE) + k * ln(n) up to the same additive constant as the AIC, so only differences between models fitted on the same data are meaningful.

The BIC penalizes a parameter with ln(n) where the AIC penalizes it with 2, i.e. it prefers the smaller model as soon as there are more than seven data points and increasingly so with more of them.

Parameters:

Name Type Description Default
mse float

mean squared error of the fit.

required
n int

number of data points.

required
k int

number of fitted parameters.

required

Returns:

Type Description
float

Bayesian information criterion.

Raises:

Type Description
ValueError

if the mean squared error or the number of data points is not positive.

r_squared

r_squared(y_observed, y_predicted)

Coefficient of determination (R²) of a prediction.

R² = 1 - SSE / SST with SST the total sum of squares of the data. The predictions of a non-linear model are not a linear regression of the data, so R² is not the square of a correlation and can be negative: a negative R² means the prediction is worse than the mean of the data.

Parameters:

Name Type Description Default
y_observed ArrayLike

measured values.

required
y_predicted ArrayLike

predicted values.

required

Returns:

Type Description
float

Coefficient of determination, nan if the data has no variance.

Raises:

Type Description
ValueError

if the data and the prediction have different lengths.