Commit 5ea6a803 authored by Le Roux Erwan's avatar Le Roux Erwan
Browse files

[quantile regression project] improve display for the quantile regression simulation framework

parent 927b62f6
No related merge requests found
Showing with 50 additions and 16 deletions
+50 -16
...@@ -32,6 +32,7 @@ class AbstractSimulation(object): ...@@ -32,6 +32,7 @@ class AbstractSimulation(object):
self.quantile = quantile self.quantile = quantile
self.time_series_lengths = time_series_lengths self.time_series_lengths = time_series_lengths
self.nb_time_series = nb_time_series self.nb_time_series = nb_time_series
self.uncertainty_interval_size = 0.5
def generate_all_observation(self, nb_time_series, length) -> List[AbstractSpatioTemporalObservations]: def generate_all_observation(self, nb_time_series, length) -> List[AbstractSpatioTemporalObservations]:
raise NotImplementedError raise NotImplementedError
...@@ -83,7 +84,8 @@ class AbstractSimulation(object): ...@@ -83,7 +84,8 @@ class AbstractSimulation(object):
length_to_error_values = OrderedDict() length_to_error_values = OrderedDict()
for length, estimators_fitted in d_sub.items(): for length, estimators_fitted in d_sub.items():
errors = self.compute_errors(length, estimators_fitted) 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 length_to_error_values[length] = error_values
d[model_class] = length_to_error_values d[model_class] = length_to_error_values
return d return d
...@@ -92,15 +94,28 @@ class AbstractSimulation(object): ...@@ -92,15 +94,28 @@ class AbstractSimulation(object):
raise NotImplementedError raise NotImplementedError
def plot_error_for_last_year_quantile(self, show=True): def plot_error_for_last_year_quantile(self, show=True):
# Display properties
alpha = 0.1
colors = ['green', 'orange', 'blue', 'red']
ax = plt.gca() 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()) lengths = list(length_to_error_values.keys())
errors_values = np.array(list(length_to_error_values.values())) errors_values = np.array(list(length_to_error_values.values()))
mean_error = errors_values[:, 1] mean_error = errors_values[:, 1]
label = get_display_name_from_object_type(model_class) label = get_display_name_from_object_type(model_class)
ax.plot(lengths, mean_error, label=label) ax.plot(lengths, mean_error, label=label)
ax.set_xlabel('# Data') 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() ax.legend()
if show: if show:
plt.show() plt.show()
...@@ -59,7 +59,7 @@ class StationarySimulation(GevSimulation): ...@@ -59,7 +59,7 @@ class StationarySimulation(GevSimulation):
fit_method=TemporalMarginFitMethod.extremes_fevd_mle) fit_method=TemporalMarginFitMethod.extremes_fevd_mle)
class NonStationaryLocationSimulation(GevSimulation): class NonStationaryLocationGumbelSimulation(GevSimulation):
def create_model(self, coordinates): def create_model(self, coordinates):
gev_param_name_to_coef_list = { gev_param_name_to_coef_list = {
...@@ -69,3 +69,15 @@ class NonStationaryLocationSimulation(GevSimulation): ...@@ -69,3 +69,15 @@ class NonStationaryLocationSimulation(GevSimulation):
} }
return NonStationaryLocationTemporalModel.from_coef_list(coordinates, gev_param_name_to_coef_list, return NonStationaryLocationTemporalModel.from_coef_list(coordinates, gev_param_name_to_coef_list,
fit_method=TemporalMarginFitMethod.extremes_fevd_mle) 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)
from extreme_fit.model.margin_model.linear_margin_model.temporal_linear_margin_models import StationaryTemporalModel, \ 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, \ from extreme_fit.model.quantile_model.quantile_regression_model import ConstantQuantileRegressionModel, \
TemporalCoordinatesQuantileRegressionModel 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 nb_time_series = 10
quantile = 0.9 quantile = 0.98
time_series_lengths = [50, 100, 200] 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, simulation = NonStationaryLocationGumbelSimulation(nb_time_series=nb_time_series,
# model_classes=[StationaryTemporalModel, ConstantQuantileRegressionModel]) quantile=quantile,
# simulation.plot_error_for_last_year_quantile() time_series_lengths=time_series_lengths,
model_classes=model_classes,
simulation = NonStationaryLocationSimulation(nb_time_series=nb_time_series, quantile=quantile, time_series_lengths=time_series_lengths, transformation_class=transformation_class)
model_classes=[NonStationaryLocationTemporalModel, TemporalCoordinatesQuantileRegressionModel][:])
simulation.plot_error_for_last_year_quantile() simulation.plot_error_for_last_year_quantile()
...@@ -5,7 +5,7 @@ from extreme_fit.model.margin_model.linear_margin_model.temporal_linear_margin_m ...@@ -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, \ from extreme_fit.model.quantile_model.quantile_regression_model import ConstantQuantileRegressionModel, \
TemporalCoordinatesQuantileRegressionModel TemporalCoordinatesQuantileRegressionModel
from projects.quantile_regression_vs_evt.GevSimulation import GevSimulation, StationarySimulation, \ from projects.quantile_regression_vs_evt.GevSimulation import GevSimulation, StationarySimulation, \
NonStationaryLocationSimulation NonStationaryLocationGumbelSimulation
class TestGevSimulations(unittest.TestCase): class TestGevSimulations(unittest.TestCase):
...@@ -17,8 +17,8 @@ class TestGevSimulations(unittest.TestCase): ...@@ -17,8 +17,8 @@ class TestGevSimulations(unittest.TestCase):
simulation.plot_error_for_last_year_quantile(self.DISPLAY) simulation.plot_error_for_last_year_quantile(self.DISPLAY)
def test_non_stationary_run(self): def test_non_stationary_run(self):
simulation = NonStationaryLocationSimulation(nb_time_series=1, quantile=0.5, time_series_lengths=[50, 60], simulation = NonStationaryLocationGumbelSimulation(nb_time_series=1, quantile=0.5, time_series_lengths=[50, 60],
model_classes=[NonStationaryLocationTemporalModel, model_classes=[NonStationaryLocationTemporalModel,
TemporalCoordinatesQuantileRegressionModel]) TemporalCoordinatesQuantileRegressionModel])
simulation.plot_error_for_last_year_quantile(self.DISPLAY) simulation.plot_error_for_last_year_quantile(self.DISPLAY)
......
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