Last data update: 2014.03.03

R: Gaussian Kernel Distance Computation
gausskernelR Documentation

Gaussian Kernel Distance Computation

Description

Given a N by D numeric data matrix, this function computes the N by N distance matrix with the pairwise distances between the rows of the data matrix as measured by a Gaussian Kernel.

Usage

gausskernel(X = NULL, sigma = NULL)

Arguments

X

N by N numeric data matrix.

sigma

Positive scalar that specifies the bandwidth of the Gaussian kernel (see details).

Details

Given two D dimensional vectors x_i and x_j. The Gaussian kernel is defined as

k(x_i,x_j)=exp(-|| x_i - x_j ||^2 / sigma^2)

where ||x_i - x_j|| is the Euclidean distance given by

||x_i - x_j||=((x_i1-x_j1)^2 + (x_i2-x_j2)^2 + ... + (x_iD-x_jD)^2)^.5

and sigma^2 is the bandwidth of the kernel.

Note that the Gaussian kernel is a measure of similarity between x_i and x_j. It evalues to 1 if the x_i and x_j are identical, and approaches 0 as x_i and x_j move further apart.

The function relies on the dist function in the stats package for an initial estimate of the euclidean distance.

Value

An N by N numeric distance matrix that contains the pairwise distances between the rows in X.

Author(s)

Jens Hainmueller (Stanford) and Chad Hazlett (MIT)

See Also

dist function in the stats package.

Examples

X <- matrix(rnorm(6),ncol=2)
gausskernel(X=X,sigma=1)

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(KRLS)
## KRLS Package for Kernel-based Regularized Least Squares.

## See Hainmueller and Hazlett (2014) for details.

> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/KRLS/gausskernel.Rd_%03d_medium.png", width=480, height=480)
> ### Name: gausskernel
> ### Title: Gaussian Kernel Distance Computation
> ### Aliases: gausskernel
> 
> ### ** Examples
> 
> X <- matrix(rnorm(6),ncol=2)
> gausskernel(X=X,sigma=1)
           1         2          3
1 1.00000000 0.4612798 0.08013023
2 0.46127984 1.0000000 0.38238943
3 0.08013023 0.3823894 1.00000000
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>