Esempio n. 1
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)
Esempio n. 2
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)
Esempio n. 3
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()
Esempio n. 4
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()
Esempio n. 5
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)
Esempio n. 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)
Esempio n. 7
0
def test_fit_with_box_cox(data, lmbda):
    periods = (5, 6, 7)
    mod = MSTL(endog=data, periods=periods, lmbda=lmbda)
    mod.fit()
Esempio n. 8
0
def test_raise_value_error_when_periods_and_windows_diff_lengths(
        periods, windows):
    with pytest.raises(ValueError,
                       match="Periods and windows must have same length"):
        MSTL(endog=[1, 2, 3, 4, 5], periods=periods, windows=windows)
Esempio n. 9
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
Esempio n. 10
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)
Esempio n. 11
0
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

from statsmodels.tsa.seasonal import MSTL

pd.plotting.register_matplotlib_converters()
plt.rc("figure", figsize=(8, 10))
plt.rc("font", size=10)
np.random.seed(0)

t = np.arange(1, 1000)
trend = 0.0001 * t**2 + 100
daily_seasonality = 5 * np.sin(2 * np.pi * t / 24)
weekly_seasonality = 10 * np.sin(2 * np.pi * t / (24 * 7))
noise = np.random.randn(len(t))
y = trend + daily_seasonality + weekly_seasonality + noise
index = pd.date_range(start='2000-01-01', periods=len(t), freq='H')
data = pd.DataFrame(data=y, index=index)

res = MSTL(data, periods=(24, 24 * 7)).fit()
res.plot()
plt.tight_layout()
plt.show()
Esempio n. 12
0
ax2 = fig.add_subplot(222)
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