<jupyter_start><jupyter_text>Stock Market Analysis@<NAME> 2020I am looking at data from the stock market, particularly some technology stocks. I will also be predicting future stock prices through a Long Short Term Memory (LSTM) network algorithmThis will be achieved through the following outline    - visualize    - analyze relationships    - assess risk    - predict<jupyter_code>#imports
import math
import numpy as np
import pandas as pd
import seaborn as sns
import tensorflow as tf
import matplotlib.pyplot as plt
from numpy import array
from datetime import datetime
from pandas_datareader.data import DataReader
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
sns.set_style('whitegrid')
plt.style.use("fivethirtyeight")
%matplotlib inline<jupyter_output>/opt/anaconda3/lib/python3.7/site-packages/pandas_datareader/compat/__init__.py:7: FutureWarning: pandas.util.testing is deprecated. Use the functions in the public API at pandas.testing instead.
  from pandas.util.testing import assert_frame_equal<jupyter_text>VISUALIZE<jupyter_code># the stocks to use for this analysis
tech_list = ['GOOG', 'AMZN', 'MSFT', 'AAPL']

end = datetime.now()
start = datetime(end.year - 1, end.month, end.day)

for stock in tech_list:   
    globals()[stock] = DataReader(stock, 'yahoo', start, end)
company_list = [GOOG, AMZN, MSFT, AAPL]
company_name = ["GOOGLE", "AMAZON", "MICROSOFT", "APPLE"]

for company, com_name in zip(company_list, company_name):
    company["company_name"] = com_name
    
df = pd.concat(company_list, axis=0)
# HISTORICAL VIEW OF CLOSING PRICE

plt.figure(figsize=(12, 8))
plt.subplots_adjust(top=1.25, bottom=1.2)

for i, company in enumerate(company_list, 1):
    plt.subplot(2, 2, i)
    company['Adj Close'].plot()
    plt.ylabel('Adj Close')
    plt.xlabel(None)
    plt.title(f"{tech_list[i - 1]}")
# HISTORICAL VIEW OF TOTAL VOLUME TRADED PER DAY

plt.figure(figsize=(12, 8))
plt.subplots_adjust(top=1.25, bottom=1.2)

for i, company in enumerate(company_list, 1):
    plt.subplot(2, 2, i)
    company['Volume'].plot()
    plt.ylabel('Volume')
    plt.xlabel(None)
    plt.title(f"{tech_list[i - 1]}")<jupyter_output><empty_output><jupyter_text>ANALYZE RELATIONSHIPS<jupyter_code># grab all the closing prices for the tech stock list into one DataFrame
closing_df = DataReader(tech_list, 'yahoo', start, end)['Adj Close']
closing_df.head() 
tech_rets = closing_df.pct_change()
tech_rets.head()<jupyter_output><empty_output><jupyter_text>DAILY PERCENTAGE RETURN OF EACH MAIN STOCK<jupyter_code># i use joinplot to compare the daily returns of Google and Microsoft
sns.jointplot('GOOG', 'MSFT', tech_rets, kind='hex')
sns.pairplot(tech_rets, kind='reg')
#CROSS STOCK ANALYSIS<jupyter_output><empty_output><jupyter_text>ASSESS RISK There are many ways we can quantify risk, one of the most basic ways using the information we've gathered on daily percentage returns is by comparing the expected return with the standard deviation of the daily returns.<jupyter_code>rets = tech_rets.dropna()

area = np.pi*20

plt.figure(figsize=(12, 10))
plt.scatter(rets.mean(), rets.std(), s=area)
plt.xlabel('Expected return')
plt.ylabel('Risk')

for label, x, y in zip(rets.columns, rets.mean(), rets.std()):
    plt.annotate(label, xy=(x, y), xytext=(50, 50), textcoords='offset points', ha='right', va='bottom', 
                 arrowprops=dict(arrowstyle='-', color='blue', connectionstyle='arc3,rad=-0.3'))<jupyter_output><empty_output><jupyter_text>PREDICT  Stock Market Forecasting Using Stacked LSTM<jupyter_code>#CHANGE
STOCK = 'MSFT'
#Get the stock quote
df = DataReader(STOCK, data_source='yahoo', start='2012-01-01', end=datetime.now())
#see what this data looks like
df.head()
df.tail()
df1=df.reset_index()['Close']
plt.plot(df1)
plt.title("Past Stock Value at Daily Close")
scaler=MinMaxScaler(feature_range=(0,1))
df1=scaler.fit_transform(np.array(df1).reshape(-1,1))
##splitting dataset into train and test split
training_size=int(len(df1)*0.65)
test_size=len(df1)-training_size
train_data,test_data=df1[0:training_size,:],df1[training_size:len(df1),:1]
# convert an array of values into a dataset matrix
def create_dataset(dataset, time_step=1):
	dataX, dataY = [], []
	for i in range(len(dataset)-time_step-1):
		a = dataset[i:(i+time_step), 0]   ###i=0, 0,1,2,3-----99   100 
		dataX.append(a)
		dataY.append(dataset[i + time_step, 0])
	return np.array(dataX), np.array(dataY)
# reshape into X=t,t+1,t+2,t+3 and Y=t+4
time_step = 100
X_train, y_train = create_dataset(train_data, time_step)
X_test, ytest = create_dataset(test_data, time_step)
# reshape input to be [samples, time steps, features] which is required for LSTM
X_train =X_train.reshape(X_train.shape[0],X_train.shape[1] , 1)
X_test = X_test.reshape(X_test.shape[0],X_test.shape[1] , 1)
### Create the Stacked LSTM model
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import LSTM
model=Sequential()
model.add(LSTM(50,return_sequences=True,input_shape=(100,1)))
model.add(LSTM(50,return_sequences=True))
model.add(LSTM(50))
model.add(Dense(1))
model.compile(loss='mean_squared_error',optimizer='adam')
model.summary()
model.fit(X_train,y_train,validation_data=(X_test,ytest),epochs=50,batch_size=64,verbose=1)
### Lets Do the prediction and check performance metrics
train_predict=model.predict(X_train)
test_predict=model.predict(X_test)
##Transformback to original form
train_predict=scaler.inverse_transform(train_predict)
test_predict=scaler.inverse_transform(test_predict)
### Calculate RMSE performance metrics
math.sqrt(mean_squared_error(y_train,train_predict))
### Test Data RMSE
math.sqrt(mean_squared_error(ytest,test_predict))
import numpy
### Plotting 
# shift train predictions for plotting
look_back=100
trainPredictPlot = numpy.empty_like(df1)
trainPredictPlot[:, :] = np.nan
trainPredictPlot[look_back:len(train_predict)+look_back, :] = train_predict
# shift test predictions for plotting
testPredictPlot = numpy.empty_like(df1)
testPredictPlot[:, :] = numpy.nan
testPredictPlot[len(train_predict)+(look_back*2)+1:len(df1)-1, :] = test_predict
# plot baseline and predictions
plt.plot(scaler.inverse_transform(df1))
plt.plot(trainPredictPlot)
plt.plot(testPredictPlot)
plt.show()<jupyter_output><empty_output><jupyter_text>As you can see from above, the yellow is my training. I will attempt to apply it to a 30-day prediction below<jupyter_code>x_input=test_data[(len(test_data)-100):].reshape(1,-1)
x_input.shape
temp_input=list(x_input)
temp_input=temp_input[0].tolist()
from numpy import array

lst_output=[]
n_steps=100
i=0
while(i<30):
    if(len(temp_input)>100):
        x_input=np.array(temp_input[1:])
        x_input=x_input.reshape(1,-1)
        x_input = x_input.reshape((1, n_steps, 1))
        yhat = model.predict(x_input, verbose=0)
        temp_input.extend(yhat[0].tolist())
        temp_input=temp_input[1:]
        lst_output.extend(yhat.tolist())
        i=i+1
    else:
        x_input = x_input.reshape((1, n_steps,1))
        yhat = model.predict(x_input, verbose=0)
        temp_input.extend(yhat[0].tolist())
        lst_output.extend(yhat.tolist())
        i=i+1
print(lst_output)
day_new=np.arange(1,101)
print(x_input.shape)
day_pred=np.arange(101,131)
plt.plot(day_new,scaler.inverse_transform(df1[(len(df1)-100):]))
plt.plot(day_pred,scaler.inverse_transform(lst_output))
plt.title("STOCK PREDICTION OVER NEXT 30 DAYS")
plt.xlabel("Elapsed Days")
plt.ylabel("Stock Value ($)")
print('input length of predicted forecast (1-30): ')
n = int(input())
predictions = scaler.inverse_transform(lst_output)[:n]
print('PREDICTIONS:    \n')
for counter, pred in enumerate(predictions):
    print('day', counter+1, pred[0])<jupyter_output>input length of predicted forecast (1-30): 
5
PREDICTIONS:    

day 1 199.3919366901206
day 2 198.20846175690838
day 3 196.7091941002309
day 4 195.01405855263613
day 5 193.22185166697238
{'fraction_non_alphanumeric': 0.12877742946708465, 'fraction_numerical': 0.03398119122257053, 'mean_word_length': 6.089777777777778, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '609727', 'license': 'Apache-2.0', 'n_tokens_mistral': 3066, 'n_tokens_neox': 2835, 'n_words': 638, 'path': 'stock_market_analysis.ipynb', 'repo_name': 'ayaanrahim/Ascend', 'size': '7985'}