"""Truth tables, set operations and conditional probability with exact fractions."""
from fractions import Fraction
from itertools import product
import json
import platform
cases = []
def check(name, inputs, expected, actual):
assert actual == expected, (name, expected, actual)
cases.append(dict(name=name, input=inputs, expected=expected, actual=actual, passed=True))
def probabilities(prevalence, sensitivity, false_positive):
p, t, f = (Fraction(value, 100) for value in (prevalence, sensitivity, false_positive))
positive = p*t + (1-p)*f
return str(positive), str(p*t/positive) if positive else None
by_inputs = {
(True, True): ("imp_true_true", [True, True, False, True, True]),
(True, False): ("imp_true_false", [False, True, False, False, False]),
(False, True): ("imp_false_true", [False, True, True, True, False]),
(False, False): ("imp_false_false", [False, False, True, True, True]),
}
for p, q in product((True, False), repeat=2):
name, expected = by_inputs[p, q]
check(name, [p, q], expected, [p and q, p or q, not p, (not p) or q, p == q])
check("boolean_conditions_short_circuit", "empty list and indexing at zero",
[False, True], [bool([] and [][0]), bool(True or [][0])])
universe = set(range(1, 11))
a, b = {2, 3, 5, 6}, {5, 6, 8}
sets = [sorted(a), sorted(b), sorted(universe)]
check("membership_and_nonmembership", [sorted(a), 3, 4], [True, True], [3 in a, 4 not in a])
check("empty_set_and_subset", sets, [True, True, False, True], [
{1, 4}.isdisjoint(a), {2, 3} <= a, a < a, set() <= a])
check("sets_union", sets, [2, 3, 5, 6, 8], sorted(a | b))
check("sets_intersection", sets, [5, 6], sorted(a & b))
check("sets_left_difference", sets, [2, 3], sorted(a - b))
check("sets_right_difference", sets, [8], sorted(b - a))
check("sets_symmetric_difference", sets, [2, 3, 8], sorted(a ^ b))
check("sets_complement", sets, [1, 4, 7, 8, 9, 10], sorted(universe - a))
check("sets_membership_correspondence", sets, [True]*3, [
all((x in (a | b)) == (x in a or x in b) for x in universe),
all((x in (a & b)) == (x in a and x in b) for x in universe),
all((x in (universe - a)) == (x not in a) for x in universe)])
evens = {x for x in range(1, 13) if x % 2 == 0}
multiples = {x for x in range(1, 13) if x % 3 == 0}
check("sets_even_and_multiples", [1, 12], [[6, 12], [2, 3, 4, 6, 8, 9, 10, 12]],
[sorted(evens & multiples), sorted(evens | multiples)])
die = set(range(1, 7))
even_results = {x for x in die if x % 2 == 0}
above_four = {x for x in die if x > 4}
check("dice_events", "fair die, even and above 4", [[2,4,6],[6],[1,3,5]],
[sorted(even_results), sorted(even_results & above_four), sorted(die - even_results)])
check("events_inclusion_exclusion", sets, 5, len(a) + len(b) - len(a & b))
check("mutually_exclusive_events", [[1, 3], [2, 4]], True, {1, 3}.isdisjoint({2, 4}))
check("card_events", [52, 13, 4, 1], ["1/52", "4/13", "1/4"],
[str(Fraction(1, 52)), str(Fraction(13+4-1, 52)), str(Fraction(1,4))])
check("bayes_rare_prior", [1, 95, 5], ["59/1000","19/118"], list(probabilities(1, 95, 5)))
check("bayes_reference_slider", [1, 90, 9], ["981/10000","10/109"], list(probabilities(1, 90, 9)))
check("bayes_zero_evidence", [10, 0, 0], ["0", None], list(probabilities(10, 0, 0)))
check("bayes_positive_without_false_positives", [1, 95, 0], ["19/2000","1"], list(probabilities(1, 95, 0)))
print(json.dumps(dict(version=platform.python_version(), cases=cases), ensure_ascii=False))
本例需在 Windows、Python 3.12.14、Chromium、HTML 与 SVG。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows、Python 3.12.14、Chromium、HTML 与 SVG。
入口文件:projects/ai-math-logic-lab/logic-numerics.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
logic-numerics.py,或下载右侧课程工程包。 - 确认本地已准备 Windows、Python 3.12.14、Chromium、HTML 与 SVG。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联54章) · 含当前章节或实例 · mbskycom202612a33.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a68.zip