<jupyter_start><jupyter_text><jupyter_code>%%capture
!pip install yfinance
import numpy as np
import pandas as pd
import scipy as sp
import seaborn as sn
import matplotlib.pyplot as plt


import yfinance as yf

import statsmodels.api as sm
from statsmodels.tsa.arima_model import ARIMA
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
from statsmodels.stats.diagnostic import acorr_ljungbox
from statsmodels.tsa.stattools import adfuller, kpss
from statsmodels.tsa.arima_model import ARIMAResults

stock_ticker = 'PETR4.SA'
start_date = '2015-11-30'
end_date = '2021-5-27'

yf_petr4 = yf.Ticker(stock_ticker)
df_petr4 = yf_petr4.history(start=start_date, end=end_date)
def series_graph(df_series, column_name):
    df_series_diff = df_series[column_name].diff().dropna()
    fig, ax = plt.subplots(2, sharex=True)
    df_series[column_name].plot(title = f"{stock_ticker}'s stock price", ax=ax[0])
    df_series_diff.plot(ax=ax[1], title='First Differences',figsize=(20,10))
series_graph(df_petr4, "Close")
series_graph(df_petr4.iloc[:525], "Close")
def adf_test(df_series):
    print ('Results of Dickey-Fuller Test:')
    df_test = adfuller(df_series, autolag='AIC')
    df_output = pd.Series(df_test[0:4], index=['Test Statistic','p-value','#Lags Used','Number of Observations Used'])
    for key,value in df_test[4].items():
       df_output['Critical Value (%s)'%key] = value
    print (df_output)
adf_test(df_petr4["Close"])
adf_test(df_petr4["Close"].iloc[:525])
def acf_pacf(df_series):
    fig = plt.figure(figsize=(12,10))
    ax1 = fig.add_subplot(211)
    fig = plot_acf(df_series.values.squeeze(), lags=100, ax=ax1)
    ax2 = fig.add_subplot(212)
    fig = plot_pacf(df_series, lags=40, ax=ax2)
acf_pacf(df_petr4["Close"].diff().dropna())
acf_pacf(df_petr4["Close"].iloc[:525].diff().dropna())
arima_model_1 = ARIMA(df_petr4["Close"], order=(1, 1, 1))
arima_model_fit_1 = arima_model_1.fit()
print(arima_model_fit_1.summary())
arima_model = ARIMA(df_petr4["Close"].iloc[:525], order=(1, 1, 1))
arima_model_fit = arima_model.fit()
print(arima_model_fit.summary())<jupyter_output>ARIMA Model Results                              
==============================================================================
Dep. Variable:                D.Close   No. Observations:                  524
Model:                 ARIMA(1, 1, 1)   Log Likelihood                -145.324
Method:                       css-mle   S.D. of innovations              0.319
Date:                Tue, 01 Jun 2021   AIC                            298.649
Time:                        20:20:01   BIC                            315.695
Sample:                             1   HQIC                           305.324
                                                                              
=================================================================================
                    coef    std err          z      P>|z|      [0.025      0.975]
---------------------------------------------------------------------------------
const             0.0152      0.013      1.[...]
{'fraction_non_alphanumeric': 0.1890224098733355, 'fraction_numerical': 0.046443650535888274, 'mean_word_length': 2.5483870967741935, 'pattern_counts': {'":': 0, '<': 4, '<?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': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '135414', 'license': 'MIT', 'n_tokens_mistral': 1113, 'n_tokens_neox': 1009, 'n_words': 197, 'path': 'I2A2_PETR4_ARIMA_Forecast.ipynb', 'repo_name': 'nakanoelio/I2A2-challenge-petr4-trad-sys', 'size': '3079'}