From 90a15a573d2b3a1e0f430c0d414717092201d696 Mon Sep 17 00:00:00 2001 From: Le Roux Erwan <erwan.le-roux@irstea.fr> Date: Fri, 20 Sep 2019 14:32:18 +0200 Subject: [PATCH] [TREND ANALYSIS] add test_mean, and test_variance for the GevParams class --- .../poster_EVAN2019/some_experiment_EVAN.py | 6 +++--- .../margin_fits/gev/gev_params.py | 5 +++++ .../test_gev/test_gev_params.py | 21 +++++++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/experiment/paper1_steps/poster_EVAN2019/some_experiment_EVAN.py b/experiment/paper1_steps/poster_EVAN2019/some_experiment_EVAN.py index 6ea0da24..0dae7739 100644 --- a/experiment/paper1_steps/poster_EVAN2019/some_experiment_EVAN.py +++ b/experiment/paper1_steps/poster_EVAN2019/some_experiment_EVAN.py @@ -18,15 +18,15 @@ mpl.rcParams['hatch.linewidth'] = 0.3 def main_non_stationary_model_comparison(): - stop_loop = True + stop_loop = False for altitude in POSTER_ALTITUDES[:]: for trend_test_class in [GevLocationTrendTest, GevScaleTrendTest, GevLocationAndScaleTrendTest, - ComparisonAgainstMu, ComparisonAgainstSigma][::-1][-1:]: + ComparisonAgainstMu, ComparisonAgainstSigma][::-1][:]: vizualiser = get_full_altitude_visualizer(Altitude_Hypercube_Year_Visualizer, altitude=altitude, exact_starting_year=1958, reduce_strength_array=False, trend_test_class=trend_test_class, ) - vizualiser.save_to_file = False + # vizualiser.save_to_file = False vizualiser.visualize_massif_trend_test_one_altitude(poster_plot=True, write_text_on_massif=False) if stop_loop: return diff --git a/extreme_estimator/margin_fits/gev/gev_params.py b/extreme_estimator/margin_fits/gev/gev_params.py index 395032b5..65c12f79 100644 --- a/extreme_estimator/margin_fits/gev/gev_params.py +++ b/extreme_estimator/margin_fits/gev/gev_params.py @@ -2,6 +2,7 @@ from collections import OrderedDict from typing import List from cached_property import cached_property +from mpmath import euler, pi from extreme_estimator.extreme_models.utils import r from extreme_estimator.margin_fits.extreme_params import ExtremeParams @@ -78,6 +79,8 @@ class GevParams(ExtremeParams): return np.nan elif self.shape >= 1: return np.inf + elif self.shape == 0: + return self.location + self.scale * euler else: return self.location + self.scale * (self.g(k=1) - 1) / self.shape @@ -87,6 +90,8 @@ class GevParams(ExtremeParams): return np.nan elif self.shape >= 0.5: return np.inf + elif self.shape == 0.0: + return (self.scale * pi) ** 2 / 6 else: return ((self.scale / self.shape) ** 2) * (self.g(k=2) - self.g(k=1) ** 2) diff --git a/test/test_extreme_estimator/test_margin_fits/test_gev/test_gev_params.py b/test/test_extreme_estimator/test_margin_fits/test_gev/test_gev_params.py index 0b19d97a..0e7d8903 100644 --- a/test/test_extreme_estimator/test_margin_fits/test_gev/test_gev_params.py +++ b/test/test_extreme_estimator/test_margin_fits/test_gev/test_gev_params.py @@ -1,6 +1,9 @@ import unittest +from mpmath import euler + import numpy as np +from scipy.special.cython_special import gamma from extreme_estimator.margin_fits.gev.gev_params import GevParams @@ -33,6 +36,24 @@ class TestGevParams(unittest.TestCase): self.assertEqual(gev_params.variance, np.inf) self.assertEqual(gev_params.std, np.inf) + def test_mean(self): + mu = 1.0 + sigma = 1.0 + gev_params = GevParams(loc=mu, shape=0.0, scale=sigma) + self.assertEqual(gev_params.mean, mu + sigma * euler) + chi = 0.5 + gev_params = GevParams(loc=mu, shape=chi, scale=sigma) + self.assertEqual(gev_params.mean, mu + sigma * (gamma(1 - 0.5) - 1) / chi) + + def test_variance(self): + mu = 1.0 + sigma = 1.0 + gev_params = GevParams(loc=mu, shape=0.0, scale=sigma) + self.assertEqual(gev_params.variance, ((sigma * np.math.pi) ** 2) / 6) + chi = 0.25 + gev_params = GevParams(loc=mu, shape=chi, scale=sigma) + self.assertEqual(gev_params.variance, ((sigma / chi) ** 2) * (gamma(1 - 2 * chi) - (gamma(1 - chi) ** 2))) + if __name__ == '__main__': unittest.main() -- GitLab