Ejemplo n.º 1
0
def get_bracket_array(bracket_dict):
    bracket_array = np.zeros((16, 4))
    round_array = generate_round_array(16)
    for i in range(15):
        # pdb.set_trace()
        winning_team = bracket_dict[i].lower()
        if winning_team in team_id_dict:
            winning_team_idx = team_id_dict[winning_team]
            bracket_array[winning_team_idx, round_array[i]] = 1

    # return bracket_array.astype(int)
    return bracket_array.reshape((1, -1)).astype(int)
def generate_outcome_matrix(num_teams, score_array=None):
    # num_teams = 16
    round_array = generate_round_array(num_teams)
    game_pointer_array, round_pointer_array = generate_pointer_arrays(
        num_teams)
    if score_array is None:
        score_array = np.array([4, 8, 16, 32])

    max_games_per_team = int(np.log2(num_teams))
    total_games = num_teams - 1

    # Create everything using generators
    row_list = []
    likelihood_list = []
    team_ratings = team_ratings_538
    for truthtable_row in tqdm(bracket_truth_table_generator(num_teams),
                               total=2**total_games):
        outcome_row, outcome_likelihood = generate_outcome_row(
            num_teams, total_games, max_games_per_team, truthtable_row,
            round_array, round_pointer_array, game_pointer_array, score_array,
            team_ratings)
        row_list.append(outcome_row.reshape((-1, 1)))
        likelihood_list.append(outcome_likelihood)

    outcome_matrix = np.hstack(row_list)
    likelihood_array = np.hstack(likelihood_list)
    # This  was the preallocation method
    # bracket_truth_table = generate_bracket_truth_table(num_teams)
    #
    # outcome_matrix = np.zeros((bracket_truth_table.shape[0], num_teams * max_games_per_team))
    #
    # # random_row = np.random.choice(np.arange(bracket_truth_table.shape[0]))
    # for row in tqdm(range(bracket_truth_table.shape[0])):
    # 	game_outcome_array = bracket_truth_table[row,:]
    #
    # 	outcome_row = generate_outcome_row(num_teams, total_games, max_games_per_team, game_outcome_array, round_array, round_pointer_array, game_pointer_array, score_array)
    # 	outcome_matrix[row, :] = outcome_row.reshape((1,-1))
    # print(game_outcome_array)
    # print(outcome_row)
    return outcome_matrix, likelihood_array
Ejemplo n.º 3
0
def pool_overview(bracket_list, bracket_matrix, current_score_array,
                  df_bracket_pool, score_array):
    page_header()

    if bracket_list is not None and bracket_matrix is not None and current_score_array is not None:
        st.header('3.0 Bracket Pool Results')

        outcome_matrix, likelihood_array = get_outcome_matrix(score_array)
        # print(outcome_matrix)
        selected_outcome_matrix = outcome_matrix.copy()
        data_mode = st.selectbox(
            'What do you want to do with the bracket pool data?',
            ['Inspect Data', 'Look at Scenarios'])
        if data_mode == 'Look at Scenarios':
            winner_dict = {}
            round_array = generate_round_array(16)
            for i in range(15):
                winner_dict[i] = None

            for i in range(0, 8):
                print(i)
                game_dict = all_games_dict[i]
                # winner = optional_winner_selectbox(game_name=game_dict['name'], team0=game_dict['team0'],
                #                                         team1=game_dict['team1'], i=i, index=2)
                option_list = [game_dict['team0'], game_dict['team1'], 'None']
                index = index_list_2021[i]
                if index == -1:
                    index = len(option_list) - 1
                winner = optional_winner_selectbox(
                    game_name=game_dict['name'],
                    game_type=game_type_dict[round_array[i]],
                    option_list=option_list,
                    i=-1,
                    index=index)
                if winner == 'None':
                    winner = None
                winner_dict[i] = winner

            for i in range(8, 15):
                print(i)
                game_dict = all_games_dict[i]
                print(game_dict)
                team0 = winner_dict[game_dict['team0']]
                team1 = winner_dict[game_dict['team1']]
                print(team0)
                print(team1)
                print(game_dict['name'].format(team0=team0, team1=team1))
                print(team0 is not None)
                print(team1 is not None)
                if team0 is not None or team1 is not None:
                    option_list = []
                    if team0 is None:
                        team0 = 'Other'
                    else:
                        option_list.append(team0)
                    if team1 is None:
                        team1 = 'Other'
                    else:
                        option_list.append(team1)
                    option_list.append('None')
                    index = index_list_2021[i]
                    if index == -1:
                        index = len(option_list) - 1
                    winner = optional_winner_selectbox(
                        game_name=game_dict['name'].format(team0=team0,
                                                           team1=team1),
                        game_type=game_type_dict[round_array[i]],
                        option_list=option_list,
                        i=-1,
                        index=index)

                    if winner == 'None':
                        winner = None
                    winner_dict[i] = winner

            score_array = np.array([4, 8, 16, 32])
            for i in range(15):
                winner_key = winner_dict[i]
                if winner_key is not None:
                    winner_key = winner_key.lower()
                    winner_id = team_id_dict[winner_key]
                    winner_idx = winner_id * 4 + round_array[i]
                    winner_value = score_array[round_array[i]]

                    print(selected_outcome_matrix.shape)

                    selected_outcomes = selected_outcome_matrix[
                        winner_idx, :] == winner_value
                    likelihood_array = likelihood_array[selected_outcomes]
                    selected_outcome_matrix = selected_outcome_matrix[:,
                                                                      selected_outcomes]

                    print(selected_outcome_matrix.shape)

        with st.spinner('Calculating Outcomes...'):
            bracket_pool_scores = load_or_generate_bracket_pool_scores(
                static_path,
                bracket_matrix,
                selected_outcome_matrix,
                current_score_array,
                force_generation=False)

            likelihood_array = likelihood_array / np.sum(likelihood_array)
            df_money_chances = print_money_chances(bracket_list,
                                                   bracket_pool_scores,
                                                   likelihood_array)
        #columns=['first_alone', 'first_tied', 'second_alone', 'second_tied', 'third_alone', 'third_tied']
        total_cases = selected_outcome_matrix.shape[1]
        if total_cases > 0:
            df_money_chances_with_percents = df_money_chances.copy()
            df_money_chances_with_percents[
                'first_alone_percent'] = df_money_chances.first_alone / total_cases * 100
            df_money_chances_with_percents[
                'first_tied_percent'] = df_money_chances.first_tied / total_cases * 100
            df_money_chances_with_percents[
                'second_alone_percent'] = df_money_chances.second_alone / total_cases * 100
            df_money_chances_with_percents[
                'second_tied_percent'] = df_money_chances.second_tied / total_cases * 100
            df_money_chances_with_percents[
                'third_alone_percent'] = df_money_chances.third_alone / total_cases * 100
            df_money_chances_with_percents[
                'third_tied_percent'] = df_money_chances.third_tied / total_cases * 100

            df_money_chances_with_percents = df_money_chances_with_percents[[
                'bracket_names',
                'first_alone',
                'first_alone_percent',
                'first_alone_prob',
                'first_tied',
                'first_tied_percent',
                'first_tied_prob',
                'second_alone',
                'second_alone_percent',
                'second_alone_prob',
                'second_tied',
                'second_tied_percent',
                'second_tied_prob',
                'third_alone',
                'third_alone_percent',
                'third_alone_prob',
                'third_tied',
                'third_tied_percent',
                'third_tied_prob',
            ]]

            st.dataframe(df_money_chances_with_percents)
        else:
            st.warning(
                'It is impossible to achieve this combination of selected winners (How did you even do this??)'
            )
def sweet_16_individual_brackets(score_array, bracket_matrix, current_score_array, bracket_index, selected_outcome_matrix, likelihood_array):
    winner_dict = {}
    round_array = generate_round_array(16)
    for i in range(15):
        winner_dict[i] = None

    for i in range(0, 8):
        print(i)
        game_dict = all_games_dict[i]
        # winner = optional_winner_selectbox(game_name=game_dict['name'], team0=game_dict['team0'],
        #                                         team1=game_dict['team1'], i=i, index=2)
        option_list = [game_dict['team0'], game_dict['team1'], 'None']
        index = index_list_2021[i]
        if index == -1:
            index = len(option_list) - 1
        winner = optional_winner_selectbox(game_name=game_dict['name'],
                                           game_type=game_type_dict[round_array[i]],
                                           option_list=option_list, i=-1, index=index)
        if winner == 'None':
            winner = None
        winner_dict[i] = winner

    for i in range(8, 15):
        print(i)
        game_dict = all_games_dict[i]
        print(game_dict)
        team0 = winner_dict[game_dict['team0']]
        team1 = winner_dict[game_dict['team1']]
        print(team0)
        print(team1)
        print(game_dict['name'].format(team0=team0, team1=team1))
        print(team0 is not None)
        print(team1 is not None)
        if team0 is not None or team1 is not None:
            option_list = []
            if team0 is None:
                team0 = 'Other'
            else:
                option_list.append(team0)
            if team1 is None:
                team1 = 'Other'
            else:
                option_list.append(team1)
            option_list.append('None')
            index = index_list_2021[i]
            if index == -1:
                index = len(option_list) - 1
            winner = optional_winner_selectbox(game_name=game_dict['name'].format(team0=team0, team1=team1),
                                               game_type=game_type_dict[round_array[i]],
                                               option_list=option_list, i=-1, index=index)

            if winner == 'None':
                winner = None
            winner_dict[i] = winner

    # score_array = np.array([4,8,16,32])
    for i in range(15):
        winner_key = winner_dict[i]
        if winner_key is not None:
            winner_key = winner_key.lower()
            winner_id = team_id_dict[winner_key]
            winner_idx = winner_id * 4 + round_array[i]
            winner_value = score_array[round_array[i]]

            print(selected_outcome_matrix.shape)

            selected_outcomes = selected_outcome_matrix[winner_idx, :] == winner_value
            likelihood_array = likelihood_array[selected_outcomes]
            selected_outcome_matrix = selected_outcome_matrix[:, selected_outcomes]

            print(selected_outcome_matrix.shape)


    st.subheader('Who should I root for?')
    with st.spinner('Calculating Outcomes...'):
        total_outcomes = selected_outcome_matrix.shape[1]
        bracket_pool_scores = load_or_generate_bracket_pool_scores(static_path, bracket_matrix, selected_outcome_matrix,
                                                                   current_score_array, force_generation=False)

        # df_money_chances = print_money_chances(bracket_list, bracket_pool_scores)
        likelihood_array = likelihood_array / np.sum(likelihood_array)
        # df_money_chances = print_money_chances(bracket_list, bracket_pool_scores[bracket_index, :], likelihood_array)
        sweet_16_case_dict = print_sweet_16_case_probabilities(bracket_index, bracket_matrix, bracket_pool_scores,
                                                               selected_outcome_matrix, likelihood_array)

        game = 0
        count_tracker = 0
        #
        # team0_dict['win_paths'] = team_0_win_count
        # team0_dict['win_paths_delta'] = team_0_win_count - base_total_paths
        # team0_dict['win_percent'] = team_0_win_count / total_outcomes * 100
        # team0_dict['win_percent_delta'] = team0_dict['win_percent'] - base_paths_percent
        # team0_dict['win_likelihood'] = np.sum(team_0_likeliood) * 100
        # team0_dict['win_likelihood_delta'] = team0_dict['win_likelihood'] - base_likelihood

        # st.write('<p style="color:green">THIS TEXT WILL BE RED</p>', unsafe_allow_html=True)6

        for key in sweet_16_case_dict.keys():
            if count_tracker == 0:
                st.subheader('Game {game}: {game_name}'.format(
                    game=game,
                    game_name=all_games_dict[game]['name']
                ))
            win_string = 'Win paths left if {team_name} wins: {win_paths} (Change: {win_paths_delta})'.format(
                team_name=key,
                win_paths=sweet_16_case_dict[key]['win_paths'],
                win_paths_delta=sweet_16_case_dict[key]['win_paths_delta'],
            )
            st.write(win_string)
            if sweet_16_case_dict[key]['win_percent_delta'] > 0:
                color = 'green'
            elif sweet_16_case_dict[key]['win_percent_delta'] < 0:
                color = 'red'
            else:
                color = 'black'

            win_string = 'Win path percentages left if {team_name} wins: {win_percent:2f}% (Change: <font style="color:{color}">{win_percent_delta:2f}%</font>)'.format(
                team_name=key,
                win_percent=sweet_16_case_dict[key]['win_percent'],
                win_percent_delta=sweet_16_case_dict[key]['win_percent_delta'],
                color=color
            )
            # st.write(win_string)
            print(win_string)
            st.markdown(win_string, unsafe_allow_html=True)
            if sweet_16_case_dict[key]['win_likelihood_delta'] > 0:
                color = 'green'
            elif sweet_16_case_dict[key]['win_likelihood_delta'] < 0:
                color = 'red'
            else:
                color = 'black'
            win_string = 'Win likelihood left if {team_name} wins: {win_likelihood:2f}% (Change: <font style="color:{color}">{win_likelihood_delta:2f}%</font>)'.format(
                team_name=key,
                win_likelihood=sweet_16_case_dict[key]['win_likelihood'],
                win_likelihood_delta=sweet_16_case_dict[key]['win_likelihood_delta'],
                color=color
            )
            print(win_string)
            st.write(win_string, unsafe_allow_html=True)
            game += count_tracker
            if count_tracker == 0:
                count_tracker = 1
            else:
                count_tracker = 0