oracle7-slim / tests /statistics /test_binomial_probability_binding.py
psiloceyeben's picture
Release 10.3.1 (slim): ember arlynx's #8 (DNA strand direction) and #9 (probability literals), the math organ's number grammar, the echo guard by value; slim carries 10.3's coverage work; release test pre-registration v40, run once; hashes anchored on Solana
0ab7697 verified
Raw History Blame Contribute Delete
5.86 kB
"""Independent regressions for untrusted Python binomial probability readers.
No verified theorem is claimed. Expected values come from enumerating all
Bernoulli sequences with exact rational mass, independently of the organ's
closed-form coefficient formula. Run with ORACLE7_TEST_ROOT pointing to a
checkout or python -B -m unittest discover -s tests/statistics.
"""
from fractions import Fraction
import importlib
from itertools import product
import math
import os
from pathlib import Path
import sys
import unittest
ROOT = Path(os.environ.get('ORACLE7_TEST_ROOT', Path(__file__).resolve().parents[2]))
SPECS = [('statistics', 'organ_stats_v1', 'StatsOrgan'),
('statistics-v2', 'organ_stats_v2', 'StatsOrganV2'),
('statistics-v3', 'organ_stats_v3', 'StatsOrganV3')]
def organ(sid, module, cls):
for name in ('organ_stats_v1', 'organ_stats_v2', 'organ_stats_v3'):
sys.modules.pop(name, None)
sys.path.insert(0, str(ROOT/'root/specialists'/sid))
try:
return getattr(importlib.import_module(module), cls)()
finally:
sys.path.pop(0)
def mass(k, n, p):
return sum((math.prod(p if bit else 1-p for bit in bits)
for bits in product((0, 1), repeat=n) if sum(bits) == k), Fraction())
def question(p, n=2, k=1):
return f'What is the probability of exactly {k} successes in {n} trials with p = {p}?'
class BinomialProbabilityBinding(unittest.TestCase):
def test_percent_exponent_and_signed_scalar_preserve_probability(self):
literals = [('0.5%', Fraction(1, 200)), ('.5%', Fraction(1, 200)),
('0.5 %', Fraction(1, 200)), ('50.0%', Fraction(1, 2)),
('1.5%', Fraction(3, 200)), ('100.0%', Fraction(1)),
('+0.5%', Fraction(1, 200)), ('0.5e-2', Fraction(1, 200)),
('5e-1%', Fraction(1, 200)), ('5E-1%', Fraction(1, 200)),
('0', Fraction()), ('1', Fraction(1)), ('25%', Fraction(1, 4))]
for spec in SPECS:
instance = organ(*spec)
for literal, p in literals:
for n in range(0, 6):
for k in range(n+1):
with self.subTest(spec=spec[0], literal=literal, n=n, k=k):
reply = instance.answer(question(literal, n, k))
self.assertIsNotNone(reply)
self.assertEqual(reply['status'], 'answered')
self.assertAlmostEqual(reply['value'], float(mass(k, n, p)), places=13)
def test_all_probability_role_spellings(self):
for spec in SPECS[1:]:
instance = organ(*spec)
for role in ['p = 0.5%', 'chance of success is 0.5%',
'probability of success on each trial is 0.5%',
'success probability is 0.5%', 'with probability 0.5%']:
with self.subTest(spec=spec[0], role=role):
reply = instance.answer(f'Probability of exactly 1 success in 2 trials, {role}?')
self.assertEqual(reply['status'], 'answered')
self.assertAlmostEqual(reply['value'], 0.00995, places=13)
def test_out_of_domain_scalar_is_withheld(self):
for spec in SPECS:
instance = organ(*spec)
for literal in ['-0.5', '-0.5%', '1.01', '101%', '100.1%', '2', '1e2']:
with self.subTest(spec=spec[0], literal=literal):
reply = instance.answer(question(literal))
self.assertIsNotNone(reply)
self.assertEqual(reply['status'], 'withheld')
def test_exact_domain_and_float_endpoint_refusals(self):
literals = ['1e-10000', '1e10000', '-1e-10000',
'1.000000000000000000000000001',
'0.999999999999999999999999999',
'100.000000000000000000000001%',
'99.9999999999999999999999999%',
'1e10001', '1e-10001', '1e999999999999999999999999',
'0.' + '0'*4096 + '1']
for spec in SPECS:
instance = organ(*spec)
for literal in literals:
with self.subTest(spec=spec[0], literal=literal[:70]):
reply = instance.answer(question(literal))
self.assertIsNotNone(reply)
self.assertEqual(reply['status'], 'withheld')
# Subnormal but nonzero probabilities remain distinguishable from
# the endpoint, and the existing formula can represent this mass.
with self.subTest(spec=spec[0], literal='1e-320'):
reply = instance.answer(question('1e-320', n=1, k=1))
self.assertIsNotNone(reply)
self.assertEqual(reply['status'], 'answered')
self.assertGreater(reply['value'], 0)
self.assertEqual(reply['value'], float(Fraction(1, 10**320)))
for literal in ['0e-10000', '1.000000000000000000000000000', '100.0%']:
with self.subTest(spec=spec[0], literal=literal):
reply = instance.answer(question(literal, n=1, k=1))
self.assertIsNotNone(reply)
self.assertEqual(reply['status'], 'answered')
def test_no_numeric_prefix_for_malformed_or_unsupported_scalar(self):
for spec in SPECS:
instance = organ(*spec)
for literal in ['0.5e', '0.5e-', '0.5.2', '0.5%%', '0.5abc', '0.5/2',
'0.5+0.1', '0.5,2', '0.5 % %', '0.5e-2oops']:
with self.subTest(spec=spec[0], literal=literal):
reply = instance.answer(question(literal))
self.assertTrue(reply is None or reply['status'] == 'withheld', reply)
if __name__ == '__main__':
unittest.main()