"""Chapitre 16 — vérification des chiffres cités dans le texte et les solutions.""" import sys, pathlib, math import numpy as np, pytest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1] / "cases")) import ch16_selection as c16 from traite import comparables as cp @pytest.fixture(scope="module") def res(): return c16.main(figures=False) def test_entonnoir_et_comparables(res): e = res["selection"]["etapes"] assert e == {"candidats_total": 600, "apres_delai": 236, "apres_rayon": 40, "apres_type": 27, "apres_superficie": 16} top = res["selection"]["comparables"] assert list(top["id_vente"]) == ["G0574", "G0404", "G0468", "G0477", "G0575"] assert (top["secteur"] == "Hull").all() and (top["mois"] <= 18).all() and (top["dist_km"] <= 3).all() assert list(top["prix_vente"]) == [598000, 506500, 624500, 538000, 513500] def test_indication_brute(res): top = res["selection"]["comparables"]; s = res["sujet"] ind = top["prix_m2"].median() * s["superficie_habitable_m2"] assert ind == pytest.approx(551029, abs=1) assert top["prix_vente"].min() == 506500 and top["prix_vente"].max() == 624500 assert top["prix_vente"].mean() == pytest.approx(556100) and top["prix_vente"].median() == 538000 cand = res["selection"]["candidats"] assert cand["prix_m2"].median() == pytest.approx(3286, abs=1) and cand["prix_m2"].min() == pytest.approx(2361, abs=1) and cand["prix_m2"].max() == pytest.approx(4140, abs=1) def test_sensibilite(res): s = res["sensibilite"] assert len(s) == 48 assert s.indication_m2.min() == pytest.approx(525768, abs=1) and s.indication_m2.max() == pytest.approx(608014, abs=1) assert s.indication_m2.std() == pytest.approx(24069, abs=2) g = s.groupby("k").indication_m2.agg(["min", "median", "max"]) assert g.loc[4, "median"] == pytest.approx(579522, abs=1) and g.loc[10, "max"] == pytest.approx(596921, abs=1) def test_solutions_exercices(): # ex. 16.1 pu = [525000 / 152, 489000 / 141, 561000 / 171] assert sorted(pu)[1] * 165 == pytest.approx(569902, abs=1) # ex. 16.3 e = {"s": 25, "t": 150, "a": 10, "q": 1}; w = {"s": 2, "t": 0.7, "a": 1, "q": 1} suj = {"s": 165, "t": 620, "a": 28, "q": 3} dA = cp.distance_attributs(suj, {"s": 158, "t": 600, "a": 25, "q": 3}, e, w) dB = cp.distance_attributs(suj, {"s": 190, "t": 650, "a": 10, "q": 4}, e, w) dC = cp.distance_attributs(suj, {"s": 150, "t": 400, "a": 30, "q": 2}, e, w) assert dA == pytest.approx(0.509, abs=0.001) and dB == pytest.approx(2.504, abs=0.001) and dC == pytest.approx(1.807, abs=0.001) # ex. 16.4 assert 506500 * (1.0443 ** (15 / 12) - 1) == pytest.approx(28200, rel=0.01) # ex. 16.5 inv = [1 / 20**2, 1 / 30**2, 1 / 40**2]; S = sum(inv) assert [x / S for x in inv] == pytest.approx([0.590, 0.262, 0.148], abs=0.001) assert math.sqrt(1 / S) == pytest.approx(15.4, abs=0.05) and math.sqrt((400 + 900 + 1600) / 9) == pytest.approx(17.95, abs=0.01) # ex. 16.7 k = (25000**2 / (2 * 2000**2)) ** (1 / 3) assert k == pytest.approx(4.27, abs=0.01) assert (25000**2 / (2 * 500**2)) ** (1 / 3) == pytest.approx(10.8, abs=0.05) eqm = 2000**2 * 16 + 25000**2 / 4 assert math.sqrt(eqm) == pytest.approx(14800, rel=0.01)