By hoxo_m
1. Overview
Density ratio estimation is described as follows: for given two data samples $x$ and $y$ from unknown distributions $p(x)$ and $q(y)$ respectively, estimate $$ w(x) = frac{p(x)}{q(x)} $$ where $x$ and $y$ are $d$-dimensional real numbers.
The estimated density ratio function $w(x)$ can be used in many applications such as the inlier-based outlier detection [1] and covariate shift adaptation [2]. Other useful applications about density ratio estimation were summarized by Sugiyama et al. (2012) [3].
The package densratio provides a function densratio()
that returns a result has the function to estimate density ratio compute_density_ratio()
.
For example,
set.seed(3)
x y library(densratio)
result result
##
## Call:
## densratio(x = x, y = y, method = "uLSIF")
##
## Kernel Information:
## Kernel type: Gaussian RBF
## Number of kernels: 100
## Bandwidth(sigma): 0.1
## Centers: num [1:100, 1] 1.007 0.752 0.917 0.824 0.7 ...
##
## Kernel Weights(alpha):
## num [1:100] 0.4044 0.0479 0.1736 0.125 0.0597 ...
##
## The Function to Estimate Density Ratio:
## compute_density_ratio()
In this case, the true density ratio $w(x)$ is known, so we can compare $w(x)$ with the estimated density ratio $hat{w}(x)$.
true_density_ratio estimated_density_ratio plot(true_density_ratio, xlim=c(-1, 3), lwd=2, col="red", xlab = "x", ylab = "Density Ratio")
plot(estimated_density_ratio, xlim=c(-1, 3), lwd=2, col="green", add=TRUE)
legend("topright", legend=c(expression(w(x)), expression(hat(w)(x))), col=2:3, lty=1, lwd=2, pch=NA)
2. How to Install
You can install the densratio package from CRAN.
install.packages("densratio")
You can also install the package from GitHub.
install.packages("devtools") # if you have not installed "devtools" package
devtools::install_github("hoxo-m/densratio")
The source code for densratio package is available on GitHub at
3. Details
3.1. Basics
The package provides densratio()
that the result has the function to estimate density ratio.
For data samples x
and y
,
library(densratio)result
In this case, result$compute_density_ratio()
can compute estimated density ratio.