def test_fit_alpha_only(self): alpha = 0.7 np.random.seed(345) T = 200 l = 0.2 y = [0] * T for t in range(0, T): d = np.random.normal() y[t] = l + d l = l + alpha * d c = Components(use_arma_errors=False) p = ModelParams(c, alpha=0.09) # default starting value for alpha optimizer = ParamsOptimizer(self.context) optimizer.optimize(y, p) fitted_model = optimizer.optimal_model() resid = fitted_model.resid # AGAIN why x0 estimation is so shitty, l is far from being 0.2 # Residuals should form a normal distribution _, pvalue = stats.normaltest(resid) assert 0.05 < pvalue # large p-value, we can not reject null hypothesis of normal distribution # Mean of residuals should be close to 0 _, pvalue = stats.ttest_1samp(resid, popmean=0.0) assert 0.05 < pvalue # large p-value we can not reject null hypothesis that mean is 0 # We expect 95% of residuals to lie within [-2,2] interval assert len(resid[np.where(np.abs(resid) < 2)]) / len(resid) > 0.90
def test_calculate_seed_x0(self, components, params, expected): y = [2.0, 4.0, 2.0] c = Components(**components) p = ModelParams(c, **params) m = ParamsOptimizer(self.context) x0 = m._calculate_seed_x0(y, p) assert np.allclose(expected, x0)
def test_with_vector_values(self): c = Components(use_trend=True, use_damped_trend=True, use_arma_errors=True, use_box_cox=True, seasonal_periods=[7, 30], p=1, q=2) p = ModelParams(c, alpha=0.1, box_cox_lambda=1.4, beta=0.2, phi=0.3, gamma_params=[0.4, 0.5], ar_coefs=[-0.3], ma_coefs=[-0.4, 0.6]) v = p.to_vector() assert np.array_equal([0.1, 1.4, 0.2, 0.3, 0.4, 0.5, -0.3, -0.4, 0.6], v) p = ModelParams(c, alpha=0.9, box_cox_lambda=1.4) p = p.with_vector_values(v) assert p.alpha == 0.1 assert p.box_cox_lambda == 1.4 assert p.beta == 0.2 assert p.phi == 0.3 assert np.array_equal([0.4, 0.5], p.gamma_params) assert np.array_equal([-0.3], p.ar_coefs) assert np.array_equal([-0.4, 0.6], p.ma_coefs)
def test_create_x0_of_zeroes(self, components, params, expected): c = Components(**components) p = ModelParams(c, **params) x0 = p._create_x0_of_zeroes() assert np.array_equal(expected, x0)
def test_to_vector(self, components, params, expected): c = Components(**components) p = ModelParams(c, **params) vector = p.to_vector() assert np.array_equal(expected, vector)
def test_gamma(self, components, params, expected_gamma): c = Components(**components) p = ModelParams(c, **params) assert np.array_equal(expected_gamma, p.gamma_params)
def test_make_vector(self, components, params, expected_w, expected_g): m = MatrixBuilder(ModelParams(Components(**components), **params)) assert np.array_equal(expected_w, m.make_w_vector()) assert np.array_equal(expected_g, m.make_g_vector())
def test_make_F_matrix(self, components, params, expected_matrix): m = MatrixBuilder(ModelParams(Components(**components), **params)) assert np.array_equal(expected_matrix, m.make_F_matrix())