Commit 52c73edb authored by Le Roux Erwan's avatar Le Roux Erwan
Browse files

[FULL ESTIMATOR] load/visualize margin_function object fitted

parent e23e220a
No related merge requests found
Showing with 54 additions and 32 deletions
+54 -32
...@@ -10,12 +10,12 @@ import matplotlib.pyplot as plt ...@@ -10,12 +10,12 @@ import matplotlib.pyplot as plt
from spatio_temporal_dataset.dataset.simulation_dataset import FullSimulatedDataset from spatio_temporal_dataset.dataset.simulation_dataset import FullSimulatedDataset
nb_points = 50 nb_points = 5
nb_obs = 100 nb_obs = 10
show = False
coordinates = LinSpaceCoordinates.from_nb_points(nb_points=nb_points) coordinates = LinSpaceCoordinates.from_nb_points(nb_points=nb_points)
########## GENERATING THE DATA ##################### ########## GENERATING THE DATA #####################
# MarginModel Linear with respect to the shape (from 0.01 to 0.02) # MarginModel Linear with respect to the shape (from 0.01 to 0.02)
...@@ -24,19 +24,20 @@ max_stable_model = Smith() ...@@ -24,19 +24,20 @@ max_stable_model = Smith()
dataset = FullSimulatedDataset.from_double_sampling(nb_obs=nb_obs, margin_model=margin_model, dataset = FullSimulatedDataset.from_double_sampling(nb_obs=nb_obs, margin_model=margin_model,
coordinates=coordinates, coordinates=coordinates,
max_stable_model=max_stable_model) max_stable_model=max_stable_model)
# Visualize the sampling margin if show:
dataset.margin_model.margin_function_sample.visualize_all() # Visualize the sampling margin
# Plot a realization from the maxima gev (i.e the maxima obtained just by simulating the marginal law) dataset.margin_model.margin_function_sample.visualize_all()
for maxima_gev in np.transpose(dataset.maxima_gev): # Plot a realization from the maxima gev (i.e the maxima obtained just by simulating the marginal law)
plt.plot(coordinates.coordinates_values, maxima_gev) for maxima_gev in np.transpose(dataset.maxima_gev):
plt.show() plt.plot(coordinates.coordinates_values, maxima_gev)
plt.show()
######### FITTING A MODEL ################# ######### FITTING A MODEL #################
margin_model = LinearAllParametersAllAxisMarginModel(coordinates) margin_model = LinearAllParametersAllAxisMarginModel(coordinates)
full_estimator = FullEstimatorInASingleStepWithSmoothMargin(dataset, margin_model, max_stable_model) full_estimator = FullEstimatorInASingleStepWithSmoothMargin(dataset, margin_model, max_stable_model)
full_estimator.fit() full_estimator.fit()
print(full_estimator.full_params_fitted) full_estimator.margin_function_fitted.visualize_all()
# Plot the margin functions # Plot the margin functions
# margin_model.margin_function_sample.visualize_2D() # margin_model.margin_function_sample.visualize_2D()
from extreme_estimator.extreme_models.margin_model.abstract_margin_model import AbstractMarginModel from extreme_estimator.extreme_models.margin_model.abstract_margin_model import AbstractMarginModel
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 \
AbstractMarginFunction AbstractMarginFunction
from extreme_estimator.extreme_models.margin_model.margin_function.linear_margin_function import LinearMarginFunction
from extreme_estimator.extreme_models.margin_model.smooth_margin_model import LinearMarginModel from extreme_estimator.extreme_models.margin_model.smooth_margin_model import LinearMarginModel
from extreme_estimator.extreme_models.max_stable_model.abstract_max_stable_model import AbstractMaxStableModel from extreme_estimator.extreme_models.max_stable_model.abstract_max_stable_model import AbstractMaxStableModel
from extreme_estimator.estimator.abstract_estimator import AbstractEstimator from extreme_estimator.estimator.abstract_estimator import AbstractEstimator
...@@ -60,7 +61,9 @@ class FullEstimatorInASingleStepWithSmoothMargin(AbstractFullEstimator): ...@@ -60,7 +61,9 @@ class FullEstimatorInASingleStepWithSmoothMargin(AbstractFullEstimator):
max_stable_model: AbstractMaxStableModel): max_stable_model: AbstractMaxStableModel):
super().__init__(dataset) super().__init__(dataset)
self.max_stable_model = max_stable_model self.max_stable_model = max_stable_model
self.smooth_margin_function_to_fit = margin_model.margin_function_start_fit self.linear_margin_model = margin_model
self.linear_margin_function_to_fit = self.linear_margin_model.margin_function_start_fit
assert isinstance(self.linear_margin_function_to_fit, LinearMarginFunction)
def _fit(self): def _fit(self):
# Estimate both the margin and the max-stable structure # Estimate both the margin and the max-stable structure
...@@ -68,11 +71,14 @@ class FullEstimatorInASingleStepWithSmoothMargin(AbstractFullEstimator): ...@@ -68,11 +71,14 @@ class FullEstimatorInASingleStepWithSmoothMargin(AbstractFullEstimator):
maxima_frech=self.dataset.maxima_frech, maxima_frech=self.dataset.maxima_frech,
df_coordinates=self.dataset.df_coordinates, df_coordinates=self.dataset.df_coordinates,
fit_marge=True, fit_marge=True,
fit_marge_form_dict=self.smooth_margin_function_to_fit.form_dict, fit_marge_form_dict=self.linear_margin_function_to_fit.form_dict,
margin_start_dict=self.smooth_margin_function_to_fit.coef_dict margin_start_dict=self.linear_margin_function_to_fit.coef_dict
) )
# Initialize # Create the fitted margin function
# self._margin_function_fitted = coef_dict = {k: v for k, v in self.full_params_fitted.items() if 'Coeff' in k}
self._margin_function_fitted = LinearMarginFunction.from_coef_dict(coordinates=self.dataset.coordinates,
gev_param_name_to_linear_dims=self.linear_margin_function_to_fit.gev_param_name_to_linear_dims,
coef_dict=coef_dict)
class PointwiseAndThenUnitaryMsp(AbstractFullEstimator): class PointwiseAndThenUnitaryMsp(AbstractFullEstimator):
......
from typing import Dict, List, Tuple from typing import Dict, List
from extreme_estimator.extreme_models.margin_model.margin_function.independent_margin_function import \ from extreme_estimator.extreme_models.margin_model.margin_function.independent_margin_function import \
IndependentMarginFunction IndependentMarginFunction
...@@ -29,8 +29,8 @@ class LinearMarginFunction(IndependentMarginFunction): ...@@ -29,8 +29,8 @@ class LinearMarginFunction(IndependentMarginFunction):
gev_param_name_to_linear_dims: Dict[str, List[int]], gev_param_name_to_linear_dims: Dict[str, List[int]],
gev_param_name_to_linear_coef: Dict[str, LinearCoef]): gev_param_name_to_linear_coef: Dict[str, LinearCoef]):
super().__init__(coordinates) super().__init__(coordinates)
self.gev_param_name_to_linear_coef = gev_param_name_to_linear_coef self.gev_param_name_to_linear_coef = gev_param_name_to_linear_coef # type: Dict[str, LinearCoef]
self.gev_param_name_to_linear_dims = gev_param_name_to_linear_dims self.gev_param_name_to_linear_dims = gev_param_name_to_linear_dims # type: Dict[str, List[int]]
# Build gev_parameter_to_param_function dictionary # Build gev_parameter_to_param_function dictionary
self.gev_param_name_to_param_function = {} # type: Dict[str, ParamFunction] self.gev_param_name_to_param_function = {} # type: Dict[str, ParamFunction]
...@@ -54,11 +54,18 @@ class LinearMarginFunction(IndependentMarginFunction): ...@@ -54,11 +54,18 @@ class LinearMarginFunction(IndependentMarginFunction):
self.gev_param_name_to_param_function[gev_param_name] = param_function self.gev_param_name_to_param_function[gev_param_name] = param_function
@classmethod @classmethod
def from_margin_coeff_dict(cls, coordinates, margin_coeff_dict): def from_coef_dict(cls, coordinates: AbstractCoordinates, gev_param_name_to_linear_dims: Dict[str, List[int]],
pass coef_dict: Dict[str, float]):
gev_param_name_to_linear_coef = {}
for gev_param_name in GevParams.GEV_PARAM_NAMES:
linear_dims = gev_param_name_to_linear_dims.get(gev_param_name, [])
linear_coef = LinearCoef.from_coef_dict(coef_dict=coef_dict, gev_param_name=gev_param_name,
linear_dims=linear_dims)
gev_param_name_to_linear_coef[gev_param_name] = linear_coef
return cls(coordinates, gev_param_name_to_linear_dims, gev_param_name_to_linear_coef)
@property @property
def form_dict(self) -> dict: def form_dict(self) -> Dict[str, str]:
form_dict = {} form_dict = {}
for gev_param_name in GevParams.GEV_PARAM_NAMES: for gev_param_name in GevParams.GEV_PARAM_NAMES:
linear_dims = self.gev_param_name_to_linear_dims.get(gev_param_name, []) linear_dims = self.gev_param_name_to_linear_dims.get(gev_param_name, [])
...@@ -66,7 +73,7 @@ class LinearMarginFunction(IndependentMarginFunction): ...@@ -66,7 +73,7 @@ class LinearMarginFunction(IndependentMarginFunction):
return form_dict return form_dict
@property @property
def coef_dict(self) -> dict: def coef_dict(self) -> Dict[str, float]:
coef_dict = {} coef_dict = {}
for gev_param_name in GevParams.GEV_PARAM_NAMES: for gev_param_name in GevParams.GEV_PARAM_NAMES:
linear_dims = self.gev_param_name_to_linear_dims.get(gev_param_name, []) linear_dims = self.gev_param_name_to_linear_dims.get(gev_param_name, [])
......
...@@ -12,7 +12,7 @@ class LinearCoef(object): ...@@ -12,7 +12,7 @@ class LinearCoef(object):
dim = 3 correspond to the coordinate Z dim = 3 correspond to the coordinate Z
""" """
def __init__(self, gev_param_name: str, default_value: float = 0.0, dim_to_coef: Dict[int, float] = None): def __init__(self, gev_param_name: str, dim_to_coef: Dict[int, float] = None, default_value: float = 0.0):
self.gev_param_name = gev_param_name self.gev_param_name = gev_param_name
self.dim_to_coef = dim_to_coef self.dim_to_coef = dim_to_coef
self.default_value = default_value self.default_value = default_value
...@@ -27,21 +27,28 @@ class LinearCoef(object): ...@@ -27,21 +27,28 @@ class LinearCoef(object):
def intercept(self): def intercept(self):
return self.get_coef(dim=0) return self.get_coef(dim=0)
@classmethod @staticmethod
def from_dict(cls, coef_dict: Dict[int, float], gev_param_name: str, default_value: float = 0.0): def coef_template_str(gev_param_name):
pass return gev_param_name + 'Coeff{}'
def coef_dict(self, linear_dims): @classmethod
coef_dict = {} def from_coef_dict(cls, coef_dict: Dict[str, float], gev_param_name: str, linear_dims):
coef_template_str = self.gev_param_name + 'Coeff{}' dims = [0] + linear_dims
dim_to_coef = {}
for j, dim in enumerate(dims, 1):
coef = coef_dict[cls.coef_template_str(gev_param_name).format(j)]
dim_to_coef[dim] = coef
return cls(gev_param_name, dim_to_coef)
def coef_dict(self, linear_dims) -> Dict[str, float]:
# Constant param must be specified for all the parameters # Constant param must be specified for all the parameters
coef_dict[coef_template_str.format(1)] = self.intercept coef_dict = {self.coef_template_str(self.gev_param_name).format(1): self.intercept}
# Specify only the param that belongs to dim_to_coef # Specify only the param that belongs to dim_to_coef
for j, dim in enumerate(linear_dims, 2): for j, dim in enumerate(linear_dims, 2):
coef_dict[coef_template_str.format(j)] = self.dim_to_coef[dim] coef_dict[self.coef_template_str(self.gev_param_name).format(j)] = self.dim_to_coef[dim]
return coef_dict return coef_dict
def form_dict(self, linear_dims): def form_dict(self, linear_dims) -> Dict[str, str]:
""" """
Example of formula that could be specified: Example of formula that could be specified:
loc.form = loc ~ coord_x loc.form = loc ~ coord_x
......
...@@ -32,6 +32,7 @@ class AbstractMaxStableModel(AbstractModel): ...@@ -32,6 +32,7 @@ class AbstractMaxStableModel(AbstractModel):
data = np.transpose(maxima_frech) data = np.transpose(maxima_frech)
# Prepare the coord # Prepare the coord
df_coordinates = df_coordinates.copy()
# In the one dimensional case, fitmaxstab isn't working # In the one dimensional case, fitmaxstab isn't working
# therefore, we treat our 1D coordinate as 2D coordinate on the line y=x, and enforce iso=TRUE # therefore, we treat our 1D coordinate as 2D coordinate on the line y=x, and enforce iso=TRUE
fitmaxstab_with_one_dimensional_data = len(df_coordinates.columns) == 1 fitmaxstab_with_one_dimensional_data = len(df_coordinates.columns) == 1
......
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