Example #1
0
def test_ets_full_seasonal_fit(synthetic_data, estimator_type):
    train_df, test_df, coef = synthetic_data
    ets = ETSFull(response_col='response',
                  date_col='week',
                  prediction_percentiles=[5, 95],
                  seasonality=52,
                  num_warmup=50,
                  num_sample=50,
                  verbose=False,
                  estimator_type=estimator_type)
    ets.fit(train_df)

    init_call = ets.get_init_values()
    assert isinstance(init_call, ETSInitializer)
    assert init_call.s == 52
    init_values = init_call()
    assert init_values['init_sea'].shape == (51, )

    predict_df = ets.predict(test_df)

    expected_columns = ['week', 'prediction_5', 'prediction', 'prediction_95']
    expected_shape = (51, len(expected_columns))
    expected_num_parameters = 5

    assert predict_df.shape == expected_shape
    assert predict_df.columns.tolist() == expected_columns
    assert len(ets._posterior_samples) == expected_num_parameters

    assert ets.is_fitted()
Example #2
0
def test_ets_full_reproducibility(synthetic_data, estimator_type, seasonality):
    train_df, test_df, coef = synthetic_data

    ets1 = ETSFull(response_col='response',
                   date_col='week',
                   prediction_percentiles=[5, 95],
                   seasonality=seasonality,
                   num_warmup=50,
                   num_sample=50,
                   verbose=False,
                   estimator_type=estimator_type)

    # first fit and predict
    ets1.fit(train_df)
    posteriors1 = copy(ets1._posterior_samples)
    prediction1 = ets1.predict(test_df)

    # second fit and predict
    # note a new instance must be created to reset the seed
    # note both fit and predict contain random generation processes
    ets2 = ETSFull(response_col='response',
                   date_col='week',
                   prediction_percentiles=[5, 95],
                   seasonality=seasonality,
                   num_warmup=50,
                   num_sample=50,
                   verbose=False,
                   estimator_type=estimator_type)

    ets2.fit(train_df)
    posteriors2 = copy(ets2._posterior_samples)
    prediction2 = ets2.predict(test_df)

    # assert same posterior keys
    assert set(posteriors1.keys()) == set(posteriors2.keys())

    # assert posterior draws are reproducible
    for k, v in posteriors1.items():
        assert np.allclose(posteriors1[k], posteriors2[k])

    # assert prediction is reproducible
    assert prediction1.equals(prediction2)
Example #3
0
def test_ets_non_seasonal_fit(synthetic_data, estimator_type):
    train_df, test_df, coef = synthetic_data

    ets = ETSFull(
        response_col='response',
        date_col='week',
        estimator_type=estimator_type,
        num_warmup=50,
    )

    ets.fit(train_df)
    predict_df = ets.predict(test_df)

    expected_columns = ['week', 'prediction_5', 'prediction', 'prediction_95']
    expected_shape = (51, len(expected_columns))
    expected_num_parameters = 4

    assert predict_df.shape == expected_shape
    assert predict_df.columns.tolist() == expected_columns
    assert len(ets._posterior_samples) == expected_num_parameters
Example #4
0
def test_full_prediction_percentiles(iclaims_training_data,
                                     prediction_percentiles):
    df = iclaims_training_data

    ets = ETSFull(
        response_col='claims',
        date_col='week',
        seasonality=52,
        seed=8888,
        prediction_percentiles=prediction_percentiles,
    )

    if not prediction_percentiles:
        p_labels = ['_5', '', '_95']
    else:
        p_labels = ['_5', '_10', '', '_95']

    ets.fit(df)
    predicted_df = ets.predict(df)
    expected_columns = ['week'] + ["prediction" + p for p in p_labels]
    assert predicted_df.columns.tolist() == expected_columns
    assert predicted_df.shape[0] == df.shape[0]

    predicted_df = ets.predict(df, decompose=True)
    plot_components = [
        'prediction', PredictedComponents.TREND.value,
        PredictedComponents.SEASONALITY.value
    ]

    expected_columns = ['week']
    for pc in plot_components:
        for p in p_labels:
            expected_columns.append(pc + p)
    assert predicted_df.columns.tolist() == expected_columns
    assert predicted_df.shape[0] == df.shape[0]