German tank problem in Stan

I thought about the most straight-forward solution to the German tank problem in stan:

data {
  int<lower=0> N;
  vector[N] y;
}

parameters {
  real<lower=0> max_y;
}
model {
  for(i in 1:N) {
    y[i] ~ uniform(0, max_y);
  }
}

I drive it with the following R code:

sample_data<-function(N, max_y) {
  runif(N, max=max_y)
}

sampl<-sample_data(40, 1000)

library(rstan)
options(mc.cores = parallel::detectCores())

fit<-rstan::stan(file='~/tmp/tank_problem.stan', data = list(
  N=length(sampl), y=sampl
), iter = 10000)

I get thousands of errors like this after the model is compiled:

Rejecting initial value:
   Log probability evaluates to log(0), i.e. negative infinity.
   Stan can't start sampling from this initial value.

Why does the model fail to sample?


I’ve asked the same problem on https://stats.stackexchange.com/q/415719/10069, but it was put on hold for being off-topic.

1 Like

The main problem is that the lower bound of max_y must be more than max(y). So the working model should read

data {
  int<lower=0> N;
  vector[N] y;
}

parameters {
  real<lower=max(y)> max_y;
}
model {
  for(i in 1:N) {
    y[i] ~ uniform(0, max_y);
  }
}

Moreover, it is possible to use vectorized model formula

data {
  int<lower=0> N;
  vector[N] y;
}

parameters {
  real<lower=max(y)> max_y;
}
model {
  y ~ uniform(0, max_y);
}

In the first model that you showed, shouldn’t real<lower=0> max_y be real<lower=max(y)> max_y (as it is in the second model)?

Of course. Good catch! Thank you.