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

bug in my_etruncnorm #78

Open
stephens999 opened this issue Dec 11, 2017 · 5 comments
Open

bug in my_etruncnorm #78

stephens999 opened this issue Dec 11, 2017 · 5 comments

Comments

@stephens999
Copy link
Owner

there seems to be a bug here:

ashr:::my_etruncnorm(0,99,-2,3)
[1] 5.795534
truncnorm:::etruncnorm(0,99,-2,3)
[1] 1.795534

@stephens999
Copy link
Owner Author

I fixed this problem but now there still seems to be a problem with infinite limits:

ashr:::my_etruncnorm(0,99,-2,3)
[1] 1.795534
ashr:::my_etruncnorm(0,Inf,-2,3)
[1] 0
ashr:::my_etruncnorm(0,9999,-2,3)
[1] 1.795534

@stephens999
Copy link
Owner Author

this turned out to be a bug (or feature) of truncnorm::etruncnorm
with infinite limits when a>b

truncnorm:::etruncnorm(-1,-Inf,-2,3)
[1] NA
truncnorm:::etruncnorm(-1,-999,-2,3)
[1] -3.795471
truncnorm:::etruncnorm(-Inf,-1,-2,3)
[1] -3.795471

@willwerscheid
Copy link
Contributor

willwerscheid commented Jan 25, 2019

etruncnorm does not do well when a and/or b are far away from zero, due to the following lines:

  /* Special case numerically instable case when (a, b) is far away from the
   * center of mass. */
  if (b < mean - 6.0 * sd || a > mean + 6.0 * sd)
    return (a + b) / 2.0;

So, for example, truncnorm::etruncnorm(-31.571, -6.379, 0, 1) gives -18.975 (the correct expected value should be -6.529). This often leads to problems for semi-nonnegative flash (and, I'd expect, for any other applications using unimix). The weird thing is that this case shouldn't actually be unstable, given their implementation (all calculations are done on log scale). We could easily implement the function ourselves -- it's not complicated. See here, ll. 47-76:
https://github.com/olafmersmann/truncnorm/blob/master/src/truncnorm.c

@stephens999
Copy link
Owner Author

stephens999 commented Jan 25, 2019 via email

@willwerscheid
Copy link
Contributor

No, it's been there since 2015.

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

No branches or pull requests

2 participants