XenonStack Glossary: AI, Data and Cloud Terms Explained

Goodness of Fit: Tests, Metrics and Model Fit

Written by Dr. Jagreet Kaur | 17 Mar 2023

Goodness of fit describes how well a statistical or machine learning model matches a set of observations, usually by summarising the gap between the observed values and the values the model expects. A good fit means the model captures the real pattern in the data without memorising its noise.

This page is for analysts, data scientists and technical leaders who need to judge whether a model can be trusted. It covers common goodness of fit tests, how fit is judged in machine learning, and what to check first.

What is goodness of fit?

The goodness of fit of a model explains how well it matches a set of observations. Goodness of fit indicators typically summarise the disparity between observed values and the values the model anticipates. A small disparity suggests the model describes the data well, though how small is small enough depends on the test and the use case.

How is goodness of fit tested in statistics?

The NIST Engineering Statistics Handbook describes the chi-square test as a way to test whether a sample of data came from a population with a specific distribution. The null hypothesis is that the data follow the specified distribution. The data are split into bins, and the test statistic sums the squared gap between observed and expected counts, divided by the expected count, across bins. The handbook notes two limits: the result depends on how the data is binned, and the test needs a sufficient sample size for its approximation to be valid. It can also be applied to discrete distributions, unlike the Kolmogorov-Smirnov and Anderson-Darling tests.

How do common fit measures compare?

MeasureWhat it checksWatch for
Chi-square testWhether a sample follows a specified distributionBinning choices and small samples
Kolmogorov-Smirnov and Anderson-Darling testsWhether a sample follows a specified continuous distributionNot suited to discrete distributions, per NIST
R-squared and squared errorHow closely a regression model's predictions match observed valuesPer scikit-learn, R-squared gives the same ranking as squared error
Training and validation loss curvesWhether the model generalises to unseen dataCurves that diverge signal overfitting

The scikit-learn model evaluation guide names accuracy as the most common score for classifiers and the coefficient of determination (R-squared) for regressors.

What does a good fit look like in machine learning?

As an algorithm learns, the error on the training data falls, and so does the error on the test data. If training continues too long, the training error may keep falling while the model learns irrelevant detail and noise, and the test error begins to rise again as its ability to generalise decreases. The point just before the test error starts to rise, where the model does well on both training and unseen data, is the good fit. The Google Machine Learning Crash Course describes the same pattern: in an overfit model the training and validation loss curves behave similarly at first and then diverge, while a well-fit model shows two curves with similar shapes. An underfit model does not even predict well on the training data.

SituationTraining errorTest or validation errorTypical cause
UnderfitHighHighModel too simple or too little training
Good fitLowLowModel matches the real pattern
OverfitLowRisingModel too complex, or training data not representative

How does goodness of fit relate to AI agents?

An agent that acts on a model's output is only as reliable as that model's fit on the data it will actually see. A model that fitted well on past data can drift as inputs change, so fit checks belong in ongoing monitoring, not only in the build. ElixirClaw is XenonStack's AI agents computing platform.

What should you do first?

  1. Hold out a test set before you start tuning.
  2. Plot training and validation loss together and look for divergence.
  3. Choose a fit measure that matches the model type, such as a distribution test or a regression score.
  4. Repeat the check when the input data changes.

To map one decision your agents make from model outputs, and the data and policy it depends on, book a Context Workshop. See what happens in the workshop.

Frequently asked questions

What is goodness of fit?

Goodness of fit measures how well a model matches a set of observations, usually by summarising the gap between observed values and the values the model expects.

What is the chi-square goodness of fit test?

It tests whether a sample came from a population with a specified distribution by comparing observed counts in bins with the counts the distribution expects.

What does a good fit mean in machine learning?

A good fit is the point where the model has low error on both the training data and unseen test data, before the test error begins to rise.

How can you tell overfitting from underfitting?

An overfit model does well on training data but poorly on new data, so its validation loss rises while training loss keeps falling. An underfit model performs poorly even on the training data.