# coding: utf-8
# pylint: disable = invalid-name, C0111
import gpboost as gpb
import numpy as np
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
plt.style.use('ggplot')

#--------------------Cross validation for tree-boosting without GP or random effects----------------
print('Simulating data...')
# Simulate and create your dataset
def f1d(x):
    """Non-linear function for simulation"""
    return (1.7 * (1 / (1 + np.exp(-(x - 0.5) * 20)) + 0.75 * x))
x = np.linspace(0, 1, 200, endpoint=True)
plt.plot(x, f1d(x), linewidth=2, color="r")
plt.title("Mean function")
plt.show()
def sim_data(n):
    """Function that simulates data. Two covariates of which only one has an effect"""
    X = np.random.rand(n, 2)
    # mean function plus noise
    y = f1d(X[:, 0]) + np.random.normal(scale=0.1, size=n)
    return ([X, y])

# Simulate data
n = 1000
data = sim_data(2 * n)
# create dataset for gpb.train
data_train = gpb.Dataset(data[0][0:n, :], data[1][0:n])

# specify your configurations as a dict
params = {
    'objective': 'regression_l2',
    'metric': {'l2', 'l1'},
    'learning_rate': 0.1,
    'max_depth': 6,
    'min_data_in_leaf': 5,
    'verbose': 0
}

print('Starting cross-validation...')
# do cross-validation
cvbst = gpb.cv(params=params, train_set=data_train,
               num_boost_round=100, early_stopping_rounds=5,
               nfold=2, verbose_eval=True, show_stdv=False, seed=1)
print("Best number of iterations: " + str(np.argmin(cvbst['l2-mean'])))


# --------------------Combine tree-boosting and grouped random effects model----------------
print('Simulating data...')
# Simulate data
def f1d(x):
    """Non-linear function for simulation"""
    return (1.7 * (1 / (1 + np.exp(-(x - 0.5) * 20)) + 0.75 * x))
x = np.linspace(0, 1, 200, endpoint=True)
plt.figure("Mean function")
plt.plot(x, f1d(x), linewidth=2, color="r")
plt.title("Mean function")
plt.show()
n = 1000  # number of samples
np.random.seed(1)
X = np.random.rand(n, 2)
F = f1d(X[:, 0])
# Simulate grouped random effects
m = 25  # number of categories / levels for grouping variable
group = np.arange(n)  # grouping variable
for i in range(m):
    group[int(i * n / m):int((i + 1) * n / m)] = i
# incidence matrix relating grouped random effects to samples
Z1 = np.zeros((n, m))
for i in range(m):
    Z1[np.where(group == i), i] = 1
sigma2_1 = 1 ** 2  # random effect variance
sigma2 = 0.1 ** 2  # error variance
b1 = np.sqrt(sigma2_1) * np.random.normal(size=m)  # simulate random effects
eps = Z1.dot(b1)
xi = np.sqrt(sigma2) * np.random.normal(size=n)  # simulate error term
y = F + eps + xi  # observed data

# define GPModel
gp_model = gpb.GPModel(group_data=group)
gp_model.set_optim_params(params={"optimizer_cov": "fisher_scoring"})
# create dataset for gpb.train
data_train = gpb.Dataset(X, y)
# specify your configurations as a dict
params = {
    'objective': 'regression_l2',
    'learning_rate': 0.05,
    'max_depth': 6,
    'min_data_in_leaf': 5,
    'verbose': 0
}

print('Starting cross-validation...')
# do cross-validation
cvbst = gpb.cv(params=params, train_set=data_train,
               gp_model=gp_model, use_gp_model_for_validation=False,
               num_boost_round=100, early_stopping_rounds=5,
               nfold=2, verbose_eval=True, show_stdv=False, seed=1)
print("Best number of iterations: " + str(np.argmin(cvbst['l2-mean'])))

# Include random effect predictions for validation (observe the lower test error)
gp_model = gpb.GPModel(group_data=group)
print("Running cross validation for GPBoost model and use_gp_model_for_validation = TRUE")
cvbst = gpb.cv(params=params, train_set=data_train,
               gp_model=gp_model, use_gp_model_for_validation=True,
               num_boost_round=100, early_stopping_rounds=5,
               nfold=2, verbose_eval=True, show_stdv=Falsem, seed=1)
print("Best number of iterations: " + str(np.argmin(cvbst['l2-mean'])))
cvbst.best_iteration

[((176, 199), 'matplotlib.pyplot.style.use', 'plt.style.use', (['"""ggplot"""'], {}), "('ggplot')\n", (189, 199), True, 'import matplotlib.pyplot as plt\n'), ((492, 529), 'numpy.linspace', 'np.linspace', (['(0)', '(1)', '(200)'], {'endpoint': '(True)'}), '(0, 1, 200, endpoint=True)\n', (503, 529), True, 'import numpy as np\n'), ((574, 600), 'matplotlib.pyplot.title', 'plt.title', (['"""Mean function"""'], {}), "('Mean function')\n", (583, 600), True, 'import matplotlib.pyplot as plt\n'), ((601, 611), 'matplotlib.pyplot.show', 'plt.show', ([], {}), '()\n', (609, 611), True, 'import matplotlib.pyplot as plt\n'), ((948, 990), 'gpboost.Dataset', 'gpb.Dataset', (['data[0][0:n, :]', 'data[1][0:n]'], {}), '(data[0][0:n, :], data[1][0:n])\n', (959, 990), True, 'import gpboost as gpb\n'), ((1266, 1416), 'gpboost.cv', 'gpb.cv', ([], {'params': 'params', 'train_set': 'data_train', 'num_boost_round': '(100)', 'early_stopping_rounds': '(5)', 'nfold': '(2)', 'verbose_eval': '(True)', 'show_stdv': '(False)', 'seed': '(1)'}), '(params=params, train_set=data_train, num_boost_round=100,\n    early_stopping_rounds=5, nfold=2, verbose_eval=True, show_stdv=False,\n    seed=1)\n', (1272, 1416), True, 'import gpboost as gpb\n'), ((1777, 1814), 'numpy.linspace', 'np.linspace', (['(0)', '(1)', '(200)'], {'endpoint': '(True)'}), '(0, 1, 200, endpoint=True)\n', (1788, 1814), True, 'import numpy as np\n'), ((1815, 1842), 'matplotlib.pyplot.figure', 'plt.figure', (['"""Mean function"""'], {}), "('Mean function')\n", (1825, 1842), True, 'import matplotlib.pyplot as plt\n'), ((1887, 1913), 'matplotlib.pyplot.title', 'plt.title', (['"""Mean function"""'], {}), "('Mean function')\n", (1896, 1913), True, 'import matplotlib.pyplot as plt\n'), ((1914, 1924), 'matplotlib.pyplot.show', 'plt.show', ([], {}), '()\n', (1922, 1924), True, 'import matplotlib.pyplot as plt\n'), ((1955, 1972), 'numpy.random.seed', 'np.random.seed', (['(1)'], {}), '(1)\n', (1969, 1972), True, 'import numpy as np\n'), ((1977, 1997), 'numpy.random.rand', 'np.random.rand', (['n', '(2)'], {}), '(n, 2)\n', (1991, 1997), True, 'import numpy as np\n'), ((2119, 2131), 'numpy.arange', 'np.arange', (['n'], {}), '(n)\n', (2128, 2131), True, 'import numpy as np\n'), ((2290, 2306), 'numpy.zeros', 'np.zeros', (['(n, m)'], {}), '((n, m))\n', (2298, 2306), True, 'import numpy as np\n'), ((2670, 2699), 'gpboost.GPModel', 'gpb.GPModel', ([], {'group_data': 'group'}), '(group_data=group)\n', (2681, 2699), True, 'import gpboost as gpb\n'), ((2814, 2831), 'gpboost.Dataset', 'gpb.Dataset', (['X', 'y'], {}), '(X, y)\n', (2825, 2831), True, 'import gpboost as gpb\n'), ((3079, 3287), 'gpboost.cv', 'gpb.cv', ([], {'params': 'params', 'train_set': 'data_train', 'gp_model': 'gp_model', 'use_gp_model_for_validation': '(False)', 'num_boost_round': '(100)', 'early_stopping_rounds': '(5)', 'nfold': '(2)', 'verbose_eval': '(True)', 'show_stdv': '(False)', 'seed': '(1)'}), '(params=params, train_set=data_train, gp_model=gp_model,\n    use_gp_model_for_validation=False, num_boost_round=100,\n    early_stopping_rounds=5, nfold=2, verbose_eval=True, show_stdv=False,\n    seed=1)\n', (3085, 3287), True, 'import gpboost as gpb\n'), ((3487, 3516), 'gpboost.GPModel', 'gpb.GPModel', ([], {'group_data': 'group'}), '(group_data=group)\n', (3498, 3516), True, 'import gpboost as gpb\n'), ((3616, 3824), 'gpboost.cv', 'gpb.cv', ([], {'params': 'params', 'train_set': 'data_train', 'gp_model': 'gp_model', 'use_gp_model_for_validation': '(True)', 'num_boost_round': '(100)', 'early_stopping_rounds': '(5)', 'nfold': '(2)', 'verbose_eval': '(True)', 'show_stdv': 'Falsem', 'seed': '(1)'}), '(params=params, train_set=data_train, gp_model=gp_model,\n    use_gp_model_for_validation=True, num_boost_round=100,\n    early_stopping_rounds=5, nfold=2, verbose_eval=True, show_stdv=Falsem,\n    seed=1)\n', (3622, 3824), True, 'import gpboost as gpb\n'), ((724, 744), 'numpy.random.rand', 'np.random.rand', (['n', '(2)'], {}), '(n, 2)\n', (738, 744), True, 'import numpy as np\n'), ((2447, 2464), 'numpy.sqrt', 'np.sqrt', (['sigma2_1'], {}), '(sigma2_1)\n', (2454, 2464), True, 'import numpy as np\n'), ((2467, 2491), 'numpy.random.normal', 'np.random.normal', ([], {'size': 'm'}), '(size=m)\n', (2483, 2491), True, 'import numpy as np\n'), ((2541, 2556), 'numpy.sqrt', 'np.sqrt', (['sigma2'], {}), '(sigma2)\n', (2548, 2556), True, 'import numpy as np\n'), ((2559, 2583), 'numpy.random.normal', 'np.random.normal', ([], {'size': 'n'}), '(size=n)\n', (2575, 2583), True, 'import numpy as np\n'), ((799, 834), 'numpy.random.normal', 'np.random.normal', ([], {'scale': '(0.1)', 'size': 'n'}), '(scale=0.1, size=n)\n', (815, 834), True, 'import numpy as np\n'), ((1481, 1508), 'numpy.argmin', 'np.argmin', (["cvbst['l2-mean']"], {}), "(cvbst['l2-mean'])\n", (1490, 1508), True, 'import numpy as np\n'), ((3363, 3390), 'numpy.argmin', 'np.argmin', (["cvbst['l2-mean']"], {}), "(cvbst['l2-mean'])\n", (3372, 3390), True, 'import numpy as np\n'), ((3900, 3927), 'numpy.argmin', 'np.argmin', (["cvbst['l2-mean']"], {}), "(cvbst['l2-mean'])\n", (3909, 3927), True, 'import numpy as np\n'), ((2333, 2353), 'numpy.where', 'np.where', (['(group == i)'], {}), '(group == i)\n', (2341, 2353), True, 'import numpy as np\n'), ((450, 473), 'numpy.exp', 'np.exp', (['(-(x - 0.5) * 20)'], {}), '(-(x - 0.5) * 20)\n', (456, 473), True, 'import numpy as np\n'), ((1735, 1758), 'numpy.exp', 'np.exp', (['(-(x - 0.5) * 20)'], {}), '(-(x - 0.5) * 20)\n', (1741, 1758), True, 'import numpy as np\n')]