{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "density_discrete.ipynb\n", "\n", "Discussion: Sampling from a discrete distribution over a square\n", "\n", "Licensing: This code is distributed under the GNU LGPL license.\n", " \n", "Modified: 09 November 2016\n", "\n", "Author: John Burkardt, Lukas Bystricky" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Using matplotlib backend: agg\n" ] } ], "source": [ "# Import necessary libraries and set plot option\n", "%matplotlib\n", "%matplotlib inline\n", "%config InlineBackend.figure_format = 'svg'\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import math\n", "import scipy.spatial as spatial" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Sampling a discrete distribution #\n", "\n", "From our work on sampling and densities, we know that the sampling method\n", "is the key to whether the CVT is uniform or not. Rather than specifying\n", "a density function rho(x,y), we can modify our sampling technique, as\n", "we did in the circle example, and get a nonuniform result.\n", "\n", "We'd like to be able to model population density over a state. Populations\n", "are not defined by a function, but by chunks of data, that is, so many people\n", "live in this block, and so many in this block, and so on. Within the block,\n", "we might assume the population density is constant, but from block to block\n", "it must vary. \n", "\n", "In order to do a CVT, we would need to be able, say, to pick 10,000 people\n", "uniformly at random from the population; however, these people would be\n", "distributed nonuniformly with respect to geography. (We expect a lot\n", "of samples from Miami, but not many from the Everglades!)\n", "\n", "To understand how this can be done, we will look at an example that embodies\n", "the same problem, but with simple geometry and only a few sets of data.\n", "So we'll look at a square, which has been subdivided into squares, and for\n", "which the \"population\" of each subsquare is known.\n", "\n", "We will work out a fair way of sampling the population, plot a typical sample,\n", "and then work out how a CVT iteration could be set up for such a problem.\n", "If we understand this problem, then the only difference with Florida is \n", "a more complicated geometry and larger set of population data.\n", "\n", "We will look at:\n", "* reading the population data from a file\n", "* sampling from the population and plotting the sample\n", "* setting up a CVT for this toy problem." ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# Our discrete data #\n", "\n", "We suppose our \"state\" is a square in [0,5]x[0,5], divided into a 5x5 array of \n", "unit squares, which we can think of as 25 counties. We will find that indexing \n", "individual counties can be confusing, since sometimes we will want to order them \n", "by an index running 0 to 24, and other times by double indices running (0,0) to \n", "(4,4), and other times by the (X,Y) coordinates of the vertices of the corners\n", "of the county. Let us try to make sense out of this and agree on orderings.\n", " \n", "Here is how we will index the counties in linear order:\n", "\n", " 0 1 2 3 4\n", " 5 6 7 8 9\n", " 10 11 12 13 14\n", " 15 16 17 18 19\n", " 20 21 22 23 24\n", "\n", "Here is how we will index the counties in (I,J) or (row,column) order:\n", "\n", " (4,0) (4,1) (4,2) (4,3) (4,4)\n", " (3,0) (3,1) (3,2) (3,3) (3,4)\n", " (2,0) (2,1) (2,2) (2,3) (2,4)\n", " (1,0) (1,1) (1,2) (1,3) (1,4)\n", " (0,0) (0,1) (0,2) (0,3) (0,4)\n", "\n", "Unfortunately, the (X,Y) coordinates of the lower left corner of each county\n", "are in the reverse order, (J,I)!\n", "\n", " (0.0,4.0) (1.0,4.0) (2.0,4.0) (3.0,4.0) (4.0,4.0)\n", " (0.0,3.0) (1.0,3.0) (2.0,3.0) (3.0,3.0) (4.0,3.0)\n", " (0.0,2.0) (1.0,2.0) (2.0,2.0) (3.0,2.0) (4.0,2.0)\n", " (0.0,1.0) (1.0,1.0) (2.0,1.0) (3.0,1.0) (4.0,1.0)\n", " (0.0,0.0) (1.0,0.0) (2.0,0.0) (3.0,0.0) (4.0,0.0)\n", "\n", "Notice that county 13, with (I,J) coordinates (2,3) is inside the\n", "X,Y coordinate box with lower left corner (3.0,2.0) and upper right\n", "corner (4.0,3.0):\n", "\n", " (3.0,3.0)----(4.0,3.0)\n", " | |\n", " | |\n", " (3.0,2.0)----(4.0,2.0)\n", " \n", "which we could also describe as the box [3,4]x[2,3].\n", "\n", "Now we supply, for each county, a population, that is, the number of people\n", "who live there. Here's the data, arranged in the same ordering. We can imagine \n", "that the data is measured in thousands, so county 0 has 5,000 inhabitants.\n", "\n", " 5 5 2 2 2\n", " 3 3 2 2 2\n", " 3 3 5 5 5\n", " 1 2 5 5 10\n", " 1 2 5 10 10\n", "\n", "These numbers add up to 100, so we can see right away that our probability\n", "table, indicating how often we should pick a person from each county is:\n", "\n", " 0.05 0.05 0.02 0.02 0.02\n", " 0.03 0.03 0.02 0.02 0.02\n", " 0.03 0.03 0.05 0.05 0.05\n", " 0.01 0.02 0.05 0.05 0.10\n", " 0.01 0.02 0.05 0.10 0.10\n", " \n", "Now, we have seen this kind of problem before when sampling a triangle of\n", "a polygon before. What we need now is a running sum of probabilities, so\n", "that we can pick a random number R, and use that to choose at random the\n", "county from which to sample. If we \"read\" the counties in the linear order,\n", "then our running sum will look like this:\n", "\n", " 0.05 0.10 0.12 0.14 0.16\n", " 0.19 0.22 0.24 0.26 0.28\n", " 0.31 0.34 0.39 0.44 0.49\n", " 0.50 0.52 0.57 0.62 0.72\n", " 0.73 0.75 0.80 0.90 1.00\n", " \n", "So if our random value R was 0.3173, we would want to pick the county whose\n", "linear index is 11, with (row,column) index (2,1), and (x,y) coordinates\n", "(1.0,2.0) for the lower left corner of the box.\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Using discrete data to do sampling #\n", "\n", "Write a function that returns the population data as a list of 25 values;\n", "```python\n", "def pop_data ( ):\n", " pop = np.array ( [ ... ])\n", " return pop\n", "```\n", "\n", "Write a function that converts the population data into probabilities.\n", "```python\n", "def prob_data ( pop )\n", " prob = np.array ( [ ... ] )\n", " return prob\n", "```\n", "\n", "Write a function that returns the running sum of the probabilities.\n", "```python\n", "def cdf_data ( prob ):\n", " cdf = np.array ( [...])\n", " return cdf\n", "```\n", "\n", "Write a function that returns M random samples of the numbers from 0 to 24,\n", "using the CDF data. That is, this function simply picks a county, with\n", "a weight based on the population. If we ask for 100 samples, then we'd expect, \n", "for example, to see county 24 show up about 10 times in the list.\n", "```python\n", "def county_sample_data ( m ):\n", " county = np.zeros ( m )\n", " ???\n", " return county\n", "```\n" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Population data:\n", "\n", "[ 5 5 2 2 2 3 3 2 2 2 3 3 5 5 5 1 2 5 5 10 1 2 5 10 10]\n", "\n", "Probabilities:\n", "\n", "[ 0.05 0.05 0.02 0.02 0.02 0.03 0.03 0.02 0.02 0.02 0.03 0.03\n", " 0.05 0.05 0.05 0.01 0.02 0.05 0.05 0.1 0.01 0.02 0.05 0.1\n", " 0.1 ]\n", "\n", "CDF\n", "\n", "[ 0.05 0.1 0.12 0.14 0.16 0.19 0.22 0.24 0.26 0.28 0.31 0.34\n", " 0.39 0.44 0.49 0.5 0.52 0.57 0.62 0.72 0.73 0.75 0.8 0.9 1. ]\n", "\n", "County data (sorted)\n", "\n", "[ 0. 0. 0. 0. 0. 0. 1. 1. 2. 3. 3. 3. 3. 3. 4.\n", " 4. 4. 4. 4. 5. 5. 5. 6. 6. 6. 6. 6. 6. 6. 7.\n", " 7. 7. 7. 8. 9. 11. 11. 11. 12. 12. 12. 13. 13. 13. 13.\n", " 13. 14. 14. 14. 14. 14. 14. 15. 16. 16. 16. 17. 17. 17. 18.\n", " 18. 19. 19. 19. 19. 19. 19. 19. 19. 19. 19. 21. 21. 22. 22.\n", " 22. 23. 23. 23. 23. 23. 23. 23. 23. 23. 23. 23. 23. 23. 23.\n", " 23. 24. 24. 24. 24. 24. 24. 24. 24. 24.]\n" ] } ], "source": [ "# Code using discrete dagta to do sampling\n", "#\n", "# Write a function that returns the population data as a list of 25 values;\n", "#\n", "def pop_data ( ):\n", " pop = np.array ( [ \\\n", " 5, 5, 2, 2, 2,\\\n", " 3, 3, 2, 2, 2,\\\n", " 3, 3, 5, 5, 5,\\\n", " 1, 2, 5, 5,10,\\\n", " 1, 2, 5,10,10 ] )\n", " return pop\n", "\n", "pop = pop_data ( )\n", "print ( '' )\n", "print ( 'Population data:' )\n", "print ( '' )\n", "print ( pop )\n", "#\n", "# Write a function that converts the population data into probabilities.\n", "#\n", "def prob_data ( pop ):\n", " pop_total = np.sum ( pop )\n", " prob = pop.copy () / float ( pop_total )\n", " return prob\n", "\n", "prob = prob_data ( pop )\n", "print ( '' )\n", "print ( 'Probabilities:')\n", "print ( '' ) \n", "print ( prob )\n", " \n", "#\n", "# Write a function that returns the running sum of the probabilities.\n", "#\n", "def cdf_data ( prob ):\n", " cdf = prob.copy ()\n", " for i in range ( 1, len ( cdf ) ):\n", " cdf[i] = cdf[i] + cdf[i-1]\n", " return cdf\n", "\n", "cdf = cdf_data ( prob )\n", "print ( '' ) \n", "print ( 'CDF:' )\n", "print ( '' ) \n", "print ( cdf )\n", "#\n", "# Write a function that returns M random samples of the numbers from 0 to 24,\n", "# using the CDF data. That is, this function simply picks a county, with\n", "# a weight based on the population. If we ask for 100 samples, then we'd expect, \n", "# for example, to see county 24 show up about 10 times in the list.\n", "#\n", "def county_sample_data ( m ):\n", " county = -1 * np.ones( m )\n", " for j in range ( 0, m ):\n", " r = np.random.rand ( )\n", " for i in range ( 0, 25 ):\n", " if ( r < cdf[i] ):\n", " county[j] = i\n", " break\n", " return county\n", "\n", "m = 100\n", "county = county_sample_data ( m )\n", "county.sort()\n", "print ( '' )\n", "print ( 'County data (sorted)')\n", "print ( '' )\n", "print ( county )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Pick a random person from the random county #\n", "\n", "Suppose we wanted to pick a single person uniformly at random from the 100,000\n", "people in the state? \n", "\n", "Step 1 is to pick a county at random. We did that with the county_sample_data()\n", "function. But once we have a county, we have to pick a person. Suppose, for\n", "instance, that we have picked county 13, which we know is in the box with\n", "corners (3,2) and (4,3).\n", "\n", "Assuming we have no better information, we simply want to pick a random point in\n", "this box as the site of this random person. So we have to pick random r1 and r2\n", "between 0 and 1, and rescale them to an x and a y in the box.\n", "\n", " x = 3 + ( 4 - 3 ) * r1\n", " y = 2 + ( 3 - 2 ) * r2\n", " \n", "So to do this in general, we need to be able to go from a county index, such\n", "as 13, to the row and column indices, (2,3), to the coordinates of the lower\n", "left and upper right corners of the box, (3.0,2.0) and (4.0,3.0). \n", "\n", "Given a county index, such as 13, produce the row and column of the county,\n", "(2,3) in this case, where the rows go from 4 down to 0, and the columns \n", "rise from 0 to 4:\n", "```python\n", "def county_row_column ( index )\n", " ???\n", " return row, column\n", "```\n", "Given a county index, such as 13, produce the coordinates \n", "of the lower left corner (x1,y1) and the upper right corner (x2,y2) of the county,\n", "which in this case would be (3.0,2.0) and (4.0,3.0):\n", "```python\n", "def county_corners ( row, col ):\n", " ???\n", " return x1, y1, x2, y2\n", "```\n", "\n", "Given a county index, such as 13, produce a random point inside that county,\n", "which might be (3.14,2.79)\n", "```python\n", "def county_sample ( index )\n", " ???\n", " return x, y\n", "```" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", " For index 13, row = 2, column = 3\n", " For index 13, lower left corner = (3,2), upper right = (4,3)\n", "\n", "Five sample points from county 13:\n", " (3.44768,2.41223)\n", " (3.27209,2.40767)\n", " (3.76894,2.43551)\n", " (3.96072,2.49167)\n", " (3.52386,2.47286)\n" ] } ], "source": [ "# Code to pick a random person\n", "\n", "def county_row_column ( index ):\n", " k = 0\n", " for row in range ( 4, -1, -1 ):\n", " for column in range ( 0, 5 ):\n", " if ( k == index ):\n", " return row, column\n", " k = k + 1\n", " return row, column\n", "\n", "index = 13\n", "row, column = county_row_column ( index )\n", "print ( '' )\n", "print ( ' For index %d, row = %d, column = %d' % ( index, row, column ) )\n", " \n", "def county_corners ( index ):\n", " row, column = county_row_column ( index )\n", " x1 = float ( column )\n", " y1 = float ( row )\n", " x2 = x1 + 1.0\n", " y2 = y1 + 1.0\n", " return x1, y1, x2, y2\n", "\n", "x1, y1, x2, y2 = county_corners ( index )\n", "print ( ' For index %d, lower left corner = (%g,%g), upper right = (%g,%g)' \\\n", " % ( index, x1, y1, x2, y2 ) )\n", "\n", "def county_sample ( index ):\n", " x1, y1, x2, y2 = county_corners ( index )\n", " r1 = np.random.rand ( )\n", " r2 = np.random.rand ( )\n", " x = x1 + ( x2 - x1 ) * r1\n", " y = y1 + ( y2 - y1 ) * r2\n", " return x, y\n", "\n", "print ( '' )\n", "print ( 'Five sample points from county 13:')\n", "for i in range ( 0, 5 ):\n", " x, y = county_sample ( index )\n", " print ( ' (%g,%g)' % ( x, y ) )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Compute and plot a random sample #\n", "\n", "So to pick the location of a random person in the state, according to\n", "the discrete density information we were given to start with, we need\n", "to first select uniformly at random the county, and then select uniformly\n", "at random a location in the county.\n", "\n", "Compute 1,000 samples from the state, and plot them." ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Code to compute and plot a random sample\n", "\n", "m = 1000\n", "s = np.zeros ( [ m, 2 ] )\n", "c = county_sample_data ( m )\n", "\n", "for i in range ( 0, m ):\n", " index = c[i]\n", " x, y = county_sample ( index )\n", " s[i,:] = [ x, y ]\n", "\n", "plt.plot ( s[:,0], s[:,1], 'ro' )\n", "plt.axis ( 'Equal' )\n", "plt.show ( )" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.8" } }, "nbformat": 4, "nbformat_minor": 0 }