1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| import numpy as np import pandas as pd import matplotlib.pyplot as plt from statsmodels.tsa.arima.model import ARIMA
np.random.seed(42) time = np.arange(100) data = 2 * np.sin(0.2 * time) + np.random.normal(0, 1, size=100)
df = pd.DataFrame({'Time': time, 'Data': data})
plt.plot(df['Time'], df['Data']) plt.xlabel('Time') plt.ylabel('Data') plt.title('Synthetic Time Series Data') plt.show()
order = (1, 1, 1) model = ARIMA(df['Data'], order=order) results = model.fit()
forecast_steps = 10 forecast = results.get_forecast(steps=forecast_steps)
plt.plot(df['Time'], df['Data'], label='Original Data') plt.plot(np.arange(100, 100 + forecast_steps), forecast.predicted_mean, label='Forecast', color='red') plt.fill_between(np.arange(100, 100 + forecast_steps), forecast.conf_int()['lower Data'], forecast.conf_int()['upper Data'], color='red', alpha=0.2) plt.xlabel('Time') plt.ylabel('Data') plt.title('ARIMA Forecasting') plt.legend() plt.show()
|