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_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_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_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_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)
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_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))