# Vectorization of state space models - double check

I am building out some state-space models for our time series data. I am working off these models State space models . I am trying to vectorize the models on github and then compare them to the results in the book.

I wanted to check my understanding here.

Given this:

``````mu ~ normal(y, sigma);
for (t in 2:n)
mu[t] ~ normal(mu[t-1], sigma);
``````

I can do this?:

``````mu ~ normal(y, sigma);
mu[2:n] ~ normal(mu[n-1], sigma);
``````

And

`````` v ~ normal(0, sigma);
for(t in 2:n-1)
v[t] ~ normal(v[t-1], sigma);
``````

would be ?

``````v ~ normal(0, sigma);
v[2:n-1] ~ normal(v[n-1], sigma);
``````

Thanks for eyeballs and double check on this!

ara

I think you need.

``````mu[2:n] ~ normal(mu[1:(n-1)], sigma);
``````

and

``````v[2:(n-1)] ~ normal(v[1:(n-2)], sigma);
``````

Otherwise the mean for each time point is the one but last time point in the time series. I am not sure whether you need the round brackets around the indices (n-1, n-2) but I got burned in R a couple of days ago, so I just added them to be sure.

2 Likes

Thanks @stijn! Mine looked weird but I couldn’t place my finger on it. Appreciate the clarification.

ara