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

[EXTREME ESTIMATOR] add margin_fits folder. add gpd params & gpdmle_fit....

[EXTREME ESTIMATOR] add margin_fits folder. add gpd params & gpdmle_fit. refactor code to handle gev and gpd together
parent cdbdb269
No related merge requests found
Showing with 165 additions and 179 deletions
+165 -179
...@@ -5,7 +5,7 @@ from extreme_estimator.extreme_models.margin_model.margin_function.linear_margin ...@@ -5,7 +5,7 @@ from extreme_estimator.extreme_models.margin_model.margin_function.linear_margin
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 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 extreme_estimator.gev_params import GevParams from extreme_estimator.margin_fits.gev.gev_params import GevParams
from spatio_temporal_dataset.coordinates.spatial_coordinates.coordinates_1D import LinSpaceSpatialCoordinates from spatio_temporal_dataset.coordinates.spatial_coordinates.coordinates_1D import LinSpaceSpatialCoordinates
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
...@@ -23,9 +23,9 @@ coordinates = LinSpaceSpatialCoordinates.from_nb_points(nb_points=nb_points) ...@@ -23,9 +23,9 @@ coordinates = LinSpaceSpatialCoordinates.from_nb_points(nb_points=nb_points)
# 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)
params_sample = { params_sample = {
# (GevParams.GEV_SHAPE, 0): 0.2, # (GevParams.GEV_SHAPE, 0): 0.2,
(GevParams.GEV_LOC, 0): 10, (GevParams.LOC, 0): 10,
(GevParams.GEV_SHAPE, 0): 1.0, (GevParams.SHAPE, 0): 1.0,
(GevParams.GEV_SCALE, 0): 1.0, (GevParams.SCALE, 0): 1.0,
} }
margin_model = ConstantMarginModel(coordinates=coordinates, params_sample=params_sample) margin_model = ConstantMarginModel(coordinates=coordinates, params_sample=params_sample)
margin_model_for_estimator_class = [LinearAllParametersAllDimsMarginModel, ConstantMarginModel][-1] margin_model_for_estimator_class = [LinearAllParametersAllDimsMarginModel, ConstantMarginModel][-1]
...@@ -44,7 +44,7 @@ for i in range(nb_estimator): ...@@ -44,7 +44,7 @@ for i in range(nb_estimator):
max_stable_model=max_stable_model) max_stable_model=max_stable_model)
if show and i == 0: if show and i == 0:
# Plot a realization from the maxima gev (i.e the maxima obtained just by simulating the marginal law) # Plot a realization from the maxima margin_fits (i.e the maxima obtained just by simulating the marginal law)
for maxima in np.transpose(dataset.maxima_frech()): for maxima in np.transpose(dataset.maxima_frech()):
plt.plot(coordinates.coordinates_values(), maxima, 'o') plt.plot(coordinates.coordinates_values(), maxima, 'o')
plt.show() plt.show()
......
...@@ -2,10 +2,8 @@ import os ...@@ -2,10 +2,8 @@ import os
from typing import List from typing import List
import matplotlib import matplotlib
import matplotlib.pyplot as plt
import matplotlib.colors as colors import matplotlib.colors as colors
import matplotlib.cm as cmx import matplotlib.cm as cmx
import numpy as np
import os.path as op import os.path as op
import pickle import pickle
...@@ -21,7 +19,7 @@ from extreme_estimator.extreme_models.margin_model.margin_function.abstract_marg ...@@ -21,7 +19,7 @@ from extreme_estimator.extreme_models.margin_model.margin_function.abstract_marg
from extreme_estimator.extreme_models.margin_model.margin_function.combined_margin_function import \ from extreme_estimator.extreme_models.margin_model.margin_function.combined_margin_function import \
CombinedMarginFunction CombinedMarginFunction
from extreme_estimator.extreme_models.margin_model.margin_function.utils import error_dict_between_margin_functions from extreme_estimator.extreme_models.margin_model.margin_function.utils import error_dict_between_margin_functions
from extreme_estimator.gev_params import GevParams from extreme_estimator.margin_fits.gev.gev_params import GevParams
from spatio_temporal_dataset.dataset.abstract_dataset import get_subset_dataset from spatio_temporal_dataset.dataset.abstract_dataset import get_subset_dataset
from spatio_temporal_dataset.dataset.simulation_dataset import SimulatedDataset from spatio_temporal_dataset.dataset.simulation_dataset import SimulatedDataset
from spatio_temporal_dataset.slicer.split import split_to_display_kwargs from spatio_temporal_dataset.slicer.split import split_to_display_kwargs
...@@ -129,14 +127,14 @@ class AbstractSimulation(object): ...@@ -129,14 +127,14 @@ class AbstractSimulation(object):
@staticmethod @staticmethod
def load_fig_and_axes(): def load_fig_and_axes():
fig, axes = plt.subplots(len(GevParams.GEV_VALUE_NAMES), 2) fig, axes = plt.subplots(len(GevParams.SUMMARY_NAMES), 2)
fig.subplots_adjust(hspace=0.4, wspace=0.4) fig.subplots_adjust(hspace=0.4, wspace=0.4)
return fig, axes return fig, axes
def visualize(self, fig=None, axes=None, show=True): def visualize(self, fig=None, axes=None, show=True):
if fig is None or axes is None: if fig is None or axes is None:
fig, axes = self.load_fig_and_axes() fig, axes = self.load_fig_and_axes()
for i, gev_value_name in enumerate(GevParams.GEV_VALUE_NAMES): for i, gev_value_name in enumerate(GevParams.SUMMARY_NAMES):
self.margin_graph(axes[i, 0], gev_value_name) self.margin_graph(axes[i, 0], gev_value_name)
self.score_graph(axes[i, 1], gev_value_name) self.score_graph(axes[i, 1], gev_value_name)
if show: if show:
......
...@@ -4,7 +4,7 @@ from extreme_estimator.estimator.margin_estimator.margin_estimator_for_simulatio ...@@ -4,7 +4,7 @@ from extreme_estimator.estimator.margin_estimator.margin_estimator_for_simulatio
MARGIN_ESTIMATORS_FOR_SIMULATION MARGIN_ESTIMATORS_FOR_SIMULATION
from extreme_estimator.extreme_models.margin_model.smooth_margin_model import ConstantMarginModel from extreme_estimator.extreme_models.margin_model.smooth_margin_model import 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 extreme_estimator.gev_params import GevParams from extreme_estimator.margin_fits.gev.gev_params import GevParams
from spatio_temporal_dataset.coordinates.spatial_coordinates.coordinates_1D import LinSpaceSpatialCoordinates from spatio_temporal_dataset.coordinates.spatial_coordinates.coordinates_1D import LinSpaceSpatialCoordinates
from spatio_temporal_dataset.dataset.simulation_dataset import FullSimulatedDataset from spatio_temporal_dataset.dataset.simulation_dataset import FullSimulatedDataset
...@@ -22,9 +22,9 @@ class LinSpace5Simulation(AbstractSimulation): ...@@ -22,9 +22,9 @@ class LinSpace5Simulation(AbstractSimulation):
train_split_ratio=0.75) train_split_ratio=0.75)
# MarginModel Constant for simulation # MarginModel Constant for simulation
params_sample = { params_sample = {
(GevParams.GEV_LOC, 0): 1.0, (GevParams.LOC, 0): 1.0,
(GevParams.GEV_SHAPE, 0): 1.0, (GevParams.SHAPE, 0): 1.0,
(GevParams.GEV_SCALE, 0): 1.0, (GevParams.SCALE, 0): 1.0,
} }
self.margin_model = ConstantMarginModel(coordinates=self.coordinates, self.margin_model = ConstantMarginModel(coordinates=self.coordinates,
params_sample=params_sample) params_sample=params_sample)
......
from experiment.simulation.abstract_simulation import AbstractSimulation from experiment.simulation.abstract_simulation import AbstractSimulation
from extreme_estimator.estimator.full_estimator.full_estimator_for_simulation import FULL_ESTIMATORS_FOR_SIMULATION
from extreme_estimator.estimator.margin_estimator.margin_estimator_for_simulation import \
MARGIN_ESTIMATORS_FOR_SIMULATION
from extreme_estimator.extreme_models.margin_model.smooth_margin_model import ConstantMarginModel from extreme_estimator.extreme_models.margin_model.smooth_margin_model import 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 extreme_estimator.gev_params import GevParams from extreme_estimator.margin_fits.gev.gev_params import GevParams
from spatio_temporal_dataset.coordinates.spatial_coordinates.coordinates_1D import LinSpaceSpatialCoordinates from spatio_temporal_dataset.coordinates.spatial_coordinates.coordinates_1D import LinSpaceSpatialCoordinates
from spatio_temporal_dataset.dataset.simulation_dataset import FullSimulatedDataset from spatio_temporal_dataset.dataset.simulation_dataset import FullSimulatedDataset
...@@ -19,9 +16,9 @@ class LinSpaceSimulation(AbstractSimulation): ...@@ -19,9 +16,9 @@ class LinSpaceSimulation(AbstractSimulation):
self.coordinates = LinSpaceSpatialCoordinates.from_nb_points(nb_points=21, train_split_ratio=0.75) self.coordinates = LinSpaceSpatialCoordinates.from_nb_points(nb_points=21, train_split_ratio=0.75)
# 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)
params_sample = { params_sample = {
(GevParams.GEV_LOC, 0): 10, (GevParams.LOC, 0): 10,
(GevParams.GEV_SHAPE, 0): 1.0, (GevParams.SHAPE, 0): 1.0,
(GevParams.GEV_SCALE, 0): 1.0, (GevParams.SCALE, 0): 1.0,
} }
self.margin_model = ConstantMarginModel(coordinates=self.coordinates, params_sample=params_sample) self.margin_model = ConstantMarginModel(coordinates=self.coordinates, params_sample=params_sample)
self.max_stable_model = Smith() self.max_stable_model = Smith()
......
...@@ -5,7 +5,7 @@ from extreme_estimator.extreme_models.abstract_model import AbstractModel ...@@ -5,7 +5,7 @@ from extreme_estimator.extreme_models.abstract_model import AbstractModel
from extreme_estimator.extreme_models.margin_model.margin_function.abstract_margin_function \ from extreme_estimator.extreme_models.margin_model.margin_function.abstract_margin_function \
import AbstractMarginFunction import AbstractMarginFunction
from extreme_estimator.extreme_models.utils import r from extreme_estimator.extreme_models.utils import r
from extreme_estimator.gev_params import GevParams from extreme_estimator.margin_fits.gev.gev_params import GevParams
from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoordinates from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoordinates
......
...@@ -5,7 +5,7 @@ import matplotlib.pyplot as plt ...@@ -5,7 +5,7 @@ import matplotlib.pyplot as plt
import numpy as np import numpy as np
import pandas as pd import pandas as pd
from extreme_estimator.gev_params import GevParams from extreme_estimator.margin_fits.gev.gev_params import GevParams
from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoordinates from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoordinates
from spatio_temporal_dataset.slicer.split import Split from spatio_temporal_dataset.slicer.split import Split
...@@ -36,10 +36,10 @@ class AbstractMarginFunction(object): ...@@ -36,10 +36,10 @@ class AbstractMarginFunction(object):
def gev_value_name_to_serie(self) -> Dict[str, pd.Series]: def gev_value_name_to_serie(self) -> Dict[str, pd.Series]:
# Load the gev_params # Load the gev_params
gev_params = [self.get_gev_params(coordinate) for coordinate in self.coordinates.coordinates_values()] gev_params = [self.get_gev_params(coordinate) for coordinate in self.coordinates.coordinates_values()]
# Load the dictionary of values (gev parameters + the quantiles) # Load the dictionary of values (margin_fits parameters + the quantiles)
value_dicts = [gev_param.value_dict for gev_param in gev_params] value_dicts = [gev_param.summary_dict for gev_param in gev_params]
gev_value_name_to_serie = {} gev_value_name_to_serie = {}
for value_name in GevParams.GEV_VALUE_NAMES: for value_name in GevParams.SUMMARY_NAMES:
s = pd.Series(data=[d[value_name] for d in value_dicts], index=self.coordinates.index) s = pd.Series(data=[d[value_name] for d in value_dicts], index=self.coordinates.index)
gev_value_name_to_serie[value_name] = s gev_value_name_to_serie[value_name] = s
return gev_value_name_to_serie return gev_value_name_to_serie
...@@ -58,10 +58,10 @@ class AbstractMarginFunction(object): ...@@ -58,10 +58,10 @@ class AbstractMarginFunction(object):
def visualize(self, axes=None, show=True, dot_display=False): def visualize(self, axes=None, show=True, dot_display=False):
self.datapoint_display = dot_display self.datapoint_display = dot_display
if axes is None: if axes is None:
fig, axes = plt.subplots(1, len(GevParams.GEV_VALUE_NAMES)) fig, axes = plt.subplots(1, len(GevParams.SUMMARY_NAMES))
fig.subplots_adjust(hspace=1.0, wspace=1.0) fig.subplots_adjust(hspace=1.0, wspace=1.0)
self.visualization_axes = axes self.visualization_axes = axes
for i, gev_value_name in enumerate(GevParams.GEV_VALUE_NAMES): for i, gev_value_name in enumerate(GevParams.SUMMARY_NAMES):
ax = axes[i] ax = axes[i]
self.visualize_single_param(gev_value_name, ax, show=False) self.visualize_single_param(gev_value_name, ax, show=False)
title_str = gev_value_name title_str = gev_value_name
...@@ -69,8 +69,8 @@ class AbstractMarginFunction(object): ...@@ -69,8 +69,8 @@ class AbstractMarginFunction(object):
if show: if show:
plt.show() plt.show()
def visualize_single_param(self, gev_value_name=GevParams.GEV_LOC, ax=None, show=True): def visualize_single_param(self, gev_value_name=GevParams.LOC, ax=None, show=True):
assert gev_value_name in GevParams.GEV_VALUE_NAMES assert gev_value_name in GevParams.SUMMARY_NAMES
if self.coordinates.nb_coordinates_spatial == 1: if self.coordinates.nb_coordinates_spatial == 1:
self.visualize_1D(gev_value_name, ax, show) self.visualize_1D(gev_value_name, ax, show)
elif self.coordinates.nb_coordinates_spatial == 2: elif self.coordinates.nb_coordinates_spatial == 2:
...@@ -80,7 +80,7 @@ class AbstractMarginFunction(object): ...@@ -80,7 +80,7 @@ class AbstractMarginFunction(object):
# Visualization 1D # Visualization 1D
def visualize_1D(self, gev_value_name=GevParams.GEV_LOC, ax=None, show=True): def visualize_1D(self, gev_value_name=GevParams.LOC, ax=None, show=True):
x = self.coordinates.x_coordinates x = self.coordinates.x_coordinates
grid, linspace = self.grid_1D(x) grid, linspace = self.grid_1D(x)
if ax is None: if ax is None:
...@@ -118,13 +118,13 @@ class AbstractMarginFunction(object): ...@@ -118,13 +118,13 @@ class AbstractMarginFunction(object):
grid = [] grid = []
for i, xi in enumerate(linspace): for i, xi in enumerate(linspace):
gev_param = self.get_gev_params(np.array([xi])) gev_param = self.get_gev_params(np.array([xi]))
grid.append(gev_param.value_dict) grid.append(gev_param.summary_dict)
grid = {gev_param: [g[gev_param] for g in grid] for gev_param in GevParams.GEV_VALUE_NAMES} grid = {gev_param: [g[gev_param] for g in grid] for gev_param in GevParams.SUMMARY_NAMES}
return grid, linspace return grid, linspace
# Visualization 2D # Visualization 2D
def visualize_2D(self, gev_value_name=GevParams.GEV_LOC, ax=None, show=True): def visualize_2D(self, gev_value_name=GevParams.LOC, ax=None, show=True):
x = self.coordinates.x_coordinates x = self.coordinates.x_coordinates
y = self.coordinates.y_coordinates y = self.coordinates.y_coordinates
grid = self.grid_2D(x, y) grid = self.grid_2D(x, y)
...@@ -155,7 +155,7 @@ class AbstractMarginFunction(object): ...@@ -155,7 +155,7 @@ class AbstractMarginFunction(object):
grid = [] grid = []
for i, xi in enumerate(np.linspace(x.min(), x.max(), resolution)): for i, xi in enumerate(np.linspace(x.min(), x.max(), resolution)):
for j, yj in enumerate(np.linspace(y.min(), y.max(), resolution)): for j, yj in enumerate(np.linspace(y.min(), y.max(), resolution)):
grid.append(self.get_gev_params(np.array([xi, yj])).value_dict) grid.append(self.get_gev_params(np.array([xi, yj])).summary_dict)
grid = {value_name: np.array([g[value_name] for g in grid]).reshape([resolution, resolution]) grid = {value_name: np.array([g[value_name] for g in grid]).reshape([resolution, resolution])
for value_name in GevParams.GEV_VALUE_NAMES} for value_name in GevParams.SUMMARY_NAMES}
return grid return grid
...@@ -5,7 +5,7 @@ import numpy as np ...@@ -5,7 +5,7 @@ import numpy as np
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.gev_params import GevParams from extreme_estimator.margin_fits.gev.gev_params import GevParams
from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoordinates from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoordinates
......
...@@ -3,7 +3,7 @@ from typing import Dict ...@@ -3,7 +3,7 @@ from typing import Dict
import numpy as np import numpy as np
from extreme_estimator.extreme_models.margin_model.param_function.param_function import ParamFunction from extreme_estimator.extreme_models.margin_model.param_function.param_function import ParamFunction
from extreme_estimator.gev_params import GevParams from extreme_estimator.margin_fits.gev.gev_params import GevParams
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 spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoordinates from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoordinates
...@@ -22,7 +22,7 @@ class IndependentMarginFunction(AbstractMarginFunction): ...@@ -22,7 +22,7 @@ class IndependentMarginFunction(AbstractMarginFunction):
assert self.gev_param_name_to_param_function is not None assert self.gev_param_name_to_param_function is not None
assert len(self.gev_param_name_to_param_function) == 3 assert len(self.gev_param_name_to_param_function) == 3
gev_params = {} gev_params = {}
for gev_param_name in GevParams.GEV_PARAM_NAMES: for gev_param_name in GevParams.PARAM_NAMES:
param_function = self.gev_param_name_to_param_function[gev_param_name] param_function = self.gev_param_name_to_param_function[gev_param_name]
gev_params[gev_param_name] = param_function.get_gev_param_value(coordinate) gev_params[gev_param_name] = param_function.get_gev_param_value(coordinate)
return GevParams.from_dict(gev_params) return GevParams.from_dict(gev_params)
......
...@@ -5,7 +5,7 @@ from extreme_estimator.extreme_models.margin_model.margin_function.independent_m ...@@ -5,7 +5,7 @@ from extreme_estimator.extreme_models.margin_model.margin_function.independent_m
from extreme_estimator.extreme_models.margin_model.param_function.linear_coef import LinearCoef from extreme_estimator.extreme_models.margin_model.param_function.linear_coef import LinearCoef
from extreme_estimator.extreme_models.margin_model.param_function.param_function import ConstantParamFunction, \ from extreme_estimator.extreme_models.margin_model.param_function.param_function import ConstantParamFunction, \
ParamFunction, LinearParamFunction ParamFunction, LinearParamFunction
from extreme_estimator.gev_params import GevParams from extreme_estimator.margin_fits.gev.gev_params import GevParams
from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoordinates from spatio_temporal_dataset.coordinates.abstract_coordinates import AbstractCoordinates
...@@ -40,7 +40,7 @@ class LinearMarginFunction(IndependentMarginFunction): ...@@ -40,7 +40,7 @@ class LinearMarginFunction(IndependentMarginFunction):
assert 0 < dim <= coordinates.nb_coordinates, "dim={}, nb_columns={}".format(dim, coordinates.nb_coordinates) assert 0 < dim <= coordinates.nb_coordinates, "dim={}, nb_columns={}".format(dim, coordinates.nb_coordinates)
# Map each gev_param_name to its corresponding param_function # Map each gev_param_name to its corresponding param_function
for gev_param_name in GevParams.GEV_PARAM_NAMES: for gev_param_name in GevParams.PARAM_NAMES:
linear_coef = self.gev_param_name_to_linear_coef[gev_param_name] linear_coef = self.gev_param_name_to_linear_coef[gev_param_name]
# By default, if linear_dims are not specified, a constantParamFunction is chosen # By default, if linear_dims are not specified, a constantParamFunction is chosen
if gev_param_name not in self.gev_param_name_to_linear_dims.keys(): if gev_param_name not in self.gev_param_name_to_linear_dims.keys():
...@@ -57,7 +57,7 @@ class LinearMarginFunction(IndependentMarginFunction): ...@@ -57,7 +57,7 @@ class LinearMarginFunction(IndependentMarginFunction):
def from_coef_dict(cls, coordinates: AbstractCoordinates, gev_param_name_to_linear_dims: Dict[str, List[int]], def from_coef_dict(cls, coordinates: AbstractCoordinates, gev_param_name_to_linear_dims: Dict[str, List[int]],
coef_dict: Dict[str, float]): coef_dict: Dict[str, float]):
gev_param_name_to_linear_coef = {} gev_param_name_to_linear_coef = {}
for gev_param_name in GevParams.GEV_PARAM_NAMES: for gev_param_name in GevParams.PARAM_NAMES:
linear_dims = gev_param_name_to_linear_dims.get(gev_param_name, []) 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_coef = LinearCoef.from_coef_dict(coef_dict=coef_dict, gev_param_name=gev_param_name,
linear_dims=linear_dims) linear_dims=linear_dims)
...@@ -67,7 +67,7 @@ class LinearMarginFunction(IndependentMarginFunction): ...@@ -67,7 +67,7 @@ class LinearMarginFunction(IndependentMarginFunction):
@property @property
def form_dict(self) -> Dict[str, str]: 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.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, [])
form_dict.update(self.gev_param_name_to_linear_coef[gev_param_name].form_dict(linear_dims=linear_dims)) form_dict.update(self.gev_param_name_to_linear_coef[gev_param_name].form_dict(linear_dims=linear_dims))
return form_dict return form_dict
...@@ -75,7 +75,7 @@ class LinearMarginFunction(IndependentMarginFunction): ...@@ -75,7 +75,7 @@ class LinearMarginFunction(IndependentMarginFunction):
@property @property
def coef_dict(self) -> Dict[str, float]: 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.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, [])
coef_dict.update(self.gev_param_name_to_linear_coef[gev_param_name].coef_dict(linear_dims=linear_dims)) coef_dict.update(self.gev_param_name_to_linear_coef[gev_param_name].coef_dict(linear_dims=linear_dims))
return coef_dict return coef_dict
import numpy as np
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.gev_params import GevParams from extreme_estimator.margin_fits.gev.gev_params import GevParams
def relative_abs_error(reference_value, fitted_value): def relative_abs_error(reference_value, fitted_value):
...@@ -23,7 +21,7 @@ def error_dict_between_margin_functions(reference: AbstractMarginFunction, fitte ...@@ -23,7 +21,7 @@ def error_dict_between_margin_functions(reference: AbstractMarginFunction, fitte
reference_values = reference.gev_value_name_to_serie reference_values = reference.gev_value_name_to_serie
fitted_values = fitted.gev_value_name_to_serie fitted_values = fitted.gev_value_name_to_serie
gev_param_name_to_error_serie = {} gev_param_name_to_error_serie = {}
for gev_value_name in GevParams.GEV_VALUE_NAMES: for gev_value_name in GevParams.SUMMARY_NAMES:
error = 100 * relative_abs_error(reference_values[gev_value_name], fitted_values[gev_value_name]) error = 100 * relative_abs_error(reference_values[gev_value_name], fitted_values[gev_value_name])
gev_param_name_to_error_serie[gev_value_name] = error gev_param_name_to_error_serie[gev_value_name] = error
return gev_param_name_to_error_serie return gev_param_name_to_error_serie
...@@ -3,14 +3,12 @@ from typing import Dict ...@@ -3,14 +3,12 @@ from typing import Dict
import numpy as np import numpy as np
import pandas as pd import pandas as pd
from extreme_estimator.extreme_models.margin_model.margin_function.abstract_margin_function import \
AbstractMarginFunction
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.linear_margin_function import LinearMarginFunction from extreme_estimator.extreme_models.margin_model.margin_function.linear_margin_function import LinearMarginFunction
from extreme_estimator.extreme_models.margin_model.param_function.linear_coef import LinearCoef from extreme_estimator.extreme_models.margin_model.param_function.linear_coef import LinearCoef
from extreme_estimator.extreme_models.utils import safe_run_r_estimator, r, retrieve_fitted_values, get_coord, \ from extreme_estimator.extreme_models.utils import safe_run_r_estimator, r, retrieve_fitted_values, get_coord, \
get_margin_formula get_margin_formula
from extreme_estimator.gev_params import GevParams from extreme_estimator.margin_fits.gev.gev_params import GevParams
class LinearMarginModel(AbstractMarginModel): class LinearMarginModel(AbstractMarginModel):
...@@ -37,7 +35,7 @@ class LinearMarginModel(AbstractMarginModel): ...@@ -37,7 +35,7 @@ class LinearMarginModel(AbstractMarginModel):
default_intercept = 1 default_intercept = 1
default_slope = 0.01 default_slope = 0.01
gev_param_name_and_dim_to_coef = {} gev_param_name_and_dim_to_coef = {}
for gev_param_name in GevParams.GEV_PARAM_NAMES: for gev_param_name in GevParams.PARAM_NAMES:
gev_param_name_and_dim_to_coef[(gev_param_name, 0)] = default_intercept gev_param_name_and_dim_to_coef[(gev_param_name, 0)] = default_intercept
for dim in [1, 2, 3]: for dim in [1, 2, 3]:
gev_param_name_and_dim_to_coef[(gev_param_name, dim)] = default_slope gev_param_name_and_dim_to_coef[(gev_param_name, dim)] = default_slope
...@@ -46,7 +44,7 @@ class LinearMarginModel(AbstractMarginModel): ...@@ -46,7 +44,7 @@ class LinearMarginModel(AbstractMarginModel):
@staticmethod @staticmethod
def gev_param_name_to_linear_coef(param_name_and_dim_to_coef): def gev_param_name_to_linear_coef(param_name_and_dim_to_coef):
gev_param_name_to_linear_coef = {} gev_param_name_to_linear_coef = {}
for gev_param_name in GevParams.GEV_PARAM_NAMES: for gev_param_name in GevParams.PARAM_NAMES:
dim_to_coef = {dim: param_name_and_dim_to_coef[(gev_param_name, dim)] for dim in [0, 1, 2, 3]} dim_to_coef = {dim: param_name_and_dim_to_coef[(gev_param_name, dim)] for dim in [0, 1, 2, 3]}
linear_coef = LinearCoef(gev_param_name=gev_param_name, dim_to_coef=dim_to_coef) linear_coef = LinearCoef(gev_param_name=gev_param_name, dim_to_coef=dim_to_coef)
gev_param_name_to_linear_coef[gev_param_name] = linear_coef gev_param_name_to_linear_coef[gev_param_name] = linear_coef
...@@ -55,7 +53,7 @@ class LinearMarginModel(AbstractMarginModel): ...@@ -55,7 +53,7 @@ class LinearMarginModel(AbstractMarginModel):
@classmethod @classmethod
def from_coef_list(cls, coordinates, gev_param_name_to_coef_list): def from_coef_list(cls, coordinates, gev_param_name_to_coef_list):
params = {} params = {}
for gev_param_name in GevParams.GEV_PARAM_NAMES: for gev_param_name in GevParams.PARAM_NAMES:
for dim, coef in enumerate(gev_param_name_to_coef_list[gev_param_name]): for dim, coef in enumerate(gev_param_name_to_coef_list[gev_param_name]):
params[(gev_param_name, dim)] = coef params[(gev_param_name, dim)] = coef
return cls(coordinates, params_sample=params, params_start_fit=params) return cls(coordinates, params_sample=params, params_start_fit=params)
...@@ -79,41 +77,41 @@ class ConstantMarginModel(LinearMarginModel): ...@@ -79,41 +77,41 @@ class ConstantMarginModel(LinearMarginModel):
class LinearShapeDim1MarginModel(LinearMarginModel): class LinearShapeDim1MarginModel(LinearMarginModel):
def load_margin_functions(self, margin_function_class: type = None, gev_param_name_to_linear_dims=None): def load_margin_functions(self, margin_function_class: type = None, gev_param_name_to_linear_dims=None):
super().load_margin_functions({GevParams.GEV_SHAPE: [1]}) super().load_margin_functions({GevParams.SHAPE: [1]})
class LinearScaleDim1MarginModel(LinearMarginModel): class LinearScaleDim1MarginModel(LinearMarginModel):
def load_margin_functions(self, margin_function_class: type = None, gev_param_name_to_linear_dims=None): def load_margin_functions(self, margin_function_class: type = None, gev_param_name_to_linear_dims=None):
super().load_margin_functions({GevParams.GEV_SCALE: [1]}) super().load_margin_functions({GevParams.SCALE: [1]})
class LinearShapeDim1and2MarginModel(LinearMarginModel): class LinearShapeDim1and2MarginModel(LinearMarginModel):
def load_margin_functions(self, margin_function_class: type = None, gev_param_name_to_linear_dims=None): def load_margin_functions(self, margin_function_class: type = None, gev_param_name_to_linear_dims=None):
super().load_margin_functions({GevParams.GEV_SHAPE: [1, 2]}) super().load_margin_functions({GevParams.SHAPE: [1, 2]})
class LinearAllParametersDim1MarginModel(LinearMarginModel): class LinearAllParametersDim1MarginModel(LinearMarginModel):
def load_margin_functions(self, margin_function_class: type = None, gev_param_name_to_linear_dims=None): def load_margin_functions(self, margin_function_class: type = None, gev_param_name_to_linear_dims=None):
super().load_margin_functions({GevParams.GEV_SHAPE: [1], super().load_margin_functions({GevParams.SHAPE: [1],
GevParams.GEV_LOC: [1], GevParams.LOC: [1],
GevParams.GEV_SCALE: [1]}) GevParams.SCALE: [1]})
class LinearMarginModelExample(LinearMarginModel): class LinearMarginModelExample(LinearMarginModel):
def load_margin_functions(self, margin_function_class: type = None, gev_param_name_to_linear_dims=None): def load_margin_functions(self, margin_function_class: type = None, gev_param_name_to_linear_dims=None):
super().load_margin_functions({GevParams.GEV_SHAPE: [1], super().load_margin_functions({GevParams.SHAPE: [1],
GevParams.GEV_LOC: [2], GevParams.LOC: [2],
GevParams.GEV_SCALE: [1]}) GevParams.SCALE: [1]})
class LinearAllParametersAllDimsMarginModel(LinearMarginModel): class LinearAllParametersAllDimsMarginModel(LinearMarginModel):
def load_margin_functions(self, margin_function_class: type = None, gev_param_name_to_linear_dims=None): def load_margin_functions(self, margin_function_class: type = None, gev_param_name_to_linear_dims=None):
all_dims = list(range(1, self.coordinates.nb_coordinates + 1)) all_dims = list(range(1, self.coordinates.nb_coordinates + 1))
super().load_margin_functions({GevParams.GEV_SHAPE: all_dims.copy(), super().load_margin_functions({GevParams.SHAPE: all_dims.copy(),
GevParams.GEV_LOC: all_dims.copy(), GevParams.LOC: all_dims.copy(),
GevParams.GEV_SCALE: all_dims.copy()}) GevParams.SCALE: all_dims.copy()})
import rpy2.robjects as ro
import numpy as np
import rpy2.robjects.numpy2ri as rpyn
import os.path as op
# Defining some constants
from extreme_estimator.gev_params import GevParams
from extreme_estimator.extreme_models.utils import get_associated_r_file
from extreme_estimator.extreme_models.utils import r
# def gev_mle_fit(x_gev: np.ndarray, start_loc=0.0, start_scale=1.0, start_shape=1.0):
# assert np.ndim(x_gev) == 1
# assert start_scale > 0
# r = ro.r
# x_gev = rpyn.numpy2ri(x_gev)
# r.assign('x_gev', x_gev)
# r.assign('python_wrapping', True)
# r.source(file=get_associated_r_file(python_filepath=op.abspath(__file__)))
# print(start_loc, start_scale, start_shape)
# res = r.mle_gev(loc=start_loc, scale=start_scale, shape=start_shape)
# mle_params = dict(r.attr(res, 'coef').items())
# print('mle params', mle_params)
# return mle_params
def spatial_extreme_gevmle_fit(x_gev):
res = r.gevmle(x_gev, method="Nelder")
return dict(zip(res.names, res))
class GevMleFit(object):
def __init__(self, x_gev: np.ndarray):
assert np.ndim(x_gev) == 1
assert len(x_gev) > 1
self.x_gev = x_gev
self.mle_params = spatial_extreme_gevmle_fit(x_gev)
self.gev_params = GevParams.from_dict(self.mle_params)
self.shape = self.mle_params[GevParams.GEV_SHAPE]
self.scale = self.mle_params[GevParams.GEV_SCALE]
self.loc = self.mle_params[GevParams.GEV_LOC]
import numpy as np
import pandas as pd
from extreme_estimator.extreme_models.utils import r
class GevParams(object):
# GEV parameters
GEV_SCALE = 'scale'
GEV_LOC = 'loc'
GEV_SHAPE = 'shape'
GEV_PARAM_NAMES = [GEV_LOC, GEV_SCALE, GEV_SHAPE]
# GEV quantiles
GEV_QUANTILE_10 = 'quantile 10'
GEV_QUANTILE_100 = 'quantile 100'
GEV_QUANTILE_1000 = 'quantile 1000'
GEV_QUANTILE_NAMES = [GEV_QUANTILE_10, GEV_QUANTILE_100, GEV_QUANTILE_1000]
GEV_QUANTILE_P_VALUES = [0.9, 0.99, 0.999]
# GEV values
GEV_VALUE_NAMES = GEV_PARAM_NAMES + GEV_QUANTILE_NAMES[:-1]
# GEV parameters
def __init__(self, loc: float, scale: float, shape: float):
self.location = loc
self.scale = scale
self.shape = shape
# self.scale = max(self.scale, 1e-4)
assert self.scale > 0
@classmethod
def from_dict(cls, params: dict):
return cls(**params)
def to_dict(self) -> dict:
return {
self.GEV_LOC: self.location,
self.GEV_SCALE: self.scale,
self.GEV_SHAPE: self.shape,
}
def to_array(self) -> np.ndarray:
return self.to_serie().values
def to_serie(self) -> pd.Series:
return pd.Series(self.to_dict(), index=self.GEV_PARAM_NAMES)
# GEV quantiles
def qgev(self, p) -> float:
return r.qgev(p, self.location, self.scale, self.shape)[0]
@property
def quantile_name_to_p(self) -> dict:
return dict(zip(self.GEV_QUANTILE_NAMES, self.GEV_QUANTILE_P_VALUES))
@property
def quantile_dict(self) -> dict:
return {quantile_name: self.qgev(p) for quantile_name, p in self.quantile_name_to_p.items()}
# GEV values
@property
def value_dict(self) -> dict:
return {**self.to_dict(), **self.quantile_dict}
@property
def value_serie(self) -> pd.Series:
return pd.Series(self.value_dict, index=self.GEV_VALUE_NAMES)
from typing import List
import numpy as np
import pandas as pd
from extreme_estimator.extreme_models.utils import r
class AbstractParams(object):
# Parameters
PARAM_NAMES = []
# Quantile
QUANTILE_10 = 'quantile 10'
QUANTILE_100 = 'quantile 100'
QUANTILE_1000 = 'quantile 1000'
QUANTILE_NAMES = [QUANTILE_10, QUANTILE_100, QUANTILE_1000][:-1]
QUANTILE_P_VALUES = [0.9, 0.99, 0.999]
# Summary
SUMMARY_NAMES = PARAM_NAMES + QUANTILE_NAMES
@classmethod
def from_dict(cls, params: dict):
return cls(**params)
# Parameters
@property
def param_values(self) -> List[float]:
raise NotImplementedError
def to_dict(self) -> dict:
return dict(zip(self.PARAM_NAMES, self.param_values))
def to_serie(self) -> pd.Series:
return pd.Series(self.to_dict(), index=self.PARAM_NAMES)
def to_array(self) -> np.ndarray:
return self.to_serie().values
# Quantile
def quantile(self, p) -> float:
raise NotImplementedError
@property
def quantile_name_to_p(self) -> dict:
return dict(zip(self.QUANTILE_NAMES, self.QUANTILE_P_VALUES))
@property
def quantile_name_to_value(self) -> dict:
return {quantile_name: self.quantile(p) for quantile_name, p in self.quantile_name_to_p.items()}
# Summary (i.e. parameters & quantiles)
@property
def summary_dict(self) -> dict:
return {**self.to_dict(), **self.quantile_name_to_value}
@property
def summary_serie(self) -> pd.Series:
return pd.Series(self.summary_dict, index=self.SUMMARY_NAMES)
from extreme_estimator.margin_fits.abstract_params import AbstractParams
class ExtremeParams(AbstractParams):
# Extreme parameters
SCALE = 'scale'
LOC = 'loc'
SHAPE = 'shape'
PARAM_NAMES = [LOC, SCALE, SHAPE]
def __init__(self, loc: float, scale: float, shape: float):
self.location = loc
self.scale = scale
self.shape = shape
assert self.scale > 0
@property
def param_values(self):
return [self.location, self.scale, self.shape]
\ No newline at end of file
from extreme_estimator.extreme_models.utils import r
from extreme_estimator.margin_fits.extreme_params import ExtremeParams
class GevParams(ExtremeParams):
def __init__(self, loc: float, scale: float, shape: float, block_size: int = None):
super().__init__(loc, scale, shape)
self.block_size = block_size
def quantile(self, p) -> float:
return r.qgev(p, self.location, self.scale, self.shape)[0]
import numpy as np
from extreme_estimator.margin_fits.gev.gev_params import GevParams
from extreme_estimator.margin_fits.margin_fits_utils import spatial_extreme_gevmle_fit
class GevMleFit(object):
def __init__(self, x_gev: np.ndarray, block_size=None):
assert np.ndim(x_gev) == 1
assert len(x_gev) > 1
self.x_gev = x_gev
self.mle_params = spatial_extreme_gevmle_fit(x_gev)
self.gev_params = GevParams.from_dict({**self.mle_params, 'block_size': block_size})
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