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.
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.
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.
| Measure | What it checks | Watch for |
|---|---|---|
| Chi-square test | Whether a sample follows a specified distribution | Binning choices and small samples |
| Kolmogorov-Smirnov and Anderson-Darling tests | Whether a sample follows a specified continuous distribution | Not suited to discrete distributions, per NIST |
| R-squared and squared error | How closely a regression model's predictions match observed values | Per scikit-learn, R-squared gives the same ranking as squared error |
| Training and validation loss curves | Whether the model generalises to unseen data | Curves 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.
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.
| Situation | Training error | Test or validation error | Typical cause |
|---|---|---|---|
| Underfit | High | High | Model too simple or too little training |
| Good fit | Low | Low | Model matches the real pattern |
| Overfit | Low | Rising | Model too complex, or training data not representative |
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.
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.
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.
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.
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.
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.