COMP 200: Elements of Computer Science
Spring 2013

Assignment 2

Put all your answers in one CodeSkulptor file. Use Python comments to clearly mark each problem number.

Work in assigned pairs on this assignment. Submit it on OWL-Space, under Assignment 2. Only the FIRST person listed in the pair should submit the Codeskulptor URL. Put the pair designation as posted, (NetID1, NetID2), at the top of your submission as well as in Python comments at the top of your CodeSkulptor code. Including the person's actual names too would be nice.

As a matter of good style and conciseness, wherever reasonable you should use functions that have been defined previously on this assignment, on a previous assignment, or in class.

Be sure to read the course policies.

Statistics (64 points total)

In engineering, natural science, and social science, it is very common to generate or collect sample data and analyze it statistically. Statistical analysis has also been creeping into the humanities. Python does not have statistical tools built-in, but we can define many such tools with simple functions on lists.

  1. (8 points)

    The arithmetic mean, or average, of a collection of numbers is the sum of the numbers divided by the count of numbers.

    E.g., arithmetic_mean([3, 7, 1, 2, 10]) should return 4.6, since (3 + 7 + 1 + 2 + 10) / 5 = 4.6.

    Define a function arithmetic_mean() that takes a non-empty list of numbers and returns its arithmetic mean. If the input is an empty list, it should return None.

    Define this function. You can define it without a loop by using appropriate built-in operations.

  2. (15 points)

    The variance of a collection of numbers is the mean of the squared deviation (difference) of each number from the collection's mean. Note that the variance is also the square of the standard deviation of the numbers.

    E.g., variance([3, 7, 1, 2, 10]) should return 11.44, since that is the mean of [(3-4.6)2, (7-4.6)2, (1-4.6)2, (2-4.6)2, (10-4.6)2]. If the input is an empty list, it should return None.

    Define this function. Clearly, you should use your previous arithmetic_mean function in this definition.

  3. (8 points)

    We've seen the built-in function sum() to sum all the numbers of a list. We would now like a more flexible version that also takes non-negative starting and ending indices for the part of the list to add.

    E.g., sum_list([5, 9, 2, -3, 8, 1], 2, 4) should return -1, since the list elements beginning with index 2 and ending before index 4 are [2, -3], and 2 + -3 = -1. The function should print an error message and return None if either index argument is out of range.

    Define this function. You can define it in without a loop by using appropriate built-in operations.

  4. (8 points)

    The lower median of a collection of numbers is, intuitively, the “middle” number when looking at the numbers in order. The “lower” part refers to using the first of the middle two numbers when we have an even-sized collection. The median is often used instead of the better-known arithmetic mean, as it is less affected by having a few extreme values.

    E.g., lower_median([5, 2, 3]) should return 3, since 3 is in the middle of the sorted list [2, 3, 5]. E.g., lower_median([5, 2, 8, 3]) should also return 3, since 3 is the first of the two middle elements of the sorted list [2, 3, 5, 8]. The function should return None if the input list is empty.

    Define this function. As part of this, you'll need to turn the following description into a simple Python formula. If the input is a list of n numbers, the lower median is in position i of the sorted list, where

    n: 1 2 3 4 17 18
    i: 0 0 1 1 8 8
  5. (25 points)

    The simple moving average of a list of numbers and a size n is a new list of numbers. Each element of the result list is an average of n contiguous elements in the input list. The simple moving average is often used in investment analysis and scientific data analysis to smooth out fluctuations in the data. The following diagram illustrates three different moving averages, for different values of n.

    The following is a more precise definition. Observe that the result is generally a shorter list: len(result) = len(input) - n + 1.

    result[0] = arithmetic_mean(input[0 : n])
    result[1] = arithmetic_mean(input[1 : n+1])
    result[i] = arithmetic_mean(input[i : n+i])
    result[len(input) - n] = arithmetic_mean(input[len(input) - n : len(input)])

    For example, sma([5, 2, 8, 3, 7, 4], 3) should return [5, 4.3333333, 6, 4.666667], which consists of four averages: (5+2+8)/3, (2+8+3)/3, (8+3+7)/3, and (3+7+4)/3.

    Define the function. Clearly, one way to define the function uses your function arithmetic_mean(). Another approach is based upon calculating the next value in the result from the previous one. (Use arithmetic to determine a formula result[i+1] = result[i] + ???.)

Predator/prey (36 points total)

In class, we will introduce the function plot_populations() for calculating and plotting predator-prey populations according to the Lotka-Volterra equations.

  1. (36 points total)

    Consider the predator/prey model and code developed in class. For each of the following behaviors, give an example call to plot_populations() that illustrates this behavior during a forecasted period of at least 50 years. Each set of inputs must include only non-negative numbers.

    For the first four parts, which ask for extreme behaviors, consider what happens with extremely high or low rates. As illustrated in class, the inputs need not be realistic. Rather than just blindly trying numbers, think about the underlying problem and the meanings of the rates.

    1. (7 points) The hare population continually increases.
    2. (7 points) The lynx population continually increases.
    3. (7 points) The hare population continually decreases.
    4. (7 points) The lynx population continually decreases.

    For the last part, we want to avoid any extreme behaviors, so use more moderate rates. The example numbers used in the introductory predator-prey notes are in the right ballpark.

    1. (8 points) The hare and lynx populations both fluctuate up and down.