def format_test_output()

in sdk/python/jobs/automl-standalone-jobs/automl-forecasting-recipes-univariate/helper_functions.py [0:0]


def format_test_output(test_name, test_res, H0_unit_root=True):
    """
    Helper function to format output. Return a dictionary with specific keys. Will be used to
    construct the summary data frame for all unit root tests.

    TODO: Add functionality of choosing based on the max lag order specified by user.

    :param test_name: name of the test
    :param test_res: object that contains corresponding test information. Can be None if test failed.
    :param H0_unit_root: does the null hypothesis of the test assume a unit root process? Some tests do (ADF),
                         some don't (KPSS).
    :return: dictionary of summary table for all tests and final decision on stationary vs non-stationary.
             If test failed (test_res is None), return empty dictionary.
    """
    # Check if the test failed by trying to extract the test statistic
    if test_name in ("ADF", "KPSS"):
        try:
            test_res["statistic"]
        except BaseException:
            test_res = None
    else:
        try:
            test_res.stat
        except BaseException:
            test_res = None

    if test_res is None:
        return {}

    # extract necessary information
    if test_name in ("ADF", "KPSS"):
        statistic = test_res["statistic"]
        crit_val = test_res["critical"]["5%"]
        p_val = test_res["pval"]
        lags = test_res["resstore"].usedlag if test_name == "ADF" else test_res["lags"]
    else:
        statistic = test_res.stat
        crit_val = test_res.critical_values["5%"]
        p_val = test_res.pvalue
        lags = test_res.lags

    if H0_unit_root:
        H0 = "The process is non-stationary"
        stationary = "yes" if p_val < 0.05 else "not"
    else:
        H0 = "The process is stationary"
        stationary = "yes" if p_val > 0.05 else "not"

    out = {
        "test_name": test_name,
        "statistic": statistic,
        "crit_val": crit_val,
        "p_val": p_val,
        "lags": int(lags),
        "stationary": stationary,
        "Null Hypothesis": H0,
    }
    return out