Lab 7: First Hour

  • Discussion of numerical stability
    • Binary decimal structure. Is it possible to represent every number exactly?
      >>> a = 3.1
      >>> a
      >>> a = 35.95
      >>> a
    • Numpy's round allows you to choose the number of decimal places, but how can you floor a decimal to two places?
      a = floor(a * 100.0)/100.0
  • In lab problem
    • The plot of Office Space and Superman 3.
      • Write a function named create_accounts that, given input length n, will generate a pseudo-random list of n bank accounts between $0.01 and $10000.00 (inclusive).
      • Write a function named real_interest that, given a list of account balances as input and a percentage of interest p, will calculate the yearly interest, compounded monthly, and update the balance of each account. Note that interest is always rounded to the nearest cent (2 decimal places).
      • Write a function named evil_interest that, given a list of transactions as input and a percentage of interest p, will calculate the yearly interest, and implement the malicious plot as in the movie Office Space, where the interest is always rounded down, and the remainder is dropped into a personal account. The function should return as the amount that you were able to steal with this diabolical plot in one year.
      • Use __main__ and provide a function that will create accounts, and then run the evil interest for 25 years. Use VPython to graph the amount of money you have skimmed into your account, making one plot every year.

Sample Solution

from numpy import round, floor
from random import uniform
def create_accounts(n):
   accounts = []
   for i in xrange(n):
       accounts.append(round(uniform(0.01, 10000.0), 2))
   return accounts
def real_interest(accounts, p):
   for i in xrange(len(accounts)):
       for j in xrange(12):
           accounts[i] += round(accounts[i] * p/12.0, 2)
def evil_interest(accounts, p):
   my_account = 0.0
   for i in xrange(len(accounts)):
       for j in xrange(12):
           real_interest = accounts[i] * p/12.0
           interest_floor = floor(real_interest * 100.0)/100.0
           my_share = real_interest - interest_floor
           accounts[i] += interest_floor
           my_account += my_share
   return round(my_account, 2)
if __name__ == "__main__":
   from visual import rate, color
   from visual.graph import gdisplay, gcurve
   scene = display(                       
     title="How to succeed at business without really trying",
   gdisplay(                               # 2D graph window parameters
     title="Money Illegally Earned",     # Window title
     xtitle="Years",                     # Label on x-axis
     ytitle="Dollars",                   # Label on y-axis
   green_line = gcurve(color=(0,1,0))       # Set up a green curve
   accounts = create_accounts(100000)
   my_account = 0.0
   for i in xrange(25):
      my_account += evil_interest(accounts, 0.03)
      print "$", my_account
      green_line.plot(pos=(i, my_account))

Second Hour

  • Discuss Part 2 of Project 2: What plots to create and what data to analyze.
    • show the example plot
    • generate the corresponding plots for the other two forms of percolation
    • two setup questions
    • study on impact of grid size is only done for unrestricted percolation
    • mention possible problem with recursion limit
    • point them to needed set of plot formats
  • Answer questions over Part 1 and provide time to work.
cs190c/lab7.txt · Last modified: 2008/07/24 12:09 by seh
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki