tokenfabrikker/tests/test_eierskap.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

767 lines
34 KiB
Python

"""Business cash flow, tax and ownership (chapters 7 and 8): S28, S29 and S30.
Made-up round numbers only; the ownership forms are those of ``conftest.py``, not the
parameter file's. Nothing here says anything about the hypothesis.
"""
from __future__ import annotations
import dataclasses
import math
from typing import TYPE_CHECKING
import numpy as np
import pytest
from tokenfabrikker import eierskap as ei
from tokenfabrikker import kontantstrom as ks
from tokenfabrikker import ligninger as lg
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."""
def _crf(rho: float, levetid: float) -> float:
return float(rho / (1.0 - (1.0 + rho) ** -levetid))
def _lcoc_v() -> float:
"""S15 for the made-up competitor, per MW IT and year."""
return 100e6 * _crf(0.08, 15) + 200e6 * _crf(0.08, 5) + 3e6 + 8760.0 * 0.75 * 1.2 * 800.0
def _regn(
inn: Inndata,
losning: Losning,
*,
N_anlegg: int = 1,
knapphet: Knapphet = Knapphet.NORMALISERING,
) -> 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, dim=dim)
def _lonnsom(inn: Inndata) -> ei.Bedriftsstrom:
"""One facility in R with the scarcity markup: the made-up price is about twice the cost."""
return ei.bedriftsstrom(inn, _regn(inn, Losning.R, knapphet=Knapphet.VARER))
# ------------------------------------------------------- the equations one at a time
def test_avskrivning_er_lineaer_over_levetiden() -> None:
aar = [
lg.s28_avskrivning(kostnad=100.0, levetid=4, start=2030, slutt=2033, t=t)
for t in range(2029, 2035)
]
assert aar == pytest.approx([0.0, 25.0, 25.0, 25.0, 25.0, 0.0])
def test_resten_skrives_av_naar_eiendelen_gaar_ut_for_tiden() -> None:
"""A lifetime of ten years, but the facility goes out after three: 10, 10 and the last 80."""
aar = [
lg.s28_avskrivning(kostnad=100.0, levetid=10, start=2030, slutt=2032, t=t)
for t in range(2030, 2034)
]
assert aar == pytest.approx([10.0, 10.0, 80.0, 0.0])
assert math.fsum(aar) == pytest.approx(100.0)
def test_underskudd_fores_fram_uten_rente() -> None:
"""A loss of 100 is used against 60 and then against 40 of the next 80."""
assert lg.s28_etter_framforing([-100.0, 60.0, 80.0, 50.0]) == (0.0, 0.0, 40.0, 50.0)
assert lg.s28_etter_framforing([10.0, -5.0, -5.0, 30.0]) == (10.0, 0.0, 0.0, 20.0)
def test_s28_framfort_underskudd_taper_realverdi() -> None:
"""Without interest means nominal: a loss of 100 is worth 100 / 1.1 the year after.
With 10 % price growth: the loss of 100 is 90.91 in year 2, where 60 is used (30.91 left);
28.10 in year 3, where it is used up and 80 - 28.10 is taxed.
"""
grunnlag = lg.s28_etter_framforing([-100.0, 60.0, 80.0], faktor=1.0 / 1.1)
assert grunnlag[:2] == (0.0, 0.0)
assert grunnlag[2] == pytest.approx(80.0 - (100.0 / 1.1 - 60.0) / 1.1)
assert lg.s28_realverdi(belop=121.0, iota=0.1, aar=2) == pytest.approx(100.0)
assert lg.s28_realverdi(belop=121.0, iota=0.0, aar=7) == 121.0
def test_s28_naaverdi() -> None:
assert lg.s28_naaverdi(netto=[-100.0, 0.0, 121.0], rente=0.1) == pytest.approx(0.0)
def test_s29_kapitalkostnad() -> None:
"""Half debt at 5 % (4 % after 20 % tax) and half equity at 10 %: 7 % nominal, 4.9 % real."""
r_e = lg.s29_kapitalkostnad(h=0.5, r_d=0.05, r_q=0.10, tau_s=0.2, iota=0.02)
assert r_e == pytest.approx(1.07 / 1.02 - 1.0)
assert lg.realrente(nominell=0.071, iota=0.05) == pytest.approx(0.02)
def test_s30_skatt_med_egen_framforing_for_saerskatten() -> None:
"""Corporate tax 20 % on profit after the loss; special tax 50 % above 5 % of capital 200.
Corporate tax: 0, 0, 8, 10. The basis of the special tax is the profit minus the corporate
tax and minus 5 % of 200: -100, 50, 62, 30. The negative basis is carried with 5 %
interest: 105 meets the 50 (55 left), 57.75 meets the 62 (4.25 taxed), then 30. The rate
on that basis is 0.5 / 0.8, so one more krone of profit pays 20 % + 50 %.
"""
selskap, saer = lg.s30_skatt(
overskudd=[-100.0, 60.0, 80.0, 50.0],
kapital_inn=[0.0, 200.0, 200.0, 200.0],
tau_s=0.2,
tau_g=0.5,
r_e=0.05,
)
assert selskap == pytest.approx((0.0, 0.0, 8.0, 10.0))
assert saer == pytest.approx((0.0, 0.0, 0.625 * 4.25, 0.625 * 30.0))
def test_s30_statens_kontantstrom() -> None:
"""State share 0.6, direct share 0.25, the state carries all overruns (zeta = 1)."""
KS = lg.s30_statens_kontantstrom(
skatt=30.0, FKE=100.0, NK=200.0, overskridelse=40.0, s=0.6, theta=0.25, zeta=1.0
)
assert KS == pytest.approx(30.0 + 0.6 * 0.75 * 100.0 + 0.25 * 200.0 - 0.4 * 0.75 * 40.0)
# with zeta equal to the ownership share the state covers nothing beyond its share
uten = lg.s30_statens_kontantstrom(
skatt=30.0, FKE=100.0, NK=200.0, overskridelse=40.0, s=0.6, theta=0.25, zeta=0.6
)
assert uten == pytest.approx(30.0 + 45.0 + 50.0)
# ------------------------------------------------------------ the business cash flow
def test_R_bedriftsstrom_haandregnet(oppdiktet: LagInndata) -> None:
"""One facility in R to 2040, as a business pays.
Building 110 mrd. kr and first vintage 200, paid in 2028 and 2029; vintages of 200 paid
in 2034 and 2039; no grid investment. In operation it pays operation, power at the
price, electricity tax, grid tariff on 1 200 MW and disconnection for 600 MW.
"""
inn = oppdiktet(T_slutt=2040)
res = _regn(inn, Losning.R)
strom = ei.bedriftsstrom(inn, res)
aar = dict(zip(res.aar, range(len(res.aar)), strict=True))
pris = 700.0 - 10e-6 * (10e6 - 0.5 * E_D)
utkobling = 100.0 * _lcoc_v() / (8760.0 * 1.2) * 600.0
assert strom.inntekt[aar[2031]] == pytest.approx(1000.0 * _lcoc_v())
# the state's levies: the electricity tax, minus what the private data centres that are
# displaced from half of the grid capacity would have paid; the tariff is not the state's
assert strom.avgifter[aar[2031]] == pytest.approx(70.0 * E_D - 0.5 * 70.0 * E_D)
assert strom.drift[aar[2031]] == pytest.approx(
2.5e9 + pris * E_D + 70.0 * E_D + 0.3e6 * 1200.0 + utkobling
)
assert not strom.drift[: aar[2030]].any()
investert = {2028: 155e9, 2029: 155e9, 2034: 200e9, 2039: 200e9}
for t in res.aar:
assert strom.investering[aar[t]] == pytest.approx(investert.get(t, 0.0))
assert (strom.budsjett == strom.investering).all()
# The building over 15 years and one vintage at a time over 5. The deduction is fixed in
# the kroner of the year each half was paid (2028 and 2029; later vintages the year before
# they start) and loses 2 % a year in constant kroner (clarification 47). At the final
# year the project is valued as sold, and what is left is written off (clarification 62):
# 4 of the building's 15 years on top of the year's own, and the whole vintage of 2040.
def realt(aar_siden: int) -> float:
return 1.02**-aar_siden
assert strom.avskrivning[aar[2030]] == pytest.approx(
(55e9 / 15.0 + 20e9) * (realt(2) + realt(1))
)
assert strom.avskrivning[aar[2036]] == pytest.approx(
55e9 / 15.0 * (realt(8) + realt(7)) + 40e9 * realt(2)
)
assert strom.avskrivning[aar[2040]] == pytest.approx(
55e9 * 5.0 / 15.0 * (realt(12) + realt(11)) + 200e9 * realt(1)
)
assert not strom.avskrivning[: aar[2030]].any()
assert strom.kapital[aar[2029]] == pytest.approx(310e9)
assert strom.kapital[aar[2030]] == pytest.approx(310e9 - 110e9 / 15.0 - 40e9)
assert strom.kapital[aar[2040]] == pytest.approx(0.0, abs=1.0)
assert strom.NK[aar[2034]] == pytest.approx(
strom.inntekt[aar[2034]] - strom.drift[aar[2034]] - 200e9
)
# The residual value: the facility runs 20 years from 2030, so 9 are left after 2040.
# The average of the last five years holds one vintage of 200 mrd. kr (paid in 2039).
assert res.anlegg[0].L_rest == 9
snitt = float(strom.inntekt[aar[2040]] - strom.drift[aar[2040]]) - 200e9 / 5.0
ventet = max(0.0, snitt / _crf(0.08, 9))
assert strom.restverdi[aar[2040]] == pytest.approx(ventet)
assert strom.restverdi_anlegg == pytest.approx((ventet,))
assert not strom.restverdi[: aar[2040]].any()
assert strom.NK[aar[2040]] == pytest.approx(
strom.inntekt[aar[2040]] - strom.drift[aar[2040]] + ventet
)
@pytest.mark.parametrize("losning", list(Losning))
@pytest.mark.parametrize(
"endringer", [{}, {"T_slutt": 2045, "u_xi": 0.1}, {"L_N": 12.0, "L_B": 7.0}]
)
def test_eiendelene_gir_de_samme_investeringene_som_kontantstrommen(
oppdiktet: LagInndata, losning: Losning, endringer: dict[str, float]
) -> None:
"""The booked assets, paid over their construction time, are the investments of S34 and S35.
All but the grid connection, which the tariff covers. In A the table for the buffer
also holds its fixed operation, which is taken out here.
"""
inn = oppdiktet(u_eksport=None, **endringer)
res = _regn(inn, losning, N_anlegg=2)
strom = ei.bedriftsstrom(inn, res)
ventet = np.zeros(len(res.aar))
for k, a in enumerate(res.anlegg):
ventet += a.I_bygg + a.I_H + a.I_g + a.buffer
for tabell in a.investering.values():
ventet += tabell
if losning is Losning.A:
i_drift = np.array([res.plan.i_drift(k, t) for t in res.aar], dtype=np.float64)
ventet -= i_drift * 0.4e6 * 600.0
for f in res.felles:
ventet += f.investering
assert strom.investering == pytest.approx(ventet)
# the overrun factor is 1.5 in the second set, and only nuclear power carries it
overskridelse = float((strom.investering - strom.budsjett).sum())
if losning.kjernekraft and res.dim.xi_N > 1.0:
eiendeler = [e for a in res.anlegg for e in a.eiendeler]
eiendeler += [e for f in res.felles for e in f.eiendeler]
kjernekraft = math.fsum(e.kostnad for e in eiendeler if e.slag == ks.KJERNEKRAFT)
assert overskridelse == pytest.approx(kjernekraft / 3.0)
else:
assert overskridelse == 0.0
def test_avskrivningene_summerer_seg_til_det_som_er_betalt(oppdiktet: LagInndata) -> None:
"""Every asset is out of operation by 2060, so the booked capital ends at zero.
Without price growth the tax depreciation sums to what is paid; with it, to less.
"""
for iota in (0.0, 0.02):
inn = oppdiktet(iota=iota)
res = _regn(inn, Losning.A)
strom = ei.bedriftsstrom(inn, res)
assert strom.kapital[-1] == pytest.approx(0.0, abs=1.0)
assert (strom.kapital > -1.0).all()
betalt = float(strom.investering.sum())
if iota == 0.0:
assert float(strom.avskrivning.sum()) == pytest.approx(betalt)
else:
assert 0.5 * betalt < float(strom.avskrivning.sum()) < 0.99 * betalt
def test_A_betaler_elavgift_men_ikke_nettariff(oppdiktet: LagInndata) -> None:
inn = oppdiktet()
res = _regn(inn, Losning.A)
strom = ei.bedriftsstrom(inn, res)
j = 2040 - 2027
assert strom.avgifter[j] == pytest.approx(70.0 * E_D)
anlegg = res.anlegg[0]
assert strom.drift[j] == pytest.approx(
2.5e9
+ float(anlegg.kraftdrift[ks.KJERNEKRAFT][j])
+ float(anlegg.ulykke[j])
+ 0.4e6 * 600.0
+ 70.0 * E_D
)
def test_kraftverk_paa_nettet_selger_uten_tilleggsregelen(oppdiktet: LagInndata) -> None:
"""The business keeps the plant's whole sales, also when S45 binds in M1."""
inn = oppdiktet(c_N_ref=10e6, f_N=0.3e6)
res = _regn(inn, Losning.B_MERKET)
assert res.varsigma[ks.KJERNEKRAFT] < 1.0
strom = ei.bedriftsstrom(inn, res)
j = 2040 - 2027
anlegg = res.anlegg[0]
uten_salg = (
2.5e9
+ float(anlegg.kraftdrift[ks.KJERNEKRAFT][j])
+ float(anlegg.ulykke[j])
+ res.kraftaar[j].pris * E_D
+ 70.0 * E_D
+ 0.3e6 * 1200.0
)
assert strom.drift[j] == pytest.approx(uten_salg - float(anlegg.salg[ks.KJERNEKRAFT][j]))
# S30 is computed with the factor of S45 on the sales (chapter 1.4)
med_regel = ei.bedriftsstrom(inn, res, tilleggsregel=True)
assert med_regel.drift[j] == pytest.approx(
uten_salg - res.varsigma[ks.KJERNEKRAFT] * float(anlegg.salg[ks.KJERNEKRAFT][j])
)
assert (med_regel.investering == strom.investering).all()
# -------------------------------------------------------------- the ownership forms
def test_kapitalkostnad_per_eierform(oppdiktet: LagInndata) -> None:
inn = oppdiktet()
assert ei.kapitalkostnad(inn, inn.eierformer["direkte"]) == 0.02
assert ei.kapitalkostnad(inn, inn.eierformer["heleid_selskap"]) == pytest.approx(
1.07 / 1.02 - 1.0
)
assert ei.kapitalkostnad(inn, inn.eierformer["majoritet_boers"]) == pytest.approx(
1.08 / 1.02 - 1.0
)
def test_direkte_eierskap_gir_staten_hele_kontantstrommen(oppdiktet: LagInndata) -> None:
"""No company and no corporate tax: FKE = NK, and the state also receives the levies."""
inn = oppdiktet(T_slutt=2040)
strom = ei.bedriftsstrom(inn, _regn(inn, Losning.R))
ut = ei.eierresultat(inn, strom, inn.eierformer["direkte"])
assert not ut.selskapsskatt.any()
assert not ut.saerskatt.any()
assert (ut.FKE == strom.NK).all()
assert not ut.gjeld.any()
assert ut.KS_stat == pytest.approx(strom.NK + strom.elavgift)
NV = math.fsum(float(x) / 1.02**j for j, x in enumerate(strom.NK))
assert ei.naaverdi_etter_skatt(inn, strom, inn.eierformer["direkte"]) == pytest.approx(NV)
# the state's cash flow is valued with rho_D (clarification 65), with the electricity tax
# the displaced private data centres would have paid deducted (63): the whole surplus
assert strom.elavgift[4] == pytest.approx(70.0 * E_D)
assert strom.fortrengt[4] == pytest.approx(0.5 * 70.0 * E_D)
NV_stat = math.fsum(float(x) / 1.08**j for j, x in enumerate(strom.NK + strom.avgifter))
assert ut.NV_stat == pytest.approx(NV_stat)
assert ut.NV_samlet == pytest.approx(NV_stat)
assert not ut.KS_privat.any()
assert ut.NV_privat == 0.0
# with the state's borrowing rate, shown as a sensitivity
med_laanerente = ei.eierresultat(inn, strom, inn.eierformer["direkte"], rente=0.02)
assert med_laanerente.NV_stat == pytest.approx(
math.fsum(float(x) / 1.02**j for j, x in enumerate(strom.NK + strom.avgifter))
)
# 310 mrd. kr is paid before the first revenue
assert ut.storste_innskutte_kapital == pytest.approx(310e9)
def test_heleid_selskap_skatt_gjeld_og_statens_kontantstrom(oppdiktet: LagInndata) -> None:
"""One wholly owned company, half debt at 5 % nominal (1.05 / 1.02 - 1 real).
Tax is 20 % of revenue minus operation and depreciation; nothing is carried forward
because the first operating year already has a profit. In the final year the residual
value is taxed as a sale. The state owns everything, so the tax cancels: what it gets
is NK and the levies, minus interest, plus new debt.
"""
inn = oppdiktet(T_slutt=2040)
strom = _lonnsom(inn)
assert strom.restverdi[-1] > 0
ut = ei.eierresultat(inn, strom, inn.eierformer["heleid_selskap"])
overskudd = strom.inntekt - strom.drift + strom.restverdi - strom.avskrivning
assert (overskudd[3:] > 0).all()
assert ut.selskapsskatt == pytest.approx(0.2 * overskudd)
assert not ut.saerskatt.any()
assert ut.gjeld[:-1] == pytest.approx(0.5 * strom.kapital[:-1])
assert ut.gjeld[-1] == 0.0
gjeld_inn = np.concatenate(([0.0], ut.gjeld[:-1]))
rente = 1.05 / 1.02 - 1.0
FKE = strom.NK - ut.selskapsskatt - rente * gjeld_inn + ut.gjeld - gjeld_inn
assert ut.FKE == pytest.approx(FKE)
assert ut.KS_stat == pytest.approx(
strom.NK + strom.elavgift - rente * gjeld_inn + ut.gjeld - gjeld_inn
)
r_e = 1.07 / 1.02 - 1.0
NV_e = ei.naaverdi_etter_skatt(inn, strom, inn.eierformer["heleid_selskap"])
assert NV_e == pytest.approx(
math.fsum(float(x) / (1.0 + r_e) ** j for j, x in enumerate(strom.NK - ut.selskapsskatt))
)
# Valued with rho_D, and the debt flows with the borrowing rate, where they are worth
# nothing (clarification 65): the state's value is that of the whole surplus, exactly as
# with direct ownership
assert math.fsum(
float(x) / (1.0 + rente) ** j
for j, x in enumerate(ut.gjeld - gjeld_inn - rente * gjeld_inn)
) == pytest.approx(0.0, abs=1.0)
assert ut.NV_stat == pytest.approx(
math.fsum(float(x) / 1.08**j for j, x in enumerate(strom.NK + strom.avgifter))
)
direkte = ei.eierresultat(inn, strom, inn.eierformer["direkte"])
assert ut.NV_stat == pytest.approx(direkte.NV_stat)
assert ut.andel_av_overskudd == pytest.approx(1.0)
assert not ut.KS_privat.any()
# the owners put in half of what is built, and the interest in 2029 on what was borrowed
# in 2028; the other half is borrowed
assert ut.storste_innskutte_kapital == pytest.approx(155e9 + rente * 77.5e9)
def test_privat_minoritet_og_direkte_andel(oppdiktet: LagInndata) -> None:
"""The petroleum model: the state owns 60 % of the company and 25 % of the facilities directly.
The company's part, 75 %, pays corporate tax and special tax; the state's direct part
does not. Electricity tax and grid tariff come from the whole project.
"""
inn = oppdiktet(T_slutt=2040)
strom = _lonnsom(inn)
ut = ei.eierresultat(inn, strom, inn.eierformer["petroleumsmodell"])
assert ut.saerskatt.sum() > 0
skatt = ut.selskapsskatt + ut.saerskatt
ventet = 0.75 * skatt + strom.elavgift + 0.6 * 0.75 * ut.FKE + 0.25 * strom.NK
assert ut.KS_stat == pytest.approx(ventet)
# the basis of the special tax is the profit with straight-line depreciation in constant
# kroner, minus the corporate tax and minus r_e on the capital booked at the start of the
# year; the rate on it is 0.5 / 0.8. The return on what was booked during construction is
# carried forward with interest and used in 2030
r_e = 1.08 / 1.02 - 1.0
overskudd = strom.inntekt - strom.drift - strom.avskrivning_fast
j = 2031 - 2027
grunnlag = overskudd[j] - ut.selskapsskatt[j] - r_e * strom.kapital[j - 1]
assert ut.saerskatt[j] == pytest.approx(0.5 / 0.8 * grunnlag)
boers = ei.eierresultat(inn, strom, inn.eierformer["majoritet_boers"])
assert boers.NV_stat < ut.NV_stat
# the private owners have 40 % of the company: all of it, or the 75 % outside the state's
# direct part. Their share is valued as the state's: with rho_D and without the debt flows
assert boers.KS_privat == pytest.approx(0.4 * boers.FKE)
assert ut.KS_privat == pytest.approx(0.4 * 0.75 * ut.FKE)
etter_skatt = strom.NK - boers.selskapsskatt
assert boers.NV_privat == pytest.approx(
math.fsum(float(x) / 1.08**j for j, x in enumerate(0.4 * etter_skatt))
)
# what the state and the private owners have together is the whole surplus
assert boers.NV_stat + boers.NV_privat == pytest.approx(boers.NV_samlet)
assert ut.NV_stat + ut.NV_privat == pytest.approx(ut.NV_samlet)
# the special tax lowers the project's value after tax (S28)
assert ei.naaverdi_etter_skatt(
inn, strom, inn.eierformer["majoritet_boers"]
) > ei.naaverdi_etter_skatt(inn, strom, inn.eierformer["petroleumsmodell"])
@pytest.mark.parametrize("navn", ["heleid_selskap", "majoritet_boers", "petroleumsmodell"])
def test_statens_eiernes_og_laangivernes_strommer_summerer_seg(
oppdiktet: LagInndata, navn: str
) -> None:
"""Clarifications 48 and 64: nothing is counted twice or lost between the parties.
What the state, the private owners and the lenders receive adds up to the project's net
cash flow plus the electricity tax, every year: when the state covers overruns by its share only
(zeta = s), and when it covers all of them and the private owners are spared theirs.
"""
inn = oppdiktet(u_xi=0.1)
strom = ei.bedriftsstrom(inn, _regn(inn, Losning.A, knapphet=Knapphet.VARER))
assert (strom.investering - strom.budsjett).sum() > 0
for zeta in (inn.eierformer[navn].s, 1.0):
e = dataclasses.replace(inn.eierformer[navn], zeta=zeta)
ut = ei.eierresultat(inn, strom, e)
gjeld_inn = np.concatenate(([0.0], ut.gjeld[:-1]))
rente = (1.0 + e.r_d) / 1.02 - 1.0
laangivere = (1.0 - e.theta) * (rente * gjeld_inn - (ut.gjeld - gjeld_inn))
assert ut.KS_stat + ut.KS_privat + laangivere == pytest.approx(
strom.NK + strom.elavgift, abs=1.0
)
def test_saerskatten_treffer_bare_avkastning_utover_r_e() -> None:
"""Clarification 48: the special tax is neutral.
100 paid in year 0 for an asset written off over 4 years, r_e 6 % after a corporate tax
of 20 %. The corporate tax has its own basis (20, 30, 10, 40: nominal deductions), so the
tax is 4, 6, 2, 8. A project whose profit after that tax is exactly 6 % of the booked
capital at the start of each year has no special tax, however the corporate tax is
computed. With 10 more in profit a year, corporate tax takes 2 and the special tax 5:
50 % of the 10. Negative bases are carried with interest.
"""
kapital_inn = [0.0, 100.0, 75.0, 50.0, 25.0]
selskapsgrunnlag = [0.0, 20.0, 30.0, 10.0, 40.0]
normal = [0.2 * g + 0.06 * k for g, k in zip(selskapsgrunnlag, kapital_inn, strict=True)]
for ekstra in (0.0, 10.0):
selskap, saer = lg.s30_skatt(
overskudd=[0.0] + [g + ekstra for g in selskapsgrunnlag[1:]],
overskudd_fast=[0.0] + [x + ekstra for x in normal[1:]],
kapital_inn=kapital_inn,
tau_s=0.2,
tau_g=0.5,
r_e=0.06,
iota=0.02,
)
assert saer == pytest.approx((0.0, *([0.5 * ekstra] * 4)))
assert selskap[1] == pytest.approx(0.2 * (20.0 + ekstra))
# a negative basis of 50 in year 1 is worth 53 in year 2
_, saer = lg.s30_skatt(
overskudd=[0.0] * 3,
overskudd_fast=[0.0, -50.0, 60.0],
kapital_inn=[0.0] * 3,
tau_s=0.0,
tau_g=0.5,
r_e=0.06,
)
assert saer == pytest.approx((0.0, 0.0, 0.5 * (60.0 - 53.0))) # tau_s = 0: the rate is tau_g
def test_staten_dekker_overskridelser_ut_over_eierandelen(oppdiktet: LagInndata) -> None:
"""Overrun 50 % on nuclear power; the state owns 60 % but carries all of it (zeta = 1)."""
inn = oppdiktet(u_xi=0.1)
res = _regn(inn, Losning.A)
assert res.dim.xi_N == 1.5
strom = ei.bedriftsstrom(inn, res)
overskridelse = strom.investering - strom.budsjett
assert overskridelse.sum() > 0
ut = ei.eierresultat(inn, strom, inn.eierformer["majoritet_boers"])
skatt = ut.selskapsskatt + ut.saerskatt
uten = skatt + strom.elavgift + 0.6 * ut.FKE
assert ut.KS_stat == pytest.approx(uten - 0.4 * overskridelse)
def test_underskudd_i_oppstarten_gir_ingen_skatt(oppdiktet: LagInndata) -> None:
"""«Overkapasitet»: the price is 30 % under the competitor's cost, and nothing is taxable.
The losses carried forward cover what little profit there is. At the final year the
project is valued as sold (clarification 62), but the value is lower than what is
written off, so the sale gives no tax either.
"""
inn = oppdiktet(T_slutt=2040)
strom = ei.bedriftsstrom(inn, _regn(inn, Losning.R, knapphet=Knapphet.OVERKAPASITET))
assert 0 < strom.restverdi[-1] < strom.avskrivning[-1]
ut = ei.eierresultat(inn, strom, inn.eierformer["heleid_selskap"])
assert not ut.selskapsskatt.any()
assert ut.NV_stat < 0
assert ut.andel_av_overskudd is None
def test_kapitalkostnaden_flytter_kostnaden_for_regnekraft(oppdiktet: LagInndata) -> None:
inn = oppdiktet()
res = _regn(inn, Losning.B_MERKET)
lav = ei.lcoc_med_kapitalkostnad(inn, res, 0.02)
hoy = ei.lcoc_med_kapitalkostnad(inn, res, 0.06)
assert lav < hoy
# ------------------------------------------------------ the criteria over the draws
def test_risikomaalene() -> None:
utfall = [float(x) for x in range(-10, 30)] # 40 draws: -10 .. 29
assert ei.sannsynlighet_for_tap(utfall) == pytest.approx(10 / 40)
# 5 % of 40 draws is the two worst
assert ei.snitt_av_de_daarligste(utfall) == pytest.approx(-9.5)
assert ei.snitt_av_de_daarligste([3.0, 1.0, 2.0]) == 1.0
def test_storste_innskutte_kapital() -> None:
"""Paid in 100 and 50, then 30 back, then 40 more: 160 out at the most."""
KS = np.array([-100.0, -50.0, 30.0, -40.0, 500.0])
assert ei.storste_innskutte_kapital(KS) == pytest.approx(160.0)
assert ei.storste_innskutte_kapital(np.array([10.0, 20.0])) == 0.0
def _kriterier(navn: str, **endringer: float | None) -> ei.Eierkriterier:
verdier: dict[str, float | None] = {
"oppgjor": 0.0,
"verdi": 100.0,
"andel_av_overskudd": 0.9,
"andel_for_oppgjor": 0.9,
"r_e": 0.05,
"LCOC": 80.0,
"tapssannsynlighet": 0.1,
"daarligste": -50.0,
"kapital_ute": 300.0,
}
verdier.update(endringer)
return ei.Eierkriterier(navn=navn, **verdier) # type: ignore[arg-type]
def test_eierkriterier_fra_utfallene(oppdiktet: LagInndata) -> None:
"""Two draws with the same made-up path: the averages are those of the one outcome."""
inn = oppdiktet(T_slutt=2040)
strom = _lonnsom(inn)
ut = ei.eierresultat(inn, strom, inn.eierformer["heleid_selskap"])
k = ei.eierkriterier("heleid_selskap", [ut, ut], [80.0, 90.0])
assert k.oppgjor == 0.0
assert k.verdi == pytest.approx(ut.NV_stat)
assert k.andel_av_overskudd == pytest.approx(1.0)
assert k.r_e == pytest.approx(ut.r_e)
assert k.LCOC == pytest.approx(85.0)
assert k.tapssannsynlighet == 0.0
assert k.daarligste == pytest.approx(ut.NV_stat)
assert k.kapital_ute == pytest.approx(ut.storste_innskutte_kapital)
def test_regelen_for_eierformer() -> None:
"""One form fails the EEA test, one is worse on everything, one breaks the risk limit."""
kriterier = [
_kriterier("a", verdi=120.0),
_kriterier("b", verdi=150.0, tapssannsynlighet=0.3),
_kriterier(
"c",
verdi=90.0,
andel_av_overskudd=0.8,
r_e=0.06,
LCOC=85.0,
tapssannsynlighet=0.2,
daarligste=-60.0,
kapital_ute=310.0,
),
_kriterier("d", verdi=200.0),
_kriterier("e", verdi=110.0, r_e=0.06, LCOC=85.0),
]
eos = {"a": "består", "b": "usikker", "c": "består", "d": "består ikke", "e": "består"}
rangering = ei.ranger_eierformer(kriterier, eos)
assert rangering.rangert == ("a", "e")
assert rangering.over_risikogrensen == ("b",)
assert set(rangering.ute) == {"c", "d"}
assert rangering.ute["d"] == "består ikke EØS-vurderingen"
assert "dårligere enn a" in rangering.ute["c"]
# e is worse than a on value and capital cost, but not on risk, so it stays
strengere = ei.ranger_eierformer(kriterier, eos, risikogrense=0.05)
assert strengere.rangert == ()
assert strengere.over_risikogrensen == ("b", "a", "e")
# a form that fails the EEA assessment cannot take another form out: without a, only d is
# better than c on everything, and d is out itself
uten_a = ei.ranger_eierformer(kriterier[1:], eos)
assert set(uten_a.ute) == {"d"}
def test_private_eiere_betaler_det_andelen_er_verdt(oppdiktet: LagInndata) -> None:
"""Clarification 64: the settlement is the average present value of the private owners' share.
Two draws, one with the scarcity markup and one with overcapacity. The private owners
gain in the first and lose in the second; the settlement is the average, paid at T_0,
the same amount in both draws. It moves the state's present value and every cumulative
sum of its cash flow by that amount. After it the state's expected value is that of the
whole surplus, as with a wholly owned company, and the public's share is 1; what the
form changes is how the outcome is spread over the draws.
"""
inn = oppdiktet(T_slutt=2040)
strommer = [
ei.bedriftsstrom(inn, _regn(inn, Losning.R, knapphet=k))
for k in (Knapphet.VARER, Knapphet.OVERKAPASITET)
]
e = inn.eierformer["majoritet_boers"]
utfall = [ei.eierresultat(inn, strom, e) for strom in strommer]
assert utfall[0].NV_privat > 0 > utfall[1].NV_privat
k = ei.eierkriterier("majoritet_boers", utfall, [80.0, 90.0])
oppgjor = (utfall[0].NV_privat + utfall[1].NV_privat) / 2.0
assert oppgjor > 0
assert k.oppgjor == pytest.approx(oppgjor)
samlet = (utfall[0].NV_samlet + utfall[1].NV_samlet) / 2.0
assert k.verdi == pytest.approx(samlet)
assert k.andel_av_overskudd == pytest.approx(1.0)
assert k.andel_for_oppgjor == pytest.approx(1.0 - oppgjor / samlet)
assert k.tapssannsynlighet == ei.sannsynlighet_for_tap([u.NV_stat + oppgjor for u in utfall])
assert k.kapital_ute == pytest.approx(
math.fsum(max(0.0, -(u.laveste_sum + oppgjor)) for u in utfall) / 2.0
)
heleid = [ei.eierresultat(inn, s, inn.eierformer["heleid_selskap"]) for s in strommer]
h = ei.eierkriterier("heleid_selskap", heleid, [80.0, 90.0])
assert h.oppgjor == 0.0
assert h.verdi == pytest.approx(k.verdi)
# the private owners carry part of the loss in the bad draw, so the state's worst is better
assert k.daarligste > h.daarligste
# the sensitivity: the settlement priced with the private owners' own required return on
# equity (12 % nominal), on their cash flow with the debt
krav = ei.eierkriterier("majoritet_boers", utfall, [80.0, 90.0], egenkapitalkrav=True)
assert krav.oppgjor == pytest.approx(
math.fsum(float(x) / (1.12 / 1.02) ** j for u in utfall for j, x in enumerate(u.KS_privat))
/ 2.0
)
assert krav.verdi == pytest.approx(k.verdi - k.oppgjor + krav.oppgjor)
# with the state's borrowing rate as a sensitivity the price is still the market's
lav = [ei.eierresultat(inn, s, e, rente=0.02) for s in strommer]
assert [u.NV_privat for u in lav] == pytest.approx([u.NV_privat for u in utfall])
assert lav[0].NV_stat > utfall[0].NV_stat
@pytest.mark.parametrize("losning", list(Losning))
def test_anleggenes_kontantstrom_summerer_seg_til_programmets(
oppdiktet: LagInndata, losning: Losning
) -> None:
"""Clarification 62: the facilities' parts add up to the business cash flow of the programme.
In B the programme's nuclear units are shared among the facilities operated that year,
so only the years with a facility in operation are compared.
"""
inn = oppdiktet(u_eksport=None)
res = _regn(inn, losning, N_anlegg=2)
for tilleggsregel in (False, True):
strom = ei.bedriftsstrom(inn, res, tilleggsregel=tilleggsregel)
sum_anlegg = np.sum(ei.netto_per_anlegg(inn, res, tilleggsregel=tilleggsregel), axis=0)
programmet = strom.inntekt - strom.drift - strom.investering
med = np.array(
[losning is not Losning.B or res.plan.antall_i_drift(t) > 0 for t in res.aar]
)
assert med.sum() > 20
assert sum_anlegg[med] == pytest.approx(programmet[med], abs=1.0)
def test_restverdien_trekker_fra_avvikling_etter_sluttaaret(oppdiktet: LagInndata) -> None:
"""Clarification 62 in A: the value of the years left, minus the decommissioning after them.
The decommissioning is discounted with the rate of the plant in A over the years left.
The value is the same whatever the ownership form, and the booked capital ends at zero.
"""
inn = oppdiktet(T_slutt=2045)
res = _regn(inn, Losning.A, knapphet=Knapphet.VARER)
anlegg = res.anlegg[0]
assert anlegg.L_rest > 0
strom = ei.bedriftsstrom(inn, res)
netto = ei.netto_per_anlegg(inn, res)[0]
avvikling = math.fsum(anlegg.avvikling_etter.values())
assert avvikling > 0
verdi = max(0.0, float(np.mean(netto[-5:])) / _crf(0.08, anlegg.L_rest))
assert verdi > 0
ventet = verdi - avvikling * (1.0 + res.dim.rho_A) ** -anlegg.L_rest
assert strom.restverdi[-1] == pytest.approx(ventet)
assert lg.s28_restverdi(
NK_snitt=-1.0, L_rest=10, rho_D=0.08, rho_avvikling=0.06, avvikling=100.0
) == pytest.approx(-100.0 / 1.06**10)
assert (
lg.s28_restverdi(NK_snitt=5.0, L_rest=0, rho_D=0.08, rho_avvikling=0.06, avvikling=9.0)
== 0.0
)
assert strom.kapital[-1] == pytest.approx(0.0, abs=1.0)
def test_kravet_for_skatt(oppdiktet: LagInndata) -> None:
"""Clarification 66: r_e / (1 - tau_s) for a form that pays corporate tax."""
inn = oppdiktet()
assert lg.s29_krav_for_skatt(r_e=0.06, tau_s=0.25) == pytest.approx(0.08)
assert ei.krav_for_skatt(inn, inn.eierformer["direkte"]) == 0.02
assert ei.krav_for_skatt(inn, inn.eierformer["heleid_selskap"]) == pytest.approx(
(1.07 / 1.02 - 1.0) / 0.8
)
def test_private_eieres_kontantstrom() -> None:
"""Clarification 64: 40 % of 75 % of the free cash flow, and the overruns they are spared."""
assert lg.s30_private_eiere(
FKE=100.0, overskridelse=20.0, s=0.6, theta=0.25, zeta=1.0
) == pytest.approx(0.4 * 0.75 * 100.0 + 0.4 * 0.75 * 20.0)
assert lg.s30_private_eiere(FKE=100.0, overskridelse=20.0, s=1.0, theta=0.0, zeta=1.0) == 0.0
def test_like_verdier_staar_likt_i_rangeringen() -> None:
"""Clarification 67: a difference below the tolerance is no difference."""
kriterier = [
_kriterier("a", verdi=100.0),
_kriterier(
"b",
verdi=100.0 * (1.0 - 1e-12),
andel_av_overskudd=0.9 * (1.0 - 1e-12),
r_e=0.06,
LCOC=85.0,
tapssannsynlighet=0.2,
daarligste=-60.0,
kapital_ute=310.0,
),
_kriterier("c", verdi=90.0),
]
eos = dict.fromkeys("abc", "består")
rangering = ei.ranger_eierformer(kriterier, eos)
# b is not strictly worse than a on value, so it is not taken out
assert rangering.ute == {}
assert rangering.rangert == ("a", "b", "c")
assert rangering.like == (("a", "b"),)
# within a group of equal values the order given decides, not the rounding residue
omvendt = ei.ranger_eierformer([kriterier[1], kriterier[0], kriterier[2]], eos)
assert omvendt.rangert == ("b", "a", "c")
assert ei.klart_lavere(1.0, 1.1)
assert not ei.klart_lavere(1.0, 1.0 + 1e-12)
# amounts in kroner near zero: rounding residue is no difference
assert ei.klart_lavere(-2e-4, 1e-4)
assert not ei.klart_lavere(-2e-4, 1e-4, kroner=True)
assert ei.klart_lavere(0.0, 2.0, kroner=True)