<jupyter_start><jupyter_text>The curve is smoother than the previous one<jupyter_code>plot_graph(data.close)<jupyter_output><empty_output><jupyter_text>Cheking for stationarity of time series<jupyter_code>from statsmodels.tsa.stattools import adfuller

#Ho: It is not stationary
#H1: It is stationary

def adfuller_test(records):
    result=adfuller(records)
    labels=['ADF Test Statistic','p-value','#Lags Used','Number of Observations Used']
    for label,value in zip(labels,result):
        print(label+" : "+str(value))
    if result[1]<=0.05:
        print("Null hypothesis is rejected. Series is stationary.")
    else:
        print("Null hypothesis is accepted. Series in not stationary.")
adfuller_test(data.close)
first_difference=data['close']-data['close'].shift(1)
adfuller_test(first_difference.dropna())
from statsmodels.graphics.tsaplots import plot_acf,plot_pacf

fig = plt.figure(figsize=(12,8))
ax1 = fig.add_subplot(211)
fig = plot_acf(data['close'].dropna(),lags=60,ax=ax1)
ax2 = fig.add_subplot(212)
fig = plot_pacf(data['close'].dropna(),lags=60,ax=ax2)
from statsmodels.tsa.arima_model import ARIMA

model=ARIMA(data['close'],order=(10,1,0))
model_fit=model.fit(disp=0)
print(model_fit.summary())
residuals = pd.DataFrame(model_fit.resid)
residuals.plot()
residuals.plot(kind='kde')
residuals.describe()
X=data.close.values
len(X)
size = int(len(X) * 0.66)  #splitting of data into train and test set
train, test = X[0:size], X[size:len(X)]
actual_train = [x for x in train]
predictions=list()
for t in range(len(test)):
    model = ARIMA(actual_train, order=(10,1,0))
    model_fit = model.fit(disp=0)
    output = model_fit.forecast()
    prediction = output[0]
    predictions.append(prediction)
    test_value = test[t]
    actual_train.append(test_value)
    print('%d. predicted=%f, expected=%f'%(t+1,prediction,test_value))
from sklearn.metrics import mean_squared_error
import math

error=mean_squared_error(test,predictions)
rmse=math.sqrt(error)
print(f'The mean squared error is {error}')
print(f'The rmse is {rmse}')
rng=data.iloc[250:,:].index
pred=pd.DataFrame(data=predictions[5:],index=rng,columns=['prediction'])<jupyter_output><empty_output><jupyter_text>The model worked well in test data<jupyter_code>plt.figure(figsize=(20,12))
plt.plot(data,label="real")
plt.plot(pred,color='red',label='predicted')
plt.legend()<jupyter_output><empty_output><jupyter_text>Forecasting closing price for next 20 weeks.It is to be kept in mind that as we keep moving in forecasting period(towards the end of 20 weeks period), the       uncertainity in the prediction increases.I have implemented 2 methods of forecasting:* using forecast() function of ARIMA. * training ARIMA again for next 20 weeks, appending the predicted value in training list.<jupyter_code>forecast_20=model_fit.forecast(steps=20)
rng_20=pd.date_range(start="2020-03-23",end="2020-08-12",freq='W')
len(rng_20)
rng_20
forecast=pd.DataFrame(data=forecast_20[0],index=rng_20,columns=['forecast'])
forecast.head(10)
from tqdm import tqdm<jupyter_output><empty_output><jupyter_text>Method 2<jupyter_code>forecasts=list()
forecast_train=actual_train.copy()
for t in tqdm(range(20)):
    model2 = ARIMA(forecast_train, order=(10,1,0))
    model_fit2 = model2.fit(disp=0)
    output = model_fit2.forecast()
    forecast = output[0]
    forecasts.append(forecast)
    forecast_train.append(forecast)
sample_forecasted=pd.DataFrame(data=forecasts,index=rng_20,columns=['forecast'])
sample_forecasted.plot()
forecast.plot()<jupyter_output><empty_output><jupyter_text>The 2nd method seems to be working well. So code in kernels below uses the prediction from this method.I have spliced the dataframe to get recent data(>=2019) and concatenated it with predicted value. If concatenation is not to done, there occurs discontinuity in the graph. This is because two different dataframes are plotted, terminating value of 1st and initiating value of 2nd dataframe doesn't coincide at a point resulting in discontinuity.<jupyter_code>recent_data=data["2019":].copy()
recent_data.columns=['forecast']
recent_data.head()<jupyter_output><empty_output><jupyter_text>Discontinuous Graph<jupyter_code>plt.figure(figsize=(20,12))
plt.plot(recent_data,label="real")
plt.plot(sample_forecasted,color='red',label='forecasted')
plt.legend()
collective_data=pd.concat([recent_data,sample_forecasted])
collective_data.head()
from datetime import datetime
plt.figure(figsize=(20,12))
plt.plot(collective_data)
plt.annotate("Forecast begins here",
             xy= (datetime(2020,3,29), 702),
             arrowprops=dict(arrowstyle="->"),
             xytext=(datetime(2020,3,29), 720))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13057392788564112, 'fraction_numerical': 0.030083208875613398, 'mean_word_length': 6.865771812080537, 'pattern_counts': {'":': 1, '<': 30, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '404676', 'license': 'MIT', 'n_tokens_mistral': 1735, 'n_tokens_neox': 1611, 'n_words': 340, 'path': 'time_series_ARIMA.ipynb', 'repo_name': 'samiptimalsena/Time_Series_FbProphet', 'size': '4687'}