def test_calendar_time_trend_base(time_index): ct = CalendarTimeTrend("M", True, order=3, base_period="1960-1-1") ct2 = CalendarTimeTrend("M", True, order=3) assert ct != ct2 str(ct) str(ct2) assert ct.base_period is not None assert ct2.base_period is None
def test_check_index_type(): ct = CalendarTimeTrend("A", True, order=3) idx = pd.RangeIndex(0, 20) with pytest.raises(TypeError, match="CalendarTimeTrend terms can only"): ct._check_index_type(idx, pd.DatetimeIndex) with pytest.raises(TypeError, match="CalendarTimeTrend terms can only"): ct._check_index_type(idx, (pd.DatetimeIndex, )) with pytest.raises(TypeError, match="CalendarTimeTrend terms can only"): ct._check_index_type(idx, (pd.DatetimeIndex, pd.PeriodIndex)) idx = pd.Index([0, 1, 1, 2, 3, 5, 8, 13]) with pytest.raises(TypeError, match="CalendarTimeTrend terms can only"): types = (pd.DatetimeIndex, pd.PeriodIndex, pd.RangeIndex) ct._check_index_type(idx, types)
def test_calendar_time_trend_smoke(time_index, forecast_index): ct = CalendarTimeTrend("A", order=2) ct.in_sample(time_index) steps = 83 if forecast_index is None else len(forecast_index) ct.out_of_sample(steps, time_index, forecast_index) str(ct) hash(ct) assert isinstance(ct.order, int) assert isinstance(ct.constant, bool) assert isinstance(ct.freq, str) assert ct.base_period is None
def test_calendar_time_trend(reset_randomstate): inc = np.abs(np.random.standard_normal(1000)) inc = np.cumsum(inc) inc = 10 * inc / inc[-1] offset = (24 * 3600 * inc).astype(np.int64) base = pd.Timestamp("2000-1-1") index = [base + pd.Timedelta(val, "s") for val in offset] index = pd.Index(index) ctt = CalendarTimeTrend("D", True, order=3, base_period=base) assert ctt.order == 3 terms = ctt.in_sample(index) cols = ["const", "trend", "trend_squared", "trend_cubed"] assert list(terms.columns) == cols inc = 1 + offset / (24 * 3600) expected = [] for i in range(4): expected.append(inc**i) expected = np.column_stack(expected) np.testing.assert_allclose(expected, terms.values) ctt = CalendarTimeTrend("D", True, order=2, base_period=base) ctt2 = CalendarTimeTrend.from_string("D", trend="ctt", base_period=base) pd.testing.assert_frame_equal(ctt.in_sample(index), ctt2.in_sample(index)) ct = CalendarTimeTrend("D", True, order=1, base_period=base) ct2 = CalendarTimeTrend.from_string("D", trend="ct", base_period=base) pd.testing.assert_frame_equal(ct.in_sample(index), ct2.in_sample(index)) ctttt = CalendarTimeTrend("D", True, order=4, base_period=base) assert ctttt.order == 4 terms = ctttt.in_sample(index) cols = ["const", "trend", "trend_squared", "trend_cubed", "trend**4"] assert list(terms.columns) == cols
def test_unknown_freq(): with pytest.raises(ValueError, match="freq is not understood by pandas"): CalendarTimeTrend("unknown", True, order=3)
def test_forbidden_index(): index = pd.RangeIndex(0, 10) ct = CalendarTimeTrend("A", order=2) with pytest.raises(TypeError, match="CalendarTimeTrend terms can only"): ct.in_sample(index)