def test_multivariate_penalty(): alphas = [1, 2] weights = [1, 1] np.random.seed(1) x, y, pol = multivariate_sample_data() univ_pol1 = UnivariatePolynomialSmoother(x[:, 0], degree=pol.degrees[0]) univ_pol2 = UnivariatePolynomialSmoother(x[:, 1], degree=pol.degrees[1]) gp1 = UnivariateGamPenalty(alpha=alphas[0], univariate_smoother=univ_pol1) gp2 = UnivariateGamPenalty(alpha=alphas[1], univariate_smoother=univ_pol2) mgp = MultivariateGamPenalty(multivariate_smoother=pol, alpha=alphas, weights=weights) for i in range(10): params1 = np.random.randint(-3, 3, pol.smoothers[0].dim_basis) params2 = np.random.randint(-3, 3, pol.smoothers[1].dim_basis) params = np.concatenate([params1, params2]) c1 = gp1.func(params1) c2 = gp2.func(params2) c = mgp.func(params) assert_allclose(c, c1 + c2, atol=1.e-10, rtol=1.e-10) d1 = gp1.deriv(params1) d2 = gp2.deriv(params2) d12 = np.concatenate([d1, d2]) d = mgp.deriv(params) assert_allclose(d, d12) h1 = gp1.deriv2(params1) h2 = gp2.deriv2(params2) h12 = block_diag(h1, h2) h = mgp.deriv2(params) assert_allclose(h, h12)
def test_gam_gradient(): # test the gam gradient for the example polynomial np.random.seed(1) pol, y = polynomial_sample_data() alpha = 1 smoother = pol.smoothers[0] gp = UnivariateGamPenalty(alpha=alpha, univariate_smoother=smoother) for _ in range(10): params = np.random.uniform(-2, 2, 4) params = np.array([1, 1, 1, 1]) gam_grad = gp.deriv(params) grd = grad(params) assert_allclose(gam_grad, grd, rtol=1.e-2, atol=1.e-2)