{"id":32208565,"url":"https://github.com/hoxo-m/densratio","last_synced_at":"2026-02-19T09:13:46.718Z","repository":{"id":56936840,"uuid":"53514916","full_name":"hoxo-m/densratio","owner":"hoxo-m","description":"R Package for Density Ratio Estimation","archived":false,"fork":false,"pushed_at":"2023-10-30T02:04:44.000Z","size":731,"stargazers_count":23,"open_issues_count":7,"forks_count":8,"subscribers_count":2,"default_branch":"master","last_synced_at":"2026-01-29T21:26:54.665Z","etag":null,"topics":["anomalydetection","machine-learning","machine-learning-algorithms","machine-learning-library","r-language","r-package","statistics"],"latest_commit_sha":null,"homepage":"https://github.com/hoxo-m/densratio","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hoxo-m.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2016-03-09T16:53:41.000Z","updated_at":"2026-01-18T10:42:47.000Z","dependencies_parsed_at":"2022-08-21T01:10:13.964Z","dependency_job_id":"147a0cbe-ee4d-4294-9019-c614a2a5553f","html_url":"https://github.com/hoxo-m/densratio","commit_stats":{"total_commits":55,"total_committers":1,"mean_commits":55.0,"dds":0.0,"last_synced_commit":"fe2c10293f08daeaaebb94125f2bf20f0c433aa0"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/hoxo-m/densratio","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hoxo-m%2Fdensratio","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hoxo-m%2Fdensratio/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hoxo-m%2Fdensratio/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hoxo-m%2Fdensratio/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hoxo-m","download_url":"https://codeload.github.com/hoxo-m/densratio/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hoxo-m%2Fdensratio/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29609524,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-19T06:47:36.664Z","status":"ssl_error","status_checked_at":"2026-02-19T06:45:47.551Z","response_time":117,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["anomalydetection","machine-learning","machine-learning-algorithms","machine-learning-library","r-language","r-package","statistics"],"created_at":"2025-10-22T06:01:11.571Z","updated_at":"2026-02-19T09:13:46.713Z","avatar_url":"https://github.com/hoxo-m.png","language":"R","funding_links":[],"categories":[],"sub_categories":[],"readme":"---\noutput: \n  md_document:\n    variant: gfm\n  html_document:\n    keep_md: true\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n```{r setup, include=FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n  fig.path = \"man/figures/README-\",\n  message = FALSE\n)\nlibrary(mvtnorm)\n```\n\n# An R Package for Density Ratio Estimation\n\n#### *Koji MAKIYAMA (@hoxo-m)*\n\n\u003c!-- badges: start --\u003e\n[![Travis-CI Build Status](https://travis-ci.org/hoxo-m/densratio.svg?branch=master)](https://travis-ci.org/hoxo-m/densratio)\n[![CRAN Version](https://www.r-pkg.org/badges/version/densratio)](https://CRAN.R-project.org/package=densratio)\n[![CRAN Downloads](https://cranlogs.r-pkg.org/badges/densratio)](https://CRAN.R-project.org/package=densratio)\n[![Coverage Status](https://coveralls.io/repos/github/hoxo-m/densratio/badge.svg?branch=master)](https://coveralls.io/github/hoxo-m/densratio?branch=master)\n[![Say Thanks!](https://img.shields.io/badge/Say%20Thanks-!-1EAEDB.svg)](https://saythanks.io/to/hoxo-m)\n\u003c!-- badges: end --\u003e\n\n## 1. Overview\n\n**Density ratio estimation** is described as follows: for given two data samples `x1` and `x2` from unknown distributions `p(x)` and `q(x)` respectively, estimate `w(x) = p(x) / q(x)`, where `x1` and `x2` are d-dimensional real numbers.\n\nThe estimated density ratio function `w(x)` can be used in many applications such as **anomaly detection** [Hido et al. 2011], **change-point detection** [Liu et al. 2013], and **covariate shift adaptation** [Sugiyama et al. 2007].\nOther useful applications about density ratio estimation were summarized by [Sugiyama et al. 2012].\n\nThe package **densratio** provides a function `densratio()` that returns an object with a method to estimate density ratio as `compute_density_ratio()`.\n\nFor example, \n\n```{r result, cache=TRUE}\nset.seed(3)\nx1 \u003c- rnorm(200, mean = 1, sd = 1/8)\nx2 \u003c- rnorm(200, mean = 1, sd = 1/2)\n\nlibrary(densratio)\ndensratio_obj \u003c- densratio(x1, x2)\n```\n\nThe densratio object has a function `compute_density_ratio()` that can compute the estimated density ratio `w_hat(x)` for any d-dimensional input `x` (now d=1).\n\n```{r compute-estimated-density-ratio, fig.width=5, fig.height=4}\nnew_x \u003c- seq(0, 2, by = 0.03)\nw_hat \u003c- densratio_obj$compute_density_ratio(new_x)\n\nplot(new_x, w_hat, pch=19)\n```\n\nIn this case, the true density ratio `w(x) = p(x)/q(y) = Norm(1, 1/8) / Norm(1, 1/2)` is known. \nSo we can compare `w(x)` with the estimated density ratio `w-hat(x)`.\n\n```{r compare-true-estimate, fig.width=5, fig.height=4}\ntrue_density_ratio \u003c- function(x) dnorm(x, 1, 1/8) / dnorm(x, 1, 1/2)\n\nplot(true_density_ratio, xlim=c(0, 2), lwd=2, col=\"red\", xlab = \"x\", ylab = \"Density Ratio\")\nplot(densratio_obj$compute_density_ratio, xlim=c(0, 2), lwd=2, col=\"green\", add=TRUE)\nlegend(\"topright\", legend=c(expression(w(x)), expression(hat(w)(x))), col=2:3, lty=1, lwd=2, pch=NA)\n```\n\n## 2. Installation\n\nYou can install the **densratio** package from [CRAN](https://CRAN.R-project.org/package=densratio).\n\n```{r eval=FALSE}\ninstall.packages(\"densratio\")\n```\n\nYou can also install the package from [GitHub](https://github.com/hoxo-m/densratio).\n\n```{r eval=FALSE}\ninstall.packages(\"remotes\") # if you have not installed \"remotes\" package\nremotes::install_github(\"hoxo-m/densratio\")\n```\n\nThe source code for **densratio** package is available on GitHub at\n\n- https://github.com/hoxo-m/densratio.\n\n## 3. Details\n\n### 3.1 Basics\n\nThe package provides `densratio()`.\nThe function returns an object that has a function to compute estimated density ratio.\n\nFor data samples `x1` and `x2`,\n\n```{r eval=FALSE}\nset.seed(3)\nx1 \u003c- rnorm(200, mean = 1, sd = 1/8)\nx2 \u003c- rnorm(200, mean = 1, sd = 1/2)\n\nlibrary(densratio)\ndensratio_obj \u003c- densratio(x1, x2)\n```\n\nIn this case, `densratio_obj$compute_density_ratio()` can compute estimated density ratio.\n\n```{r basics-compute-estimated-density-ratio, fig.width=5, fig.height=4}\nnew_x \u003c- seq(0, 2, by = 0.03)\nw_hat \u003c- densratio_obj$compute_density_ratio(new_x)\n\nplot(new_x, w_hat, pch=19)\n```\n\n### 3.2 Methods\n\n`densratio()` has `method` argument that you can pass `\"uLSIF\"`, `\"RuSLIF\"`, or `\"KLIEP\"`.\n\n- **uLSIF** (unconstrained Least-Squares Importance Fitting) is the default method.\nThis algorithm estimates density ratio by minimizing the squared loss.\nYou can find more information in [Kanamori et al. 2009] and [Hido et al. 2011].\n- **RuLSIF** (Relative unconstrained Least-Squares Importance Fitting).\nThis algorithm estimates relative density ratio by minimizing the squared loss.\nYou can find more information in [Yamada et al. 2011] and [Liu et al. 2013].\n- **KLIEP** (Kullback-Leibler Importance Estimation Procedure).\nThis algorithm estimates density ratio by minimizing Kullback-Leibler divergence.\nYou can find more information in [Sugiyama et al. 2007].\n\nThe methods assume that density ratio are represented by linear model:\n\n- `w(x) = theta_1 * K(x, c_1) + theta_2 * K(x, c_2) + ... + theta_b * K(x, c_b)`\n\nwhere\n\n- `K(x, c) = exp(-||x - c||^2 / 2 * sigma^2)`\n\nis the Gaussian (RBF) kernel.\n\n`densratio()` performs the following: \n\n- Decides kernel parameter `sigma` by cross-validation, \n- Optimizes the kernel weights `theta` (in other words, find the optimal coefficients of the linear model), and\n- The parameters `sigma` and `theta` are saved into `densratio` object, and are used when to compute density ratio in the call `compute_density_ratio()`.\n\n### 3.3 Result and Arguments\n\n`densratio()` outputs the result like as follows:\n\n```{r echo=FALSE}\ndensratio_obj\n```\n\n- **Kernel type** is fixed as Gaussian.\n- **Number of kernels** is the number of kernels in the linear model. \nYou can change by setting `kernel_num` argument. \nIn default, `kernel_num = 100`.\n- **Bandwidth (sigma)** is the Gaussian kernel bandwidth.\nIn default, `sigma = \"auto\"`, the algorithm automatically select an optimal value by cross validation. \nIf you set `sigma` a number, that will be used. \nIf you set `sigma` a numeric vector, the algorithm select an optimal value in them by cross validation.\n- **Centers** are centers of Gaussian kernels in the linear model. \nThese are selected at random from the data sample `x1` underlying a numerator distribution `p(x)`. \nYou can find the whole values in `result$kernel_info$centers`.\n- **Kernel Weights** are `theta` parameters in the linear kernel model. \nYou can find these values in `result$kernel_weights`.\n- **Function to Estimate Density Ratio** is named `compute_density_ratio()`.\n\n## 4. Multi Dimensional Data Samples\n\nSo far, the input data samples `x1` and `x2` were one dimensional. \n`densratio()` allows to input multidimensional data samples as `matrix`, as long as their dimensions are the same.\n\nFor example,\n\n```{r cache=TRUE}\nlibrary(densratio)\nlibrary(mvtnorm)\n\nset.seed(3)\nx1 \u003c- rmvnorm(300, mean = c(1, 1), sigma = diag(1/8, 2))\nx2 \u003c- rmvnorm(300, mean = c(1, 1), sigma = diag(1/2, 2))\n\ndensratio_obj_2d \u003c- densratio(x1, x2)\ndensratio_obj_2d\n```\n\nIn this case, as well, we can compare the true density ratio with the estimated density ratio.\n\n```{r compare-2d, fig.width=7, fig.height=4}\ntrue_density_ratio \u003c- function(x) {\n  dmvnorm(x, mean = c(1, 1), sigma = diag(1/8, 2)) /\n    dmvnorm(x, mean = c(1, 1), sigma = diag(1/2, 2))\n}\n\nN \u003c- 20\nrange \u003c- seq(0, 2, length.out = N)\ninput \u003c- expand.grid(range, range)\nw_true \u003c- matrix(true_density_ratio(input), nrow = N)\nw_hat \u003c- matrix(densratio_obj_2d$compute_density_ratio(input), nrow = N)\n\npar(mfrow = c(1, 2))\ncontour(range, range, w_true, main = \"True Density Ratio\")\ncontour(range, range, w_hat, main = \"Estimated Density Ratio\")\n```\n\n## 5. Related work\n\n- A Python Package for Density Ratio Estimation\n    - https://pypi.org/project/densratio/\n- APPEstimation: Adjusted Prediction Model Performance Estimation\n    - https://cran.r-project.org/package=APPEstimation\n\n## References\n\n- Hido, S., Y. Tsuboi, H. Kashima, M. Sugiyama, and T. Kanamori.\n**Statistical outlier detection using direct density ratio estimation.**\nKnowledge and Information Systems, 2011.\n- Kanamori, T., S. Hido, and M. Sugiyama.\n**A least-squares approach to direct importance estimation.**\nJournal of Machine Learning Research, 2009.\n- Liu, S., M. Yamada, N. Collier, M. Sugiyama.\n**Change-point detection in time-series data by relative density-ratio estimation.**\nNeural Net, 2013\n- Sugiyama, M., S. Nakajima, H. Kashima, P. von Bünau, and M. Kawanabe. \n**Direct importance estimation with model selection and its application to covariate shift adaptation.**\nNIPS 2007.\n- Sugiyama, M., T. Suzuki, and T. Kanamori. \n**Density ratio estimation in machine learning.**\nCambridge University Press, 2012.\n- Yamada, M., T. Suzuki, T. Kanamori, H. Hachiya, and M. Sugiyama.\n**Relative density-ratio estimation for robust distribution comparison.**\nNIPS 2011.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhoxo-m%2Fdensratio","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhoxo-m%2Fdensratio","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhoxo-m%2Fdensratio/lists"}