import numpy as np import pandas as pd import matplotlib.pyplot as plt import statsmodels.api as sm from tsdata import generate_sample_data sample_ts, test_ts = generate_sample_data(trend=0.2, undiff=True) ts_fig, ts_ax = plt.subplots(tight_layout=True) sample_ts.plot(ax=ts_ax, c="b", label="Observed") ts_ax.set_title("Training time series data") ts_ax.set_xlabel("Date") ts_ax.set_ylabel("Value") diffs = sample_ts.diff().dropna() ap_fig, (acf_ax, pacf_ax) = plt.subplots(2, 1, tight_layout=True, sharex=True) sm.graphics.tsa.plot_acf(diffs, ax=acf_ax) sm.graphics.tsa.plot_pacf(diffs, ax=pacf_ax) acf_ax.set_ylabel("Value") pacf_ax.set_xlabel("Lag") pacf_ax.set_ylabel("Value")
import matplotlib.pyplot as plt import statsmodels.api as sm from tsdata import generate_sample_data sample_ts, _ = generate_sample_data() ts_fig, ts_ax = plt.subplots(tight_layout=True) sample_ts.plot(ax=ts_ax, label="Observed") ts_ax.set_title("Time series data") ts_ax.set_xlabel("Date") ts_ax.set_ylabel("Value") adf_results = sm.tsa.adfuller(sample_ts) adf_pvalue = adf_results[1] print("Augmented Dickey-Fuller test:\nP-value:", adf_pvalue) ap_fig, (acf_ax, pacf_ax) = plt.subplots(2, 1, sharex=True, tight_layout=True) sm.graphics.tsa.plot_acf(sample_ts, ax=acf_ax, title="Observed autocorrelation") sm.graphics.tsa.plot_pacf(sample_ts, ax=pacf_ax, title="Observed partial autocorrelation") pacf_ax.set_xlabel("Lags") pacf_ax.set_ylabel("Value") acf_ax.set_ylabel("Value") arma_model = sm.tsa.ARMA(sample_ts, order=(1, 1))
import pandas as pd import matplotlib.pyplot as plt from fbprophet import Prophet from tsdata import generate_sample_data sample_ts, test_ts = generate_sample_data(undiff=True, trend=0.2) df_for_prophet = pd.DataFrame({ "ds": sample_ts.index, # dates "y": sample_ts.values # values }) model = Prophet() model.fit(df_for_prophet) forecast_df = model.make_future_dataframe(periods=50) forecast = model.predict(forecast_df) fig, ax = plt.subplots(tight_layout=True) sample_ts.plot(ax=ax, label="Observed", title="Forecasts") forecast.plot(x="ds", y="yhat", ax=ax, c="r", label="Predicted") ax.fill_between(forecast["ds"].values, forecast["yhat_lower"].values, forecast["yhat_upper"].values, color="r", alpha=0.4) test_ts.plot(ax=ax, c="k", label="Future") ax.legend() ax.set_xlabel("Date")
appear at regular intervals. This behavior is called seasonality in the analysis of time series. It is relatively easy to adapt the standard ARIMA model to incorporate seasonality, resulting in what is sometimes called a SARIMA model. This module illustrates how to model time series data that includes seasonal behavior and use this model to produce forecasts. """ import numpy as np import pandas as pd import matplotlib.pyplot as plt import statsmodels.api as sm from tsdata import generate_sample_data sample_ts, test_ts = generate_sample_data(undiff=True, seasonal=True) ts_fig, ts_ax = plt.subplots(tight_layout=True) sample_ts.plot(ax=ts_ax, title="Time series", label="Observed") ts_ax.set_xlabel("Date") ts_ax.set_ylabel("Value") ap_fig, (acf_ax, pacf_ax) = plt.subplots(2, 1, sharex=True, tight_layout=True) sm.graphics.tsa.plot_acf(sample_ts, ax=acf_ax) sm.graphics.tsa.plot_pacf(sample_ts, ax=pacf_ax) pacf_ax.set_xlabel("Lag") pacf_ax.set_ylabel("Value") acf_ax.set_ylabel("Value") diffs = sample_ts.diff().dropna() dap_fig, (dacf_ax, dpacf_ax) = plt.subplots(2,