Commit 2a312ff5 authored by Le Roux Erwan's avatar Le Roux Erwan
Browse files

[EXTREME FIT][MODEL] add covariance attribute to result_from_ismev

parent 19c9a2f4
No related merge requests found
Showing with 34 additions and 20 deletions
+34 -20
...@@ -280,11 +280,11 @@ class ComparisonAnalysis(object): ...@@ -280,11 +280,11 @@ class ComparisonAnalysis(object):
margin_model=margin_model, margin_model=margin_model,
max_stable_model=max_stable_model) max_stable_model=max_stable_model)
estimator.fit() estimator.fit()
print(estimator.result_from_model_fit.margin_coef_dict) print(estimator.result_from_model_fit.margin_coef_ordered_dict)
print(estimator.result_from_model_fit.other_coef_dict) print(estimator.result_from_model_fit.other_coef_dict)
estimators.append(estimator) estimators.append(estimator)
# Compare the sign of them margin coefficient for the estimators # Compare the sign of them margin coefficient for the estimators
coefs = [{k: v for k, v in e.result_from_model_fit.margin_coef_dict.items() if 'Coeff1' not in k} for e in coefs = [{k: v for k, v in e.result_from_model_fit.margin_coef_ordered_dict.items() if 'Coeff1' not in k} for e in
estimators] estimators]
different_sign = [k for k, v in coefs[0].items() if np.sign(coefs[1][k]) != np.sign(v)] different_sign = [k for k, v in coefs[0].items() if np.sign(coefs[1][k]) != np.sign(v)]
print('All linear coefficient have the same sign: {}, different_signs for: {}'.format( print('All linear coefficient have the same sign: {}, different_signs for: {}'.format(
......
...@@ -5,7 +5,7 @@ from extreme_fit.model.margin_model.margin_function.linear_margin_function impor ...@@ -5,7 +5,7 @@ from extreme_fit.model.margin_model.margin_function.linear_margin_function impor
def load_margin_function(estimator: AbstractEstimator, margin_model: LinearMarginModel, margin_function_class=LinearMarginFunction, coef_dict=None): def load_margin_function(estimator: AbstractEstimator, margin_model: LinearMarginModel, margin_function_class=LinearMarginFunction, coef_dict=None):
if coef_dict is None: if coef_dict is None:
coef_dict = estimator.result_from_model_fit.margin_coef_dict coef_dict = estimator.result_from_model_fit.margin_coef_ordered_dict
return margin_function_class.from_coef_dict(coordinates=estimator.dataset.coordinates, return margin_function_class.from_coef_dict(coordinates=estimator.dataset.coordinates,
gev_param_name_to_dims=margin_model.margin_function_start_fit.gev_param_name_to_dims, gev_param_name_to_dims=margin_model.margin_function_start_fit.gev_param_name_to_dims,
coef_dict=coef_dict, coef_dict=coef_dict,
......
...@@ -20,9 +20,13 @@ class AbstractResultFromModelFit(object): ...@@ -20,9 +20,13 @@ class AbstractResultFromModelFit(object):
raise NotImplementedError raise NotImplementedError
@property @property
def margin_coef_dict(self): def margin_coef_ordered_dict(self):
raise NotImplementedError raise NotImplementedError
@property
def margin_coef_ordered_names(self):
return list(self.margin_coef_ordered_dict.keys())
@property @property
def other_coef_dict(self): def other_coef_dict(self):
raise NotImplementedError raise NotImplementedError
...@@ -39,6 +43,10 @@ class AbstractResultFromModelFit(object): ...@@ -39,6 +43,10 @@ class AbstractResultFromModelFit(object):
def convergence(self) -> str: def convergence(self) -> str:
raise NotImplementedError raise NotImplementedError
@property
def covariance(self):
raise NotImplementedError
......
...@@ -5,7 +5,7 @@ from rpy2 import robjects ...@@ -5,7 +5,7 @@ from rpy2 import robjects
from extreme_fit.distribution.gev.gev_params import GevParams from extreme_fit.distribution.gev.gev_params import GevParams
from extreme_fit.model.result_from_model_fit.abstract_result_from_model_fit import \ from extreme_fit.model.result_from_model_fit.abstract_result_from_model_fit import \
AbstractResultFromModelFit AbstractResultFromModelFit
from extreme_fit.model.result_from_model_fit.utils import get_margin_coef_dict from extreme_fit.model.result_from_model_fit.utils import get_margin_coef_ordered_dict
from extreme_fit.model.utils import r from extreme_fit.model.utils import r
...@@ -40,10 +40,10 @@ class ResultFromExtremes(AbstractResultFromModelFit): ...@@ -40,10 +40,10 @@ class ResultFromExtremes(AbstractResultFromModelFit):
def get_coef_dict_from_posterior_sample(self, s: pd.Series): def get_coef_dict_from_posterior_sample(self, s: pd.Series):
assert len(s) >= 3 assert len(s) >= 3
values = {i: v for i, v in enumerate(s)} values = {i: v for i, v in enumerate(s)}
return get_margin_coef_dict(self.gev_param_name_to_dim, values) return get_margin_coef_ordered_dict(self.gev_param_name_to_dim, values)
@property @property
def margin_coef_dict(self): def margin_coef_ordered_dict(self):
""" It is the coef for the margin function corresponding to the mean posterior parameters """ """ It is the coef for the margin function corresponding to the mean posterior parameters """
mean_posterior_parameters = self.df_posterior_samples.iloc[:, :-2].mean(axis=0) mean_posterior_parameters = self.df_posterior_samples.iloc[:, :-2].mean(axis=0)
return self.get_coef_dict_from_posterior_sample(mean_posterior_parameters) return self.get_coef_dict_from_posterior_sample(mean_posterior_parameters)
......
from rpy2 import robjects import numpy as np
import pandas as pd
from extreme_fit.model.result_from_model_fit.abstract_result_from_model_fit import \
AbstractResultFromModelFit
from extreme_fit.model.result_from_model_fit.utils import convertFloatVector_to_float, get_margin_coef_dict
from rpy2 import robjects from rpy2 import robjects
from extreme_fit.model.result_from_model_fit.abstract_result_from_model_fit import \ from extreme_fit.model.result_from_model_fit.abstract_result_from_model_fit import \
AbstractResultFromModelFit AbstractResultFromModelFit
from extreme_fit.model.result_from_model_fit.utils import convertFloatVector_to_float from extreme_fit.model.result_from_model_fit.utils import convertFloatVector_to_float
from extreme_fit.model.result_from_model_fit.utils import get_margin_coef_ordered_dict
class ResultFromIsmev(AbstractResultFromModelFit): class ResultFromIsmev(AbstractResultFromModelFit):
...@@ -17,12 +15,12 @@ class ResultFromIsmev(AbstractResultFromModelFit): ...@@ -17,12 +15,12 @@ class ResultFromIsmev(AbstractResultFromModelFit):
self.gev_param_name_to_dim = gev_param_name_to_dim self.gev_param_name_to_dim = gev_param_name_to_dim
@property @property
def margin_coef_dict(self): def margin_coef_ordered_dict(self):
return get_margin_coef_dict(self.gev_param_name_to_dim, self.name_to_value['mle']) return get_margin_coef_ordered_dict(self.gev_param_name_to_dim, self.name_to_value['mle'])
@property @property
def all_parameters(self): def all_parameters(self):
return self.margin_coef_dict return self.margin_coef_ordered_dict
@property @property
def nllh(self): def nllh(self):
...@@ -35,3 +33,9 @@ class ResultFromIsmev(AbstractResultFromModelFit): ...@@ -35,3 +33,9 @@ class ResultFromIsmev(AbstractResultFromModelFit):
@property @property
def convergence(self) -> str: def convergence(self) -> str:
return convertFloatVector_to_float(self.name_to_value['conv']) == 0 return convertFloatVector_to_float(self.name_to_value['conv']) == 0
@property
def covariance(self):
return pd.DataFrame(np.array(self.name_to_value['cov']),
index=self.margin_coef_ordered_names,
columns=self.margin_coef_ordered_names)
...@@ -33,7 +33,7 @@ class ResultFromSpatialExtreme(AbstractResultFromModelFit): ...@@ -33,7 +33,7 @@ class ResultFromSpatialExtreme(AbstractResultFromModelFit):
return {key: fitted_values.rx2(key)[0] for key in fitted_values.names} return {key: fitted_values.rx2(key)[0] for key in fitted_values.names}
@property @property
def margin_coef_dict(self): def margin_coef_ordered_dict(self):
return {k: v for k, v in self.all_parameters.items() if LinearCoef.COEFF_STR in k} return {k: v for k, v in self.all_parameters.items() if LinearCoef.COEFF_STR in k}
@property @property
......
from collections import OrderedDict
import numpy as np import numpy as np
from extreme_fit.distribution.gev.gev_params import GevParams from extreme_fit.distribution.gev.gev_params import GevParams
...@@ -9,10 +11,10 @@ def convertFloatVector_to_float(f): ...@@ -9,10 +11,10 @@ def convertFloatVector_to_float(f):
return np.array(f)[0] return np.array(f)[0]
def get_margin_coef_dict(gev_param_name_to_dim, mle_values): def get_margin_coef_ordered_dict(gev_param_name_to_dim, mle_values):
assert gev_param_name_to_dim is not None assert gev_param_name_to_dim is not None
# Build the Coeff dict from gev_param_name_to_dim # Build the Coeff dict from gev_param_name_to_dim
coef_dict = {} coef_dict = OrderedDict()
i = 0 i = 0
for gev_param_name in GevParams.PARAM_NAMES: for gev_param_name in GevParams.PARAM_NAMES:
# Add intercept # Add intercept
......
...@@ -22,7 +22,7 @@ class TestGevTemporal(unittest.TestCase): ...@@ -22,7 +22,7 @@ class TestGevTemporal(unittest.TestCase):
set_seed_r() set_seed_r()
r(""" r("""
N <- 50 N <- 50
loc = 0; scale = 1; shape <- 1 loc = 0; scale = 2; shape <- 1
x_gev <- rgev(N, loc = loc, scale = scale, shape = shape) x_gev <- rgev(N, loc = loc, scale = scale, shape = shape)
start_loc = 0; start_scale = 1; start_shape = 1 start_loc = 0; start_scale = 1; start_shape = 1
""") """)
...@@ -39,7 +39,7 @@ class TestGevTemporal(unittest.TestCase): ...@@ -39,7 +39,7 @@ class TestGevTemporal(unittest.TestCase):
margin_model = StationaryTemporalModel(self.coordinates) margin_model = StationaryTemporalModel(self.coordinates)
estimator = LinearMarginEstimator(self.dataset, margin_model) estimator = LinearMarginEstimator(self.dataset, margin_model)
estimator.fit() estimator.fit()
ref = {'loc': 0.0219, 'scale': 1.0347, 'shape': 0.8295} ref = {'loc': 0.04309190816463247, 'scale': 2.0688696961628437, 'shape': 0.8291528207825063}
for year in range(1, 3): for year in range(1, 3):
mle_params_estimated = estimator.margin_function_from_fit.get_gev_params(np.array([year])).to_dict() mle_params_estimated = estimator.margin_function_from_fit.get_gev_params(np.array([year])).to_dict()
for key in ref.keys(): for key in ref.keys():
......
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