Esempio n. 1
0
def make_and_compare_predictions(
    gathered_stats: TimeSeries,
    predictions_to_make: ModelsToMake,
    prediction_duration_past: pd.Timedelta = _ONE_DAY,
    prediction_duration_future: pd.Timedelta = None,
    metric: Metric = metrics.coefficient_of_variation,
    transform: bool = False,
) -> PredictionEvaluations:
    """Run multiple forecasts and compare their accuracy."""
    train, actual = gathered_stats.split_after(
        gathered_stats.end_time() - prediction_duration_past, )
    n_pred: int = len(actual)
    if prediction_duration_future:
        n_pred += int(prediction_duration_future / gathered_stats.freq())
    if transform:
        forecasts = make_forecasts_ensure_positive(
            train=train,
            n_pred=n_pred,
            predictions_to_make=predictions_to_make,
        )
    else:
        forecasts = make_forecasts(
            train=train,
            n_pred=n_pred,
            predictions_to_make=predictions_to_make,
        )
    return PredictionEvaluations(
        predictions=forecasts,
        evaluations=compare_predictions(actual, forecasts, metric),
    )
Esempio n. 2
0
    def helper_test_split(test_case, test_series: TimeSeries):
        seriesA, seriesB = test_series.split_after(pd.Timestamp('20130104'))
        test_case.assertEqual(seriesA.end_time(), pd.Timestamp('20130104'))
        test_case.assertEqual(seriesB.start_time(), pd.Timestamp('20130105'))

        seriesC, seriesD = test_series.split_before(pd.Timestamp('20130104'))
        test_case.assertEqual(seriesC.end_time(), pd.Timestamp('20130103'))
        test_case.assertEqual(seriesD.start_time(), pd.Timestamp('20130104'))

        test_case.assertEqual(test_series.freq_str(), seriesA.freq_str())
        test_case.assertEqual(test_series.freq_str(), seriesC.freq_str())
Esempio n. 3
0
    def helper_test_append(test_case, test_series: TimeSeries):
        # reconstruct series
        seriesA, seriesB = test_series.split_after(pd.Timestamp('20130106'))
        test_case.assertEqual(seriesA.append(seriesB), test_series)
        test_case.assertEqual(
            seriesA.append(seriesB).freq(), test_series.freq())

        # Creating a gap is not allowed
        seriesC = test_series.drop_before(pd.Timestamp('20130107'))
        with test_case.assertRaises(ValueError):
            seriesA.append(seriesC)

        # Changing frequence is not allowed
        seriesM = TimeSeries.from_times_and_values(
            pd.date_range('20130107', '20130507', freq='30D'), range(5))
        with test_case.assertRaises(ValueError):
            seriesA.append(seriesM)
Esempio n. 4
0
    def helper_test_append_values(test_case, test_series: TimeSeries):
        # reconstruct series
        seriesA, seriesB = test_series.split_after(pd.Timestamp('20130106'))
        test_case.assertEqual(
            seriesA.append_values(seriesB.values(), seriesB.time_index()),
            test_series)
        test_case.assertEqual(seriesA.append_values(seriesB.values()),
                              test_series)

        # test for equality
        test_case.assertEqual(
            test_series.drop_after(pd.Timestamp('20130105')).append_values(
                test_series.drop_before(pd.Timestamp('20130104')).values()),
            test_series)
        test_case.assertEqual(seriesA.append_values([]), seriesA)

        # randomize order
        rd_order = np.random.permutation(range(len(seriesB.values())))
        test_case.assertEqual(
            seriesA.append_values(seriesB.values()[rd_order],
                                  seriesB.time_index()[rd_order]), test_series)

        # add non consecutive index
        with test_case.assertRaises(ValueError):
            test_case.assertEqual(
                seriesA.append_values(seriesB.values(),
                                      seriesB.time_index() + seriesB.freq()),
                test_series)

        # add existing indices
        with test_case.assertRaises(ValueError):
            test_case.assertEqual(
                seriesA.append_values(
                    seriesB.values(),
                    seriesB.time_index() - 3 * seriesB.freq()), test_series)

        # other frequency
        with test_case.assertRaises(ValueError):
            test_case.assertEqual(
                seriesA.append_values(
                    seriesB.values(),
                    pd.date_range('20130107', '20130113', freq='2d')),
                test_series)