feat(terskler): demonstrated effect, the halves of rho_D and the conditions taken together (clarification 115)

A threshold counts when the quantity has a source, its first-order index
for the condition is above the chi-square limit, and, for rho_N, the target
holds under the condition in each half of the draws by rho_D. For a solution
whose conditions can be named, the share of the draws that meets the chosen
conditions together is given, and the share of them in which M1 to M4 hold.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Kjell Tore Guttormsen 2026-11-13 19:39:36 +01:00
commit 1da369daa4
Signed by: ktg
SSH key fingerprint: SHA256:JakMjO6FTBBzN0Bhfj9saOoEjaFxlSdYuZQQpM/lF9Q
5 changed files with 455 additions and 40 deletions

View file

@ -157,14 +157,117 @@ def test_terskelen_trenger_en_verdi_per_trekning() -> None:
terskler.terskel([], [], Retning.HOYST)
def test_terskler_for_et_maal_teller_bare_for_storrelser_med_kilde() -> None:
def _kjerne(t: Terskel) -> tuple[object, ...]:
"""A threshold without its index, which is compared for itself."""
return (
t.storrelse,
t.retning,
t.verdi,
t.sannsynlighet,
t.andel_oppfylt,
t.teller,
t.kilde,
t.paavist,
t.begge_halvdeler,
)
def test_med_ti_trekninger_kan_ingen_virkning_paavises() -> None:
"""Two classes of five: the index is 2/3, and the limit is 10.83 / 10, above one."""
oppfylt: list[bool | None] = [v <= 6 for v in X10]
storrelser = {"a": X10, "b": [11.0 - v for v in X10], "c": [1.0] * 10}
status = {"a": "kilde", "b": "åpen forutsetning", "c": "kilde"}
assert terskler.terskler_for_maal(storrelser, status, oppfylt) == (
Terskel("a", Retning.HOYST, 7.0, 0.7, 6 / 7, teller=True),
Terskel("b", Retning.MINST, 4.0, 0.7, 6 / 7, teller=False),
a, b = terskler.terskler_for_maal(storrelser, status, oppfylt)
assert _kjerne(a) == ("a", Retning.HOYST, 7.0, 0.7, 6 / 7, False, True, False, None)
assert _kjerne(b) == ("b", Retning.MINST, 4.0, 0.7, 6 / 7, False, False, False, None)
# between the classes 5 * 0.4^2 + 5 * 0.4^2 = 1.6, over all the draws 10 * 0.6 * 0.4 = 2.4
assert a.indeks == pytest.approx(2 / 3)
assert b.indeks == pytest.approx(2 / 3)
X40 = [float(v) for v in range(1, 41)]
"""Forty draws: the 10th percentile is 4.9, the 90th 36.1, and the classes hold 14, 13 and 13."""
Z40 = [
float(v)
for v in (
*(*range(1, 12), *range(15, 24), *range(28, 36)), # the 28 draws that meet the condition
*(12, 13, 14, 24, 25, 26, 27, 36, 37, 38, 39, 40), # the 12 that do not
)
]
"""A quantity the outcome below hardly depends on: 3, 4 and 5 misses in its three classes."""
def test_grensen_for_paavist_virkning_er_kjikvadratpersentilen_over_antall_trekninger() -> None:
"""With two degrees of freedom the 99.9th percentile is -2 ln 0.001 = 13.8155."""
assert terskler.grense_paavist(40, 3) == pytest.approx(-2 * math.log(0.001) / 40)
assert terskler.grense_paavist(10, 2) == pytest.approx(10.8276 / 10, abs=1e-5)
assert terskler.grense_paavist(10_000, 50) == pytest.approx(0.0085, abs=5e-5)
assert terskler.grense_paavist(10, 1) is None
def test_en_terskel_teller_bare_naar_virkningen_er_paavist() -> None:
"""Met where x <= 28, 70 % of forty draws. The limit for three classes is 0.3454.
x: «at most 35» gives 28 of 35. Its classes have the averages 1, 1 and 1/13, so the
index is (27 * 0.3^2 + 13 * (1/13 - 0.7)^2) / (40 * 0.7 * 0.3) = 0.890: demonstrated.
z: the misses lie at 12-14, 24-27 and 36-40, so «at most 35» again gives 28 of 35. Its
classes have the averages 11/14, 9/13 and 8/13 and the index 0.023: not demonstrated.
"""
oppfylt: list[bool | None] = [v <= 28 for v in X40]
status = {"x": "kilde", "z": "kilde", "aapen": "åpen forutsetning"}
x, z, aapen = terskler.terskler_for_maal({"x": X40, "z": Z40, "aapen": X40}, status, oppfylt)
assert _kjerne(x) == ("x", Retning.HOYST, 35.0, 0.875, 0.8, True, True, True, None)
assert _kjerne(z) == ("z", Retning.HOYST, 35.0, 0.875, 0.8, False, True, False, None)
assert _kjerne(aapen) == ("aapen", Retning.HOYST, 35.0, 0.875, 0.8, False, False, True, None)
assert x.indeks == pytest.approx((27 * 0.09 + 13 * (1 / 13 - 0.7) ** 2) / 8.4)
assert z.indeks == pytest.approx(
(14 * (11 / 14 - 0.7) ** 2 + 13 * (9 / 13 - 0.7) ** 2 + 13 * (8 / 13 - 0.7) ** 2) / 8.4
)
assert terskler.virkning(X40, oppfylt) == (x.indeks, True)
# the same outcome in every draw: the index has no value and nothing is demonstrated
assert terskler.virkning(X40, [True] * 40) == (None, False)
# a quantity with one value has one class and no degree of freedom
assert terskler.virkning([1.0] * 40, oppfylt) == (0.0, False)
def test_en_terskel_i_rho_N_maa_holde_i_begge_halvdeler_av_rho_D() -> None:
"""Met where rho_N <= 30: «at most 36» gives 30 of 36.
With rho_D alternating, each half has 18 draws under the condition and 15 that meet
it. With rho_D equal to rho_N the upper half (21 to 40) has 16 draws under it and 10
that meet it, 62.5 %: the threshold then carries the effect of rho_D and does not count.
"""
oppfylt: list[bool | None] = [v <= 30 for v in X40]
status = {"rho_N": "kilde", "rho_D": "åpen forutsetning"}
veksler = [float(j % 2) for j in range(40)]
(t,) = terskler.terskler_for_maal({"rho_N": X40, "rho_D": veksler}, status, oppfylt)
assert _kjerne(t) == ("rho_N", Retning.HOYST, 36.0, 0.9, 30 / 36, True, True, True, True)
assert terskler.holder_i_begge_halvdeler(X40, veksler, oppfylt, Retning.HOYST, 36.0)
n, d = terskler.terskler_for_maal({"rho_N": X40, "rho_D": X40}, status, oppfylt)
assert _kjerne(n) == ("rho_N", Retning.HOYST, 36.0, 0.9, 30 / 36, False, True, True, False)
# the assumption itself has a threshold that does not count, and no halves to hold in
assert _kjerne(d) == ("rho_D", Retning.HOYST, 36.0, 0.9, 30 / 36, False, False, True, None)
# exactly 80 % in each half is enough: at most 25 is 20 of 20 below and 4 of 5 above
tett: list[bool | None] = [v != 25.0 for v in X40]
assert terskler.holder_i_begge_halvdeler(X40, X40, tett, Retning.HOYST, 25.0)
# a half the condition covers no draw in does not hold: at most 20 leaves the upper half empty
assert not terskler.holder_i_begge_halvdeler(X40, X40, [True] * 40, Retning.HOYST, 20.0)
# at least 21 covers only the upper half
assert not terskler.holder_i_begge_halvdeler(X40, X40, [True] * 40, Retning.MINST, 21.0)
assert terskler.holder_i_begge_halvdeler(X40, veksler, [True] * 40, Retning.MINST, 21.0)
def test_en_terskel_i_rho_N_trenger_trekningene_av_rho_D() -> None:
oppfylt: list[bool | None] = [v <= 30 for v in X40]
with pytest.raises(ValueError, match="needs the drawn values of rho_D"):
terskler.terskler_for_maal({"rho_N": X40}, {"rho_N": "kilde"}, oppfylt)
with pytest.raises(ValueError, match="for the same draws"):
terskler.holder_i_begge_halvdeler(X40, X10, oppfylt, Retning.HOYST, 36.0)
# another pairing can be passed in; without any, no quantity has halves to hold in
(t,) = terskler.terskler_for_maal({"rho_N": X40}, {"rho_N": "kilde"}, oppfylt, partnere={})
assert (t.teller, t.begge_halvdeler) == (True, None)
# ------------------------------------------------------------ the threshold surfaces