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.
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.
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.
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.
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.
Sign in to take notes on this problem
Accepts: array
Accepts: array
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.
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.
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.
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.
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.
Sign in to take notes on this problem
Accepts: array
Accepts: array