Exemple #1
0
    def to_dict(self):
        """
        Summarize the results of optimization.

        Returns:
            dict[str, float or int]:
                - (parameters of the model)
                - tau
                - Rt: basic or phase-dependent reproduction number
                - (dimensional parameters [day])
                - {metric name}: score with the metric
                - Trials: the number of trials
                - Runtime: run time of estimation
        """
        tau, param_dict = self._param()
        model_instance = self.model(population=self.population, **param_dict)
        return {
            **param_dict, self.TAU:
            tau,
            self.RT:
            model_instance.calc_r0(),
            **model_instance.calc_days_dict(tau), self._metric:
            self._score(tau, param_dict),
            self.TRIALS:
            self.total_trials,
            self.RUNTIME:
            StopWatch.show(self.runtime)
        }
    def run(self,
            timeout=60,
            reset_n_max=3,
            timeout_iteration=5,
            allowance=(0.98, 1.02),
            seed=0,
            **kwargs):
        """
        Run optimization.
        If the result satisfied the following conditions, optimization ends.
        - all values are not under than 0
        - values of monotonic increasing variables increases monotonically
        - predicted values are in the allowance when each actual value shows max value

        Args:
            timeout (int): time-out of run
            reset_n_max (int): if study was reset @reset_n_max times, will not be reset anymore
            timeout_iteration (int): time-out of one iteration
            allowance (tuple(float, float)): the allowance of the predicted value
            seed (int or None): random seed of hyperparameter optimization
            kwargs: other keyword arguments will be ignored

        Notes:
            @n_jobs was obsoleted because this is not effective for Optuna.
        """
        # Create a study of optuna
        if self.study is None:
            self._init_study(seed=seed)
        reset_n = 0
        iteration_n = math.ceil(timeout / timeout_iteration)
        increasing_cols = [f"{v}{self.P}" for v in self.model.VARS_INCLEASE]
        stopwatch = StopWatch()
        for _ in range(iteration_n):
            # Perform optimization
            self.study.optimize(self.objective,
                                n_jobs=1,
                                timeout=timeout_iteration)
            # Create a table to compare observed/estimated values
            tau = self.tau or super().param()[self.TAU]
            train_df = self.divide_minutes(tau)
            comp_df = self.compare(train_df, self.predict())
            # Check monotonic variables
            mono_ok_list = [
                comp_df[col].is_monotonic_increasing for col in increasing_cols
            ]
            if not all(mono_ok_list):
                if reset_n == reset_n_max - 1:
                    break
                # Initialize the study
                self._init_study()
                reset_n += 1
                continue
            # Need additional trials when the values are not in allowance
            if self._is_in_allowance(comp_df, allowance):
                break
        # Calculate run-time and the number of trials
        self.run_time = stopwatch.stop()
        self.run_time_show = stopwatch.show()
        self.total_trials = len(self.study.trials)