<jupyter_start><jupyter_text>Imports<jupyter_code>import numpy as np

from sklearn import metrics
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_absolute_error , mean_squared_error , mean_absolute_percentage_error

import tensorflow as tf
from tensorflow.keras.layers import LSTM , Dropout , Dense
from tensorflow.keras.models import Sequential
import pandas_datareader.data as web
import datetime

import datetime
import pandas as pd 
import matplotlib.pyplot as plt
start = datetime.datetime(2011 , 1 , 1)
end= datetime.datetime.now()
df = web.DataReader('^NSEI' , 'yahoo' , start , end)
df.drop(['Volume' , 'Adj Close'] , axis=1 , inplace=True)
df['pct_change'] = df['Close'].pct_change()
df.dropna(inplace=True)
data = df['pct_change'].copy()
# Training and testing data
train_size = int(len(data) *0.80)
train = data[:train_size]
test = data[train_size :]
X_train = []
y_train = []

for i in range(60,len(train)-1):
    X_train.append(train[i-60:i])
    
    y_train.append(train[i])
X_train = np.array(X_train)
y_train = np.array(y_train)
X_train[1] , y_train[0]
X_test = []
y_test = []

for i in range(60,len(test)-1):
    X_test.append(test[i-60 :i])
    
    y_test.append(test[i])
X_test = np.array(X_test)
y_test = np.array(y_test)
lstm = Sequential()
lstm.add(LSTM(60 ,return_sequences=True ,  input_shape=(60,1)))
lstm.add(LSTM(60 , return_sequences = True))
lstm.add(LSTM(60))
lstm.add(Dense(1))
lstm.compile(loss='mean_squared_error' , optimizer='adam' , metrics=['accuracy','mse'])
lstm.summary()
callback = tf.keras.callbacks.ModelCheckpoint(filepath='./models/lstmYahooPctChange/',
                                              save_weights_only=True,
                                              verbose=1)
X_train = X_train.reshape((-1 ,60 , 1))
X_test = X_test.reshape((-1,60,1))
y_train = y_train.reshape(-1,1)
y_test = y_test.reshape(-1,1)
lstm.fit(X_train ,y_train , validation_data=(X_test,y_test) ,epochs=100 , batch_size=64 ,verbose=1 , callbacks=[callback] )
lstm.save('./models/lstmYahooPctChange/lstmyahooPctChange.h5')
training_predictions = lstm.predict(X_train)
plt.figure(figsize=(20,10))
plt.plot(training_predictions , label='Training Fitting')
plt.plot(np.array(y_train).reshape(-1,1) ,label='Original Values')
plt.legend()
plt.show()
preds = lstm.predict(X_test)
print('Mean Squared Error : {}'.format(mean_squared_error(scaler.inverse_transform(np.array(y_test).reshape(-1,1)) ,scaler.inverse_transform(preds) )))
print('Mean Absolute Error : {}'.format(mean_absolute_error(scaler.inverse_transform(np.array(y_test).reshape(-1,1)) ,scaler.inverse_transform(preds))))
plt.figure(figsize=(20,10))
plt.plot(scaler.inverse_transform(preds) , label='Testing Prediction')
plt.plot(scaler.inverse_transform(np.array(y_test).reshape(-1,1)) ,label='Original')
plt.legend()
plt.show()
scaled_original = scaler.inverse_transform(np.array(y_test).reshape(-1,1))
scaled_lstm10 = scaler.inverse_transform(preds)
scaled_original = scaled_original.reshape(1,-1).tolist()
scaled_lstm10  = scaled_lstm10.reshape(1,-1).tolist()
index = test[61:].index
data = {'Original':scaled_original[0] ,
        'last 10 years model prediction': scaled_lstm10[0]}
df = pd.DataFrame(data=data , index =index)
df.loc['2021-01':]
df.to_csv('./yahooTraining.csv')
print('Mean Absolute Percentage Error : {}'.format(mean_absolute_percentage_error(scaler.inverse_transform(np.array(y_test).reshape(-1,1)) ,scaler.inverse_transform(preds) )))
print('Mean Absolute Percentage Error : {}'.format(mean_absolute_percentage_error(scaler.inverse_transform(np.array(y_train).reshape(-1,1)) ,scaler.inverse_transform(training_predictions) )))
plt.figure(figsize=(20,10))
plt.scatter(index , scaler.inverse_transform(preds) , label='Testing Prediction')
plt.scatter(index , scaler.inverse_transform(np.array(y_test).reshape(-1,1)) ,label='Original')
plt.legend()
plt.show()
df['pct_change_original']= df['Original'].pct_change()
df['pct_change_predicted']= df['last 10 years model prediction'].pct_change()
df.head()
diff = df['Original']- df['last 10 years model prediction']
print(diff.max())
print(diff.min())
diff.plot()
plt.show()
df.loc['2020-03']
df.loc['2020-12']
plt.figure(figsize=(20,10))
plt.plot(index , df['pct_change_original'] , label='Testing Prediction')
plt.plot(index , df['pct_change_predicted'] ,label='Original')
plt.legend()
plt.show()
plt.figure(figsize=(20,10))
plt.scatter(index , df['pct_change_original'] , label='Testing Prediction')
plt.scatter(index , df['pct_change_predicted'] ,label='Original')
plt.legend()
plt.show()
np.mean(df['pct_change_original']) - np.mean(df['pct_change_predicted'])<jupyter_output><empty_output><jupyter_text>calc<jupyter_code>spot_price = np.arange(1990 , 2110 , 10)
spot_price
strike = 2050
premium = 6.35
for i in range(len(spot_price)):
    out = max(0 , int(spot_price[i] - strike)) - premium
    print(out)<jupyter_output>-6.35
-6.35
-6.35
-6.35
-6.35
-6.35
-6.35
3.6500000000000004
13.65
23.65
33.65
43.65
{'fraction_non_alphanumeric': 0.16152019002375298, 'fraction_numerical': 0.0395882818685669, 'mean_word_length': 6.137005649717514, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '384164', 'license': 'MIT', 'n_tokens_mistral': 2033, 'n_tokens_neox': 1862, 'n_words': 297, 'path': 'PredictionModels/LSTMonYahooDataWithPercentChange.ipynb', 'repo_name': 'Rahul-singh98/CodeSure', 'size': '5052'}