{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Probability distributions based on SciPy\n\nIn this example,\nwe seek to create a probability distribution based on the SciPy library.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from __future__ import annotations\n\nfrom gemseo import configure_logger\nfrom gemseo.uncertainty import create_distribution\nfrom gemseo.uncertainty import get_available_distributions\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()\nall_distributions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and filter the ones based on the SciPy library\n(their names start with the acronym 'SP'):\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "sp_distributions = get_available_distributions(\"SPDistribution\")\nsp_distributions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create a distribution\nThen,\nwe can create a probability distribution for a two-dimensional random variable\nwith independent components that follow a normal distribution.\n\n### Case 1: the SciPy distribution has a GEMSEO class\nFor 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\", \"SPNormalDistribution\", 2)\ndistribution_0_1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For a normal with mean = 1 and standard deviation = 2:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "distribution_1_2 = create_distribution(\n \"x\", \"SPNormalDistribution\", 2, mu=1.0, sigma=2.0\n)\ndistribution_1_2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Case 2: the SciPy distribution has no GEMSEO class\nWhen GEMSEO does not offer a class for the SciPy distribution,\nwe can use the generic GEMSEO class :class:`.SPDistribution`\nto create any SciPy distribution\nby setting ``interfaced_distribution`` to its SciPy name\nand ``parameters`` as a dictionary of SciPy parameter names and values\n([see the documentation of SciPy](https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.norm.html)_).\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "distribution_1_2 = create_distribution(\n \"x\",\n \"SPDistribution\",\n 2,\n interfaced_distribution=\"norm\",\n parameters={\"loc\": 1.0, \"scale\": 2.0},\n)\ndistribution_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()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
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.