Created
October 1, 2022 03:57
-
-
Save arif9799/49b961bab0aeff967c0d1e62c1a015ee to your computer and use it in GitHub Desktop.
Time Series Prediction Interval - Stock Price Animation Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!pip install --upgrade pandas | |
!pip install --upgrade pandas-datareader | |
!pip install celluloid | |
from celluloid import Camera as Cam | |
from IPython.display import HTML, clear_output | |
import pandas_datareader as pdr | |
import pandas as pd | |
from statsmodels.tsa.ar_model import AutoReg | |
import numpy as np | |
import seaborn as sb | |
import matplotlib.pyplot as plt | |
from matplotlib import rcParams | |
from cycler import cycler | |
shares_df = pdr.DataReader('AAPL', 'yahoo', start='2021-01-01', end='2021-12-31') | |
data = shares_df['Close'] | |
data = data.asfreq('d') | |
data = pd.DataFrame(data.interpolate()) | |
model_fit = AutoReg(data, lags=1).fit() | |
pred = model_fit.get_prediction(start=pd.to_datetime('2022-01-01'),end = pd.to_datetime('2022-03-31'), dynamic=False) | |
predictions = pd.DataFrame(pred.predicted_mean) | |
fig, ax = plt.subplots(dpi = 400) | |
cam = Cam(fig) | |
rcParams['figure.figsize'] = 15,5 | |
rcParams['axes.spines.top'] = False | |
rcParams['axes.spines.right'] = False | |
rcParams['lines.linewidth'] = 2.5 | |
rcParams['axes.prop_cycle'] = cycler(color = ['#000080']) | |
plt.title("Stock Prices") | |
plt.xlabel("Date") | |
plt.ylabel("Price (in $)") | |
sb.despine(right = True, top = True) | |
for i in range(1,len(data)): | |
sb.lineplot(data = data.iloc[0:i], x = data.index[0:i], y = data.Close[0:i], color = '#000080') | |
cam.snap() | |
for i in range(1,len(data)): | |
sb.lineplot(data = data, x = data.index, y = data.Close, color = '#000080') | |
cam.snap() | |
plt.close(fig) | |
animation = cam.animate(blit=False, interval=15) | |
HTML(animation.to_html5_video()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment