def _score_tau(self, tau, data, quantile): """ Calculate score for the tau value. Args: tau (int): tau value [min] 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 quantile (float): quantile to guess ODE parameter values for the candidates of tau """ info_dict = self._info_dict.copy() for (phase, phase_dict) in info_dict.items(): start, end = phase_dict[self.START], phase_dict[self.END] df = data.loc[(start <= data[self.DATE]) & (data[self.DATE] <= end)] info_dict[phase]["param"] = self._model.guess(df, tau, q=quantile) solver = _MultiPhaseODESolver(self._model, self._first, tau) sim_df = solver.simulate(*info_dict.values()) evaluator = Evaluator(data.set_index(self.DATE), sim_df.set_index(self.DATE)) return evaluator.score(metric=self._metric)
def _score(self, tau, param_dict): """ Calculate score. Args: tau (int): tau value [min] param_dict (dict[str, int or float]): dictionary of parameter values Returns: float: score """ self.tau = tau self._set_taufree() cols = [self.TS, *self.variables_evaluate] rec_df = self.taufree_df.loc[:, cols] sim_df = self._simulate(self.step_n, param_dict).loc[:, cols] evaluator = Evaluator(rec_df, sim_df, on=self.TS) return evaluator.score(metric=self._metric)
def _score(self, **kwargs): """ Objective function to minimize. Score will be calculated the data and metric. Args: kwargs: values of non-dimensional model parameters, including rho and sigma Returns: float: score """ # Simulate with applied parameter values solver = _ODESolver(model=self._model, **kwargs) sim_df = solver.run(step_n=self._step_n, **self._y0_dict) # The first variable (Susceptible) will be ignored in score calculation taufree_df = self._taufree_df.loc[:, self._taufree_df.columns[1:]] sim_df = sim_df.loc[:, sim_df.columns[1:]] # Calculate score evaluator = Evaluator(taufree_df, sim_df, how="inner", on=None) return evaluator.score(metric=self._metric)
def score(self, variables=None, phases=None, y0_dict=None, **kwargs): """ Evaluate accuracy of phase setting and parameter estimation of selected enabled phases. Args: variables (list[str] or None): variables to use in calculation phases (list[str] or None): phases to use in calculation y0_dict(dict[str, float] or None): dictionary of initial values of variables kwargs: keyword arguments of covsirphy.Evaluator.score() Returns: float: score with the specified metrics Note: If @variables is None, ["Infected", "Fatal", "Recovered"] will be used. "Confirmed", "Infected", "Fatal" and "Recovered" can be used in @variables. If @phases is None, all phases will be used. """ # Arguments variables = variables or [self.CI, self.F, self.R] variables = self._ensure_list(variables, self.VALUE_COLUMNS, name="variables") # Disable the non-target phases all_phases, _ = self.past_phases(phases=None) target_phases, _ = self.past_phases(phases=phases) ignored_phases = list(set(all_phases) - set(target_phases)) if ignored_phases: self.disable(ignored_phases) # Get the number of cases rec_df, sim_df = self._compare_with_actual(variables=variables, y0_dict=y0_dict) # Calculate score evaluator = Evaluator(rec_df, sim_df) score = evaluator.score(**find_args(Evaluator.score, **kwargs)) # Enable the disabled non-target phases if ignored_phases: self.enable(ignored_phases) return score
def score(self, change_points, metric): """ Calculate scores of the phases. Args: change_points (list[pandas.Timestamp]): list of change points metric (str): metric name Returns: list[float]: scores for phases Note: Please refer to covsirphy.Evaluator.score() for metric names """ fit_df = self._fitting(change_points) phases = [self.num2str(num) for num in range(len(change_points) + 1)] scores = [] for phase in phases: df = fit_df[[self.ACTUAL, phase]].dropna() evaluator = Evaluator(df[self.ACTUAL], df[phase], how="all") scores.append(evaluator.score(metric=metric)) return scores