The Simple Moving Average (SMA) is the most basic time series smoothing technique. It computes the unweighted mean of a sliding window of consecutive observations, producing a smoother signal that filters out short-term fluctuations and highlights longer-term trends.
Given a list of numeric values and a window size k, compute the SMA for each valid window position.
For each position i from 0 to n - k, compute the average of k consecutive values:
SMA[i]=k1j=0∑k−1x[i+j]The output has length n - k + 1, where n is the input length.
Return n minus window_size plus one moving-average values.
Input: values = [1, 2, 3, 4, 5], window_size = 3
Output: [2.0, 3.0, 4.0]
Explanation: The three complete windows have means 2, 3, and 4.
Input: values = [10, 20, 30, 40], window_size = 2
Output: [15.0, 25.0, 35.0]
Iterate over every start index that leaves a complete window.
Average the slice from the current start through window_size elements.
Sign in to take notes on this problem
Accepts: array
Accepts: number
The Simple Moving Average (SMA) is the most basic time series smoothing technique. It computes the unweighted mean of a sliding window of consecutive observations, producing a smoother signal that filters out short-term fluctuations and highlights longer-term trends.
Given a list of numeric values and a window size k, compute the SMA for each valid window position.
For each position i from 0 to n - k, compute the average of k consecutive values:
SMA[i]=k1j=0∑k−1x[i+j]The output has length n - k + 1, where n is the input length.
Return n minus window_size plus one moving-average values.
Input: values = [1, 2, 3, 4, 5], window_size = 3
Output: [2.0, 3.0, 4.0]
Explanation: The three complete windows have means 2, 3, and 4.
Input: values = [10, 20, 30, 40], window_size = 2
Output: [15.0, 25.0, 35.0]
Iterate over every start index that leaves a complete window.
Average the slice from the current start through window_size elements.
Sign in to take notes on this problem
Accepts: array
Accepts: number