CSci 150: Foundations of computer science I
Home Syllabus Assignments Tests

Assignment 1: Polls

Due: 9:00am, Monday, February 1. Value: 30 pts. Submit your saved file using the Sauron Submission System. [instructions]

Note: You will be assigned to work with another student on this assignment, and you should submit your solution to the assignment together. Unless you're told otherwise, working with another student will be optional for all future assignments in this course.

We'll be using Wing 101 IDE in this course. On the laboratory computers in MCReynolds 314, you can go to the Start menu, then into All Programs, then Computer Science, then Wing IDE. [Installation instructions if you want to install it on another computer]

During campaign season, news media often report on polls of how people are expected to vote in the upcoming election. When they report the polling results, they include a margin of error such as ±3%. In this assignment, we'll explore this error estimate by simulate many polls and computing their accuracy.

To simulate a poll, we'll use Python's built-in module random for generating pseudorandom numbers. In particular, the random module includes a function randrange which takes an integer parameter k and computes a random number between 0 and k − 1, inclusive. We'll imagine our campaign has only two candidates, so we'll always use 2 for k, so that the function returns either 0 or 1.

The following fragment illustrates how one might use randrange to simulate a poll of 100 people.

import random

count1 = 0
for person in range(100):
    vote = random.randrange(2)
    count1 = count1 + vote
print 'The sample contained', count1, 'votes of 1'

Notice how this works: We initialize count1 to 0, and then we have a defined loop that iterates through 100 people. For each person, we find the person's simulated vote by using random.randrange(2) and saving that in vote. Then we add that vote into count1: If the person voted 0, then count1 remains unchanged, but if the person voted 1, then count1 becomes 1 more than it was before. By the end of the defined loop, then, count1 contains the number of our 100 people who voted 1.

Part 1: A single poll

Write a function run_poll that takes a parameter sample_size, simulates a poll of sample_size people, and displays the percentage of the sample who voted 1. Your solution will be very similar to the above fragment, but the number of people in your sample should vary according to the parameter rather than always using exactly 100 people. Note that you should display the percentage of people who voted 1. Thus, if you execute run_poll(4), it should display one of 0.0%, 25.0%, 50.0%, 75.0%, or 100.0%, depending on the particular random numbers found. (It will most frequently be 50.0%.)

Watch out for integer division! One way to test for this is to try run_poll(3) a few times: If it displays 33% or 66%, then you're using integer division and thus getting erroneous results. It should be 33.3333333333% or 66.6666666667%.

Complete and test your run_poll function now before going on to the rest of the assignment.

Part 2: Multiple polls

Continue to use the same file you developed in Part 1, although you will not modify or use the run_poll function. In the end, you should have a single file that contains answers to both parts of the assignmens.

Write a function run_polls that takes a parameter sample_size, simulates 200 polls of sample_size people each, and displays the computed margin of error across those polls. To compute the margin of error, you can use the following formula:

Here, n is the number of polls taken, xi is the outcome of the ith poll, and xexp is the expected outcome of the poll (it is 50 here, since randrange returns 1 half the time, or 50%). For example, if we ran 3 polls, each with 10 people, and the outcomes were 60%, 30%, and 40%, then we'd first find the sum of (60 − 50)² + (30 − 50)² + (40 − 50)², which turns out to be 600. Dividing by 3, we get 200. The square root of 200 is about 14.1, which we multiply by 1.96 to arrive at a margin of error of about 27.7%.

An outline of how your solution will look is the following.

def run_polls(sample_size):
    # Initialize a variable to be 0; this will hold the sum of squares
    for poll in range(200):
        # Your code from Part 1, to compute percentage of a poll.
        # Then add (percentage - 50) ** 2 into your sum-of-squares variable.
    # Now compute and display 1.96 sqrt(sum-of-squares / 200)

Because your code from Part 1 includes a defined loop, this program will actually contain a loop within a loop.

(You should find that run_polls(1000) gives you a margin of error of around 3%. News organizations often report polls with an error of 3%, indicating that they polled 1,000 likely voters.)

After you have completed the assignment, you should submit your file via the Sauron Submission System. [instructions]