# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import warnings
import itertools
import pandas as pd

import numpy as np
import statsmodels.api as sm
import matplotlib.pyplot as plt
#import pandas_datareader.data
# import pandas_datareader.data as web

import seaborn as sns
# from pylab import rcParams
# from pandas import datetime
# from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
# from statsmodels.tsa.stattools import adfuller 
from statsmodels.tsa.arima_model import ARIMA 
sns.set_style("whitegrid")
# -

path = '../Data/df2.csv'

df1 = pd.read_csv(path,  parse_dates=['Date'])
df1 = df.rename(columns = {"Date":"ds","Close":"y"}) 
df1['ds'] = pd.to_datetime(df1['ds'])
df1

import datetime as datetime
ctf_date = datetime.datetime(2020, 3, 1)
df = df1.loc[df1.ds < ctf_date, ].copy()
df

pred_periods = 252
cutoff = len(df) - 252
df_train = df[:cutoff].copy()
df_test = df[cutoff:].copy()
print(cutoff)

df.columns[1:]

# +
#feature normalization

normal_constants = []

for col in df.columns[1:]:
    tmp_max = np.max(abs(df[col]))
    normal_constants.append(tmp_max)
    df[col]= df[col]/tmp_max

print(normal_constants)

print(df.head())
# -





# +
# # processing data, removing the space in coumn names
# df= pd.read_csv('../data/df.csv')
# new_name = [name.strip() for name in total_data.columns]
# total_data.columns = new_name
# total_data = total_data.sort_values(by='Date')

# #extract features to use
# sub_feature = ['Date','Close','bond','fed funds','fed total assets']
# total_data = total_data[sub_feature]


# total_data=total_data.iloc[:10000]

# #change the columns names for fitting prophet()
# total_data.columns = ['ds','y','bond','fed funds','fed total assets']
 


# total_data


# +
#feature normalization

# normal_constants = []

# for col in total_data.columns[1:]:
#     tmp_max = np.max(abs(total_data[col]))
#     normal_constants.append(tmp_max)
#     df[col]= df[col]/tmp_max

# print(normal_constants)

# print(df.head())


# +
#data = df.sort_index(ascending=True, axis=0)


training = df_train['y']
validation = df_test['y']

#model = auto_arima(training, start_p=1, start_q=1,max_p=3, max_q=3, m=12,start_P=0, seasonal=True,d=1, D=1, trace=True,error_action='ignore',suppress_warnings=True)
#model.fit(training)

#forecast = model.predict(n_periods=248)
#forecast = pd.DataFrame(forecast,index = valid.index,columns=['Prediction'])

# +
from pmdarima.arima import auto_arima



# +
model = auto_arima(training, start_p=1, start_q=1,max_p=3, max_q=3, m=12,start_P=0, seasonal=True,d=1, D=1, trace=True,error_action='ignore',suppress_warnings=True)


# -

model.fit(training)

arima_forecast = model.predict(n_periods=pred_size)
forecast = pd.DataFrame(forecast,index = test_data.index,columns=['Prediction'])

#test1 = test_data[:248]
rms=np.sqrt(np.mean(np.power((np.array(test_data['y'])-np.array(forecast['Prediction'])),2)))
print(rms)

#plot
plt.plot(train_data['y'])
plt.plot(test_data['y'])
plt.plot(forecast['Prediction'])









StarcoderdataJupyterScriptsDedupFiltered