Error "Initialization between (-2, 2) failed after 100 attempts" with parameters defined

1,139 views
Skip to first unread message

tl2...@columbia.edu

unread,
Oct 2, 2016, 1:04:49 AM10/2/16
to Stan users mailing list
Hi Stan friends,

I'm new to Stan and encountered an issue. The problem is to understand, for 1 dog, how likely it would jump when light turns on after repeatedly giving it electric shock if it fails to jump at light turning on.  In my model below, I'm assuming the probability the dog jumps at trial 1 follows a Beta(1,13) distribution. Then the probability is expected to increase over trials. I specified the model as
a(t)~N( a(t-1)+rho, sigma^2), where t is the trial number. Then the probability theta(t)=sigmoid(a(t)) and the observed y(t) ~Bernoulli( theta(t)). So the probability to jump next trial only depends on the previous.

Below is my model

data {
int T;
int<lower=0,upper=1> Y[T];
}
parameters {
real<lower=0> rho; //learning rate
real<lower=0> sigma; 
real<lower=0,upper=1> theta0;  //initial probability that produces Y[1]
}
transformed parameters {
vector[T] a;  //argument for sigmoid function
real a0;
vector<lower=0,upper=1>[T] Theta; //probability at each trial
Theta[1]=theta0;
a0=logit(Theta[1]);
a[1]=a0;
for(t in 2:T)
Theta[t]=exp(a[t])/(1+exp(a[t]));  //sigmoid function
}
model{
sigma~cauchy(0,0.1);  //half-cauchy distribution on sigma as variance
rho~normal(0,0.1); //half-normal distribution on dog's learning rate
theta0~beta(1,13);
for(t in 2:T)
a[t]~normal(a[t-1]+rho,sigma);

for(t in 1:T)
Y[t]~binomial(1,Theta[t]);
}

And data is Y=c(0,0,1,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1) with T=25.

The code above gives me the error 
Initialization between (-2, 2) failed after 100 attempts,The current Metropolis proposal is about to be rejected because of the following issue: validate transformed params: Theta[1] is nan, but must be greater than or equal to 0

I saw another post with the same error, and the solution was to define parameters before using them. However, it seems in my code, I'm using parameters only after I define them. First I sample theta0 from the beta(1,13), then pass it to Theta[1], and get the corresponding a[1]. Then the a[2],a[3]...a[T] would follow the 'markov chain' specification. 

Incidentally, if I delete the line in model block
for(t in 2:T)
a[t]~normal(a[t-1]+rho,sigma);

and add in transformed parameter block
for(t in 2:T)
a[t]=a[t-1]+rho;

The simulation works fine. I wonder why this is the case & would want to hear some great insights. Thanks!

stijn masschelein

unread,
Oct 2, 2016, 4:55:29 PM10/2/16
to Stan users mailing list
 First I sample theta0 from the beta(1,13)

This is not right. Stan does not sample like JAGS or BUGS.

theta0~beta(1,13)

The snippet above does not assign a value to theta0 but it increments the log probability ('target'). I think you will have to follow the advice from the other post you found. Have a look at chapter 27 in the Stan Reference Manual. It explains the difference between assigning a value to a parameter and incrementing the log probability better than I can.
Reply all
Reply to author
Forward
0 new messages