def estimate_tau(self, data, guess_quantile=0.5): """ Select tau value [min] which minimize the score of the metric. Args: data (pandas.DataFrame): Index reset index Columns - Date (pd.Timestamp): Observation date - Susceptible(int): the number of susceptible cases - Infected (int): the number of currently infected cases - Fatal(int): the number of fatal cases - Recovered (int): the number of recovered cases guess_quantile (float): quantile to guess ODE parameter values for the candidates of tau Returns: int: estimated tau value [min] Raises: covsirphy.UnExecutedError: phase information was not set Note: ODE parameter for each tau value will be guessed by .guess() classmethod of the model. Tau value will be selected from the divisors of 1440 [min] and set to self. """ self._ensure_dataframe(data, name="data", columns=self.DSIFR_COLUMNS) df = data.loc[:, self.DSIFR_COLUMNS] if not self._info_dict: raise UnExecutedError("ODEHandler.add()") # Calculate scores of tau candidates self._ensure_float(guess_quantile, name="quantile") calc_f = functools.partial(self._score_tau, data=df, quantile=guess_quantile) divisors = self.divisors(1440) if self._n_jobs == 1: scores = [calc_f(candidate) for candidate in divisors] else: with Pool(self._n_jobs) as p: scores = p.map(calc_f, divisors) score_dict = {k: v for (k, v) in zip(divisors, scores)} # Return the best tau value comp_f = { True: min, False: max }[Evaluator.smaller_is_better(metric=self._metric)] self._tau = comp_f(score_dict.items(), key=lambda x: x[1])[0] return self._tau
def fit(self, metric): """ Fit regressors and select the best regressor based on the scores with test dataset. Args: metric (str): metric name to select the best regressor Note: All regressors are here. - Indicators -> Parameters with Elastic Net """ # All approaches approach_dict = { _ParamElasticNetRegressor.DESC: self._fit_param_reg(_ParamElasticNetRegressor), _RateElasticNetRegressor.DESC: self._fit_param_reg(_RateElasticNetRegressor), } # Predicted all parameter values must be >= 0 self._reg_dict = { k: v for (k, v) in approach_dict.items() if v.predict().ge(0).all().all() and v.predict().le(1).all().all()} # Select the best regressor with the metric comp_f = {True: min, False: max}[Evaluator.smaller_is_better(metric=metric)] self._best, _ = comp_f(self._reg_dict.items(), key=lambda x: x[1].score_test(metric=metric))