def rbf_sympy(input_dim, ARD=False, variance=1., lengthscale=1.): """ Radial Basis Function covariance. """ X = [sp.var('x%i' % i) for i in range(input_dim)] Z = [sp.var('z%i' % i) for i in range(input_dim)] rbf_variance = sp.var('rbf_variance', positive=True) if ARD: rbf_lengthscales = [ sp.var('rbf_lengthscale_%i' % i, positive=True) for i in range(input_dim) ] dist_string = ' + '.join([ '(x%i-z%i)**2/rbf_lengthscale_%i**2' % (i, i, i) for i in range(input_dim) ]) dist = parse_expr(dist_string) f = rbf_variance * sp.exp(-dist / 2.) else: rbf_lengthscale = sp.var('rbf_lengthscale', positive=True) dist_string = ' + '.join( ['(x%i-z%i)**2' % (i, i) for i in range(input_dim)]) dist = parse_expr(dist_string) f = rbf_variance * sp.exp(-dist / (2 * rbf_lengthscale**2)) return kern(input_dim, [spkern(input_dim, f)])
def rbf_sympy(input_dim, ARD=False, variance=1., lengthscale=1.): """ Radial Basis Function covariance. """ X = [sp.var('x%i' % i) for i in range(input_dim)] Z = [sp.var('z%i' % i) for i in range(input_dim)] rbf_variance = sp.var('rbf_variance',positive=True) if ARD: rbf_lengthscales = [sp.var('rbf_lengthscale_%i' % i, positive=True) for i in range(input_dim)] dist_string = ' + '.join(['(x%i-z%i)**2/rbf_lengthscale_%i**2' % (i, i, i) for i in range(input_dim)]) dist = parse_expr(dist_string) f = rbf_variance*sp.exp(-dist/2.) else: rbf_lengthscale = sp.var('rbf_lengthscale',positive=True) dist_string = ' + '.join(['(x%i-z%i)**2' % (i, i) for i in range(input_dim)]) dist = parse_expr(dist_string) f = rbf_variance*sp.exp(-dist/(2*rbf_lengthscale**2)) return kern(input_dim, [spkern(input_dim, f)])
def sympykern(input_dim, k): """ A kernel from a symbolic sympy representation """ return kern(input_dim, [spkern(input_dim, k)])