{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Drawing cards\n", "\n", "NOTE: In this notebook I use the `stats` sub-module of `scipy` for all statistics functions, including generation of random numbers. There are other modules with some overlapping functionality, e.g., the regular python random module, and the `scipy.random` module, but I do not use them here. The `stats` sub-module includes tools for a large number of distributions, it includes a large and growing set of statistical functions, and there is a unified class structure. (And namespace issues are minimized.) See https://docs.scipy.org/doc/scipy/reference/stats.html." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from scipy import stats" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Intro\n", "\n", "For each of the draws, the probability of getting a heart is $\\frac{1}{4}$. The probability of getting $k$ successes in $n$ independent trials, where the $p$ is the probability of success in an indvidual trial is given by the binomial distribution `stats.binom.pmf(k,n,p)`:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The probability of 0 hearts is 0.17797851562499994\n", "The probability of 1 hearts is 0.35595703125\n", "The probability of 2 hearts is 0.296630859375\n", "The probability of 3 hearts is 0.13183593749999992\n", "The probability of 4 hearts is 0.03295898437499997\n", "The probability of 5 hearts is 0.004394531250000001\n", "The probability of 6 hearts is 0.000244140625\n" ] } ], "source": [ "for k in range(7):\n", " n = 6\n", " p = 0.25\n", " print(\"The probability of\",k,\"hearts is\", stats.binom.pmf(k,n,p))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Version information\n", "`version_information` is from J.R. Johansson (jrjohansson at gmail.com); see Introduction to scientific computing with Python for more information and instructions for package installation.\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "%load_ext version_information" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "application/json": { "Software versions": [ { "module": "Python", "version": "3.11.5 64bit [MSC v.1916 64 bit (AMD64)]" }, { "module": "IPython", "version": "8.15.0" }, { "module": "OS", "version": "Windows 10 10.0.26100 SP0" }, { "module": "scipy", "version": "1.11.1" }, { "module": "matplotlib", "version": "3.7.2" } ] }, "text/html": [ "
Software | Version |
---|---|
Python | 3.11.5 64bit [MSC v.1916 64 bit (AMD64)] |
IPython | 8.15.0 |
OS | Windows 10 10.0.26100 SP0 |
scipy | 1.11.1 |
matplotlib | 3.7.2 |
Sat Feb 08 14:44:35 2025 Eastern Standard Time |