Skip to content

Instantly share code, notes, and snippets.

@kbroman
Created March 19, 2018 03:55
Show Gist options
  • Save kbroman/aedc8d8313f5f471e7089c3d0205f45e to your computer and use it in GitHub Desktop.
Save kbroman/aedc8d8313f5f471e7089c3d0205f45e to your computer and use it in GitHub Desktop.
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector sim_crossovers_orig(const double L)
{
int n_xo;
n_xo = R::rpois(L/100.0);
NumericVector tmp = runif(n_xo, 0.0, L);
return tmp.sort();
}
// [[Rcpp::export]]
NumericVector sim_crossovers_new(const double L)
{
int n_xo;
n_xo = R::rpois(L/100.0);
NumericVector tmp(0);
if(n_xo > 0) tmp = runif(n_xo, 0.0, L);
return tmp.sort();
}
@kbroman
Copy link
Author

kbroman commented Mar 19, 2018

Code to explore a change in Rcpp from version 0.12.15 to 0.12.16.

With Rcpp ver 0.12.15, these two things give the same results.

Rcpp::sourceCpp("sim_crossovers.cpp")

set.seed(20180318)
z <- replicate(10000, sim_crossovers_orig(100))
table(sapply(z, length))
##    0    1    2    3    4    5    6
## 3661 3681 1837  631  151   35    4

set.seed(20180318)
z <- replicate(10000, sim_crossovers_new(100))
table(sapply(z, length))
##    0    1    2    3    4    5    6
## 3661 3681 1837  631  151   35    4

But with Rcpp ver 0.12.16, the former gives a ton of 0-length vectors, while the latter works fine, though it gives somewhat different results from ver 0.12.15. (The tables being printed should be 10,000 draws from a Poisson distribution with mean 1.)

Rcpp::sourceCpp("sim_crossovers.cpp")

set.seed(20180318)
z <- replicate(10000, sim_crossovers_orig(100))
table(sapply(z, length))
##    0    1    3
## 9994    5    1

set.seed(20180318)
z <- replicate(10000, sim_crossovers_new(100))
table(sapply(z, length))
##    0    1    2    3    4    5    6
## 3637 3698 1846  621  152   41    5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment