# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: conda_pytorch_p36
#     language: python
#     name: conda_pytorch_p36
# ---

# #!pip install tensorflow
# #!pip install keras
# #!pip install pandas-datareader
import tensorflow
import keras
# #!pip install pandas
import pandas
import numpy

import pandas_datareader.data as web
import pandas as pd
import datetime
import numpy as np
import matplotlib.pyplot as plt
# %matplotlib inline
from numpy import array
import sklearn
from sklearn import preprocessing
from sklearn.tree import DecisionTreeRegressor
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.linear_model import HuberRegressor
from sklearn.preprocessing import MinMaxScaler
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM


# +
#start = datetime.datetime(2014, 1, 1)
#end = datetime.datetime(2020, 7, 24)

# +
###axisgold = web.DataReader("AXISGOLD.NS", 'yahoo', start, end)
###bslgoldetf = web.DataReader("BSLGOLDETF.NS", 'yahoo', start, end)
#cangold = web.DataReader("CANGOLD.BO", 'yahoo', start, end)
#hfcmgetf = web.DataReader("HFCMFGETF.BO", 'yahoo', start, end)
#ipgetf = web.DataReader("IPGETF.BO", 'yahoo', start, end)
###idbigold = web.DataReader("IDBIGOLD.NS", 'yahoo', start, end)
#kotakgold = web.DataReader("KOTAKGOLD.NS", 'yahoo', start, end)
###qgoldhalf = web.DataReader("QGOLDHALF.NS", 'yahoo', start, end)
#relgold = web.DataReader("RELGOLD.BO", 'yahoo', start, end)
#religarego = web.DataReader("RELIGAREGO.BO", 'yahoo', start, end)
#setfgold = web.DataReader("SETFGOLD.NS", 'yahoo', start, end)
###goldshare = web.DataReader("GOLDSHARE.NS", 'yahoo', start, end)
###goldbees = web.DataReader("GOLDBEES.NS", 'yahoo', start, end)
# -

kotakgold = pd.read_csv('KOTAKGOLD.csv')
###axisgold.to_csv('AXISGOLD.csv')
###bslgoldetf.to_csv('BSLGOLDETF.csv')
###idbigold.to_csv('IDBIGOLD.csv')
kotakgold.to_csv('KOTAKGOLD.csv')
###qgoldhalf.to_csv('QGOLDHALF.csv')
#setfgold.to_csv('SETFGOLD.csv')
####goldshare.to_csv('GOLDSHARE.csv')
###goldbees.to_csv('GOLDBEES.csv')

###axisgold['Open'].plot(label = 'axisgold', figsize= (15,7))
###bslgoldetf['Open'].plot(label = 'bslgoldetf')
###idbigold['Open'].plot(label = 'idbigold')
kotakgold['Open'].plot(label = 'kotakgold')
###qgoldhalf['Open'].plot(label = 'qgoldhalf')
###goldshare['Open'].plot(label = 'goldshare')
###goldbees['Open'].plot(label = 'goldbees')
plt.title('Gold ETF stock price')
plt.legend()

# +
#We are going to predict the price of one Gold ETF below. Similarly all other predictions can also be made. Let us chose Axisgold ETF
# -

# Select Adjusted close data
data = kotakgold[['Adj Close']]
print(data.shape)
data[-20:]

from sklearn.preprocessing import MinMaxScaler
scaler=MinMaxScaler(feature_range=(0,1))
data_scaled=scaler.fit_transform(np.array(data).reshape(-1,1))

print(data_scaled)

# +
#Split data 80% training 20% testing

training_size=int(len(data_scaled)*0.8)
test_size=len(data_scaled)-training_size
train_data,test_data=data_scaled[0:training_size],data_scaled[training_size:len(data)]
# -

training_size,test_size

train_data


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 numpy.array(dataX), numpy.array(dataY)


#reshape into X=t,t+1,t+2,t+3 and Y=t+4
time_step = 1
x_train, y_train = create_dataset(train_data, time_step)
x_test, y_test = create_dataset(test_data, time_step)
#x_train, x_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)

#Regression coefficients
x_train_lr = x_train
y_train_lr = y_train
x_test_lr = x_test
y_test_lr = y_test

print(x_train_lr.shape), print(y_train_lr.shape)

print(x_test_lr.shape), print(y_test_lr.shape)


x_train_lr.shape, x_test_lr.shape

x_train_lr

# +
#Create models
#Create decision tree model
#modeltree = DecisionTreeRegressor()
#modeltree.fit(x_train, y_train)

#Create linear decision model
modellr = LinearRegression()
modellr.fit(x_train_lr, y_train_lr)
# -

# The coefficient
print('Slope: ', np.asscalar(np.squeeze(modellr.coef_)))
# The Intercept
print('Intercept: ', modellr.intercept_)

# Train set graph
plt.figure(1, figsize=(16,10))
plt.title('Linear Regression | Price vs Time')
plt.scatter(x_train_lr, y_train_lr, edgecolor='w', label='Actual Price')
plt.plot(x_train_lr, modellr.predict(x_train_lr), color='r', label='Predicted Price')
plt.xlabel('Integer Date')
plt.ylabel('Stock Price')
plt.legend()
plt.show()

# Create test arrays
x_test_lr

# Generate array with predicted values
train_pred_lr = modellr.predict(x_train_lr)
test_pred_lr = modellr.predict(x_test_lr)

# +
#model accurcy
#results_lr = model.evaluate(x_test_lr, y_test_lr, batch_size=128)
#print("test loss, test acc:", results_lr)
# -

# reshape input to be [samples, time steps, features] which is required for LSTM
train_pred_lr =train_pred_lr.reshape(train_pred_lr.shape[0], 1)
test_pred_lr = test_pred_lr.reshape(test_pred_lr.shape[0], 1)

train_pred_lr=scaler.inverse_transform(train_pred_lr)
test_pred_lr=scaler.inverse_transform(test_pred_lr)

y_train_lr =y_train_lr.reshape(y_train_lr.shape[0], 1)
y_test_lr = y_test_lr.reshape(y_test_lr.shape[0], 1)

y_train_lr=scaler.inverse_transform(y_train_lr)
y_test_lr=scaler.inverse_transform(y_test_lr)

print(x_train_lr)
print(train_pred_lr)

# +
#plt.plot(x_test_lr)
#plt.plot(test_pred_lr)
#plt.plot(x_train_lr)
#plt.plot(train_pred_lr)
# -

print(y_test_lr.shape) 
print(train_pred_lr.shape)
print(y_train_lr.shape)
print(test_pred_lr.shape)

# +
### Calculate RMSE performance metrics
import math
from sklearn.metrics import mean_squared_error
print("Training Linear Regression Root Mean Squared error is:{}". format(math.sqrt(mean_squared_error(y_train_lr,train_pred_lr))))
print("Training Linear Regression Mean Squared error is {}".format(mean_squared_error(y_train_lr,train_pred_lr)))

print("Test Linear Regression Root Mean Squared error is:{}". format(math.sqrt(mean_squared_error(y_test_lr,test_pred_lr))))
print("Test Linear Regression Mean Squared error is {}".format(mean_squared_error(y_test_lr,test_pred_lr)))

# -

# shift train predictions for plotting
look_back=100
trainPredictPlot = numpy.empty_like(data_scaled)
trainPredictPlot[:, :] = np.nan
trainPredictPlot[look_back:len(train_pred_lr)+look_back, :] = train_pred_lr
# shift test predictions for plotting
testPredictPlot = numpy.empty_like(data_scaled)
testPredictPlot[:, :] = numpy.nan
testPredictPlot[len(train_pred_lr)+(look_back*2)-196:len(data)+100, :] = test_pred_lr
# plot baseline and predictions
plt.figure(figsize = (16,8))
plt.plot(scaler.inverse_transform(data_scaled))
plt.plot(trainPredictPlot)
plt.plot(testPredictPlot)
plt.legend(['Original','Training', 'Test'])
plt.show()





# +
#data_1 = pd.read_csv('KOTAKGOLD.csv')

# +
#data = data_1[['Adj Close']]

# +
#from sklearn.preprocessing import MinMaxScaler
#scaler=MinMaxScaler(feature_range=(0,1))
#data=scaler.fit_transform(np.array(data).reshape(-1,1))

# +
#Split data 80% training 20% testing

#training_size=int(len(data)*0.8)
#test_size=len(data)-training_size
#train_data,test_data=data[0:training_size],data[training_size:len(data)]

# +
#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 numpy.array(dataX), numpy.array(dataY)

# +
#reshape into X=t,t+1,t+2,t+3 and Y=t+4
#time_step = 1
#x_train, y_train = create_dataset(train_data, time_step)
#x_test, y_test = create_dataset(test_data, time_step)
#x_train, x_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)
# -

#LSTM coefficients
x_train_lstm = x_train
y_train_lstm = y_train
x_test_lstm = x_test
y_test_lstm = y_test

# reshape input to be [samples, time steps, features] which is required for LSTM
x_train_lstm =x_train_lstm.reshape(x_train_lstm.shape[0],x_train_lstm.shape[1] , 1)
x_test_lstm = x_test_lstm.reshape(x_test_lstm.shape[0],x_test_lstm.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
from tensorflow.keras.layers import Dropout, Activation

# +
#from keras import backend as K
#K.set_image_data_format('channels_last')  
#keras.backend.image_data_format()  
# -

model=Sequential()
model.add(LSTM(5,return_sequences=True,input_shape=(5,1)))
model.add(Dropout(0.5))
#model.add(LSTM(100,return_sequences=True))
#model.add(Dropout(0.5))
#model.add(LSTM(100,return_sequences=True))
#model.add(Dropout(0.5))
model.add(LSTM(5))
#model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation("linear"))
model.compile(loss='mse',optimizer='adam')

model.summary()

history = model.fit(x_train_lstm,y_train_lstm,validation_data=(x_test_lstm,y_test_lstm),epochs=50,batch_size=64,verbose=1)

plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])

### Lets Do the prediction and check performance metrics
train_pred_lstm=model.predict(x_train_lstm)
test_pred_lstm=model.predict(x_test_lstm)

##Transformback to original form
train_pred_lstm=scaler.inverse_transform(train_pred_lstm)
test_pred_lstm=scaler.inverse_transform(test_pred_lstm)


# reshape input to be [samples, time steps, features] which is required for LSTM
y_train_lstm =y_train_lstm.reshape(y_train_lstm.shape[0], 1)
y_test_lstm = y_test_lstm.reshape(y_test_lstm.shape[0], 1)
y_train_lstm=scaler.inverse_transform(y_train_lstm)
y_test_lstm=scaler.inverse_transform(y_test_lstm)

print(y_test_lstm)
print(test_pred_lstm)

# shift train predictions for plotting
look_back=100
trainPredictPlot = numpy.empty_like(data_scaled)
trainPredictPlot[:, :] = np.nan
trainPredictPlot[look_back:len(train_pred_lstm)+look_back, :] = train_pred_lstm
# shift test predictions for plotting
testPredictPlot = numpy.empty_like(data_scaled)
testPredictPlot[:, :] = numpy.nan
testPredictPlot[len(train_pred_lstm)+(look_back*2)-196:len(data)+100, :] = test_pred_lstm
# plot baseline and predictions
plt.figure(figsize = (16,8))
plt.plot(scaler.inverse_transform(data_scaled))
plt.plot(trainPredictPlot)
plt.plot(testPredictPlot)
plt.legend(['Original','Training', 'Test'])
plt.show()

### Calculate RMSE performance metrics
import math
from sklearn.metrics import mean_squared_error
print("Training LSTM Root Mean Squared error is:{}". format(math.sqrt(mean_squared_error(y_train_lstm,train_pred_lstm))))
print("Traning LSTM Mean Squared error is {}".format(mean_squared_error(y_train_lstm,train_pred_lstm)))

### Test Data RMSE
print("Test LSTM Root Mean Squared error is:{}". format(math.sqrt(mean_squared_error(y_test_lstm,test_pred_lstm))))
print("Test LSTM Mean Squared error is {}".format(mean_squared_error(y_test_lstm,test_pred_lstm)))


results_lstm = model.evaluate(x_test_lstm, y_test_lstm, batch_size=128)
print("test loss, test acc:", results_lstm)

# ### shift train predictions for plotting
# look_back=100
# trainPredictPlot = numpy.empty_like(data)
# trainPredictPlot[:, :] = np.nan
# trainPredictPlot[look_back:len(train_predict)+look_back, :] = train_predict
# # shift test predictions for plotting
# testPredictPlot = numpy.empty_like(data)
# testPredictPlot[:, :] = numpy.nan
# testPredictPlot[len(train_predict)+(look_back*2)-196:len(data)+100, :] = test_predict
# # plot baseline and predictions
# plt.plot(scaler.inverse_transform(data))
# plt.plot(trainPredictPlot)
# plt.plot(testPredictPlot)
# plt.legend(['Original','Training', 'Test'])
# plt.show()
#

len(data_scaled)
data_scaled

#Predicting to next 1 day data using previous 7 days
x_input=data_scaled[len(data_scaled)-1:].reshape(1,-1)
x_input.shape

temp_input=list(x_input)
temp_input=temp_input[0].tolist()

len(temp_input)

temp_input

x_input

# +
from numpy import array

lst_output=[]
n_steps=1
i=0
while(i<7):
    
    if(len(temp_input)>1):
        #print(temp_input)
        x_input=np.array(temp_input[1:])
        print("{} day input {}".format(i,x_input))
        x_input=x_input.reshape(1,-1)
        x_input = x_input.reshape((1, n_steps, 1))
        #print(x_input)
        yhat = model.predict(x_input, verbose=0)
        print("{} day output {}".format(i,yhat))
        temp_input.extend(yhat[0].tolist())
        temp_input=temp_input[1:]
        #print(temp_input)
        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)
        print(yhat[0])
        temp_input.extend(yhat[0].tolist())
        print(len(temp_input))
        lst_output.extend(yhat.tolist())
        i=i+1
    

print(lst_output)
# -

day_new=np.arange(1,len(lst_output)+1)
day_pred=np.arange(len(lst_output)+1,len(lst_output)+8)

len(day_new)

lst_output

day_pred.shape

tx = scaler.inverse_transform(lst_output)
tx

plt.plot(day_new,scaler.inverse_transform(data_scaled[len(data_scaled)-7:]))
plt.plot(day_pred,tx)

df3=data_scaled.tolist()
df3.extend(lst_output)
plt.plot(df3[1500:])

df3=scaler.inverse_transform(df3).tolist()

plt.plot(df3)

df3[-40:]

# +
#train_pred_lr = modellr.predict(x_train_lr)
#test_pred_lr = modellr.predict(x_test_lr)
#train_pred_lstm=model.predict(x_train_lstm)
#test_pred_lstm=model.predict(x_test_lstm)
# -

print(train_pred_lr-y_train_lr)

print(train_pred_lstm-x_train_lstm)



StarcoderdataJupyterScriptsDedupFiltered