<jupyter_start><jupyter_text>***Time Series Forecasting using ARIMA model***<jupyter_code>#https://www.machinelearningplus.com/time-series/arima-model-time-series-forecasting-python/
from google.colab import files
uploaded = files.upload()<jupyter_output><empty_output><jupyter_text>***find the order of differencing (d) in ARIMA model***<jupyter_code>from statsmodels.tsa.stattools import adfuller
from numpy import log
result = adfuller(df.value.dropna())
print('ADF Statistic: %f' % result[0])
print('p-value: %f' % result[1])

import numpy as np, pandas as pd
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
import matplotlib.pyplot as plt
plt.rcParams.update({ 'figure.figsize':(12,7), 'figure.dpi':120})

# Import data
df = pd.read_csv('TEST CASE 9.csv', names=['value'], header=0)

# Original Series
fig, axes = plt.subplots(3, 2, sharex=True)
axes[0, 0].plot(df.value); axes[0, 0].set_title('Original Series')
plot_acf(df.value, ax=axes[0, 1])

# 1st Differencing
axes[1, 0].plot(df.value.diff()); axes[1, 0].set_title('1st Order Differencing')
plot_acf(df.value.diff().dropna(), ax=axes[1, 1])

# 2nd Differencing
axes[2, 0].plot(df.value.diff().diff()); axes[2, 0].set_title('2nd Order Differencing')
plot_acf(df.value.diff().diff().dropna(), ax=axes[2, 1])

plt.xticks([])
plt.show()<jupyter_output><empty_output><jupyter_text>***Finding the order of the AR term (p)***<jupyter_code># PACF plot of 1st differenced series
import matplotlib.pyplot as plt
import statsmodels.tsa.seasonal

plt.rcParams.update({'figure.figsize':(12,3), 'figure.dpi':120})

fig, axes = plt.subplots(1, 2, sharex=True)
axes[0].plot(df.value.diff()); axes[0].set_title('1st Differencing')
axes[1].set(ylim=(0,1.5))
plot_pacf(df.value.diff().dropna(), ax=axes[1])

plt.xticks([])
plt.show()<jupyter_output><empty_output><jupyter_text>***Finding the order of the MA term (q)***<jupyter_code>import pandas as pd
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
import matplotlib.pyplot as plt
plt.rcParams.update({'figure.figsize':(12,3), 'figure.dpi':120})

# Import data
df = pd.read_csv('TEST CASE 9.csv')

fig, axes = plt.subplots(1, 2, sharex=True)
axes[0].plot(df.value.diff()); axes[0].set_title('1st Differencing')
axes[1].set(ylim=(0,1.2))
plot_acf(df.value.diff().dropna(), ax=axes[1])

plt.xticks([])
plt.show()<jupyter_output><empty_output><jupyter_text>***Differencing in the Time series***<jupyter_code># Import
import pandas as pd
import matplotlib.pyplot as plt

data = pd.read_csv('TEST CASE 9.csv', parse_dates=['date'], index_col='date')

# Plot
fig, axes = plt.subplots(2, 1, figsize=(10,7), dpi=100, sharex=True)

# Usual Differencing
axes[0].plot(data[:], label='Original Series')
axes[0].plot(data[:].diff(1), label='Usual Differencing')
axes[0].set_title('Usual Differencing')
axes[0].legend(loc='upper left', fontsize=10)


# Seasinal Dei
axes[1].plot(data[:], label='Original Series')
axes[1].plot(data[:].diff(12), label='Seasonal Differencing', color='green')
axes[1].set_title('Seasonal Differencing')
plt.legend(loc='upper left', fontsize=10)
plt.suptitle('a10 - Drug Sales', fontsize=16)
plt.show()<jupyter_output><empty_output><jupyter_text>***Forecasting with the ARIMA model*** ***importing the ARMIA model***<jupyter_code># !pip3 install pyramid-arima
!pip install pmdarima

import pmdarima as pm

data = pd.read_csv('TEST CASE 9.csv', parse_dates=['date'], index_col='date')

# Seasonal - fit stepwise auto-ARIMA
smodel = pm.auto_arima(data, start_p=1, start_q=1,
                         test='adf',
                         max_p=3, max_q=3, m=12,
                         start_P=0, seasonal=True,
                         d=None, D=1, trace=True,
                         error_action='ignore',  
                         suppress_warnings=True, 
                         stepwise=True)

smodel.summary()<jupyter_output>Requirement already satisfied: pmdarima in /usr/local/lib/python3.7/dist-packages (1.8.3)
Requirement already satisfied: setuptools!=50.0.0,>=38.6.0 in /usr/local/lib/python3.7/dist-packages (from pmdarima) (57.4.0)
Requirement already satisfied: numpy>=1.19.3 in /usr/local/lib/python3.7/dist-packages (from pmdarima) (1.19.5)
Requirement already satisfied: scikit-learn>=0.22 in /usr/local/lib/python3.7/dist-packages (from pmdarima) (0.22.2.post1)
Requirement already satisfied: Cython!=0.29.18,>=0.29 in /usr/local/lib/python3.7/dist-packages (from pmdarima) (0.29.24)
Requirement already satisfied: scipy>=1.3.2 in /usr/local/lib/python3.7/dist-packages (from pmdarima) (1.4.1)
Requirement already satisfied: urllib3 in /usr/local/lib/python3.7/dist-packages (from pmdarima) (1.24.3)
Requirement already satisfied: joblib>=0.11 in /usr/local/lib/python3.7/dist-packages (from pmdarima) (1.0.1)
Requirement already satisfied: pandas>=0.19 in /usr/local/lib/python3.7/dist-packages (from pmdarima)[...]<jupyter_text>***deploying the model***<jupyter_code># 1,1,1 ARIMA Model
df=pd.read_csv('TEST CASE 9.csv', parse_dates=['date'], index_col='date')
from statsmodels.tsa.arima_model import ARIMA

model = ARIMA(df.value, order=(1,1,1))
model_fit = model.fit(disp=0)
print(model_fit.summary())<jupyter_output>ARIMA Model Results                              
==============================================================================
Dep. Variable:                D.value   No. Observations:                  132
Model:                 ARIMA(1, 1, 1)   Log Likelihood                 -80.548
Method:                       css-mle   S.D. of innovations              0.445
Date:                Sat, 25 Sep 2021   AIC                            169.097
Time:                        16:41:57   BIC                            180.628
Sample:                    09-01-2010   HQIC                           173.783
                         - 08-01-2021                                         
=================================================================================
                    coef    std err          z      P>|z|      [0.025      0.975]
---------------------------------------------------------------------------------
const             0.2610      0.040      6.[...]<jupyter_text>***interpreting residual plots***<jupyter_code>from matplotlib.pyplot import figure

# Plot residual errors
residuals = pd.DataFrame(model_fit.resid)
fig, ax = plt.subplots(1,2)
residuals.plot(title="Residuals", ax=ax[0])
residuals.plot(kind='kde', title='Density', ax=ax[1])
plt.figure(figsize=(1,1))
plt.show()<jupyter_output><empty_output><jupyter_text>***Visualing Actual and Forecast values***<jupyter_code># Actual vs Fitted
model_fit.plot_predict(dynamic=False)
plt.show()<jupyter_output><empty_output><jupyter_text>***Forecast for the next 12 months***<jupyter_code># Forecast
n_periods = 12
fitted, confint = smodel.predict(n_periods=n_periods, return_conf_int=True)
index_of_fc = pd.date_range(data.index[-1], periods = n_periods, freq='MS')

# make series for plotting purpose
fitted_series = pd.Series(fitted, index=index_of_fc)
lower_series = pd.Series(confint[:, 0], index=index_of_fc)
upper_series = pd.Series(confint[:, 1], index=index_of_fc)

# Plot
plt.plot(data)
plt.plot(fitted_series, color='darkgreen')
plt.fill_between(lower_series.index, 
                 lower_series, 
                 upper_series, 
                 color='k', alpha=.15)

plt.title("SARIMA - Final Forecast of CPI for food (September 2021 to August 2022)")
plt.show()<jupyter_output><empty_output><jupyter_text>***Building a SARIMAX Model with exogenous variable***<jupyter_code># Import Data
data = pd.read_csv('TEST CASE 9.csv', parse_dates=['date'], index_col='date')


# Compute Seasonal Index
from statsmodels.tsa.seasonal import seasonal_decompose
from dateutil.parser import parse

# multiplicative seasonal component
result_mul = seasonal_decompose(data['value'][-36:],   # 3 years
                                model='multiplicative', 
                                extrapolate_trend='freq')

seasonal_index = result_mul.seasonal[-12:].to_frame()
seasonal_index['month'] = pd.to_datetime(seasonal_index.index).month

# merge with the base data
data['month'] = data.index.month
df = pd.merge(data, seasonal_index, how='left', on='month')
df.columns = ['value', 'month', 'seasonal_index']
df.index = data.index  # reassign the index.

import pmdarima as pm

# SARIMAX Model
sxmodel = pm.auto_arima(df[['value']], exogenous=df[['seasonal_index']],
                           start_p=1, start_q=1,
                           test='adf',
                           max_p=3, max_q=3, m=12,
                           start_P=0, seasonal=True,
                           d=None, D=1, trace=True,
                           error_action='ignore',  
                           suppress_warnings=True, 
                           stepwise=True)

sxmodel.summary()<jupyter_output>Performing stepwise search to minimize aic
 ARIMA(1,0,1)(0,1,1)[12] intercept   : AIC=inf, Time=1.10 sec
 ARIMA(0,0,0)(0,1,0)[12] intercept   : AIC=509.900, Time=0.05 sec
 ARIMA(1,0,0)(1,1,0)[12] intercept   : AIC=236.446, Time=0.61 sec
 ARIMA(0,0,1)(0,1,1)[12] intercept   : AIC=inf, Time=0.74 sec
 ARIMA(0,0,0)(0,1,0)[12]             : AIC=653.397, Time=0.09 sec
 ARIMA(1,0,0)(0,1,0)[12] intercept   : AIC=255.589, Time=0.08 sec
 ARIMA(1,0,0)(2,1,0)[12] intercept   : AIC=208.632, Time=1.56 sec
 ARIMA(1,0,0)(2,1,1)[12] intercept   : AIC=inf, Time=3.34 sec
 ARIMA(1,0,0)(1,1,1)[12] intercept   : AIC=inf, Time=1.35 sec
 ARIMA(0,0,0)(2,1,0)[12] intercept   : AIC=461.047, Time=0.78 sec
 ARIMA(2,0,0)(2,1,0)[12] intercept   : AIC=210.278, Time=2.39 sec
 ARIMA(1,0,1)(2,1,0)[12] intercept   : AIC=210.277, Time=2.77 sec
 ARIMA(0,0,1)(2,1,0)[12] intercept   : AIC=348.120, Time=1.45 sec
 ARIMA(2,0,1)(2,1,0)[12] intercept   : AIC=204.856, Time=4.17 sec
 ARIMA(2,0,1)(1,1,0)[12] intercept   : AIC=227.41[...]
{'fraction_non_alphanumeric': 0.17771237186643662, 'fraction_numerical': 0.05460265908860246, 'mean_word_length': 3.553604436229205, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 50, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '394710', 'license': 'MIT', 'n_tokens_mistral': 3878, 'n_tokens_neox': 3537, 'n_words': 669, 'path': 'Statistical analysis and Machine learning models/Consumer_Price_Index_Forecasting_Model_for_Food.ipynb', 'repo_name': 'Vancouver-Datajam/CPI', 'size': '9853'}