Package: densratio
A Python Package for Density Ratio EstimationKoji Makiyama (@hoxo-m), Ameya Daigavane (@ameya98), and Krzysztof
Mierzejewski (@mierzejk)1. OverviewDensity ratio estimationis described as follows: for given two data
samplesx1andx2from unknown distributionsp(x)andq(x)respectively, estimatew(x) = p(x) / q(x), wherex1andx2are
d-dimensional real numbers.The estimated density ratio functionw(x)can be used in many
applications such as the inlier-based outlier detection [1] and
covariate shift adaptation [2]. Other useful applications for density
ratio estimation were summarized by Sugiyama et al.ﾂ(2012) in [3].The packagedensratioprovides a functiondensratio()that returns
an object with a method to estimate density ratio ascompute_density_ratio().Further, the alpha-relative density ratiop(x)/(alpha * p(x) + (1 - alpha) * q(x))(where alpha is in the range
[0, 1]) can also be estimated. When alpha is 0, this reduces to the
ordinary density ratiow(x). The alpha-relative PE-divergence and
KL-divergence betweenp(x)andq(x)are also computed.For example,importnumpyasnpfromscipy.statsimportnormfromdensratioimportdensrationp.random.seed(1)x=norm.rvs(size=500,loc=0,scale=1./8)y=norm.rvs(size=500,loc=0,scale=1./2)alpha=0.1densratio_obj=densratio(x,y,alpha=alpha)print(densratio_obj)gives the following output:#> Method: RuLSIF
#> 
#> Alpha: 0.1
#> 
#> Kernel Information:
#>   Kernel type: Gaussian
#>   Number of kernels: 100
#>   Bandwidth(sigma): 0.1
#>   Centers: array([[-0.09591373],..
#> 
#> Kernel Weights (theta):
#>   array([0.04990797, 0.0550548 , 0.04784736, 0.04951904, 0.04840418,..
#> 
#> Regularization Parameter (lambda): 0.1
#> 
#> Alpha-Relative PE-Divergence: 0.618794133598705
#> 
#> Alpha-Relative KL-Divergence: 0.7037648129307483
#> 
#> Function to Estimate Density Ratio:
#>   compute_density_ratio(x)
#>In this case, the true density ratiow(x)is known, so we can comparew(x)with the estimated density ratiow-hat(x). The code below gives
the plot shown above.frommatplotlibimportpyplotaspltfromnumpyimportlinspacedeftrue_alpha_density_ratio(sample):returnnorm.pdf(sample,0,1./8)/(alpha*norm.pdf(sample,0,1./8)+(1-alpha)*norm.pdf(sample,0,1./2))defestimated_alpha_density_ratio(sample):returndensratio_obj.compute_density_ratio(sample)sample_points=np.linspace(-1,3,400)plt.plot(sample_points,true_alpha_density_ratio(sample_points),'b-',label='True Alpha-Relative Density Ratio')plt.plot(sample_points,estimated_alpha_density_ratio(sample_points),'r-',label='Estimated Alpha-Relative Density Ratio')plt.title("Alpha-Relative Density Ratio - Normal Random Variables (alpha={:03.2f})".format(alpha))plt.legend()plt.show()2. InstallationYou can install the package fromPyPI.$ pip install densratioAlso, you can install the package fromGitHub.$ pip install git+https://github.com/hoxo-m/densratio_py.gitThe source code fordensratiopackage is available on GitHub athttps://github.com/hoxo-m/densratio_py.3. Details3.1. BasicsThe package providesdensratio(). The function returns an object that
has a function to compute estimated density ratio.For data samplesxandy,fromscipy.statsimportnormfromdensratioimportdensratiox=norm.rvs(size=200,loc=1,scale=1./8)y=norm.rvs(size=200,loc=1,scale=1./2)result=densratio(x,y)In this case,result.compute_density_ratio()can compute estimated
density ratio.frommatplotlibimportpyplotaspltdensity_ratio=result.compute_density_ratio(y)plt.plot(y,density_ratio,"o")plt.xlabel("x")plt.ylabel("Density Ratio")plt.show()3.2. The MethodThe package estimates density ratio by the RuLSIF method.RuLSIF(Relative unconstrained Least-Squares Importance Fitting)
estimates the alpha-relative density ratio by minimizing the squared
loss between the true and estimated alpha-relative ratios. You can find
more information in Hido et al.ﾂ(2011) [1] and Liu et al (2013) [4].The method assumes that the alpha-relative density ratio is represented
by a linear kernel model:w(x) = theta1 * K(x, c1) + theta2 * K(x, c2) + ... + thetab * K(x, cb)whereK(x, c) = exp(- ||x - c||^2 / (2 * sigma ^ 2))is the Gaussian
RBF kernel.densratio()performs the following: - Decides kernel parametersigmaby cross-validation. - Optimizes for kernel weightstheta. - Computes
the alpha-relative PE-divergence and KL-divergence from the learned
alpha-relative ratio.As the result, you can obtaincompute_density_ratio(), which will
compute the alpha-relative density ratio at the passed coordinates.3.3. Result and Parameter Settingsdensratio()outputs the result like as follows:#> Method: RuLSIF
#> 
#> Alpha: 0
#> 
#> Kernel Information:
#>   Kernel type: Gaussian
#>   Number of kernels: 100
#>   Bandwidth(sigma): 0.1
#>   Centers: array([[0.92113356],..
#> 
#> Kernel Weights (theta):
#>   array([0.08848922, 0.03377533, 0.0753727 , 0.06141277, 0.02543963,..
#> 
#> Regularization Parameter (lambda): 1.0
#> 
#> Alpha-Relative PE-Divergence: 0.9635169300831041
#> 
#> Alpha-Relative KL-Divergence: 0.838826626547327
#> 
#> Function to Estimate Density Ratio:
#>   compute_density_ratio(x)
#>Methodis fixed as RuLSIF.Kernel typeis fixed as Gaussian RBF.Number of kernelsis the number of kernels in the linear model.
You can change by settingkernel_numparameter. In default,kernel_num = 100.Bandwidth(sigma)is the Gaussian kernel bandwidth. In default,sigma = "auto", the algorithm automatically select an optimal
value by cross validation. If you setsigmaa number, that will be
used. If you setsigmaa numeric array, the algorithm select an
optimal value in them by cross validation.Centersare centers of Gaussian kernels in the linear model.
These are selected at random from the data samplexunderlying a
numerator distributionp(x). You can find the whole values inresult.kernel_info.centers.Kernel weights(theta)are theta parameters in the linear kernel
model. You can find these values inresult.theta.The function to estimate the alpha-relative density ratiois
namedcompute_density_ratio().4. Multi Dimensional Data SamplesSo far, we have deal with one-dimensional data samplesxandy.densratio()allows to input multidimensional data samples asnumpy.ndarrayornumpy.matrix, as long as their dimensions are the
same.For example,fromscipy.statsimportmultivariate_normalfromdensratioimportdensrationp.random.seed(1)x=multivariate_normal.rvs(size=3000,mean=[1,1],cov=[[1./8,0],[0,1./8]])y=multivariate_normal.rvs(size=3000,mean=[1,1],cov=[[1./2,0],[0,1./2]])alpha=0densratio_obj=densratio(x,y,alpha=alpha,sigma_range=[0.1,0.3,0.5,0.7,1],lambda_range=[0.01,0.02,0.03,0.04,0.05])print(densratio_obj)gives the following output:#> Method: RuLSIF
#> 
#> Alpha: 0
#> 
#> Kernel Information:
#>   Kernel type: Gaussian
#>   Number of kernels: 100
#>   Bandwidth(sigma): 0.3
#>   Centers: array([[1.01477443, 1.38864061],..
#> 
#> Kernel Weights (theta):
#>   array([0.06151164, 0.08012094, 0.10467369, 0.13868176, 0.14917063,..
#> 
#> Regularization Parameter (lambda): 0.04
#> 
#> Alpha-Relative PE-Divergence: 0.653615870855595
#> 
#> Alpha-Relative KL-Divergence: 0.6214285743087565
#> 
#> Function to Estimate Density Ratio:
#>   compute_density_ratio(x)
#>In this case, as well, we can compare the true density ratio with the
estimated density ratio.frommatplotlibimportpyplotaspltfromnumpyimportlinspace,dstack,meshgrid,concatenatedeftrue_alpha_density_ratio(x):returnmultivariate_normal.pdf(x,[1.,1.],[[1./8,0],[0,1./8]])/\(alpha*multivariate_normal.pdf(x,[1.,1.],[[1./8,0],[0,1./8]])+(1-alpha)*multivariate_normal.pdf(x,[1.,1.],[[1./2,0],[0,1./2]]))defestimated_alpha_density_ratio(x):returndensratio_obj.compute_density_ratio(x)range_=np.linspace(0,2,200)grid=np.concatenate(np.dstack(np.meshgrid(range_,range_)))levels=[0,0.5,1,1.5,2,2.5,3,3.5,4.5]plt.figure(figsize=(10,4))plt.subplot(1,2,1)plt.contourf(range_,range_,true_alpha_density_ratio(grid).reshape(200,200),levels)#> <matplotlib.contour.QuadContourSet object at 0x0000022E950202E0>plt.colorbar()#> <matplotlib.colorbar.Colorbar object at 0x0000022E9500DA80>plt.title("True Alpha-Relative Density Ratio")plt.subplot(1,2,2)plt.contourf(range_,range_,estimated_alpha_density_ratio(grid).reshape(200,200),levels)#> <matplotlib.contour.QuadContourSet object at 0x0000022E942C8EE0>plt.colorbar()#> <matplotlib.colorbar.Colorbar object at 0x0000022E95095150>plt.title("Estimated Alpha-Relative Density Ratio")plt.show()5. References[1] Hido, S., Tsuboi, Y., Kashima, H., Sugiyama, M., & Kanamori, T.Statistical outlier detection using direct density ratio estimation.Knowledge and Information Systems 2011.[2] Sugiyama, M., Nakajima, S., Kashima, H., von Bﾃｼnau, P. & Kawanabe,
M.Direct importance estimation with model selection and its
application to covariate shift adaptation.NIPS 2007.[3] Sugiyama, M., Suzuki, T. & Kanamori, T.Density Ratio Estimation
in Machine Learning.Cambridge University Press 2012.[4] Liu, S., Yamada, M., Collier, N., & Sugiyama, M.Change-Point
Detection in Time-Series Data by Relative Density-Ratio EstimationNeural Networks, 2013.6. Related Workdensratio for Rhttps://github.com/hoxo-m/densratiopykliephttps://github.com/srome/pykliep