Bias-Corrected Exponential Moving Average (the Heart of Adam)

~15 mincode completion

Implement ema(xs, beta, bias_correct) returning the list of values (or when bias_correct is true), one per input, for .

Examples

The worked example

Input
ema([4, 4, 8], 0.5, True)
Output
[4, 4, 6.285714]

Uncorrected EMA starts near zero

Input
ema([10, 10, 10, 10], 0.9, False)
Output
[1, 1.9, 2.71, 3.439]

Corrected EMA of a constant is the constant

Input
ema([10, 10, 10, 10], 0.9, True)
Output
[10, 10, 10, 10]

Hints

Hint 1

gives you the index and the value together.

Hint 2

Watch for this: corrected with beta power t minus 1.

Requirements

  • Return List of len(xs) floats: m_t, or m_t / (1 - beta**t) if bias_correct.

Constraints

  • Standard library only, no imports required

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~15 min

8 employers weight this skill

4 big tech firms, 2 quant funds, 1 frontier lab, 1 autonomy company. Top match scores 93.

Python
def ema(xs, beta: float, bias_correct: bool) -> list:
    """
    Exponential moving average, optionally bias-corrected as in Adam.

    Returns:
        List of len(xs) floats: m_t, or m_t / (1 - beta**t) if bias_correct.
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.