<jupyter_start><jupyter_text>ARIMA Model<jupyter_code>import os
import sys

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from statsmodels.tsa.arima.model import ARIMA
import torch

src_path = os.path.abspath(os.path.join('../../..'))
if src_path not in sys.path:
    sys.path.append(src_path)

from src.models.arima import MultiARIMA
from src.utils.datasets import HSMD<jupyter_output><empty_output><jupyter_text>Load the Huge Stock Market DatasetFor proof of concept, load only the `Close` column `NFLX` stock. Load the data in 1 day windows (ARIMA consider the series 1 data point at a time).<jupyter_code>dataset = HSMD('../../../data/HSMD', stocks=['nflx'], columns=['Close'], window_size=1)<jupyter_output><empty_output><jupyter_text>Split the data into a train and test Use 80% of the data for the training set, and 20% of the data for the test set. Seed the PRNG to get the same split every time, for the purpose of effective cross-validation.<jupyter_code>TRAIN_PCT = 0.8
train_size = int(len(dataset) * TRAIN_PCT)
test_size = len(dataset) - train_size
generator = torch.Generator().manual_seed(1)
train_set, test_set = torch.utils.data.random_split(dataset, [train_size, test_size], generator=generator)
# Reformat the data before feeding it to ARIMA
history = [x.astype(float).flatten() for x in train_set]<jupyter_output><empty_output><jupyter_text>Train an ARIMA model and summarize the fit<jupyter_code>model = ARIMA(history, order=(5, 1, 0))
fit = model.fit()
print(fit.summary())<jupyter_output><empty_output><jupyter_text>Plot the residuals of the fit. The residual error for the model varies from the -100 to +150 range.The distribution of the residuals seems to be centered below 0, indicating the model typically underestimates the stock price.<jupyter_code>residuals = pd.DataFrame(fit.resid)
residuals.plot()
plt.show()
# density plot of residuals
residuals.plot(kind='kde')
plt.show()<jupyter_output><empty_output><jupyter_text>Evaluate the model on the test setFor each data point in the test set, we train the model on the preceeding `history` (including the train set) to calculate a fit. We create a forecast from this fit.<jupyter_code>preds = [] # predictions
gt = [] # ground truth
for window in test_set:
    window = window.astype(float).flatten()
    model = ARIMA(history, order=(5, 1, 0))
    fit = model.fit()
    output = fit.forecast()
    preds.append(output)
    gt.append(window)
    history.append(window)<jupyter_output><empty_output><jupyter_text>Visualize model predictions<jupyter_code>plt.figure(figsize=(15, 10))
time = np.arange(0, len(gt))
plt.plot(time, gt, 'g', label='Ground Truths')
plt.plot(time, preds, 'b', label='Predictions')
plt.xlabel('Time')
plt.ylabel('Stock Price')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1148479427549195, 'fraction_numerical': 0.01001788908765653, 'mean_word_length': 5.705035971223022, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '555953', 'license': 'MIT', 'n_tokens_mistral': 974, 'n_tokens_neox': 928, 'n_words': 281, 'path': 'src/models/notebooks/arima.ipynb', 'repo_name': 'aiatuci/StockBot', 'size': '2795'}