2012Fall7646 Homework 3
The purpose of this assignment is to:
- Get you started using real stock data with QSTK.
- Have you explore the statistical properties of daily returns.
Part 1: Review, and make sure you understand QSTK_Tutorial_1.
Part 2: Search for relationships in daily returns of the S&P 500 stocks in 2011.
Find the two most correlated (closest to 1.0) and two most anti-correlated (closest to -1.0) stocks in the S&P 500.
Some hints: Use the file ~/QSTK/QSData/Yahoo/Lists/sp5002012.txt for the list of stocks in the S&P 500. Then follow the code in QSTK_Tutorial_1 to read them in.
symbols = data.get_symbols_from_list("sp5002012")
Be sure to fill the price data forward, then fill it back using Pandas' fill method which is described here: 
Part 3: Create some plots and generate some numbers to assess the data.
- Scatter plot comparing daily returns of two most correlated stocks. Also fit a like (use polyfit) to the data and print out: Beta (the slope of the line) and the correlation coefficient.
- Scatter plot comparing daily returns of two most anti-correlated stocks. Also fit a like (use polyfit) to the data and print out: Beta (the slope of the line) and the correlation coefficient.
- Create a histogram of the daily returns of all S&P 500 stocks for 2011 (approximately 500*252 data points). Print out: mean and standard deviation of this data.
- For extra credit: Fit a normal distribution (Gaussian) or other standard PDF function to the data and describe how well it fits (or doesn't fit) the data.
- Turn in files as attachments by t-square. Please do not "zip" your files together first; just submit separate files as named below:
- program.py Your Python program.
- results.txt The numerical results described above.
- correlated.pdf The scatter plot and fitted line.
- anticorrelated.pdf Scatter plot and fitted line.
- histogram.pdf The histogram of daily returns (with optional distribution).
How to submit
Go to the t-square site for the class, then click on the "assignments" tab. Click on "add attachment" to add your three files. Once you are sure you've added both files, click "submit."
To help get you started, here are some snippets of code from a possible solution. More to come.
# # Example code regarding Homework 3 # import numpy as np import matplotlib.pyplot as plt import matplotlib.mlab as mlab from pylab import * num_positions = 1000 position_value = 1000/num_positions num_trials = 100000 # # main code goes here # plt.hist(daily_ret,100,range=[-1,1])