def make_matern(nu): '''returns an instance of a Matern RBF with order *nu*''' tol = 1e-10 r, eps = get_r(), get_eps() # get symbolic variables coeff = 2**(1 - nu) / gamma(nu) * (np.sqrt(2 * nu) * r / eps)**nu expr = coeff * besselk(nu, np.sqrt(2 * nu) * r / eps) # Handle a removable singularity at r=0 by setting it to 1. expr = Piecewise((1.0, r < tol), (expr, True)) return RBF(expr)
This script demonstrates how to create a periodic Gaussian process using the *gpiso* function. ''' import numpy as np import matplotlib.pyplot as plt from sympy import sin, exp, pi from rbf.basis import get_r, get_eps, RBF from rbf.gproc import gpiso np.random.seed(1) period = 5.0 cls = 0.5 # characteristic length scale var = 1.0 # variance r = get_r() # get symbolic variables eps = get_eps() # create a symbolic expression of the periodic covariance function expr = exp(-sin(r * pi / period)**2 / eps**2) # define a periodic RBF using the symbolic expression basis = RBF(expr) # define a Gaussian process using the periodic RBF gp = gpiso(basis, eps=cls, var=var) t = np.linspace(-10, 10, 1000)[:, None] sample = gp.sample(t) # draw a sample mu, sigma = gp(t) # evaluate mean and std. dev. # plot the results fig, ax = plt.subplots(figsize=(6, 4)) ax.grid(True) ax.plot(t[:, 0], mu, 'b-', label='mean') ax.fill_between(t[:, 0],
from rbf.gauss import gpiso from scipy.signal import periodogram def make_matern(nu): '''returns an instance of a Matern RBF with order *nu*''' tol = 1e-10 r, eps = get_r(), get_eps() # get symbolic variables coeff = 2**(1 - nu) / gamma(nu) * (np.sqrt(2 * nu) * r / eps)**nu expr = coeff * besselk(nu, np.sqrt(2 * nu) * r / eps) # Handle a removable singularity at r=0 by setting it to 1. expr = Piecewise((1.0, r < tol), (expr, True)) return RBF(expr) r, eps = get_r(), get_eps() se = RBF(exp(-r**2 / (2 * eps**2))) ou = RBF(exp(-r / eps)) x = np.linspace(0.0, 3.0, 500)[:, None] center = np.array([[0.0]]) fig, ax = plt.subplots() for nu in [0.6, 2.0, 30.0]: mat = make_matern(nu) ax.plot(x, mat(x, center), label='$\\nu = $%s' % str(nu)) ax.plot(x, se(x, center), label='se') ax.plot(x, ou(x, center), label='ou') ax.set_xlim((0.0, 3.0))
''' In this script we define and plot an RBF which is based on the sinc function ''' import numpy as np import sympy import matplotlib.pyplot as plt from rbf.basis import RBF, get_r, get_eps r, eps = get_r(), get_eps() # get symbolic variables expr = sympy.sin(eps * r) / (eps * r) # symbolic expression for the RBF sinc_rbf = RBF(expr) # instantiate the RBF x = np.linspace(-5, 5, 500) points = np.reshape(np.meshgrid(x, x), (2, 500 * 500)).T # interp points centers = np.array([[0.0, -3.0], [3.0, 2.0], [-2.0, 1.0]]) # RBF centers eps = np.array([5.0, 5.0, 5.0]) # shape parameters values = sinc_rbf(points, centers, eps=eps) # Evaluate the RBFs # plot the sum of each RBF fig, ax = plt.subplots() p = ax.tripcolor(points[:, 0], points[:, 1], np.sum(values, axis=1), shading='gouraud', cmap='viridis') plt.colorbar(p, ax=ax) ax.set_xlim((-5, 5)) ax.set_ylim((-5, 5)) plt.tight_layout() plt.savefig('../figures/basis.b.png') plt.show()