<commit_before># Generic libraries
import GPflow
import numpy as np
import tensorflow as tf
from matplotlib import pyplot as plt
from GPclust import OMGP
import GPy
import GPyOpt
# import pickle
import time
# Branching files
import VBHelperFunctions
import BranchingTree as bt
import branch_kernParamGPflow as bk
import assigngp_denseSparse
import assigngp_dense
import BayesianOptimiser


def InitParams(m):
    m.likelihood.variance = mo.variance.values[0]
    # set lengthscale to maximum
    m.kern.branchkernelparam.kern.lengthscales = np.max(
        np.array([mo.kern[0].lengthscale.values, mo.kern[1].lengthscale.values]))
    # set process variance to average
    m.kern.branchkernelparam.kern.variance = np.mean(
        np.array([mo.kern[0].variance.values, mo.kern[1].variance.values]))

########################################
#         Test parameters
########################################
fPlot = True  # do we do plots?
fUsePriors = False  # Test priors on kernel hyperparameters
fModelSelectionGrid = False
fBO = False  # Bayesian optimisation
fDebug = True  # Enable debugging output - tensorflow print ops
########################################
np.set_printoptions(precision=4)  # precision to print numpy array
seed = 43
np.random.seed(seed=seed)  # easy peasy reproducibeasy
tf.set_random_seed(seed)
# Data generation
N = 20
t = np.linspace(0, 1, N)
print(t)
trueB = np.ones((1, 1))*0.5
Y = np.zeros((N, 1))
idx = np.nonzero(t > 0.5)[0]
idxA = idx[::2]
idxB = idx[1::2]
print(idx)
print(idxA)
print(idxB)
Y[idxA, 0] = 2 * t[idxA]
Y[idxB, 0] = -2 * t[idxB]
# Create tree structures
tree = bt.BinaryBranchingTree(0, 1, fDebug=False)
tree.add(None, 1, trueB)
(fm, _) = tree.GetFunctionBranchTensor()
XExpanded, indices, _ = VBHelperFunctions.GetFunctionIndexListGeneral(t)
print('XExpanded', XExpanded.shape)
print('indices', len(indices))
# Use OMGP for initialisation of branching model
komgp1 = GPy.kern.Matern32(1)
komgp2 = GPy.kern.Matern32(1)
mo = OMGP(t[:, None], Y, K=2, variance=0.01, kernels=[komgp1, komgp2],
          prior_Z='DP')  # use a truncated DP with K=2 UNDONE
mo.kern[0].lengthscale = 5.*np.ptp(t)  # initialise length scale to range of data
mo.kern[1].lengthscale = 5.*np.ptp(t)
mo.optimize(step_length=0.01, maxiter=30)
# plotting OMGP fit
if(fPlot):
    plt.ion()
    fig = plt.figure(figsize=(10, 10))
    mo.plot()
# Create model
Kbranch = bk.BranchKernelParam(GPflow.kernels.Matern32(1), fm, b=trueB.copy()) + GPflow.kernels.White(1)
Kbranch.branchkernelparam.kern.variance = 1
Kbranch.white.variance = 1e-6  # controls the discontinuity magnitude, the gap at the branching point
Kbranch.white.variance.fixed = True  # jitter for numerics
print('Kbranch matrix', Kbranch.compute_K(XExpanded, XExpanded))
print('Branching K free parameters', Kbranch.branchkernelparam)
print('Branching K branching parameter', Kbranch.branchkernelparam.Bv.value)
# Initialise all model parameters using the OMGP model
# Note that the OMGP model has different kernel hyperparameters for each latent function whereas the branching model
# has one common set.
M = 5  # number of inducing pts
ir = np.random.choice(XExpanded.shape[0], M)
ZExpanded = XExpanded  # [ir, :]
print('Random choice for Z', ZExpanded)
mV = assigngp_denseSparse.AssignGPSparse(t, XExpanded, Y, Kbranch, indices, mo.phi,
                                         Kbranch.branchkernelparam.Bv.value, ZExpanded, fDebug=fDebug)
InitParams(mV)

mVFull = assigngp_dense.AssignGP(t, XExpanded, Y, Kbranch, indices, mo.phi,
                                 Kbranch.branchkernelparam.Bv.value, fDebug=fDebug)

InitParams(mVFull)

print('Sparse Ll', mV.compute_log_likelihood(), 'Full Ll', mVFull.compute_log_likelihood())
# put prior to penalise short length scales
assert mV.compute_log_likelihood() == -mV.objectiveFun()
# Prior to penalize small length scales
# print('Initialised mv', mV)
<commit_msg>updated test for clearer output<commit_after># Generic libraries
import GPflow
import numpy as np
import tensorflow as tf
from matplotlib import pyplot as plt
from GPclust import OMGP
import GPy
import GPyOpt
# import pickle
import time
# Branching files
import VBHelperFunctions
import BranchingTree as bt
import branch_kernParamGPflow as bk
import assigngp_denseSparse
import assigngp_dense
import BayesianOptimiser


def InitParams(m):
    m.likelihood.variance = mo.variance.values[0]
    # set lengthscale to maximum
    m.kern.branchkernelparam.kern.lengthscales = np.max(
        np.array([mo.kern[0].lengthscale.values, mo.kern[1].lengthscale.values]))
    # set process variance to average
    m.kern.branchkernelparam.kern.variance = np.mean(
        np.array([mo.kern[0].variance.values, mo.kern[1].variance.values]))

########################################
#         Test parameters
########################################
fPlot = True  # do we do plots?
fUsePriors = False  # Test priors on kernel hyperparameters
fModelSelectionGrid = False
fBO = False  # Bayesian optimisation
fDebug = True  # Enable debugging output - tensorflow print ops
########################################
np.set_printoptions(precision=4)  # precision to print numpy array
seed = 43
np.random.seed(seed=seed)  # easy peasy reproducibeasy
tf.set_random_seed(seed)
# Data generation
N = 20
t = np.linspace(0, 1, N)
print(t)
trueB = np.ones((1, 1))*0.5
Y = np.zeros((N, 1))
idx = np.nonzero(t > 0.5)[0]
idxA = idx[::2]
idxB = idx[1::2]
print(idx)
print(idxA)
print(idxB)
Y[idxA, 0] = 2 * t[idxA]
Y[idxB, 0] = -2 * t[idxB]
# Create tree structures
tree = bt.BinaryBranchingTree(0, 1, fDebug=False)
tree.add(None, 1, trueB)
(fm, _) = tree.GetFunctionBranchTensor()
XExpanded, indices, _ = VBHelperFunctions.GetFunctionIndexListGeneral(t)
print('XExpanded', XExpanded.shape)
print('indices', len(indices))
# Use OMGP for initialisation of branching model
komgp1 = GPy.kern.Matern32(1)
komgp2 = GPy.kern.Matern32(1)
mo = OMGP(t[:, None], Y, K=2, variance=0.01, kernels=[komgp1, komgp2],
          prior_Z='DP')  # use a truncated DP with K=2 UNDONE
mo.kern[0].lengthscale = 5.*np.ptp(t)  # initialise length scale to range of data
mo.kern[1].lengthscale = 5.*np.ptp(t)
mo.optimize(step_length=0.01, maxiter=30)
# plotting OMGP fit
if(fPlot):
    plt.ion()
    fig = plt.figure(figsize=(10, 10))
    mo.plot()
# Create model
Kbranch = bk.BranchKernelParam(GPflow.kernels.Matern32(1), fm, b=trueB.copy()) + GPflow.kernels.White(1)
Kbranch.branchkernelparam.kern.variance = 1
Kbranch.white.variance = 1e-6  # controls the discontinuity magnitude, the gap at the branching point
Kbranch.white.variance.fixed = True  # jitter for numerics
print('Kbranch matrix', Kbranch.compute_K(XExpanded, XExpanded))
print('Branching K free parameters', Kbranch.branchkernelparam)
print('Branching K branching parameter', Kbranch.branchkernelparam.Bv.value)
# Initialise all model parameters using the OMGP model
# Note that the OMGP model has different kernel hyperparameters for each latent function whereas the branching model
# has one common set.
M = 5  # number of inducing pts
ir = np.random.choice(XExpanded.shape[0], M)
ZExpanded = XExpanded  # [ir, :]
print('Random choice for Z', ZExpanded)
mV = assigngp_denseSparse.AssignGPSparse(t, XExpanded, Y, Kbranch, indices, mo.phi,
                                         Kbranch.branchkernelparam.Bv.value, ZExpanded, fDebug=fDebug)
InitParams(mV)

mVFull = assigngp_dense.AssignGP(t, XExpanded, Y, Kbranch, indices, mo.phi,
                                 Kbranch.branchkernelparam.Bv.value, fDebug=fDebug)

InitParams(mVFull)

lsparse = mV.compute_log_likelihood()
lfull = mVFull.compute_log_likelihood()
print('Sparse Log lik', lsparse, 'Full Log luk', lfull)
# put prior to penalise short length scales
assert np.allclose(lsparse, lfull), 'Log likelihoods not close'
# Prior to penalize small length scales
# print('Initialised mv', mV)

{'fraction_non_alphanumeric': 0.14007881021990592, 'fraction_numerical': 0.01703317656031524, 'mean_word_length': 4.718023255813954, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4790343', 'n_tokens_mistral': 2723, 'n_tokens_neox': 2587, 'n_words': 704}