Beispiel #1
0
def test_plot(data, data_pd, close_figures):
    mod = MSTL(endog=data, periods=5)
    res = mod.fit()
    res.plot()

    mod = MSTL(endog=data_pd, periods=5)
    res = mod.fit()
    res.plot()
Beispiel #2
0
def test_return_pandas_series_when_input_pandas_and_len_periods_one(data_pd):
    mod = MSTL(endog=data_pd, periods=5)
    res = mod.fit()
    assert isinstance(res.trend, pd.Series)
    assert isinstance(res.seasonal, pd.Series)
    assert isinstance(res.resid, pd.Series)
    assert isinstance(res.weights, pd.Series)
Beispiel #3
0
def test_output_similar_to_R_implementation(data_pd, mstl_results):
    mod = MSTL(
        endog=data_pd,
        periods=(24, 24 * 7),
        stl_kwargs={
            "seasonal_deg": 0,
            "seasonal_jump": 1,
            "trend_jump": 1,
            "trend_deg": 1,
            "low_pass_jump": 1,
            "low_pass_deg": 1,
            "inner_iter": 2,
            "outer_iter": 0,
        },
    )
    res = mod.fit()

    expected_observed = mstl_results["Data"]
    expected_trend = mstl_results["Trend"]
    expected_seasonal = mstl_results[["Seasonal24", "Seasonal168"]]
    expected_resid = mstl_results["Remainder"]

    assert_allclose(res.observed, expected_observed)
    assert_allclose(res.trend, expected_trend)
    assert_allclose(res.seasonal, expected_seasonal)
    assert_allclose(res.resid, expected_resid)
Beispiel #4
0
def test_output_invariant_to_period_order(
    data,
    periods_ordered,
    windows_ordered,
    periods_not_ordered,
    windows_not_ordered,
):
    mod1 = MSTL(endog=data, periods=periods_ordered, windows=windows_ordered)
    res1 = mod1.fit()
    mod2 = MSTL(endog=data,
                periods=periods_not_ordered,
                windows=windows_not_ordered)
    res2 = mod2.fit()

    assert_equal(res1.observed, res2.observed)
    assert_equal(res1.trend, res2.trend)
    assert_equal(res1.seasonal, res2.seasonal)
    assert_equal(res1.resid, res2.resid)
Beispiel #5
0
def test_stl_kwargs_smoke(data):
    stl_kwargs = {
        "period": 12,
        "seasonal": 15,
        "trend": 17,
        "low_pass": 15,
        "seasonal_deg": 0,
        "trend_deg": 1,
        "low_pass_deg": 1,
        "seasonal_jump": 2,
        "trend_jump": 2,
        "low_pass_jump": 3,
        "robust": False,
        "inner_iter": 3,
        "outer_iter": 3,
    }
    periods = (5, 6, 7)
    mod = MSTL(endog=data,
               periods=periods,
               lmbda="auto",
               stl_kwargs=stl_kwargs)
    mod.fit()
Beispiel #6
0
def test_auto_fit_with_box_cox(data):
    periods = (5, 6, 7)
    mod = MSTL(endog=data, periods=periods, lmbda="auto")
    mod.fit()
    assert hasattr(mod, "est_lmbda")
    assert isinstance(mod.est_lmbda, float)
Beispiel #7
0
def test_fit_with_box_cox(data, lmbda):
    periods = (5, 6, 7)
    mod = MSTL(endog=data, periods=periods, lmbda=lmbda)
    mod.fit()
Beispiel #8
0
def test_number_of_seasonal_components(data, periods, windows, expected):
    mod = MSTL(endog=data, periods=periods, windows=windows)
    res = mod.fit()
    n_seasonal_components = (res.seasonal.shape[1]
                             if res.seasonal.ndim > 1 else res.seasonal.ndim)
    assert n_seasonal_components == expected
Beispiel #9
0
def test_seasonal_is_datafame_when_input_pandas_and_multiple_periods(data_pd):
    mod = MSTL(endog=data_pd, periods=(3, 5))
    res = mod.fit()
    assert isinstance(res.seasonal, pd.DataFrame)
Beispiel #10
0
prob = stats.probplot(comparison["stats-non-standardised"], dist=stats.norm, plot=ax2)
ax2.set_title('Probplot after Yeo-Johnson transformation')

ax3 = fig.add_subplot(223)
prob = stats.probplot(comparison["standardised"], dist=stats.norm, plot=ax3)
ax3.set_title('Probplot after Yeo-Johnson transformation, standardised')
plt.show()

# seasonal_deg, the polynomial degree used by Loess to extract the seasonal component in STL (typically set to 0 or 1).
stl_kwargs = {"seasonal_deg": 0} 

# model = MSTL(data_MSTL, periods=(24, 24 * 365), stl_kwargs=stl_kwargs)
# https://arxiv.org/pdf/2107.13462.pdf
model = MSTL(data_MSTL, periods=(24, 24 * 28), stl_kwargs=stl_kwargs)

res = model.fit()

# Start with the plot from the results object `res`
plt.rc("figure", figsize=(16, 20))
plt.rc("font", size=13)
fig = res.plot()

plt.tight_layout()
plt.savefig(f"MSTL-plot.png", dpi = 300)
plt.show()

seasonal_components = res.seasonal
seasonal_trend = res.trend
seasonal_resid = res.resid
print(seasonal_components)
print(seasonal_trend)