Activity: Stocks 📈

Create a new file stocks.py to save to your cs100/ch5 folder. Copy and paste the following code to your file.

import math
import turtle
import urllib.request
 
# compute and return the mean of a given list
def mean(alist):
    mean = sum(alist) / len(alist)
    return mean
 
# compute and return the standard deviation of a given list
def standardDev(alist):
    theMean = mean(alist)    
    sum = 0
    for item in alist:
        difference = item - theMean
        diffsq = difference ** 2
        sum = sum + diffsq
         
    sdev = math.sqrt(sum/(len(alist)-1))
    return sdev
 
# compute and return the Pearson's correlation coefficient between two lists
def correlation(xlist, ylist):
    xbar = mean(xlist)
    ybar = mean(ylist)
    xstd = standardDev(xlist)
    ystd = standardDev(ylist)
    num = 0.0
    for i in range(len(xlist)):
        num = num + (xlist[i]-xbar) * (ylist[i]-ybar)
    corr = num / ((len(xlist)-1) * xstd * ystd) 
    return corr
 
# given two lists, plot the correlation between them
def plotCorrelation(xlist, ylist):
    wn = turtle.Screen()
    minX = min(xlist)
    minY = min(ylist)
    maxX = max(xlist)
    maxY = max(ylist)
    wn.setworldcoordinates(minX, minY, maxX, maxY)
     
    t = turtle.Turtle()
    t.speed('fastest')
    turtle.tracer(0, 0)
     
    for i in range(0, len(xlist)-1):
        t.up()
        t.goto(xlist[i], ylist[i])
        t.down()
        t.dot()

# open two CSV files from the internet to read them (without downloading them locally)
url1 = urllib.request.urlopen("http://csweb.wooster.edu/tmontelione/cs100/assignments/AMZN.csv")
url2 = urllib.request.urlopen("http://csweb.wooster.edu/tmontelione/cs100/assignments/AAPL.csv")
 
# use list comprehension to parse file from website as bytestring into a list of list of strings,
# getting rid of header rows
amazonTable = url1.readlines()
appleTable = url2.readlines()
amazonStocks = [line.decode("utf-8").split(',') for line in amazonTable[1:] ]
appleStocks = [line.decode("utf-8").split(',') for line in appleTable[1:] ]
 
# use list comprehension to form a list of just closing price as a floating point number
amazonClose = [ float(amazonStocks[i][4]) for i in range( len(amazonStocks) )]
appleClose = [ float(appleStocks[i][4]) for i in range( len(appleStocks) )]
 
# TODO: add code here to answer the next few questions
  1. Add code to print out Pearson’s correlation coefficient between amazonClose (the closing price of Amazon’s stock over the last 10 years), and appleClose (the closing price of Apple’s stock over the last 10 years).

  2. Add code to plot the correlation between amazonClose and appleClose. What does the correlation indicate? If Apple’s stock price goes up, is it likely that Amazon’s stock price will go up? Or down? Write your answer as a comment.

  3. Add code to read data from http://csweb.wooster.edu/tmontelione/cs100/assignments/NYT.csv which has the last 10 years worth of stock information for the New York Times. Compute the Pearson’s correlation coefficient to determine how NYT correlates with Apple, and how NYT correlates with Amazon. Print the correlation coefficient for both.

If you finish early

  1. Use list comprehension to create a list of the first 10 numbers as strings (with quotes), e.g., “0”, “1”, “2”, …

  2. Use list comprehension to create the list c consisting of all odd numbers within the range [0,20]. Print the list.

  3. Use list comprehension to create the list d consisting of all numbers evenly divisible by 3 within the range [0, 100]. Print the list.

  4. Write code that sums up all the positive numbers entered by the user. When the user enters a negative number, the program displays the sum of the positive numbers and how many numbers were entered. Hint: Use a while loop to check if the number is positive or negative.

How to submit

Submit your working python file to Moodle.