Note
Go to the end to download the full example code.
Test Normality¶
Normal fitting test using the Henry line¶
In this paragraph we perform a visual goodness-of-fit test for a univariate Normal distribution using the Henry line test, which is the QQ plot adapted for Gaussian distributions.
import openturns as ot
import openturns.viewer as viewer
ot.Log.Show(ot.Log.NONE)
We first create the data :
distribution = ot.Normal(2.0, 0.5)
sample1 = distribution.getSample(100)
We draw the Henry line plot and expect a good fitting :
graph = ot.VisualTest.DrawHenryLine(sample1)
view = viewer.View(graph)
For comparison sake e draw the Henry line plot for a Beta distribution. The result is expected to be bad.
sample2 = ot.Beta(0.7, 0.9, 0.0, 2.0).getSample(100)
graph = ot.VisualTest.DrawHenryLine(sample2)
view = viewer.View(graph)
Normality tests¶
We use two tests to check whether a sample follows a Normal distribution :
the Anderson-Darling test
the Cramer-Von Mises test
We first generate two samples, one from a standard unit Gaussian and another from a Gumbel distribution with parameters and .
sample1 = ot.Normal().getSample(200)
sample2 = ot.Gumbel().getSample(200)
We test the normality of the sample. We can display the result of the test as a yes/no answer with the getBinaryQualityMeasure. We can retrieve the p-value and the threshold with the getPValue and getThreshold methods.
test_result = ot.NormalityTest.AndersonDarlingNormal(sample1)
print(
"Component is normal?",
test_result.getBinaryQualityMeasure(),
"p-value=%.6g" % test_result.getPValue(),
"threshold=%.6g" % test_result.getThreshold(),
)
Component is normal? True p-value=0.695477 threshold=0.05
test_result = ot.NormalityTest.AndersonDarlingNormal(sample2)
print(
"Component is normal?",
test_result.getBinaryQualityMeasure(),
"p-value=%.6g" % test_result.getPValue(),
"threshold=%.6g" % test_result.getThreshold(),
)
Component is normal? False p-value=3.47192e-06 threshold=0.05
test_result = ot.NormalityTest.CramerVonMisesNormal(sample1)
print(
"Component is normal?",
test_result.getBinaryQualityMeasure(),
"p-value=%.6g" % test_result.getPValue(),
"threshold=%.6g" % test_result.getThreshold(),
)
Component is normal? True p-value=0.798882 threshold=0.05
test_result = ot.NormalityTest.CramerVonMisesNormal(sample2)
print(
"Component is normal?",
test_result.getBinaryQualityMeasure(),
"p-value=%.6g" % test_result.getPValue(),
"threshold=%.6g" % test_result.getThreshold(),
)
Component is normal? False p-value=4.80269e-05 threshold=0.05