# Useful Functions
import numpy as np
import matplotlib.pyplot as plt

"""
Explanation: Exercises: Plotting - Answer Key
By Christopher van Hoecke, Max Margenot, and Delaney Mackenzie
Lecture Link:
https://www.quantopian.com/lectures/plotting-data
IMPORTANT NOTE:
This lecture corresponds to the Plotting Data lecture, which is part of the Quantopian lecture series. This homework expects you to rely heavily on the code presented in the corresponding lecture. Please copy and paste regularly from that lecture when starting to work on the problems, as trying to do them from scratch will likely be too difficult.
Part of the Quantopian Lecture Series:

www.quantopian.com/lectures
github.com/quantopian/research_public
End of explanation
"""


data = get_pricing('SPY', fields='price', start_date='2010-01-01', end_date='2017-01-01')
returns = data.pct_change()[1:]

"""
Explanation: Exercise 1:  Histograms
a. Returns
Find the daily returns for SPY over a 7-year window.
End of explanation
"""


plt.hist(returns, bins = 30);
plt.xlabel('Random Numbers');
plt.ylabel('Number of Times Observed');
plt.title('Frequency Distribution of randomly generated number');

"""
Explanation: b. Graphing
Using the techniques laid out in lecture, plot a histogram of the returns
End of explanation
"""


plt.hist(returns, bins = 30, cumulative='true');

"""
Explanation: c. Cumulative distribution
Plot the cumulative distribution histogram for your returns
End of explanation
"""


SPY  = get_pricing('SPY', fields='close_price', start_date='2013-06-19', end_date='2018-06-19', frequency='daily')
SBUX = get_pricing('SBUX', fields='close_price', start_date='2013-06-19', end_date='2018-06-19', frequency='daily')

"""
Explanation: Exercise 2 : Scatter Plots
a. Data
Start by collecting the close prices of McDonalds Corp. (MCD) and Starbucks (SBUX) for the last 5 years with daily frequency.
End of explanation
"""


plt.scatter(SPY, SBUX);
plt.title('Scatter plot of spy and sbux');
plt.xlabel('SPY Price');
plt.ylabel('SBUX Price');

"""
Explanation: b. Plotting
Graph a scatter plot of SPY and Starbucks.
End of explanation
"""


SPY_R = SPY.pct_change()[1:]
SBUX_R = SBUX.pct_change()[1:]

plt.scatter(SPY_R, SBUX_R);
plt.title('Scatter plot of spy and starbucks returns');
plt.xlabel('SPY Return');
plt.ylabel('SBUX Return');

"""
Explanation: c. Plotting Returns
Graph a scatter plot of the returns of SPY and Starbucks.
End of explanation
"""


data = get_pricing(['SBUX', 'DNKN'], fields='open_price', start_date = '2015-01-01', end_date='2017-01-01') ## Your code goes here. 
data.head()

"""
Explanation: Remember a scatter plot must have the same number of values for each parameter. If spy and SBUX did not have the same number of data points, your graph will return an error 

Exercise 3 : Linear Plots
a. Getting Data
Use the techniques laid out in lecture to find the open price over a 2-year period for Starbucks (SBUX) and Dunkin Brands Group (DNKN). Print them out in a table.
End of explanation
"""


data.columns = [e.symbol for e in data.columns] 
data['SBUX'].head()

"""
Explanation: b. Data Structure
The data is returned to us as a pandas dataframe object. Index your data to convert them into simple strings.
End of explanation
"""


plt.plot(data['SBUX']);
plt.xlabel('Time');
plt.ylabel('Price');
plt.title('Price vs Time');

"""
Explanation: c. Plotting
Plot the data for SBUX stock price as a function of time. Remember to label your axis and title the graph.
End of explanation
"""


data1 = get_pricing('SBUX', fields='open_price', start_date='2013-01-01', end_date='2014-01-01')
data2 = get_pricing('SPY', fields='open_price', start_date = '2013-01-01', end_date='2014-01-01')

rdata1= data1.pct_change()[1:]
rdata2= data2.pct_change()[1:]
plt.scatter(rdata2, rdata1);

plt.scatter(rdata2, rdata1)

# Answer
a = 0.0004
b = 1.14
#Answer

x = np.arange(-0.02, 0.03, 0.01)
y = a + (b*x)
plt.plot(x,y, color='r');

"""
Explanation: Exercise 4 : Best fits plots
Here we have a scatter plot of two data sets. Vary the a  and b parameter in the code to try to draw a line that 'fits' our data nicely. The line should seem as if it is describing a pattern in the data. While quantitative methods exist to do this automatically, we would like you to try to get an intuition for what this feels like.
End of explanation
"""


