{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1 align=\"center\">Random module</h1>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from random import seed, randrange\n",
"\n",
"seed()\n",
"for i in range(20):\n",
" print('{:<4d}'.format(randrange(0, 100, 5)), end = '')\n",
"print()\n",
"for n in range(2):\n",
" print()\n",
" seed(0)\n",
" for i in range(20):\n",
" print('{:<4d}'.format(randrange(0, 100, 5)), end = '')\n",
" print()\n",
" seed(1)\n",
" for i in range(20):\n",
" print('{:<4d}'.format(randrange(0, 100, 5)), end = '')\n",
" print()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"from matplotlib.pyplot import bar, xticks\n",
"\n",
"from random import randint, randrange, choice\n",
"\n",
"counts = [0] * 10\n",
"for i in range(100):\n",
" counts[randint(0, 9)] += 1\n",
"bar(range(10), counts, 0.2)\n",
"counts = [0] * 10\n",
"for i in range(100):\n",
" counts[randrange(0, 10)] += 1\n",
"bar([0.2 + i for i in range(10)], counts, 0.2, color = 'r')\n",
"counts = [0] * 10\n",
"for i in range(100):\n",
" counts[choice(range(0, 10))] += 1\n",
"bar([0.4 + i for i in range(10)], counts, 0.2, color = 'y')\n",
"xticks([i + 0.4 for i in range(10)], range(0, 10))\n",
"print()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from random import random, getstate, setstate\n",
"\n",
"for i in range(5):\n",
" print(random())\n",
"state = getstate()\n",
"print()\n",
"\n",
"for i in range(5):\n",
" print(random())\n",
"print()\n",
"\n",
"setstate(state)\n",
"for i in range(5):\n",
" print(random())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from random import getrandbits\n",
"\n",
"for k in range(1, 6):\n",
" for i in range(20):\n",
" print('{:<4d}'.format(getrandbits(k)), end = '')\n",
" print()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from random import shuffle\n",
"\n",
"L = list(range(6))\n",
"\n",
"for i in range(10):\n",
" shuffle(L)\n",
" print(L)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from random import sample\n",
"\n",
"for k in range(1, 6):\n",
" for i in range(5):\n",
" print(sample(range(100), k))\n",
" print()\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Uniform density function $\\frac{x}{b - a}$ over $[a,b]$ illustrated with $a=-1$ and $b=1$"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"from matplotlib.pyplot import hist, gca\n",
"from matplotlib.ticker import FuncFormatter\n",
"\n",
"from random import uniform\n",
"\n",
"nb_of_values = 100000\n",
"generated_values = []\n",
"for i in range(nb_of_values):\n",
" generated_values.append(uniform(-1, 1))\n",
" \n",
"def to_percent(x, _):\n",
" return str(x / nb_of_values * 100) + '%'\n",
"\n",
"gca().yaxis.set_major_formatter(FuncFormatter(to_percent))\n",
"hist(generated_values, 20)\n",
"print()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Normal density function $\\frac{1}{\\sqrt(2\\pi\\sigma)}e^{\\frac{-(x-\\mu)^2}{2\\sigma^2}}$ illustrated with $\\mu=15$ and $\\sigma=8$"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"from matplotlib.pyplot import hist, gca\n",
"from matplotlib.ticker import FuncFormatter\n",
"\n",
"from random import gauss\n",
"# Alternatively: from random import normalvariate\n",
"\n",
"nb_of_values = 100000\n",
"generated_values = []\n",
"for i in range(nb_of_values):\n",
" generated_values.append(gauss(15, 8))\n",
"# Alternatively: generated_values.append(normalvariate(15, 8))\n",
" \n",
"def to_percent(x, position):\n",
" return str(x / nb_of_values * 100) + '%'\n",
"\n",
"#gca().yaxis.set_major_formatter(FuncFormatter(to_percent))\n",
"hist(generated_values, 100)\n",
"print()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Log normal density function, that is, the density function of the random variable $e^X$ where $X$ is a normal random variable, illustrated with $\\mu_X=10000$ and $\\sigma_X=2$"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"from matplotlib.pyplot import hist, gca, semilogx\n",
"from matplotlib.ticker import FuncFormatter\n",
"\n",
"from random import lognormvariate\n",
"from math import exp, log\n",
"\n",
"nb_of_values = 100000\n",
"generated_values = []\n",
"for i in range(nb_of_values):\n",
" generated_values.append(lognormvariate(log(10000), 2))\n",
"minimal_value = min(generated_values)\n",
"maximal_value = max(generated_values)\n",
"\n",
"def to_percent(x, _):\n",
" return str(x / nb_of_values * 100) + '%'\n",
"\n",
"gca().set_xscale('log')\n",
"gca().yaxis.set_major_formatter(FuncFormatter(to_percent))\n",
"hist(generated_values, [exp((log(maximal_value) - log(minimal_value)) * i / 100)\n",
" for i in range(101)])\n",
"print()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Exponential density function $\\lambda e^{-\\lambda x}$ illustrated with $\\lambda=7$"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"from matplotlib.pyplot import hist, gca, text\n",
"from matplotlib.ticker import FuncFormatter\n",
"\n",
"from random import expovariate\n",
"\n",
"nb_of_values = 100000\n",
"generated_values = []\n",
"for i in range(nb_of_values):\n",
" # Normal distribution of mean 15 and standard deviation 8\n",
" generated_values.append(expovariate(7))\n",
" \n",
"def to_percent(x, _):\n",
" return str(x / nb_of_values * 100) + '%'\n",
"\n",
"gca().yaxis.set_major_formatter(FuncFormatter(to_percent))\n",
"hist(generated_values, 50)\n",
"text(1, 10000, 'Mean: $\\\\frac{1}{7}\\\\approx 0.14$')\n",
"print()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Given $a\\leq b$ and $c\\in[a,b]$ (the *mode*), the density function of the *triangular* distribution over $[a,b]$ is equal to:\n",
"* $\\frac{2(x-a)}{(b-a)(c-a)}$ if $a<x<c$,\n",
"* $\\frac{2}{b-a}$ if $x=c$,\n",
"* $\\frac{2(b-x)}{(b-a)(b-c)}$ if $c<x<b$.\n",
"\n",
"It is illustrated with $a=1$, $b=4$ and $c=3$."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"from matplotlib.pyplot import hist, gca\n",
"from matplotlib.ticker import FuncFormatter\n",
"\n",
"from random import triangular\n",
"\n",
"nb_of_values = 100000\n",
"generated_values = []\n",
"for i in range(nb_of_values):\n",
" generated_values.append(triangular(1, 4, 3))\n",
" \n",
"def to_percent(x, _):\n",
" return str(x / nb_of_values * 100) + '%'\n",
"\n",
"gca().yaxis.set_major_formatter(FuncFormatter(to_percent))\n",
"hist(generated_values, 20)\n",
"print()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Given $\\alpha>0$, the density function of the *Pareto* distribution of parameter $\\alpha$, defined over $[1,\\infty)$, is equal to $\\frac{\\alpha}{x^{\\alpha+1}}$.\n",
"It is illustrated with $\\alpha=10000$."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"from matplotlib.pyplot import hist, gca\n",
"from matplotlib.ticker import FuncFormatter\n",
"\n",
"from random import paretovariate\n",
"\n",
"nb_of_values = 100000\n",
"generated_values = []\n",
"for i in range(nb_of_values):\n",
" generated_values.append(paretovariate(10000))\n",
" \n",
"def to_percent(x, _):\n",
" return str(x / nb_of_values * 100) + '%'\n",
"\n",
"def literal(x, _):\n",
" return '{:.4f}'.format(x)\n",
"\n",
"gca().yaxis.set_major_formatter(FuncFormatter(to_percent))\n",
"gca().xaxis.set_major_formatter(FuncFormatter(literal))\n",
"\n",
"hist(generated_values, 50)\n",
"print()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Given $\\alpha>0$ and $\\beta>0$, the density function of the *beta* distribution of parameters $\\alpha$ and $\\beta$, defined over $[0,1]$, is equal to $\\frac{x^{\\alpha-1}(1-x)^{\\beta-1}}{\\int_0^1x^{\\alpha-1}(1-x)^{\\beta-1}dx}$. It is illustrated with $\\alpha=4$ and $\\beta=2.5$."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"from matplotlib.pyplot import hist, gca\n",
"from matplotlib.ticker import FuncFormatter\n",
"\n",
"from random import betavariate\n",
"\n",
"nb_of_values = 100000\n",
"generated_values = []\n",
"for i in range(nb_of_values):\n",
" generated_values.append(betavariate(4, 2.5))\n",
" \n",
"def to_percent(x, _):\n",
" return str(x / nb_of_values * 100) + '%'\n",
"\n",
"gca().yaxis.set_major_formatter(FuncFormatter(to_percent))\n",
"hist(generated_values, 50)\n",
"print()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Given $\\alpha>0$ and $\\beta>0$, the density function of the *gamma* distribution of parameters $\\alpha$ and $\\beta$, defined over $(0,\\infty)$, is equal to $\\frac{\\beta^\\alpha}{\\Gamma(\\alpha)}x^{\\alpha-1}e^{-\\beta x}$. It is illustrated with $\\alpha=4$ and $\\beta=2.5$."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"from matplotlib.pyplot import hist, gca\n",
"from matplotlib.ticker import FuncFormatter\n",
"\n",
"from random import gammavariate\n",
"\n",
"nb_of_values = 100000\n",
"generated_values = []\n",
"for i in range(nb_of_values):\n",
" generated_values.append(gammavariate(4, 2.5))\n",
" \n",
"def to_percent(x, _):\n",
" return str(x / nb_of_values * 100) + '%'\n",
"\n",
"gca().yaxis.set_major_formatter(FuncFormatter(to_percent))\n",
"hist(generated_values, 50)\n",
"print()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Given $\\mu\\in[0,2\\pi)$ and $\\kappa>0$, the density function of the *von Mises* (or *circular normal*, or *Tikhonov*) distribution of parameters $\\mu$ and $\\kappa$, defined over $[0,2\\pi]$, is equal to $\\frac{e^{\\kappa\\cos(x-\\mu)}}{2\\pi\\Sigma_{m=0}^\\infty\\frac{1}{m!\\Gamma(m+\\alpha+1)}(\\frac{x}{2})^{2m+\\alpha}}$. It is illustrated with $\\mu=\\pi$ and $\\kappa=4$."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"from matplotlib.pyplot import hist, xlim, gca\n",
"from matplotlib.ticker import FuncFormatter\n",
"\n",
"from random import vonmisesvariate\n",
"from math import pi\n",
"\n",
"nb_of_values = 100000\n",
"generated_values = []\n",
"for i in range(nb_of_values):\n",
" generated_values.append(vonmisesvariate(pi, 4))\n",
" \n",
"def to_percent(x, _):\n",
" return str(x / nb_of_values * 100) + '%'\n",
"\n",
"xlim(0, 2 * pi)\n",
"gca().yaxis.set_major_formatter(FuncFormatter(to_percent))\n",
"hist(generated_values, 50)\n",
"print()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Given $\\lambda>0$ and $\\kappa>0$, the density function of the *Weibull* distribution of parameters $\\lambda$ and $\\kappa$, defined over $[0,\\infty)$, is equal to $\\frac{\\kappa}{\\lambda}(\\frac{x}{\\lambda})^{k-1}e^{-(\\frac{x}{\\lambda})^\\kappa}$. It is illustrated with $\\lambda=1$ and $\\kappa=1.5$."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"from matplotlib.pyplot import hist, gca\n",
"from matplotlib.ticker import FuncFormatter\n",
"\n",
"from random import weibullvariate\n",
"\n",
"nb_of_values = 100000\n",
"generated_values = []\n",
"for i in range(nb_of_values):\n",
" generated_values.append(weibullvariate(1, 1.5))\n",
" \n",
"def to_percent(x, _):\n",
" return str(x / nb_of_values * 100) + '%'\n",
"\n",
"gca().yaxis.set_major_formatter(FuncFormatter(to_percent))\n",
"hist(generated_values, 50)\n",
"print()"
]
}
],
"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.5.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Resource created Wednesday 14 October 2015, 02:39:50 PM.
file: random_module.ipynb