Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobbossek committed Dec 17, 2019
0 parents commit b0ca8fd
Show file tree
Hide file tree
Showing 25 changed files with 1,783 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
test.R
^.*\.Rproj$
^\.Rproj\.user$
^.*tar.gz$
.gitignore
.gitmodules
README.md
test_all.R
.travis.yml
appveyor.yml
examples
man-roxygen
benchmark.R
todo
benchmark
cran-comments.md
^docs$
bottleneck
data-raw
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.Rproj.user
.Rhistory
.RData
ecr.Rcheck
ecr.Rproj
ecr_*.tar.gz
playground.R
test_specific.R
Rplots.pdf
*.o
*.so
inst/doc
25 changes: 25 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Package: sampling
Title: Sampling Plans in R
Description: Calculation of star discrepancy of a point set and methods for pseudo-random and quasi-random point set generation: uniform random sampling, latin hypercube sampling, Sobol and Halton sequences.
Version: 1.0.0.0000
Encoding: UTF-8
Date: 2019-12-20
Authors@R: c(
person("Jakob", "Bossek", email = "j.bossek@gmail.com", role = c("aut", "cre", "cph")),
person("Michael H.", "Buselli", role = c("ctb", "cph")))
Maintainer: Jakob Bossek <j.bossek@gmail.com>
License: GPL-3 | file LICENSE
URL: https://github.com/jakobbossek/sampling
BugReports: https://github.com/jakobbossek/sampling/issues
Depends:
R (>= 2.10),
BBmisc (>= 1.6)
Imports:
checkmate (>= 1.1),
randtoolbox,
lhs
Suggests:
testthat (>= 0.9.1)
ByteCompile: yes
LazyData: yes
RoxygenNote: 6.1.1
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(stardiscrepancy)
import(BBmisc)
import(checkmate)
useDynLib(sampling, .registration = TRUE)
93 changes: 93 additions & 0 deletions R/design.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#' @title
#' Create a design.
#'
#' @description
#' To be written ...
#'
#' @param n [\code{integer(1)}]\cr
#' Number of design points (rows).
#' @param k [\code{integer(1)}]\cr
#' Number of variables (columns).
#' @param l [\code{numeric}]\cr
#' Lower bound for design points. Either a single numeric value
#' or a vector of length \code{k}.
#' Default is 0.
#' @param u [\code{numeric}]\cr
#' Upper bound for design points. Either a single numeric value
#' or a vector of length \code{k}.
#' Default is 1.
#' @param method [\code{character(1)}]\cr
#' Name of method used to generate the design. Possible values are
#' \describe{
#' \item{uniform}{Uniform random sampling.}
#' \item{improvedlhs}{Delegate to \code{\link[lhs]{improvedLHS}}.}
#' \item{maximinlhs}{Delegate to \code{\link[lhs]{maximinLHS}}.}
#' \item{geneticlhs}{Delegate to \code{\link[lhs]{geneticLHS}}.}
#' \item{halton}{Delegate to \code{\link[randtoolbox]{halton}}.}
#' \item{sobol}{Delegate to \code{\link[randtoolbox]{sobol}}.}
#' }
#' @param as.df [\code{logical(1)}]\cr
#' Return points as data frame?
#' Default is \code{TRUE}.
#' @param ... [any]\cr
#' Further parameters passed down to generator if applicable.
#' @return Design points as a matrix or data.frame (see parameter \code{as.df}).
#' @examples
#' methods = getSupportedMethods()
#' designs = lapply(methods, function(method) {
#' design(n = 100, k = 2, method = method, l = -5, upper = c(5, 10), as.df = FALSE)
#' })
#'
#' # pass down options to generator
#' d = design(n = 50, k = 4, method = "sobol", scrambling = 2, seed = 123)
#' print(d)
#' @export
design = function(n, k, method, l = 0, u = 1, as.df = TRUE, ...) {
n = checkmate::asInt(n, lower = 2L, na.ok = FALSE)
k = checkmate::asInt(k, lower = 1L, na.ok = FALSE)
checkmate::assertChoice(method, choices = getSupportedMethods())
checkmate::assertFlag(as.df)

if (length(l) == 1L)
l = rep(l, k)
if (length(u) == 1L)
u = rep(u, k)

checkmate::assertNumeric(l, len = k, any.missing = FALSE, all.missing = FALSE)
checkmate::assertNumeric(u, len = k, any.missing = FALSE, all.missing = FALSE)

if (any(l >= u))
BBmisc::stopf("[sampling::design] Lower bounds must be strictly lower than upper bounds.")

des = if (method == "uniform") {
matrix(runif(n * k), nrow = n)
} else if (method == "improvedlhs") {
lhs::improvedLHS(n = n, k = k, ...)
} else if (method == "maximinlhs") {
lhs::maximinLHS(n = n, k = k, ...)
} else if (method == "improvedlhs") {
lhs::geneticLHS(n = n, k = k, ...)
} else if (method == "halton") {
randtoolbox::halton(n = n, dim = k, ...)
} else if (method == "sobol") {
randtoolbox::sobol(n = n, dim = k, scrambling = 3, seed = ceiling(runif(1L, min = 1, max = 1000000)))
} else {
BBmisc::stopf("[sampling::design] Unsupported method '%s'.", method)
}

# linear transformation
des = t(t(des) * (u - l) + l)

if (as.df) {
des = as.data.frame(des)
colnames(des) = paste0("x", seq_len(k))
}

return(des)
}

#' Get character vector of supported design generators.
#' @export
getSupportedMethods = function() {
c("uniform", "improvedlhs", "maximinlhs", "geneticlhs" "halton", "sobol")
}
18 changes: 18 additions & 0 deletions R/stardiscrepancy.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#' @title
#' Calculate star discrepancy of a set of points.
#'
#' @description
#' To be written ...
#'
#' @param x [\code{matrix(n, d)}]\cr
#' An \eqn{n \times d} matrix where \eqn{n} is the number of points and \eqn{d}
#' is the dimension of the search space.
#' @return [\numeric(1)]\cr Star discrepancy of \code{x}.
#' @export
stardiscrepancy = function(x) {
if (checkmate::testDataFrame(x))
x = unname(as.matrix(x))

checkmate::assertMatrix(x, min.rows = 2L, min.cols = 2L, any.missing = FALSE, all.missing = FALSE, mode = "numeric")
return(.Call("starDiscrepancyC", t(x)))
}
Empty file added R/utilities.R
Empty file.
4 changes: 4 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#' @import BBmisc
#' @import checkmate
#' @useDynLib sampling, .registration = TRUE
NULL
100 changes: 100 additions & 0 deletions lhs.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
0.253022915450856 0.22990761208348
0.12112352787517 0.601796171162277
0.493718087440357 0.597349786665291
0.142885130131617 0.170670251362026
0.4271760948468 0.376491232775152
0.257806516485289 0.640252922661602
0.663358768448234 0.594089074293151
0.682992773130536 0.118916020030156
0.568049597553909 0.25547092128545
0.605033716885373 0.432114630239084
0.836378659587353 0.0078568363096565
0.552425206406042 0.975648536114022
0.634326823754236 0.0595397695433348
0.345927965827286 0.109214485622942
0.945101228775457 0.930800352711231
0.781372675672174 0.942845947109163
0.151048242812976 0.811321821063757
0.956188748357818 0.495036167092621
0.51406750571914 0.341001246124506
0.472398628247902 0.0805169607046992
0.617566416505724 0.46968577709049
0.989536985754967 0.243651201948524
0.73607155540958 0.497004712000489
0.718945110449567 0.10995641280897
0.44273673533462 0.538304798305035
0.00404965062625706 0.316003878368065
0.958370928186923 0.345929171191528
0.385171438101679 0.356566360220313
0.591663437196985 0.557763922261074
0.43044931255281 0.187970697879791
0.210821153363213 0.629827392287552
0.536389691289514 0.145325358491391
0.986469031078741 0.0393241739366204
0.758043571840972 0.651457436848432
0.930054697208107 0.101356349652633
0.0522755342535675 0.257429311284795
0.412571460939944 0.666399947367609
0.771341178100556 0.153658266412094
0.285345751326531 0.508523574098945
0.928118038689718 0.128511247457936
0.187369283754379 0.0804562836419791
0.356243156362325 0.843947794986889
0.991327811265364 0.580040421336889
0.78501507290639 0.626025115838274
0.178484899224713 0.849973013391718
0.692535544279963 0.915508342208341
0.0205433941446245 0.496192467864603
0.974999770289287 0.38546999101527
0.556123227346689 0.71235323138535
0.20126667059958 0.722723721992224
0.819714242592454 0.993817307520658
0.548541401512921 0.880915115121752
0.489991025533527 0.441458477405831
0.252151322318241 0.731771438848227
0.936088181566447 0.154205961385742
0.0715441480278969 0.785758606391028
0.975039725191891 0.160593141335994
0.0500071628484875 0.770783729385585
0.614662708714604 0.136371437925845
0.158437236445025 0.359703139634803
0.304497928824276 0.126026706304401
0.0381341730244458 0.43527658819221
0.431226160610095 0.441052877577022
0.06781883421354 0.389975815545768
0.277522057993338 0.985981978243217
0.206932082306594 0.0300460774451494
0.290593875804916 0.462638955563307
0.0951430327259004 0.551165213808417
0.725281604100019 0.195682109333575
0.710990203311667 0.576784510165453
0.418155794497579 0.56084486306645
0.674193867715076 0.727994589600712
0.9920595115982 0.402155606541783
0.470080117927864 0.390917636686936
0.117692516418174 0.679528910433874
0.359322067350149 0.0988261876627803
0.403197608655319 0.90773221058771
0.28662470006384 0.693085825769231
0.919821421150118 0.836114576319233
0.941048826789483 0.163995319977403
0.000979589065536857 0.910818479489535
0.246757663553581 0.42727664578706
0.271023839944974 0.245750994421542
0.506279193330556 0.921838203445077
0.198215828742832 0.916073467116803
0.34282570425421 0.414162284694612
0.321702299639583 0.0103310050908476
0.00422446336597204 0.20220457855612
0.627962226280943 0.549648890970275
0.804501985199749 0.532187291420996
0.659987275255844 0.708451870130375
0.136188758304343 0.551180250244215
0.100476911524311 0.894132857443765
0.498229328542948 0.361522853840142
0.649411271326244 0.333848457317799
0.602355357725173 0.479661879595369
0.327465348877013 0.185043154284358
0.668415803462267 0.67052994389087
0.637243793811649 0.553390565561131
0.604255649028346 0.175885989330709
19 changes: 19 additions & 0 deletions man/stardiscrepancy.Rd

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

Empty file added src/Makevars
Empty file.
Loading

0 comments on commit b0ca8fd

Please sign in to comment.