def two_sided_midpoint_PV(dist, s, out_dir, PVs_file):
    """
    two-sided midpoint PV - Hohna et al. 2017
    :param dist: simulated distributions
    :param s: current original statistic to compare
    :param out_dir: where to write results to
    :param PVs_file: the calculated two-sided midpoint p-values
    :return: 0 or 1 - inadequate or adequate
    """
    sim_is_lower = len([x for x in dist if x < s])  # V
    sim_equals = len([x for x in dist if x == s])  # V
    nsims = len(dist)
    Pml = round((sim_is_lower / nsims) + (sim_equals / (2 * nsims)), 3)  # midpoint one-tailed lower p-value
    Pmu = 1 - Pml
    Pmt = 2 * min(Pml, Pmu)
    with open(out_dir + PVs_file, "a") as p_vals:
        p_vals.write(str(Pmt) + "\n")
    if Pmt <= 0.05:
        return 0
    else:
        return 1