{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import random" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "range_end = 26\n", "max_count = 1_000_000" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "\n", "def make_barchart(title, random_ints, show_line=False):\n", " \"\"\"Draw a frequency barchart.\"\"\"\n", " values, frequencies = np.unique(random_ints, return_counts=True)\n", " \n", " plt.figure(figsize=(10, 4))\n", " sns.set_style('whitegrid')\n", " \n", " full_values = [] # values starting with 0 without any missing\n", " full_freqs = [] # frequencies of the full values\n", " \n", " # Fill in missing values with 0 frequencies.\n", " index = 0\n", " for i in range(range_end):\n", " full_values += [i]\n", "\n", " if (index >= len(frequencies)) or (i < values[index]):\n", " full_freqs += [0] # missing value\n", " else:\n", " full_freqs += [frequencies[index]]\n", " index += 1 \n", " \n", " axes = sns.barplot(x=full_values, y=full_freqs, color='blue')\n", " \n", " # Show a line plot only if requested.\n", " if show_line:\n", " axes = sns.lineplot(x=full_values, y=full_freqs, color='red')\n", " \n", " axes.set_title(title)\n", " axes.set(xlabel='Value', ylabel='Frequency')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "\n", "def make_histogram(title, random_values):\n", " \"\"\"Make a histogram.\"\"\"\n", " plt.figure(figsize=(10, 4))\n", " sns.set_style('whitegrid')\n", " \n", " axes = sns.distplot(random_values)\n", " axes.set_title(title)\n", " axes.set(xlabel='Value', ylabel='Frequency')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "uniform_ints = [int(random.uniform(0, range_end)) for _ in range(max_count)]\n", "make_barchart('Uniform Distribution', uniform_ints)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "uniform_values = [random.uniform(0, range_end) for _ in range(max_count)]\n", "make_histogram('Uniform Distribution', uniform_values)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "normal_ints = []\n", "\n", "while len(normal_ints) < max_count:\n", " n = int(random.gauss(12.5, 3.3))\n", " if n in range(0, range_end):\n", " normal_ints += [n]\n", " \n", "title = 'Normal Distribution' + \", count = \" + str(max_count);\n", " \n", "values, frequencies = np.unique(normal_ints, return_counts=True)\n", "make_barchart(title, normal_ints, show_line=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "normal_values = [random.gauss(12.5, 3.3) for _ in range(max_count)]\n", "make_histogram(title, normal_values)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "expo_ints = []\n", "lambd = 1/12.5\n", "\n", "while len(expo_ints) < max_count:\n", " n = int(random.expovariate(lambd))\n", " if n < 2*range_end:\n", " expo_ints += [n]\n", " \n", "make_barchart('Exponential Distribution', expo_ints, show_line=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "expo_values = []\n", "lambd = 1/12.5\n", "\n", "while len(expo_values) < max_count:\n", " n = random.expovariate(lambd)\n", " if n < 2*range_end:\n", " expo_values += [n]\n", " \n", "make_histogram('Exponential Distribution', expo_values)" ] } ], "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.7.7" } }, "nbformat": 4, "nbformat_minor": 4 }