Skip to content

Commit

Permalink
New Binomial unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
mbaudin47 committed Sep 4, 2024
1 parent 63a8fa4 commit 84597f3
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
51 changes: 51 additions & 0 deletions python/test/t_Binomial_std.expout
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,57 @@ kurtosis= class=Point name=Unnamed dimension=1 values=[2.91746]
covariance= class=CovarianceMatrix dimension=1 implementation=class=MatrixImplementation name=Unnamed rows=1 columns=1 values=[3.15]
parameters= [class=PointWithDescription name=X0 dimension=2 description=[n,p] values=[15,0.7]]
Standard representative= Binomial(n = 15, p = 0.7)
95.00% bilateral confidence interval = [7, 14]
Check on dataset
row = 0/37, x = 0, n = 10, pr = 0.0, P(X = x) = 1.0, CDF = 1.0, Compl. CDF = 0.0
computePDF. Computed = 1.0, expected = 1.0, abs.err. = 0.0
computeCDF. Computed = 1.0, expected = 1.0, abs.err. = 0.0
computeComplementaryCDF. Computed = 0.0, expected = 0.0, abs.err. = 0.0
row = 1/37, x = 10, n = 10, pr = 0.0, P(X = x) = 0.0, CDF = 1.0, Compl. CDF = 0.0
computePDF. Computed = 0.0, expected = 0.0, abs.err. = 0.0
computeCDF. Computed = 1.0, expected = 1.0, abs.err. = 0.0
computeComplementaryCDF. Computed = 0.0, expected = 0.0, abs.err. = 0.0
computeQuantile. Computed X = 10, expected = 10, diff = 0
row = 2/37, x = 0, n = 10, pr = 1.0, P(X = x) = 0.0, CDF = 0.0, Compl. CDF = 1.0
computePDF. Computed = 0.0, expected = 0.0, abs.err. = 0.0
computeCDF. Computed = 0.0, expected = 0.0, abs.err. = 0.0
computeComplementaryCDF. Computed = 1.0, expected = 1.0, abs.err. = 0.0
computeQuantile. Computed X = 0, expected = 0, diff = 0
row = 3/37, x = 10, n = 10, pr = 1.0, P(X = x) = 1.0, CDF = 1.0, Compl. CDF = 0.0
computePDF. Computed = 1.0, expected = 1.0, abs.err. = 0.0
computeCDF. Computed = 1.0, expected = 1.0, abs.err. = 0.0
computeComplementaryCDF. Computed = 0.0, expected = 0.0, abs.err. = 0.0
computeQuantile. Computed X = 10, expected = 10, diff = 0
row = 4/37, x = 1, n = 1030, pr = 0.5, P(X = x) = 8.952445602587756e-308, CDF = 8.96113729734744e-308, Compl. CDF = 1.0
computePDF. Computed = 8.952445602586738e-308, expected = 8.952445602587756e-308, abs.err. = 1.018e-320
computeCDF. Computed = 8.961137297347362e-308, expected = 8.96113729734744e-308, abs.err. = 7.9e-322
computeComplementaryCDF. Computed = 1.0, expected = 1.0, abs.err. = 0.0
computeQuantile. Computed X = 1, expected = 1, diff = 0
row = 5/37, x = 400, n = 1030, pr = 0.5, P(X = x) = 1.4435068396634365e-13, CDF = 3.897352466243578e-13, Compl. CDF = 0.9999999999996103
computePDF. Computed = 1.4435068396634415e-13, expected = 1.4435068396634365e-13, abs.err. = 5.048709793414476e-28
computeCDF. Computed = 3.8973524662436084e-13, expected = 3.897352466243578e-13, abs.err. = 3.0292258760486853e-27
computeComplementaryCDF. Computed = 0.9999999999996103, expected = 0.9999999999996103, abs.err. = 0.0
computeQuantile. Computed X = 400, expected = 400, diff = 0
row = 6/37, x = 589, n = 1030, pr = 0.5, P(X = x) = 5.835293291163824e-07, CDF = 0.9999983439256698, Compl. CDF = 1.6560743301714625e-06
computePDF. Computed = 5.835293291163835e-07, expected = 5.835293291163824e-07, abs.err. = 1.0587911840678754e-21
computeCDF. Computed = 0.9999983439256698, expected = 0.9999983439256698, abs.err. = 0.0
computeComplementaryCDF. Computed = 1.6560743301714386e-06, expected = 1.6560743301714625e-06, abs.err. = 2.3928680759933985e-20
computeQuantile. Computed X = 589, expected = 589, diff = 0
row = 7/37, x = 1, n = 30, pr = 0.1, P(X = x) = 0.14130386091738736, CDF = 0.18369501919260356, Compl. CDF = 0.8163049808073964
computePDF. Computed = 0.14130386091738736, expected = 0.14130386091738736, abs.err. = 0.0
computeCDF. Computed = 0.18369501919260367, expected = 0.18369501919260356, abs.err. = 1.1102230246251565e-16
computeComplementaryCDF. Computed = 0.8163049808073963, expected = 0.8163049808073964, abs.err. = 1.1102230246251565e-16
computeQuantile. Computed X = 1, expected = 1, diff = 0
row = 8/37, x = 1, n = 50, pr = 0.001, P(X = x) = 0.047607889299150725, CDF = 0.9988135174961821, Compl. CDF = 0.001186482503817932
computePDF. Computed = 0.047607889299150725, expected = 0.047607889299150725, abs.err. = 0.0
computeCDF. Computed = 0.9988135174961821, expected = 0.9988135174961821, abs.err. = 0.0
computeComplementaryCDF. Computed = 0.0011864825038179335, expected = 0.001186482503817932, abs.err. = 1.5178830414797062e-18
computeQuantile. Computed X = 1, expected = 1, diff = 0
row = 9/37, x = 16, n = 30, pr = 0.999, P(X = x) = 1.4311328174833965e-34, CDF = 1.4326623224074817e-34, Compl. CDF = 1.0
computePDF. Computed = 1.4311328174833965e-34, expected = 1.4311328174833965e-34, abs.err. = 0.0
computeCDF. Computed = 1.432662322407474e-34, expected = 1.4326623224074817e-34, abs.err. = 7.697562365065523e-49
computeComplementaryCDF. Computed = 1.0, expected = 1.0, abs.err. = 0.0
computeQuantile. Computed X = 17, expected = 16, diff = 1
95.00% bilateral confidence interval = [7, 13]
n=59 k=1 p(k-1)=0.0485 p(k)=0.1991 ok=True
n=60 k=1 p(k-1)=0.0461 p(k)=0.1916 ok=True
Expand Down
45 changes: 45 additions & 0 deletions python/test/t_Binomial_std.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#! /usr/bin/env python

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

ot.TESTPREAMBLE()

Expand Down Expand Up @@ -80,6 +82,49 @@
distribution = ot.Binomial(3, 0.5)
assert distribution.computeScalarQuantile(0.9, True) == 0

print("Check on dataset")
separator = ","
skipped_lines = 13
path = os.path.join(os.path.dirname(__file__), "t_binomial_dataset.csv")
sample = ot.Sample.ImportFromTextFile(path, separator, skipped_lines)
rtol = 1.e-12
atol = 1.e-300
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)
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}, expected = {expected_pdf}, abs.err. = {abs(computed_p - expected_pdf)}")
ott.assert_almost_equal(computed_p, expected_pdf, rtol, atol)
# Check CDF
print(f" computeCDF. Computed = {computed_cdf}, expected = {expected_cdfp}, abs.err. = {abs(computed_cdf - expected_cdfp)}")
ott.assert_almost_equal(computed_cdf, expected_cdfp, rtol, atol)
# Check complementary CDF
print(f" computeComplementaryCDF. Computed = {computed_ccdf}, expected = {expected_cdfq}, abs.err. = {abs(computed_ccdf - expected_cdfq)}")
ott.assert_almost_equal(computed_ccdf, expected_cdfq, 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(expected_cdfp)[0])
print(f" computeQuantile. Computed X = {computed_x}, expected = {x}, diff = {abs(computed_x - x)}")
assert x == computed_x
else:
computed_x = int(distribution.computeQuantile(expected_cdfq, True)[0])
print(f" computeQuantile. Computed X = {computed_x}, expected = {x}, diff = {abs(computed_x - x)}")
assert x == computed_x


# %%
# quantile bug
alpha = 0.05
beta = 0.05
Expand Down
2 changes: 1 addition & 1 deletion python/test/t_binomial_dataset.csv
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# Assessing the quality of the Binomial distribution function
# Table of inputs computed using R software
x,N,pr,PDF-P,CDF-P,CDF-Q
"x","N","pr","PDF-P","CDF-P","CDF-Q"
0,10,0,1,1,0
10,10,0,0,1,0
0,10,1,0,0,1
Expand Down

0 comments on commit 84597f3

Please sign in to comment.