Exponential Moving Average
Exponential Moving Average
The Exponential Moving Average (EMA) is a weighted moving average that gives more importance to recent observations. Unlike the Simple Moving Average which weights all values equally, EMA applies exponentially decreasing weights to older data points, making it more responsive to recent changes.
Given a list of numeric values and a smoothing factor alpha (0 < alpha <= 1), compute the EMA for each position.
Algorithm
Initialize with the first value and apply the recursive formula:
EMA[0]=x[0] EMA[t]=α⋅x[t]+(1−α)⋅EMA[t−1]Higher alpha values make the EMA respond faster to recent changes, while lower alpha values produce a smoother signal.
Examples
Input:
values = [1, 2, 3, 4, 5], alpha = 0.5
Output:
[1, 1.5, 2.25, 3.125, 4.0625]
EMA[0]=1. EMA[1]=0.5×2+0.5×1=1.5. EMA[2]=0.5×3+0.5×1.5=2.25. And so on.
Input:
values = [100, 0, 0, 0], alpha = 0.5
Output:
[100, 50, 25, 12.5]
After a spike, the EMA decays exponentially. Each step halves the previous value since alpha = 0.5 and new values are 0.
Hint 1
Start with ema = [values[0]]. Then loop from index 1 to the end. At each step, compute alpha * values[i] + (1 - alpha) * ema[-1] and append it to the result.
Hint 2
Think of alpha as the weight given to the new observation. When alpha = 1, EMA equals the raw data. When alpha = 0, EMA stays at the initial value forever. Test with alpha = 0.5 for intuition - each new observation and the previous EMA contribute equally.
Requirements
- Initialize EMA[0] with the first value in the series
- Apply the recursive EMA formula for each subsequent value
- Return a list of the same length as the input
- Return a list of floats
Constraints
- values has at least 1 element
- 0 < alpha <= 1
- Return a list of floats of the same length as values
- Time limit: 300 ms
Log in to take notes on this problem
Accepts: array
Accepts: number
Exponential Moving Average
Exponential Moving Average
The Exponential Moving Average (EMA) is a weighted moving average that gives more importance to recent observations. Unlike the Simple Moving Average which weights all values equally, EMA applies exponentially decreasing weights to older data points, making it more responsive to recent changes.
Given a list of numeric values and a smoothing factor alpha (0 < alpha <= 1), compute the EMA for each position.
Algorithm
Initialize with the first value and apply the recursive formula:
EMA[0]=x[0] EMA[t]=α⋅x[t]+(1−α)⋅EMA[t−1]Higher alpha values make the EMA respond faster to recent changes, while lower alpha values produce a smoother signal.
Examples
Input:
values = [1, 2, 3, 4, 5], alpha = 0.5
Output:
[1, 1.5, 2.25, 3.125, 4.0625]
EMA[0]=1. EMA[1]=0.5×2+0.5×1=1.5. EMA[2]=0.5×3+0.5×1.5=2.25. And so on.
Input:
values = [100, 0, 0, 0], alpha = 0.5
Output:
[100, 50, 25, 12.5]
After a spike, the EMA decays exponentially. Each step halves the previous value since alpha = 0.5 and new values are 0.
Hint 1
Start with ema = [values[0]]. Then loop from index 1 to the end. At each step, compute alpha * values[i] + (1 - alpha) * ema[-1] and append it to the result.
Hint 2
Think of alpha as the weight given to the new observation. When alpha = 1, EMA equals the raw data. When alpha = 0, EMA stays at the initial value forever. Test with alpha = 0.5 for intuition - each new observation and the previous EMA contribute equally.
Requirements
- Initialize EMA[0] with the first value in the series
- Apply the recursive EMA formula for each subsequent value
- Return a list of the same length as the input
- Return a list of floats
Constraints
- values has at least 1 element
- 0 < alpha <= 1
- Return a list of floats of the same length as values
- Time limit: 300 ms
Log in to take notes on this problem
Accepts: array
Accepts: number