The issue is that the model
------------
data {
int[0,] J; // number of days
real y[J]; // Polls
}
parameters {
real alpha[J];
real[0,.1] tau;
}
model {
alpha[1]~uniform(0,1);
for(i in 2:J)
alpha[i]~normal(alpha[i-1],tau);
for(i in 1:J)
y[i]~normal(alpha[i],0.01);
}
---------------
tries to sample
alpha[1] ~ uniform(0,1);
but doesn't constrain alpha[1] to fall between 0 and 1.
So what happens is that in initialization or when
randomizing momentum in each iteration, alpha[1] is going
outside its support in the 0 to 1 range, and thus
uniform() returns not-a-number (NaN).
Fix 1.
Put a different prior on alpha[1].
For instance,
alpha[1] ~ normal(0,100);
If you just drop the sampling statement for
alpha[1], you implicitly have an improper uniform
prior on alpha[1]. Because alpha[1] is constrained
through the sampling statement for alpha[2] in the
loop, this might work. In general, the posterior
must be proper.
Fix 2.
Define alpha[1] separately with a constraint.
real<lower=0,upper=1> alpha_1;
and then make alpha one element shorter:
real alpha[J-1];
and then you can sample
alpha_1 ~ uniform(0,1);
We've changed the constraint syntax on you since
your first post. This is probably the last syntax change
before the 1.0 release, and we'll try very hard to
maintain backward compatibility after that.
Then you'll need to change the rest of the model:
for(i in 2:J)
alpha[i]~normal(alpha[i-1],tau);
for(i in 1:J)
y[i]~normal(alpha[i],0.01);
to something where the new indexing is one less
alpha[1] ~ normal(alpha_1,tau);
for (i in 2:(J-1))
alpha[i] ~ normal(alpha[i-1],tau);
y[1] ~ normal(alpha_1,0.01);
for (i in 2:J)
y[i] ~ normal(alpha[i-1],0.01);
This kind of loop unfolding and consequent index
fiddling's a pain, but we don't have a way to
define a constraint on a single member of
an array.
An only slight less efficient alternative would be to
define a derived
real<lower=0,upper=1> alpha_1
real alpha_raw[J-1];
and then a transformed parameter
transformed parameters {
real alpha[J];
alpha[1] <- alpha_1;
for (j in 2:J)
alpha[j] <- alpha_raw[j-1];
}
Then you can use alpha as before.
- Bob
On 8/23/12 3:38 AM, dynotec wrote:
> The issue is that I get domain errors at every iteration, as well as non-sense parameter estimates even after very long
> runs on multiple chains on simulated chains that disagree with estimates obtained from BUGS(Giving estimated values
> above 1 or below zero when the range of observations is between .48 and .51). Is there an error with the model code that
> is causing this?
>
> On Wednesday, August 22, 2012 9:42:05 AM UTC-5, Marcus wrote:
>
> The compiler warnings can be ignored safely. The domain_errors mean
> the sampler tried a parameter value which resulted in a bad value
> somewhere along the line. This is normal, particularly in the
> adaptation stage.
>
> Cheers,
> Marcus
>