{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Lab 2-4: Monte Carlo Tests & Random Numbers\n", "\n", "## Generating Random Numbers from a Given Probability Distribution" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Given any distribution for which we have a CDF available as a closed form expression, or simple lookup table (made when we create an empirical CDF), we can use the following approach to generate random numbers from that distribution.\n", "\n", "A basic approach for generating a sample of n random numbers from a desired probability distribution:\n", "\n", "1. Generate n random numbers from a uniform distribution on the interval [0,1]. (e.g. by using `np.random.uniform`) Recall that a CDF always ranges from 0 to 1 for a set of numbers, this first set of numbers we've generated from a uniform distribution are quantile values.\n", "\n", "2. Map the quantile values we just generated to the quantile value from the CDF of the desired distribution. (e.g. as a lookup table, or with a function like [`np.quantile`](https://numpy.org/doc/stable/reference/generated/numpy.quantile.html#numpy.quantile))\n", "\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Start by importing the python libraries we'll need, and load our data file for the Skykomish river annual peak flow values." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import scipy.stats as st\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/opt/conda/lib/python3.8/site-packages/openpyxl/worksheet/_reader.py:312: UserWarning: Unknown extension is not supported and will be removed\n", " warn(msg)\n" ] }, { "data": { "text/html": [ "
\n", " | date of peak | \n", "water year | \n", "peak value (cfs) | \n", "gage_ht (feet) | \n", "
---|---|---|---|---|
0 | \n", "1928-10-09 | \n", "1929 | \n", "18800 | \n", "10.55 | \n", "
1 | \n", "1930-02-05 | \n", "1930 | \n", "15800 | \n", "10.44 | \n", "
2 | \n", "1931-01-28 | \n", "1931 | \n", "35100 | \n", "14.08 | \n", "