File size: 5,329 Bytes
0caf2ba
 
 
 
 
 
 
 
 
 
 
 
 
 
c047289
0caf2ba
 
 
 
 
 
 
c047289
0caf2ba
 
 
 
 
 
 
 
 
 
 
 
c047289
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d1932d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3cffb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
from pathlib import Path

from dota2tuned.recommend import DraftRecommender
from dota2tuned.schemas import DraftInput, Recommendation
from dota2tuned.storage import write_parquet


def test_recommendation_schema_and_exclusions(tmp_path: Path):
    write_parquet(
        tmp_path / "dim_hero.parquet",
        [
            {
                "hero_id": 1,
                "hero_name": "Anti-Mage",
                "roles": "Carry,Escape,Nuker",
                "pro_pick": 1000,
                "pro_win": 520,
                "pro_win_rate": 0.52,
            },
            {
                "hero_id": 2,
                "hero_name": "Axe",
                "roles": "Initiator,Durable,Disabler",
                "pro_pick": 200,
                "pro_win": 90,
                "pro_win_rate": 0.45,
            },
        ],
    )
    write_parquet(tmp_path / "fact_hero_pair_stats.parquet", [])
    recommender = DraftRecommender(tmp_path)
    recs = recommender.recommend(DraftInput(banned_heroes=[1]), limit=5)
    assert len(recs) == 1
    assert isinstance(recs[0], Recommendation)
    assert recs[0].hero_id == 2


def test_mid_recommendations_filter_support_first_heroes(tmp_path: Path):
    write_parquet(
        tmp_path / "dim_hero.parquet",
        [
            {
                "hero_id": 13,
                "hero_name": "Puck",
                "roles": "Initiator,Disabler,Escape,Nuker",
                "pro_pick": 12,
                "pro_win": 4,
                "pro_win_rate": 0.3333,
            },
            {
                "hero_id": 91,
                "hero_name": "Io",
                "roles": "Support,Escape,Nuker",
                "pro_pick": 4,
                "pro_win": 4,
                "pro_win_rate": 1.0,
            },
        ],
    )
    write_parquet(tmp_path / "fact_hero_pair_stats.parquet", [])

    recs = DraftRecommender(tmp_path).recommend(DraftInput(role="mid"), limit=5)

    assert [rec.hero_name for rec in recs] == ["Puck"]


def test_low_sample_win_rates_are_shrunk(tmp_path: Path):
    write_parquet(
        tmp_path / "dim_hero.parquet",
        [
            {
                "hero_id": 1,
                "hero_name": "Tiny Sample",
                "roles": "Carry",
                "pro_pick": 1,
                "pro_win": 1,
                "pro_win_rate": 1.0,
            },
            {
                "hero_id": 2,
                "hero_name": "Large Sample",
                "roles": "Carry",
                "pro_pick": 200,
                "pro_win": 120,
                "pro_win_rate": 0.6,
            },
        ],
    )
    write_parquet(tmp_path / "fact_hero_pair_stats.parquet", [])

    recs = DraftRecommender(tmp_path).recommend(DraftInput(role="carry"), limit=2)

    assert recs[0].hero_name == "Large Sample"


def test_recommendations_use_player_match_samples_for_confidence(tmp_path: Path):
    write_parquet(
        tmp_path / "dim_hero.parquet",
        [
            {
                "hero_id": 1,
                "hero_name": "Bigger Sample Hero",
                "roles": "Carry",
                "pro_pick": 3,
                "pro_win": 3,
                "pro_win_rate": 1.0,
            }
        ],
    )
    write_parquet(tmp_path / "fact_hero_pair_stats.parquet", [])
    write_parquet(
        tmp_path / "fact_player_match.parquet",
        [
            {
                "match_id": match_id,
                "hero_id": 1,
                "is_radiant": True,
                "win": 1 if match_id <= 120 else 0,
            }
            for match_id in range(1, 151)
        ],
    )

    recs = DraftRecommender(tmp_path).recommend(DraftInput(role="carry"), limit=1)

    assert recs[0].sample_size == 150
    assert recs[0].confidence == "medium"
    assert "normalized player matches" in recs[0].sources


def test_pair_lift_matches_filter_aggregate_semantics(tmp_path: Path):
    write_parquet(
        tmp_path / "dim_hero.parquet",
        [{"hero_id": 1, "hero_name": "Anti-Mage", "roles": "Carry", "pro_pick": 10, "pro_win": 6}],
    )
    write_parquet(
        tmp_path / "fact_hero_pair_stats.parquet",
        [
            {"hero_id": 1, "other_hero_id": 9, "relation": "enemy", "win_rate": 0.6, "games": 100},
            {"hero_id": 1, "other_hero_id": 8, "relation": "enemy", "win_rate": 0.4, "games": 50},
            {"hero_id": 1, "other_hero_id": 7, "relation": "ally", "win_rate": 0.7, "games": 30},
        ],
    )
    rec = DraftRecommender(tmp_path)

    # mean(0.6, 0.4) = 0.5 -> (0.5 - 0.5) * 0.25 = 0.0 ; games summed across matches
    assert rec._pair_lift(1, [9, 8], "enemy") == (0.0, 150)
    # single match: (0.6 - 0.5) * 0.25 = 0.025
    lift, games = rec._pair_lift(1, [9], "enemy")
    assert games == 100
    assert round(lift, 6) == 0.025
    # ally relation is indexed separately from enemy
    lift, games = rec._pair_lift(1, [7], "ally")
    assert games == 30
    assert round(lift, 6) == 0.05
    # duplicates behave like is_in membership, not double counting
    assert rec._pair_lift(1, [9, 9], "enemy") == rec._pair_lift(1, [9], "enemy")
    # misses, empty input, and wrong relation all yield (0.0, 0)
    assert rec._pair_lift(1, [999], "enemy") == (0.0, 0)
    assert rec._pair_lift(1, [], "enemy") == (0.0, 0)
    assert rec._pair_lift(1, [7], "enemy") == (0.0, 0)