def test_glm_gaussian(make_gaus_data, make_random): X, y, Xs, ys = make_gaus_data basis = LinearBasis(onescol=True) lhood = Gaussian() # simple SGD glm = GeneralizedLinearModel(lhood, basis, random_state=make_random) glm.fit(X, y) Ey = glm.predict(Xs) assert smse(ys, Ey) < 0.1 # Test BasisCat basis = LinearBasis(onescol=True) \ + RandomRBF(nbases=20, Xdim=X.shape[1]) \ + RandomMatern52(nbases=20, Xdim=X.shape[1]) glm = GeneralizedLinearModel(lhood, basis, random_state=make_random) glm.fit(X, y) Ey = glm.predict(Xs) assert smse(ys, Ey) < 0.1 # Test upper quantile estimates py, _, _ = glm.predict_cdf(Xs, 1e5) assert np.allclose(py, 1.) # Test log probability lpy, _, _ = glm.predict_logpdf(Xs, Ey) assert np.all(lpy > -100) EyQn, EyQx = glm.predict_interval(Xs, 0.9) assert all(Ey <= EyQx) assert all(Ey >= EyQn)
def test_glm_gaussian(make_gaus_data, make_random): X, y, Xs, ys = make_gaus_data basis = LinearBasis(onescol=True) lhood = Gaussian() # simple SGD glm = GeneralizedLinearModel(lhood, basis, random_state=make_random) glm.fit(X, y) Ey = glm.predict(Xs) assert smse(ys, Ey) < 0.1 # Test BasisCat basis = LinearBasis(onescol=True) \ + RandomRBF(nbases=20, Xdim=X.shape[1]) \ + RandomMatern52(nbases=20, Xdim=X.shape[1]) glm = GeneralizedLinearModel(lhood, basis, random_state=make_random) glm.fit(X, y) Ey = glm.predict(Xs) assert smse(ys, Ey) < 0.1 # Test upper quantile estimates py, _, _ = glm.predict_cdf(Xs, 1e5) assert np.allclose(py, 1.) # Test log probability lpy, _, _ = glm.predict_logpdf(Xs, Ey) assert np.all(lpy > -100) EyQn, EyQx = glm.predict_interval(Xs, 0.9) assert all(Ey <= EyQx) assert all(Ey >= EyQn)
def test_glm_binomial(make_binom_data, make_random): # This is more to test the logic than to test if the model can overfit, # hence more relaxed SMSE. This is because this is a harder problem than # the previous case. We also haven't split training ans test sets, since we # want to check the latent function and bounds X, y, p, n = make_binom_data f = p * n basis = LinearBasis(onescol=True) \ + RandomRBF(nbases=20, Xdim=X.shape[1]) \ + RandomMatern52(nbases=20, Xdim=X.shape[1]) lhood = Binomial() largs = (n,) # SGD glm = GeneralizedLinearModel(lhood, basis, random_state=make_random) glm.fit(X, y, likelihood_args=largs) Ey = glm.predict(X, likelihood_args=largs) assert smse(f, Ey) < 1 # Test upper quantile estimates py, _, _ = glm.predict_cdf(X, 1e5, likelihood_args=largs) assert np.allclose(py, 1.) EyQn, EyQx = glm.predict_interval(X, 0.9, likelihood_args=largs) assert all(Ey <= EyQx) assert all(Ey >= EyQn)
def test_glm_binomial(make_binom_data, make_random): # This is more to test the logic than to test if the model can overfit, # hence more relaxed SMSE. This is because this is a harder problem than # the previous case. We also haven't split training ans test sets, since we # want to check the latent function and bounds X, y, p, n = make_binom_data f = p * n basis = LinearBasis(onescol=True) \ + RandomRBF(nbases=20, Xdim=X.shape[1]) \ + RandomMatern52(nbases=20, Xdim=X.shape[1]) lhood = Binomial() largs = (n, ) # SGD glm = GeneralizedLinearModel(lhood, basis, random_state=make_random) glm.fit(X, y, likelihood_args=largs) Ey = glm.predict(X, likelihood_args=largs) assert smse(f, Ey) < 1 # Test upper quantile estimates py, _, _ = glm.predict_cdf(X, 1e5, likelihood_args=largs) assert np.allclose(py, 1.) EyQn, EyQx = glm.predict_interval(X, 0.9, likelihood_args=largs) assert all(Ey <= EyQx) assert all(Ey >= EyQn)
def test_glm_binomial(make_binom_data): # This is more to test the logic than to test if the model can overfit, # hence more relaxed SMSE. This is because this is a harder problem than # the previous case. X, y, p, n = make_binom_data f = p * n basis = LinearBasis(onescol=True) \ + RandomRBF(nbases=20, Xdim=X.shape[1]) \ + RandomMatern52(nbases=20, Xdim=X.shape[1]) lhood = Binomial() largs = (n,) # SGD glm = GeneralisedLinearModel(lhood, basis, random_state=randstate) glm.fit(X, y, likelihood_args=largs) Ey = glm.predict(X, likelihood_args=largs) assert smse(f, Ey) < 1 # Test upper quantile estimates py, _, _ = glm.predict_cdf(1e5, X, likelihood_args=largs) assert np.allclose(py, 1.) EyQn, EyQx = glm.predict_interval(0.9, X, likelihood_args=largs) assert all(Ey <= EyQx) assert all(Ey >= EyQn)
def test_pipeline_slm(make_gaus_data): X, y, Xs, ys = make_gaus_data slm = StandardLinearModel(LinearBasis(onescol=True)) estimators = [('PCA', PCA()), ('SLM', slm)] pipe = Pipeline(estimators) pipe.fit(X, y) Ey = pipe.predict(Xs) assert smse(ys, Ey) < 0.1
def test_pipeline_slm(make_gaus_data): X, y, Xs, ys = make_gaus_data slm = StandardLinearModel(LinearBasis(onescol=True)) estimators = [('PCA', PCA()), ('SLM', slm)] pipe = Pipeline(estimators) pipe.fit(X, y) Ey = pipe.predict(Xs) assert smse(ys, Ey) < 0.1
def test_slm(make_gaus_data): X, y, Xs, ys = make_gaus_data basis = LinearBasis(onescol=False) slm = StandardLinearModel(basis) slm.fit(X, y) Ey = slm.predict(Xs) assert smse(ys, Ey) < 0.1 basis = LinearBasis(onescol=False) \ + RandomRBF(nbases=10, Xdim=X.shape[1]) \ + RandomMatern52(nbases=10, Xdim=X.shape[1]) slm = StandardLinearModel(basis) slm.fit(X, y) Ey = slm.predict(Xs) assert smse(ys, Ey) < 0.1
def test_slm(make_gaus_data): X, y, Xs, ys = make_gaus_data basis = LinearBasis(onescol=False) slm = StandardLinearModel(basis) slm.fit(X, y) Ey = slm.predict(Xs) assert smse(ys, Ey) < 0.1 basis = LinearBasis(onescol=False) \ + RandomRBF(nbases=10, Xdim=X.shape[1]) \ + RandomMatern52(nbases=10, Xdim=X.shape[1]) slm = StandardLinearModel(basis) slm.fit(X, y) Ey = slm.predict(Xs) assert smse(ys, Ey) < 0.1
def test_pipeline_glm(make_gaus_data, make_random): X, y, Xs, ys = make_gaus_data glm = GeneralizedLinearModel(Gaussian(), LinearBasis(onescol=True), random_state=make_random) estimators = [('PCA', PCA()), ('SLM', glm)] pipe = Pipeline(estimators) pipe.fit(X, y) Ey = pipe.predict(Xs) assert smse(ys, Ey) < 0.1
def test_pipeline_glm(make_gaus_data, make_random): X, y, Xs, ys = make_gaus_data glm = GeneralizedLinearModel(Gaussian(), LinearBasis(onescol=True), random_state=make_random) estimators = [('PCA', PCA()), ('SLM', glm) ] pipe = Pipeline(estimators) pipe.fit(X, y) Ey = pipe.predict(Xs) assert smse(ys, Ey) < 0.1
from uncoverml import mpiops, diagnostics from uncoverml import predict, geoio from uncoverml import features as feat from uncoverml import targets as targ from uncoverml.learn import all_modelmaps as modelmaps from uncoverml.optimise.models import transformed_modelmaps _logger = logging.getLogger(__name__) MINPROB = 1e-5 # Numerical guard for log-loss evaluation regression_metrics = { 'r2_score': lambda y, py, vy, y_t, py_t, vy_t: r2_score(y, py), 'expvar': lambda y, py, vy, y_t, py_t, vy_t: explained_variance_score(y, py), 'smse': lambda y, py, vy, y_t, py_t, vy_t: smse(y, py), 'lins_ccc': lambda y, py, vy, y_t, py_t, vy_t: lins_ccc(y, py), 'mll': lambda y, py, vy, y_t, py_t, vy_t: mll(y, py, vy) } transformed_regression_metrics = { 'r2_score_transformed': lambda y, py, vy, y_t, py_t, vy_t: r2_score(y_t, py_t), 'expvar_transformed': lambda y, py, vy, y_t, py_t, vy_t: explained_variance_score(y_t, py_t), 'smse_transformed': lambda y, py, vy, y_t, py_t, vy_t: smse(y_t, py_t), 'lins_ccc_transformed': lambda y, py, vy, y_t, py_t, vy_t: lins_ccc(y_t, py_t), 'mll_transformed': lambda y, py, vy, y_t, py_t, vy_t: mll(y_t, py_t, vy_t) } def _binarizer(y, p, func, **kwargs): yb = np.zeros_like(p)
# Log output to the terminal attached to this notebook logging.basicConfig(level=logging.INFO) # Load the data boston = load_boston() X = boston.data y = boston.target - boston.target.mean() folds = 5 (tr_ind, ts_ind) = list(KFold(len(y), n_folds=folds, shuffle=True))[0] # Make Basis and Likelihood N, D = X.shape lenscale = 10. nbases = 50 lenARD = lenscale * np.ones(D) lenscale_init = Parameter(lenARD, Positive()) base = LinearBasis(onescol=True) + RandomMatern32( Xdim=D, nbases=nbases, lenscale_init=lenscale_init) like = Gaussian() # Fit and predict the model glm = GeneralisedLinearModel(like, base, maxiter=6000) glm.fit(X[tr_ind], y[tr_ind]) Ey, Vy = glm.predict_moments(X[ts_ind]) # Score y_true = y[ts_ind] print("SMSE = {}, MSLL = {}".format(smse(y_true, Ey), msll(y_true, Ey, Vy, y[tr_ind])))
from uncoverml import mpiops from uncoverml import predict, geoio from uncoverml import features as feat from uncoverml import targets as targ from uncoverml.learn import all_modelmaps as modelmaps from uncoverml.optimise.models import transformed_modelmaps log = logging.getLogger(__name__) MINPROB = 1e-5 # Numerical guard for log-loss evaluation regression_metrics = { 'r2_score': lambda y, py, vy, y_t, py_t, vy_t: r2_score(y, py), 'expvar': lambda y, py, vy, y_t, py_t, vy_t: explained_variance_score(y, py), 'smse': lambda y, py, vy, y_t, py_t, vy_t: smse(y, py), 'lins_ccc': lambda y, py, vy, y_t, py_t, vy_t: lins_ccc(y, py), 'mll': lambda y, py, vy, y_t, py_t, vy_t: mll(y, py, vy) } transformed_regression_metrics = { 'r2_score_transformed': lambda y, py, vy, y_t, py_t, vy_t: r2_score(y_t, py_t), 'expvar_transformed': lambda y, py, vy, y_t, py_t, vy_t: explained_variance_score(y_t, py_t), 'smse_transformed': lambda y, py, vy, y_t, py_t, vy_t: smse(y_t, py_t), 'lins_ccc_transformed': lambda y, py, vy, y_t, py_t, vy_t: lins_ccc(y_t, py_t), 'mll_transformed': lambda y, py, vy, y_t, py_t, vy_t: mll(y_t, py_t, vy_t)
# Log output to the terminal attached to this notebook logging.basicConfig(level=logging.INFO) # Load the data boston = load_boston() X = boston.data y = boston.target - boston.target.mean() folds = 5 (tr_ind, ts_ind) = list(KFold(len(y), n_folds=folds, shuffle=True))[0] # Make Basis and Likelihood N, D = X.shape lenscale = 10. nbases = 50 lenARD = lenscale * np.ones(D) lenscale_init = Parameter(lenARD, Positive()) base = LinearBasis(onescol=True) + RandomMatern32(Xdim=D, nbases=nbases, lenscale_init=lenscale_init) like = Gaussian() # Fit and predict the model glm = GeneralisedLinearModel(like, base, maxiter=6000) glm.fit(X[tr_ind], y[tr_ind]) Ey, Vy = glm.predict_moments(X[ts_ind]) # Score y_true = y[ts_ind] print("SMSE = {}, MSLL = {}".format(smse(y_true, Ey), msll(y_true, Ey, Vy, y[tr_ind])))