# Force final period to be zero in time series

Hi all, I have a seemingly simple question, but I cannot figure it out.
In short I am running a Dynamic Factor Model, in which the subtrends follow a random walk, and the (log) index values start and end at zero. Especially the ending part is an issue. If I wanted just the starting value to be zero I could easily do the following (just showing the subtrends for brevity);

``````parameters {
matrix[Nt-1,Nm]             innovSub;  // innovations of sub trends
vector<lower=0>[Nm]         sigSub;   // variance of sub trends
}

transformed parameters {
matrix[Nt,Nm] muSub;                  // latent trends

for(m in 1:Nm){
muSub[1,m]   = 0;
for(t in 2:Nt){
muSub[t,m] = muSub[t-1,m] + innovSub[t-1,m]*sigSub[m];
}
}
``````

However, note that in this code, the final period is not forced to be zero, which is costum in DFM literature. So somehow I want to incorporate something like this;

``````transformed parameters {
for(m in 1:Nm){
muSub[Nt,m]  = 0;
}
}
``````

Buy I am sure that adding this to the transformed parameter block in itself does not work. Likewise, adding something like -sum() to the final period would not work, as this violates the random walk. In other words, without the random walk structure I can think of multiple solutions, but now I am unsure how to solve it.

Alex

Conditional on the first N - 1 steps the final N th step is always fixed. Thus, when you say “violates the random walk”, I imagine that you mean something like “this would result in a step that looks out of place given the step-size distribution of the random walk”.

It seems that you might want:

``````for (m in 1:Nm) {
muSub[1,m]   = 0;
for (t in 2:(Nt - 1)) {
muSub[t,m] = muSub[t-1,m] + innovSub[t-1,m]*sigSub[m];
}
innovSub[Nt - 1, m] = -muSub[Nt - 1, m]/sigSub[m]; // the random-walk-inducing
// prior on innovSub and estimate of sigSub will ensure that the walk as a whole tends
// to end up at a location where this final step looks reasonable.
}
``````

Again, note that this construction still “violates the random walk” in the sense that the steps of the random walk are no longer unconditional draws from the step-size distribution, and neither is the realized step-size distribution exactly the same distribution as the unconditional step-size distribution implied by `sigSub` and the prior on `innovSub`.

Thanks,

yes, this is the type of answer I am looking for.
Unfortunately in this case Stan doesn’t like the fact that innovSub in my specification is a parameter, which can therefore not be on the left-hand side in a transformed parameter block.

Any other ideas are very welcome!
Alex

Indeed; I was sloppy here. The last row of innovSub should be declared as a separate transformed parameter (with a different name), but then should be given the same prior as `innovSub`.