from typing import Optional


def ema(x: float, mu: Optional[float] = None, alpha: float = 0.3) -> float:
    # taken from https://github.com/timwedde/rich-utils/blob/master/rich_utils/progress.py
    """
    Exponential moving average: smoothing to give progressively lower
    weights to older values.
    Parameters
    ----------
    x  : float
        New value to include in EMA.
    mu  : float, optional
        Previous EMA value.
    alpha  : float, optional
        Smoothing factor in range [0, 1], [default: 0.3].
        Increase to give more weight to recent values.
        Ranges from 0 (yields mu) to 1 (yields x).
    """
    return x if mu is None else (alpha * x) + (1 - alpha) * mu