def run_experiment_0(exp, league, type_evaluation, **params):
    """

    :param exp:
    :param league:
    :param type_evaluation:
    :param params:
    :return:
    """
    predictor = Predictor.get_predictor()
    filter_season = util.get_default(params, "season", None)
    for season in league.get_seasons():
        if not util.is_None(filter_season) and season != filter_season:
            continue

        invest = 0
        profit = 0

        print(season)
        if season == util.get_current_season():
            break

        for stage in range(1, league.get_stages_by_season(season) + 1):

            # KEY: match id     VALUE: <prediction, probability>
            stage_predictions = predictor.predict(league, season, stage,
                                                  **params)
            current_stage_bet = StageBet(stage, type_evaluation)

            for match_id, pair in stage_predictions.items():
                if len(pair) == 0:
                    continue
                match = Match.read_by_match_id(match_id)
                if util.is_None(match.B365H) or util.is_None(
                        match.B365D) or util.is_None(match.B365A):
                    continue

                predicted_label, prob = pair[0], pair[1]
                bet_odd = get_bet_odd(predicted_label, match)

                m_invest, m_profit = evaluate_bet(predictor, type_evaluation,
                                                  match, predicted_label, prob)

                if type_evaluation == 5:
                    if m_invest == 1:
                        current_stage_bet.add_bet(prob, m_profit, bet_odd)

                elif type_evaluation == 6:
                    current_stage_bet.add_bet(prob, m_profit, bet_odd)

                elif m_invest == 1:
                    current_stage_bet.add_bet(prob, m_profit)

            profit += current_stage_bet.get_profit()
            invest += current_stage_bet.get_invest()

            print(stage, "\t",
                  str(round(profit - invest, 2)).replace(".", ","))
        print("Final investment:\t", str(round(invest, 2)).replace(".", ","))
        print("Final profit:\t", str(round(profit, 2)).replace(".", ","))
    def train_predict(self, stage):
        ml_alg = MachineLearningAlgorithm.get_machine_learning_algorithm(self.ml_alg_framework,
                                                                         self.ml_alg_method,
                                                                         self.matches,
                                                                         self.labels,
                                                                         data_description=self.matches_id,
                                                                         train_percentage=1,
                                                                         **self.ml_alg_params)
        try:
            ml_alg.train()
        except ValueError as ve:
            print(ve)
            raise MLException(3)

        predicted_labels, probability_events = ml_alg.predict(self.matches_to_predict)
        accuracy = 0
        # print("***",stage,"***")
        for predicted_label, prob, label, match_id in zip(predicted_labels,
                                                            probability_events,
                                                            self.labels_to_predict,
                                                            self.matches_to_predict_id):

            # print(match_name, predicted_label, "[",label,"]")

            if predicted_label == label:
                accuracy += 1
            home_team_name = Match.read_by_match_id(match_id).get_home_team().team_short_name
            away_team_name = Match.read_by_match_id(match_id).get_away_team().team_short_name
            try:
                self.accuracy_by_team_dic[home_team_name].next_prediction(predicted_label, label,True)
            except KeyError:
                self.accuracy_by_team_dic[home_team_name] = TeamPredictionAccuracy(home_team_name)
                self.accuracy_by_team_dic[home_team_name].next_prediction(predicted_label, label,True)
            try:
                self.accuracy_by_team_dic[away_team_name].next_prediction(predicted_label, label, False)
            except KeyError:
                self.accuracy_by_team_dic[away_team_name] = TeamPredictionAccuracy(away_team_name)
                self.accuracy_by_team_dic[away_team_name].next_prediction(predicted_label, label,False)

        return accuracy/len(predicted_labels)
Esempio n. 3
0
    def predict(self, league, season, stage):
        try:
            self.predictions[league.id]
        except KeyError:
            self.predictions[league.id] = dict()

        this_predictions = dict()
        try:
            for match_id, p in self.predictions[league.id].items():
                match = Match.read_by_match_id(match_id)
                if match.season == season and match.stage == stage:
                    this_predictions[match_id] = p
        except KeyError:
            pass

        if len(this_predictions) > 0:
            return this_predictions
        this_predictions[league.id] = dict()

        try:
            matches, labels, matches_id, matches_to_predict, matches_to_predict_id, labels_to_predict = \
                mli.get_input_to_train(self.ml_train_input_id,
                                       league,
                                       self.ml_train_input_representation,
                                       stage,
                                       self.ml_train_stages_to_train,
                                       season)

            ml_alg = mla.get_machine_learning_algorithm(self.ml_alg_framework,
                                                        self.ml_alg_method,
                                                        matches,
                                                        labels,
                                                        matches_id,
                                                        train_percentage=1,
                                                        )

            ml_alg.train()
            predicted_labels, probability_events = ml_alg.predict(matches_to_predict)

            for match_id, prediction, probability in zip(matches_to_predict_id, predicted_labels, probability_events):
                this_predictions[match_id] = [prediction, probability]
                self.predictions[league.id][match_id] = [prediction, probability]

            return this_predictions
        except Exception as e:
            return {}
def run_experiment_4(exp,
                     league,
                     predictor=Predictor.get_predictor(),
                     **params):

    for season in league.get_seasons()[4:]:
        distance = {i: 0 for i in [0, 1, 2]}
        number_bet_odds = {i: 0 for i in [0, 1, 2]}
        print(season)
        for stage in range(1, league.get_stages_by_season(season) + 1):

            stage_predictions = predictor.predict(league, season, stage,
                                                  **params)

            for match_id, pair in stage_predictions.items():
                if len(pair) == 0:
                    continue
                match = Match.read_by_match_id(match_id)
                if util.is_None(match.B365H) or util.is_None(match.B365D) or util.is_None(match.B365A)\
                        or match.B365H == 0 or match.B365D == 0 or match.B365A == 0:
                    continue

                predicted_label, prob = pair[0], pair[1]

                our_bet_odds = 1 / prob
                bm_bet_odds = -1

                if predicted_label == 1:
                    bm_bet_odds = match.B365H
                elif predicted_label == 0:
                    bm_bet_odds = match.B365D
                elif predicted_label == 2:
                    bm_bet_odds = match.B365A

                distance[predicted_label] += math.fabs(our_bet_odds -
                                                       bm_bet_odds)
                number_bet_odds[predicted_label] += 1

        print(1, distance[1] / max(number_bet_odds[1], 1))
        print(0, distance[0] / max(number_bet_odds[0], 1))
        print(2, distance[2] / max(number_bet_odds[2], 1))
Esempio n. 5
0
    def get_best_team_predicted(self, league, season, stage, n_teams_returned=3):

        best_teams = dict()
        s = season
        i = 1
        if stage - 1 == 0:
            y = int(s.split("/")[0]) - 1
            s = str(y) + "/" + str(y + 1)
            stage_predictions = league.get_stages_by_season(s)
        else:
            stage_predictions = stage - 1

        while i <= self.ml_train_stages_to_train:
            predictions = self.predict(league, s, stage_predictions)

            for match_id, pair in predictions.items():
                if len(pair) == 0:
                    continue
                match = Match.read_by_match_id(match_id)
                pred_label = pair[0]
                if pred_label == MLUtil.get_label(match):
                    util.increase_dict_entry(match.home_team_api_id, best_teams)
                    util.increase_dict_entry(match.away_team_api_id, best_teams)
            i += 1
            if stage_predictions - i == 0:
                y = int(s.split("/")[0])-1
                s = str(y)+"/"+str(y+1)
                stage_predictions = league.get_stages_by_season(s)
            else:
                stage_predictions -= 1

        h = []
        for team_api_id, accuracy in best_teams.items():
            heapq.heappush(h, (accuracy, team_api_id))

        top_k = [Team.read_by_team_api_id(team_api_id) for a, team_api_id
                 in heapq.nlargest(n_teams_returned, h, lambda x: x[0])[:n_teams_returned]]
        return top_k