TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Weighted Moving Average

Time Series
Easy

A weighted moving average smooths a sequence while allowing different positions within each window to have different influence. For values x, weights w, and window length k = len(weights), compute

WMA⁡i=∑j=0k−1wjxi+j∑j=0k−1wj\operatorname{WMA}_i = \frac{\sum_{j=0}^{k-1} w_j x_{i+j}}{\sum_{j=0}^{k-1} w_j}WMAi​=∑j=0k−1​wj​∑j=0k−1​wj​xi+j​​

Here, i is the window's starting index, x_{i+j} is a value in that window, and w_j is the corresponding weight. Evaluate every window that fits completely inside values. Return the weighted averages as a list of floats.

Loading visualization...

Examples

Input: values = [1, 2, 3, 4, 5], weights = [1, 1, 1]

Output: [2.0, 3.0, 4.0]

Explanation: Equal weights make each result the ordinary mean of its three-value window.

Input: values = [10, 20, 30, 40], weights = [1, 2, 3]

Output: [23.333333, 33.333333]

Hint 1

Compute sum(weights) once before processing the windows.

Hint 2

For a window beginning at i, pair weights[j] with values[i + j].

Requirements

  • Apply the weights to each complete sliding window.
  • Divide each weighted sum by the sum of all weights.
  • Use len(weights) as the window size.
  • Return a list of floats.

Constraints

  • values contains at least len(weights) elements.
  • weights is nonempty and contains positive numbers.
  • The sum of weights is positive.
  • Time limit: 300 ms.
Try Similar Problems
Simple Moving AverageExponential Moving AverageMoving MedianDouble Exponential SmoothingRolling Standard Deviation

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

You must run your code first.
PrevNext

Weighted Moving Average

Time Series
Easy

A weighted moving average smooths a sequence while allowing different positions within each window to have different influence. For values x, weights w, and window length k = len(weights), compute

WMA⁡i=∑j=0k−1wjxi+j∑j=0k−1wj\operatorname{WMA}_i = \frac{\sum_{j=0}^{k-1} w_j x_{i+j}}{\sum_{j=0}^{k-1} w_j}WMAi​=∑j=0k−1​wj​∑j=0k−1​wj​xi+j​​

Here, i is the window's starting index, x_{i+j} is a value in that window, and w_j is the corresponding weight. Evaluate every window that fits completely inside values. Return the weighted averages as a list of floats.

Loading visualization...

Examples

Input: values = [1, 2, 3, 4, 5], weights = [1, 1, 1]

Output: [2.0, 3.0, 4.0]

Explanation: Equal weights make each result the ordinary mean of its three-value window.

Input: values = [10, 20, 30, 40], weights = [1, 2, 3]

Output: [23.333333, 33.333333]

Hint 1

Compute sum(weights) once before processing the windows.

Hint 2

For a window beginning at i, pair weights[j] with values[i + j].

Requirements

  • Apply the weights to each complete sliding window.
  • Divide each weighted sum by the sum of all weights.
  • Use len(weights) as the window size.
  • Return a list of floats.

Constraints

  • values contains at least len(weights) elements.
  • weights is nonempty and contains positive numbers.
  • The sum of weights is positive.
  • Time limit: 300 ms.
Try Similar Problems
Simple Moving AverageExponential Moving AverageMoving MedianDouble Exponential SmoothingRolling Standard Deviation

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

You must run your code first.