This file contains hidden or 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
import requests | |
from io import BytesIO | |
wpi1 = requests.get("https://www.stata-press.com/data/r12/wpi1.dta").content | |
data = pd.read_stata(BytesIO(wpi1)) | |
ts_wpi = data.set_index("t").wpi | |
tsplot(ts_wpi) |
This file contains hidden or 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
param_grid = {"window_length": [5, 10, 15, 20, 25, 30]} | |
forecaster = create_forecaster() | |
nl_lgb_mae, nl_lgb_mape = grid_serch_forecaster(nl_train, nl_test, forecaster, param_grid) |
This file contains hidden or 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
fh = np.arange(test_len) + 1 | |
forecast = forecaster.predict(fh=fh) | |
forecast_int = forecaster.predict_interval(fh=fh, coverage=coverage)['Coverage'][coverage] | |
nl_arima_mae, nl_arima_mape = plot_forecast(nl_train, nl_test, forecast, forecast_int) |
This file contains hidden or 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
test_len = int(len(ts_nl) * 0.3) | |
nl_train, nl_test = ts_nl.iloc[:-test_len], ts_nl.iloc[-test_len:] | |
forecaster = AutoARIMA(suppress_warnings=True) | |
forecaster.fit(nl_train) | |
forecaster.summary() |
This file contains hidden or 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
ts_nl_diff = (ts_nl - ts_nl.shift(1)).dropna() | |
tsplot(ts_nl_diff) |
This file contains hidden or 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
ts_nl = sm.datasets.get_rdataset("Nile").data | |
ts_nl = ts_nl.set_index('time').value | |
tsplot(ts_nl) |
This file contains hidden or 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
from sktime.forecasting.compose import make_reduction, TransformedTargetForecaster | |
from sktime.forecasting.model_selection import ExpandingWindowSplitter, ForecastingGridSearchCV | |
from sktime.performance_metrics.forecasting import MeanAbsolutePercentageError | |
import lightgbm as lgb | |
def create_forecaster(): | |
# creating forecaster with LightGBM | |
regressor = lgb.LGBMRegressor() | |
forecaster = make_reduction(regressor, window_length=5, strategy="recursive") |
This file contains hidden or 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
fh = np.arange(test_len) + 1 | |
forecast, forecast_int = forecaster.predict(fh=fh, return_pred_int=True, alpha=0.05) | |
sun_arima_mae, sun_arima_mape = plot_forecast(sun_train, sun_test, forecast, forecast_int) |
This file contains hidden or 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
from sklearn.metrics import mean_absolute_error | |
from sklearn.metrics import mean_absolute_percentage_error | |
def plot_forecast(series_train, series_test, forecast, forecast_int=None): | |
mae = mean_absolute_error(series_test, forecast) | |
mape = mean_absolute_percentage_error(series_test, forecast) | |
plt.figure(figsize=(12, 6)) |
This file contains hidden or 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
from sktime.forecasting.arima import AutoARIMA | |
forecaster = AutoARIMA(start_p=8, max_p=9, suppress_warnings=True) | |
sun_train.index = sun_train.index.astype(int) | |
forecaster.fit(sun_train) | |
forecaster.summary() |