Docsity

Goodness-of-Fit Test: Theory and Implementation in R and Python, Lecture notes of Accounting

Toggle sidebar
bg1
Review Section 7.5 in Goldsman, D. & Goldsman, P. (2020). A First Course in Probability and
Statistics. Lulu. Download link
Goodness-of-Fit Test
A goodness-of-fit test helps us to see if our observed data fits a certain distribution. In this
context, we're interested in seeing if a set of random variables, , comes from a
Uniform(0,1) distribution. This is our null hypothesis .
We first divide the unit interval (0 to 1) into k cells or subintervals. If we divide the unit interval
into equal parts, each observation will fall into one of these cells with a probability of 1/k.
We then count the number of observations that fall into each of these k cells. Let's denote the
number of 's in cell as . Since the 's are independently and identically distributed,
follows a binomial distribution with parameters and . That's because each observation
has a chance of falling into a particular cell, and we have n independent "trials"
(observations).
We expect each cell to have observations on average. So (the expected count for cell
) is .
We then compute the chi-square goodness-of-fit statistic, which measures how far away our
observed counts are from what we'd expect if the null hypothesis were true. If this statistic is
large, it suggests that our observed data doesn't fit the Uniform(0,1) distribution well.
We reject our null hypothesis if the chi-square statistic is greater than the quantile of the
chi-square distribution with k-1 degrees of freedom. Otherwise, we fail to reject the null
hypothesis.
The chi-square goodness-of-fit test involves an approximation. The chi-square distribution is
used to calculate the p-value for the test, but the actual distribution of the test statistic depends
on the true underlying distribution of the data. The chi-square distribution is used because it is a
convenient and relatively accurate approximation when certain conditions are met.
These conditions are captured by the "rule of thumb" that each expected count should be at
least 5 and the total count n should be at least 30:
Each expected count should be at least 5: When the expected counts in all cells are at least 5,
the chi-square distribution is a good approximation for the distribution of the test statistic. If
some expected counts are less than 5, especially if they are 0, the approximation is less
accurate, because the chi-square distribution does not well approximate the distribution of the
test statistic in this case. It can lead to a test that rejects the null hypothesis too often.
pf3
pf4
pf5

Partial preview of the text

Download Goodness-of-Fit Test: Theory and Implementation in R and Python and more Lecture notes Accounting in PDF only on Docsity!

Review Section 7.5 in Goldsman, D. & Goldsman, P. (2020). A First Course in Probability and Statistics. Lulu. Download link

Goodness-of-Fit Test

A goodness-of-fit test helps us to see if our observed data fits a certain distribution. In this context, we're interested in seeing if a set of random variables, , comes from a Uniform(0,1) distribution. This is our null hypothesis.

We first divide the unit interval (0 to 1) into k cells or subintervals. If we divide the unit interval

into equal parts, each observation will fall into one of these cells with a probability of 1/k.

We then count the number of observations that fall into each of these k cells. Let's denote the

number of 's in cell as. Since the 's are independently and identically distributed,

follows a binomial distribution with parameters and. That's because each observation

has a chance of falling into a particular cell, and we have n independent "trials" (observations).

We expect each cell to have observations on average. So (the expected count for cell

) is.

We then compute the chi-square goodness-of-fit statistic, which measures how far away our observed counts are from what we'd expect if the null hypothesis were true. If this statistic is large, it suggests that our observed data doesn't fit the Uniform(0,1) distribution well.

We reject our null hypothesis if the chi-square statistic is greater than the quantile of the chi-square distribution with k-1 degrees of freedom. Otherwise, we fail to reject the null hypothesis.

The chi-square goodness-of-fit test involves an approximation. The chi-square distribution is used to calculate the p-value for the test, but the actual distribution of the test statistic depends on the true underlying distribution of the data. The chi-square distribution is used because it is a convenient and relatively accurate approximation when certain conditions are met.

These conditions are captured by the "rule of thumb" that each expected count should be at least 5 and the total count n should be at least 30:

Each expected count should be at least 5: When the expected counts in all cells are at least 5, the chi-square distribution is a good approximation for the distribution of the test statistic. If some expected counts are less than 5, especially if they are 0, the approximation is less accurate, because the chi-square distribution does not well approximate the distribution of the test statistic in this case. It can lead to a test that rejects the null hypothesis too often.

The total count n should be at least 30: As with many statistical methods, the chi-square test becomes more accurate as the sample size increases. A minimum sample size of 30 is a common rule of thumb for a number of statistical methods, and it's also applicable here. With smaller sample sizes, the chi-square distribution is a less accurate approximation for the distribution of the test statistic.

However, remember that these are only guidelines, and not strict rules. The chi-square test can still be used when these conditions aren't met, but caution is needed in interpreting the results, as the risk of a false positive (Type I error) can be higher. In cases where the expected frequencies are small or the sample size is small, other types of goodness-of-fit tests may be more appropriate.

The advantage of using the approximation formula

comes when the number of degrees of freedom (k-1 in this case) is large. As k (the number of cells or subintervals) increases, the chi-square distribution begins to approximate a normal distribution. This is a result of the Central Limit Theorem, which states that the sum of a large number of independent and identically distributed (i.i.d) variables will approximate a normal distribution.

Under these conditions, the distribution of the test statistic can be approximated by a normal distribution and it becomes computationally simpler and faster to calculate the critical value using the standard normal quantile ( ) instead of referring to the chi-square distribution.

This approximation becomes particularly important in scenarios where we have a large number of cells (like in the context of PRN generator testing), where computation of the exact chi-square quantile might be computationally expensive or potentially unfeasible. Moreover, when we are dealing with such large sample sizes, the approximation tends to be quite accurate.

Therefore, the use of this approximation can be seen as a trade-off between computational complexity and accuracy, which is manageable because the approximation becomes more accurate as the number of cells (k) increases.

This approximation is part of the Wilson–Hilferty transformation, which becomes more accurate as the degrees of freedom increase (i.e., large k). Authors often emphasize “large k” to guarantee the theoretical underpinnings hold well.

The practical need for this approximation is mostly historical or for cases of extreme computational constraints. The approximation is surprisingly good even at small k, and modern computing makes the issue mostly moot unless you’re in a very constrained environment.

Goodness-of-Fit Test Example R Code

This code defines a function chi_sq_gof_test() that performs a chi-square goodness of fit (GoF) test. This test is useful for determining if a given set of data follows a specified distribution—in this case, a uniform distribution on the interval [0,1]. The function accepts the following parameters:

  1. rng_func: A random number generating function that specifies the distribution from which to generate the data.
  2. seed: A seed value for the random number generator to ensure reproducibility.
  3. n: The number of random variates to generate.
  4. k: The number of bins to use in the test.
  5. alpha: The significance level of the test.

Here is what the function does in more detail:

  1. Sets the seed for the random number generator using set.seed(). This ensures the random data generated in each run of the function will be identical, provided the same seed is used.
  2. Generates n random numbers using the specified random number generating function (rng_func).
  3. Counts the number of generated numbers that fall within each of k equally sized subintervals (or bins) of the [0,1] interval. These counts are the observed counts (observed).
  4. Computes the expected counts (expected), which are equal for each bin since we assume a uniform distribution.
  5. Computes the chi-square test statistic (chi_sq_stat), which measures the difference between the observed and expected counts. The larger the test statistic, the larger the discrepancy between the observed and expected counts.
  6. Computes the critical value (chi_sq_critical) from the chi-square distribution with k - 1 degrees of freedom and the specified significance level alpha.
  7. Finally, the function decides whether to reject or fail to reject the null hypothesis (H_0) that the data follows a Uniform(0,1) distribution based on whether the test statistic exceeds the critical value.

Then, it uses the defined function to perform the chi-square GoF test for data generated from three different distributions: Uniform(0,1), Exponential(1), and Normal(0,1). It should reject the null hypothesis for data from the Exponential(1) and Normal(0,1) distributions, and fail to reject it for data from the Uniform(0,1) distribution, assuming the sample sizes are large enough.

# Chi-square GoF test for Uniform(0,1) chi_sq_gof_test <- function (rng_func, seed, n, k, alpha) { # Set seed for reproducibility set.seed(seed)

# Generate data data <- rng_func(n)

# Perform Chi-Square GoF Test observed <- table(cut(data, breaks = k)) # observed counts cat("Observed") print(observed) expected <- rep(n/k, k) # expected counts cat("\n\nExpected\n") cat(expected) cat("\n\n(Observed - Expected)^2 / Expected\n") cat((as.numeric(observed) - expected)^ 2 / expected) chi_sq_stat <- sum((as.numeric(observed) - expected)^ 2 / expected) cat("\n\nChi^2 Test Stat\n") cat(chi_sq_stat)

# Compute critical value chi_sq_critical <- qchisq( 1 - alpha, df = k - 1 )

# Decide to reject or not reject H_ if (chi_sq_stat > chi_sq_critical) { cat("\n\nReject H_0: The data does not follow a Uniform(0,1) distribution") } else { cat("\n\nFail to reject H_0: The data follows a Uniform(0,1) distribution") } }

chi_sq_gof_test(runif, 1234 , 1000 , 5 , 0.05) # Uniform(0,1) data

Observed

(-0.000657,0.2] (0.2,0.4] (0.4,0.6] (0.6,0.8]

(0.8,1]

190 205 198 199

208

Expected

200 200 200 200 200

(Observed - Expected)^2 / Expected

0.5 0.125 0.02 0.005 0.

Chi^2 Test Stat