alashworth / test-issue-import

0 stars 0 forks source link

Two-sided truncation can be numerically problematic #130

Open alashworth opened 5 years ago

alashworth commented 5 years ago

Issue by bgoodri Monday Feb 06, 2017 at 23:47 GMT Originally opened as https://github.com/stan-dev/stan/issues/2226


Summary:

There exists a Stan program where the two-sided truncation notation [L,U] results in the model failing to initialize no matter how small init_r is, while doing the truncation manually in the Stan program works fine with the default value of init_r.

Description:

Double-sided truncation parses to log_diff_exp(foo_cdf_log(U, ...), foo_cdf_log(L, ...)) which can turn out worse than -log(foo_cdf(U, ...) - foo_cdf(L, ...))

Reproducible Steps:

Can change the works variable to 1

transformed data {
  real<lower=0,upper=1> y = 0.5;
  real<lower=0> sigma = 0.001;
  int<lower=0,upper=1> works = 0;
}
parameters {
  real<lower=0,upper=1> mu;
}
model {
  if (works) {
    y ~ normal(mu, sigma);
    target += -log(normal_cdf(1, mu, sigma) - normal_cdf(0, mu, sigma));
  }
  else y ~ normal(mu, sigma) T[0,1];
}

Current Output:

Initialization between (-x, x) failed after 100 attempts. for any value of x

Expected Output:

     mean se_mean  sd  2.5%   25%   50%   75% 97.5% n_eff Rhat
mu    0.5    0.00 0.0  0.50  0.50  0.50  0.50  0.50  2235    1
lp__ -1.9    0.02 0.7 -3.87 -2.07 -1.61 -1.45 -1.39  1812    1

Additional Information:

None

Current Version:

v2.14.0

alashworth commented 5 years ago

Comment by bob-carpenter Tuesday Feb 07, 2017 at 21:01 GMT


That's really surprising. Any idea what's going on? Is it an underflow to zero that's causing the log to become -infinity?

alashworth commented 5 years ago

Comment by bob-carpenter Tuesday Feb 07, 2017 at 21:01 GMT


Accidentally sent that. Maybe we could fix log_sum_diff to deal with the case where the second entry is -infinity.

alashworth commented 5 years ago

Comment by sdewaele Friday Mar 03, 2017 at 16:33 GMT


Thanks for looking into this. Looking forward to the fixed version so i can eliminate the workaround.