Exemplo n.º 1
0
def test_fit_regressor_visnights(load_df_and_hier_visnights):
    hierarchical_vis_data, vis_hier = load_df_and_hier_visnights
    ht = HTSRegressor(model="holt_winters", revision_method="OLS")
    ht.fit(df=hierarchical_vis_data, nodes=vis_hier)
    assert isinstance(ht.hts_result.errors, dict)
    assert isinstance(ht.hts_result.models, dict)
    assert "total" in ht.hts_result.models
Exemplo n.º 2
0
def test_fit_regressor_uv(load_df_and_hier_uv):
    hierarchical_sine_data, sine_hier = load_df_and_hier_uv
    ht = HTSRegressor(model='holt_winters', revision_method='OLS')
    ht.fit(df=hierarchical_sine_data, nodes=sine_hier)
    assert isinstance(ht.hts_result.errors, dict)
    assert isinstance(ht.hts_result.models, dict)
    assert 'total' in ht.hts_result.models
Exemplo n.º 3
0
def test_exog_fit_predict_fb_model(hierarchical_mv_data, mv_tree_empty):
    exogenous = {
        k: ["precipitation", "temp"]
        for k in hierarchical_mv_data.columns
        if k not in ["precipitation", "temp"]
    }
    horizon = 7
    train = hierarchical_mv_data[:500]
    test = hierarchical_mv_data[500:500 + horizon]
    clf = HTSRegressor(model="prophet", revision_method="OLS", n_jobs=0)
    model = clf.fit(train, mv_tree_empty, exogenous=exogenous)
    preds = model.predict(exogenous_df=test)
    assert isinstance(preds, pandas.DataFrame)

    # base + steps ahead
    assert len(preds) == len(train) + horizon
    assert len(model.hts_result.forecasts["total"]) == len(train) + horizon

    assert len(model.hts_result.errors) == len(train.columns) - 2

    for column in train.columns:
        if column not in ("precipitation", "temp"):
            assert column in model.hts_result.errors
            assert column in model.hts_result.forecasts
            assert column in model.hts_result.residuals
Exemplo n.º 4
0
def test_convenience(load_df_and_hier_uv):
    hd, hier = load_df_and_hier_uv
    hd = hd.head(200)
    ht = HTSRegressor(model='holt_winters', revision_method='OLS')
    ht = ht.fit(df=hd, nodes=hier)
    preds = ht.predict(steps_ahead=10)

    rev = revise_forecasts(
        'OLS',
        nodes=ht.nodes,
        forecasts=ht.hts_result.forecasts,
        errors=ht.hts_result.errors,
        residuals=ht.hts_result.residuals,
    )

    assert isinstance(rev, pandas.DataFrame)
    assert numpy.allclose(preds.values, rev.values)

    rev_s = revise_forecasts(
        'OLS',
        summing_matrix=ht.sum_mat,
        forecasts=ht.hts_result.forecasts,
        errors=ht.hts_result.errors,
        residuals=ht.hts_result.residuals,
    )
    assert isinstance(rev_s, pandas.DataFrame)
    assert numpy.allclose(rev.values, rev_s.values)

    rev = revise_forecasts('AHP',
                           nodes=ht.nodes,
                           forecasts=ht.hts_result.forecasts)
    assert isinstance(rev, pandas.DataFrame)
Exemplo n.º 5
0
def test_instantiate_revision(load_df_and_hier_visnights):
    hierarchical_visnights_data, visnights_hier = load_df_and_hier_visnights

    for method in ['OLS', 'FP', 'WLSS', 'WLSV', 'PHA', 'AHP', 'NONE']:
        ht = HTSRegressor(model='holt_winters', revision_method=method)
        ht.fit(df=hierarchical_visnights_data, nodes=visnights_hier)

        rm = RevisionMethod(method,
                            sum_mat=ht.sum_mat,
                            transformer=ht.transform)

        _ = ht.predict(steps_ahead=3)
        revised = rm.revise(forecasts=ht.hts_result.forecasts,
                            mse=ht.hts_result.errors,
                            nodes=ht.nodes)
        assert isinstance(revised, numpy.ndarray)
        assert revised.shape == (11, len(ht.hts_result.forecasts))
Exemplo n.º 6
0
def test_multiprocessing_fit(load_df_and_hier_uv):
    distributor = MultiprocessingDistributor(n_workers=2)
    hsd, hier = load_df_and_hier_uv
    reg = HTSRegressor()
    reg = reg.fit(df=hsd, nodes=hier, distributor=distributor)

    node_keys = list(chain.from_iterable([v for v in hier.values()]))

    for node in node_keys:
        assert node in reg.hts_result.models.keys()
Exemplo n.º 7
0
def test_predict_regressor_visnights(load_df_and_hier_visnights):
    hierarchical_vis_data, vis_hier = load_df_and_hier_visnights
    hvd = hierarchical_vis_data
    for model in ["holt_winters", "auto_arima"]:
        ht = HTSRegressor(model=model, revision_method="OLS")

        ht.fit(df=hvd, nodes=vis_hier)
        preds = ht.predict(steps_ahead=4)

        assert isinstance(preds, pandas.DataFrame)

        # base + steps ahead
        assert len(preds) == len(hvd) + 4
        assert len(ht.hts_result.forecasts["total"]) == len(hvd) + 4
        assert "total" in ht.hts_result.errors
        assert "total" in ht.hts_result.forecasts
        assert "total" in ht.hts_result.residuals

        assert "NSW" in ht.hts_result.errors
        assert "NSW" in ht.hts_result.forecasts
        assert "NSW" in ht.hts_result.residuals
Exemplo n.º 8
0
def test_dask_cluster_two_workers(load_df_and_hier_uv):
    with Client(n_workers=1, processes=False) as client:
        address = client.scheduler_info()['address']
        distributor = ClusterDaskDistributor(address=address)

        hsd, hier = load_df_and_hier_uv
        reg = HTSRegressor()
        reg = reg.fit(df=hsd, nodes=hier, distributor=distributor)

        node_keys = list(chain.from_iterable([v for v in hier.values()]))

        for node in node_keys:
            assert node in reg.hts_result.models.keys()
Exemplo n.º 9
0
def test_predict_regressor_uv(load_df_and_hier_uv):
    hierarchical_sine_data, sine_hier = load_df_and_hier_uv
    hsd = hierarchical_sine_data.head(200)

    for model in ["holt_winters", "auto_arima", "sarimax"]:
        ht = HTSRegressor(model=model, revision_method="OLS")

        ht.fit(df=hsd, nodes=sine_hier)
        preds = ht.predict(steps_ahead=10)

        assert isinstance(preds, pandas.DataFrame)

        # base + steps ahead
        assert len(preds) == len(hsd) + 10
        assert len(ht.hts_result.forecasts["total"]) == len(hsd) + 10
        assert "total" in ht.hts_result.errors
        assert "total" in ht.hts_result.forecasts
        assert "total" in ht.hts_result.residuals

        assert "a" in ht.hts_result.errors
        assert "a" in ht.hts_result.forecasts
        assert "a" in ht.hts_result.residuals