From 5ea6a8031772e799070fe1504521e4b7cc16b7af Mon Sep 17 00:00:00 2001 From: Le Roux Erwan <erwan.le-roux@irstea.fr> Date: Fri, 20 Mar 2020 19:04:02 +0100 Subject: [PATCH] [quantile regression project] improve display for the quantile regression simulation framework --- .../AbstractSimulation.py | 21 +++++++++++++--- .../GevSimulation.py | 14 ++++++++++- .../main_quantile_regression.py | 25 ++++++++++++------- .../test_gev_simulations.py | 6 ++--- 4 files changed, 50 insertions(+), 16 deletions(-) diff --git a/projects/quantile_regression_vs_evt/AbstractSimulation.py b/projects/quantile_regression_vs_evt/AbstractSimulation.py index bb35b518..f17d7b1c 100644 --- a/projects/quantile_regression_vs_evt/AbstractSimulation.py +++ b/projects/quantile_regression_vs_evt/AbstractSimulation.py @@ -32,6 +32,7 @@ class AbstractSimulation(object): self.quantile = quantile self.time_series_lengths = time_series_lengths self.nb_time_series = nb_time_series + self.uncertainty_interval_size = 0.5 def generate_all_observation(self, nb_time_series, length) -> List[AbstractSpatioTemporalObservations]: raise NotImplementedError @@ -83,7 +84,8 @@ class AbstractSimulation(object): length_to_error_values = OrderedDict() for length, estimators_fitted in d_sub.items(): errors = self.compute_errors(length, estimators_fitted) - error_values = [np.quantile(errors, q=0.025), np.mean(errors), np.quantile(errors, q=0.975)] + leftover = (1 - self.uncertainty_interval_size) / 2 + error_values = [np.quantile(errors, q=leftover), np.mean(errors), np.quantile(errors, q=1-leftover)] length_to_error_values[length] = error_values d[model_class] = length_to_error_values return d @@ -92,15 +94,28 @@ class AbstractSimulation(object): raise NotImplementedError def plot_error_for_last_year_quantile(self, show=True): + # Display properties + alpha = 0.1 + colors = ['green', 'orange', 'blue', 'red'] ax = plt.gca() - for model_class, length_to_error_values in self.model_class_to_error_last_year_quantile.items(): + for color, (model_class, length_to_error_values) in zip(colors, self.model_class_to_error_last_year_quantile.items()): lengths = list(length_to_error_values.keys()) errors_values = np.array(list(length_to_error_values.values())) mean_error = errors_values[:, 1] label = get_display_name_from_object_type(model_class) ax.plot(lengths, mean_error, label=label) ax.set_xlabel('# Data') - ax.set_ylabel('Relative error for the {} quantile at the last coordinate'.format(self.quantile)) + ax.set_ylabel('Average (out of {} samples) relative error\nfor the {} quantile at the last coordinate (%)'.format(self.nb_time_series, + self.quantile)) + + lower_bound = errors_values[:, 0] + upper_bound = errors_values[:, 2] + # confidence_interval_str = '95 \% confidence interval' + ax.fill_between(lengths, lower_bound, upper_bound, color=color, alpha=alpha) + title = "{} + {}".format(get_display_name_from_object_type(type(self)), + get_display_name_from_object_type(self.transformation_class)) + ax.set_title(title) + ax.legend() if show: plt.show() diff --git a/projects/quantile_regression_vs_evt/GevSimulation.py b/projects/quantile_regression_vs_evt/GevSimulation.py index a1dc5697..b14b36e7 100644 --- a/projects/quantile_regression_vs_evt/GevSimulation.py +++ b/projects/quantile_regression_vs_evt/GevSimulation.py @@ -59,7 +59,7 @@ class StationarySimulation(GevSimulation): fit_method=TemporalMarginFitMethod.extremes_fevd_mle) -class NonStationaryLocationSimulation(GevSimulation): +class NonStationaryLocationGumbelSimulation(GevSimulation): def create_model(self, coordinates): gev_param_name_to_coef_list = { @@ -69,3 +69,15 @@ class NonStationaryLocationSimulation(GevSimulation): } return NonStationaryLocationTemporalModel.from_coef_list(coordinates, gev_param_name_to_coef_list, fit_method=TemporalMarginFitMethod.extremes_fevd_mle) + + +class NonStationaryLocationGevSimulation(GevSimulation): + + def create_model(self, coordinates): + gev_param_name_to_coef_list = { + GevParams.LOC: [0, 1], + GevParams.SHAPE: [0.3], + GevParams.SCALE: [1], + } + return NonStationaryLocationTemporalModel.from_coef_list(coordinates, gev_param_name_to_coef_list, + fit_method=TemporalMarginFitMethod.extremes_fevd_mle) diff --git a/projects/quantile_regression_vs_evt/main_quantile_regression.py b/projects/quantile_regression_vs_evt/main_quantile_regression.py index c13d6da7..6618762a 100644 --- a/projects/quantile_regression_vs_evt/main_quantile_regression.py +++ b/projects/quantile_regression_vs_evt/main_quantile_regression.py @@ -1,17 +1,24 @@ from extreme_fit.model.margin_model.linear_margin_model.temporal_linear_margin_models import StationaryTemporalModel, \ - NonStationaryLocationTemporalModel + NonStationaryLocationTemporalModel, NonStationaryLocationGumbelModel from extreme_fit.model.quantile_model.quantile_regression_model import ConstantQuantileRegressionModel, \ TemporalCoordinatesQuantileRegressionModel -from projects.quantile_regression_vs_evt.GevSimulation import StationarySimulation, NonStationaryLocationSimulation +from projects.quantile_regression_vs_evt.GevSimulation import StationarySimulation, \ + NonStationaryLocationGumbelSimulation, NonStationaryLocationGevSimulation +from spatio_temporal_dataset.coordinates.transformed_coordinates.transformation.abstract_transformation import \ + CenteredScaledNormalization, IdentityTransformation +from spatio_temporal_dataset.coordinates.transformed_coordinates.transformation.uniform_normalization import \ + BetweenZeroAndOneNormalization nb_time_series = 10 -quantile = 0.9 +quantile = 0.98 time_series_lengths = [50, 100, 200] +transformation_class = [IdentityTransformation, CenteredScaledNormalization][0] +model_classes = [NonStationaryLocationTemporalModel, TemporalCoordinatesQuantileRegressionModel, NonStationaryLocationGumbelModel] +simulation_class = [NonStationaryLocationGumbelSimulation, NonStationaryLocationGevSimulation][0] -# simulation = StationarySimulation(nb_time_series=nb_time_series, quantile=quantile, time_series_lengths=time_series_lengths, -# model_classes=[StationaryTemporalModel, ConstantQuantileRegressionModel]) -# simulation.plot_error_for_last_year_quantile() - -simulation = NonStationaryLocationSimulation(nb_time_series=nb_time_series, quantile=quantile, time_series_lengths=time_series_lengths, - model_classes=[NonStationaryLocationTemporalModel, TemporalCoordinatesQuantileRegressionModel][:]) +simulation = NonStationaryLocationGumbelSimulation(nb_time_series=nb_time_series, + quantile=quantile, + time_series_lengths=time_series_lengths, + model_classes=model_classes, + transformation_class=transformation_class) simulation.plot_error_for_last_year_quantile() diff --git a/test/test_projects/test_quantile_regression/test_gev_simulations.py b/test/test_projects/test_quantile_regression/test_gev_simulations.py index c2657ac6..b18321f3 100644 --- a/test/test_projects/test_quantile_regression/test_gev_simulations.py +++ b/test/test_projects/test_quantile_regression/test_gev_simulations.py @@ -5,7 +5,7 @@ from extreme_fit.model.margin_model.linear_margin_model.temporal_linear_margin_m from extreme_fit.model.quantile_model.quantile_regression_model import ConstantQuantileRegressionModel, \ TemporalCoordinatesQuantileRegressionModel from projects.quantile_regression_vs_evt.GevSimulation import GevSimulation, StationarySimulation, \ - NonStationaryLocationSimulation + NonStationaryLocationGumbelSimulation class TestGevSimulations(unittest.TestCase): @@ -17,8 +17,8 @@ class TestGevSimulations(unittest.TestCase): simulation.plot_error_for_last_year_quantile(self.DISPLAY) def test_non_stationary_run(self): - simulation = NonStationaryLocationSimulation(nb_time_series=1, quantile=0.5, time_series_lengths=[50, 60], - model_classes=[NonStationaryLocationTemporalModel, + simulation = NonStationaryLocationGumbelSimulation(nb_time_series=1, quantile=0.5, time_series_lengths=[50, 60], + model_classes=[NonStationaryLocationTemporalModel, TemporalCoordinatesQuantileRegressionModel]) simulation.plot_error_for_last_year_quantile(self.DISPLAY) -- GitLab