Last data update: 2014.03.03

R: Kernel Matrix functions
kernelMatrixR Documentation

Kernel Matrix functions

Description

kernelMatrix calculates the kernel matrix K_{ij} = k(x_i,x_j) or K_{ij} = k(x_i,y_j).
kernelPol computes the quadratic kernel expression H = z_i z_j k(x_i,x_j), H = z_i k_j k(x_i,y_j).
kernelMult calculates the kernel expansion f(x_i) = ∑_{i=1}^m z_i k(x_i,x_j)
kernelFast computes the kernel matrix, identical to kernelMatrix, except that it also requires the squared norm of the first argument as additional input, useful in iterative kernel matrix calculations.

Usage

## S4 method for signature 'kernel'
kernelMatrix(kernel, x, y = NULL)

## S4 method for signature 'kernel'
kernelPol(kernel, x, y = NULL, z, k = NULL)

## S4 method for signature 'kernel'
kernelMult(kernel, x, y = NULL, z, blocksize = 256)

## S4 method for signature 'kernel'
kernelFast(kernel, x, y, a)

Arguments

kernel

the kernel function to be used to calculate the kernel matrix. This has to be a function of class kernel, i.e. which can be generated either one of the build in kernel generating functions (e.g., rbfdot etc.) or a user defined function of class kernel taking two vector arguments and returning a scalar.

x

a data matrix to be used to calculate the kernel matrix.

y

second data matrix to calculate the kernel matrix.

z

a suitable vector or matrix

k

a suitable vector or matrix

a

the squared norm of x, e.g., rowSums(x^2)

blocksize

the kernel expansion computations are done block wise to avoid storing the kernel matrix into memory. blocksize defines the size of the computational blocks.

Details

Common functions used during kernel based computations.
The kernel parameter can be set to any function, of class kernel, which computes the inner product in feature space between two vector arguments. KERE provides the most popular kernel functions which can be initialized by using the following functions:

  • rbfdot Radial Basis kernel function

  • polydot Polynomial kernel function

  • vanilladot Linear kernel function

  • tanhdot Hyperbolic tangent kernel function

  • laplacedot Laplacian kernel function

  • besseldot Bessel kernel function

  • anovadot ANOVA RBF kernel function

  • splinedot the Spline kernel

(see example.)

kernelFast is mainly used in situations where columns of the kernel matrix are computed per invocation. In these cases, evaluating the norm of each row-entry over and over again would cause significant computational overhead.

Value

kernelMatrix returns a symmetric diagonal semi-definite matrix.
kernelPol returns a matrix.
kernelMult usually returns a one-column matrix.

Author(s)

Alexandros Karatzoglou
alexandros.karatzoglou@ci.tuwien.ac.at

See Also

rbfdot, polydot, tanhdot, vanilladot

Examples

## use the spam data
x <- matrix(rnorm(10*10),10,10)

## initialize kernel function 
rbf <- rbfdot(sigma = 0.05)
rbf

## calculate kernel matrix
kernelMatrix(rbf, x)

y <- matrix(rnorm(10*1),10,1)


## calculate the quadratic kernel expression
kernelPol(rbf, x, ,y)

## calculate the kernel expansion
kernelMult(rbf, x, ,y)

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(KERE)
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/KERE/kernelMatrix.Rd_%03d_medium.png", width=480, height=480)
> ### Name: kernelMatrix
> ### Title: Kernel Matrix functions
> ### Aliases: kernelMatrix kernelMult kernelPol kernelFast
> ###   kernelPol,kernel-method kernelMatrix,kernel-method
> ###   kernelMult,kernel-method kernelFast,kernel-method
> ###   kernelMatrix,rbfkernel-method kernelMatrix,polykernel-method
> ###   kernelMatrix,vanillakernel-method kernelMatrix,tanhkernel-method
> ###   kernelMatrix,laplacekernel-method kernelMatrix,anovakernel-method
> ###   kernelMatrix,splinekernel-method kernelMatrix,besselkernel-method
> ###   kernelMult,rbfkernel,ANY-method kernelMult,splinekernel,ANY-method
> ###   kernelMult,polykernel,ANY-method kernelMult,tanhkernel,ANY-method
> ###   kernelMult,laplacekernel,ANY-method
> ###   kernelMult,besselkernel,ANY-method kernelMult,anovakernel,ANY-method
> ###   kernelMult,vanillakernel,ANY-method
> ###   kernelMult,character,kernelMatrix-method kernelPol,rbfkernel-method
> ###   kernelPol,splinekernel-method kernelPol,polykernel-method
> ###   kernelPol,tanhkernel-method kernelPol,vanillakernel-method
> ###   kernelPol,anovakernel-method kernelPol,besselkernel-method
> ###   kernelPol,laplacekernel-method kernelFast,rbfkernel-method
> ###   kernelFast,splinekernel-method kernelFast,polykernel-method
> ###   kernelFast,tanhkernel-method kernelFast,vanillakernel-method
> ###   kernelFast,anovakernel-method kernelFast,besselkernel-method
> ###   kernelFast,laplacekernel-method kernelFast,splinekernel-method
> ### Keywords: algebra array
> 
> ### ** Examples
> 
> ## use the spam data
> x <- matrix(rnorm(10*10),10,10)
> 
> ## initialize kernel function 
> rbf <- rbfdot(sigma = 0.05)
> rbf
Gaussian Radial Basis kernel function. 
 Hyperparameter : sigma =  0.05 
> 
> ## calculate kernel matrix
> kernelMatrix(rbf, x)
An object of class "kernelMatrix"
            [,1]      [,2]      [,3]       [,4]      [,5]      [,6]      [,7]
 [1,] 1.00000000 0.2450902 0.5336876 0.06339618 0.4273521 0.4231652 0.2284997
 [2,] 0.24509021 1.0000000 0.5961881 0.51881357 0.6311762 0.6207197 0.3201811
 [3,] 0.53368760 0.5961881 1.0000000 0.19028809 0.6227229 0.6925982 0.5494947
 [4,] 0.06339618 0.5188136 0.1902881 1.00000000 0.4160948 0.3146792 0.2022280
 [5,] 0.42735207 0.6311762 0.6227229 0.41609484 1.0000000 0.5472924 0.2797277
 [6,] 0.42316524 0.6207197 0.6925982 0.31467920 0.5472924 1.0000000 0.4930907
 [7,] 0.22849972 0.3201811 0.5494947 0.20222803 0.2797277 0.4930907 1.0000000
 [8,] 0.38930078 0.6247381 0.7085214 0.25495133 0.6659879 0.5732332 0.3665913
 [9,] 0.39022485 0.4553018 0.5461838 0.24847897 0.6193246 0.6680882 0.2815247
[10,] 0.22094660 0.4697757 0.3062454 0.27564374 0.2806980 0.5239379 0.3117201
           [,8]      [,9]     [,10]
 [1,] 0.3893008 0.3902248 0.2209466
 [2,] 0.6247381 0.4553018 0.4697757
 [3,] 0.7085214 0.5461838 0.3062454
 [4,] 0.2549513 0.2484790 0.2756437
 [5,] 0.6659879 0.6193246 0.2806980
 [6,] 0.5732332 0.6680882 0.5239379
 [7,] 0.3665913 0.2815247 0.3117201
 [8,] 1.0000000 0.5662027 0.3108274
 [9,] 0.5662027 1.0000000 0.2173987
[10,] 0.3108274 0.2173987 1.0000000
> 
> y <- matrix(rnorm(10*1),10,1)
> 
> 
> ## calculate the quadratic kernel expression
> kernelPol(rbf, x, ,y)
              [,1]         [,2]          [,3]          [,4]         [,5]
 [1,]  0.089409078 -0.020822735  0.0028627522  0.0042537778 -0.094399310
 [2,] -0.020822735  0.080731311 -0.0030388562 -0.0330790659  0.132484116
 [3,]  0.002862752 -0.003038856  0.0003218190  0.0007660169 -0.008252639
 [4,]  0.004253778 -0.033079066  0.0007660169  0.0503549099 -0.068977182
 [5,] -0.094399310  0.132484116 -0.0082526393 -0.0689771817  0.545738262
 [6,] -0.013270200  0.018496656 -0.0013030584 -0.0074056916  0.042402192
 [7,] -0.040380483  0.053766497 -0.0058259176 -0.0268199081  0.122130093
 [8,] -0.037017089  0.056447561 -0.0040418946 -0.0181930018  0.156453292
 [9,]  0.010361192 -0.011487470  0.0008700588  0.0049512461 -0.040626975
[10,] -0.037083503  0.074922823 -0.0030837421 -0.0347193564  0.116394968
              [,6]         [,7]         [,8]          [,9]        [,10]
 [1,] -0.013270200 -0.040380483 -0.037017089  0.0103611915 -0.037083503
 [2,]  0.018496656  0.053766497  0.056447561 -0.0114874697  0.074922823
 [3,] -0.001303058 -0.005825918 -0.004041895  0.0008700588 -0.003083742
 [4,] -0.007405692 -0.026819908 -0.018193002  0.0049512461 -0.034719356
 [5,]  0.042402192  0.122130093  0.156453292 -0.0406269752  0.116394968
 [6,]  0.010999004  0.030563180  0.019117635 -0.0062217771  0.030843179
 [7,]  0.030563180  0.349293126  0.068897478 -0.0147745919  0.103409985
 [8,]  0.019117635  0.068897478  0.101123559 -0.0159882927  0.055481490
 [9,] -0.006221777 -0.014774592 -0.015988293  0.0078851134 -0.010835864
[10,]  0.030843179  0.103409985  0.055481490 -0.0108358636  0.315068690
> 
> ## calculate the kernel expansion
> kernelMult(rbf, x, ,y)
            [,1]
 [1,] -0.4551183
 [2,] -1.2262616
 [3,] -1.1553096
 [4,] -0.5742818
 [5,] -1.2228194
 [6,] -1.1844555
 [7,] -1.0833305
 [8,] -1.2021433
 [9,] -0.8543799
[10,] -1.0874541
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>