def test_poisson_no_intercept(): """Tests the Poisson fitter with no intercept.""" n_features = 3 n_samples = 10000 # create data X, y, beta, _ = make_poisson_regression(n_samples=n_samples, n_features=n_features, n_informative=n_features, beta=np.array([0.5, 1.0, 1.5]), random_state=2332) # lbfgs poisson = Poisson(alpha=0., l1_ratio=0., fit_intercept=False, solver='lbfgs', max_iter=5000) poisson.fit(X, y) assert_allclose(poisson.coef_, beta, rtol=0.5) # coordinate descent poisson = Poisson(alpha=0., l1_ratio=0., fit_intercept=False, solver='cd', max_iter=5000) poisson.fit(X, y) assert_allclose(poisson.coef_, beta, rtol=0.5) # broken solver poisson = Poisson(alpha=0., l1_ratio=0., fit_intercept=False, solver='ruff', max_iter=5000) assert_raises(ValueError, poisson.fit, X, y)
def test_fit(): """Test the entire fitting procedure for the Poisson GLM.""" # compare after 50 iterations poisson = Poisson(max_iter=50, fit_intercept=False) poisson.fit(X, y, init=0.5 * np.ones(beta.size)) beta_new_true = np.array([ -3.865910169523823, 6.243915946623266, -0.728804736275411, -0.463706073765083, -3.622620769371424 ]) assert np.allclose(beta_new_true, poisson.coef_)
def test_score_predictions(): """Test the score predictions function in UoI Poisson.""" X = np.array([[np.log(2), -1, -3], [np.log(3), -2, -4], [np.log(4), -3, -5], [np.log(5), -4, -6]]) y = 1. / np.log([2., 3., 4., 5.]) support = np.array([True, False, False]) n_samples = y.size # create fitter by hand fitter = Poisson() fitter.coef_ = np.array([1]) fitter.intercept_ = 0 uoi_fitter = UoI_Poisson() # test log-likelihood ll = uoi_fitter._score_predictions( metric='log', fitter=fitter, X=X, y=y, support=support) assert_allclose(ll, -2.5) # test information criteria total_ll = ll * n_samples aic = uoi_fitter._score_predictions( metric='AIC', fitter=fitter, X=X, y=y, support=support) assert_allclose(aic, 2 * total_ll - 2) aicc = uoi_fitter._score_predictions( metric='AICc', fitter=fitter, X=X, y=y, support=support) assert_allclose(aicc, aic - 2) bic = uoi_fitter._score_predictions( metric='BIC', fitter=fitter, X=X, y=y, support=support) assert_allclose(bic, 2 * total_ll - np.log(y.size)) # test invalid metric assert_raises(ValueError, uoi_fitter._score_predictions, 'fake', fitter, X, y, support)
def test_cd_sweep(): """Test one pass through the coordinate sweep function.""" poisson = Poisson(fit_intercept=False) w, z = Poisson.adjusted_response(X, y, beta) active_idx = np.argwhere(beta != 0).ravel() beta_new, intercept_new = poisson.cd_sweep(beta, X, w, z, active_idx) beta_new_true = np.array( [2.922553187460584, 0.909849302128083, 0, -1.850644198436912, 0]) assert np.allclose(beta_new_true, beta_new) assert intercept_new == 0
def test_poisson_with_sparsity(): """Tests the Poisson fitter with no intercept.""" n_features = 3 n_samples = 10000 # create data X, y, beta, _ = make_poisson_regression(n_samples=n_samples, n_features=n_features, n_informative=n_features, beta=np.array([0., 1.0, 1.5]), random_state=2332) # lbfgs poisson = Poisson(alpha=0.1, l1_ratio=1., fit_intercept=False, solver='lbfgs', max_iter=5000) poisson.fit(X, y) assert_equal(np.abs(poisson.coef_[0]), 0) # coordinate descent poisson = Poisson(alpha=0.1, l1_ratio=1., fit_intercept=False, solver='cd', max_iter=5000) poisson.fit(X, y) assert_equal(np.abs(poisson.coef_[0]), 0.)
def test_poisson_with_intercept(): """Tests the Poisson fitter with no intercept.""" n_features = 3 n_samples = 10000 # create data X, y, beta, intercept = make_poisson_regression( n_samples=n_samples, n_features=n_features, n_informative=n_features, beta=np.array([0.5, 1.0, 1.5]), include_intercept=True, random_state=2332) # lbfgs poisson = Poisson(alpha=0., l1_ratio=0., fit_intercept=True, solver='lbfgs', max_iter=5000) poisson.fit(X, y) assert_allclose(poisson.coef_, beta, rtol=0.5) assert_allclose(poisson.intercept_, intercept, rtol=0.5) # coordinate descent poisson = Poisson(alpha=0., l1_ratio=0., fit_intercept=True, solver='cd', max_iter=5000) poisson.fit(X, y) assert_allclose(poisson.coef_, beta, rtol=0.5) assert_allclose(poisson.intercept_, intercept, rtol=0.5)
def test_poisson_reg_params(): """Test whether the upper bound on the regularization parameters correctly zero out the coefficients.""" n_features = 5 n_samples = 1000 X, y, beta, intercept = make_poisson_regression( n_samples=n_samples, n_features=n_features, n_informative=n_features, random_state=2332) alpha_sets = [np.array([0.5]), np.array([1.0])] for alpha_set in alpha_sets: uoi_poisson = UoI_Poisson(alphas=alpha_set) reg_params = uoi_poisson.get_reg_params(X, y) alpha = reg_params[0]['alpha'] l1_ratio = reg_params[0]['l1_ratio'] # check that coefficients get set to zero poisson = Poisson(alpha=1.01 * alpha, l1_ratio=l1_ratio, standardize=False, fit_intercept=True) poisson.fit(X, y) assert_equal(poisson.coef_, 0.) # check that coefficients below the bound are not set to zero poisson = Poisson(alpha=0.99 * alpha, l1_ratio=l1_ratio, standardize=False, fit_intercept=True) poisson.fit(X, y) assert np.count_nonzero(poisson.coef_) > 0
def test_poisson_standardize(): """Tests the Poisson fitter `standardize=True`.""" n_features = 3 n_samples = 200 # create data X, y, beta, intercept = make_poisson_regression( n_samples=n_samples, n_features=n_features, n_informative=n_features, beta=np.array([0.5, 1.0, 1.5]), include_intercept=True, random_state=2332) # lbfgs poisson = Poisson(alpha=0., l1_ratio=0., fit_intercept=True, solver='lbfgs', standardize=True) poisson.fit(X, y)
def test_predict(): """Test the predict function in the Poisson class""" # design matrix X = np.array([[np.log(2.5), -1, -3], [np.log(3.5), -2, -4], [np.log(4.5), -3, -5], [np.log(5.5), -4, -6]]) poisson = Poisson() # test for NotFittedError assert_raises(NotFittedError, poisson.predict, X) # create "fit" poisson.coef_ = np.array([1, 0, 0]) poisson.intercept_ = 0 y_pred = poisson.predict(X) y_mode = np.array([2, 3, 4, 5]) # test for predict assert_almost_equal(y_pred, y_mode)
def test_soft_threshold(): """Tests the soft threshold function against equivalent output from a MATLAB implementation.""" true_threshold = np.array([[0, 0.34, 0.45, 0.27, 0.38], [0, 0.26, 0, 0, 0], [0, 0, 0, 0.32, 0], [0.23, 0.43, 0, 0.27, 0.22], [0.19, 0.38, 0, 0.04, 0], [0, 0, 0.05, 0, 0], [0, 0, 0.0, 0, 0.24], [0.44, 0, 0.47, 0, 0], [0.11, 0.05, 0.22, 0, 0], [0.04, 0, 0.48, 0, 0]]) assert np.allclose(true_threshold, Poisson.soft_threshold(X, 0.5))
def test_adjusted_response(): """Tests the adjusted response function against equivalent output from a MATLAB implementation.""" w, z = Poisson.adjusted_response(X, y, beta) w_true = np.array([ 1.419067548593257, 6.488296399286710, 0.990049833749168, 4.854955811237434, 6.488296399286709, 2.054433210643888, 1.537257523548281, 17.115765537145876, 7.099327065156633, 3.706173712210199]) z_true = np.array([ 0.759376179437427, 1.794741970890788, -1.01, 1.403900392819534, 1.794741970890788, 1.180256767879915, -0.57, 2.774810655432013, 2.086867367368360, 2.198740394692808]) assert_allclose(w_true, w) assert_allclose(z_true, z)
def test_Poisson_response_constant(): """Test that UoI Poisson correctly fits the data when the response variable is constant.""" n_features = 5 n_samples = 100 X = np.random.normal(size=(n_samples, n_features)) y = np.zeros(n_samples) poisson = Poisson() poisson.fit(X, y) assert_equal(poisson.intercept_, -np.inf) assert_equal(poisson.coef_, np.zeros(n_features)) y += 1. poisson.fit(X, y) assert_equal(poisson.intercept_, 0.) assert_equal(poisson.coef_, np.zeros(n_features))
def test_poisson_warm_start(): """Tests if the warm start initializes coefficients correctly.""" n_features = 3 n_samples = 10000 # create data X, y, beta, _ = make_poisson_regression(n_samples=n_samples, n_features=n_features, n_informative=n_features, beta=np.array([0.5, 1.0, 1.5]), random_state=2332) # lbfgs poisson = Poisson(alpha=0., l1_ratio=0., fit_intercept=False, solver='lbfgs', max_iter=5000, warm_start=True) poisson.fit(X, y) first_coef = poisson.coef_ poisson.coef_ = np.zeros(n_features) poisson.fit(X, y) second_coef = poisson.coef_ assert_allclose(first_coef, second_coef, rtol=0.1) # cd poisson = Poisson(alpha=0., l1_ratio=0., fit_intercept=False, solver='cd', max_iter=5000, warm_start=True) poisson.fit(X, y) first_coef = poisson.coef_ poisson.coef_ = np.zeros(n_features) poisson.fit(X, y) second_coef = poisson.coef_ assert_allclose(first_coef, second_coef, rtol=0.1)