Giter VIP home page Giter VIP logo

gerrymandertests's Introduction

Metrics for quantifying gerrymandering

PyPI version

This repository contains:

  1. Python code for implementing a number of metrics for quantifying gerrymandering9:
    • Mean-median difference and variant:
      • Mean-median difference1,2
      • Equal vote weight2
    • Lopsided margins (two-sample t-test on win margins)1
    • Bootstrap (Monte Carlo) simulation1
    • Mann-Whitney U test
    • Declination variants3
      • Declination
      • Declination (buffered)
      • Declination variant
      • Declination variant (buffered)
    • Efficiency gap variants
      • Efficiency gap4
      • Difference gap5,6,7
      • Loss gap7
      • Surplus gap8
      • Vote-centric gap6,7
      • Vote-centric gap 26,7
      • Tau gap3
    • Partisan bias6,7
  2. Historical election results:
    • Congressional elections, 1948–2016 (CSV)
    • State legislative elections (lower house), 1971–2017 (CSV, full repository)
  3. Jupyter notebook demonstrating how to run the tests on all elections, as well as reporting the percentile ranking for all tests of any particular election.

Installation

If using pip, do pip install gerrymetrics

References

  1. Samuel S.-H. Wang. (2016). Three Tests for Practical Evaluation of Partisan Gerrymandering. Stanford Law Review.
  2. Michael D. McDonald and Robin E. Best. (2015). Unfair Partisan Gerrymanders in Politics and Law: A Diagnostic Applied to Six Cases. Election Law Journal.
  3. Gregory S. Warrington. (2018). Quantifying Gerrymandering Using the Vote Distribution. Election Law Journal.
  4. Eric McGhee. (2014). Measuring Partisan Bias in Single‐Member District Electoral Systems. Legislative Studies Quarterly.
  5. Whitford v. Gill, No. 15-cv-421, F. Supp. 3d. (2016). Griesbach, dissenting, 128.
  6. Benjamin P. Cover. (2018). Quantifying Partisan Gerrymandering: An Evaluation of the Efficiency Gap Proposal. Stanford Law Review.
  7. John F. Nagle. (2017). How Competitive Should a Fair Single Member Districting Plan Be? Election Law Journal.
  8. Wendy K. Tam Cho. (2018). Measuring Partisan Fairness: How Well Does the Efficiency Gap Guard Against Sophisticated as well as Simple-Minded Modes of Partisan Discrimination? University of Pennsylvania Law Review.
  9. Gregory S. Warrington. (2018). A Comparison of Gerrymandering Metrics. arXiv.

gerrymandertests's People

Contributors

baxterdemers avatar hjohns12 avatar rmw2 avatar wtadler avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gerrymandertests's Issues

TypeError when using g.bootstrap

I'm trying to use Wang's 3 tests. However, when trying to use Test 1 (the excess seats test), I get the following errors:
KeyError: 'bootstrap'
TypeError: object of type 'numpy.float64' has no len()

Code to replicate the error:

impute_val = 1
chambers = defaultdict(lambda: defaultdict(list))
chambers['Congressional']['filepath'] = 'election_data/congressional_election_results_post1948.csv'

metric_dict = {'bootstrap': g.bootstrap,
               't_test_diff':            g.t_test_diff,
               'mean_median_diff':       g.mean_median}
for chamber in chambers:
    chambers[chamber]['elections_df'] = g.parse_results(chambers[chamber]['filepath'])
    chambers[chamber]['tests_df'] = g.tests_df(g.run_all_tests(
        chambers[chamber]['elections_df'],
        impute_val=impute_val,
        metrics=metric_dict))

I'm using Python 3.6.
I guess the error may come from this part of the function 'bootstrap', but I'm not sure how to solve it:
n_matches = len(match_seats)

Is it also possible that I'm using the function incorrectly. I would appreciate any help!

Update: I also got the same error when using g.t_test and g.mean_median_test.

First Notebook example doesn't work: apparently expects a state data file to already be there?

I'm trying to run the gerrymandertests, but apparently it relies on my separately downloading state-specific files (I'm particularly interested in New Mexico) and I can't find any documentation on where to get them.

If I just run the notebook, here's the error:

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-1-54dcfe840d25> in <module>
     41 
     42 for chamber in chambers:
---> 43     chambers[chamber]['elections_df'] = g.parse_results(chambers[chamber]['filepath'])
     44     chambers[chamber]['tests_df'] = g.tests_df(g.run_all_tests(
     45         chambers[chamber]['elections_df'],

~/outsrc/gerrymandertests/gerrymetrics/utils.py in parse_results(input_filepath, start_year, coerce_odd_years)
     12     '''
     13 
---> 14     df = pd.read_csv(input_filepath)
     15 
     16     df = df[df['Year'] >= start_year]

~/pythonenv/gerry/lib/python3.7/site-packages/pandas/io/parsers.py in parser_f(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, dialect, error_bad_lines, warn_bad_lines, delim_whitespace, low_memory, memory_map, float_precision)
    674         )
    675 
--> 676         return _read(filepath_or_buffer, kwds)
    677 
    678     parser_f.__name__ = name

~/pythonenv/gerry/lib/python3.7/site-packages/pandas/io/parsers.py in _read(filepath_or_buffer, kwds)
    446 
    447     # Create the parser.
--> 448     parser = TextFileReader(fp_or_buf, **kwds)
    449 
    450     if chunksize or iterator:

~/pythonenv/gerry/lib/python3.7/site-packages/pandas/io/parsers.py in __init__(self, f, engine, **kwds)
    878             self.options["has_index_names"] = kwds["has_index_names"]
    879 
--> 880         self._make_engine(self.engine)
    881 
    882     def close(self):

~/pythonenv/gerry/lib/python3.7/site-packages/pandas/io/parsers.py in _make_engine(self, engine)
   1112     def _make_engine(self, engine="c"):
   1113         if engine == "c":
-> 1114             self._engine = CParserWrapper(self.f, **self.options)
   1115         else:
   1116             if engine == "python":

~/pythonenv/gerry/lib/python3.7/site-packages/pandas/io/parsers.py in __init__(self, src, **kwds)
   1889         kwds["usecols"] = self.usecols
   1890 
-> 1891         self._reader = parsers.TextReader(src, **kwds)
   1892         self.unnamed_cols = self._reader.unnamed_cols
   1893 

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader.__cinit__()

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._setup_parser_source()

FileNotFoundError: [Errno 2] File election_data/state_legislative/state_legislative_election_results_post1971.csv does not exist: 'election_data/state_legislative/state_legislative_election_results_post1971.csv'

election_data/congressional_election_results_post1948.csv comes as part of the repository, but election_data/state_legislative/ is an empty directory. Where can I get the files that it expected there?

In NM we're actively fighting for better redistricting (I'm webmaster for fairdistrictsnm.org) and I'd love to get some quantitative measurements I could show to legislators and display on the website.

Need to use older Pandas version

This package uses deprecated features from Pandas that are no longer available in Pandas version 1.0 or higher. Accordingly, one should use a virtual environment to install an older version of pandas e.g. pandas==0.25.1 until this issues.

Creating classes

I've been playing with the methods in the metrics.py file and have found myself mimicking the functionality in new classes. For example, I've replaced use of _stats in metrics.py with a class I call VoteShares:

Screen Shot 2020-04-15 at 12 07 15 AM

Screen Shot 2020-04-15 at 12 06 36 AM

I had hoped to put up an explanatory PR, but I'm not allowed to make pull requests. Would you have interest in adding some of these classes to your repo? They make some use of the methods a bit more concise e.g.
mwu: MannWhitneyU = MannWhitneyU([.1, .2, .3, .4, .5, .6, .7, .8, .9, 1.0])
mwu.get_statistic()
mwu.get_p_value()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.