SPB Git forge

spb/wp21_uqo

Public
0commits 0branches 0releases
0 Bsize
maindefault branch
—last push
2.0 KB · 33 lines python
Raw Blame History
1import math, sys2from pathlib import Path3import numpy as np4sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts" / "harmonization"))5from harmonize import pct_premium, semi_elasticity, partial_r, recover_se, harmonise_row, norm_unit678def test_pct_premium_kennedy():9    est, se, f = pct_premium(0.1, 0.02)10    assert abs(est - 100 * (math.exp(0.1 - 0.0002) - 1)) < 1e-9 and abs(se - 100 * math.exp(0.1) * 0.02) < 1e-911    est0, _, _ = pct_premium(0.1, 0.02, kennedy=False); assert abs(est0 - 100 * (math.exp(0.1) - 1)) < 1e-9121314def test_partial_r():15    r, se, _ = partial_r(2.0, 100); assert abs(r - 2 / math.sqrt(104)) < 1e-12 and se > 016    assert np.isnan(partial_r(np.nan, 100)[0])171819def test_recover_se():20    assert recover_se(0.5, None, 2.5, None, None, None)[0] == 0.221    assert abs(recover_se(0.5, None, None, None, 0.1, 0.9)[0] - 0.8 / (2 * 1.959964)) < 1e-922    assert np.isnan(recover_se(0.5, None, None, None, None, None)[0])232425def test_units_and_flags():26    assert norm_unit("Square Feet") == "sqft" and norm_unit("km") == "km"27    r = harmonise_row({"beta": 0.001, "se": 0.0002, "t": None, "p": None, "ci_lo": None, "ci_hi": None, "dep_transform": "log", "x_transform": "level", "x_unit": "sqft", "taxonomy_id": "living_area", "n_obs": 1000, "var_name_printed": "sqft"})28    assert r["family"] == "semi_elasticity" and abs(r["es"] - 100 * 0.001 / 0.09290304) < 1e-929    r2 = harmonise_row({"beta": -0.002, "se": 0.001, "t": None, "p": None, "ci_lo": None, "ci_hi": None, "dep_transform": "log", "x_transform": "level", "x_unit": "years", "taxonomy_id": "age", "n_obs": 500, "var_name_printed": "Year built"})30    assert "year_built_not_age" in r2["harm_flags"]31    r3 = harmonise_row({"beta": 0.3, "se": 0.05, "t": None, "p": None, "ci_lo": None, "ci_hi": None, "dep_transform": "log", "x_transform": "binary", "x_unit": "", "taxonomy_id": "pool", "n_obs": 500, "var_name_printed": "pool"})32    assert r3["family"] == "pct_premium" and r3["harm_flags"] == ""33