def fill_matrix(func: Callable, symmetry: bool, matrix: np.ndarray, table_map: TableMap, table: LeagueTable, half: Half): verbose_message('Filling matrix for season {}'.format(table.season)) for fixture in table.season.fixtures(): home_team_position = table.team_position(fixture.home_team) home_team_chunk_id = table_map.get_chunk(home_team_position) away_team_position = table.team_position(fixture.away_team) away_team_chunk_id = table_map.get_chunk(away_team_position) if half == Half.both: result = fixture.full_time() elif half == Half.first: result = fixture.first_half() elif half == Half.second: result = fixture.second_half() else: assert False if result: if func(result): matrix[home_team_chunk_id][away_team_chunk_id] += 1 if symmetry: matrix[away_team_chunk_id][home_team_chunk_id] += 1
def show_results(league: League, season: Season, results: List[str]): matched = [] for fixture in season.fixtures(): result = fixture.full_time() if result: for given_result in results: left, right = map(int, given_result.split('-')) if left == result.left and right == result.right: matched.append(fixture) else: messages.warning_message('Ignoring {}'.format(fixture)) if matched: table = LeagueTable(season, Half.both) print('>' * 40, season.year, '<' * 40) for fixture in matched: print('{:<2} vs {:<2}: {}'.format( table.team_position(fixture.home_team) + 1, table.team_position(fixture.away_team) + 1, fixture)) print()
def main(args: Namespace): load_teams(args.database) league = league_register[get_unique_league(args)] load_league(args.database, league) seasons = Season.seasons(league) if not seasons: messages.error_message("No season data found") if args.history: seasons = seasons[-args.history:] if args.team: (row, ) = extract_picked_team(args.database, args.team, league) selected_team = Team.inventory[row[0]] else: selected_team = None func = Event.get(get_unique_event(args)) if args.chunks: data = compute_chunked_data(seasons, func, args.negate, args.venue, args.half, args.chunks) nrows = len(data) if selected_team is not None: ncols = 3 else: ncols = 1 fig, axes = plt.subplots(nrows=len(data), ncols=ncols, figsize=(20, 13), squeeze=False, constrained_layout=True) x_limit, _ = find_limits(data) for i, datum in enumerate(data): ax = axes[i, 0] plot(ax, datum, x_limit, args.lines) if selected_team is not None: golden_season = seasons[-1] golden_table = LeagueTable(golden_season, args.half) golden_map = golden_table.group(args.chunks) chunk_to_seasons = OrderedDict() for chunk_id in range(golden_map.number_of_chunks()): if chunk_id not in chunk_to_seasons: chunk_to_seasons[chunk_id] = [] for season in seasons: if season != golden_season: table = LeagueTable(season, args.half) table_map = table.group(args.chunks) if selected_team in season.teams(): position = table.team_position(selected_team) chunk_id = table_map.get_chunk(position) chunk_to_seasons[chunk_id].append(season) chunk_to_datum = OrderedDict() for chunk_id, chunk_seasons in chunk_to_seasons.items(): if chunk_seasons: datum = DataUnit(Counter(), chunk_seasons, team=selected_team, positions=golden_map.get_rows(chunk_id)) chunk_to_datum[chunk_id] = datum for season in seasons: if season == golden_season: position = golden_table.team_position(selected_team) datum = DataUnit(Counter(), [golden_season], team=selected_team, positions=[position], highlight=True) golden_datum = datum else: if selected_team in season.teams(): table = LeagueTable(season, args.half) table_map = table.group(args.chunks) position = table.team_position(selected_team) chunk_id = table_map.get_chunk(position) datum = chunk_to_datum[chunk_id] count_events(season, selected_team, args.venue, args.half, func, args.negate, datum) position = golden_table.team_position(selected_team) chunk_id = golden_map.get_chunk(position) ax = axes[chunk_id, 1] plot(ax, golden_datum, x_limit, args.lines) used = {(chunk_id, 1)} for chunk_id, datum in chunk_to_datum.items(): ax = axes[chunk_id, 2] plot(ax, datum, x_limit, args.lines) used.add((chunk_id, 2)) for i in range(0, nrows): for j in range(1, 3): if (i, j) not in used: fig.delaxes(axes[i, j]) else: data = compute_aggregated_data(seasons, selected_team, func, args.negate, args.venue, args.half) x_limit, _ = find_limits(data) display = DisplayGrid(len(data), 2) fig, axes = plt.subplots(nrows=display.nrows, ncols=display.ncols, figsize=(20, 10), squeeze=False) for i, datum in enumerate(data): cell_x, cell_y = display.index(i) ax = axes[cell_x, cell_y] plot(ax, datum, x_limit, args.lines) title = construct_title(league, func, args.negate, args.venue, args.half) fig.suptitle(title, fontweight='bold', fontsize=14) plt.show(block=args.block)
def main(args: Namespace): league = league_register[get_unique_league(args)] load_database(args.database, league) seasons = Season.seasons(league) this_season = seasons.pop() assert this_season.current func = Event.get(get_unique_event(args)) for i in range(1, 38): prediction = Prediction(func, i) penalties = Counter() for season in seasons: table = LeagueTable(season, Half.both) states = {team: TeamState() for team in season.teams()} season.sort_fixtures() fixture: Fixture for fixture in season.fixtures(): if args.half == Half.both: result = fixture.full_time() elif args.half == Half.first: result = fixture.first_half() elif args.half == Half.second: result = fixture.second_half() if result: home_result = fixture.canonicalise_result( fixture.home_team, result) home_outcome = prediction.func(home_result) home_state = states[fixture.home_team] if not home_outcome: home_state.fixtures.append(fixture) else: if home_state.alive: index = len( home_state.fixtures) - prediction.minimum + 1 penalties[index] += 1 states[fixture.home_team] = TeamState() if len(home_state.fixtures) == prediction.minimum: final_position = table.team_position(fixture.home_team) if final_position not in [ 0, 1, len(table) - 2, len(table) - 1 ]: home_state.alive = True away_result = fixture.canonicalise_result( fixture.away_team, result) away_outcome = prediction.func(away_result) away_state = states[fixture.away_team] if not away_outcome: away_state.fixtures.append(fixture) else: if away_state.alive: index = len( away_state.fixtures) - prediction.minimum + 1 penalties[index] += 1 states[fixture.away_team] = TeamState() away_state.fixtures.append(fixture) if len(away_state.fixtures) == prediction.minimum: final_position = table.team_position(fixture.away_team) if final_position not in [ 0, 1, len(table) - 2, len(table) - 1 ]: away_state.alive = True total_penalty = 0 total_correct = 0 for distance, correct in penalties.items(): exponent = distance this_penalty = 2**exponent - 1 total_penalty += this_penalty * correct total_correct += correct if total_penalty: print( 'Betting from sequences of {} returns {} right with a penalty of {}' .format(i, total_correct, total_penalty))