Commit f29ad0db authored by Le Roux Erwan's avatar Le Roux Erwan
Browse files

[EXTREME ESTIMATOR][MARGIN MODEL] add temporal linear margin model

parent d45d0f43
No related merge requests found
Showing with 86 additions and 11 deletions
+86 -11
......@@ -50,10 +50,9 @@ class LinearMarginFunction(ParametricMarginFunction):
if self.starting_point is not None:
# Shift temporal coordinate to enable to model temporal trend with starting point
assert self.coordinates.has_temporal_coordinates
idx_coordinate_t = self.coordinates.coordinates_names.index(self.coordinates.COORDINATE_T)
assert 0 <= idx_coordinate_t < len(coordinate)
if coordinate[idx_coordinate_t] < self.starting_point:
coordinate[idx_coordinate_t] = self.starting_point
assert 0 <= self.coordinates.idx_temporal_coordinates < len(coordinate)
if coordinate[self.coordinates.idx_temporal_coordinates] < self.starting_point:
coordinate[self.coordinates.idx_temporal_coordinates] = self.starting_point
return super().get_gev_params(coordinate)
@classmethod
......
import numpy as np
import pandas as pd
from extreme_estimator.extreme_models.margin_model.linear_margin_model import LinearMarginModel
from extreme_estimator.extreme_models.result_from_fit import ResultFromFit, ResultFromIsmev
from extreme_estimator.extreme_models.utils import r, ro, get_null
from extreme_estimator.extreme_models.utils import safe_run_r_estimator
from extreme_estimator.margin_fits.gev.gev_params import GevParams
from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoordinates
class TemporalLinearMarginModel(LinearMarginModel):
# Linearity only with respect to the temporal coordinates
def __init__(self, coordinates: AbstractCoordinates, use_start_value=False, params_start_fit=None,
params_sample=None, starting_point=None):
super().__init__(coordinates, use_start_value, params_start_fit, params_sample, starting_point)
def fitmargin_from_maxima_gev(self, data: np.ndarray, df_coordinates_spat: pd.DataFrame,
df_coordinates_temp: pd.DataFrame) -> ResultFromFit:
# Modify df_coordinates_temp
df_coordinates_temp = self.add_starting_temporal_point(df_coordinates_temp)
# Gev Fit
res = safe_run_r_estimator(function=r('gev.fit'), use_start=self.use_start_value,
xdat=ro.FloatVector(data[0]), y=df_coordinates_temp.values, mul=self.mul)
return ResultFromIsmev(res, self.margin_function_start_fit.gev_param_name_to_dims)
@property
def mul(self):
return NotImplementedError
class StationaryStationModel(TemporalLinearMarginModel):
def load_margin_functions(self, gev_param_name_to_dims=None):
super().load_margin_functions({})
@property
def mul(self):
return get_null()
class NonStationaryStationModel(TemporalLinearMarginModel):
def load_margin_functions(self, gev_param_name_to_dims=None):
super().load_margin_functions({GevParams.LOC: [self.coordinates.idx_temporal_coordinates]})
@property
def mul(self):
return 1
......@@ -3,6 +3,8 @@ from typing import Dict
from rpy2 import robjects
from extreme_estimator.extreme_models.margin_model.param_function.linear_coef import LinearCoef
from extreme_estimator.margin_fits.gev.gev_params import GevParams
from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoordinates
class ResultFromFit(object):
......@@ -27,17 +29,38 @@ class ResultFromFit(object):
class ResultFromIsmev(ResultFromFit):
pass
def __init__(self, result_from_fit: robjects.ListVector, gev_param_name_to_dim) -> None:
super().__init__(result_from_fit)
self.gev_param_name_to_dim = gev_param_name_to_dim
@property
def mle(self):
return self.res['mle']
def margin_coef_dict(self):
# Build the Coeff dict from gev_param_name_to_dim
coef_dict = {}
i = 0
mle_values = self.name_to_value['mle']
for gev_param_name in GevParams.PARAM_NAMES:
# Add intercept
intercept_coef_name = LinearCoef.coef_template_str(gev_param_name, LinearCoef.INTERCEPT_NAME).format(1)
coef_dict[intercept_coef_name] = mle_values[i]
i += 1
# Add a potential linear temporal trend
if gev_param_name in self.gev_param_name_to_dim:
temporal_coef_name = LinearCoef.coef_template_str(gev_param_name, AbstractCoordinates.COORDINATE_T).format(1)
coef_dict[temporal_coef_name] = mle_values[i]
i += 1
return coef_dict
@property
def all_parameters(self):
return self.margin_coef_dict
@property
def nllh(self):
return self.res['nllh']
class ResultFromSpatialExtreme(ResultFromFit):
"""
Handler from any result with the result of a fit functions from the package Spatial Extreme
......@@ -45,7 +68,6 @@ class ResultFromSpatialExtreme(ResultFromFit):
FITTED_VALUES_NAME = 'fitted.values'
CONVERGENCE_NAME = 'convergence'
@property
def convergence(self) -> str:
convergence_value = self.name_to_value[self.CONVERGENCE_NAME]
......
......@@ -16,8 +16,6 @@ from rpy2.rinterface._rinterface import RRuntimeError
from rpy2.robjects import numpy2ri
from rpy2.robjects import pandas2ri
from extreme_estimator.extreme_models.result_from_fit import ResultFromFit, ResultFromSpatialExtreme
r = ro.R()
numpy2ri.activate()
pandas2ri.activate()
......
......@@ -202,6 +202,10 @@ class AbstractCoordinates(object):
df_temporal_coordinates = self.df_temporal_coordinates(split)
return int(df_temporal_coordinates.min()), int(df_temporal_coordinates.max()),
@property
def idx_temporal_coordinates(self):
return self.coordinates_names.index(self.COORDINATE_T)
# Spatio temporal attributes
@property
......
......@@ -12,7 +12,7 @@ class AbstractSpatioTemporalObservations(object):
OBSERVATIONS_GEV = 'obs_gev'
OBSERVATIONS_FRECH = 'obs_frech'
def __init__(self, df_maxima_frech: pd.DataFrame = None, df_maxima_gev: pd.DataFrame = None):
def __init__(self, df_maxima_gev: pd.DataFrame = None, df_maxima_frech: pd.DataFrame = None):
"""
Main attribute of the class is the DataFrame df_maxima
Index are coordinates index
......
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