Hi,
I am estimating a model for animal movement where in each time step the animal is in one of two Z states:
- '0' state - foraging: the animal searches for food; moving relatively short distances per time step.
- '1' state - Migrating: the animal is moving to a far-away destination, and hence moves long distances per time step.
I attach fake data for this problem (the true state of the animal is in the 'Z' part of the list, and the distance moved is in the 'l' part of the list). Visually it is fairly obvious as to which state the animal is in at any point in time. I would like to use Stan to try to estimate the state of the animal at a given time step, given its distance moved in this step and all past steps.
I am generating the fake data by assuming that the state is persistent; an animal remains foraging or migrating for multiple time steps. I want to estimate a model in Stan that similarly allows for a persistence in State.
The issue here is that the state is discrete, and more troubling, it takes on a value for each of the time periods (I have chosen N=200 for the fake data I attach, although the real data is around 10000 time steps.) The issue with this long data is that I can't see a way to marginalise out the latent variable. Instead at the moment I have tried to use a continuous latent variable, constrained between 0 and 1, with a switch at 0.5 that dictates the actual state of the system for each time point. The code for this model is below.
Unsurprisingly this model doesn't work. The chains for the 'Z_continuous' parameters aren't mixing at all; I suppose this is because the log-probability does not vary with this variable unless it crosses 0.5.
Does anyone have an idea as to how I might be able to estimate this model, or a similarly-behaving one in Stan?
Further information: I have estimated the model in JAGS, and it works well; the model is very good at returning the state 'Z' at any point in time. The issue however is speed; JAGS is slow, and as such will be impossible to use to estimate the full data set. If I can't get this to work in Stan then I may try 'pymc3', although have never used it before. It also occurs to me that I could do some sort of particle MC sampling here. However, any other ideas are also very welcome!
Best,
Ben
data {
int N; ## Num obs
real<lower=0> l[N]; ## Distance moved
real<lower=0> aPrior0;
real<lower=0> aPriorExtra;
}
parameters{
real<lower=0,upper=1> Z_continuous[N];
real<lower=0> a0;
real<lower=0> aExtra;
real<lower=0> sigma;
}
transformed parameters{
real Z[N];
for(t in 1:N){
Z[t] = Z_continuous[t] > 0.5;
}
}
model{
for(t in 2:N){
if(Z_continuous[t]<0.5){
l[t] ~ exponential(a0+aExtra);
} else{
l[t] ~ exponential(a0);
}
Z_continuous[t] ~ normal(Z_continuous[t-1],sigma);
}
a0 ~ exponential(aPrior0);
aExtra ~ exponential(aPriorExtra);
}