Lirsen Myrtaj
commited on
Commit
·
0558ebf
1
Parent(s):
4434d8b
Upload arima.py
Browse files
arima.py
CHANGED
@@ -4,11 +4,13 @@ from bs4 import BeautifulSoup
|
|
4 |
import io
|
5 |
import yfinance as yf
|
6 |
from datetime import datetime
|
|
|
7 |
import numpy as np
|
8 |
import statsmodels.api as sm
|
9 |
|
10 |
-
|
11 |
-
import
|
|
|
12 |
|
13 |
# from statistics import covariance
|
14 |
|
@@ -50,6 +52,9 @@ def get_model_accuracy(data, ticker_symbol):
|
|
50 |
|
51 |
def main(tickers, earliest_date):
|
52 |
df = pd.read_csv('data_and_sp500.csv')
|
|
|
|
|
|
|
53 |
for ticker in tickers:
|
54 |
x = np.array(df['Date'])
|
55 |
y = np.array(df[ticker])
|
@@ -59,20 +64,25 @@ def main(tickers, earliest_date):
|
|
59 |
model_fit = model.fit(disp=-1)
|
60 |
# print(model_fit.summary())
|
61 |
forecast = model_fit.forecast(7, alpha=0.05)#.predict(start=1259, end=1289)
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
65 |
# plt.plot(fit1.fittedvalues, marker="o", color="blue", label='smoothing')
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
|
73 |
-
plt.title('ARIMA forecast model vs. actual for {}'.format('ticker'))
|
74 |
-
plt.xlim(df.shape[0]-
|
75 |
-
plt.show()
|
76 |
# plot_df = data.to_frame().reset_index().rename(columns={'index': 'date', 0: 'price'})
|
77 |
# plot_df.columns = ['date', 'price']
|
78 |
# print(plot_df)
|
|
|
4 |
import io
|
5 |
import yfinance as yf
|
6 |
from datetime import datetime
|
7 |
+
from datetime import timedelta
|
8 |
import numpy as np
|
9 |
import statsmodels.api as sm
|
10 |
|
11 |
+
import plotly.express as px
|
12 |
+
import plotly.graph_objects as go
|
13 |
+
# import matplotlib.pyplot as plt
|
14 |
|
15 |
# from statistics import covariance
|
16 |
|
|
|
52 |
|
53 |
def main(tickers, earliest_date):
|
54 |
df = pd.read_csv('data_and_sp500.csv')
|
55 |
+
df = df[['Date']+tickers]
|
56 |
+
fig = px.line(df, x='Date', y=df.columns)
|
57 |
+
|
58 |
for ticker in tickers:
|
59 |
x = np.array(df['Date'])
|
60 |
y = np.array(df[ticker])
|
|
|
64 |
model_fit = model.fit(disp=-1)
|
65 |
# print(model_fit.summary())
|
66 |
forecast = model_fit.forecast(7, alpha=0.05)#.predict(start=1259, end=1289)
|
67 |
+
begin_date = datetime.strptime('2021-10-22', '%Y-%m-%d')
|
68 |
+
forecast_dates = [begin_date+timedelta(days=i-1258) for i in forecast.index]
|
69 |
+
fig.add_trace(go.Scatter(x=forecast_dates, y=forecast.to_list(),
|
70 |
+
mode='lines',
|
71 |
+
name='{} forecast'.format(ticker)))
|
72 |
+
|
73 |
+
fig.update_xaxes(range=[begin_date-timedelta(days=120), begin_date+timedelta(days=10)])
|
74 |
+
fig.show()
|
75 |
# plt.plot(fit1.fittedvalues, marker="o", color="blue", label='smoothing')
|
76 |
|
77 |
+
# plt.plot(x, y, label='{} historical'.format(ticker))
|
78 |
+
# plt.plot(forecast, label='{} forecast'.format(ticker))
|
79 |
+
# plt.legend(loc="upper left")
|
80 |
+
# step = 10
|
81 |
+
# plt.xticks([x[i+step] for i in range(-step, len(x), step) if i+step < len(x)], rotation=90)
|
82 |
|
83 |
+
# plt.title('ARIMA forecast model vs. actual for {}'.format('ticker'))
|
84 |
+
# plt.xlim(df.shape[0]-21, df.shape[0]+21)
|
85 |
+
# plt.show()
|
86 |
# plot_df = data.to_frame().reset_index().rename(columns={'index': 'date', 0: 'price'})
|
87 |
# plot_df.columns = ['date', 'price']
|
88 |
# print(plot_df)
|