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

[SIMULATION] add constant estimators. handle graph plot of constant margin...

[SIMULATION] add constant estimators. handle graph plot of constant margin estimator (when the sampling margin was also constant)
parent 2a846717
No related merge requests found
Showing with 40 additions and 13 deletions
+40 -13
...@@ -13,6 +13,7 @@ import matplotlib.cm as cm ...@@ -13,6 +13,7 @@ import matplotlib.cm as cm
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import numpy as np import numpy as np
import seaborn as sns import seaborn as sns
from numpy.linalg import LinAlgError
from extreme_estimator.estimator.abstract_estimator import AbstractEstimator from extreme_estimator.estimator.abstract_estimator import AbstractEstimator
from extreme_estimator.extreme_models.margin_model.margin_function.abstract_margin_function import \ from extreme_estimator.extreme_models.margin_model.margin_function.abstract_margin_function import \
...@@ -193,7 +194,11 @@ class AbstractSimulation(object): ...@@ -193,7 +194,11 @@ class AbstractSimulation(object):
else: else:
markersize = 10 markersize = 10
ax.plot([data.mean()], [0], color=self.color, marker='o', markersize=markersize) ax.plot([data.mean()], [0], color=self.color, marker='o', markersize=markersize)
sns.kdeplot(data, bw=1, ax=ax, color=self.color, **display_kwargs).set(xlim=0) try:
sns.kdeplot(data, bw=1, ax=ax, color=self.color, **display_kwargs).set(xlim=0)
except LinAlgError as e:
if 'singular_matrix' in e.__repr__():
continue
ax.legend() ax.legend()
# X axis # X axis
...@@ -242,6 +247,7 @@ class AbstractSimulation(object): ...@@ -242,6 +247,7 @@ class AbstractSimulation(object):
# Pickle Dataset # Pickle Dataset
if op.exists(self.dataset_pickle_path): if op.exists(self.dataset_pickle_path):
print('A dataset already exists, we will keep it intact, delete it manually if you want to change it') print('A dataset already exists, we will keep it intact, delete it manually if you want to change it')
# todo: print the parameters of the existing data, the parameters that were used to generate it
else: else:
with open(self.dataset_pickle_path, 'wb') as fp: with open(self.dataset_pickle_path, 'wb') as fp:
pickle.dump(dataset, fp) pickle.dump(dataset, fp)
......
...@@ -9,7 +9,7 @@ from spatio_temporal_dataset.coordinates.spatial_coordinates.coordinates_1D impo ...@@ -9,7 +9,7 @@ from spatio_temporal_dataset.coordinates.spatial_coordinates.coordinates_1D impo
from spatio_temporal_dataset.dataset.simulation_dataset import FullSimulatedDataset from spatio_temporal_dataset.dataset.simulation_dataset import FullSimulatedDataset
class LinSpace3Simulation(AbstractSimulation): class LinSpace5Simulation(AbstractSimulation):
FITTED_ESTIMATORS = [] FITTED_ESTIMATORS = []
def __init__(self, nb_fit=1): def __init__(self, nb_fit=1):
...@@ -34,8 +34,9 @@ class LinSpace3Simulation(AbstractSimulation): ...@@ -34,8 +34,9 @@ class LinSpace3Simulation(AbstractSimulation):
if __name__ == '__main__': if __name__ == '__main__':
simu = LinSpace3Simulation(nb_fit=7) simu = LinSpace5Simulation(nb_fit=10)
simu.dump() simu.dump()
for estimator_class in MARGIN_ESTIMATORS_FOR_SIMULATION + FULL_ESTIMATORS_FOR_SIMULATION: estimators_class = MARGIN_ESTIMATORS_FOR_SIMULATION + FULL_ESTIMATORS_FOR_SIMULATION
for estimator_class in estimators_class[:]:
simu.fit(estimator_class, show=False) simu.fit(estimator_class, show=False)
simu.visualize_comparison_graph() simu.visualize_comparison_graph()
from extreme_estimator.estimator.full_estimator.abstract_full_estimator import \ from extreme_estimator.estimator.full_estimator.abstract_full_estimator import \
FullEstimatorInASingleStepWithSmoothMargin FullEstimatorInASingleStepWithSmoothMargin
from extreme_estimator.extreme_models.margin_model.smooth_margin_model import LinearAllParametersAllDimsMarginModel from extreme_estimator.extreme_models.margin_model.smooth_margin_model import LinearAllParametersAllDimsMarginModel, \
ConstantMarginModel
from extreme_estimator.extreme_models.max_stable_model.max_stable_models import Smith from extreme_estimator.extreme_models.max_stable_model.max_stable_models import Smith
from spatio_temporal_dataset.dataset.abstract_dataset import AbstractDataset from spatio_temporal_dataset.dataset.abstract_dataset import AbstractDataset
...@@ -14,6 +15,17 @@ class FullEstimatorInASingleStepWithSmoothMargin_LinearAllParametersAllDims_Smit ...@@ -14,6 +15,17 @@ class FullEstimatorInASingleStepWithSmoothMargin_LinearAllParametersAllDims_Smit
max_stable_model=Smith()) max_stable_model=Smith())
class FullEstimatorInASingleStepWithSmoothMargin_Constant_Smith(
FullEstimatorInASingleStepWithSmoothMargin):
@classmethod
def from_dataset(cls, dataset: AbstractDataset):
return cls(dataset, margin_model=ConstantMarginModel(dataset.coordinates),
max_stable_model=Smith())
FULL_ESTIMATORS_FOR_SIMULATION = [ FULL_ESTIMATORS_FOR_SIMULATION = [
FullEstimatorInASingleStepWithSmoothMargin_LinearAllParametersAllDims_Smith FullEstimatorInASingleStepWithSmoothMargin_LinearAllParametersAllDims_Smith,
FullEstimatorInASingleStepWithSmoothMargin_Constant_Smith,
] ]
from extreme_estimator.estimator.margin_estimator.abstract_margin_estimator import SmoothMarginEstimator from extreme_estimator.estimator.margin_estimator.abstract_margin_estimator import SmoothMarginEstimator
from extreme_estimator.extreme_models.margin_model.smooth_margin_model import LinearAllParametersAllDimsMarginModel from extreme_estimator.extreme_models.margin_model.smooth_margin_model import LinearAllParametersAllDimsMarginModel, \
ConstantMarginModel
from spatio_temporal_dataset.dataset.abstract_dataset import AbstractDataset from spatio_temporal_dataset.dataset.abstract_dataset import AbstractDataset
...@@ -10,6 +11,14 @@ class SmoothMarginEstimator_LinearAllParametersAllDims(SmoothMarginEstimator): ...@@ -10,6 +11,14 @@ class SmoothMarginEstimator_LinearAllParametersAllDims(SmoothMarginEstimator):
return cls(dataset, LinearAllParametersAllDimsMarginModel(dataset.coordinates)) return cls(dataset, LinearAllParametersAllDimsMarginModel(dataset.coordinates))
class SmoothMarginEstimator_Constant(SmoothMarginEstimator):
@classmethod
def from_dataset(cls, dataset: AbstractDataset):
return cls(dataset, ConstantMarginModel(dataset.coordinates))
MARGIN_ESTIMATORS_FOR_SIMULATION = [ MARGIN_ESTIMATORS_FOR_SIMULATION = [
SmoothMarginEstimator_LinearAllParametersAllDims SmoothMarginEstimator_LinearAllParametersAllDims,
SmoothMarginEstimator_Constant,
] ]
...@@ -88,7 +88,6 @@ class AbstractMarginFunction(object): ...@@ -88,7 +88,6 @@ class AbstractMarginFunction(object):
if self.datapoint_display: if self.datapoint_display:
ax.plot(linspace, grid[gev_value_name], marker=self.datapoint_marker, color=self.color) ax.plot(linspace, grid[gev_value_name], marker=self.datapoint_marker, color=self.color)
else: else:
print('here')
ax.plot(linspace, grid[gev_value_name], color=self.color, linewidth=self.linewidth) ax.plot(linspace, grid[gev_value_name], color=self.color, linewidth=self.linewidth)
# X axis # X axis
ax.set_xlabel('coordinate X') ax.set_xlabel('coordinate X')
...@@ -109,7 +108,6 @@ class AbstractMarginFunction(object): ...@@ -109,7 +108,6 @@ class AbstractMarginFunction(object):
if self.datapoint_display: if self.datapoint_display:
# todo: keep only the index of interest here # todo: keep only the index of interest here
linspace = self.coordinates.coordinates_values(spatio_temporal_split)[:, 0] linspace = self.coordinates.coordinates_values(spatio_temporal_split)[:, 0]
print(self.spatio_temporal_split, linspace)
if self.filter is not None: if self.filter is not None:
linspace = linspace[self.filter] linspace = linspace[self.filter]
resolution = len(linspace) resolution = len(linspace)
......
...@@ -6,6 +6,7 @@ from extreme_estimator.gev_params import GevParams ...@@ -6,6 +6,7 @@ from extreme_estimator.gev_params import GevParams
def relative_abs_error(reference_value, fitted_value): def relative_abs_error(reference_value, fitted_value):
# todo: handle the case when the location, or the shape we aim to estimate might be equal to zero
return (reference_value - fitted_value).abs() / reference_value return (reference_value - fitted_value).abs() / reference_value
......
...@@ -96,7 +96,7 @@ class AbstractCoordinates(object): ...@@ -96,7 +96,7 @@ class AbstractCoordinates(object):
return cls.from_df(df) return cls.from_df(df)
@property @property
def index(self): def index(self) -> pd.Index:
return self.df_all_coordinates.index return self.df_all_coordinates.index
@property @property
...@@ -124,7 +124,7 @@ class AbstractCoordinates(object): ...@@ -124,7 +124,7 @@ class AbstractCoordinates(object):
return ind_train_from_s_split(s_split=self.s_split_temporal) return ind_train_from_s_split(s_split=self.s_split_temporal)
@property @property
def df_split(self): def df_split(self) -> pd.DataFrame:
split_name_to_s_split = { split_name_to_s_split = {
self.SPATIAL_SPLIT: self.s_split_spatial, self.SPATIAL_SPLIT: self.s_split_spatial,
self.TEMPORAL_SPLIT: self.s_split_temporal, self.TEMPORAL_SPLIT: self.s_split_temporal,
...@@ -173,7 +173,7 @@ class AbstractCoordinates(object): ...@@ -173,7 +173,7 @@ class AbstractCoordinates(object):
return self.df_all_coordinates[self.COORDINATE_Z].values.copy() return self.df_all_coordinates[self.COORDINATE_Z].values.copy()
@property @property
def t_coordinates(self): def t_coordinates(self) -> np.ndarray:
return self.df_all_coordinates[self.COORDINATE_T].values.copy() return self.df_all_coordinates[self.COORDINATE_T].values.copy()
def visualize(self): def visualize(self):
......
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