<jupyter_start><jupyter_text><jupyter_code>!pip install statsmodels
from pandas import read_csv
from pandas import datetime
from pandas import DataFrame
from pandas import Series
from statsmodels.tsa.arima_model import ARIMA
from statsmodels.tsa.stattools import acf
from matplotlib import pyplot
from sklearn.metrics import mean_squared_error
from math import sqrt
from numpy import *
from google.colab import drive
drive.mount('/content/drive/')
# load dataset
def parser(x):
	return datetime.strptime(str(x), '%Y/%m/%d-%H')
 
series = read_csv('PUB_Demand_2021.csv', header=0, index_col=0, parse_dates=True, squeeze=True, date_parser=parser)
series.index = series.index.to_period('H')
series.drop(series.columns[1], axis=1, inplace=True)
series.head(15)
from pandas.plotting import autocorrelation_plot
autocorrelation_plot(series)
pyplot.show()
# fit model
model = ARIMA(series, order=(5,2,2))
model_fit = model.fit()
# summary of fit model
print(model_fit.summary())
# line plot of residuals
residuals = DataFrame(model_fit.resid)
residuals.plot()
pyplot.show()
# density plot of residuals
residuals.plot(kind='kde')
pyplot.show()
# summary stats of residuals
print(residuals.describe())
# split into train and test sets
X = series.values
size = int(len(X) * 0.75)
train, test = X[0:size], X[size:len(X)]

for i in range(2,6):
	for j in range(1,3):
		history = [x for x in train]
		predictions = list()

		for t in range(len(test)):
			model = ARIMA(history, order=(i,2,j))
			model_fit = model.fit()
			output = model_fit.forecast()
			yhat = output[0]
			predictions.append(yhat)
			obs = test[t]
			history.append(obs)
			#print('predicted=%f, expected=%f' % (yhat, obs))
			#print(yhat)
		
		# evaluate forecasts
		rmse = sqrt(mean_squared_error(test, predictions))
		rmse_normalized = rmse/(max(test)-min(test))
		print(i, j, rmse_normalized)

		# plot forecasts against actual outcomes
		pyplot.plot(test)
		pyplot.plot(predictions, color='red')
		pyplot.show()
# # Forecast
# fitted = model.fit(disp=-1)
# fc, se, conf = fitted.forecast(15, alpha=0.05)  # 95% conf

# Make as pandas series
fc_series = Series(fc, index=test.index)
lower_series = Series(conf[:, 0], index=test.index)
upper_series = Series(conf[:, 1], index=test.index)

# # Plot
# plt.figure(figsize=(12,5), dpi=100)
# plt.plot(train, label='training')
# plt.plot(test, label='actual')
# plt.plot(fc_series, label='forecast')
# plt.fill_between(lower_series.index, lower_series, upper_series, 
#                  color='k', alpha=.15)
# plt.title('Forecast vs Actuals')
# plt.legend(loc='upper left', fontsize=8)
# plt.show()

# Build Model
model = ARIMA(train, order=(3, 2, 1))  
fitted = model.fit(disp=-1)  
print(fitted.summary())

# Forecast
fc, se, conf = fitted.forecast(15, alpha=0.05)  # 95% conf

# Make as pandas series
fc_series = Series(fc, index=test.index)
lower_series = Series(conf[:, 0], index=test.index)
upper_series = Series(conf[:, 1], index=test.index)

# Plot
plt.figure(figsize=(12,5), dpi=100)
plt.plot(train, label='training')
plt.plot(test, label='actual')
plt.plot(fc_series, label='forecast')
plt.fill_between(lower_series.index, lower_series, upper_series, 
                 color='k', alpha=.15)
plt.title('Forecast vs Actuals')
plt.legend(loc='upper left', fontsize=8)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14849397590361446, 'fraction_numerical': 0.01927710843373494, 'mean_word_length': 5.362068965517241, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '121110', 'license': 'MIT', 'n_tokens_mistral': 1276, 'n_tokens_neox': 1190, 'n_words': 276, 'path': 'ARIMA_model.ipynb', 'repo_name': 'cowiety/energy-forecast', 'size': '3320'}