File: plot_test_normality.py

package info (click to toggle)
openturns 1.26-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 67,708 kB
  • sloc: cpp: 261,605; python: 67,030; ansic: 4,378; javascript: 406; sh: 185; xml: 164; makefile: 101
file content (95 lines) | stat: -rw-r--r-- 2,550 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""
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 otv


# %%
# 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 = otv.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 = otv.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 :math:`\beta = 1` and :math:`\gamma = 0`.
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(),
)

# %%
test_result = ot.NormalityTest.AndersonDarlingNormal(sample2)
print(
    "Component is normal?",
    test_result.getBinaryQualityMeasure(),
    "p-value=%.6g" % test_result.getPValue(),
    "threshold=%.6g" % test_result.getThreshold(),
)

# %%
test_result = ot.NormalityTest.CramerVonMisesNormal(sample1)
print(
    "Component is normal?",
    test_result.getBinaryQualityMeasure(),
    "p-value=%.6g" % test_result.getPValue(),
    "threshold=%.6g" % test_result.getThreshold(),
)

# %%
test_result = ot.NormalityTest.CramerVonMisesNormal(sample2)
print(
    "Component is normal?",
    test_result.getBinaryQualityMeasure(),
    "p-value=%.6g" % test_result.getPValue(),
    "threshold=%.6g" % test_result.getThreshold(),
)

# %%
# Display all figures
otv.View.ShowAll()