MKERNEL2 Multivariate Kernel Function, alternative version. CALL: z = mkernel2(X,kernel); z = kernel function values evaluated at X X = matrix size N x D (D = # dimensions) kernel = 'epanechnikov' - Epanechnikov kernel. 'epa1' - product of 1D Epanechnikov kernel. 'biweight' - Bi-weight kernel. 'biw1' - product of 1D Bi-weight kernel. 'triweight' - Tri-weight kernel. 'triangular' - Triangular kernel. 'gaussian' - Gaussian kernel 'rectangular' - Rectangular kernel. 'laplace' - Laplace kernel. 'logistic' - Logistic kernel. Note that only the first 4 letters of the kernel name is needed. See also kde, kdefun, kdebin
calculates volume of d-dimensional sphere with radius r0 | |
Display message and abort function. | |
Convert string to lowercase. |
001 function [z,c]=mkernel(X,kstr) 002 %MKERNEL2 Multivariate Kernel Function, alternative version. 003 % 004 % CALL: z = mkernel2(X,kernel); 005 % 006 % z = kernel function values evaluated at X 007 % X = matrix size N x D (D = # dimensions) 008 % 009 % kernel = 'epanechnikov' - Epanechnikov kernel. 010 % 'epa1' - product of 1D Epanechnikov kernel. 011 % 'biweight' - Bi-weight kernel. 012 % 'biw1' - product of 1D Bi-weight kernel. 013 % 'triweight' - Tri-weight kernel. 014 % 'triangular' - Triangular kernel. 015 % 'gaussian' - Gaussian kernel 016 % 'rectangular' - Rectangular kernel. 017 % 'laplace' - Laplace kernel. 018 % 'logistic' - Logistic kernel. 019 % 020 % Note that only the first 4 letters of the kernel name is needed. 021 % 022 % See also kde, kdefun, kdebin 023 024 % Reference: 025 % B. W. Silverman (1986) 026 % 'Density estimation for statistics and data analysis' 027 % Chapman and Hall, pp. 43, 76 028 % 029 % Wand, M. P. and Jones, M. C. (1995) 030 % 'Density estimation for statistics and data analysis' 031 % Chapman and Hall, pp 31, 103, 175 032 033 %Tested on: matlab 5.3 034 % History: 035 % Revised pab Dec2003 036 % removed some code 037 % revised pab 27.04.2001 038 % - renamed from mkernel to mkernel2 039 % - removed some old calls 040 % - improved speed for all kernels 041 % revised pab 01.01.2001 042 % - speeded up tri3 043 % revised pab 01.12.1999 044 % - added four weight, sphere 045 % - made comparison smarter => faster execution for d>1 046 % revised pab 26.10.1999 047 % fixed normalization fault in epan 048 % by pab 21.09.99 049 % added multivariate epan, biweight and triweight 050 % 051 % collected all knorm,kepan ... into this file 052 % adapted from kdetools CB 053 error(nargchk(2,2,nargin)) 054 [n,d]=size(X); 055 % n=number of evaluation points, 056 % d=dimension of the data. 057 z = zeros(n,1); 058 switch lower(kstr(1:4)) 059 case {'sphe','epan','biwe','triw','four'} 060 switch lower(kstr(1:4)) 061 case 'sphe', r=0; %Sphere = rect for 1D 062 case 'epan', r=1; %Multivariate Epanechnikov kernel. 063 case 'biwe', r=2; %Multivariate Bi-weight Kernel 064 case 'triw', r=3; %Multi variate Tri-weight Kernel 065 case 'four', r=4; %Multi variate Four-weight Kernel 066 % as r -> infty, b -> infty => kernel -> Gaussian distribution 067 end 068 b = 1; % radius of the kernel 069 b2 = b^2; % radius squared 070 s = sum(X.^2,2); 071 k = find(s<=b2); 072 073 if any(k) 074 c=2^r*prod(1:r)*vsph(d,b)/prod((d+2):2:(d+2*r)); % normalizing constant 075 %c=beta(r+1,r+1)*vsph(d,b)*(2^(2*r)); % Wand and Jones pp 31 076 % the commented c above does note yield the right scaling 077 % for d>1 078 z(k)=((1-s(k)/b2).^r)/c; 079 end 080 081 case 'rect', % 1D product Rectangular Kernel 082 k=find(all(abs(X)<=1,2)); 083 if any(k) 084 z(k)=(0.5^d); 085 end 086 case {'epa1','biw1','triw1','fou1'} 087 switch lower(kstr(1:4)) 088 %case 'rect', r=0; %rectangular 089 case 'epa1', r=1; %1D product Epanechnikov kernel. 090 case 'biw1', r=2; %1D product Bi-weight Kernel 091 case 'tri1', r=3; %1D product Tri-weight Kernel 092 case 'fou1', r=4; %1D product Four-weight Kernel 093 end 094 b = 1; 095 b2 = b^2; 096 b21 = 1/b2; 097 k = find(all(abs(X)<=b,2)); 098 if any(k) 099 c=2^r*prod(1:r)*vsph(1,b)/prod((1+2):2:(1+2*r)); % normalizing constant 100 z(k) = prod(1-X(k,:).^2*b21,2)/c^d; 101 end 102 case 'tria',% 1D product Triangular Kernel 103 k = find(all(abs(X)<=1,2)); 104 if any(k) 105 z(k) = prod(1-abs(X(k,:)),2); 106 end 107 case {'norm','gaus'},% multivariate gaussian Density Function. 108 z = (2*pi)^(-d/2)*exp(-0.5*sum(X.^2,2)); 109 case 'lapl' % Laplace Kernel 110 z=0.5^d*exp(-sum(abs(X),2)); 111 case 'logi', % Logistic Kernel 112 s = exp(X); 113 z = prod(s./(s+1).^2,2); 114 otherwise, error('unknown kernel') 115 end 116 117 return 118 119 120 121 122 123 124 125 126
Comments or corrections to the WAFO group