Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dqrrademacher #50

Merged
merged 14 commits into from Jul 5, 2023
Merged

Add dqrrademacher #50

merged 14 commits into from Jul 5, 2023

Conversation

kylebutts
Copy link
Contributor

In statistics, a lot of methods require drawing Rademacher weights, -1 and 1, with probably 1/2 each (e.g. bootstrapping and approximations). To minimize the number of random numbers needed, I draw random 64 bits and use each bit of the integer (which is 0/1 with equal probability) and uses that as a 0/1 variable. It's about 8x as fast as dqrng::sample(c(-1, 1))!

I believe I fixed the segfault (user error on 0-based indexing in C++ 🤦🏼‍♂️). I tested for segfaults by calling the following and nothing came-up, so I think we're good.

lapply(1:1000, function(i) dqrrademacher(1e7))
dqrrademacher(1e9)

Benchmarks

library(dqrng)
library(rademacher)
n = 1e7
bench::mark(
  dqrrademacher = dqrrademacher(n),
  rademacher = rademacher::sample_rademacher(n),
  runif = (stats::runif(n) > 0.5) * 2 - 1,
  samp = sample(x = c(1, -1), size = n, replace = TRUE),
  dqsamp = dqsample(x = c(1,-1), size = n, replace = TRUE),
  check = FALSE
)
#> Warning: Some expressions had a GC in every iteration; so filtering is
#> disabled.
#> # A tibble: 5 × 6
#>   expression         min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr>    <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 dqrrademacher   4.59ms   5.44ms    187.      38.1MB    91.4 
#> 2 rademacher      5.69ms    6.7ms    135.      39.4MB    53.8 
#> 3 runif          88.62ms  91.13ms     10.8    190.7MB    12.6 
#> 4 samp          105.22ms 109.35ms      9.23   114.4MB     7.38
#> 5 dqsamp         45.01ms  48.31ms     20.7    114.5MB    16.9

Created on 2023-07-01 with reprex v2.0.2

R/dqset.seed.R Outdated Show resolved Hide resolved
src/dqrng.cpp Show resolved Hide resolved
@rstub
Copy link
Member

rstub commented Jul 2, 2023

BTW, the checks via GHA are currently failing, because I need to add some changes from #47. If you don't mind I would cherry pick those changes into this PR.

@rstub
Copy link
Member

rstub commented Jul 2, 2023

LGTM! Do you want to add yourself as contributor in DESCRIPTION?

@kylebutts
Copy link
Contributor Author

Great, Just added myself! Thanks for your help Ralf!

@rstub rstub merged commit f009c0e into daqana:master Jul 5, 2023
7 checks passed
@rstub
Copy link
Member

rstub commented Jul 5, 2023

Thanks for the contribution! It will take a while until this makes its way to CRAN, since I want to finish #47 first. In the meantime, it should soon be available from https://daqana.r-universe.dev/dqrng.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants