-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotlydemo.py
More file actions
131 lines (99 loc) · 4.51 KB
/
Copy pathplotlydemo.py
File metadata and controls
131 lines (99 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import copy
import math
import random
from typing import Dict, List
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import plotly.express as px
import seaborn as sns
import torch
import torch.nn as nn
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import MinMaxScaler
from tqdm import tqdm
import multiple_ticker_models as mtm
from APIs.close_price import get_all_adjusted_prices
import streamlit as st
st.title('Predicting Stocks using RNNs')
target_ticker = st.text_input('Enter the ticker you want to predict', 'AAPL')
target_ticker = target_ticker.upper()
print("target ticker")
print(target_ticker)
INPUT_DIM = 1
HIDDEN_DIM = 32
NUM_LAYERS = 2
OUTPUT_DIM = 1
SEQ_LENGTH = 20
def plotly_plot_single(predicted, actual, ticker, model_info):
scaler_predicted = copy.deepcopy(mtm.scalers[ticker])
scaler_actual = copy.deepcopy(mtm.scalers[ticker])
predicted = scaler_predicted.inverse_transform(predicted)
actual = scaler_actual.inverse_transform(actual)
dates_x = mtm.dates[ticker].copy()
dates_x.reverse()
dates_x = dates_x[SEQ_LENGTH:]
dates_x = pd.to_datetime(dates_x)
df = pd.DataFrame({'Actual price': actual.flatten(),
'Predicted price': predicted.flatten()},
index=dates_x)
fig = px.line(df, title=f'Predictions for {ticker} - {model_info}')
return fig
# fig.show()
def eval_model(model, x_train_map, y_train_map, x_test_map, y_test_map, model_type):
ret = {}
for ticker in x_test_map:
x_test = torch.from_numpy(x_test_map[ticker]).type(torch.Tensor).to(mtm.device)
# y_test = torch.from_numpy(y_test_map[ticker]).type(torch.Tensor).to(device)
y_test = y_test_map[ticker]
testing_predictions = model(x_test)
testing_predictions = testing_predictions.cpu().detach().numpy()
# test_mse = mean_squared_error(y_test[:, 0], testing_predictions[:, 0])
scaler = copy.deepcopy(mtm.scalers[ticker])
scaler2 = copy.deepcopy(mtm.scalers[ticker])
y_test_inverted = scaler.inverse_transform(np.array(y_test))
testing_predictions_inverted = scaler2.inverse_transform(np.array(testing_predictions))
print(y_test_inverted[:, 0])
print(testing_predictions_inverted[:, 0])
test_rmse = math.sqrt(mean_squared_error(y_test_inverted[:, 0], testing_predictions_inverted[:, 0]))
# print(f"{model_type} Test RMSE for {ticker}: {test_rmse}")
# mtm.plot_singular_complete(testing_predictions, y_test, ticker, model_type)
ret[ticker] = (testing_predictions, y_test, test_rmse)
# TODO remove the func call later after testing
# plotly_plot_single(testing_predictions, y_test, ticker, model_type)
return ret
def render_streamlit_page():
# These are just there to make the code work lol
ticker_list = list()
training_tickers = list()
testing_tickers = [target_ticker]
# Using saved model files
lstm_model = mtm.LSTM(INPUT_DIM, HIDDEN_DIM, NUM_LAYERS, OUTPUT_DIM)
lstm_model.load_state_dict(torch.load('./models/lstm_multiple_ticker2.pt', map_location=torch.device('cpu')))
lstm_model.eval()
gru_model = mtm.GRU(INPUT_DIM, HIDDEN_DIM, NUM_LAYERS, OUTPUT_DIM)
gru_model.load_state_dict(torch.load('./models/gru_multiple_ticker.pt', map_location=torch.device('cpu')))
gru_model.eval()
ticker_dataframes_map = mtm.get_data_frames(tuple(ticker_list), tuple(training_tickers), tuple(testing_tickers), False)
prices = mtm.preprocess(ticker_dataframes_map)
print("-------------------")
print("prices")
print(prices)
print("-------------------")
x_train, y_train, x_test, y_test = mtm.sequence_and_split(prices, SEQ_LENGTH, False)
x_train_copy = copy.deepcopy(x_train)
y_train_copy = copy.deepcopy(y_train)
ret = eval_model(lstm_model, x_train_copy, y_train_copy, x_test, y_test, "LSTM")
ret_gru = eval_model(gru_model, x_train_copy, y_train_copy, x_test, y_test, "GRU")
print("ret")
print(ret)
ret = list(ret.items())[0][1]
ret_gru = list(ret_gru.items())[0][1]
f = plotly_plot_single(ret[0], ret[1], target_ticker, 'LSTM')
st.plotly_chart(f, use_container_width=False)
st.write(f"RMSE for LSTM for ticker {target_ticker}: {ret[2]} USD")
f2 = plotly_plot_single(ret_gru[0], ret_gru[1], target_ticker, 'GRU')
st.plotly_chart(f2, use_container_width=False)
st.write(f"RMSE for GRU for ticker {target_ticker}: {ret_gru[2]} USD")
if __name__ == "__main__":
render_streamlit_page()