File: plot_smirnov_test.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 (76 lines) | stat: -rw-r--r-- 2,334 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
"""
Test identical distributions
============================
"""

# %%

# %%
# In this example we are going to estimate whether two samples follow the same
# distribution using the two samples Kolmogorov-Smirnov test and the graphical QQ-plot test.
#
# The Smirnov test relies on the maximum distance between the cumulative distribution function.
# If :math:`F_{n_1}^{*}` and :math:`F_{n_2}^{*}` are the empirical cumulative
# density functions of both samples of size :math:`n_1` and :math:`n_2`,
# the Smirnov test evaluates the decision variable:
#
# .. math::
#    D^2 = \displaystyle \sqrt{\frac{n_1n_2}{n_1+n_2}} \sup_{x}|F_{n_1}^{*}(x) - F_{n_2}^{*}(x)|
#
# which tends towards the Kolmogorov distribution.
# The hypothesis of same distribution is rejected if :math:`D^2` is too high
# (depending on the p-value threshold).
#
# The QQ-plot graph plots empirical quantiles levels from two samples.
# If both samples correspond to the same probability distribution the curve
# should be close to the diagonal.
#

# %%
import openturns as ot
import openturns.viewer as otv


# %%
# Generate 3 samples, `sample1` and `sample2` arise from the same distribution
distribution1 = ot.Gumbel(0.2, 0.5)
distribution2 = ot.Uniform()

ot.RandomGenerator.SetSeed(5)
sample1 = distribution1.getSample(100)
sample2 = distribution1.getSample(100)
sample3 = distribution2.getSample(100)

# %%
# Visually compare `sample1` and `sample2` using QQ-plot
graph = ot.VisualTest.DrawQQplot(sample1, sample2)
view = otv.View(graph)

# %%
# Visually compare `sample1` and `sample3` using QQ-plot
graph = ot.VisualTest.DrawQQplot(sample1, sample3)
view = otv.View(graph)

# %%
# Numerically test `sample1` against `sample2`
test_result = ot.HypothesisTest.TwoSamplesKolmogorov(sample1, sample2)
print(
    "Samples follow the same distribution?",
    test_result.getBinaryQualityMeasure(),
    "p-value=%.6g" % test_result.getPValue(),
    "threshold=%.6g" % test_result.getThreshold(),
)

# %%
# Numerically test `sample1` against `sample3`
test_result = ot.HypothesisTest.TwoSamplesKolmogorov(sample1, sample3)
print(
    "Samples follow the same distribution?",
    test_result.getBinaryQualityMeasure(),
    "p-value=%.6g" % test_result.getPValue(),
    "threshold=%.6g" % test_result.getThreshold(),
)

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