from typing import Dict
from typing import Any


def approx_match(costs: Dict[Any, float], prev_costs: Dict[Any, float], stability_coef: float) -> bool:
    """
    Check if a cost message match the previous message.

    Costs are considered to match if the variation is bellow STABILITY_COEFF.

    Parameters
    ----------
    costs : Dict[val, cost]
      Cost information as a dictionary mapping values to costs
    prev_costs : Dict[val, cost]
      Previous costs as a dict val -> cost
    stability_coef : float
      If the difference between the two is less than this value, the
      costs are considered to match.

    Returns
    -------
    bool : True if `costs` match `prev_costs`
    """
    if prev_costs is None:
        return False

    for d, c in costs.items():
        prev_c = prev_costs[d]
        if prev_c != c:
            delta = abs(prev_c - c)
            if prev_c + c != 0:
                if not ((2 * delta / abs(prev_c + c)) < stability_coef):
                    return False
            else:
                return False
    return True