File: t_Binomial_std.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 (189 lines) | stat: -rwxr-xr-x 6,122 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#! /usr/bin/env python

# %%
import openturns as ot
import openturns.testing as ott
import os

# %%
ot.TESTPREAMBLE()

# Instantiate one distribution object
distribution = ot.Binomial(15, 0.7)
print("Distribution ", repr(distribution))
print("Distribution ", distribution)

# Is this distribution elliptical ?
print("Elliptical = ", distribution.isElliptical())

# Is this distribution continuous ?
print("Continuous = ", distribution.isContinuous())

# Test for realization of distribution
oneRealization = distribution.getRealization()
print("oneRealization=", repr(oneRealization))

# Define a point
point = ot.Point(distribution.getDimension(), 5.0)
print("Point= ", repr(point))

# PDF value
PDF = distribution.computePDF(point)
print("pdf     =%.6f" % PDF)

# derivative of the PDF with regards the parameters of the distribution
CDF = distribution.computeCDF(point)
print("cdf=%.6f" % CDF)
# quantile
quantile = distribution.computeQuantile(0.95)
print("quantile=", repr(quantile))
print("cdf(quantile)=%.6f" % distribution.computeCDF(quantile))
print("entropy=%.6f" % distribution.computeEntropy())
mean = distribution.getMean()
print("mean=", repr(mean))
standardDeviation = distribution.getStandardDeviation()
print("standard deviation=", repr(standardDeviation))
skewness = distribution.getSkewness()
print("skewness=", repr(skewness))
kurtosis = distribution.getKurtosis()
print("kurtosis=", repr(kurtosis))
covariance = distribution.getCovariance()
print("covariance=", repr(covariance))
parameters = distribution.getParametersCollection()
print("parameters=", repr(parameters))
print("Standard representative=", distribution.getStandardRepresentative())
# Confidence interval
alpha = 0.05
bounds = distribution.computeBilateralConfidenceInterval(1 - alpha)
print("%.2f%% bilateral confidence interval" % ((1 - alpha) * 100), " =", bounds)

# %%
# check survival at upper bound
distribution = ot.Binomial(10, 1.0)
assert distribution.computeCDF(10.0) == 1.0
assert distribution.computeComplementaryCDF(10.0) == 0.0
assert distribution.computeSurvivalFunction(10.0) == 0.0

# negative quantile bug
distribution = ot.Binomial(3, 0.5)
assert distribution.computeScalarQuantile(0.9, True) == 0

# %%
print("Check on dataset")
separator = ","
path = os.path.join(os.path.dirname(__file__), "t_Binomial_std.csv")
sample = ot.Sample.ImportFromTextFile(path, separator)

rtol = 1.0e-12
atol = ot.SpecFunc.MinScalar
sample_size = sample.getSize()
for i in range(sample_size):
    x, n, pr, expected_pdf, expected_cdfp, expected_cdfq = sample[i]
    x = int(x)
    n = int(n)
    distribution = ot.Binomial(n, pr)
    computed_p = distribution.computePDF(x)
    computed_cdf = distribution.computeCDF(x)
    computed_ccdf = distribution.computeComplementaryCDF(x)
    computed_surv = distribution.computeSurvivalFunction(x)
    print(
        f"row = {i}/{sample_size}, x = {x}, n = {n}, pr = {pr}, "
        f"P(X = x) = {expected_pdf}, CDF = {expected_cdfp}, Compl. CDF = {expected_cdfq}"
    )
    # Check PDF
    print(
        f"    computePDF. Computed = {computed_p:.11e}, expected = {expected_pdf:.11e}"
    )
    ott.assert_almost_equal(computed_p, expected_pdf, rtol, atol)
    # Check CDF
    print(
        f"    computeCDF. Computed = {computed_cdf:.11e}, expected = {expected_cdfp:.11e}"
    )
    ott.assert_almost_equal(computed_cdf, expected_cdfp, rtol, atol)
    # Check complementary CDF
    print(
        f"    computeComplementaryCDF. Computed = {computed_ccdf:.11e}, "
        f"expected = {expected_cdfq:.11e}"
    )
    ott.assert_almost_equal(computed_ccdf, expected_cdfq, rtol, atol)
    # Check Survival
    print(
        f"    computeSurvivalFunction. Computed = {computed_surv:.11e}, "
        f"expected = {computed_ccdf:.11e}"
    )
    ott.assert_almost_equal(computed_surv, computed_ccdf, rtol, atol)
    # Check quantile
    if pr == 0.0 and x < n:
        # The function is not invertible
        # for this particular input.
        continue
    elif expected_cdfp < expected_cdfq:
        computed_x = int(distribution.computeQuantile(computed_cdf)[0])
        cdfXM1 = distribution.computeCDF(computed_x - 1)
        cdfX = distribution.computeCDF(computed_x)
        print(
            f"    computeQuantile (A). Computed X = {computed_x}, "
            f"F(X - 1) = {cdfXM1:.7e}, F(X) = {cdfX:.7e}"
        )
        if computed_cdf == 0.0:
            assert computed_x == 0.0
        else:
            assert cdfXM1 < computed_cdf and computed_cdf <= cdfX
    else:
        computed_x = int(distribution.computeQuantile(computed_ccdf, True)[0])
        print(f"    computeQuantile (B). Computed X = {computed_x}, expected = {x}")
        assert x == computed_x
    if i > 0:
        ott.assert_almost_equal(
            distribution.computeScalarQuantile(expected_cdfp), x, 0.0, 1.0
        )  # Can be off by 1 unit


# %%
# quantile bug
alpha = 0.05
beta = 0.05
for n in range(59, 100):
    d = ot.Binomial(n, alpha)
    k = d.computeQuantile(beta)[0]
    p1 = d.computeCDF(k - 1)
    p2 = d.computeCDF(k)
    ok = p1 < beta <= p2
    print(f"n={n} k={k:.0f} p(k-1)={p1:.4f} p(k)={p2:.4f} ok={ok}")
    assert ok

ot.Log.Show(ot.Log.TRACE)
validation = ott.DistributionValidation(distribution)
validation.run()

# Corner cases

# 2147483647 is the maximum integer.
# Values greater than this are not doubles anymore.
binomial = ot.Binomial()
N = 2147483647
binomial.setN(N)
binomial.setP(1.0 / N)
computed = binomial.computePDF(1.0)
expected = 0.3678794
ott.assert_almost_equal(computed, expected, 1.0e-6, 0.0)

computed = binomial.computePDF(2.0)
expected = 0.1839397
ott.assert_almost_equal(computed, expected, 1.0e-6, 0.0)

# Extreme inputs
binomial = ot.Binomial()
binomial.setN(9999)
binomial.setP(0.5)
computed = binomial.computePDF(4999.0)
expected = 0.0079786461393821558191
ott.assert_almost_equal(computed, expected, 1.0e-7, 0.0)

# Check pdf for values of P closer to 1
binomial = ot.Binomial()
binomial.setN(2)
binomial.setP(0.9999)
computed = binomial.computePDF(1.0)
expected = 1.999799999999779835e-04
ott.assert_almost_equal(computed, expected, 1e-12, 0.0)