2

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

Statsanalyst
  • 331
  • 2
  • 3
  • 16
  • 1
    The first time through the loop (which seems to be what you are talking about), `i0` will be 1 as you say, but `s0` will be 12, not 13. (1 + period - 1), so that is one mistake. – Gregor Thomas Nov 10 '17 at 19:36
  • You could replicate this in base R that way you could easily see what is going on with print statements and what not. Just keep in mind, everything you see here is base 0. – Joseph Wood Nov 10 '17 at 20:25
  • Yes @Gregor, you are correct - I meant 12. – Statsanalyst Nov 13 '17 at 11:30

1 Answers1

0

First you need to keep in mind that HW needs initial values for components level, trend and season, inserted in positions level[0], trend[0] and season[0]...season[period-1]. It is important to note that initial season component is a vector with period elements.

The HoltWinters function begins estimating the ith value of the time series, then it computes next values of level, trend and season. The variable i0 corresponds to next empty position in vectors trend and level, and variable s0 corresponds to next empty position in vector season.

In first iteration, i0 and s0 are assigned the following values:

i0=1
s0=period

From second iteration and on, they get incremented by one, just like i0++ and s0++. Their way of updating these variables seems confusing because it is done in function of variable i, but it is for a good reason: statements are the same in all iterations, including the first.

Jefferson
  • 529
  • 4
  • 9