|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Rejection Sampling" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "Rejection sampling, or \"accept-reject Monte Carlo\" is a Monte Carlo method used to generate obsrvations from distributions. As it is a Monte Carlo it can also be used for numerical integration." |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "markdown", |
| 19 | + "metadata": {}, |
| 20 | + "source": [ |
| 21 | + "## Example: Approximation of $\\pi$" |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | + "cell_type": "markdown", |
| 26 | + "metadata": {}, |
| 27 | + "source": [ |
| 28 | + "Enclose a circle of radius $1$ in a square of side length $2$. Then uniformly sample points inside the bounds of the square in Cartesian coordinates. If the point lies inside the circle record this information. At the ends of many throws the ratio of points inside the circle to all points thrown will approximate the ratio of the area of the cricle to the area of the square" |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + "cell_type": "markdown", |
| 33 | + "metadata": {}, |
| 34 | + "source": [ |
| 35 | + "$$\n", |
| 36 | + "\\frac{\\text{points inside circle}}{\\text{all points thrown}} \\approx \\frac{\\text{area of circle}}{\\text{area of square}} = \\frac{\\pi r^2}{l^2},\n", |
| 37 | + "$$" |
| 38 | + ] |
| 39 | + }, |
| 40 | + { |
| 41 | + "cell_type": "markdown", |
| 42 | + "metadata": {}, |
| 43 | + "source": [ |
| 44 | + "thus, an approximation of $\\pi$ can be found to be" |
| 45 | + ] |
| 46 | + }, |
| 47 | + { |
| 48 | + "cell_type": "markdown", |
| 49 | + "metadata": {}, |
| 50 | + "source": [ |
| 51 | + "$$\n", |
| 52 | + "\\pi \\approx \\frac{l^2}{r^2} \\cdot \\frac{\\text{points inside circle}}{\\text{all points thrown}}.\n", |
| 53 | + "$$" |
| 54 | + ] |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "code", |
| 58 | + "execution_count": 1, |
| 59 | + "metadata": {}, |
| 60 | + "outputs": [], |
| 61 | + "source": [ |
| 62 | + "import numpy as np\n", |
| 63 | + "import matplotlib as plt" |
| 64 | + ] |
| 65 | + }, |
| 66 | + { |
| 67 | + "cell_type": "code", |
| 68 | + "execution_count": 2, |
| 69 | + "metadata": {}, |
| 70 | + "outputs": [], |
| 71 | + "source": [ |
| 72 | + "def approximate_pi(n_throws=1000):\n", |
| 73 | + " radius = 1\n", |
| 74 | + " side_length = 2\n", |
| 75 | + " \n", |
| 76 | + " n_circle_points = 0\n", |
| 77 | + "\n", |
| 78 | + " x_coord = np.random.uniform(-1, 1, n_throws)\n", |
| 79 | + " y_coord = np.random.uniform(-1, 1, n_throws)\n", |
| 80 | + "\n", |
| 81 | + " for x,y in zip(x_coord, y_coord):\n", |
| 82 | + " radius = np.sqrt(x**2 + y**2)\n", |
| 83 | + " if radius <=1:\n", |
| 84 | + " n_circle_points += 1\n", |
| 85 | + " \n", |
| 86 | + " approx_pi = (n_circle_points / n_throws) * side_length**2\n", |
| 87 | + " print('The approximation of pi after {} throws is: {}'.format(n_throws, approx_pi))" |
| 88 | + ] |
| 89 | + }, |
| 90 | + { |
| 91 | + "cell_type": "code", |
| 92 | + "execution_count": 3, |
| 93 | + "metadata": {}, |
| 94 | + "outputs": [ |
| 95 | + { |
| 96 | + "name": "stdout", |
| 97 | + "output_type": "stream", |
| 98 | + "text": [ |
| 99 | + "The approximation of pi after 1000 throws is: 3.128\n" |
| 100 | + ] |
| 101 | + } |
| 102 | + ], |
| 103 | + "source": [ |
| 104 | + "approximate_pi()" |
| 105 | + ] |
| 106 | + } |
| 107 | + ], |
| 108 | + "metadata": { |
| 109 | + "kernelspec": { |
| 110 | + "display_name": "Python [conda root]", |
| 111 | + "language": "python", |
| 112 | + "name": "conda-root-py" |
| 113 | + }, |
| 114 | + "language_info": { |
| 115 | + "codemirror_mode": { |
| 116 | + "name": "ipython", |
| 117 | + "version": 3 |
| 118 | + }, |
| 119 | + "file_extension": ".py", |
| 120 | + "mimetype": "text/x-python", |
| 121 | + "name": "python", |
| 122 | + "nbconvert_exporter": "python", |
| 123 | + "pygments_lexer": "ipython3", |
| 124 | + "version": "3.5.4" |
| 125 | + } |
| 126 | + }, |
| 127 | + "nbformat": 4, |
| 128 | + "nbformat_minor": 2 |
| 129 | +} |
0 commit comments