# densratio: New R Package for Density Ratio Estimation

## 1. Overview

Density ratio estimationis 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 <- rnorm(200, mean = 1, sd = 1/8)y <- rnorm(200, mean = 1, sd = 1/2)library(densratio)result <- densratio(x, y)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 <- function(x) dnorm(x, 1, 1/8) / dnorm(x, 1, 1/2)estimated_density_ratio <- result\$compute_density_ratioplot(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)``