def test_kernel_decompose_1d(): '''Checks that a kernel decomposes into a sum properly''' sk = fk.repr_string_to_kernel('ScoredKernel(k_opt=ProductKernel([ MaskKernel(ndim=1, active_dimension=0, base_kernel=RQKernel(lengthscale=4.853529, output_variance=-0.648382, alpha=0.457387)), SumKernel([ ProductKernel([ MaskKernel(ndim=1, active_dimension=0, base_kernel=SqExpPeriodicKernel(lengthscale=1.395371, period=-3.990523, output_variance=0.565365)), MaskKernel(ndim=1, active_dimension=0, base_kernel=LinKernel(offset=0.000420, lengthscale=-0.120045)) ]), ProductKernel([ MaskKernel(ndim=1, active_dimension=0, base_kernel=LinKernel(offset=0.802417, lengthscale=3.350816)), SumKernel([ MaskKernel(ndim=1, active_dimension=0, base_kernel=SqExpPeriodicKernel(lengthscale=-3.899540, period=0.087011, output_variance=2.430187)), MaskKernel(ndim=1, active_dimension=0, base_kernel=RQKernel(lengthscale=3.865315, output_variance=4.028649, alpha=-5.060996)), ProductKernel([ MaskKernel(ndim=1, active_dimension=0, base_kernel=SqExpPeriodicKernel(lengthscale=3.251723, period=1.540000, output_variance=-2.497487)), MaskKernel(ndim=1, active_dimension=0, base_kernel=LinKernel(offset=-1.424416, lengthscale=-1.732677)) ]) ]) ]) ]) ]), nll=558.339977, laplace_nle=-266.580399, bic_nle=1216.076221, noise=[ 1.66059002])') k = fk.strip_masks(sk.k_opt) correct_answer = ['RQ \\times Per \\times Lin', 'RQ \\times Lin \\times Per', 'RQ \\times Lin \\times RQ', 'RQ \\times Lin \\times Per \\times Lin'] kd = fk.break_kernel_into_summands(k) assert( [k.latex_print() for k in kd] == correct_answer )
def test_kernel_decompose_1d(): '''Checks that a kernel decomposes into a sum properly''' sk = fk.repr_string_to_kernel( 'ScoredKernel(k_opt=ProductKernel([ MaskKernel(ndim=1, active_dimension=0, base_kernel=RQKernel(lengthscale=4.853529, output_variance=-0.648382, alpha=0.457387)), SumKernel([ ProductKernel([ MaskKernel(ndim=1, active_dimension=0, base_kernel=SqExpPeriodicKernel(lengthscale=1.395371, period=-3.990523, output_variance=0.565365)), MaskKernel(ndim=1, active_dimension=0, base_kernel=LinKernel(offset=0.000420, lengthscale=-0.120045)) ]), ProductKernel([ MaskKernel(ndim=1, active_dimension=0, base_kernel=LinKernel(offset=0.802417, lengthscale=3.350816)), SumKernel([ MaskKernel(ndim=1, active_dimension=0, base_kernel=SqExpPeriodicKernel(lengthscale=-3.899540, period=0.087011, output_variance=2.430187)), MaskKernel(ndim=1, active_dimension=0, base_kernel=RQKernel(lengthscale=3.865315, output_variance=4.028649, alpha=-5.060996)), ProductKernel([ MaskKernel(ndim=1, active_dimension=0, base_kernel=SqExpPeriodicKernel(lengthscale=3.251723, period=1.540000, output_variance=-2.497487)), MaskKernel(ndim=1, active_dimension=0, base_kernel=LinKernel(offset=-1.424416, lengthscale=-1.732677)) ]) ]) ]) ]) ]), nll=558.339977, laplace_nle=-266.580399, bic_nle=1216.076221, noise=[ 1.66059002])' ) k = fk.strip_masks(sk.k_opt) correct_answer = [ 'RQ \\times Per \\times Lin', 'RQ \\times Lin \\times Per', 'RQ \\times Lin \\times RQ', 'RQ \\times Lin \\times Per \\times Lin' ] kd = fk.break_kernel_into_summands(k) assert ([k.latex_print() for k in kd] == correct_answer)
def plot_decomposition(kernel, X, y, figname, noise=None, X_mean=0, X_scale=1, y_mean=0, y_scale=1): matlab_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "matlab")) figname = os.path.abspath(os.path.join(os.path.dirname(__file__), figname)) print "Plotting to: %s" % figname kernel_components = fk.break_kernel_into_summands(kernel) latex_names = [k.latex_print().strip() for k in kernel_components] kernel_params_list = ",".join("[ %s ]" % " ".join(str(p) for p in k.param_vector()) for k in kernel_components) if X.ndim == 1: X = X[:, nax] if y.ndim == 1: y = y[:, nax] if noise is None: noise = np.log(np.var(y) / 10) # Just a heuristic. data = {"X": X, "y": y} (fd1, temp_data_file) = tempfile.mkstemp(suffix=".mat") scipy.io.savemat(temp_data_file, data) code = MATLAB_PLOT_DECOMP_CALLER_CODE % { "datafile": temp_data_file, "gpml_path": config.GPML_PATH, "matlab_script_path": matlab_dir, "kernel_family": kernel.gpml_kernel_expression(), "kernel_params": "[ %s ]" % " ".join(str(p) for p in kernel.param_vector()), "kernel_family_list": "{ %s }" % ",".join(str(k.gpml_kernel_expression()) for k in kernel_components), "kernel_params_list": "{ %s }" % kernel_params_list, "noise": str(noise), "latex_names": "{ ' %s ' }" % "','".join(latex_names), "full_kernel_name": "{ '%s' }" % kernel.latex_print().strip(), "figname": figname, "X_mean": X_mean, "X_scale": X_scale, "y_mean": y_mean, "y_scale": y_scale, } run_matlab_code(code, verbose=True, jvm=True) os.close(fd1) # os.remove(temp_data_file) return code
def plot_decomposition(kernel, X, y, figname, noise=None, X_mean=0, X_scale=1, y_mean=0, y_scale=1): matlab_dir = os.path.abspath( os.path.join(os.path.dirname(__file__), 'matlab')) figname = os.path.abspath(os.path.join(os.path.dirname(__file__), figname)) print 'Plotting to: %s' % figname kernel_components = fk.break_kernel_into_summands(kernel) latex_names = [k.latex_print().strip() for k in kernel_components] kernel_params_list = ','.join('[ %s ]' % ' '.join(str(p) for p in k.param_vector()) for k in kernel_components) if X.ndim == 1: X = X[:, nax] if y.ndim == 1: y = y[:, nax] if noise is None: noise = np.log(np.var(y) / 10) # Just a heuristic. data = {'X': X, 'y': y} (fd1, temp_data_file) = tempfile.mkstemp(dir=config.LOCAL_TEMP_PATH, suffix='.mat') scipy.io.savemat(temp_data_file, data) code = MATLAB_PLOT_DECOMP_CALLER_CODE % { 'datafile': temp_data_file, 'gpml_path': config.GPML_PATH, 'matlab_script_path': matlab_dir, 'kernel_family': kernel.gpml_kernel_expression(), 'kernel_params': '[ %s ]' % ' '.join(str(p) for p in kernel.param_vector()), 'kernel_family_list': '{ %s }' % ','.join(str(k.gpml_kernel_expression()) for k in kernel_components), 'kernel_params_list': '{ %s }' % kernel_params_list, 'noise': str(noise), 'latex_names': "{ ' %s ' }" % "','".join(latex_names), 'full_kernel_name': "{ '%s' }" % kernel.latex_print().strip(), 'figname': figname, 'X_mean': X_mean, 'X_scale': X_scale, 'y_mean': y_mean, 'y_scale': y_scale } run_matlab_code(code, verbose=True, jvm=True) os.close(fd1) #os.remove(temp_data_file) return code