Ejemplo n.º 1
0
def run_pa(plan, total_steps=10000000):
    partition, chain = set_up_chain(plan, total_steps)

    scores = {
        key: value
        for election in elections
        for key, value in get_scores(election).items()
    }

    # scores['Distance_From_Ideal_Population'] = distance_from_ideal_population

    scores['L_minus_1_Polsby-Popper'] = L_minus_1_polsby_popper

    initial_scores = {key: score(partition) for key, score in scores.items()}

    table = pipe_to_table(chain, scores)

    pathlib.Path(f"./plots/{plan}/{now}").mkdir(parents=True, exist_ok=True)

    for score in scores:
        plt.hist(table[score], bins=50)
        plt.title(score.replace('_', ' '))
        plt.axvline(x=initial_scores[score], color='r')
        plt.savefig(f"./plots/{plan}/{now}/{score}.svg")
        plt.close()

    metadata = {'plan': plan, 'total_steps': total_steps}

    report = {
        key: p_value_report(key, table[key], initial_scores[key])
        for key in scores if key != 'L_minus_1_Polsby-Popper'
    }

    return {**metadata, 'p_value_report': report}
Ejemplo n.º 2
0
def main():
    initial_partition = PA_partition()

    chain = BasicChain(initial_partition, total_steps=10000)

    scores = {
        'Mean-Median':
        functools.partial(mean_median, proportion_column_name='VoteA%'),
        'Mean-Thirdian':
        functools.partial(mean_thirdian, proportion_column_name='VoteA%'),
        'Efficiency Gap':
        functools.partial(efficiency_gap, col1='VoteA', col2='VoteB'),
    }

    initial_scores = {
        key: score(initial_partition)
        for key, score in scores.items()
    }

    table = pipe_to_table(chain, scores)

    return {
        key: p_value_report(key, table[key], initial_scores[key])
        for key in scores
    }
Ejemplo n.º 3
0
def write_p_values(a, b, c, filename=''):
    initial_plans = a[0][0]
    output = [
        outputs.p_value_report(score, a[0][score], initial_plans[score])
        for score in a[2]
    ]
    with open(filename, "w") as f:
        f.write(json.dumps(output))
Ejemplo n.º 4
0
def test_p_value_report_gives_the_right_value_when_the_whole_ensemble_is_lower_than_initial(
):
    mock_ensemble_scores = [1] * 199 + [17] * 1
    initial_plan_score = 17
    # We have 199 scores < 17, and 1 score >= 17, so we have 1/200
    # higher than the initial score. This should give a p-value of
    # sqrt(2 * 1/200) = 1/10.

    result = p_value_report('Mock Score', mock_ensemble_scores,
                            initial_plan_score)

    assert result['fraction_as_high'] == 1 / 200
    assert result['p_value'] == 1 / 10
    assert result['opposite_p_value'] == math.sqrt(2 * 199 / 200)
Ejemplo n.º 5
0
                      scores,
                      display=True,
                      number_to_display=number_to_display,
                      bin_interval=bin_interval)

print(steps, " Steps in ", time.time() - start_time, " Seconds")

pscores = dict(scores)

pscores.pop("Node Flipped:")
pscores.pop("Flipped to:")
pscores.pop("All Flips:")

# P-value reports
pv_dict = {
    key: p_value_report(key, table[key], initial_scores[key])
    for key in pscores
}
# print(pv_dict)
with open(newdir + 'pvals_report_multi.json', 'w') as fp:
    json.dump(pv_dict, fp)

print("computed p-values")

# Histogram and trace plotting paths
hist_path = newdir + "chain_histogram_multi_"
trace_path = newdir + "chain_traces_multi_"

# Plots for each election

for i in range(num_elections):
table = pipe_to_table(chain, scores, display=True, number_to_display=number_to_display,
                      bin_interval=bin_interval)

					  
					  					  
print(steps," Steps in ", time.time()-start_time," Seconds")

pscores = dict(scores)

pscores.pop("Node Flipped:")
pscores.pop("Flipped to:")
pscores.pop("All Flips:")


# P-value reports
pv_dict = {key: p_value_report(key, table[key], initial_scores[key]) for key in pscores}
# print(pv_dict)
with open(newdir + 'pvals_report_multi.json', 'w') as fp:
    json.dump(pv_dict, fp)

print("computed p-values")


# Histogram and trace plotting paths
hist_path = newdir + "chain_histogram_multi_"
trace_path = newdir + "chain_traces_multi_"


# Plots for each election

for i in range(num_elections):