def seasonal_features(cls, freq: str) -> List[TimeFeature]: offset = to_offset(freq) if offset.name == "M": return [MonthOfYearIndex()] elif offset.name == "W-SUN": return [WeekOfYearIndex()] elif offset.name == "D": return [DayOfWeekIndex()] elif offset.name == "B": # TODO: check this case return [DayOfWeekIndex()] elif offset.name == "H": return [HourOfDayIndex(), DayOfWeekIndex()] elif offset.name == "T": return [ MinuteOfHourIndex(), HourOfDayIndex(), ] else: RuntimeError(f"Unsupported frequency {offset.name}") return []
def DayOfWeekSeasonalISSM(): return SeasonalityISSM(num_seasons=7, time_feature=DayOfWeekIndex())
@pytest.mark.parametrize( "feature, index, cardinality", [ ( MinuteOfHourIndex(), pd.date_range( "01-01-2015 00:00:00", periods=60 * 2 * 24, freq="1min"), 60, ), ( HourOfDayIndex(), pd.date_range("01-01-2015 00:00:00", periods=14 * 24, freq="1h"), 24, ), ( DayOfWeekIndex(), pd.date_range("01-01-2015", periods=365 * 5, freq="D"), 7, ), ( DayOfMonthIndex(), pd.date_range("01-01-2015", periods=365 * 5, freq="D"), 31, ), ( DayOfYearIndex(), pd.date_range("01-01-2015", periods=365 * 5, freq="D"), 366, ), ( WeekOfYearIndex(),
def test_composite_issm_h(): issm = CompositeISSM( seasonal_issms=[ SeasonalityISSM(num_seasons=7, time_feature=DayOfWeekIndex()), SeasonalityISSM(num_seasons=12, time_feature=MonthOfYearIndex()), ], add_trend=False, ) assert issm.latent_dim() == 1 + 7 + 12 assert issm.output_dim() == 1 time_features = issm.time_features() assert len(time_features) == 3 time_indices = [ pd.date_range("2020-01-01 21:00:00", periods=10, freq="H"), pd.date_range("2020-01-31 22:00:00", periods=10, freq="H"), ] features = mx.nd.array( np.stack( [ np.stack([f(time_index) for f in time_features], axis=-1) for time_index in time_indices ], axis=0, )) emission_coeff, transition_coeff, innovation_coeff = issm.get_issm_coeff( features) season_indices_dow = [[2] * 3 + [3] * 7, [4] * 2 + [5] * 8] season_indices_moy = [[0] * 10, [0] * 2 + [1] * 8] for item in range(2): for time in range(10): expected_coeff = mx.nd.concat( mx.nd.ones((1, 1)), mx.nd.one_hot(mx.nd.array([season_indices_dow[item][time]]), 7), mx.nd.one_hot(mx.nd.array([season_indices_moy[item][time]]), 12), dim=-1, ) sliced_emission_coeff = mx.nd.slice( emission_coeff, begin=(item, time, None, None), end=(item + 1, time + 1, None, None), ) assert (sliced_emission_coeff == expected_coeff).asnumpy().all() sliced_transition_coeff = mx.nd.slice( transition_coeff, begin=(item, time, None, None), end=(item + 1, time + 1, None, None), ) assert ((sliced_transition_coeff == mx.nd.eye(1 + 7 + 12)).asnumpy().all()) sliced_innovation_coeff = mx.nd.slice( innovation_coeff, begin=(item, time, None), end=(item + 1, time + 1, None), ) assert (sliced_innovation_coeff == expected_coeff).asnumpy().all()