Commit 3beddf92 authored by Le Roux Erwan's avatar Le Roux Erwan
Browse files

[EXTREME ESTIMATOR] refactor code

parent d47c86bb
No related merge requests found
Showing with 16 additions and 17 deletions
+16 -17
......@@ -64,15 +64,7 @@ class AbstractEstimator(object):
def train_split(self):
return self.dataset.train_split
def scalars(self, true_max_stable_params: dict):
error = self._error(true_max_stable_params)
return {**error, **self.additional_information}
# Methods to override in the child class
def _fit(self):
raise NotImplementedError
def _error(self, true_max_stable_params: dict):
raise NotImplementedError
......@@ -28,6 +28,9 @@ class PointWiseMarginEstimator(AbstractMarginEstimator):
class SmoothMarginEstimator(AbstractMarginEstimator):
"""# with different type of marginals: cosntant, linear...."""
def _error(self, true_max_stable_params: dict):
pass
def __init__(self, dataset: AbstractDataset, margin_model: LinearMarginModel):
super().__init__(dataset)
assert isinstance(margin_model, LinearMarginModel)
......@@ -35,10 +38,10 @@ class SmoothMarginEstimator(AbstractMarginEstimator):
def _fit(self):
maxima_gev = self.dataset.maxima_gev(split=self.train_split)
df_coordinates_spatial = self.dataset.coordinates.df_spatial_coordinates(self.train_split)
df_coordinates_temporal = self.dataset.coordinates.df_temporal_coordinates(self.train_split)
df_coordinates_spat = self.dataset.coordinates.df_spatial_coordinates(self.train_split)
df_coordinates_temp = self.dataset.coordinates.df_temporal_coordinates(self.train_split)
self._result_from_fit = self.margin_model.fitmargin_from_maxima_gev(maxima_gev=maxima_gev,
df_coordinates_spatial=df_coordinates_spatial,
df_coordinates_temporal=df_coordinates_temporal)
df_coordinates_spat=df_coordinates_spat,
df_coordinates_temp=df_coordinates_temp)
self.extract_fitted_models_from_fitted_params(self.margin_model.margin_function_start_fit, self.fitted_values)
assert isinstance(self.margin_function_fitted, AbstractMarginFunction)
......@@ -23,6 +23,10 @@ class MaxStableEstimator(AbstractMaxStableEstimator):
df_coordinates=self.dataset.df_coordinates(split=self.train_split))
self.max_stable_params_fitted = self.fitted_values
def scalars(self, true_max_stable_params: dict):
error = self._error(true_max_stable_params)
return {**error, **self.additional_information}
def _error(self, true_max_stable_params: dict):
absolute_errors = {param_name: np.abs(param_true_value - self.max_stable_params_fitted[param_name])
for param_name, param_true_value in true_max_stable_params.items()}
......
......@@ -57,18 +57,18 @@ class LinearMarginModel(AbstractMarginModel):
params[(gev_param_name, dim)] = coef
return cls(coordinates, params_sample=params, params_start_fit=params)
def fitmargin_from_maxima_gev(self, maxima_gev: np.ndarray, df_coordinates_spatial: pd.DataFrame,
df_coordinates_temporal: pd.DataFrame) -> ResultFromFit:
def fitmargin_from_maxima_gev(self, maxima_gev: np.ndarray, df_coordinates_spat: pd.DataFrame,
df_coordinates_temp: pd.DataFrame) -> ResultFromFit:
# The reshaping on the line below is only valid if we have a single observation per spatio-temporal point
if maxima_gev.shape[1] == 1:
maxima_gev = maxima_gev.reshape([len(df_coordinates_temporal), len(df_coordinates_spatial)])
maxima_gev = maxima_gev.reshape([len(df_coordinates_temp), len(df_coordinates_spat)])
data = np.transpose(maxima_gev)
fit_params = get_margin_formula(self.margin_function_start_fit.form_dict)
# Covariables
covariables = get_coord(df_coordinates=df_coordinates_spatial)
fit_params['temp.cov'] = get_coord(df_coordinates=df_coordinates_temporal)
covariables = get_coord(df_coordinates=df_coordinates_spat)
fit_params['temp.cov'] = get_coord(df_coordinates=df_coordinates_temp)
# Start parameters
coef_dict = self.margin_function_start_fit.coef_dict
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment