def METHOD_NAME(tb: Table, tb_percentiles: Table, extrapolated_dict: dict, welfare_vars: list) -> Table:
    """
    Add relative poverty values by estimating the median and checking that value against the percentile distribution
    """
    log.info("add_relative_poverty.start")
    # Make copies of the tables
    tb = tb.copy()
    tb_percentiles = tb_percentiles.copy()
    # Make tb_percentiles wide, by creating a column for each welfare
    tb_percentiles = tb_percentiles.pivot(
        index=["country", "year", "p", "percentile"], columns="welfare", values=["thr", "thr_extrapolated"]
    )
    # Flatten column names
    tb_percentiles.columns = ["_".join(col).strip() for col in tb_percentiles.columns.values]
    tb_percentiles = tb_percentiles.reset_index()
    # Calculate 40, 50, and 60 percent of the median for each country and year
    for var in welfare_vars:
        for yn, extrapolated in extrapolated_dict.items():
            for pct in [40, 50, 60]:
                tb[f"median{pct}pct_{var}{extrapolated}"] = tb[f"median_{var}{extrapolated}"] * pct / 100
    # Merge the two tables
    tb_percentiles = pr.merge(tb_percentiles, tb, on=["country", "year"], how="left")
    # Calculate absolute difference between thresholds and percentage of median
    for var in welfare_vars:
        for yn, extrapolated in extrapolated_dict.items():
            for pct in [40, 50, 60]:
                tb_percentiles[f"abs_diff{pct}pct_{var}{extrapolated}"] = abs(
                    tb_percentiles[f"thr{extrapolated}_{var}"] - tb_percentiles[f"median{pct}pct_{var}{extrapolated}"]
                )
    # For each country and year, find the percentile with the minimum absolute difference for each welafre, extrapolation and pct
    tb_relative_poverty = Table()
    for var in welfare_vars:
        for yn, extrapolated in extrapolated_dict.items():
            for pct in [40, 50, 60]:
                tb_percentiles[f"min{pct}pct_{var}{extrapolated}"] = tb_percentiles.groupby(["country", "year"])[
                    f"abs_diff{pct}pct_{var}{extrapolated}"
                ].transform("min")
                # Create a table with the minimum absolute difference for each country and year
                tb_min = tb_percentiles[
                    tb_percentiles[f"abs_diff{pct}pct_{var}{extrapolated}"]
                    == tb_percentiles[f"min{pct}pct_{var}{extrapolated}"]
                ]
                # The result generates multiple values for some countries and years, so we need to drop duplicates
                tb_min = tb_min.drop_duplicates(subset=["country", "year"], keep="last")
                # Select only what is needed
                tb_min = tb_min[["country", "year", "p"]]
                # Multiply by 100 to get the headcount ratio in percentage and rename
                tb_min["p"] *= 100
                tb_min = tb_min.rename(columns={"p": f"headcount_ratio_{pct}_median_{var}{extrapolated}"})
                # Merge this table with tb_relative_poverty
                if tb_relative_poverty.empty:
                    tb_relative_poverty = tb_min
                else:
                    tb_relative_poverty = pr.merge(tb_relative_poverty, tb_min, on=["country", "year"], how="outer")
    return tb_relative_poverty