https://github.com/andrewtavis/poli-sci-kit
Political elections, appointment, analysis and visualization in Python
Science Score: 49.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
✓DOI references
Found 1 DOI reference(s) in README -
✓Academic publication links
Links to: sciencedirect.com -
○Committers with academic emails
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (15.2%) to scientific vocabulary
Keywords
Keywords from Contributors
Repository
Political elections, appointment, analysis and visualization in Python
Basic Info
Statistics
- Stars: 30
- Watchers: 3
- Forks: 7
- Open Issues: 3
- Releases: 5
Topics
Metadata Files
README.md
Political elections, appointment, analysis and visualization in Python
poli-sci-kit is a Python package for political science appointment and election analysis. The goal is to provide a comprehensive tool for all methods needed to analyze and simulate election results. See the documentation for a full outline of the package including algorithms and visualization techniques.
Contents
Installation ⇧
poli-sci-kit can be downloaded from PyPI via pip or sourced directly from this repository:
bash
pip install poli-sci-kit
bash
git clone https://github.com/andrewtavis/poli-sci-kit.git
cd poli-sci-kit
python setup.py install
python
import poli_sci_kit
Appointment ⇧
appointment.methods includes functions to allocate parliamentary seats based on population or vote shares. Included methods are:
Largest Remainder: Hare, Droop, Hagenbach–Bischoff (incl Hamilton, Vinton, Hare–Niemeyer)
Highest Averages: Jefferson, Webster, Huntington-Hill
Arguments to allow allocation thresholds, 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 political science scenarios.
A basic example of political appointment using poli-sci-kit is:
```python from poliscikit import appointment
votecounts = [2700, 900, 3300, 1300, 2150, 500] seatsto_allocate = 50
Huntington-Hill is the method used to allocate House of Representatives seats to US states
haallocations = appointment.methods.highestaverages( averagingstyle="Huntington-Hill", shares=votecounts, totalalloc=seatstoallocate, allocthreshold=None, minalloc=1, tiebreak="majority", majority_bonus=False, modifier=None, )
ha_allocations
[26, 9, 37, 12, 23, 5]
```
We can then compute various metrics to derive disproportionality:
```python
The Gallagher method is a measure of absolute difference similar to summing square residuals
disproportionality = appointment.metrics.disprindex( shares=votecounts, allocations=haallocations, metrictype='Gallagher' )
disproportionality
0.01002
```
We can also check that the allocations pass the quota condition:
```python passesqc = appointment.checks.quotacondition( shares=votecounts, seats=haallocations )
passes_qc
True
```
Allocation consistency can further be checked using dataframes of shares and seats given electoral settings. See appointment.checks and the documentation for explanations of method checks.
Plotting ⇧
poli-sci-kit provides Python only implementations of common electoral plots.
Visualizing the above results:
```python import matplotlib.pyplot as plt import poliscikit
German political parties
parties = ['CDU/CSU', 'FDP', 'Greens', 'Die Linke', 'SPD', 'AfD'] party_colors = ['#000000', '#ffed00', '#64a12d', '#be3075', '#eb001f', '#009ee0'] ```
Parliament Plots ⇧
poliscikit provides implementations of both rectangular and semicircle parliament plots:
```python fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2)
ax1 = poliscikit.plot.parliament( allocations=seatallocations, labels=parties, colors=partycolors, style="rectangle", numrows=4, markersize=300, speaker=True, axis=ax1, )
ax2 = poliscikit.plot.parliament( allocations=seatallocations, labels=parties, colors=partycolors, style="semicircle", numrows=4, markersize=175, speaker=False, axis=ax2, )
plt.show() ```
Disproportionality Bar Plot ⇧
A novel addition to social science analysis is the disproportionality bar plot, which graphically depicts the disproportionality between expected and realized results. Bar widths are the proportion of shares (ex: votes received), and heights are the difference or relative difference between shares and allocations (ex: parliament seats received).
An example follows:
```python import pltviz
ax = poliscikit.plot.disprbar( shares=votes, allocations=haallocations, labels=parties, colors=partycolors, totalshares=None, total_alloc=None, percent=True, axis=None, )
handles, labels = pltviz.plot.legend.genelements( counts=[round(v / sum(votes), 4) for v in votes], labels=parties, colors=partycolors, size=11, marker="o", padding_indexes=None, order=None, )
ax.legend( handles=handles, labels=labels, title="Vote Percents (bar widths)", titlefontsize=15, fontsize=11, ncol=2, loc="upper left", bboxto_anchor=(0, 1), frameon=True, facecolor="#FFFFFF", framealpha=1, )
ax.axes.settitle('Seat to Vote Share Disproportionality', fontsize=30) ax.setxlabel('Parties', fontsize=20) ax.set_ylabel('Percent Shift', fontsize=20)
plt.show() ```
Examples ⇧
Examples in poli-sci-kit use publicly 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
Global Parliament (Open in Colab)
- Analyzes the allocation of seats in a hypothetical global parliament given the prevalence of certain countries and organizations, the distribution of seats based on Freedom House indexes, as well as disproportionality metrics
To-Do ⇧
Please see the contribution guidelines if you are interested in contributing to this project. Work that is in progress or could be implemented includes:
Adding the Adams method to appointment.methods.highest_averages (see issue)
Deriving further needed arguments to assure that all current and historic appointment systems can be simulated using poli-sci-kit (see issue)
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
Adding methods such as quadratic voting to poli-sci-kit to allow for preference based simulations
Creating, improving and sharing examples
Improving tests for greater code coverage
References
Full list of references
- [voting](https://github.com/crflynn/voting) by [crflynn](https://github.com/crflynn) ([License](https://github.com/crflynn/voting/blob/master/LICENSE.txt)) - 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.
Owner
- Name: Andrew Tavis McAllister
- Login: andrewtavis
- Kind: user
- Location: Berlin, Germany
- Company: @activist-org, @scribe-org
- Repositories: 15
- Profile: https://github.com/andrewtavis
Data, development and design. Initiator @activist-org and @scribe-org.
GitHub Events
Total
- Watch event: 7
- Fork event: 1
Last Year
- Watch event: 7
- Fork event: 1
Committers
Last synced: almost 3 years ago
All Time
- Total Commits: 245
- Total Committers: 3
- Avg Commits per committer: 81.667
- Development Distribution Score (DDS): 0.008
Top Committers
| Name | Commits | |
|---|---|---|
| Andrew Tavis | a****r@g****m | 243 |
| ImgBotApp | I****p@g****m | 1 |
| dependabot[bot] | 4****]@u****m | 1 |
Issues and Pull Requests
Last synced: 6 months ago
All Time
- Total issues: 7
- Total pull requests: 24
- Average time to close issues: 9 months
- Average time to close pull requests: about 21 hours
- Total issue authors: 2
- Total pull request authors: 4
- Average comments per issue: 3.43
- Average comments per pull request: 1.04
- Merged pull requests: 23
- Bot issues: 0
- Bot pull requests: 3
Past Year
- Issues: 0
- Pull requests: 0
- Average time to close issues: N/A
- Average time to close pull requests: N/A
- Issue authors: 0
- Pull request authors: 0
- Average comments per issue: 0
- Average comments per pull request: 0
- Merged pull requests: 0
- Bot issues: 0
- Bot pull requests: 0
Top Authors
Issue Authors
- andrewtavis (6)
- gnegrelli (1)
Pull Request Authors
- andrewtavis (20)
- dependabot[bot] (2)
- gnegrelli (1)
- imgbot[bot] (1)
Top Labels
Issue Labels
Pull Request Labels
Packages
- Total packages: 1
-
Total downloads:
- pypi 138 last-month
- Total dependent packages: 0
- Total dependent repositories: 1
- Total versions: 32
- Total maintainers: 1
pypi.org: poli-sci-kit
Political elections, appointment, analysis and visualization in Python
- Homepage: https://github.com/andrewtavis/poli-sci-kit
- Documentation: https://poli-sci-kit.readthedocs.io/
- License: new BSD
-
Latest release: 1.1.0
published about 2 years ago
Rankings
Maintainers (1)
Dependencies
- m2r2 *
- numpydoc *
- black >=19.10b0
- certifi >=2020.12.5
- colormath >=3.0.0
- numpy >=1.19.2
- packaging >=20.9
- pandas >=1.2.1
- pyOpenSSL >=20.0.1
- pytest >=6.2.2
- pytest-cov >=2.11.1
- scipy >=1.5.2
- seaborn >=0.11.1
- actions/checkout v2 composite
- actions/setup-python main composite
- codecov/codecov-action v2 composite
- conda-incubator/setup-miniconda v2 composite
- black >=19.10b0
- numpy >=1.19.2
- packaging >=20.9
- pandas >=1.2.1
- pyopenssl >=20.0.1
- pytest >=6.2.2
- pytest-cov >=2.11.1
- scipy >=1.5.2
- seaborn >=0.11.1
