{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Hughes and Hase Problem 3.7\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": [ "#### Data" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "58\n" ] } ], "source": [ "counts = np.array([1,3,4,5,6,7,8,9,10,11,12,13])\n", "occurences = np.array([1,2,3,6,9,11,8,8,6,2,1,1])\n", "\n", "n_expts = np.sum(occurences) # check \n", "print(n_expts)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### (i) Total number of counts" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "total # of counts = 423\n" ] } ], "source": [ "total = occurences@counts # Dot product \n", "print(\"total # of counts =\", total)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### (ii) Mean number of counts\n", "\n", "This is the best estimate of the mean of the parent Poisson distribution." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "mean counts/second = 7.293103448275862\n" ] } ], "source": [ "mean = total/n_expts\n", "print('mean counts/second =', mean)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### (i) Expected # of trials with 5 or fewer counts¶\n", "\n", "Use the cumulative distribution function for the Poisson distribution" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "probability of getting 5 or fewer counts = 0.2648489299548199\n", "expected number of trials with 5 or fewer counts = 15.361237937379554\n" ] } ], "source": [ "probability1 = stats.poisson.cdf(5, mean)\n", "print(\"probability of getting 5 or fewer counts =\", probability1)\n", "n_expected1 = n_expts*probability1\n", "print(\"expected number of trials with 5 or fewer counts =\", n_expected1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### (ii) Expected # of trials with 20 or more counts¶\n", "Use the cumulative distribution function to find the probability of 19 or fewer counts. \n", "The probability of 20 or more is (1 - the probability of 19 or fewer)." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "probability of getting 20 or more counts = 7.674243593669416e-05\n", "expected number of trials with 20 or more counts = 0.0044510612843282615\n" ] } ], "source": [ "probability2 = 1 - stats.poisson.cdf(19, mean)\n", "print(\"probability of getting 20 or more counts =\", probability2)\n", "n_expected2 = n_expts*probability2\n", "print(\"expected number of trials with 20 or more counts =\", n_expected2)" ] }, { "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 . If not already installed on your machine, run `pip install --upgrade version_information` from the terminal\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "%load_ext version_information" ] }, { "cell_type": "code", "execution_count": 8, "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 13:57:21 2025 Eastern Standard Time |