tokenfabrikker/tests/test_dommer.py
Kjell Tore Guttormsen d607128ec6
feat(kode): bring the ownership comparison in line with clarifications 62-69
eierskap.py: the residual value and the sale at the final year, the levies,
the private owners' cash flow and the settlement over the draws, the present
value with rho_D without the debt flows, the required return before tax, and
equal values in the ranking. Also the median of the petroleum cash flow in
2025 kroner, the state's cash flow next to M6, the shares shown next to each
verdict, and the sources' figures for M5 (i).

Made-up numbers only: 424 tests pass, 7 documented misses. The project's
code and the independent reader's code agree to within one krone on 1 500
made-up paths.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-12 17:17:13 +01:00

289 lines
12 KiB
Python

"""The verdicts (chapter 11 and the pre-registration), tested on invented outcomes.
No array here comes from the model run on the frozen parameters: the outcomes per draw are
made up, and the two conditions that are read from the power system are computed on the
made-up set in ``conftest.py``. Nothing here says anything about the hypothesis.
"""
from __future__ import annotations
import math
from typing import TYPE_CHECKING
import pytest
from tokenfabrikker import dommer
from tokenfabrikker import kontantstrom as ks
from tokenfabrikker import ligninger as lg
from tokenfabrikker.dommer import Dom, Forsyning, Grunnlag, Trinnvalg
from tokenfabrikker.kontantstrom import Knapphet
from tokenfabrikker.utbygging import Losning
if TYPE_CHECKING:
from conftest import LagInndata
from tokenfabrikker.inndata import Inndata
E_D = 8760.0 * 0.75 * 1.2 * 1000.0
"""Yearly energy to one made-up data centre: 7 884 000 MWh."""
_UENDRET = Forsyning(ikke_svakere_uten_tiltak=True, svakere_ogsaa_med_tiltak=False)
_EFFEKT_MANGLER = Forsyning(ikke_svakere_uten_tiltak=False, svakere_ogsaa_med_tiltak=False)
_ENERGI_MANGLER = Forsyning(ikke_svakere_uten_tiltak=False, svakere_ogsaa_med_tiltak=True)
def _utfall(*deler: tuple[float | None, int]) -> list[float | None]:
"""Invented outcomes: each value repeated the given number of draws."""
return [verdi for verdi, antall in deler for _ in range(antall)]
def _regn(inn: Inndata, losning: Losning, *, N_anlegg: int = 1) -> ks.Programresultat:
dim = ks.dimensjoner(inn, losning)
plan = ks.lag_plan(inn, losning, dim, N_anlegg=N_anlegg)
return ks.regn_program(inn, losning, plan, Knapphet.NORMALISERING, dim=dim)
# ------------------------------------------------------------------- shares
def test_s37_andel() -> None:
assert lg.s37_andel([True, False, True, True]) == 0.75
with pytest.raises(ValueError, match="at least one draw"):
lg.s37_andel([])
def test_trekning_uten_verdi_teller_i_nevneren() -> None:
utfall = _utfall((1.0, 6), (-1.0, 2), (None, 2))
assert dommer.andel_positiv(utfall) == 0.6
assert dommer.andel(utfall, 0.0, minst=True) == 0.6
assert dommer.andel(utfall, 0.0, minst=False) == 0.2
assert dommer.median(utfall) == 1.0
assert dommer.median(_utfall((1.0, 4), (None, 6))) == -math.inf
# -------------------------------------------------------------- M1, M2, M7
@pytest.mark.parametrize(
("positive", "negative", "uten", "dom"),
[
(80, 20, 0, Dom.HOLDER),
(79, 21, 0, Dom.BETINGELSER),
(20, 80, 0, Dom.BETINGELSER),
(19, 81, 0, Dom.HOLDER_IKKE),
(80, 0, 20, Dom.HOLDER),
(79, 0, 21, Dom.BETINGELSER),
(19, 0, 81, Dom.HOLDER_IKKE),
],
)
def test_dom_m1(positive: int, negative: int, uten: int, dom: Dom) -> None:
"""Positive in at least 80 % of all draws holds; in under 20 % it does not."""
assert dommer.dom_m1(_utfall((5.0, positive), (-5.0, negative), (None, uten))) is dom
def test_dom_m2_teller_null_som_oppfylt() -> None:
"""S42 is an inequality with >=: a sum of exactly zero meets it."""
assert dommer.dom_m2(_utfall((0.0, 80), (-1.0, 20))) is Dom.HOLDER
assert dommer.dom_m2(_utfall((0.0, 50), (-1.0, 50))) is Dom.BETINGELSER
assert dommer.dom_m2(_utfall((1.0, 10), (-1.0, 50), (None, 40))) is Dom.HOLDER_IKKE
def test_dom_m7_a_mot_b_og_c() -> None:
"""A is best in 5 of 10 draws: holds. A draw where A cannot be built is not one of them."""
A = _utfall((10.0, 5), (1.0, 3), (None, 2))
B = _utfall((5.0, 5), (2.0, 3), (-9.0, 2))
C = _utfall((None, 5), (0.0, 5))
assert dommer.a_er_best(A, B, C) == [True] * 5 + [False] * 5
assert dommer.dom_m7(A, B, C) is Dom.HOLDER
alltid_bedre = _utfall((5.0, 5), (2.0, 5))
assert dommer.dom_m7(_utfall((10.0, 4), (1.0, 6)), alltid_bedre, C) is Dom.BETINGELSER
assert dommer.dom_m7(_utfall((10.0, 1), (1.0, 9)), alltid_bedre, C) is Dom.HOLDER_IKKE
# equal is not best
assert dommer.a_er_best([1.0], [1.0], [0.0]) == [False]
# ------------------------------------------------------------------ M3 and M4
def test_m3_verste_prisvirkning_i_R(oppdiktet: LagInndata) -> None:
"""One facility in R takes 3.942 TWh from the balance: 39.42 kr/MWh on a price of 600.
In the dry year the effect is 1.2 times larger; the price level 1.5 is in both numerator
and denominator. The made-up lines are the same in all areas.
"""
inn = oppdiktet()
res = _regn(inn, Losning.R)
normal = 10e-6 * 0.5 * E_D / 600.0
assert dommer.m3_verste_prisvirkning(inn, res) == pytest.approx(1.2 * normal)
assert dommer.m3_verste_prisvirkning(inn, _regn(inn, Losning.A)) == 0.0
# B produces more than the data centres use: the price falls
assert dommer.m3_verste_prisvirkning(inn, _regn(inn, Losning.B)) < 0
def test_dom_m3() -> None:
assert dommer.dom_m3(_utfall((0.05, 80), (0.2, 20))) is Dom.HOLDER
assert dommer.dom_m3(_utfall((0.051, 80), (0.2, 20))) is Dom.BETINGELSER
assert dommer.dom_m3(_utfall((0.16, 80), (0.0, 20))) is Dom.HOLDER_IKKE
assert dommer.dom_m3(_utfall((0.15, 80), (0.0, 20))) is Dom.BETINGELSER
# a draw without a value meets neither condition
assert dommer.dom_m3(_utfall((0.16, 79), (None, 21))) is Dom.BETINGELSER
assert dommer.dom_m3(_utfall((0.0, 79), (None, 21))) is Dom.BETINGELSER
def test_m4_forsyning_i_de_fem_losningene(oppdiktet: LagInndata) -> None:
"""A changes nothing. R only takes power. B and one facility in C add more than they take.
With three facilities C has used up hydropower and wind on land; offshore wind counts
nothing in the scarcest hour, so capacity is missing, but energy is not.
"""
inn = oppdiktet()
assert dommer.m4_forsyning(inn, _regn(inn, Losning.A)) == _UENDRET
assert dommer.m4_forsyning(inn, _regn(inn, Losning.R)) == _ENERGI_MANGLER
assert dommer.m4_forsyning(inn, _regn(inn, Losning.B)) == _UENDRET
assert dommer.m4_forsyning(inn, _regn(inn, Losning.C)) == _UENDRET
tre = _regn(inn, Losning.C, N_anlegg=3)
assert dommer.m4_forsyning(inn, tre) == _EFFEKT_MANGLER
def test_m4_C_dekker_akkurat_forbruket_i_toerraaret(oppdiktet: LagInndata) -> None:
"""With nothing displaced in the grid queue, C's dry-year balance is zero, not weaker."""
inn = oppdiktet(delta_V=0.0, kappa_vind=1.0)
res = _regn(inn, Losning.C)
assert max(abs(rad.Delta_Q_tort) for rad in res.kraftaar) < 1.0
assert not dommer.m4_forsyning(inn, res).svakere_ogsaa_med_tiltak
def test_dom_m4() -> None:
ok, effekt, energi = _UENDRET, _EFFEKT_MANGLER, _ENERGI_MANGLER
assert dommer.dom_m4([ok] * 8 + [energi] * 2) is Dom.HOLDER
assert dommer.dom_m4([ok] * 7 + [effekt] * 3) is Dom.BETINGELSER
assert dommer.dom_m4([effekt] * 10) is Dom.BETINGELSER
assert dommer.dom_m4([energi] * 8 + [ok] * 2) is Dom.HOLDER_IKKE
assert dommer.dom_m4([energi] * 7 + [None] * 3) is Dom.BETINGELSER
# -------------------------------------------------------- M6 and the ladder
def test_dom_m6() -> None:
assert dommer.dom_m6(_utfall((0.25, 80), (0.0, 20))) is Dom.HOLDER
assert dommer.dom_m6(_utfall((0.24, 80), (0.0, 20))) is Dom.BETINGELSER
assert dommer.dom_m6(_utfall((0.049, 80), (0.3, 20))) is Dom.HOLDER_IKKE
assert dommer.dom_m6(_utfall((0.05, 80), (0.3, 20))) is Dom.BETINGELSER
# a path that cannot be built does not reach 5 %
assert dommer.dom_m6(_utfall((0.3, 20), (None, 80))) is Dom.HOLDER_IKKE
def _stige(**andel_per_trinn: tuple[float, int]) -> dict[int, dict[Losning, list[float | None]]]:
"""A made-up ladder: for A, M6 is the given value in the given number of 10 draws.
B can be built in the same draws and contributes nothing; R reaches 90 % everywhere.
"""
stige = {}
for navn, (verdi, antall) in andel_per_trinn.items():
trinn = int(navn.removeprefix("n"))
stige[trinn] = {
Losning.A: _utfall((verdi, antall), (None, 10 - antall)),
Losning.B: _utfall((0.0, antall), (None, 10 - antall)),
Losning.R: _utfall((0.9, 10)),
}
return stige
def test_dommene_leses_paa_det_laveste_trinnet_som_gir_25_prosent() -> None:
stige = _stige(n1=(0.03, 10), n5=(0.1, 10), n10=(0.3, 8), n20=(0.5, 10))
assert dommer.trinn_for_dommene(stige) == Trinnvalg(10, Grunnlag.TRINN_25)
def test_ellers_paa_trinnet_som_gir_5_prosent() -> None:
"""25 % is reached in only 7 of 10 draws on the top step; 5 % in 8 of 10 from step 5."""
stige = _stige(n1=(0.03, 10), n5=(0.1, 8), n10=(0.3, 7))
assert dommer.trinn_for_dommene(stige) == Trinnvalg(5, Grunnlag.TRINN_5)
def test_ellers_paa_hoyeste_trinn_som_lar_seg_bygge() -> None:
"""No step reaches 5 %. Step 10 can be built in 7 of 10 draws, step 5 in 9."""
stige = _stige(n1=(0.01, 10), n5=(0.03, 9), n10=(0.04, 7))
assert dommer.trinn_for_dommene(stige) == Trinnvalg(5, Grunnlag.HOYESTE)
def test_referansekjoringen_kan_ikke_gi_trinnet() -> None:
"""R reaches 90 % on every step here, but only A, B and C decide the step."""
stige = _stige(n1=(0.0, 10), n5=(0.0, 10))
assert dommer.trinn_for_dommene(stige).grunnlag is Grunnlag.HOYESTE
def test_ingen_trinn_lar_seg_bygge() -> None:
stige = {
1: {
Losning.A: _utfall((None, 10)),
Losning.B: _utfall((0.1, 7), (None, 3)),
}
}
assert dommer.trinn_for_dommene(stige) == Trinnvalg(None, Grunnlag.INGEN)
# ---------------------------------------------------------------- overall verdicts
def _dommer(**endringer: Dom) -> dict[str, Dom]:
alle = dict.fromkeys(dommer.MAAL_DEL_I, Dom.HOLDER)
alle.update(endringer)
return alle
def test_del_I_holder_naar_en_losning_holder_paa_alle_maal() -> None:
per_losning = {
Losning.A: _dommer(M1=Dom.HOLDER_IKKE),
Losning.B: _dommer(),
Losning.C: _dommer(M3=Dom.BETINGELSER),
}
median = {Losning.A: -1.0, Losning.B: 5.0, Losning.C: 5.0}
paa_25 = Trinnvalg(10, Grunnlag.TRINN_25)
assert dommer.del_I(per_losning, median, paa_25) is Dom.HOLDER
# on the 5 % step, or with a single facility, it can at most hold under conditions
assert dommer.del_I(per_losning, median, Trinnvalg(10, Grunnlag.TRINN_5)) is Dom.BETINGELSER
assert dommer.del_I(per_losning, median, Trinnvalg(1, Grunnlag.TRINN_25)) is Dom.BETINGELSER
def test_del_I_holder_ikke_naar_hver_losning_er_felt() -> None:
"""A fails M1, B has a negative median, C fails M4. M6 alone cannot fell a solution."""
per_losning = {
Losning.A: _dommer(M1=Dom.HOLDER_IKKE),
Losning.B: _dommer(M1=Dom.BETINGELSER),
Losning.C: _dommer(M4=Dom.HOLDER_IKKE),
}
median = {Losning.A: -1.0, Losning.B: -0.5, Losning.C: 5.0}
valg = Trinnvalg(10, Grunnlag.TRINN_25)
assert dommer.del_I(per_losning, median, valg) is Dom.HOLDER_IKKE
per_losning[Losning.C] = _dommer(M6=Dom.HOLDER_IKKE)
assert dommer.del_I(per_losning, median, valg) is Dom.BETINGELSER
# without conditions that can be named within the range of the sources it does not hold
assert dommer.del_I(per_losning, median, valg, betingelser_kan_navngis=False) is Dom.HOLDER_IKKE
# the pre-registration says a negative median; a median of exactly zero does not fell
median[Losning.B] = 0.0
per_losning[Losning.C] = _dommer(M4=Dom.HOLDER_IKKE)
assert dommer.del_I(per_losning, median, valg) is Dom.BETINGELSER
assert dommer.del_I(per_losning, median, Trinnvalg(None, Grunnlag.INGEN)) is Dom.HOLDER_IKKE
def test_del_I_ser_bare_paa_A_B_og_C() -> None:
"""The reference run R holds on everything here; it cannot give «holder»."""
per_losning = {
Losning.A: _dommer(M2=Dom.HOLDER_IKKE),
Losning.R: _dommer(),
}
median = {Losning.A: 1.0, Losning.R: 9.0}
assert dommer.del_I(per_losning, median, Trinnvalg(10, Grunnlag.TRINN_25)) is Dom.HOLDER_IKKE
def test_del_II_folger_M7() -> None:
assert dommer.del_II(Dom.BETINGELSER) is Dom.BETINGELSER
def test_hvor_mye_av_dommen_som_skyldes_baner_som_ikke_lar_seg_bygge() -> None:
"""Clarification 69: 2 of 8 draws cannot be built; 3 of the 6 others meet the condition."""
oppfylt = [True, None, False, True, None, False, True, False]
assert dommer.ved_siden_av_dommen(oppfylt) == pytest.approx((0.25, 0.5))
assert dommer.ved_siden_av_dommen([None, None]) == (1.0, None)
assert dommer.ved_siden_av_dommen([True, True]) == (0.0, 1.0)