Skip to content

Commit

Permalink
debugged noLD uniroot tolerance issue
Browse files Browse the repository at this point in the history
  • Loading branch information
alexviiia committed Jan 9, 2020
1 parent 3dd4afb commit 626657a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: bnpsd
Title: Simulate Genotypes from the BN-PSD Admixture Model
Version: 1.2.0
Version: 1.2.1
Authors@R: c(
person(given = "Alejandro",
family = "Ochoa",
Expand Down
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,11 @@ These loci are not polymorphic so they would normally not be considered in analy
* Removed deprecated function names: `q1dc`, `q1d`, `qis`, `coanc`, `rbnpsd`, `rgeno`, `rpanc`, `rpint`, `rpiaf`.
* Moved logo to `man/figures/`
* Minor Roxygen-related updates.

# 2020-01-08 - bnpsd 1.2.1

* Fourth CRAN submission, second attempt.
* Fixed a rare bug in `bias_coeff_admix_fit`, which caused it to die if the desired bias coefficient was an extreme value (particularly `1`).
The error message was: `f() values at end points not of opposite sign`.
The actual bug was not observed in the regular R build, but rather in a limited precision setting where R was configured with `--disable-long-double`.

18 changes: 16 additions & 2 deletions R/bias_coeff_admix_fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ bias_coeff_admix_fit <- function(
bias_coeff_min <- bias_coeff_admix(admix_prop_bias_coeff_min, coanc_subpops)
if (bias_coeff < bias_coeff_min)
stop('Desired `bias_coeff` must be greater than ', bias_coeff_min, ' (the minimum achievable with `sigma = 0`), passed ', bias_coeff)

# tolerance should be precision-dependent, but when configured with --disable-long-double the value of .Machine$double.eps doesn't change!
# so here we test for that config difference (by testing if .Machine$sizeof.longdouble is zero) and reduce the tolerance accordingly
tol <- .Machine$double.eps
if ( .Machine$sizeof.longdouble == 0 )
tol <- sqrt( tol )

# function whose zero we want!
# "sigma" is the only parameter to optimize
Expand All @@ -90,15 +96,23 @@ bias_coeff_admix_fit <- function(
coord_ind_last = coord_ind_last
)
# get bias coefficient, return difference from desired value!
bias_coeff_admix(admix_proportions, coanc_subpops) - bias_coeff
delta <- bias_coeff_admix(admix_proportions, coanc_subpops) - bias_coeff

# hack to prevent issues when attempting to fit extreme values (at boundaries)
# in particular, without this (and in particular in lower precision settings, such as --disable-long-double), uniroot below complains because delta has the same sign on both extrema (due to machine error, not a real sign issue). If the sign wasn't checked then the tolerance would find that the solution is at the boundary, so here we force the check earlier.
if ( abs(delta) < tol )
delta <- 0

# return delta now
return( delta )
}

# default tolerance of .Machine$double.eps^0.25 (~ 1e-4) was not good enough, reduced to ~ 2e-16
obj <- stats::uniroot(
bias_coeff_admix_objective,
interval = c(0, 1),
extendInt = "no",
tol = .Machine$double.eps
tol = tol
)
# this is the value in terms of `x`
x_root <- obj$root
Expand Down
10 changes: 6 additions & 4 deletions cran-comments.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
## Test environments
* local: x86_64-redhat-linux-gnu R 3.6.2
* win-builder devel: x86_64-w64-mingw32 R Under development (unstable) (2020-01-03 r77630)
* win-builder release: x86_64-w64-mingw32 R 3.6.2 (2019-12-12)
* rhub: (debian) x86_64-pc-linux-gnu R Under development (unstable) (2020-01-03 r77629)
* local R: x86_64-redhat-linux-gnu R 3.6.2
* local R-devel: x86_64-redhat-linux-gnu R Under development (unstable) (2020-01-08 r77640)
* local R-devel-noLD: x86_64-redhat-linux-gnu R Under development (unstable) (2020-01-08 r77640)
* win-builder devel: x86_64-w64-mingw32 R Under development (unstable) (2020-01-03 r77630)
* win-builder release: x86_64-w64-mingw32 R 3.6.2 (2019-12-12)
* rhub: (debian) x86_64-pc-linux-gnu R Under development (unstable) (2020-01-03 r77629)

## R CMD check results
There were no ERRORs, WARNINGs, or NOTEs.
Expand Down

0 comments on commit 626657a

Please sign in to comment.