def test_ridge_regression(): rs = np.random.RandomState(0) alphas = np.array([0.0, 1.0, 10.0]) n_obs, n_covariate, n_trait = 25, 5, 3 X = rs.normal(size=(n_obs, n_covariate)) Y = X @ rs.normal(size=(n_covariate, n_trait)) + rs.normal(size=(n_obs, 1)) XtX, XtY = X.T @ X, X.T @ Y # Check that results are equal for multiple alphas res1 = ridge_regression(XtX, XtY, alphas) res2 = np.concatenate( [ridge_regression(XtX, XtY, alphas=alphas[[i]]) for i in range(len(alphas))], axis=0, ) np.testing.assert_equal(res1, res2)
def test_ridge_regression__raise_on_non_equal_first_dim(): with pytest.raises( ValueError, match="Array arguments must have same size in first dimension" ): ridge_regression(np.ones((2, 2)), np.ones((1, 1)), np.array([1.0]))
def test_ridge_regression__raise_on_non_symmetric(): with pytest.raises(ValueError, match="First argument must be symmetric"): ridge_regression(np.ones((2, 1)), np.ones((2, 1)), np.array([1.0]))