Exemple #1
0
    def get_best(self):
        """ Produce a best instance of this strategy.

            Args:
                void.

            Returns:
                self: Euler instance. With the params and model having the
                    highest score among all combinations.
        """
        # Log enter.
        logger.info("Euler: Selecting best for %s.", self.instrument)

        # Initialize the scores array and strategy parameters.
        scores = []
        strategy_params = util.get_euler_params()

        # TODO: Do more than 1 run.
        # Run for all predictive models.
        counter = 0
        for model in self.all_models:
            scores_row = []
            model_params = util.get_model_params(model)

            # Try different model parameters.
            for model_param in model_params:
                scores_col = []
                model = self.learner.build_model(model, 0.9, **model_param)
                pred, _ = self.learner.test_model(model)

                # And different strategy parameters, e.g. threshold.
                for strategy_param in strategy_params:
                    self.set_params(**strategy_param)

                    # Do the dry run.
                    plot_name = '{0}_{1}.png'.format(self.instrument, counter)
                    balance = self.dry_run(pred, export_plot=plot_name)

                    # Determine the quality of the params via score.
                    scores_col.append(util.get_strategy_score(balance))
                    counter += 1

                scores_row.append(scores_col)

            scores.append(scores_row)

        # Get the best and set the parameters to the best.
        best = np.unravel_index(np.argmax(scores), np.array(scores).shape)

        model = self.all_models[best[0]]
        model_param = util.get_model_params(model)[best[1]]
        model = self.learner.build_model(model, 1, **model_param)

        logger.info("Best score is: %s.", str(np.array(scores)[best]))
        self.set_params(**strategy_params[best[2]])
        self.model = model

        return self
Exemple #2
0
    def test_params(self):
        """ Make sure the parameter spaces are correct."""
        # First up, a tree model.
        tree_model = tree.DecisionTreeClassifier()
        model_params = util.get_model_params(tree_model)
        self.assertEqual(len(model_params), 20)
        self.assertEqual(model_params[0]['max_depth'], 4)
        self.assertEqual(model_params[-3]['min_samples_split'], 10)

        # Now params for strategy Euler.
        strategy_params = util.get_euler_params()
        self.assertEqual(len(strategy_params), 16)
        self.assertEqual(strategy_params[3]['unit_shape'], common.UNIT_LOG)
        self.assertEqual(strategy_params[-2]['threshold'], 100.0)

        return