
# coding: utf-8

# ## Estimating Apple's share price tomorrow with machine learning

# Testing data entry

# In[204]:


shares = ["201.5000", "190.2900", "189.9100", "190.9800", "194.2100", "194.8200", "193.0000", "191.6100", "191.4400", "191.8800", "190.4000", "191.4500", "190.9100", "191.3300", "191.0300", "187.8800", "190.3500", "190.5800", "187.9700", "185.4000", "183.9200", "187.1800", "185.1100", "185.5000", "184.1600", "184.4300", "182.1700", "184.9200", "185.4600", "186.5000", "185.6900", "188.7400", "188.8400", "190.8000", "190.7000", "192.2800", "191.2300", "191.7000", "193.4600", "193.9800", "193.3100", "191.8300", "190.2400", "186.8700", "187.5000", "187.9000", "188.5800", "188.1500", "188.3600", "187.1600", "187.6300", "186.3100", "186.9900", "188.1800", "186.4400", "188.1500", "188.5900", "190.0400", "187.3600", "186.0500", "185.1600", "183.8300", "176.8900", "176.5700", "169.1000", "165.2600", "162.3200", "164.2200", "163.6500", "162.9400", "165.2400", "165.7200", "172.8000", "177.8400", "178.2400", "175.8200", "174.7300", "174.1400", "172.4400", "173.2500", "170.0500", "168.3800", "172.8000", "171.6100", "168.3900", "166.6800", "167.7800", "166.4800", "168.3400", "172.7700", "164.9400", "168.8500", "171.2700", "175.2400", "175.3000", "178.0200", "178.6500", "178.4400", "179.9700", "181.7200"]


# In[205]:


print(shares)


# ## Imports 

# In[206]:


from sklearn import linear_model
import numpy as np


# ### Array of Dates and Share Prices

# In[207]:


dates = [100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2,1]
prices = [201.5000, 190.2900, 189.9100, 190.9800, 194.2100, 194.8200, 193.0000, 191.6100, 191.4400, 191.8800, 190.4000, 191.4500, 190.9100, 191.3300, 191.0300, 187.8800, 190.3500, 190.5800, 187.9700, 185.4000, 183.9200, 187.1800, 185.1100, 185.5000, 184.1600, 184.4300, 182.1700, 184.9200, 185.4600, 186.5000, 185.6900, 188.7400, 188.8400, 190.8000, 190.7000, 192.2800, 191.2300, 191.7000, 193.4600, 193.9800, 193.3100, 191.8300, 190.2400, 186.8700, 187.5000, 187.9000, 188.5800, 188.1500, 188.3600, 187.1600, 187.6300, 186.3100, 186.9900, 188.1800, 186.4400, 188.1500, 188.5900, 190.0400, 187.3600, 186.0500, 185.1600, 183.8300, 176.8900, 176.5700, 169.1000, 165.2600, 162.3200, 164.2200, 163.6500, 162.9400, 165.2400, 165.7200, 172.8000, 177.8400, 178.2400, 175.8200, 174.7300, 174.1400, 172.4400, 173.2500, 170.0500, 168.3800, 172.8000, 171.6100, 168.3900, 166.6800, 167.7800, 166.4800, 168.3400, 172.7700, 164.9400, 168.8500, 171.2700, 175.2400, 175.3000, 178.0200, 178.6500, 178.4400, 179.9700, 181.7200]


# ### Linear prediction model

# In[208]:


def predict_price(dates, prices, x):
	
	dates = np.reshape(dates, (len(dates),1)) # converting to matrix of n X 1
	prices = np.reshape(prices, (len(prices),1))
	
	linear_mod = linear_model.LinearRegression() # defining the linear regression model
	linear_mod.fit(dates, prices) # fitting the data points in the model
	
	return linear_mod.predict(x)[0][0], linear_mod.coef_[0][0], linear_mod.intercept_[0]


# ### Printing data to double check it's OK and predicting tomorrow's share price

# In[209]:


predicted_price, coefficient, constant = predict_price(dates, prices, 101)  

print(coefficient)
print(dates)
print(prices)


# In[210]:


print ('\nThe predicted next price is:', str(predicted_price))
print ("The regression coefficient is ", str(coefficient), ", and the constant is ", str(constant))
print ("The relationship equation between dates and prices is: price = ", str(coefficient), "* date + ", str(constant))


9f82e5814f2b96eb226a90d05e611bb6ed02fb7b
<mask token>


def predict_price(dates, prices, x):
    dates = np.reshape(dates, (len(dates), 1))
    prices = np.reshape(prices, (len(prices), 1))
    linear_mod = linear_model.LinearRegression()
    linear_mod.fit(dates, prices)
    return linear_mod.predict(x)[0][0], linear_mod.coef_[0][0
        ], linear_mod.intercept_[0]


<mask token>

<mask token>
print(shares)
<mask token>


def predict_price(dates, prices, x):
    dates = np.reshape(dates, (len(dates), 1))
    prices = np.reshape(prices, (len(prices), 1))
    linear_mod = linear_model.LinearRegression()
    linear_mod.fit(dates, prices)
    return linear_mod.predict(x)[0][0], linear_mod.coef_[0][0
        ], linear_mod.intercept_[0]


<mask token>
print(coefficient)
print(dates)
print(prices)
print("""
The predicted next price is:""", str(predicted_price))
print('The regression coefficient is ', str(coefficient),
    ', and the constant is ', str(constant))
print('The relationship equation between dates and prices is: price = ',
    str(coefficient), '* date + ', str(constant))

shares = ['201.5000', '190.2900', '189.9100', '190.9800', '194.2100',
    '194.8200', '193.0000', '191.6100', '191.4400', '191.8800', '190.4000',
    '191.4500', '190.9100', '191.3300', '191.0300', '187.8800', '190.3500',
    '190.5800', '187.9700', '185.4000', '183.9200', '187.1800', '185.1100',
    '185.5000', '184.1600', '184.4300', '182.1700', '184.9200', '185.4600',
    '186.5000', '185.6900', '188.7400', '188.8400', '190.8000', '190.7000',
    '192.2800', '191.2300', '191.7000', '193.4600', '193.9800', '193.3100',
    '191.8300', '190.2400', '186.8700', '187.5000', '187.9000', '188.5800',
    '188.1500', '188.3600', '187.1600', '187.6300', '186.3100', '186.9900',
    '188.1800', '186.4400', '188.1500', '188.5900', '190.0400', '187.3600',
    '186.0500', '185.1600', '183.8300', '176.8900', '176.5700', '169.1000',
    '165.2600', '162.3200', '164.2200', '163.6500', '162.9400', '165.2400',
    '165.7200', '172.8000', '177.8400', '178.2400', '175.8200', '174.7300',
    '174.1400', '172.4400', '173.2500', '170.0500', '168.3800', '172.8000',
    '171.6100', '168.3900', '166.6800', '167.7800', '166.4800', '168.3400',
    '172.7700', '164.9400', '168.8500', '171.2700', '175.2400', '175.3000',
    '178.0200', '178.6500', '178.4400', '179.9700', '181.7200']
print(shares)
<mask token>
dates = [100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 
    84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67,
    66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49,
    48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31,
    30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13,
    12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
prices = [201.5, 190.29, 189.91, 190.98, 194.21, 194.82, 193.0, 191.61, 
    191.44, 191.88, 190.4, 191.45, 190.91, 191.33, 191.03, 187.88, 190.35, 
    190.58, 187.97, 185.4, 183.92, 187.18, 185.11, 185.5, 184.16, 184.43, 
    182.17, 184.92, 185.46, 186.5, 185.69, 188.74, 188.84, 190.8, 190.7, 
    192.28, 191.23, 191.7, 193.46, 193.98, 193.31, 191.83, 190.24, 186.87, 
    187.5, 187.9, 188.58, 188.15, 188.36, 187.16, 187.63, 186.31, 186.99, 
    188.18, 186.44, 188.15, 188.59, 190.04, 187.36, 186.05, 185.16, 183.83,
    176.89, 176.57, 169.1, 165.26, 162.32, 164.22, 163.65, 162.94, 165.24, 
    165.72, 172.8, 177.84, 178.24, 175.82, 174.73, 174.14, 172.44, 173.25, 
    170.05, 168.38, 172.8, 171.61, 168.39, 166.68, 167.78, 166.48, 168.34, 
    172.77, 164.94, 168.85, 171.27, 175.24, 175.3, 178.02, 178.65, 178.44, 
    179.97, 181.72]


def predict_price(dates, prices, x):
    dates = np.reshape(dates, (len(dates), 1))
    prices = np.reshape(prices, (len(prices), 1))
    linear_mod = linear_model.LinearRegression()
    linear_mod.fit(dates, prices)
    return linear_mod.predict(x)[0][0], linear_mod.coef_[0][0
        ], linear_mod.intercept_[0]


predicted_price, coefficient, constant = predict_price(dates, prices, 101)
print(coefficient)
print(dates)
print(prices)
print("""
The predicted next price is:""", str(predicted_price))
print('The regression coefficient is ', str(coefficient),
    ', and the constant is ', str(constant))
print('The relationship equation between dates and prices is: price = ',
    str(coefficient), '* date + ', str(constant))

shares = ['201.5000', '190.2900', '189.9100', '190.9800', '194.2100',
    '194.8200', '193.0000', '191.6100', '191.4400', '191.8800', '190.4000',
    '191.4500', '190.9100', '191.3300', '191.0300', '187.8800', '190.3500',
    '190.5800', '187.9700', '185.4000', '183.9200', '187.1800', '185.1100',
    '185.5000', '184.1600', '184.4300', '182.1700', '184.9200', '185.4600',
    '186.5000', '185.6900', '188.7400', '188.8400', '190.8000', '190.7000',
    '192.2800', '191.2300', '191.7000', '193.4600', '193.9800', '193.3100',
    '191.8300', '190.2400', '186.8700', '187.5000', '187.9000', '188.5800',
    '188.1500', '188.3600', '187.1600', '187.6300', '186.3100', '186.9900',
    '188.1800', '186.4400', '188.1500', '188.5900', '190.0400', '187.3600',
    '186.0500', '185.1600', '183.8300', '176.8900', '176.5700', '169.1000',
    '165.2600', '162.3200', '164.2200', '163.6500', '162.9400', '165.2400',
    '165.7200', '172.8000', '177.8400', '178.2400', '175.8200', '174.7300',
    '174.1400', '172.4400', '173.2500', '170.0500', '168.3800', '172.8000',
    '171.6100', '168.3900', '166.6800', '167.7800', '166.4800', '168.3400',
    '172.7700', '164.9400', '168.8500', '171.2700', '175.2400', '175.3000',
    '178.0200', '178.6500', '178.4400', '179.9700', '181.7200']
print(shares)
from sklearn import linear_model
import numpy as np
dates = [100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 
    84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67,
    66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49,
    48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31,
    30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13,
    12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
prices = [201.5, 190.29, 189.91, 190.98, 194.21, 194.82, 193.0, 191.61, 
    191.44, 191.88, 190.4, 191.45, 190.91, 191.33, 191.03, 187.88, 190.35, 
    190.58, 187.97, 185.4, 183.92, 187.18, 185.11, 185.5, 184.16, 184.43, 
    182.17, 184.92, 185.46, 186.5, 185.69, 188.74, 188.84, 190.8, 190.7, 
    192.28, 191.23, 191.7, 193.46, 193.98, 193.31, 191.83, 190.24, 186.87, 
    187.5, 187.9, 188.58, 188.15, 188.36, 187.16, 187.63, 186.31, 186.99, 
    188.18, 186.44, 188.15, 188.59, 190.04, 187.36, 186.05, 185.16, 183.83,
    176.89, 176.57, 169.1, 165.26, 162.32, 164.22, 163.65, 162.94, 165.24, 
    165.72, 172.8, 177.84, 178.24, 175.82, 174.73, 174.14, 172.44, 173.25, 
    170.05, 168.38, 172.8, 171.61, 168.39, 166.68, 167.78, 166.48, 168.34, 
    172.77, 164.94, 168.85, 171.27, 175.24, 175.3, 178.02, 178.65, 178.44, 
    179.97, 181.72]


def predict_price(dates, prices, x):
    dates = np.reshape(dates, (len(dates), 1))
    prices = np.reshape(prices, (len(prices), 1))
    linear_mod = linear_model.LinearRegression()
    linear_mod.fit(dates, prices)
    return linear_mod.predict(x)[0][0], linear_mod.coef_[0][0
        ], linear_mod.intercept_[0]


predicted_price, coefficient, constant = predict_price(dates, prices, 101)
print(coefficient)
print(dates)
print(prices)
print("""
The predicted next price is:""", str(predicted_price))
print('The regression coefficient is ', str(coefficient),
    ', and the constant is ', str(constant))
print('The relationship equation between dates and prices is: price = ',
    str(coefficient), '* date + ', str(constant))


# coding: utf-8

# ## Estimating Apple's share price tomorrow with machine learning

# Testing data entry

# In[204]:


shares = ["201.5000", "190.2900", "189.9100", "190.9800", "194.2100", "194.8200", "193.0000", "191.6100", "191.4400", "191.8800", "190.4000", "191.4500", "190.9100", "191.3300", "191.0300", "187.8800", "190.3500", "190.5800", "187.9700", "185.4000", "183.9200", "187.1800", "185.1100", "185.5000", "184.1600", "184.4300", "182.1700", "184.9200", "185.4600", "186.5000", "185.6900", "188.7400", "188.8400", "190.8000", "190.7000", "192.2800", "191.2300", "191.7000", "193.4600", "193.9800", "193.3100", "191.8300", "190.2400", "186.8700", "187.5000", "187.9000", "188.5800", "188.1500", "188.3600", "187.1600", "187.6300", "186.3100", "186.9900", "188.1800", "186.4400", "188.1500", "188.5900", "190.0400", "187.3600", "186.0500", "185.1600", "183.8300", "176.8900", "176.5700", "169.1000", "165.2600", "162.3200", "164.2200", "163.6500", "162.9400", "165.2400", "165.7200", "172.8000", "177.8400", "178.2400", "175.8200", "174.7300", "174.1400", "172.4400", "173.2500", "170.0500", "168.3800", "172.8000", "171.6100", "168.3900", "166.6800", "167.7800", "166.4800", "168.3400", "172.7700", "164.9400", "168.8500", "171.2700", "175.2400", "175.3000", "178.0200", "178.6500", "178.4400", "179.9700", "181.7200"]


# In[205]:


print(shares)


# ## Imports 

# In[206]:


from sklearn import linear_model
import numpy as np


# ### Array of Dates and Share Prices

# In[207]:


dates = [100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2,1]
prices = [201.5000, 190.2900, 189.9100, 190.9800, 194.2100, 194.8200, 193.0000, 191.6100, 191.4400, 191.8800, 190.4000, 191.4500, 190.9100, 191.3300, 191.0300, 187.8800, 190.3500, 190.5800, 187.9700, 185.4000, 183.9200, 187.1800, 185.1100, 185.5000, 184.1600, 184.4300, 182.1700, 184.9200, 185.4600, 186.5000, 185.6900, 188.7400, 188.8400, 190.8000, 190.7000, 192.2800, 191.2300, 191.7000, 193.4600, 193.9800, 193.3100, 191.8300, 190.2400, 186.8700, 187.5000, 187.9000, 188.5800, 188.1500, 188.3600, 187.1600, 187.6300, 186.3100, 186.9900, 188.1800, 186.4400, 188.1500, 188.5900, 190.0400, 187.3600, 186.0500, 185.1600, 183.8300, 176.8900, 176.5700, 169.1000, 165.2600, 162.3200, 164.2200, 163.6500, 162.9400, 165.2400, 165.7200, 172.8000, 177.8400, 178.2400, 175.8200, 174.7300, 174.1400, 172.4400, 173.2500, 170.0500, 168.3800, 172.8000, 171.6100, 168.3900, 166.6800, 167.7800, 166.4800, 168.3400, 172.7700, 164.9400, 168.8500, 171.2700, 175.2400, 175.3000, 178.0200, 178.6500, 178.4400, 179.9700, 181.7200]


# ### Linear prediction model

# In[208]:


def predict_price(dates, prices, x):
	
	dates = np.reshape(dates, (len(dates),1)) # converting to matrix of n X 1
	prices = np.reshape(prices, (len(prices),1))
	
	linear_mod = linear_model.LinearRegression() # defining the linear regression model
	linear_mod.fit(dates, prices) # fitting the data points in the model
	
	return linear_mod.predict(x)[0][0], linear_mod.coef_[0][0], linear_mod.intercept_[0]


# ### Printing data to double check it's OK and predicting tomorrow's share price

# In[209]:


predicted_price, coefficient, constant = predict_price(dates, prices, 101)  

print(coefficient)
print(dates)
print(prices)


# In[210]:


print ('\nThe predicted next price is:', str(predicted_price))
print ("The regression coefficient is ", str(coefficient), ", and the constant is ", str(constant))
print ("The relationship equation between dates and prices is: price = ", str(coefficient), "* date + ", str(constant))

