Skip to content

EMA implementation not correct for initial iterations #1101

@almson

Description

@almson

The current exponential moving average implementation is "naive" and is not correct for the initial iterations. The first value has a disproportionate influence, which makes the result diverge from the true average. The correct implementation initializes with 0 and divides the result by 1 - factor**step. I'll skip the proof.

Here is a class that I use:

class EMA:
    beta: float
    ema: float
    step: int

    def __init__ (self, beta: float):
        self.beta = beta
        self.ema = 0
        self.step = 0

    def append(self, value: float):
        self.ema = self.beta * self.ema + (1 - self.beta) * value
        self.step += 1

    def get(self):
        return self.ema / (1 - self.beta ** self.step) if self.step > 0 else 'N/A'

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions