def test_model_histories_do_not_matter(self): n1 = 5 n2 = 8 usage_seq = np.hstack((np.ones(n2, dtype=int), np.zeros(n1, dtype=int))) seqs = [] for i in range(2): if i == 0: ic1 = ([0.3], [-0.2, 0.2]) ic2 = ([0.2, 0.1], [-0.1, 0.2, 0.0]) else: ic1 = None ic2 = None arma1 = Arma( [0.9], [0.1, -0.2], default_source=sources.GaussianNoise(), initial_conditions=ic1, ) arma2 = Arma( [0.1, -0.2], [0.3, 0.4, 0.5], default_source=sources.GaussianNoise(), initial_conditions=ic2, ) seq = sample_switching_models([arma1, arma2], usage_seq) seqs.append(seq) np.testing.assert_allclose(seqs[0], seqs[1])
def test_second_model_history_is_not_default(self): n1 = 13 n2 = 23 # these will get overridden seq = [0] seq1_exp = [1] seq2_exp = [2] for i in range(2): arma1 = Arma([0.9], [0.1, -0.2], default_source=sources.GaussianNoise()) arma2 = Arma([0.1, -0.2], [0.3], default_source=sources.GaussianNoise()) if i == 0: seq1_exp = arma1.transform(n1) seq2_exp = arma2.transform(n2) else: seq = sample_switching_models( [arma1, arma2], np.hstack((np.zeros(n1, dtype=int), np.ones(n2, dtype=int))), ) np.testing.assert_allclose(seq[:n1], seq1_exp) self.assertGreater(np.max(np.abs(seq[n1:] - seq2_exp)), 0)
def test_default_initial_conditions_are_zero(self): n1 = 5 n2 = 8 usage_seq = np.hstack((np.ones(n2, dtype=int), np.zeros(n1, dtype=int))) seqs = [] for i in range(2): if i == 0: ic = ([0.0], [0.0, 0.0]) else: ic = None arma1 = Arma( [0.9], [0.1, -0.2], default_source=sources.GaussianNoise(), ) arma2 = Arma( [0.1, -0.2], [0.3, 0.4, 0.5], default_source=sources.GaussianNoise(), ) seq = sample_switching_models([arma1, arma2], usage_seq, initial_conditions=ic) seqs.append(seq) np.testing.assert_allclose(seqs[0], seqs[1])
def test_transform_first_return_value_matches_sample_switching_models( self): arma_hsmm = ArmaHSMM(self.armas) n = 15 y, x, usage_seq = arma_hsmm.transform(n, return_input=True, return_usage_seq=True) y_exp = sample_switching_models(self.armas, usage_seq, X=x) np.testing.assert_allclose(y, y_exp)
def test_single_model(self): n = 20 a = [0.9] b = [0.1, -0.2] arma1 = Arma(a, b, default_source=sources.GaussianNoise()) seq_exp = arma1.transform(n) arma2 = Arma(a, b, default_source=sources.GaussianNoise()) seq = sample_switching_models([arma2], np.zeros(n, dtype=int)) np.testing.assert_allclose(seq, seq_exp)
def test_single_model(self): n = 20 rng = np.random.default_rng(1) X = rng.normal(size=n) a = [0.9] b = [0.1, -0.2] arma = Arma(a, b) _, X_ret = sample_switching_models([arma], np.zeros(n, dtype=int), X=X, return_input=True) np.testing.assert_allclose(X, X_ret)
def test_initial_conditions_obeyed(self): arma_hsmm = ArmaHSMM(self.armas) n = 15 ic = ([-0.3, 0.2, 0.8], [0.5, 0.7, 0.1]) y, x, usage_seq = arma_hsmm.transform(n, initial_conditions=ic, return_input=True, return_usage_seq=True) y_exp = sample_switching_models(self.armas, usage_seq, X=x, initial_conditions=ic) np.testing.assert_allclose(y, y_exp)
def test_multiple_models(self): n1 = 13 n2 = 23 rng = np.random.default_rng(2) X = rng.normal(size=n1 + n2) arma1 = Arma([0.9], [0.1, -0.2]) arma2 = Arma([0.1, -0.2], [0.3]) _, X_ret = sample_switching_models( [arma1, arma2], np.hstack((np.zeros(n1, dtype=int), np.ones(n2, dtype=int))), X=X, return_input=True, ) np.testing.assert_allclose(X, X_ret)
def test_multiple_models_callable_source(self): n1 = 13 n2 = 23 seed = 12 src = sources.GaussianNoise(seed) X_exp = src(size=n1 + n2) arma1 = Arma([0.9], [0.1, -0.2]) arma2 = Arma([0.1, -0.2], [0.3]) _, X_ret = sample_switching_models( [arma1, arma2], np.hstack((np.zeros(n1, dtype=int), np.ones(n2, dtype=int))), X=sources.GaussianNoise(seed), return_input=True, ) np.testing.assert_allclose(X_exp, X_ret)
def test_initial_conditions_parameter_is_obeyed(self): a = [0.8] b = [0.1, 0.2] n = 32 ic = ([-0.5], [-0.5, 0.3]) arma = Arma(a, b, default_source=sources.GaussianNoise(), initial_conditions=ic) seq_exp = arma.transform(n) arma = Arma(a, b, default_source=sources.GaussianNoise()) seq = sample_switching_models([arma], np.zeros(n, dtype=int), initial_conditions=ic) np.testing.assert_allclose(seq, seq_exp)
def test_correct_history_used_at_switch_point(self): n1 = 13 n2 = 5 arma1 = Arma([0.9], [0.1, -0.2], default_source=sources.GaussianNoise()) a2 = [0.1, -0.2] b2 = [0.3] arma2 = Arma(a2, b2, default_source=sources.GaussianNoise()) seq, X = sample_switching_models( [arma1, arma2], np.hstack((np.zeros(n1, dtype=int), np.ones(n2, dtype=int))), return_input=True, ) exp_ar = np.dot(np.flip(a2), seq[n1 - len(a2):n1]) exp_ma = np.dot(np.flip(b2), X[n1 - len(b2):n1]) + X[n1] self.assertAlmostEqual(exp_ar + exp_ma, seq[n1])
arma2=Arma([0.6], [], default_source=sources.GaussianNoise(scale=0.802, rng=rng)), ) two_ar_short.armas = [two_ar_short.arma1, two_ar_short.arma2] crt_samples1 = two_ar_short.arma1.transform(100000) crt_samples2 = two_ar_short.arma2.transform(100000) print("should be almost zero:", np.std(crt_samples1) - 1, np.std(crt_samples2) - 1) two_ar_short.n_samples = 500 two_ar_short.usage_seq = np.zeros(two_ar_short.n_samples, dtype=int) two_ar_short.usage_seq[150:390] = 1 two_ar_short.sig = sample_switching_models(two_ar_short.armas, two_ar_short.usage_seq) two_ar_short.transitions = np.cumsum( [_[1] for _ in rle_encode(two_ar_short.usage_seq)]) # %% with FigureManager(figsize=(8, 2), sharex=True, despine_kws={"left": True}) as ( fig1, ax1, ): ax1.plot(two_ar_short.sig) ax1.set_xlabel("sample") ax1.set_xlim(0, two_ar_short.n_samples) ax1.set_yticks([])
def test_raises_if_length_x_shorter_than_usage_seq(self): with self.assertRaises(ValueError): sample_switching_models([object()], [0, 0, 0], X=[0.0, 0.3])
def test_raises_if_invalid_model_indices(self): with self.assertRaises(ValueError): sample_switching_models([object()], [0, 1, 1])
def test_raises_if_no_models(self): with self.assertRaises(ValueError): sample_switching_models([], [])