-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.py
More file actions
446 lines (374 loc) · 17.9 KB
/
Copy pathsearch.py
File metadata and controls
446 lines (374 loc) · 17.9 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
import cloudscraper
import urllib.parse
from typing import Optional
class SofaScoreTeamScraper:
def __init__(self):
self.scraper = cloudscraper.create_scraper()
self.base_url = "https://api.sofascore.com/api/v1"
self._team_cache = {} # name -> id cache
def search_team(self, team_name: str, country: str = "Argentina") -> Optional[int]:
"""Search SofaScore for a team, prefer results from the given country."""
cache_key = f"{team_name}_{country}"
if cache_key in self._team_cache:
return self._team_cache[cache_key]
encoded = urllib.parse.quote(team_name)
url = f"{self.base_url}/search/all?q={encoded}&page=0"
try:
resp = self.scraper.get(url, timeout=10)
if resp.status_code == 200:
for res in resp.json().get("results", []):
if res.get("type") == "team":
entity = res["entity"]
team_country = entity.get("country", {}).get("name", "")
if country.lower() in team_country.lower():
self._team_cache[cache_key] = entity["id"]
print(f" [SofaScore] Resolved '{team_name}' -> {entity['name']} (ID: {entity['id']})")
return entity["id"]
# Fallback: return first team result if no country match
for res in resp.json().get("results", []):
if res.get("type") == "team":
entity = res["entity"]
self._team_cache[cache_key] = entity["id"]
print(f" [SofaScore] Resolved '{team_name}' -> {entity['name']} (ID: {entity['id']}, fallback)")
return entity["id"]
except Exception as e:
print(f" [SofaScore Error] Team search failed for '{team_name}': {e}")
return None
def get_team_events(self, team_id: int, page: int = 0) -> list:
"""Fetch recent finished events for a team."""
url = f"{self.base_url}/team/{team_id}/events/last/{page}"
try:
resp = self.scraper.get(url, timeout=10)
if resp.status_code == 200:
return resp.json().get("events", [])
except Exception as e:
print(f" [SofaScore Error] Events fetch failed for team {team_id}: {e}")
return []
# Module-level singleton to reuse across calls (keeps session + cache)
_scraper = SofaScoreTeamScraper()
def get_team_form(team_name: str, last_n: int = 10, country: str = "Argentina") -> str:
"""
Get recent match form for a team via SofaScore scraping.
Returns formatted string of last N matches with scores.
"""
team_id = _scraper.search_team(team_name, country)
if not team_id:
return f"Could not find team '{team_name}' on SofaScore."
events = _scraper.get_team_events(team_id)
if not events:
return f"No recent events found for '{team_name}'."
# Filter to finished matches with scores
finished = []
for e in events:
status = e.get("status", {}).get("code", 0)
hs = e.get("homeScore", {}).get("current")
as_ = e.get("awayScore", {}).get("current")
if status == 100 and hs is not None and as_ is not None:
finished.append(e)
if not finished:
return f"No completed matches with scores found for '{team_name}'."
lines = [f"---[SofaScore Form: {team_name} (last {min(last_n, len(finished))} matches)]---"]
wins, draws, losses = 0, 0, 0
for e in finished[:last_n]:
ht = e["homeTeam"]["name"]
at = e["awayTeam"]["name"]
hs = e["homeScore"]["current"]
as_ = e["awayScore"]["current"]
# Determine result from team's perspective
is_home = (team_id == e["homeTeam"]["id"])
if hs == as_:
result = "D"
draws += 1
elif (is_home and hs > as_) or (not is_home and as_ > hs):
result = "W"
wins += 1
else:
result = "L"
losses += 1
lines.append(f" [{result}] {ht} {hs} - {as_} {at}")
total = wins + draws + losses
lines.append(f" Summary: {wins}W {draws}D {losses}L (Win%: {wins/total*100:.0f}%)")
return "\n".join(lines)
def get_h2h(team_a: str, team_b: str, country: str = "Argentina") -> str:
"""
Derive H2H by fetching both teams' recent events and finding common matches.
"""
id_a = _scraper.search_team(team_a, country)
id_b = _scraper.search_team(team_b, country)
if not id_a:
return f"Could not find team '{team_a}' on SofaScore."
if not id_b:
return f"Could not find team '{team_b}' on SofaScore."
# Fetch events for team A (up to 2 pages = ~60 matches)
events_a = _scraper.get_team_events(id_a, page=0)
events_a += _scraper.get_team_events(id_a, page=1)
# Filter for matches where team B was the opponent
h2h_matches = []
for e in events_a:
home_id = e["homeTeam"]["id"]
away_id = e["awayTeam"]["id"]
if (home_id == id_a and away_id == id_b) or (home_id == id_b and away_id == id_a):
status = e.get("status", {}).get("code", 0)
hs = e.get("homeScore", {}).get("current")
as_ = e.get("awayScore", {}).get("current")
if status == 100 and hs is not None and as_ is not None:
h2h_matches.append(e)
if not h2h_matches:
return f"No H2H matches found between '{team_a}' and '{team_b}' in recent history."
lines = [f"---[SofaScore H2H: {team_a} vs {team_b} ({len(h2h_matches)} matches)]---"]
a_wins, draws, b_wins = 0, 0, 0
for e in h2h_matches:
ht = e["homeTeam"]["name"]
at = e["awayTeam"]["name"]
hs = e["homeScore"]["current"]
as_ = e["awayScore"]["current"]
if hs == as_:
draws += 1
elif (e["homeTeam"]["id"] == id_a and hs > as_) or (e["awayTeam"]["id"] == id_a and as_ > hs):
a_wins += 1
else:
b_wins += 1
lines.append(f" {ht} {hs} - {as_} {at}")
lines.append(f" H2H Summary: {team_a} {a_wins}W | Draws {draws} | {team_b} {b_wins}W")
return "\n".join(lines)
def get_team_form_stats(team_name: str, last_n: int = 10, country: str = "Argentina") -> dict:
"""
Returns STRUCTURED form data (not just a string) for Python-native probability calculation.
"""
team_id = _scraper.search_team(team_name, country)
if not team_id:
return {"error": f"Could not find team '{team_name}'", "matches": 0}
events = _scraper.get_team_events(team_id)
finished = []
for e in events:
status = e.get("status", {}).get("code", 0)
hs = e.get("homeScore", {}).get("current")
as_ = e.get("awayScore", {}).get("current")
if status == 100 and hs is not None and as_ is not None:
finished.append(e)
if not finished:
return {"error": f"No completed matches for '{team_name}'", "matches": 0}
wins, draws, losses = 0, 0, 0
goals_scored, goals_conceded = 0, 0
goals_per_match = [] # Track per-match for std dev
conceded_per_match = []
for e in finished[:last_n]:
hs = e["homeScore"]["current"]
as_ = e["awayScore"]["current"]
is_home = (team_id == e["homeTeam"]["id"])
if is_home:
goals_scored += hs
goals_conceded += as_
goals_per_match.append(hs)
conceded_per_match.append(as_)
else:
goals_scored += as_
goals_conceded += hs
goals_per_match.append(as_)
conceded_per_match.append(hs)
if hs == as_:
draws += 1
elif (is_home and hs > as_) or (not is_home and as_ > hs):
wins += 1
else:
losses += 1
total = wins + draws + losses
avg_scored = goals_scored / total if total > 0 else 0
avg_conceded = goals_conceded / total if total > 0 else 0
# Standard deviation for variance/confidence
def std_dev(values, mean):
if len(values) < 2:
return 0.0
return (sum((v - mean) ** 2 for v in values) / len(values)) ** 0.5
scored_std = std_dev(goals_per_match, avg_scored)
conceded_std = std_dev(conceded_per_match, avg_conceded)
return {
"team": team_name,
"matches": total,
"wins": wins,
"draws": draws,
"losses": losses,
"win_pct": wins / total if total > 0 else 0,
"draw_pct": draws / total if total > 0 else 0,
"loss_pct": losses / total if total > 0 else 0,
"goals_scored": goals_scored,
"goals_conceded": goals_conceded,
"avg_goals_scored": avg_scored,
"avg_goals_conceded": avg_conceded,
"goals_scored_std": round(scored_std, 2),
"goals_conceded_std": round(conceded_std, 2),
}
def get_h2h_stats(team_a: str, team_b: str, country: str = "Argentina") -> dict:
"""
Returns STRUCTURED H2H data for Python-native probability calculation.
"""
id_a = _scraper.search_team(team_a, country)
id_b = _scraper.search_team(team_b, country)
if not id_a or not id_b:
return {"matches": 0, "a_wins": 0, "draws": 0, "b_wins": 0}
events_a = _scraper.get_team_events(id_a, page=0)
events_a += _scraper.get_team_events(id_a, page=1)
a_wins, draws, b_wins = 0, 0, 0
for e in events_a:
home_id = e["homeTeam"]["id"]
away_id = e["awayTeam"]["id"]
if (home_id == id_a and away_id == id_b) or (home_id == id_b and away_id == id_a):
status = e.get("status", {}).get("code", 0)
hs = e.get("homeScore", {}).get("current")
as_ = e.get("awayScore", {}).get("current")
if status == 100 and hs is not None and as_ is not None:
if hs == as_:
draws += 1
elif (home_id == id_a and hs > as_) or (away_id == id_a and as_ > hs):
a_wins += 1
else:
b_wins += 1
total = a_wins + draws + b_wins
return {
"matches": total,
"a_wins": a_wins,
"draws": draws,
"b_wins": b_wins,
"a_win_pct": a_wins / total if total > 0 else 0,
"draw_pct": draws / total if total > 0 else 0,
"b_win_pct": b_wins / total if total > 0 else 0,
}
def compute_true_probability(home_form: dict, away_form: dict, h2h: dict, is_home: bool = True) -> dict:
"""
Python-native True Probability calculation.
Uses REAL data from SofaScore form and H2H stats.
Formula (fully logged):
Step 1: Cross-reference form: home_win = avg(home_win%, away_loss%)
Step 2: H2H adjustment (weight scales with sample size: 2 matches=10%, 5+=30%)
Step 3: Home advantage bonus (+5% home, -5% away)
Step 4: Normalize to sum to 1.0
Step 5: Poisson distribution for over/under totals
"""
from math import exp, factorial
home_matches = home_form.get("matches", 0)
away_matches = away_form.get("matches", 0)
if home_matches < 5 or away_matches < 5:
return {"error": "Insufficient form data", "home_win": 0, "draw": 0, "away_win": 0}
reasoning_lines = ["PYTHON-COMPUTED True Probability:"]
# ── STEP 1: Form-based cross-referencing ──
# Home win rate = average of (home team's win%) and (away team's loss%)
# Logic: if home wins 40% AND away loses 50%, home's true strength ≈ 45%
home_win_form = home_form["win_pct"]
away_loss_pct = away_form["loss_pct"]
home_win_rate = (home_win_form + away_loss_pct) / 2
away_win_form = away_form["win_pct"]
home_loss_pct = home_form["loss_pct"]
away_win_rate = (away_win_form + home_loss_pct) / 2
home_draw_form = home_form["draw_pct"]
away_draw_form = away_form["draw_pct"]
draw_rate = (home_draw_form + away_draw_form) / 2
reasoning_lines.append(
f" Step 1 - Form Cross-Reference:\n"
f" Home form: {home_form['wins']}W {home_form['draws']}D {home_form['losses']}L "
f"({home_win_form*100:.0f}%W {home_form['draw_pct']*100:.0f}%D {home_loss_pct*100:.0f}%L) in {home_matches} matches\n"
f" Away form: {away_form['wins']}W {away_form['draws']}D {away_form['losses']}L "
f"({away_win_form*100:.0f}%W {away_form['draw_pct']*100:.0f}%D {away_loss_pct*100:.0f}%L) in {away_matches} matches\n"
f" Home Win = avg(home_win%={home_win_form:.2f}, away_loss%={away_loss_pct:.2f}) = {home_win_rate:.3f}\n"
f" Draw = avg(home_draw%={home_draw_form:.2f}, away_draw%={away_draw_form:.2f}) = {draw_rate:.3f}\n"
f" Away Win = avg(away_win%={away_win_form:.2f}, home_loss%={home_loss_pct:.2f}) = {away_win_rate:.3f}"
)
# ── STEP 2: H2H adjustment (dynamic weight) ──
h2h_matches = h2h.get("matches", 0)
if h2h_matches >= 2:
# Weight scales with sample size: 2=15%, 3=20%, 4=25%, 5+=30%
h2h_weight = min(0.30, 0.10 + 0.05 * (h2h_matches - 1))
form_weight = 1.0 - h2h_weight
h2h_home_win = h2h.get("a_win_pct", 0)
h2h_draw = h2h.get("draw_pct", 0)
h2h_away_win = h2h.get("b_win_pct", 0)
pre_h2h = (home_win_rate, draw_rate, away_win_rate)
home_win_rate = home_win_rate * form_weight + h2h_home_win * h2h_weight
draw_rate = draw_rate * form_weight + h2h_draw * h2h_weight
away_win_rate = away_win_rate * form_weight + h2h_away_win * h2h_weight
reasoning_lines.append(
f" Step 2 - H2H Adjustment ({h2h_matches} matches, weight={h2h_weight*100:.0f}%):\n"
f" H2H record: Home {h2h.get('a_wins',0)}W, Draw {h2h.get('draws',0)}, Away {h2h.get('b_wins',0)}W\n"
f" H2H probs: Home={h2h_home_win:.2f}, Draw={h2h_draw:.2f}, Away={h2h_away_win:.2f}\n"
f" Blended: Home {pre_h2h[0]:.3f}×{form_weight:.0%} + {h2h_home_win:.2f}×{h2h_weight:.0%} = {home_win_rate:.3f}\n"
f" Blended: Draw {pre_h2h[1]:.3f}×{form_weight:.0%} + {h2h_draw:.2f}×{h2h_weight:.0%} = {draw_rate:.3f}\n"
f" Blended: Away {pre_h2h[2]:.3f}×{form_weight:.0%} + {h2h_away_win:.2f}×{h2h_weight:.0%} = {away_win_rate:.3f}"
)
else:
reasoning_lines.append(
f" Step 2 - H2H Adjustment: SKIPPED (only {h2h_matches} matches, need ≥2)"
)
# ── STEP 3: Home advantage ──
HOME_BONUS = 0.05
pre_bonus = (home_win_rate, away_win_rate)
home_win_rate += HOME_BONUS
away_win_rate -= HOME_BONUS
# Clamp away_win_rate to minimum 0
away_win_rate = max(0, away_win_rate)
reasoning_lines.append(
f" Step 3 - Home Advantage (+{HOME_BONUS*100:.0f}%):\n"
f" Home: {pre_bonus[0]:.3f} + {HOME_BONUS} = {home_win_rate:.3f}\n"
f" Away: {pre_bonus[1]:.3f} - {HOME_BONUS} = {away_win_rate:.3f}"
)
# ── STEP 4: Normalize to sum to 1.0 ──
total = home_win_rate + draw_rate + away_win_rate
if total > 0:
home_win_rate /= total
draw_rate /= total
away_win_rate /= total
reasoning_lines.append(
f" Step 4 - Normalize (sum={total:.3f}):\n"
f" FINAL: Home {home_win_rate*100:.1f}% | Draw {draw_rate*100:.1f}% | Away {away_win_rate*100:.1f}%"
)
# ── STEP 5: Expected goals & Poisson for over/under ──
home_avg_scored = home_form["avg_goals_scored"]
home_avg_conceded = home_form["avg_goals_conceded"]
away_avg_scored = away_form["avg_goals_scored"]
away_avg_conceded = away_form["avg_goals_conceded"]
expected_home_goals = (home_avg_scored + away_avg_conceded) / 2
expected_away_goals = (away_avg_scored + home_avg_conceded) / 2
expected_total_goals = expected_home_goals + expected_away_goals
# Poisson CDF: P(X <= k) = sum(e^-λ * λ^i / i!, i=0..k)
def poisson_cdf(k, lam):
if lam <= 0:
return 1.0
# For large lambda (like NBA scores), use Normal approximation to prevent OverflowError
if lam > 30:
import math
# Continuity correction +0.5
z = (k + 0.5 - lam) / math.sqrt(lam)
return 0.5 * (1.0 + math.erf(z / math.sqrt(2.0)))
cdf = 0.0
for i in range(k + 1):
cdf += (exp(-lam) * (lam ** i)) / factorial(i)
return cdf
under_2_5 = poisson_cdf(2, expected_total_goals)
over_2_5 = 1.0 - under_2_5
under_1_5 = poisson_cdf(1, expected_total_goals)
over_1_5 = 1.0 - under_1_5
reasoning_lines.append(
f" Step 5 - Poisson Totals (λ={expected_total_goals:.2f}):\n"
f" Home xG: avg(scored={home_avg_scored:.1f}±{home_form.get('goals_scored_std', 0):.1f}, opp_conceded={away_avg_conceded:.1f}±{away_form.get('goals_conceded_std', 0):.1f}) = {expected_home_goals:.2f}\n"
f" Away xG: avg(scored={away_avg_scored:.1f}±{away_form.get('goals_scored_std', 0):.1f}, opp_conceded={home_avg_conceded:.1f}±{home_form.get('goals_conceded_std', 0):.1f}) = {expected_away_goals:.2f}\n"
f" Total xG: {expected_total_goals:.2f} (variance note: high σ = less reliable)\n"
f" Poisson P(≤1) = {under_1_5:.3f} → Over 1.5: {over_1_5*100:.1f}% | Under 1.5: {under_1_5*100:.1f}%\n"
f" Poisson P(≤2) = {under_2_5:.3f} → Over 2.5: {over_2_5*100:.1f}% | Under 2.5: {under_2_5*100:.1f}%"
)
reasoning = "\n".join(reasoning_lines)
return {
"home_win": round(home_win_rate, 4),
"draw": round(draw_rate, 4),
"away_win": round(away_win_rate, 4),
"expected_total_goals": round(expected_total_goals, 2),
"expected_home_goals": round(expected_home_goals, 2),
"expected_away_goals": round(expected_away_goals, 2),
"over_2_5": round(over_2_5, 4),
"under_2_5": round(under_2_5, 4),
"over_1_5": round(over_1_5, 4),
"under_1_5": round(under_1_5, 4),
"reasoning": reasoning,
"data_quality": {
"home_form_matches": home_matches,
"away_form_matches": away_matches,
"h2h_matches": h2h_matches,
}
}