Seasonal Average
Seasonal Average
Seasonal averaging computes the typical value for each position within a repeating cycle. For example, with daily data and a weekly period (7), it computes the average Monday value, average Tuesday value, and so on. This reveals seasonal patterns: "sales are always higher on Fridays" or "traffic peaks in the 8 AM hour". It is a key step in seasonal decomposition and seasonal naive forecasting.
Given a time series and a period length, compute the average value for each seasonal position (0 through period-1).
Algorithm
For each position p in [0, period-1], collect all values at indices p, p+period, p+2*period, ... and compute their mean. The output has length equal to the period.
Examples
Input:
series = [1, 2, 3, 4, 5, 6], period = 3
Output:
[2.5, 3.5, 4.5]
Position 0: indices 0,3 -> values 1,4 -> mean 2.5. Position 1: indices 1,4 -> values 2,5 -> mean 3.5. Position 2: indices 2,5 -> values 3,6 -> mean 4.5.
Input:
series = [1, 2, 1, 2, 1, 2], period = 2
Output:
[1.0, 2.0]
Position 0 (indices 0,2,4): values 1,1,1 -> mean 1.0. Position 1 (indices 1,3,5): values 2,2,2 -> mean 2.0. The alternating pattern is captured perfectly.
Hint 1
For each position p from 0 to period-1, collect values at indices p, p+period, p+2*period, etc. using range(p, len(series), period). Average the collected values.
Hint 2
The output always has exactly 'period' elements. Each element is the mean of values at that seasonal position. Use range with a step equal to the period to gather the right indices.
Requirements
- Group values by their position within the seasonal cycle
- Compute the mean for each seasonal position
- The output has exactly 'period' elements
- Return a list of floats
Constraints
- series has at least 'period' elements
- period >= 1
- Return a list of floats of length equal to period
- Time limit: 300 ms
Log in to take notes on this problem
Accepts: array
Accepts: number
Seasonal Average
Seasonal Average
Seasonal averaging computes the typical value for each position within a repeating cycle. For example, with daily data and a weekly period (7), it computes the average Monday value, average Tuesday value, and so on. This reveals seasonal patterns: "sales are always higher on Fridays" or "traffic peaks in the 8 AM hour". It is a key step in seasonal decomposition and seasonal naive forecasting.
Given a time series and a period length, compute the average value for each seasonal position (0 through period-1).
Algorithm
For each position p in [0, period-1], collect all values at indices p, p+period, p+2*period, ... and compute their mean. The output has length equal to the period.
Examples
Input:
series = [1, 2, 3, 4, 5, 6], period = 3
Output:
[2.5, 3.5, 4.5]
Position 0: indices 0,3 -> values 1,4 -> mean 2.5. Position 1: indices 1,4 -> values 2,5 -> mean 3.5. Position 2: indices 2,5 -> values 3,6 -> mean 4.5.
Input:
series = [1, 2, 1, 2, 1, 2], period = 2
Output:
[1.0, 2.0]
Position 0 (indices 0,2,4): values 1,1,1 -> mean 1.0. Position 1 (indices 1,3,5): values 2,2,2 -> mean 2.0. The alternating pattern is captured perfectly.
Hint 1
For each position p from 0 to period-1, collect values at indices p, p+period, p+2*period, etc. using range(p, len(series), period). Average the collected values.
Hint 2
The output always has exactly 'period' elements. Each element is the mean of values at that seasonal position. Use range with a step equal to the period to gather the right indices.
Requirements
- Group values by their position within the seasonal cycle
- Compute the mean for each seasonal position
- The output has exactly 'period' elements
- Return a list of floats
Constraints
- series has at least 'period' elements
- period >= 1
- Return a list of floats of length equal to period
- Time limit: 300 ms
Log in to take notes on this problem
Accepts: array
Accepts: number