Skip to content

Commit

Permalink
🚑 fixed random sampling error
Browse files Browse the repository at this point in the history
R's global seed interfered with the sampling procedure in pspGlobal
  • Loading branch information
lenarddome committed Aug 11, 2022
1 parent eedd6f9 commit 358eee7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: psp
Title: Parameter Space Partitioning MCMC for Global Model Evaluation
Version: 0.5.3
Version: 0.5.4
Date: 2022-06-12
Authors@R: c(person("Lenard", "Dome",
email = "lenarddome@gmail.com",
Expand Down
9 changes: 8 additions & 1 deletion src/pspGlobal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ using namespace arma;
// https://mathworld.wolfram.com/HyperspherePointPicking.html
// pick new jumping distributions from the unit hypersphere scaled by the radius
mat HyperPoints(int counts, int dimensions, double radius) {
// set a random seed in R for better sampling
// see Documentation about the sampling
//std::srand(std::time_t(nullptr));
int pool = std::rand();
Rcpp::Environment base_env("package:base");
Rcpp::Function set_seed_r = base_env["set.seed"];
set_seed_r(pool);
// create a uniform distribution
mat hypersphere = randn(counts, dimensions, distr_param(0, 1) );
colvec denominator = sum(square(hypersphere), 1);
Expand Down Expand Up @@ -212,7 +219,7 @@ List pspGlobal(Function model, List control, bool save = false,
iteration += 1;
// generate new jumping distributions from ordinal patterns with counts < population
mat jumping_distribution = HyperPoints(last_eval.n_rows, dimensions, radius);
jumping_distribution += last_eval;
jumping_distribution = jumping_distribution + last_eval;
jumping_distribution = ClampParameters(jumping_distribution, lower, upper);
jumping_distribution.shed_rows(find(counts > population));

Expand Down

0 comments on commit 358eee7

Please sign in to comment.