-
Notifications
You must be signed in to change notification settings - Fork 9
/
app.py
297 lines (169 loc) · 8.98 KB
/
app.py
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
from helper_functions import *
from statsmodels.tsa.seasonal import seasonal_decompose
from statsmodels.tsa.arima.model import ARIMA as arima_model
import streamlit as st
import datetime
######### initial setup
companyNames = ['AMAZON', 'APPLE', 'GOOGLE', 'META', 'NETFLIX']
st.set_page_config(page_title='TSA@streamlit', layout='wide')
st.title(':orange[Time Series Analysis]')
######### About the app ###############
markdown_about_msg = """
## Introduction
The app implements the fundamental steps in time series analysis and forecasting.
It lets you play around with key paramters and visualise their effect in the statistics and prediction. Currently the app
is using a model data set (source below)
Data source : KAGGLE : [link](https://www.kaggle.com/datasets/nikhil1e9/netflix-stock-price) to the data set
Companies used in the dataset MAANG = Meta, Apple, Amazon, Netflix and, Google
:blue[KeyWords] : **Time Series Analysis, ARMA, SeriesDecomposition, Forecasting**
"""
############ SIDEBAR for setting the parameters ##########################
with st.sidebar:
st.header(':red[Chose your Parameters]')
st.write(" :violet[Running on Streamlit version] -- " + st.__version__)
# dates = st.date_input(label=':orange[Enter the date range for the analysis]',
# value = (datetime.date(2019,1,1), datetime.date(2024,1,10)),
# min_value=,
# max_value=datetime.date(2024,1,10),
# format="YYYY-MM-DD")
minDate = st.date_input(label='Enter :orange[minimum] date for analysis', value=datetime.date(2019,1,1),
min_value=datetime.date(2018,1,1),
max_value=datetime.date(2023,1,1),format="YYYY-MM-DD")
maxDate = st.date_input(label='Enter :orange[maximum] date for analysis', value=datetime.date(2024,1,10),
min_value=datetime.date(2022,1,1),
max_value=datetime.date(2024,1,10))
if minDate > maxDate:
st.warning('Minimum Date should be earlier than maximum Date')
# minDate,maxDate = str(dates[0]), str(dates[1])
logData = st.radio(':orange[Logged Values]', options = [True, False],index=None)
company = st.radio(':orange[Chose the company to analyse]', options=companyNames)
window_size = st.slider(':orange[Chose the rolling window size]',min_value=5,max_value=50,value=28)
monthly_plot = st.button('Show Monthly Plot')
st.subheader('Lags for ACF and PACF plots')
lags = st.slider("Set the Lag", min_value=5,max_value=100,value=50)
ts_decompose_model = st.radio('# :orange[Choose Decomposition Model]', options=['additive', 'multiplicative'])
split_at = st.number_input(':orange[Split data into Train-Test starting from the end]',
min_value=100,max_value=500,value=250,step=50)
## ARIMA model parameters
st.subheader(':green[ARIMA model parameters]')
p = st.number_input(':green[p]', min_value=0,max_value=30,value=1)
d = st.number_input(':green[d]', min_value=0,max_value=30,value=1)
q = st.number_input(':green[q]', min_value=0,max_value=30,value=0)
tab1, tab2, tab3, tab4, tab5, tab6 = st.tabs(["About", "Exploration", "ARIMA models",
"Prophet", "DeepLearning Models", "Next Steps"])
with tab1:
st.markdown(markdown_about_msg)
def load_data(logData):
df = create_AnalysisData(companyNames,minDate,maxDate,logData)
return df
DF = load_data(logData)
col1,col2 = st.columns(2,gap="medium")
with col1:
st.subheader(':orange[Data frame head]')
st.dataframe(DF.head(7))
with col2:
st.subheader(f':orange[Time Series View]',)
st.line_chart(DF)
with st.expander(':orange[Expand to see the summary statistics]'):
st.write((DF.describe()))
st.divider()
##### End Message
st.markdown(':orange[:heart: and 🕊️ for all - Chakresh]')
with tab2:
st.subheader(f'Analysis for :orange[{company}] time series Data: ')
timeSeriesData = DF[company]
timeSeriesData_rolling = timeSeriesData.rolling(window_size).mean().dropna()
st.line_chart(timeSeriesData_rolling)
if monthly_plot:
st.subheader(f'Monthly price plot for :orange[{company}] data')
show_monthly_sale(timeSeriesData)
st.divider()
st.subheader('Time Series Decomposition')
st.write(f'with decomposition model as :blue[{ts_decompose_model}]')
ts_decomposition = seasonal_decompose(x=timeSeriesData[:-1],model=ts_decompose_model,period=30)
T,S,R = ts_decomposition.trend, ts_decomposition.seasonal, ts_decomposition.resid
with st.expander("See the Trend, Seasonality and Residual Plots"):
st.subheader('Trend')
st.line_chart(T)
st.subheader('Seasonality')
st.line_chart(S)
st.subheader('Residual')
st.line_chart(R,width=1)
# st.header()
st.subheader('Exponential Smoothing - (Done on the rolling average series)')
st.markdown(r"""
Using `statsmodels.tsa.api` to generate different exponential smoothing.
Single Exponential Smoothing - Trend - ❌, Seasonality - ❌
$$
s_t = \alpha x_t + (1 - \alpha) s_{t-1} \\
0 \leq \alpha \leq 1
$$
Double Exponential Smoothing - Trend - ✅, Seasonality - ❌
$$
s_t = \alpha x_t + (1 - \alpha) (s_{t-1} + b_{t-1}) \\
b_t = \beta (s_t - s_{t-1}) + (1 - \beta) b_{t-1} \\
0 \leq \alpha,\beta \leq 1
$$
:orange[$\alpha$] : Data Smoothing Factor
:orange[$\beta$] : Trend Smoothing Factor
Triple Exploential Smoothing - Trend - ✅, Seasonality - ✅
$$
s_0 = x_0 \\
s_t = \alpha \frac{x_t}{c_t - L} + (1 - \alpha) (s_{t-1} + b_{t-1}) \\
b_t = \beta (s_t - s_{t-1}) + (1 - \beta) b_{t-1} \\
c_t = \gamma \frac{x_t}{s_t} + (1 - \gamma) c_{t-L} \\
0 \leq \alpha, \beta, \gamma \leq 1
$$
:orange[$\gamma$] : Seasonal change smoothing factor, :orange[$\alpha, \beta$] same as above
""")
smoothing_type = st.multiselect(':orange[Chose Smoothing Type]',options=['Single', 'Double', 'Triple'], default=['Single'])
# Creating the training and the test data. We do it outside the functions for increased scope.
training_Data,test_Data = timeSeriesData_rolling[:-split_at], timeSeriesData_rolling[-split_at:]
gen_smooth = st.button('Generate Smoothing ')
if gen_smooth:
generate_smoothing(smoothing_type,ts_decompose_model,training_Data,test_Data,timeSeriesData_rolling,split_at)
st.subheader('Autoregression Plots')
show_arplots = st.button("Show ACF and PACF plots")
if show_arplots:
autoregression_plots(timeSeriesData_rolling,lags=lags)
st.divider()
with tab3:
st.subheader('Time Series Models - ARIMA (p,d,q)')
st.markdown(""" ##### Before we fit ARIMA models we run stationarity tests on the time series""")
with st.expander('Stationarity Test (KPSS) Results'):
check_stationarity(training_Data)
arima_model = arima_model(training_Data,order=(p,d,q))
arima_resid = arima_model.fit()
with st.expander('Model fit summary and diagnostics plots for arima model'):
st.write(arima_resid.summary())
fig3 = plt.figure()
ax = fig3.add_subplot(111)
st.pyplot(arima_resid.plot_diagnostics(figsize=(12,10)))
arima_forecast = st.button('Get ARIMA forecast')
if arima_forecast:
arima_preds,conf_90,conf_95 = get_arima_forecast(arima_resid,split_at)
train_time,test_time = timeSeriesData_rolling.index.values[:-split_at], timeSeriesData_rolling.index.values[-split_at:]
plot_arima_forecast(arima_preds,conf_90,conf_95,p,d,q,company,training_Data,test_Data,train_time,test_time)
st.divider()
with tab4:
st.subheader('Facebook - Prophet')
prophet_prediction(timeSeriesData_rolling,company)
st.divider()
with tab5:
# st.header('Deep Learning')
st.write("""
Coming soon ..........
""")
st.divider()
with tab6:
next_steps = """
### :red[what to expect in future versions]
1. `Load Data` option to use the app for analysing any time-series.
2. `User defined` arcitecture for the RNN and LSTM models
3. `SARIMA models`
4. `Theoretical` explanantion for the parameters search in statistical models
5. `Scraping` data from online
6. Implementation of `quantum algorithms` in forecasting.
"""
st.markdown(next_steps)
st.divider()