{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Probability distributions based on OpenTURNS\n\nIn this example,\nwe seek to create a probability distribution based on the OpenTURNS library.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from __future__ import annotations\n\nfrom gemseo.api import configure_logger\nfrom gemseo.uncertainty.api import create_distribution\nfrom gemseo.uncertainty.api import get_available_distributions\nfrom matplotlib import pyplot as plt\n\nconfigure_logger()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "First of all,\nwe can access the names of the available probability distributions from the API:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "all_distributions = get_available_distributions()\nprint(all_distributions)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "and filter the ones based on the OpenTURNS library\n(their names start with the acronym 'OT'):\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "ot_distributions = [dist for dist in all_distributions if dist.startswith(\"OT\")]\nprint(ot_distributions)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Create a distribution\nThen,\nwe can create a probability distribution for a two-dimensional random variable\nwhose components are independent and distributed\nas the standard normal distribution (mean = 0 and standard deviation = 1):\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "distribution_0_1 = create_distribution(\"x\", \"OTNormalDistribution\", 2)\nprint(distribution_0_1)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "or create another distribution with mean = 1 and standard deviation = 2\nfor the marginal distributions:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "distribution_1_2 = create_distribution(\n    \"x\", \"OTNormalDistribution\", 2, mu=1.0, sigma=2.0\n)\nprint(distribution_1_2)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We could also use the generic :class:`.OTDistribution`\nwhich allows access to all the OpenTURNS distributions\nbut this requires to know the signature of the methods of this library:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "distribution_1_2 = create_distribution(\n    \"x\", \"OTDistribution\", 2, interfaced_distribution=\"Normal\", parameters=(1.0, 2.0)\n)\nprint(distribution_1_2)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Plot the distribution\nWe can plot both cumulative and probability density functions\nfor the first marginal:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "distribution_0_1.plot(show=False)\n# Workaround for HTML rendering, instead of ``show=True``\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "<div class=\"alert alert-info\"><h4>Note</h4><p>We can provide a marginal index\n   as first argument of the :meth:`.Distribution.plot` method\n   but in the current version of |g|,\n   all components have the same distributions and so the plot will be the same.</p></div>\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Get mean\nWe can access the mean of the distribution:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(distribution_0_1.mean)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Get standard deviation\nWe can access the standard deviation of the distribution:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(distribution_0_1.standard_deviation)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Get numerical range\nWe can access the range, ie. the difference between the numerical minimum and maximum,\nof the distribution:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(distribution_0_1.range)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Get mathematical support\nWe can access the range, ie. the difference between the minimum and maximum,\nof the distribution:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(distribution_0_1.support)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Generate samples\nWe can generate 10 samples of the distribution:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(distribution_0_1.compute_samples(10))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Compute CDF\nWe can compute the cumulative density function component per component\n(here the probability that the first component is lower than 0.\nand that the second one is lower than 1.)::\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(distribution_0_1.compute_cdf([0.0, 1.0]))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Compute inverse CDF\nWe can compute the inverse cumulative density function\ncomponent per component\n(here the quantile at 50% for the first component\nand the quantile at 97.5% for the second one):\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(distribution_0_1.compute_inverse_cdf([0.5, 0.975]))"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "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.9.13"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}