I have been working with a HMM model where animal movement (distance moved, and relative angle) depends on a latent state. The first state is 'foraging' where the animal moves short distances, and frequently changes its movement direction between steps. The other state is 'migrating' where the animal moves with purpose in a given direction, characterised by long steps and small changes in its relative angle.
Bob very kindly helped us to get a model up and running using dynamic programming. I have been comparing this model with fake data, to see how it fares. At the moment the model seems to be (quite strongly) overestimating the time spent in state 1 (Bob mentioned this in his original posting of the model.) For some reason the switching probabilities for state 2 appear to be estimated fairly faithfully.
a. Setting priors for the switching probabilities that are concentrated towards 0.5. This helps a bit, but the priors need to be very concentrated for us to get estimates that are close to the true values.
b. If instead we supply the true switching probs as data, we still see that the estimated latent vector tends to get stuck.
c. Supplying (as data) the true values for rho, alpha and mu, and using the resultant model to estimate the latent state vector. It still seems to get stuck in a given state!
functions{
## Function that returns the log pdf of the wrapped-Cauchy
real wrappedCauchy(real aPhi, real aRho, real aMu) {
return(- log(2*pi()) + log((1-aRho^2)/(1+aRho^2-2*aRho*cos(aPhi-aMu))));
}
}
data {
int<lower=0> N;
vector<lower=-pi(), upper=pi()>[N] phi;
vector<lower=0>[N] l;
int<lower=1> K;
vector<lower=0>[K] theta_priors;
}
parameters {
simplex[K] theta[K]; ## Switching parameter
vector<lower=-pi(), upper=pi()>[K] mu; ## Location of wrapped Cauchy
vector<lower=0,upper=1>[K] rho; ## Scale of wrapped Cauchy
positive_ordered[K] alpha; ## Exponential parameters
}
model {
vector[K] log_theta[K];
vector[K] lp;
for (k in 1:K) log_theta[k] = log(theta[k]);
lp = rep_vector(-log(K), K); ## Initial state log-probability
for (n in 1:N) {
for (k in 1:K)
lp[k]
= log_sum_exp(log_theta[k] + lp)
+ wrappedCauchy(phi[n], rho[k], mu[k])
+ exponential_lpdf(l[n] | alpha[k]);
}
target += log_sum_exp(lp);
alpha[1] ~ normal(0.001,0.0001); ## Priors concentrated near to true values
alpha[2] ~ normal(0.005,0.0001);
## Supply theta prior parameters as data
for(i in 1:K){
theta[i] ~ dirichlet(theta_priors);
}
}
generated quantities {
int<lower=1,upper=K> y_star[N];
real log_p_y_star;
{
int back_ptr[N, K];
real best_logp[N, K];
real best_total_logp;
for (k in 1:K)
best_logp[1, K] = wrappedCauchy(phi[1], rho[k], mu[k])
+ exponential_lpdf(l[1] | alpha[k]);
for (t in 2:N) {
for (k in 1:K) {
best_logp[t, k] = negative_infinity();
for (j in 1:K) {
real logp;
logp = best_logp[t-1, j]
+ log(theta[j, k]) + wrappedCauchy(phi[t], rho[k], mu[k])
+ exponential_lpdf(l[t] | alpha[k]);
if (logp > best_logp[t, k]) {
back_ptr[t, k] = j;
best_logp[t, k] = logp;
}
}
}
}
log_p_y_star = max(best_logp[N]);
for (k in 1:K)
if (best_logp[N, k] == log_p_y_star)
y_star[N] = k;
for (t in 1:(N - 1))
y_star[N - t] = back_ptr[N - t + 1,
y_star[N - t + 1]];
}
}