{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Extreme Value Analysis using Extremes.jl\n", "\n", "This module provides an easy-to-use wrapper for the `Extremes.jl` Julia package, enabling seamless integration with `xarray` for extreme value analysis. However, do note that `juliacall` is not installed by default when installing `xHydro`. Consult the installation page for instructions.\n", "\n", "The `Extremes.jl` package is specifically designed for analyzing extreme values and offers a variety of powerful features:\n", "\n", "- Block Maxima and Threshold Exceedance methods, including popular distributions such as `genextreme`, `gumbel_r`, and `genpareto`.\n", "- Flexible parameter estimation techniques, supporting methods like `Probability-Weighted Moments (PWM)`, `Maximum Likelihood Estimation (MLE)`, and `Bayesian Estimation`.\n", "- Compatibility with both stationary and non-stationary models for flexible modeling of future extreme events.\n", "- Return level estimation for quantifying the risk of extreme events over different return periods.\n", "\n", "For further information on the `Extremes.jl` package, consult the following resources:\n", "- [Extremes.jl - JSS Article](https://doi.org/10.18637/jss.v109.i06)\n", "- [Extremes.jl GitHub Repository](https://github.com/jojal5/Extremes.jl)\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2024-12-11T20:54:27.904824Z", "start_time": "2024-12-11T20:54:10.703052Z" } }, "outputs": [], "source": [ "import os\n", "\n", "os.environ[\"PYTHON_JULIACALL_AUTOLOAD_IPYTHON_EXTENSION\"] = (\n", " \"no\" # To prevent random crashes with GitHub's testing interface\n", ")\n", "\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import pandas as pd\n", "import pooch\n", "\n", "import xhydro.extreme_value_analysis as xhe\n", "from xhydro.testing.helpers import deveraux" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Data acquisition\n", "\n", "This example will use climate data from the `GFDL-ESM4.1` model to demonstrate non-stationarity. The dataset includes annual total precipitation data from 1955 to 2100, spanning 97 virtual stations across the province of Quebec. For more information on how to access precipitation data or perform block maxima, consult the [Local frequency analyses](local_frequency_analysis.ipynb) notebook.\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2024-12-11T20:54:28.052959Z", "start_time": "2024-12-11T20:54:27.909829Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Downloading file 'extreme_value_analysis/NOAA_GFDL_ESM4.zip' from 'https://raw.githubusercontent.com/hydrologie/xhydro-testdata/v2025.3.31/data/extreme_value_analysis/NOAA_GFDL_ESM4.zip' to 'C:\\Users\\ospin\\AppData\\Local\\xhydro-testdata\\xhydro-testdata\\Cache\\v2025.3.31'.\n", "Unzipping contents of 'C:\\Users\\ospin\\AppData\\Local\\xhydro-testdata\\xhydro-testdata\\Cache\\v2025.3.31\\extreme_value_analysis\\NOAA_GFDL_ESM4.zip' to 'C:\\Users\\ospin\\AppData\\Local\\xhydro-testdata\\xhydro-testdata\\Cache\\v2025.3.31\\extreme_value_analysis\\NOAA_GFDL_ESM4.zip.unzip'\n" ] }, { "data": { "text/html": [ "
<xarray.Dataset> Size: 13kB\n", "Dimensions: (station_num: 5, time: 146)\n", "Coordinates:\n", " * station_num (station_num) int64 40B 1001 1004 1008 1009 1012\n", " * time (time) datetime64[ns] 1kB 1955-01-01 1956-01-01 ... 2100-01-01\n", " station_name (station_num, time) object 6kB 'Dozois' ... 'Lac St-Francois'\n", "Data variables:\n", " total_precip (station_num, time) float64 6kB 983.6 1.204e+03 ... 1.065e+03
<xarray.Dataset> Size: 460B\n", "Dimensions: (station_num: 5, dparams: 3)\n", "Coordinates:\n", " * station_num (station_num) int64 40B 1001 1004 1008 1009 1012\n", " * dparams (dparams) <U5 60B 'shape' 'loc' 'scale'\n", "Data variables:\n", " total_precip (station_num, dparams) float64 120B 0.2012 ... 155.8\n", " total_precip_lower (station_num, dparams) float64 120B 0.08898 ... 135.1\n", " total_precip_upper (station_num, dparams) float64 120B 0.3218 ... 173.4
<xarray.Dataset> Size: 164B\n", "Dimensions: (station_num: 5, return_period: 1)\n", "Coordinates:\n", " * station_num (station_num) int64 40B 1001 1004 1008 1009 1012\n", " * return_period (return_period) int32 4B 100\n", "Data variables:\n", " total_precip (station_num, return_period) float64 40B 1.704e+03 .....\n", " total_precip_lower (station_num, return_period) float64 40B 1.609e+03 .....\n", " total_precip_upper (station_num, return_period) float64 40B 1.8e+03 ... ...
<xarray.Dataset> Size: 19kB\n", "Dimensions: (station_num: 5, time: 146)\n", "Coordinates:\n", " * station_num (station_num) int64 40B 1001 1004 1008 1009 1012\n", " * time (time) datetime64[ns] 1kB 1955-01-01 1956-01-01 ... 2100-01-01\n", " station_name (station_num, time) object 6kB 'Dozois' ... 'Lac St-Francois'\n", "Data variables:\n", " total_precip (station_num, time) float64 6kB 983.6 1.204e+03 ... 1.065e+03\n", " year (station_num, time) int64 6kB 1955 1956 1957 ... 2099 2100
<xarray.Dataset> Size: 808B\n", "Dimensions: (station_num: 5, dparams: 4)\n", "Coordinates:\n", " * station_num (station_num) int64 40B 1001 1004 1008 1009 1012\n", " * dparams (dparams) <U18 288B 'shape' 'loc' ... 'scale'\n", "Data variables:\n", " total_precip (station_num, dparams) float64 160B 0.2051 ... 144.3\n", " total_precip_lower (station_num, dparams) float64 160B 0.1022 ... 127.0\n", " total_precip_upper (station_num, dparams) float64 160B 0.308 ... 164.0
<xarray.Dataset> Size: 25kB\n", "Dimensions: (station_num: 5, time: 146, return_period: 1)\n", "Coordinates:\n", " * station_num (station_num) int64 40B 1001 1004 1008 1009 1012\n", " * time (time) datetime64[ns] 1kB 1955-01-01 ... 2100-01-01\n", " station_name (station_num, time) object 6kB 'Dozois' ... 'Lac St-F...\n", " * return_period (return_period) int32 4B 100\n", "Data variables:\n", " total_precip (station_num, time) float64 6kB 1.557e+03 ... 1.771e+03\n", " total_precip_lower (station_num, time) float64 6kB 1.457e+03 ... 1.672e+03\n", " total_precip_upper (station_num, time) float64 6kB 1.656e+03 ... 1.87e+03" ], "text/plain": [ "
<xarray.Dataset> Size: 460B\n", "Dimensions: (station_num: 5, dparams: 3)\n", "Coordinates:\n", " * station_num (station_num) int64 40B 1001 1004 1008 1009 1012\n", " * dparams (dparams) <U5 60B 'shape' 'loc' 'scale'\n", "Data variables:\n", " total_precip (station_num, dparams) float64 120B 0.1816 ... 153.7\n", " total_precip_lower (station_num, dparams) float64 120B 0.08659 ... 135.6\n", " total_precip_upper (station_num, dparams) float64 120B 0.2766 ... 174.3