Skip to content

Instantly share code, notes, and snippets.

@bennyistanto
Created June 23, 2024 04:17
Show Gist options
  • Save bennyistanto/475d36ef815b55f993c4447384796461 to your computer and use it in GitHub Desktop.
Save bennyistanto/475d36ef815b55f993c4447384796461 to your computer and use it in GitHub Desktop.
Simple Generalized Extreme Value (GEV) from set of data
# Copy and paste the code below into https://python-fiddle.com/
# You will get instant result: GEV 25-year threshold: 96.12570045491432
import numpy as np
from scipy.stats import genextreme
# This data is the maximum daily precipitation in a month (let say May) for the period
# 2001-2023
data = [
35.809994, 55.120003, 97.575005, 17.01, 43.260006, 50.285004, 51.489998, 59.05999,
58.339996, 82.835, 42.875, 55.574997, 105.149994, 61.155, 83.68001, 73.56,
53.729996, 54.519997, 56.145004, 57.454994, 59.725, 72.605, 25.219997
]
data = np.array(data)
data = data[~np.isnan(data)] # Remove NaN values
if data.size == 0 or np.nanstd(data) == 0:
threshold = np.nan
else:
try:
shape, loc, scale = genextreme.fit(data)
threshold = genextreme.ppf(1 - 1 / 25, shape, loc=loc, scale=scale)
except Exception as e:
threshold = np.nan
print(f"GEV 25-year threshold: {threshold}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment