Chi-Square test of a sample

The following Chi-Square tests seem to work in investigating which of the different input data samples follow a Gamma probability distribution.

One of the references used was this presentation by Dr. Wolfgang Rolke.

HypothesisTests_asymmetric_distributions

using Random, Distributions
using StatsBase, HypothesisTests

n = 1000     # number of data points in sample
nbins = 10   # equi-probable bins will be computed via quantiles

# Draw random samples from asymmetric distributions:
Y1 = rand(LogNormal(), n)       # lognormal distribution
Y2 = rand(Gamma(), n)           # Gamma distribution
Y3 = rand(Chi(1), n)            # Chi(1) distribution

# Test Gamma on Y1:
bins1 = quantile(Y1, LinRange(1/nbins,1,nbins))
h1 = fit(Histogram, Y1, bins1)
fd1 = fit(Gamma,Y1)
theta1 = diff(cdf.(fd1,bins1))
ChisqTest(h1.weights, theta1/sum(theta1))  # Χ² = 34  -> Reject H0

# Test Gamma on Y2:
bins2 = quantile(Y2, LinRange(1/nbins,1,nbins))
h2 = fit(Histogram, Y2, bins2)
fd2 = fit(Gamma,Y2)
theta2 = diff(cdf.(fd2,bins2))
ChisqTest(h2.weights, theta2/sum(theta2))  # Χ² = 4.7  -> Fail to reject H0

# Test Gamma on Y3:
bins3 = quantile(Y3, LinRange(1/nbins,1,nbins))
h3 = fit(Histogram, Y3, bins3)
fd3 = fit(Gamma,Y3)
theta3 = diff(cdf.(fd3,bins3))
ChisqTest(h3.weights, theta3/sum(theta3))  #  Χ² = 29.6  -> Reject H0

NB: a word of caution, produced by a non-statistician, just a Julia user

1 Like