practice / tests /test_life_table.py
birgermoell's picture
Add Practice generators, indices, verification and documentation
2983a9c verified
Raw History Blame Contribute Delete
9.88 kB
"""Hand-worked cases for Life Table. Run: python3 tests/test_life_table.py"""
import os, sys
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from envs.life_table import gen
def near(a, b, tol): return abs(float(a) - float(b)) <= tol
# ------------------------------------------------------------ life table
def test_lt_four_groups():
# age 0: P 1000, D 20, m 0.02, a 0.1: q = 0.02 / (1 + 0.9 * 0.02) = 0.02 / 1.018 = 0.0196464
# 1-4: P 4000, D 4, m 0.001, a 1.5: q = 0.004 / (1 + 2.5 * 0.001) = 0.004 / 1.0025 = 0.0039900
# 5-64: n 60, m 0.005, a 30: q = 0.3 / (1 + 30 * 0.005) = 0.3 / 1.15 = 0.2608696
# 65+: m 0.08: q = 1
# l: 100000 -> 98035.36 -> 97644.20 -> 72171.80
# d: 1964.64, 391.16, 25472.40
# L0 = 98035.36 + 0.1 * 1964.64 = 98231.83
# L1 = 4 * 97644.20 + 1.5 * 391.16 = 391163.5
# L5 = 60 * 72171.80 + 30 * 25472.40 = 5094480
# L65 = 72171.80 / 0.08 = 902147.5
# T0 = 6486022.8 -> e0 = 64.860; T65 / l65 = 1 / 0.08 = 12.5
t = gen.life_table([(0, 1, 20 / 1000.0), (1, 4, 4 / 4000.0), (5, 60, 0.005), (65, None, 0.08)], 0.1, 1.5)
assert near(t[0]["q"], 0.0196464, 1e-7) and near(t[1]["q"], 0.0039900, 1e-7) and near(t[2]["q"], 0.2608696, 1e-7) and t[3]["q"] == 1.0
assert near(t[1]["l"], 98035.36, 0.01) and near(t[2]["l"], 97644.20, 0.01) and near(t[3]["l"], 72171.80, 0.01)
assert near(t[0]["L"], 98231.83, 0.01) and near(t[1]["L"], 391163.5, 0.1) and near(t[2]["L"], 5094480, 2) and near(t[3]["L"], 902147.5, 0.2)
assert near(t[0]["T"], 6486022.8, 3) and near(t[0]["e"], 64.860, 0.001) and near(t[3]["e"], 12.5, 1e-9)
def test_lt_two_groups():
# 0-4: n 5, m 0.01, a 2.5 (not the single-year age 0, so a = n / 2): q = 0.05 / 1.025 = 0.0487805
# l5 = 95121.95, d = 4878.05, L0 = 5 * 95121.95 + 2.5 * 4878.05 = 487804.88
# 5+: m 0.02: L = 95121.95 / 0.02 = 4756097.56. T0 = 5243902.44, e0 = 52.439, e5 = 50
t = gen.life_table([(0, 5, 0.01), (5, None, 0.02)], 0.1, 1.5)
assert t[0]["a"] == 2.5 and near(t[0]["q"], 0.0487805, 1e-7)
assert near(t[1]["l"], 95121.95, 0.01) and near(t[0]["L"], 487804.88, 0.01) and near(t[0]["e"], 52.439, 0.001) and near(t[1]["e"], 50, 1e-9)
# one open interval only: constant death rate 0.0125 gives e0 = 1 / 0.0125 = 80
assert near(gen.life_table([(0, None, 0.0125)], 0.1, 1.5)[0]["e"], 80, 1e-9)
def test_lt_published_first_rows():
# Preston, Heuveline and Guillot (2001), Box 3.1, Austrian males 1992 (first two rows, quoted from the book):
# age 0: m = 0.008743, a = 0.068 -> q = 0.008743 / (1 + 0.932 * 0.008743) = 0.008743 / 1.0081485 = 0.008672
# 1-4: m = 0.000370, a = 1.626 -> q = 0.00148 / (1 + 2.374 * 0.00037) = 0.00148 / 1.0008784 = 0.001479
# l1 = 100000 * (1 - 0.008672) = 99133; L0 = 99133 + 0.068 * 867 = 99192
t = gen.life_table([(0, 1, 0.008743), (1, 4, 0.000370), (5, None, 0.02)], 0.068, 1.626)
assert near(t[0]["q"], 0.008672, 5e-7) and near(t[1]["q"], 0.001479, 5e-7)
assert near(t[1]["l"], 99133, 0.5) and near(t[0]["L"], 99192, 0.5)
# a different a for age 0 changes q: a 0.3 -> 0.02 / (1 + 0.7 * 0.02) = 0.0197239
assert near(gen.life_table([(0, 1, 0.02), (1, None, 0.01)], 0.3, 1.6)[0]["q"], 0.0197239, 1e-7)
# ------------------------------------------------------------ fertility
def test_fertility_share():
# 1000 women in every group, births 20 80 120 100 50 20 10: ASFR 0.020 ... sum 0.400, TFR = 5 * 0.4 = 2.0
# GRR = 2.0 * 0.488 = 0.976
# mean age = (17.5*20 + 22.5*80 + 27.5*120 + 32.5*100 + 37.5*50 + 42.5*20 + 47.5*10) / 400
# = (350 + 1800 + 3300 + 3250 + 1875 + 850 + 475) / 400 = 11900 / 400 = 29.75
res = gen.fertility([1000] * 7, [20, 80, 120, 100, 50, 20, 10], share_female=0.488)
assert near(res["tfr"], 2.0, 1e-9) and near(res["grr"], 0.976, 1e-9) and near(res["mac"], 29.75, 1e-9)
assert near(res["asfr"][2], 0.12, 1e-12) and res["nrr"] is None
def test_fertility_unequal_women():
# women 2000 2500 3000 2500 2000 1500 1000, births 30 200 360 250 100 30 5
# ASFR 0.015 0.08 0.12 0.10 0.05 0.02 0.005, sum 0.39, TFR 1.95
# mean age = (0.2625 + 1.8 + 3.3 + 3.25 + 1.875 + 0.85 + 0.2375) / 0.39 = 11.575 / 0.39 = 29.6795
# 105 boys per 100 girls -> share of girls 100 / 205 = 0.487805, GRR = 1.95 * 0.487805 = 0.951220
res = gen.fertility([2000, 2500, 3000, 2500, 2000, 1500, 1000], [30, 200, 360, 250, 100, 30, 5], share_female=100.0 / 205.0)
assert near(res["tfr"], 1.95, 1e-9) and near(res["mac"], 29.6795, 1e-4) and near(res["grr"], 0.951220, 1e-6)
def test_fertility_grr_nrr():
# 1000 women per group, girls 10 40 58 49 24 10 5 (sum 196): GRR = 5 * 0.196 = 0.98
# L = 490000 488000 486000 484000 481000 477000 471000
# NRR = (0.010*490000 + 0.040*488000 + 0.058*486000 + 0.049*484000 + 0.024*481000 + 0.010*477000 + 0.005*471000) / 100000
# = (4900 + 19520 + 28188 + 23716 + 11544 + 4770 + 2355) / 100000 = 94993 / 100000 = 0.94993
res = gen.fertility([1000] * 7, [20, 80, 120, 100, 50, 20, 10], female_births=[10, 40, 58, 49, 24, 10, 5],
big_l=[490000, 488000, 486000, 484000, 481000, 477000, 471000])
assert near(res["grr"], 0.98, 1e-9) and near(res["nrr"], 0.94993, 1e-9) and near(res["tfr"], 2.0, 1e-9)
# with no mortality before 50 (L = 500000 in every group) NRR equals GRR
assert near(gen.fertility([1000] * 7, [20, 80, 120, 100, 50, 20, 10], female_births=[10, 40, 58, 49, 24, 10, 5], big_l=[500000] * 7)["nrr"], 0.98, 1e-9)
# ------------------------------------------------------------ standardisation
def test_standardisation_two_regions():
# A: young 8000 people, 8 deaths (0.001); old 2000, 40 deaths (0.02). crude = 48 / 10000 -> 480 per 100000
# B: young 2000, 3 deaths (0.0015); old 8000, 200 deaths (0.025). crude = 203 / 10000 -> 2030
# standard 6000 young, 4000 old:
# A: (6000*0.001 + 4000*0.02) / 10000 = 86 / 10000 -> 860; B: (9 + 100) / 10000 -> 1090; ratio 1090 / 860 = 1.267442
assert near(gen.crude_rate([8000, 2000], [8, 40]), 480, 1e-9) and near(gen.crude_rate([2000, 8000], [3, 200]), 2030, 1e-9)
a = gen.standardised_rate([8000, 2000], [8, 40], [6000, 4000]); b = gen.standardised_rate([2000, 8000], [3, 200], [6000, 4000])
assert near(a, 860, 1e-9) and near(b, 1090, 1e-9) and near(b / a, 1.267442, 1e-6)
def test_standardisation_three_bands():
# population 5000 3000 2000, deaths 5 15 60: rates 0.001 0.005 0.03. crude = 80 / 10000 -> 800
# standard 40000 35000 25000: (40 + 175 + 750) / 100000 -> 965 per 100000
assert near(gen.crude_rate([5000, 3000, 2000], [5, 15, 60]), 800, 1e-9)
assert near(gen.standardised_rate([5000, 3000, 2000], [5, 15, 60], [40000, 35000, 25000]), 965, 1e-9)
def test_standardisation_same_rates():
# equal age-specific rates but different age structures: crude rates differ, standardised rates are equal
# young-heavy: (9000*0.001 + 1000*0.02) = 29 deaths -> crude 290; old-heavy: (4 + 120) = 124 deaths -> crude 1240
# standard 1 : 1 -> (0.001 + 0.02) / 2 = 0.0105 -> 1050 for both
assert near(gen.crude_rate([9000, 1000], [9, 20]), 290, 1e-9) and near(gen.crude_rate([4000, 6000], [4, 120]), 1240, 1e-9)
assert near(gen.standardised_rate([9000, 1000], [9, 20], [500, 500]), 1050, 1e-9)
assert near(gen.standardised_rate([4000, 6000], [4, 120], [500, 500]), 1050, 1e-9)
# a standard equal to the region's own population gives back the crude rate
assert near(gen.standardised_rate([4000, 6000], [4, 120], [4000, 6000]), 1240, 1e-9)
# ------------------------------------------------------------ age structure
def test_age_broad_groups():
# groups 0-14 200, 15-29 280, 30-44 270, 45-64 150, 65-79 80, 80+ 20: total 1000
# young 200, working 700, old 100: ratios 28.5714, 14.2857, total 42.8571, ageing index 50
# median: N / 2 = 500; cumulative 200, 480, 750 -> group 30-44: 30 + (500 - 480) / 270 * 15 = 31.1111
v = gen.age_structure([200, 280, 270, 150, 80, 20], [0, 15, 30, 45, 65, 80])
assert v["total"] == 1000 and near(v["young"], 28.5714, 1e-4) and near(v["old"], 14.2857, 1e-4) and near(v["dep"], 42.8571, 1e-4)
assert near(v["ageing"], 50, 1e-9) and near(v["median"], 31.1111, 1e-4)
assert near(v["s_young"], 20, 1e-9) and near(v["s_work"], 70, 1e-9) and near(v["s_old"], 10, 1e-9)
def test_age_five_year_groups():
# nineteen five-year groups (0-4 ... 85-89, 90+) of 100 people each: total 1900
# young 300 (three groups), old 600 (65-69 ... 90+), working 1000: ratios 30 and 60, total 90, ageing index 200
# median: N / 2 = 950; the nine groups under 45 hold 900 -> 45 + (950 - 900) / 100 * 5 = 47.5
v = gen.age_structure([100] * 19, list(range(0, 95, 5)))
assert near(v["young"], 30, 1e-9) and near(v["old"], 60, 1e-9) and near(v["dep"], 90, 1e-9) and near(v["ageing"], 200, 1e-9) and near(v["median"], 47.5, 1e-9)
def test_grouped_median():
# 10, 30, 60, 0 people in groups starting at 0, 5, 10, 15: N / 2 = 50; cumulative 10, 40 -> 10 + (50 - 40) / 60 * 5 = 10.8333
assert near(gen.grouped_median([10, 30, 60, 0], [0, 5, 10, 15]), 10.8333, 1e-4)
# 40, 40, 20: N / 2 = 50 -> second group: 5 + (50 - 40) / 40 * 5 = 6.25
assert near(gen.grouped_median([40, 40, 20], [0, 5, 10]), 6.25, 1e-9)
def test_generated_tasks():
for fam in sorted(gen.FAMILIES):
for d in (1, 2, 3):
for seed in range(8):
t = gen.generate(seed, d, fam)
assert 6 <= len(t.checks) <= 40, (t.id, len(t.checks))
assert "methods.md" in t.files and "—" not in t.instruction + t.files["methods.md"]
if __name__ == "__main__":
tests = [v for k, v in sorted(globals().items()) if k.startswith("test_")]
for t in tests: t()
print("test_life_table: %d tests passed" % len(tests))