"""
# Imports
"""
!pip install pmdarima
import pandas as pd
import numpy as np
import pandas_datareader as pdr
import datetime as dt
import matplotlib.pyplot as plt
plt.style.use('seaborn')
import seaborn as sns


from statsmodels.tsa.stattools import adfuller
from statsmodels.tsa.seasonal import seasonal_decompose
from statsmodels.tsa.statespace.sarimax import SARIMAX
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
from statsmodels.tsa.arima.model import ARIMA
from pmdarima import auto_arima
plt.rcParams['figure.figsize'] = (20,15)
ticker = "GOOG"
start = dt.datetime(2015,1,1)
df = pdr.get_data_yahoo(ticker,start=start)
df= df[['Close']]
df[:20]
df.plot()
result = seasonal_decompose(np.log(df), period=5) 
result.plot();
# Make dataframe more additive using log transform
df = np.log(df)
df
"""
# Stationarity
"""
'''Clearly, series is not stationary but we shall check it using adf test. We shall also check seasonality using acf'''
print(f'The p-value of the series is {adfuller(df.Close)[1]}')
"""
Hence, series is not stationary. Next, we check the seasonality using acf plot.
"""
plot_acf(df.dropna(), lags=40);
"""
# Auto ARIMA
"""
model = auto_arima(df, max_p=6, max_q=3, m=5, seasonal=True, max_P=4, max_Q=4, max_D=2, max_order=None,
                   d=None, trace=True, trend='ct',
                   out_of_sample_size = int(len(df)*.2),
                   error_action='ignore',   # we don't want to know if an order does not work
                   suppress_warnings=True,  # we don't want convergence warnings
                   stepwise=True) # set to stepwise)
#model_fit = model.fit()
model.summary()
model = SARIMAX(df, order=(0,2,1), seasonal_order=(0,0,0,5))
model_fit = model.fit()
model_fit.summary()
fc = model_fit.predict(-51)
plt.plot(fc, label='Forecast')
plt.plot(df[-50:], label='Original')
plt.legend()

#fc.plot()
"""
So last 51 values were forecasted quiet well. But we shall see forecast in future (for period of 30 days) is not good. 
"""
period = 30 # for forecast
fc = model_fit.forecast(period)
start = dt.date.today() + dt.timedelta(days=1)
fc_ind = pd.date_range(start=start, periods=period)
fc.index = fc_ind

# plotting last 25 + period values
plt.plot(df[-25:],label='Original')
plt.plot(fc, label='Future Forecast')
plt.legend()
"""
We will do LSTM in, TO BE SHARED soon!
"""