Coverage for tests/critic/test_inferers.py: 100%

51 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-22 15:51 +0000

1"""Tests the inferers module: the effects of panels on judges' votes.""" 

2 

3from __future__ import annotations 

4 

5import amos 

6import numpy as np 

7import pandas as pd 

8import pytest 

9 

10import courtpy 

11 

12 

13def make_votes(effect: float, cases: int = 1500, seed: int = 0) -> pd.DataFrame: 

14 """Returns votes in which colleagues' party moves each vote by `effect`.""" 

15 generator = np.random.default_rng(seed) 

16 court = generator.choice([1, 5, 9], cases) 

17 year = generator.integers(2005, 2010, cases) 

18 parties = generator.choice([-1, 1], (cases, 3)) 

19 rows = [] 

20 for case in range(cases): 

21 for seat in range(3): 

22 own = parties[case, seat] 

23 others = np.delete(parties[case], seat).mean() 

24 chance = 0.25 + effect * others + 0.05 * own + 0.05 * (court[case] == 9) 

25 rows.append({ 

26 'case_id': f'c{case}', 'court_num': court[case], 'year': year[case], 

27 'judge_party': own, 'colleagues_party': others, 

28 'vote_reversal': generator.random() < chance}) 

29 return pd.DataFrame(rows) 

30 

31 

32def test_panel_effects_is_an_inferer() -> None: 

33 assert amos.library.classify('panel_effects') == 'inferer' 

34 

35 

36def test_panel_effects_finds_an_effect() -> None: 

37 item = amos.Dataset(make_votes(0.08)) 

38 courtpy.inferers.PanelEffects().apply(item, n_permutations = 200) 

39 table = item.tables['panel_effects'] 

40 assert list(table.index) == ['colleagues_party', 'colleagues_party x judge_party', 'judge_party'] 

41 assert list(table.columns) == [ 

42 'coefficient', 'std_error', 't_statistic', 'p_value', 'ri_p_value', 'ci_lower', 'ci_upper'] 

43 effect = table.loc['colleagues_party'] 

44 assert effect['ci_lower'] < 0.08 < effect['ci_upper'] 

45 assert effect['p_value'] < 0.01 and effect['ri_p_value'] < 0.01 

46 assert pd.isna(table.loc['judge_party', 'ri_p_value']) 

47 assert item.history[-1]['votes'] == 4500 and item.history[-1]['clusters'] == 1500 

48 

49 

50def test_panel_effects_finds_none_without_one() -> None: 

51 item = amos.Dataset(make_votes(0.0, seed = 2)) 

52 courtpy.inferers.PanelEffects().apply(item, n_permutations = 200, interact = False) 

53 table = item.tables['panel_effects'] 

54 assert list(table.index) == ['colleagues_party', 'judge_party'] 

55 assert table.loc['colleagues_party', 'ri_p_value'] > 0.05 

56 

57 

58def test_panel_effects_matches_a_regression_with_dummies() -> None: 

59 votes = make_votes(0.05, cases = 300, seed = 4) 

60 item = amos.Dataset(votes) 

61 courtpy.inferers.PanelEffects().apply(item, n_permutations = 0, interact = False) 

62 table = item.tables['panel_effects'] 

63 stratum = votes['court_num'].astype(str) + votes['year'].astype(str) 

64 x = np.column_stack([ 

65 votes['colleagues_party'], votes['judge_party'], 

66 pd.get_dummies(stratum).to_numpy(dtype = float)]) 

67 found, *_ = np.linalg.lstsq(x, votes['vote_reversal'].to_numpy(dtype = float), rcond = None) 

68 assert table['coefficient'].to_numpy() == pytest.approx(found[:2]) 

69 assert table['ri_p_value'].isna().all() 

70 

71 

72def test_panel_effects_needs_its_columns() -> None: 

73 with pytest.raises(KeyError, match = 'colleagues_party'): 

74 courtpy.inferers.PanelEffects().apply(amos.Dataset(pd.DataFrame({'vote_reversal': [True]})))