UrbanSim Capstone - Warmup Assignment

The following assignment is intended to give you an introduction to the supporting software for UrbanSim.

The undergrad lab machines have Python, Eclipse, and PyDev installed -- if you want to do this assignment on the ugrad machines, you just need to download and install numpy.

Due: Jan 17, 10:30am. By email to Alice, turn in the .py file for your BankAccount class for Question 1, and for the functions defined for Questions 2-5. Also turn in a copy of the transcript showing the results for Questions 3-5.

1. Python -- Objects and Classes

This question is intended to give an introduction to Eclipse and unit testing in Python. Using Eclipse, define a new Python class BankAccount.  BankAccount should have the following methods:

You'll also need to define a new exception Overdraft.

Also write unit tests for your BankAccount object.  These should test each of the methods, including a case in which there is an overdraft.

2. Higher-Order Functions in Python

In addition to support for objects, Python has functions, including higher-order functions like you encountered in CSE 341.

Using lambda and functions such as map, define the following functions. Don't use recursion or loops, and use lambda rather than defining named auxiliary helper functions.

Write appropriate unit tests for both functions.

3. numpy arrays

Define a function make_table that takes a parameter n, and returns a 2-d array showing the results of applying different functions to a range of numbers. It is defined as follows:

Set ns to a numpy array consisting of the numbers between 0 and n-1.

Return a 2-d numpy array consisting of the following rows::

You should do this just with numpy operators -- no explicit loops!!

(Hint: you can use 'vstack' to take a tuple of 1-d arrays and turn them into a 2-d array.)

4. Using the numpy 'where' function

Define a function rand_tester that takes an integer n, and returns another integer (which should be around n/2 if numpy's random number generator is working properly). Specifically, first create an array of n floats between 0 and 1 using the random.rand function. Now find how many of these are greater than 0.5, and return that number. For example, when I tried this with n=1000000, I got a result of 500039.

Again, no explicit loops!  Hints: check out the sum and where functions.

5. A simple probability computation using numpy

This last question uses numpy to do some of the kinds of computations we do in the UrbanSim model.

Define a function desirable_apartment_count that takes two arrays: one representing the monthly incomes of a set of households, and the other the monthly rents for a set of apartments.  It should return an array of the same length as the households, where each element is the number of 'desirable' apartments for that household. A 'desirable' apartment is defined as a function of the ratio of apartment rent to household income. In this problem, the desirability threshold is simply set to 1/3, with greater than 1/3 being undesirable. This is to say, a household will only consider moving to an apartment if the household income is at least thrice the rent.

The function desirable_apartment_count should define a m by n array 'cost_to_income' consisting of the ratio of the rents to incomes for each apartment/household pair. Then, using the 'cost_to_income' ratios, compute the number of apartments that each household would be willing to move to and return those values.

For example, we might test the function with these inputs:

apartments = array([800, 1000, 1300, 2000, 650])
households = array([1000, 2500, 5000])

Then within the function you'd define a 3x5 array 'cost_to_income', and compute the result. The cost_to_income array would be:

[[ 0.80, 0.32, 0.16 ],
[ 1.00, 0.40, 0.20 ],
[ 1.30, 0.52, 0.26 ],
[ 2.00, 0.80, 0.40 ],
[ 0.65, 0.26, 0.13 ]]
And the returned value would be [0, 2, 4]