{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Working with Percentiles (and Quantiles) of Distributions\n", "\n", "Many times we want to observe different distributional properties of scenario data. The **pyam** function `compute.quantiles()` can help!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 0. Define timeseries data and initialize an IamDataFrame\n", "\n", "This tutorial uses a scenario similar to the data in the **first-steps tutorial** (here on\n", "[GitHub](https://github.com/IAMconsortium/pyam/blob/master/doc/source/tutorials/pyam_first_steps.ipynb)\n", "and on [read the docs](https://pyam-iamc.readthedocs.io/en/stable/tutorials/pyam_first_steps.html)). \n", "Please read that tutorial for the reference and further information." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from pyam import IamDataFrame\n", "\n", "df = IamDataFrame(data=\"tutorial_data.csv\")\n", "df.timeseries().head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Let's see how many scenarios define CO2 emissions\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.filter(variable=\"Emissions|CO2\", region=\"World\").scenario" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. Get the median\n", "\n", "The median is the `0.5` quantile (or percentile) - let's take a look!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from matplotlib import pyplot as plt\n", "\n", "fig, ax = plt.subplots()\n", "\n", "# plot the background field of scenario data\n", "(\n", " df.filter(variable=\"Emissions|CO2\", region=\"World\").plot.line(\n", " color=\"variable\", alpha=0, fill_between=True, ax=ax\n", " )\n", ")\n", "\n", "# plot just the median\n", "(\n", " df.filter(variable=\"Emissions|CO2\", region=\"World\")\n", " .compute.quantiles([0.5])\n", " .plot.line(ax=ax)\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. Get arbitrary quantiles\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig, ax = plt.subplots()\n", "\n", "# plot the background field of scenario data\n", "(\n", " df.filter(variable=\"Emissions|CO2\", region=\"World\").plot.line(\n", " color=\"variable\", alpha=0, fill_between=True, ax=ax\n", " )\n", ")\n", "\n", "# plot quantiles\n", "(\n", " df.filter(variable=\"Emissions|CO2\", region=\"World\")\n", " .compute.quantiles([0.2, 0.6, 0.8])\n", " .plot.line(ax=ax)\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4. Weighted Quantiles\n", "\n", "Weighted quantiles are also supported via the `wquantiles` package and are keyed to model/scenario combinations (unless the `level` argument is provided to `compute.quantiles()`). " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "weights = df.meta.assign(weight=np.random.rand(len(df.meta)))\n", "weights.head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig, ax = plt.subplots()\n", "\n", "# plot the background field of scenario data\n", "(\n", " df.filter(variable=\"Emissions|CO2\", region=\"World\").plot.line(\n", " color=\"variable\", alpha=0, fill_between=True, ax=ax\n", " )\n", ")\n", "\n", "# plot weighted quantiles\n", "(\n", " df.filter(variable=\"Emissions|CO2\", region=\"World\")\n", " .compute.quantiles([0.2, 0.6, 0.8], weights=weights[\"weight\"])\n", " .plot.line(ax=ax)\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.0" } }, "nbformat": 4, "nbformat_minor": 2 }