def __init__(self, x, kernel=None): x = np.asarray(x) if x.ndim == 1: x = x[:, None] nobs, n_series = x.shape # print "%s dimensions" % n_series if kernel is None: kernel = kernels.Gaussian() # no meaningful bandwidth yet if n_series > 1: if isinstance(kernel, kernels.CustomKernel): kernel = kernels.NdKernel(n_series, kernels=kernel) self.kernel = kernel self.n = n_series # TODO change attribute self.x = x
def __init__(self, kernel=kernel.Gaussian(), params={}): """ Initialization of a new instance of KRLS. """ self.kernel = kernel # Instance of a kernel to use # initialize with some reasonable values self.adopt_thresh = 0.02 # approximate linear dependence threshold [0,1] self.dico_max_size = 15 # maximum size of the dictionary self.adaptive = True # flag if elimination is data-adaptive self.alpha = None self.p = [[1]] self.gamma = 0.01 # intrinsic regularization (ridge term coefficient) self.dico = None # the dictionary of relevant states self.target = None # the associated targets self.k = None # the kernel matrix self.kinv = None # the inverse kernel matrix self.forget_rate = 0.0 # how fast we want to forget what we did see self.dico_idx = [] # index when we added what # Allow overriding the attributes through the params dictionary for k in params: if hasattr(self, k): setattr(self, k, params[k])
if __name__ == "__main__": PLOT = True from numpy import random import matplotlib.pyplot as plt import bandwidths as bw # 1 D case random.seed(142) x = random.standard_t(4.2, size=50) h = bw.bw_silverman(x) #NOTE: try to do it with convolution support = np.linspace(-10, 10, 512) kern = kernels.Gaussian(h=h) kde = KDE(x, kern) print kde.density(1.015469) print 0.2034675 Xs = np.arange(-10, 10, 0.1) if PLOT: fig = plt.figure() ax = fig.add_subplot(111) ax.plot(Xs, kde(Xs), "-") ax.set_ylim(-10, 10) ax.set_ylim(0, 0.4) plt.show() # 2 D case # from statsmodels.sandbox.nonparametric.testdata import kdetest
def __init__(self, x, y, Kernel=None): if Kernel is None: Kernel = kernel.Gaussian() self.Kernel = Kernel self.x = np.array(x) self.y = np.array(y)