Political science appointment and analysis in Python
Project description
Political science appointment and analysis in Python
Jump to: Appointment • Examples • To-Do
poli-sci-kit is a Python package for politcal science appointment and election analysis. The goal is to provide a comprehensive tool for all methods needed to analyze and simulate election results.
Installation via PyPi
pip install poli-sci-kit
import poli_sci_kit
Appointment
appointment.methods includes functions to allocate parliamentary seats based on population or vote shares. Arguments to allow allocation threshholds, minimum allocations per group, tie break conditions, and other election features are also provided. Along with deriving results for visualization and reporting, these functions allow the user to analyze outcomes given systematic or situational changes. The appointment.metrics module further provides diagnostics to analyze the results of elections, apportionments, and other politcal science scenarios.
A basic example of political appointment using poli-sci-kit is:
from poli_sci_kit import appointment
vote_counts = [2700, 900, 3300, 1300, 2150, 500]
seats_to_allocate = 50
# Huntington-Hill is the method used to allocate House of Represenatives seats to US states
ha_allocations = appointment.methods.highest_average(averaging_style='Huntington-Hill',
shares=vote_counts,
total_alloc=seats_to_allocate,
alloc_threshold=None,
min_alloc=1,
tie_break = 'majority',
majority_bonus=False,
modifier=None)
ha_allocations
# [26, 9, 37, 12, 23, 5]
# The Gallagher method is a measure of absolute difference similar to summing square residuals
disproportionality = appointment.metrics.dispr_index(shares=vote_counts,
allocations=ha_allocations,
mertric_type='Gallagher')
disproportionality
# 0.01002
Let's visualize the results with stdviz:
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
import stdviz
# German political parties
parties = ['CDU/CSU', 'FDP', 'Greens', 'Die Linke', 'SPD', 'AfD']
party_colors = ['#000000', '#ffed00', '#64a12d', '#be3075', '#eb001f', '#009ee0']
ax = stdviz.plot.bar(counts=ha_allocations, names=parties,
faction_names=None, colors=party_colors,
horizontal=False, stacked=False,
label_bars=True, axis=None)
# Initialize empty handles and labels
handles, labels = stdviz.plot.legend.gen_elements()
# Add a majority line
ax.axhline(int(sum(ha_allocations)/2)+1, ls='--', color='black')
handles.insert(0, Line2D([0], [0], linestyle='--', color='black'))
labels.insert(0, 'Majority: {} seats'.format(int(sum(ha_allocations)/2)+1))
ax.legend(handles=handles, labels=labels,
title='Bundestag: {} seats'.format(sum(ha_allocations)),
loc='upper left', bbox_to_anchor=(0, 0.9),
title_fontsize=20, fontsize=15,
frameon=True, facecolor='#FFFFFF', framealpha=1)
ax.set_ylabel('Seats', fontsize=15)
ax.set_xlabel('Party', fontsize=15)
plt.show()
ax = stdviz.plot.parliament(seat_counts=ha_allocations,
names=parties, colors=party_colors,
style='semicircle', num_rows=4, marker_size=175,
speaker=False, df_seat_lctns=None, axis=ax2)
plt.show()
Examples
Examples in poli-sci-kit use publically available Wikidata statistics sourced via the Python package wikirepo. Current examples include:
-
- Allocates seats to a version of the US House of Representatives that includes all US territories and Washington DC given census data, with this further being used to derive relative vote strengths of state citizens in the US presidential election
-
- Analyzes the allocation of seats in a hypothetical global parliament given the prevalance of certain counties and organizations, the distribution of seats based on Freedom House indexes, and disproportionality metrics
To-Do
- Checks for [appointment.methods] implementations
- Deriving further needed arguments to assure that all current and historic appointment systems can be simulated using poli-sci-kit
- Potentially indexing preset versions of appointment.methods that coincide with the systems used by governments around the world
- This would allow quick comparisons of actual systems with variations
- Creating, improving and sharing examples
- Testing for poli-sci-kit
References
Full list of references
- https://github.com/crflynn/voting
- https://blogs.reading.ac.uk/readingpolitics/2015/06/29/electoral-disproportionality-what-is-it-and-how-should-we-measure-it/
- Balinski, M. L., and Young, H. P. (1982). Fair Representation: Meeting the Ideal of One Man, One Vote. New Haven, London: Yale University Press.
- Karpov, A. (2008). "Measurement of disproportionality in proportional representation systems". Mathematical and Computer Modelling, Vol. 48, pp. 1421-1438. URL: https://www.sciencedirect.com/science/article/pii/S0895717708001933.
- Kohler, U., and Zeh, J. (2012). “Apportionment methods”. The Stata Journal, Vol. 12, No. 3, pp. 375–392. URL: https://journals.sagepub.com/doi/pdf/10.1177/1536867X1201200303.
- Taagepera, R., and Grofman, B. (2003). "Mapping the Indices of Seats-Votes Disproportionality and Inter-Election Volatility". Party Politics, Vol. 9, No. 6, pp. 659–677. URL: https://escholarship.org/content/qt0m9912ff/qt0m9912ff.pdf.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Hashes for poli_sci_kit-0.0.2.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 54ed94afac5d672512b71dfe59fe16558a1672f3ff7400053c10956059ecc839 |
|
MD5 | 48a3b6ecf087360a35429a9fa009e426 |
|
BLAKE2b-256 | ad87037792c0baf5a86f9f8a66c9fcf6130827b4b33059cc86ec7d9a9c77ff9d |