# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: 'Python 3.8.5 64-bit (''.venv'': pipenv)'
#     name: python3
# ---

# +
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from sklearn.cluster import AgglomerativeClustering, ward_tree
from sklearn.metrics import plot_confusion_matrix, accuracy_score, mean_squared_error
from sklearn.naive_bayes import GaussianNB
import yfinance as yf
import pmdarima as pm
import matplotlib.pyplot as plt

import warnings
warnings.filterwarnings("ignore")
# -

# # Aquisição dos dados
#
# |Country|Index|Ticker|
# |---        |---        |---        |
# |Brazil|iBovespa|^BVSP|
# |USA|Nasdaq Composite|^IXIC|
# |USA|NYSE Composite|^NYA|
# |USA|Dow Jones|^DJI|
# |Argentina|Merval|M.BA|
# |Japan|Nikkei 225|^N225|
# |China|Shangai Composite|000001.SS|
# |China|Hang Seng|^HSI|
# |India|BSE 30 Sensex|^BSESN|
# |UK|FTSE 100|^FTSE|
# |Sweden|Stockholm General|^OMX|
# |Germany|Dax|^GDAXI|
# |France|Cac 40|^FCHI|

# +
start_date = "2011-01-01"
end_date = "2021-05-31"

df_ibovespa = yf.download('^BVSP', start=start_date, end=end_date)

# Indices de bolsas:
# ^BVSP
# ^IXIC
# ^NYA
# ^DJI
# M.BA
# ^N225
# 000001.SS
# ^HSI
# ^BSESN
# ^FTSE
# ^OMX
# ^GDAXI
# ^FCHI


# +
fig = plt.figure(figsize=(20, 4))
ax = fig.add_subplot(111)

ax.plot(df_ibovespa['Close'], label="iBovespa")

date_min = df_ibovespa.index.min()
date_max = df_ibovespa.index.max()
ax.xaxis.set_major_locator(plt.MaxNLocator(30))
ax.set_xlim(left=date_min, right=date_max)

ax.legend(loc='upper left', frameon=False)
plt.xticks(rotation=90)
plt.show();
# -

# ## Preparação dos dados
#
# Nesta seção, tomaremos as seguintes ações:
#
# 1. Separação do conjunto de dados de treinamento (01/01/2011 até 30/04/2021) e de validação do modelo (01/05/2021 até 30/05/2021).
# 1. Criação do atributo **"p/e"**: $ p/e = 100*(ln({r_{t}}) - ln({r_{t-1}})) $.
# 1. Criação da resposta **cluster** usando apenas a simples separação. Lembrando que no artigo, os autores compararam duas abordagens diferentes para discretizar a resposta ((a) simples separação e (b) Ward).

# +

X_train = df_ibovespa[:"2021-05-01"]
X_test = df_ibovespa["2021-05-01":]
# X_train = df_ibovespa[:-Ntest]
# X_test = df_ibovespa[-Ntest:]

print(f"""Base original: {df_ibovespa.shape}
X_train: {X_train.shape}
X_test: {X_test.shape}\n""")
Ntest = X_test.shape[0]

X_train["rt"] = (np.log(X_train["Close"]) - np.log(X_train["Close"].shift(periods=1)))*100
X_test["rt"] = (np.log(X_test["Close"]) - np.log(X_test["Close"].shift(periods=1)))*100
df_ibovespa["rt"] = (np.log(df_ibovespa["Close"]) - np.log(df_ibovespa["Close"].shift(periods=1)))*100
X_train = X_train[["Close", "rt"]][1:]
X_train.head()

# +
fig = plt.figure(figsize=(20, 5))
ax = fig.add_subplot(111)

date_min = X_train.index.min()
date_max = X_train.index.max()

ax.plot(X_train['rt'], label="Nikkey")

ax.xaxis.set_major_locator(plt.MaxNLocator(13))
ax.yaxis.set_major_locator(plt.MaxNLocator(30))
ax.set_ylim(-18, 18)
ax.set_xlim(left=date_min, right=date_max)

ax.legend(loc='lower left', frameon=False)
plt.xticks(rotation=90)
plt.show();
# -

sns.histplot(data=X_train, x="rt", kde=True);
X_train['rt'].describe().T

# ### ARIMA
#
#

X_train

model = pm.auto_arima(X_train['rt'], erro_action='ignore', trace=True, suppress_warnings=True, maxinter=10, stepwise=False, seasonal=False)

model.summary()

model.get_params()


def plot_result(model, fulldata, train, test):
    params = model.get_params()
    d = params['order'][1]

    train_pred = model.predict_in_sample(start=d, end=-1)
    test_pred, confint = model.predict(n_periods=20, return_conf_int=True)

    fig, ax = plt.subplots(figsize=(10, 5))
    ax.plot(fulldata.index, fulldata, label='data')
    ax.plot(train.index[d:], train_pred, label='fitted')
    ax.plot(test.index, test_pred, label='forecast')
    ax.fill_between(test.index, confint[:,0], confint[0:,1], color='red', alpha=0.3)
    ax.legend();


plot_result(model, df_ibovespa["rt"], X_train['rt'], X_test['rt'])


def plot_test(model, test):
    test_pred, confint = model.predict(n_periods=Ntest, return_conf_int=True)

    fig, ax = plt.subplots(figsize=(10, 5))
    ax.plot(test.index, test, label='true')
    ax.plot(test.index, test_pred, label='forecast')
    ax.fill_between(test.index, confint[:,0], confint[0:,1], color='red', alpha=0.3)
    ax.legend();


plot_test(model, X_test['rt'])


def rmse(y, t):
    return np.sqrt(np.mean((t - y)**2))


predictions = model.predict(Ntest)
# predictions2 = predictions.reshape(Ntest,1)

# +
# predictions2= predictions2.reshape(1,-1)[0]
# -

print("RSME ARIMA:", rmse(predictions, X_test["rt"]))
print("RSME Naive:", rmse(X_train['rt'].iloc[-1], X_test['rt']))







# +
## Achando a estrutura da rede

StarcoderdataJupyterScriptsDedupFiltered