I am trying to understand the C function which is called in the R Holt-Winters function.
The section by which I am confused reads:
for (i = *start_time - 1; i < *xl; i++) {
56 /* indices for period i */
57 i0 = i - *start_time + 2;
58 s0 = i0 + *period - 1;
59
60 /* forecast *for* period i */
61 xhat = level[i0 - 1] + (*beta > 0 ? trend[i0 - 1] : 0);
62 stmp = *gamma > 0 ? season[s0 - *period] : (*seasonal != 1);
63 if (*seasonal == 1)
64 xhat += stmp;
65 else
66 xhat *= stmp;
This reads as though, if "t" is 13 and there are 12 seasonal periods (i.e. "period" is 12), then i0 would be 1 and s0 would be 12. stmp would then take a value which was based on the "season" value from time (s0-12), which in this case would be time 0. But this doesn't make sense, since the seasonality component in the Holt-Winters model is from (in this case), 12 periods previously.
I would be grateful if someone could explain what i0 and s0 actually are, and where I have failed in my understanding of this.
Full code:
https://github.com/pierre/holt-winters/blob/master/holt-winters.c