# Goodness-of-Fit

![Goodness-of-Fit](https://www.investopedia.com/thmb/_bX5iKLtpxcuevGtDKI-wZcmFqk=/1500x0/filters:no_upscale():max_bytes(150000):strip_icc()/goodness-of-fit.asp-FINAL-c75bcaeb08df48d8a9b4f59041b84f9e.png)

*A good fit should show a small discrepancy between observed and expected values. — Investopedia / Joules Garcia*

> **Definition**
>
> Goodness-of-fit evaluates statistical models for their ability to accurately forecast outcomes and prevent misleading deductions.

## What Is Goodness-of-Fit?

The term goodness-of-fit refers to a statistical test that assesses how well sample data fits a distribution from a population with a [normal distribution](https://www.investopedia.com/terms/n/normaldistribution.asp). Put simply, it hypothesizes whether a sample is skewed or represents the data you would expect to find in the actual population.

Goodness-of-fit establishes the discrepancy between the observed values and those expected of the model in a normal distribution case. There are multiple methods to determine goodness-of-fit, including the chi-square.

### Key Takeaways

*   Goodness-of-fit can show you whether your sample data fit an expected set of data from a population with normal distribution.
*   There are multiple types of goodness-of-fit tests, but the most common is the chi-square test.
*   The chi-square test determines if a relationship exists between categorical data.
*   The Kolmogorov-Smirnov test determines whether a sample comes from a specific distribution of a population.  
    

## Understanding Goodness-of-Fit

Goodness-of-fit tests are statistical methods that make inferences about observed values. For instance, you can determine whether a sample group is truly representative of the entire [population](https://www.investopedia.com/terms/p/population.asp).

As such, they determine how actual values are related to the predicted values in a model. When used in decision-making, goodness-of-fit tests make it easier to predict [trends](https://www.investopedia.com/terms/t/trend.asp) and patterns in the future.

As noted above, there are several types of goodness-of-fit tests. They include the chi-square test, which is the most common, as well as the Kolmogorov-Smirnov test, and the Shapiro-Wilk test. The tests are normally conducted using computer software. But statisticians can do these tests using formulas that are tailored to the specific type of test.

To conduct the test, you need a certain variable, along with an assumption of how it is distributed. You also need a data set with clear and explicit values, such as:

*   The observed values, which are derived from the actual data set
*   The expected values, which are taken from the assumptions made
*   The total number of categories in the set

> **Important**
>
> Goodness-of-fit tests are commonly used to test for the normality of residuals or to determine whether two samples are gathered from identical distributions.

## Establish an Alpha Level

In order to interpret a goodness-of-fit test, it's important for statisticians to establish an alpha level, such as the [p-value](https://www.investopedia.com/terms/p/p-value.asp) for the chi-square test.

The p-value refers to the probability of getting results close to extremes of the observed results. This assumes that the [null hypothesis](https://www.investopedia.com/terms/n/null_hypothesis.asp) is correct.

A null hypothesis asserts there is no relationship that exists between variables, and the alternative hypothesis assumes that a relationship exists.

Instead, the frequency of the observed values is measured and subsequently used with the expected values and the [degrees of freedom](https://www.investopedia.com/terms/d/degrees-of-freedom.asp) to calculate chi-square. If the result is lower than alpha, the null hypothesis is invalid, indicating a relationship exists between the variables.  

## Types of Goodness-of-Fit Tests

### Chi-Square Test

$\chi^2=\sum\limits^k_{i=1}(O_i-E_i)^2/E_i$

The [chi-square test](https://www.investopedia.com/terms/c/chi-square-statistic.asp), which is also known as the chi-square test for independence, is an inferential statistics method that tests the validity of a claim made about a population based on a random sample.

Used exclusively for data that is separated into classes (bins), it requires a sufficient sample size to produce accurate results. But it doesn't indicate the type or intensity of the relationship. For instance, it does not conclude whether the relationship is positive or negative.

To calculate a chi-square goodness-of-fit, set the desired alpha level of significance. So if your confidence level is 95% (or 0.95), then the alpha is 0.05. Next, identify the categorical variables to test, then define hypothesis statements about the relationships between them.  

> **Fast Fact**
>
> Variables must be mutually exclusive in order to qualify for the chi-square test for independence. And the chi goodness-of-fit test should not be used for data that is continuous.

### Kolmogorov-Smirnov (K-S) Test

> $D=\max\limits_{1\leq i\leq N}\bigg(F(Y_i)-\frac{i-1}{N},\frac{i}{N}-F(Y_i)\bigg)$

Named after Russian mathematicians Andrey Kolmogorov and Nikolai Smirnov, the Kolmogorov-Smirnov (K-S) test is a statistical method that determines whether a sample is from a specific distribution within a population.

This test, which is recommended for large [samples](https://www.investopedia.com/terms/s/sample.asp) (e.g., over 2000), is non-parametric. That means it does not rely on any distribution to be valid. The goal is to prove the null hypothesis, which is the sample of the normal distribution.

Like chi-square, it uses a null and alternative hypothesis and an alpha level of significance. Null indicates that the data follow a specific distribution within the population, and alternative indicates that the data did not follow a specific distribution within the population.

The alpha is used to determine the critical value used in the test. But unlike the chi-square test, the K-S test applies to continuous distributions.

The calculated test statistic is often denoted as D. It determines whether the null hypothesis is accepted or rejected. If D is greater than the critical value at [alpha](https://www.investopedia.com/terms/a/alpha.asp), the null hypothesis is rejected. If D is less than the critical value, the null hypothesis is accepted.

### The Anderson-Darling (A-D) Test

$S = \sum_{i = 1}^{N} \frac {( 2i - 1 )}{ N } [\ln F ( Y_i ) + \ln ( 1 - F ( Y_{N + 1 - i} ) ) ]$

The Anderson-Darling (A-D) test is a variation on the K-S test, but gives more weight to the tails of the distribution.

The K-S test is more sensitive to differences that may occur closer to the center of the distribution, while the A-D test is more sensitive to variations observed in the tails. Because tail risk and the idea of "fatty tails" is prevalent in financial markets, the A-D test can give more power in financial analyses.

Like the K-S test, the A-D test produces a statistic, denoted as A, which can be compared against the null hypothesis.  

### Shapiro-Wilk (S-W) Test

> $W=\frac{\big(\sum^n_{i=1}a_i(x_{(i)}\big)^2}{\sum^n_{i=1}(x_i-\bar{x})^2},$

The Shapiro-Wilk (S-W) test determines if a sample follows a normal distribution. The test only checks for normality when using a sample with one variable of continuous data and is recommended for small sample sizes up to 2000.

The Shapiro-Wilk test uses a probability plot called the QQ Plot, which displays two sets of quantiles on the y-axis that are arranged from smallest to largest. If each quantile came from the same distribution, the series of plots are linear.

The QQ Plot is used to estimate the variance. Using QQ Plot variance along with the estimated variance of the population, one can determine if the sample belongs to a normal distribution. If the quotient of both variances equals or is close to 1, the null hypothesis can be accepted. If considerably lower than 1, it can be rejected.

Just like the tests mentioned above, this one uses alpha and forms two hypotheses: null and alternative. The null hypothesis states that the sample comes from the normal distribution, whereas the alternative hypothesis states that the sample does not come from the normal distribution.  

## Other Goodness-of-Fit Tests

Aside from the more common types of tests mentioned above, there are numerous other goodness-of-fit tests an analyst can use:

*   The **[Bayesian](https://www.investopedia.com/articles/financial-theory/09/bayesian-methods-financial-modeling.asp) information criterion (BIC)** is a statistical measure used for model selection among a finite set of models. The BIC is a goodness-of-fit test that balances the complexity of a model with its goodness-of-fit to the data.
*   The **Cramer-von Mises criterion (CVM)** is a goodness-of-fit test that is used to assess how well a set of observed data fits a hypothesized probability distribution. Often used in economics, engineering, or finance, it is based on the cumulative distribution function of the observed data and the hypothesized distribution.
*   The **Akaike information criterion (AIC)** is a measure of the relative quality of a statistical model for a given set of data, and it provides a trade-off between the goodness-of-fit of the model and its complexity. It's based on information theory and measures the amount of information lost by a model when it is used to approximate the true underlying distribution of the data.
*   The **Hosmer-Lemeshow test** compares the expected frequencies of a binary outcome with the observed frequencies of that outcome in different groups or intervals. The groups are typically formed by dividing the predicted probabilities of the outcome into ten groups or bins.
*   **Kuiper's test** is similar to the Kolmogorov-Smirnov test, but it is more sensitive to differences in the tails of the distribution.
*   **Moran's I test** or **Moran's Index** is a statistical test used to assess spatial autocorrelation in data. Spatial autocorrelation is a measure of the degree to which observations of a variable are similar or dissimilar in space.  
    

> **Tip**
>
> A very general rule of thumb is to require that every group within a goodness-of-fit test have at least five data points. This ensures that sufficient information is fed into the test to determine the distribution.

## Importance of Goodness-of-Fit Tests

Goodness-of-fit tests are important in statistics for many reasons.

1\. First, they provide a way to assess [how well a statistical model fits](https://www.investopedia.com/terms/r/runs_test.asp) a set of observed data. The main importance of running a goodness-of-fit test is to determine whether the observed data are consistent with the assumed statistical model. By extension, a goodness-of-fit test may be useful in choosing between different models which may better fit the data.

2\. Goodness-of-fit tests can also help to identify outliers or [market abnormalities](https://www.investopedia.com/articles/financial-theory/11/trading-with-market-anomalies.asp) that may be affecting the fit of the model. Outliers can have a large impact on the model fit and may need to be removed or dealt with separately. Sometimes, outliers are not easily identifiable until they have been integrated into an analytical model.

3\. Goodness-of-fit tests can provide information about the variability of the data and the estimated parameters of the model. This information can be useful for making predictions and understanding the behavior of the system being modeled.

Based on the data being fed into the model, it may be necessary to refine the model specific to the dataset being tested, the residuals being calculated, and the p-value for potentially extreme data.

## Goodness-of-Fit Test vs. Independence Test

Goodness-of-fit test and independence test are both statistical tests used to assess the relationship between variables; therefore, it may be easy to confuse the two. However, each is designed to answer different questions.

A goodness-of-fit test is used to evaluate how well a set of observed data fits a particular probability distribution.

On the other hand, an independence test is used to assess the relationship between two variables; that is, whether there is any association between the two variables. The primary purpose of an independence test is to see whether a change in one variable is related to a change in another variable.

An independence test is typically used when the research question is focused on understanding the relationship between two specific variables and whether they are related or independent (i.e. does smoking cause lung cancer?).

On the other hand, a goodness-of-fit test is used on an entire set of observed data to evaluate the appropriateness of a specific model.

## Goodness-of-Fit Example

Here's a hypothetical example to show how the goodness-of-fit test works.

Suppose a small community gym operates under the assumption that the highest attendance is on Mondays, Tuesdays, and Saturdays, average attendance occurs on Wednesdays and Thursdays, and lowest attendance occurs on Fridays and Sundays.

Based on these assumptions, the gym employs a certain number of staff members each day to check in members, clean facilities, offer training services, and teach classes.

But the gym isn't performing well financially and the owner wants to know if these attendance assumptions and staffing levels are correct. The owner decides to count the number of gym attendees each day for six weeks.

They can then compare the gym's assumed attendance with its observed attendance using a chi-square goodness-of-fit test for example.

Now that they have the new data, they can determine how to best manage the gym and improve [profitability](https://www.investopedia.com/ask/answers/012715/what-difference-between-profitability-and-profit.asp).

## What Does Goodness-of-Fit Mean?

Goodness-of-fit is a statistical hypothesis test used to see how closely observed data mirrors expected data. Goodness-of-fit tests can help determine if a sample follows a normal distribution, if categorical variables are related, or if random samples are from the same distribution.

## Why Is Goodness-of-Fit Important?

Goodness-of-fit tests are important because they can help determine if observed data aligns with what is assumed/expected. Decisions can be made based on the outcome of the hypothesis test conducted. For example, a retailer wants to know what product offering appeals to young people. The retailer surveys a random sample of old and young people to identify which product is preferred. Using chi-square, they identify that, with 95% confidence, a relationship exists between product A and young people. Based on these results, it could be determined that this sample represents the population of young adults. Retail marketers can use this to revise their marketing campaigns.

## What Is Goodness-of-Fit in the Chi-Square Test?

The chi-square test assesses whether relationships exist between categorical variables and whether the sample represents the whole. It estimates how closely the observed data mirrors the expected data, or how well they fit.

## How Do You Do the Goodness-of-Fit Test?

There is more than one test. The goal of the test will help determine which method to use. For example, if the goal is to test normality on a relatively small sample, the Shapiro-Wilk test may be suitable. If researchers want to determine whether a sample came from a specific distribution within a population, the Kolmogorov-Smirnov test will be used. Each test uses its own unique formula. However, they have commonalities, such as a null hypothesis and level of significance.

## The Bottom Line

Goodness-of-fit tests determine how well sample data fits what is expected of a population. From the sample data, an observed value is gathered and compared to the calculated expected value using a discrepancy measure.

There are different goodness-of-fit hypothesis tests available depending on what outcome you're seeking. The appropriate goodness-of-fit test largely depends on what you want to know about a sample and how large the sample is.

> **Article Sources**
>
> 1.  National Institute of Technology and Standards. "[Chi-Square Goodness-of-Fit Test](https://www.itl.nist.gov/div898/handbook/eda/section3/eda35f.htm)."
>     
> 2.  National Institute of Standards and Technology. "[Kolmogorov-Smirnov Goodness-of-Fit Test](https://www.itl.nist.gov/div898/handbook/eda/section3/eda35g.htm)."
>     
> 3.  Anderson, Theodore W. "Anderson-Darling Tests of Goodness-of-Fit." _International Encyclopedia of Statistical Science,_ vol_._ 1, 2011, pp. 52-54.
>     
> 4.  Shapiro, S.S., and M.B. Wilk (via UNC Gillings School of Global Publci Health). "[An Analysis of Variance Test for Normality (Complete Samples)](http://www.bios.unc.edu/~mhudgens/bios/662/2008fall/Backup/wilkshapiro1965.pdf)." _Biometrika,_ vol. 53, no. 3/4, Dec. 1965, pp. 591-611.