def chain(iterations): idef = random.randint(1, 10000) graph = Graph.from_json("../data/PA_init/PA_VTD.json") election = Election("SEN12", {"Dem": "USS12D", "Rep": "USS12R"}) initial_partition = GeographicPartition( graph, assignment="2011_PLA_1", updaters={ "cut_edges": cut_edges, "population": Tally("TOT_POP", alias="population"), "SEN12": election } ) ideal_population = sum(initial_partition["population"].values()) / len(initial_partition) # We use functools.partial to bind the extra parameters (pop_col, pop_target, epsilon, node_repeats) # of the recom proposal. proposal = partial(recom, pop_col="TOT_POP", pop_target=ideal_population, epsilon=0.02, node_repeats=2 ) chain = MarkovChain( proposal=proposal, constraints=[], accept=contiguous, initial_state=initial_partition, total_steps=85*iterations + 17000 ) count = 0 metrics = [] boundary_nodes = [] boundary_weighted = [] for partition in chain.with_progress_bar(): mm = mean_median(partition["SEN12"]) p = pp(partition) bias = partisan_bias(partition["SEN12"]) gini = partisan_gini(partition["SEN12"]) gap = efficiency_gap(partition["SEN12"]) cut = len(partition["cut_edges"]) if count >= 17000: if count % 85 == 0: metrics.append((mm, p, bias, gini, gap, cut, partition["SEN12"].wins("Rep"))) nodes = [0]*8921 bnodes = [0]*8921 for edge in partition["cut_edges"]: nodes[edge[0]] = 1 nodes[edge[1]] = 1 bnodes[edge[0]] += 1 bnodes[edge[1]] += 1 boundary_nodes.append(nodes) boundary_weighted.append(bnodes) assign = {i: partition.assignment[i] for i in partition.assignment} shape["CD"] = shape.index.map(assign) this_map = shape.dissolve(by='CD') this_map.plot(color='white', edgecolor='black') plt.axis('off') fig = plt.gcf() fig.set_size_inches((15,9), forward=False) fig.savefig("../images/PA_neutral/" + str(idef) + str(int((count-17000)/85)+1) + ".png", dpi=600, bbox_inches="tight", pad_inches=0) plt.close() if count % 8500 == 0: print(idef, count, mm, p, bias, gini, gap, cut, partition["SEN12"].wins("Rep")) else: if count%1000 == 0: print(idef, "Mixing...... Iteration", count, "/17000") count += 1 return metrics, boundary_nodes, boundary_weighted, idef
def chain(iterations): idef = random.randint(1, 10000) graph = Graph.from_json("./PA_VTD.json") election = Election("SEN12", {"Dem": "USS12D", "Rep": "USS12R"}) initial_partition = GeographicPartition(graph, assignment="2011_PLA_1", updaters={ "cut_edges": cut_edges, "population": Tally("TOT_POP", alias="population"), "SEN12": election }) ideal_population = sum( initial_partition["population"].values()) / len(initial_partition) # We use functools.partial to bind the extra parameters (pop_col, pop_target, epsilon, node_repeats) # of the recom proposal. proposal = partial(recom, pop_col="TOT_POP", pop_target=ideal_population, epsilon=0.02, node_repeats=2) chain = MarkovChain(proposal=proposal, constraints=[republican_constraint], accept=contiguous, initial_state=initial_partition, total_steps=iterations + 100) count = 0 metrics = [] boundary_nodes = [] boundary_weighted = [] for partition in chain.with_progress_bar(): mm = mean_median(partition["SEN12"]) p = pp(partition) bias = partisan_bias(partition["SEN12"]) gini = partisan_gini(partition["SEN12"]) gap = efficiency_gap(partition["SEN12"]) cut = len(partition["cut_edges"]) if count >= 100: metrics.append((mm, p, bias, gini, gap, cut)) nodes = [0] * 8921 bnodes = [0] * 8921 for edge in partition["cut_edges"]: nodes[edge[0]] = 1 nodes[edge[1]] = 1 bnodes[edge[0]] += 1 bnodes[edge[1]] += 1 boundary_nodes.append(nodes) boundary_weighted.append(bnodes) if count % 100 == 0: print(idef, count, mm, p, bias, gini, gap, cut, partition["SEN12"].wins("Rep")) count += 1 return metrics, boundary_nodes, boundary_weighted
pop_target=ideal_population, epsilon=0.03, node_repeats=2) compactness_bound = constraints.UpperBound( lambda p: len(p["cut_edges"]), 2 * len(initial_partition["cut_edges"])) pop_constraint = constraints.within_percent_of_ideal_population( initial_partition, 0.03) chain1 = MarkovChain(proposal=proposal, constraints=[pop_constraint, compactness_bound], accept=accept.always_accept, initial_state=initial_partition, total_steps=50000) data = pd.DataFrame(partition["SEN12"].mean_median() for partition in chain.with_progress_bar()) temp = [partition["PRES16"].mean_median() for partition in chain] temp1 = [partition["PRES16"].efficiency_gap() for partition in chain] plt.hist(temp, bins=200) count = 0 for partition in chain1: count += 1 if count < 10: print(count) partition.plot() partition['PRES16'].mean_median() fig, ax = plt.subplots(figsize=(8, 6))