Generating block giving me negative numbers when <lower=0>?

Ok. I’m trying to generate priors to help tune a model that is misbehaving. I’m using this code:

data{
    int<lower=0> N;          // Total number of rows
}

generated quantities{
    real<lower=0> Tau0 = cauchy_rng(0, 2);
    real<lower=0> Tau = cauchy_rng(0, 2);
    real<lower=0> sd_oc = cauchy_rng(0, 1);
    real<lower=0> sd_dom = cauchy_rng(0, 1);
    real<lower=0> sd0_dom = cauchy_rng(0, 1);

    real beta_oc = normal_rng(0, sd_oc);
    real beta_dom = normal_rng(0, sd_dom);
    real beta0_dom = normal_rng(0, sd0_dom);

    real Zbeta0_ind = normal_rng(0,1);
    real Zbeta_ind = normal_rng(0,1);
}

So I’m running this with this line:

fit2 <- stan(file='gen_priors.stan', data=datalist,
            iter=1, warmup=0, chains=1,
            seed=4838282, algorithm="Fixed_param")

The model compiles ok but then I’m getting this error:

SAMPLING FOR MODEL 'gen_priors' NOW (CHAIN 1).
Chain 1: Iteration: 1 / 1 [100%]  (Sampling)
Chain 1: Exception: model12a285c27a9d0_gen_priors_namespace::write_array: Tau is -7.68902, but must be greater than or equal to 0  (in 'model12a285c27a9d0_gen_priors' at line 15)

Chain 1: 
Chain 1:  Elapsed Time: 0 seconds (Warm-up)
Chain 1:                0.000169 seconds (Sampling)
Chain 1:                0.000169 seconds (Total)
Chain 1: 
[1] "Error in sampler$call_sampler(args_list[[i]]) : "                                                                                                                           
[2] "  Exception: model12a285c27a9d0_gen_priors_namespace::write_array: Tau is -7.68902, but must be greater than or equal to 0  (in 'model12a285c27a9d0_gen_priors' at line 15)"
error occurred during calling the sampler; sampling not done

Ehhh, whats goin on here then? Why is Tau negative when I’ve set <lower=0> ?

The <lower = 0> thing is a post-hoc check not a restriction on cauchy_rng(0, 2);. If you intend to draw from a half-Cauchy distribution, then you need to be doing fabs(cauchy_rng(0, 2));.

1 Like

Ah right didnt’ know that. Thanks very much.