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)