Книга: A Common-Sense Guide to Data Structures and Algorithms in Python, Volume 2 (for True Epub)
Назад: Monte Carlo Algorithms vs. Las Vegas Algorith ms
Дальше: Primality Testing

Obtaining Averages Through Random Sampling

You’ve seen how random sampling is used in the world of pollsters. Let’s now take a look at how we can use random sampling within the world of code. We’ll begin with some examples of computing averages.

Approximating the Mean

Say that we have an unsorted array containing a whole bunch of integers. To calculate the mean of these numbers, we first compute the sum of all N integers and then divide by N. Because we have to add up all N numbers, this algorithm takes roughly N steps.

With random sampling, though, we could calculate an approximate mean by picking a random sample of integers from the array and then computing the mean of that sample.

So, if our array contained 1,000,000 elements, getting the true mean would take about 1,000,000 steps. However, we could instead choose to add up 1,000 randomly chosen integers from the array and then divide that number by 1,000. This would be the sample mean. It would be less accurate than the true mean, but we’d only have to perform 1,000 steps instead of 1,000,000 steps.

Again, a proper statistical analysis needs to be done to determine if this approximate mean is good enough for your needs. But if it is, you can save a lot of time. This is especially true if your data is in the billions or trillions of elements.

For fun, though, let’s play around with this idea. Here goes:

 import​ ​random
 
 array = []
 
 for​ i ​in​ range(1000001):
  array.append(i)
 
 random.shuffle(array)
 
 sum = 0
 random_sample_size = 500
 
 for​ _ ​in​ range(random_sample_size):
  random_index = random.randint(0, 1000000)
  sum += array[random_index]
 
 mean = sum // random_sample_size
 
 print​(mean)

We first build an array of the integers 0 through 1,000,000 in random order. The mean of these numbers is basically 500,000.

Next, we randomly select 500 integers from the array and compute the mean of those 500 numbers. In effect, we’re computing the mean of our random sample.

When I run this code repeatedly, I get results like 501554, 498627, 499450, 506332, 477290, 505872, 489558, and 496410. These are all pretty close to the true mean of 500000!

You can play around with the random_sample_size variable, which chooses the size of our random sample. Even when I reduce this to 100, the results aren’t far off. I think it’s pretty cool to see in action how close random sampling comes to the ultimate truth.

Approximating the Median

We can use a similar approach to approximate the median of an array of numbers. As a reminder, the median of a list of values is the middle-most value if the values were to be sorted. In the array, [22, 56, 88, 89, 154, 207, 365], 89 is the median since it’s smack in the middle. This would be the case even if the array were shuffled; the point is that the median is the center value once the values are ordered.

If there are an even number of values, there’s no single value in the center. Rather, there are two values in the center. When this is the case, the median is the mean of those two values.

In the following code, I use random sampling to approximate the median of the numbers 0 through 1,000,000. The median in this case happens to be the same as the mean—it’s 500,000. Note that the approach is similar to what we did for approximating the mean:

 import​ ​random
 
 array = []
 
 for​ i ​in​ range(1000001):
  array.append(i)
 
 random.shuffle(array)
 
 sum = 0
 random_sample_size = 501
 random_sample = []
 
 for​ _ ​in​ range(random_sample_size):
  random_index = random.randint(0, 1000000)
  random_sample.append(array[random_index])
 
 random_sample.sort()
 median = random_sample[random_sample_size // 2]
 
 print​(median)

Here, we grab a random sample of 501 elements (I made the sample size odd to keep things simple). We then compute the median of the random sample by first sorting the random sample and then grabbing the value at the center index, which is random_sample_size // 2.

When I run this code, I also get results that are close to the truth, although not as close as when I approximated the mean. Try it out for yourself!

Назад: Monte Carlo Algorithms vs. Las Vegas Algorith ms
Дальше: Primality Testing