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