Permalink
Browse files

add test

  • Loading branch information...
hoxo-m committed Jun 12, 2016
1 parent b82bedf commit e8233e651dbef2b34a8c9c2e4432594a13ea8de7
@@ -1,6 +1,6 @@
Package: densratio
Type: Package
Version: 0.0.3
Version: 0.0.3.9001
Title: Density Ratio Estimation
Description: Density ratio estimation.
The estimated density ratio function can be used in many applications such as
@@ -17,6 +17,7 @@ Suggests:
knitr,
mvtnorm,
rmarkdown,
stats
stats,
testthat
RoxygenNote: 5.0.1
VignetteBuilder: knitr
@@ -10,6 +10,12 @@ kernel_Gaussian <- function(x, y, sigma) {
exp(- euclid_distance(x, y) / (2 * sigma * sigma))
}
#' Compute 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))
}
Binary file not shown.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1,4 @@
library(testthat)
library(densratio)
test_check("densratio")
@@ -0,0 +1,18 @@
context("KLIEP")
test_that("KLIEP", {
set.seed(1)
x <- rnorm(200, mean = 1, sd = 1/8)
y <- rnorm(200, mean = 1, sd = 1/2)
result <- KLIEP(x, y)
alpha <- result$alpha
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)
expect_equal(alpha, matrix(expected_alpha))
expect_equal(sigma, 0.19999)
})
@@ -0,0 +1,10 @@
context("compute_kernel_Gaussian")
test_that("euclid_distance", {
set.seed(3)
x <- rnorm(30)
y <- rnorm(1)
act <- euclid_distance(x, y)
expect_equal(act, 7.590585, tolerance = 1e-6)
})
@@ -0,0 +1,19 @@
context("uLSIF")
test_that("uLSIF", {
set.seed(3)
x <- rnorm(200, mean = 1, sd = 1/8)
y <- rnorm(200, mean = 1, sd = 1/2)
result <- uLSIF(x, y)
alpha <- result$alpha
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)
expect_equal(alpha, expected_alpha)
expect_equal(sigma, 0.1)
expect_equal(lambda, 0.1)
})

0 comments on commit e8233e6

Please sign in to comment.