Weighted Moving Average
Weighted Moving Average
The Weighted Moving Average (WMA) extends the Simple Moving Average by allowing different weights for each position in the window. This provides more control over how much influence each observation has on the smoothed value. Common use cases include giving more weight to recent observations or applying domain-specific weighting schemes.
Given a list of values and a list of weights, compute the weighted moving average for each valid window position. The result is normalized by the sum of weights.
Algorithm
For each window starting at position i with k = len(weights):
WMA[i]=∑j=0k−1wj∑j=0k−1wj⋅x[i+j]The output has length n - k + 1.
Examples
Input:
values = [1, 2, 3, 4, 5], weights = [1, 1, 1]
Output:
[2.0, 3.0, 4.0]
Equal weights reduce to the simple moving average: (1+2+3)/3=2.0, (2+3+4)/3=3.0, (3+4+5)/3=4.0.
Input:
values = [10, 20, 30, 40], weights = [1, 2, 3]
Output:
[23.333, 33.333]
First window: (110 + 220 + 330) / 6 = 140/6 = 23.333. Second window: (120 + 230 + 340) / 6 = 200/6 = 33.333. Higher weights on later positions emphasize recent values.
Hint 1
Compute w_sum = sum(weights) once. Loop i from 0 to len(values) - len(weights). For each i, compute the weighted sum: sum(weights[j] * values[i+j] for j in range(k)). Divide by w_sum.
Hint 2
The key difference from SMA is that each position in the window is multiplied by its corresponding weight before summing. Don't forget to normalize by the total weight.
Requirements
- Apply the given weights to each sliding window of values
- Normalize by the sum of weights
- The window size equals the length of the weights list
- Return a list of floats of length n - len(weights) + 1
Constraints
- values has at least len(weights) elements
- weights has at least 1 element, all positive
- Return a list of floats
- Time limit: 300 ms
Log in to take notes on this problem
Accepts: array
Accepts: array
Weighted Moving Average
Weighted Moving Average
The Weighted Moving Average (WMA) extends the Simple Moving Average by allowing different weights for each position in the window. This provides more control over how much influence each observation has on the smoothed value. Common use cases include giving more weight to recent observations or applying domain-specific weighting schemes.
Given a list of values and a list of weights, compute the weighted moving average for each valid window position. The result is normalized by the sum of weights.
Algorithm
For each window starting at position i with k = len(weights):
WMA[i]=∑j=0k−1wj∑j=0k−1wj⋅x[i+j]The output has length n - k + 1.
Examples
Input:
values = [1, 2, 3, 4, 5], weights = [1, 1, 1]
Output:
[2.0, 3.0, 4.0]
Equal weights reduce to the simple moving average: (1+2+3)/3=2.0, (2+3+4)/3=3.0, (3+4+5)/3=4.0.
Input:
values = [10, 20, 30, 40], weights = [1, 2, 3]
Output:
[23.333, 33.333]
First window: (110 + 220 + 330) / 6 = 140/6 = 23.333. Second window: (120 + 230 + 340) / 6 = 200/6 = 33.333. Higher weights on later positions emphasize recent values.
Hint 1
Compute w_sum = sum(weights) once. Loop i from 0 to len(values) - len(weights). For each i, compute the weighted sum: sum(weights[j] * values[i+j] for j in range(k)). Divide by w_sum.
Hint 2
The key difference from SMA is that each position in the window is multiplied by its corresponding weight before summing. Don't forget to normalize by the total weight.
Requirements
- Apply the given weights to each sliding window of values
- Normalize by the sum of weights
- The window size equals the length of the weights list
- Return a list of floats of length n - len(weights) + 1
Constraints
- values has at least len(weights) elements
- weights has at least 1 element, all positive
- Return a list of floats
- Time limit: 300 ms
Log in to take notes on this problem
Accepts: array
Accepts: array