Skip to content

Commit

Permalink
Modify Euclidean distance to square Euclidean distance in the functio…
Browse files Browse the repository at this point in the history
…n that computes Gaussian kernel (#6).
  • Loading branch information
hoxo-m committed May 8, 2019
1 parent 3b877d5 commit d82aae1
Show file tree
Hide file tree
Showing 17 changed files with 46 additions and 369 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
^Meta$
^doc$
^.*\.Rproj$
^\.Rproj\.user$
^README.html$
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Meta
doc
.Rproj.user
.Rhistory
.RData
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: densratio
Type: Package
Version: 0.0.3.9002
Version: 0.1.0
Title: Density Ratio Estimation
Description: Density ratio estimation.
The estimated density ratio function can be used in many applications such as
Expand All @@ -19,5 +19,5 @@ Suggests:
rmarkdown,
stats,
testthat
RoxygenNote: 5.0.1
RoxygenNote: 6.1.1
VignetteBuilder: knitr
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# densratio 0.1.0

## Bug fixes

* Modify Euclidean distance to square Euclidean distance in the function that computes Gaussian kernel (#6). It affects all algorithms in the packages.
10 changes: 5 additions & 5 deletions R/compute_kernel_Gaussian.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ compute_kernel_Gaussian <- function(x, centers, sigma) {
}

kernel_Gaussian <- function(x, y, sigma) {
exp(- euclid_distance(x, y) / (2 * sigma * sigma))
exp(- squared_euclid_distance(x, y) / (2 * sigma * sigma))
}

#' Compute Euclid Distance
#' Compute Squared Euclid Distance
#'
#' @param x a numeric vector.
#' @param y a numeric vector.
#'
#' @return euclid distance
euclid_distance <- function(x, y) {
sqrt(sum((x - y) ^ 2))
#' @return squared euclid distance
squared_euclid_distance <- function(x, y) {
sum((x - y) ^ 2)
}
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# An R Package for Density Ratio Estimation
Koji MAKIYAMA (@hoxo_m)
---
title: "An R Package for Density Ratio Estimation"
author: Koji MAKIYAMA (@hoxo_m)
output:
html_document:
keep_md: yes
md_document:
variant: markdown_github
---

<!-- README.md is generated from README.Rmd. Please edit that file -->

Expand Down
Binary file modified README_files/figure-html/compare-true-estimate-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified README_files/figure-html/compute-estimated-density-ratio-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions man/KLIEP.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/densratio.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 6 additions & 7 deletions man/euclid_distance.Rd → man/squared_euclid_distance.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions man/uLSIF.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions tests/testthat/test-KLIEP.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ test_that("KLIEP", {
sigma <- result$kernel_info$sigma
lambda <- result$lambda

expected_alpha <- c(0.19989552888216, 0.194091742522661, 0.0190396603242885, 0, 0, 0.111847817937993, 0, 0.275314166410662, 0.250066194656671, 0.285929838278085, 0.137819427961783, 0.0497277932054529, 0.0587696724349364, 0.0556990471588486, 0.0280886116019077, 0.119198735095363, 0, 0, 0.0198476263799833, 0.0942488348644642, 0.100477427039042, 0.150254540583809, 0, 0, 0.0561974813114708, 0, 0.0104455053872225, 0.091420126224865, 0.297386672582822, 0.0704176968009879, 0.180431108569687, 0.0498880646780557, 0, 0.149986020376348, 0, 0.10860370660987, 0.0956227974781267, 0, 0, 0.0396385217890564, 0.0679684079523449, 0.144915144946574, 0.0670388839214347, 0.137411254588231, 0.136783699689613, 0, 0.102816676466153, 0, 0.298418291290289, 0, 0.0777171720746529, 0.0685150744692756, 0.147046599217944, 0, 0.069314635746898, 0.0232625096641745, 0.0955622204740962, 0.234698210911241, 0.134735788477325, 0.0616488318603178, 0.0361857386618973, 0.125197229708402, 0.099062048495542, 0.294175399053216, 0.110697147496438, 0.25170916731008, 0.288783806311547, 0, 0.159719859866478, 0, 0, 0.132440227971601, 0.0228787903548558, 0.148631210334356, 0.0547579344816517, 0.244141284803073, 0.296652868688535, 0, 0.157191473239082, 0, 0.151978392078735, 0, 0.0342978950395334, 0, 0, 0, 0.0557533450160968, 0.00633322162316586, 0, 0.0305463743430339, 0.0861514031326755, 0, 0.0629374632325391, 0, 0, 0.0983648721580112, 0.013790246251806, 0, 0, 0.048827373784507)
expected_alpha <- c(0.12049194, 0.11873354, 0.02361388,
0.00000000, 0.01495441, 0.11510380)

expect_equal(alpha, matrix(expected_alpha))
expect_equal(sigma, 0.19999)
expect_equal(head(alpha), matrix(expected_alpha))
expect_equal(sigma, 0.0899)
})
6 changes: 3 additions & 3 deletions tests/testthat/test-compute_kernel_Gaussian.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
context("compute_kernel_Gaussian")

test_that("euclid_distance", {
test_that("squared_euclid_distance", {
set.seed(3)
x <- rnorm(30)
y <- rnorm(1)
act <- euclid_distance(x, y)
act <- squared_euclid_distance(x, y)

expect_equal(act, 7.590585, tolerance = 1e-6)
expect_equal(act, 57.61699, tolerance = 1e-6)
})
7 changes: 4 additions & 3 deletions tests/testthat/test-uLSIF.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ test_that("uLSIF", {
sigma <- result$kernel_info$sigma
lambda <- result$lambda

expected_alpha <- c(0.404438506887284, 0.0479292203749822, 0.173606157877619, 0.125042526178644, 0.0597270012073941, 0.0966603261118248, 0.272404529705359, 0.287981057489667, 0.260610049757097, 0.106278157430805, 0.0829840080770411, 0.298178274456792, 0.510772998165055, 0.469607333880889, 0.314439002127609, 0.278337795855529, 0.331619241264206, 0.170054318854631, 0.205321130356657, 0.293778700956746, 0.270357021760922, 0.0552664460251111, 0.13196964979648, 0.363769875598833, 0.184256996291219, 0.137609652667155, 0.0847578696859158, 0.32005635102062, 0.219732410235939, 0.0997501176563409, 0.393489228923113, 0.357370823360959, 0.0806127510373266, 0.529227340304734, 0.059810696530967, 0.265310896954757, 0.260129661912768, 0.388001725321486, 0.0904385083880115, 0.218070452341649, 0.304531086885202, 0.341133344752911, 0.0877919626863925, 0.352734725618458, 0.18136671171543, 0.177269236897875, 0.286441936811215, 0.220534272633438, 0.0992526984070236, 0.204229708925053, 0.0965890394815905, 0.407103026497329, 0.529665294272533, 0.321732919141618, 0.117322516640944, 0.393949947111409, 0.376943564537773, 0.204873269244207, 0.195954074621517, 0.332356529428754, 0.19981155997285, 0.269049244357359, 0.306328967063322, 0.310666908293517, 0.147463574143442, 0.139386856654683, 0.31845158811646, 0.0535958181690033, 0.125694986863042, 0.247982186906922, 0.398899193256485, 0.271780379232907, 0.386773644350962, 0.405061241567732, 0.230749178740476, 0.379691967500355, 0.14822498635532, 0.398130683107783, 0.139329664337998, 0.240384893331041, 0.17581211444761, 0.0724416717597075, 0.334560609246938, 0.234046251185158, 0.523607632955095, 0.35709470884419, 0.130887248859899, 0.265689337117447, 0.379551225111785, 0.158379575619727, 0.404091364188986, 0.380222303793605, 0.0646382327573607, 0.340377888030049, 0.40542478984169, 0.320660496561861, 0.108071578735585, 0.258826598567493, 0.169361948873109, 0.196116462197673)
expected_alpha <- c(0.08770412, 0.00000000, 0.07556255,
0.01371470, 0.00000000, 0.00000000)

expect_equal(alpha, expected_alpha)
expect_equal(head(alpha), expected_alpha)
expect_equal(sigma, 0.1)
expect_equal(lambda, 0.1)
expect_equal(lambda, 1)
})
76 changes: 0 additions & 76 deletions vignettes/densratio.R

This file was deleted.

Loading

0 comments on commit d82aae1

Please sign in to comment.