Skip to content

Commit deb5c36

Browse files
committed
arima article draft
1 parent 5ecbecb commit deb5c36

File tree

52 files changed

+2398
-319
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+2398
-319
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import numpy as np
2+
import pandas as pd
3+
import matplotlib.pyplot as plt
4+
from statsmodels.tsa.arima.model import ARIMA
5+
from statsmodels.tsa.stattools import adfuller
6+
from sklearn.metrics import mean_squared_error
7+
import yfinance as yf
8+
9+
# Download stock data
10+
ticker = "MSFT"
11+
start_date = "2018-01-01"
12+
end_date = "2023-06-23"
13+
data = yf.download(ticker, start=start_date, end=end_date)
14+
15+
# Prepare the data
16+
ts = data['Close']
17+
18+
# Check for stationarity
19+
def test_stationarity(timeseries):
20+
result = adfuller(timeseries, autolag='AIC')
21+
print('ADF Statistic:', result[0])
22+
print('p-value:', result[1])
23+
24+
test_stationarity(ts)
25+
26+
# If non-stationary, difference the series
27+
ts_diff = ts.diff().dropna()
28+
test_stationarity(ts_diff)
29+
30+
# Fit ARIMA model
31+
model = ARIMA(ts_diff, order=(1,1,1))
32+
results = model.fit()
33+
print(results.summary())
34+
35+
# Forecast
36+
forecast = results.forecast(steps=30)
37+
help(forecast)
38+
39+
# Plot the results
40+
plt.figure(figsize=(12,6))
41+
plt.plot(ts.index[-100:], ts.values[-100:], label='Observed')
42+
plt.plot(forecast.index, forecast.values, color='r', label='Forecast')
43+
plt.fill_between(forecast.index,
44+
forecast.conf_int().iloc[:, 0],
45+
forecast.conf_int().iloc[:, 1],
46+
color='pink', alpha=0.3)
47+
plt.title(f'{ticker} Stock Price Prediction')
48+
plt.legend()
49+
plt.show()
50+
51+
# Evaluate the model
52+
mse = mean_squared_error(ts.diff().dropna()[-30:], forecast)
53+
print(f'Mean Squared Error: {mse}')

0 commit comments

Comments
 (0)