Black Friday forecast: seasonality plus trend
An electronics retailer in Chicago sells much more in Q4 than in Q1, while the business also grows year after year. A plain line through the raw sales would be misleading, so the analyst first removes the seasonal pattern, fits the trend, and then puts the seasonality back to forecast the next cycle.
Task: line 1 is the season length m (for example 4 quarters). Line 2 holds the series (positive numbers); its length n is a multiple of m and at least 2m. Periods are numbered t = 1..n. Step 1: the overall mean M of all values. Step 2: for each position s = 0..m-1 in the cycle (period t has position (t-1) mod m) the seasonal index is mean of the values at that position / M. Step 3: deseasonalise each value, y_t = x_t / index[position]. Step 4: fit y = a + b * t by ordinary least squares with t = 1..n. Step 5: forecast the next m periods t = n+1..n+m as (a + b * t) * index[(t-1) mod m]. Print three lines: Indices: i0 i1 ... (3 decimals each), Trend: slope=B intercept=A (3 decimals) and Forecast: f1 f2 ... (2 decimals each). Print 0.000 instead of -0.000.
Sample tests
Input
4 120 150 170 260 140 168 195 300 160 190 215 340
Expected output
Indices: 0.698 0.844 0.963 1.495 Trend: slope=5.733 intercept=163.403 Forecast: 166.00 205.62 240.28 381.42
Input
2 10 20 12 22
Expected output
Indices: 0.688 1.312 Trend: slope=0.887 intercept=13.784 Forecast: 12.52 25.07
Your answer is also checked against 3 hidden tests.
Log in or create a free account to run your code, submit and earn points.
Run your code to see the output here. Ctrl + Enter also runs it.
