spb/traite-evaluation-qc
Public
1import numpy as np, pytest2from traite import hedonique as h34def test_mco_recupere_parametres():5 rng = np.random.default_rng(1)6 n = 2000; x = rng.normal(size=(n, 2)); X = np.column_stack([np.ones(n), x])7 y = X @ np.array([12.0, 0.5, -0.2]) + rng.normal(0, 0.1, n)8 r = h.mco(X, y)9 assert np.allclose(r["beta"], [12.0, 0.5, -0.2], atol=0.02) and r["r2"] > 0.91011def test_indice_dummies():12 idx = h.indice_dummies_temps(np.array([0.05, 0.10]))13 assert idx[0] == 100 and idx[1] == pytest.approx(100 * np.exp(0.05))1415def test_ventes_repetees_recupere_indice():16 rng = np.random.default_rng(7)17 vrai = np.array([0, 0.04, 0.09, 0.13, 0.20]) # log-indice18 n = 300019 t1 = rng.integers(0, 4, n); t2 = np.array([rng.integers(a + 1, 5) for a in t1])20 p1 = np.exp(12 + rng.normal(0, 0.3, n) + vrai[t1]); p2 = p1 * np.exp(vrai[t2] - vrai[t1] + rng.normal(0, 0.05, n))21 r = h.ventes_repetees_bmn(p1, p2, t1, t2, 5)22 assert np.allclose(np.log(r["indice"] / 100), vrai, atol=0.01)23 cs = h.ventes_repetees_case_shiller(p1, p2, t1, t2, 5)24 assert np.allclose(np.log(cs["indice"] / 100), vrai, atol=0.01)2526def test_statistiques_ratio_parfaites():27 P = np.array([300e3, 400e3, 500e3, 600e3]); V = P * 0.9528 s = h.statistiques_ratio(V, P)29 assert s["ratio_median"] == pytest.approx(0.95) and s["COD"] == pytest.approx(0.0) and s["PRD"] == pytest.approx(1.0) and abs(s["PRB"]) < 1e-930 # régressivité : ratios plus élevés pour les biens bon marché → PRD > 1, PRB < 031 V2 = P * np.array([1.05, 1.0, 0.95, 0.90])32 s2 = h.statistiques_ratio(V2, P)33 assert s2["PRD"] > 1 and s2["PRB"] < 0 and s2["COD"] > 03435def test_moran_detecte_autocorrelation():36 rng = np.random.default_rng(3)37 coords = rng.uniform(0, 10, (300, 2))38 W = h.matrice_poids_knn(coords, 6)39 assert np.allclose(W.sum(1), 1)40 x_alea = rng.normal(size=300)41 x_spat = coords[:, 0] + rng.normal(0, 0.5, 300)42 assert abs(h.moran_i(x_alea, W)["z"]) < 343 assert h.moran_i(x_spat, W)["z"] > 54445def test_sar_recupere_rho():46 rng = np.random.default_rng(11)47 n = 400; coords = rng.uniform(0, 10, (n, 2)); W = h.matrice_poids_knn(coords, 5)48 X = np.column_stack([np.ones(n), rng.normal(size=n)]); beta = np.array([1.0, 0.8]); rho = 0.549 e = rng.normal(0, 0.3, n)50 y = np.linalg.solve(np.eye(n) - rho * W, X @ beta + e)51 r = h.sar_lag_mv(y, X, W)52 assert abs(r["rho"] - rho) < 0.1 and np.allclose(r["beta"], beta, atol=0.15)53