pref_voting 1.16.31__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pref_voting-1.16.31/LICENSE.txt +21 -0
- pref_voting-1.16.31/PKG-INFO +208 -0
- pref_voting-1.16.31/README.md +174 -0
- pref_voting-1.16.31/pref_voting/__init__.py +1 -0
- pref_voting-1.16.31/pref_voting/analysis.py +496 -0
- pref_voting-1.16.31/pref_voting/axiom.py +38 -0
- pref_voting-1.16.31/pref_voting/axiom_helpers.py +129 -0
- pref_voting-1.16.31/pref_voting/axioms.py +10 -0
- pref_voting-1.16.31/pref_voting/c1_methods.py +963 -0
- pref_voting-1.16.31/pref_voting/combined_methods.py +514 -0
- pref_voting-1.16.31/pref_voting/create_methods.py +128 -0
- pref_voting-1.16.31/pref_voting/data/examples/condorcet_winner/minimal_Anti-Plurality.soc +16 -0
- pref_voting-1.16.31/pref_voting/data/examples/condorcet_winner/minimal_Borda.soc +17 -0
- pref_voting-1.16.31/pref_voting/data/examples/condorcet_winner/minimal_Bracket_Voting.soc +20 -0
- pref_voting-1.16.31/pref_voting/data/examples/condorcet_winner/minimal_Bucklin.soc +19 -0
- pref_voting-1.16.31/pref_voting/data/examples/condorcet_winner/minimal_Coombs.soc +20 -0
- pref_voting-1.16.31/pref_voting/data/examples/condorcet_winner/minimal_Coombs_PUT.soc +20 -0
- pref_voting-1.16.31/pref_voting/data/examples/condorcet_winner/minimal_Coombs_TB.soc +20 -0
- pref_voting-1.16.31/pref_voting/data/examples/condorcet_winner/minimal_Dowdall.soc +19 -0
- pref_voting-1.16.31/pref_voting/data/examples/condorcet_winner/minimal_Instant_Runoff.soc +18 -0
- pref_voting-1.16.31/pref_voting/data/examples/condorcet_winner/minimal_Instant_Runoff_PUT.soc +18 -0
- pref_voting-1.16.31/pref_voting/data/examples/condorcet_winner/minimal_Instant_Runoff_TB.soc +18 -0
- pref_voting-1.16.31/pref_voting/data/examples/condorcet_winner/minimal_Iterated_Removal_Condorcet_Loser.soc +17 -0
- pref_voting-1.16.31/pref_voting/data/examples/condorcet_winner/minimal_Pareto.soc +17 -0
- pref_voting-1.16.31/pref_voting/data/examples/condorcet_winner/minimal_Plurality.soc +18 -0
- pref_voting-1.16.31/pref_voting/data/examples/condorcet_winner/minimal_PluralityWRunoff_PUT.soc +18 -0
- pref_voting-1.16.31/pref_voting/data/examples/condorcet_winner/minimal_Positive-Negative_Voting.soc +17 -0
- pref_voting-1.16.31/pref_voting/data/examples/condorcet_winner/minimal_Simplified_Bucklin.soc +18 -0
- pref_voting-1.16.31/pref_voting/data/examples/condorcet_winner/minimal_Superior_Voting.soc +19 -0
- pref_voting-1.16.31/pref_voting/data/examples/condorcet_winner/minimal_Weighted_Bucklin.soc +19 -0
- pref_voting-1.16.31/pref_voting/data/examples/condorcet_winner/minimal_resolute_Anti-Plurality.soc +17 -0
- pref_voting-1.16.31/pref_voting/data/examples/condorcet_winner/minimal_resolute_Borda.soc +17 -0
- pref_voting-1.16.31/pref_voting/data/examples/condorcet_winner/minimal_resolute_Bracket_Voting.soc +20 -0
- pref_voting-1.16.31/pref_voting/data/examples/condorcet_winner/minimal_resolute_Bucklin.soc +19 -0
- pref_voting-1.16.31/pref_voting/data/examples/condorcet_winner/minimal_resolute_Coombs.soc +21 -0
- pref_voting-1.16.31/pref_voting/data/examples/condorcet_winner/minimal_resolute_Coombs_PUT.soc +21 -0
- pref_voting-1.16.31/pref_voting/data/examples/condorcet_winner/minimal_resolute_Coombs_TB.soc +20 -0
- pref_voting-1.16.31/pref_voting/data/examples/condorcet_winner/minimal_resolute_Dowdall.soc +18 -0
- pref_voting-1.16.31/pref_voting/data/examples/condorcet_winner/minimal_resolute_Instant_Runoff.soc +18 -0
- pref_voting-1.16.31/pref_voting/data/examples/condorcet_winner/minimal_resolute_Instant_Runoff_PUT.soc +18 -0
- pref_voting-1.16.31/pref_voting/data/examples/condorcet_winner/minimal_resolute_Instant_Runoff_TB.soc +18 -0
- pref_voting-1.16.31/pref_voting/data/examples/condorcet_winner/minimal_resolute_Plurality.soc +18 -0
- pref_voting-1.16.31/pref_voting/data/examples/condorcet_winner/minimal_resolute_PluralityWRunoff_PUT.soc +18 -0
- pref_voting-1.16.31/pref_voting/data/examples/condorcet_winner/minimal_resolute_Positive-Negative_Voting.soc +17 -0
- pref_voting-1.16.31/pref_voting/data/examples/condorcet_winner/minimal_resolute_Simplified_Bucklin.soc +20 -0
- pref_voting-1.16.31/pref_voting/data/examples/condorcet_winner/minimal_resolute_Weighted_Bucklin.soc +19 -0
- pref_voting-1.16.31/pref_voting/data/voting_methods_properties.json +414 -0
- pref_voting-1.16.31/pref_voting/data/voting_methods_properties.json.lock +0 -0
- pref_voting-1.16.31/pref_voting/dominance_axioms.py +387 -0
- pref_voting-1.16.31/pref_voting/generate_profiles.py +801 -0
- pref_voting-1.16.31/pref_voting/generate_spatial_profiles.py +198 -0
- pref_voting-1.16.31/pref_voting/generate_utility_profiles.py +160 -0
- pref_voting-1.16.31/pref_voting/generate_weighted_majority_graphs.py +506 -0
- pref_voting-1.16.31/pref_voting/grade_methods.py +184 -0
- pref_voting-1.16.31/pref_voting/grade_profiles.py +357 -0
- pref_voting-1.16.31/pref_voting/helper.py +370 -0
- pref_voting-1.16.31/pref_voting/invariance_axioms.py +671 -0
- pref_voting-1.16.31/pref_voting/io/__init__.py +0 -0
- pref_voting-1.16.31/pref_voting/io/readers.py +432 -0
- pref_voting-1.16.31/pref_voting/io/writers.py +256 -0
- pref_voting-1.16.31/pref_voting/iterative_methods.py +2425 -0
- pref_voting-1.16.31/pref_voting/maj_graph_ex1.png +0 -0
- pref_voting-1.16.31/pref_voting/mappings.py +577 -0
- pref_voting-1.16.31/pref_voting/margin_based_methods.py +2345 -0
- pref_voting-1.16.31/pref_voting/monotonicity_axioms.py +872 -0
- pref_voting-1.16.31/pref_voting/num_evaluation_method.py +77 -0
- pref_voting-1.16.31/pref_voting/other_axioms.py +161 -0
- pref_voting-1.16.31/pref_voting/other_methods.py +939 -0
- pref_voting-1.16.31/pref_voting/pairwise_profiles.py +547 -0
- pref_voting-1.16.31/pref_voting/prob_voting_method.py +105 -0
- pref_voting-1.16.31/pref_voting/probabilistic_methods.py +287 -0
- pref_voting-1.16.31/pref_voting/profiles.py +856 -0
- pref_voting-1.16.31/pref_voting/profiles_with_ties.py +1069 -0
- pref_voting-1.16.31/pref_voting/rankings.py +466 -0
- pref_voting-1.16.31/pref_voting/scoring_methods.py +481 -0
- pref_voting-1.16.31/pref_voting/social_welfare_function.py +59 -0
- pref_voting-1.16.31/pref_voting/social_welfare_functions.py +7 -0
- pref_voting-1.16.31/pref_voting/spatial_profiles.py +448 -0
- pref_voting-1.16.31/pref_voting/stochastic_methods.py +99 -0
- pref_voting-1.16.31/pref_voting/strategic_axioms.py +1394 -0
- pref_voting-1.16.31/pref_voting/swf_axioms.py +173 -0
- pref_voting-1.16.31/pref_voting/utility_functions.py +102 -0
- pref_voting-1.16.31/pref_voting/utility_methods.py +178 -0
- pref_voting-1.16.31/pref_voting/utility_profiles.py +333 -0
- pref_voting-1.16.31/pref_voting/variable_candidate_axioms.py +640 -0
- pref_voting-1.16.31/pref_voting/variable_voter_axioms.py +3747 -0
- pref_voting-1.16.31/pref_voting/voting_method.py +355 -0
- pref_voting-1.16.31/pref_voting/voting_method_properties.py +92 -0
- pref_voting-1.16.31/pref_voting/voting_methods.py +8 -0
- pref_voting-1.16.31/pref_voting/voting_methods_registry.py +136 -0
- pref_voting-1.16.31/pref_voting/weighted_majority_graphs.py +1539 -0
- pref_voting-1.16.31/pyproject.toml +34 -0
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2024 Wes Holliday and Eric Pacuit
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
@@ -0,0 +1,208 @@
|
|
1
|
+
Metadata-Version: 2.4
|
2
|
+
Name: pref_voting
|
3
|
+
Version: 1.16.31
|
4
|
+
Summary: pref_voting is a Python package that contains tools to reason about elections and margin graphs, and implementations of voting methods.
|
5
|
+
License: MIT
|
6
|
+
License-File: LICENSE.txt
|
7
|
+
Author: Eric Pacuit
|
8
|
+
Author-email: epacuit@umd.edu
|
9
|
+
Requires-Python: >=3.10,<4.0
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
12
|
+
Classifier: Programming Language :: Python :: 3.10
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
16
|
+
Classifier: Programming Language :: Python :: 3.14
|
17
|
+
Requires-Dist: filelock (>=3.12.2,<4.0.0)
|
18
|
+
Requires-Dist: matplotlib (>=3.5.2,<4.0.0)
|
19
|
+
Requires-Dist: nashpy (>=0.0.40,<0.0.41)
|
20
|
+
Requires-Dist: networkx (>=3.0,<4.0)
|
21
|
+
Requires-Dist: numba (>=0.61.0,<0.62.0)
|
22
|
+
Requires-Dist: ortools (>=9.8.0,<10.0.0)
|
23
|
+
Requires-Dist: pathos (>=0.3.3,<0.4.0)
|
24
|
+
Requires-Dist: preflibtools (>=2.0.22,<3.0.0)
|
25
|
+
Requires-Dist: prefsampling (>=0.1.16,<0.2.0)
|
26
|
+
Requires-Dist: random2 (>=1.0.1,<2.0.0)
|
27
|
+
Requires-Dist: scipy (>=1.0.0,<2.0.0)
|
28
|
+
Requires-Dist: seaborn (>=0.13.2,<0.14.0)
|
29
|
+
Requires-Dist: tabulate (>=0.9.0,<0.10.0)
|
30
|
+
Project-URL: Homepage, https://github.com/voting-tools/pref_voting
|
31
|
+
Project-URL: Repository, https://github.com/voting-tools/pref_voting
|
32
|
+
Description-Content-Type: text/markdown
|
33
|
+
|
34
|
+
pref_voting
|
35
|
+
==========
|
36
|
+
[](https://doi.org/10.21105/joss.07020) [](https://doi.org/10.5281/zenodo.14675583)
|
37
|
+
|
38
|
+
[](https://github.com/voting-tools/pref_voting/actions/workflows/tests.yml)
|
39
|
+
|
40
|
+
|
41
|
+
> [!NOTE]
|
42
|
+
> - [**Documentation**](https://pref-voting.readthedocs.io/)
|
43
|
+
> - [**Installation**](https://pref-voting.readthedocs.io/en/latest/installation.html)
|
44
|
+
> - [**Example Notebooks**](https://github.com/voting-tools/pref_voting/tree/main/examples)
|
45
|
+
> - [**Example Elections**](https://github.com/voting-tools/election-analysis)
|
46
|
+
> - [**► pref_voting web app**](https://pref.tools/pref_voting/)
|
47
|
+
|
48
|
+
See the [COMSOC community page](https://comsoc-community.org/tools) for an overview of other software tools related to Computational Social Choice.
|
49
|
+
|
50
|
+
## Installation
|
51
|
+
|
52
|
+
The package can be installed using the ``pip3`` package manager:
|
53
|
+
|
54
|
+
```bash
|
55
|
+
pip3 install pref_voting
|
56
|
+
```
|
57
|
+
**Notes**:
|
58
|
+
* The package requires Python 3.10 or higher and has been tested on Python 3.12.
|
59
|
+
|
60
|
+
* Since the package uses Numba, refer to the [Numba documentation for the latest supported Python version](https://numba.readthedocs.io/en/stable/user/installing.html#version-support-information).
|
61
|
+
* If you have both Python 2 and Python 3 installed on your system, make sure to use ``pip3`` instead of pip to install packages for Python 3. Alternatively, you can use ``python3 -m pip`` to ensure you're using the correct version of pip. If you have modified your system's defaults or soft links, adjust accordingly.
|
62
|
+
|
63
|
+
See the [installation guide](https://pref-voting.readthedocs.io/en/latest/installation.html) for more detailed instructions.
|
64
|
+
|
65
|
+
## Example Usage
|
66
|
+
|
67
|
+
A profile (of linear orders over the candidates) is created by initializing a `Profile` class object. Simply provide a list of rankings (each ranking is a tuple of numbers) and a list giving the number of voters with each ranking:
|
68
|
+
|
69
|
+
```python
|
70
|
+
from pref_voting.profiles import Profile
|
71
|
+
|
72
|
+
rankings = [
|
73
|
+
(0, 1, 2, 3), # candidate 0 is ranked first, candidate 1 is ranked second, candidate 2 is ranked 3rd, and candidate 3 is ranked last.
|
74
|
+
(2, 3, 1, 0),
|
75
|
+
(3, 1, 2, 0),
|
76
|
+
(1, 2, 0, 3),
|
77
|
+
(1, 3, 2, 0)]
|
78
|
+
|
79
|
+
rcounts = [5, 3, 2, 4, 3] # 5 voters submitted the first ranking (0, 1, 2, 3), 3 voters submitted the second ranking, and so on.
|
80
|
+
|
81
|
+
prof = Profile(rankings, rcounts=rcounts)
|
82
|
+
|
83
|
+
prof.display() # display the profile
|
84
|
+
```
|
85
|
+
|
86
|
+
The function `generate_profile` is used to generate a profile for a given number of candidates and voters:
|
87
|
+
|
88
|
+
```python
|
89
|
+
from pref_voting.generate_profiles import generate_profile
|
90
|
+
|
91
|
+
# generate a profile using the Impartial Culture probability model
|
92
|
+
prof = generate_profile(3, 4) # prof is a Profile object with 3 candidates and 4 voters
|
93
|
+
|
94
|
+
# generate a profile using the Impartial Anonymous Culture probability model
|
95
|
+
prof = generate_profile(3, 4, probmod = "IAC") # prof is a Profile object with 3 candidates and 4 voters
|
96
|
+
```
|
97
|
+
|
98
|
+
The `Profile` class has a number of methods that can be used to analyze the profile. For example, to determine the margin of victory between two candidates, the plurality scores, the Copeland scores, the Borda scores, the Condorcet winner, the weak Condorcet winner, and the Condorcet loser, and whether the profile is uniquely weighted, use the following code:
|
99
|
+
|
100
|
+
```python
|
101
|
+
|
102
|
+
prof = Profile([
|
103
|
+
[2, 1, 0, 3],
|
104
|
+
[3, 2, 0, 1],
|
105
|
+
[3, 1, 0, 2]],
|
106
|
+
rcounts=[2, 2, 3])
|
107
|
+
|
108
|
+
prof.display()
|
109
|
+
|
110
|
+
print(f"The margin of 1 over 3 is {prof.margin(1, 3)}")
|
111
|
+
print(f"The Plurality scores are {prof.plurality_scores()}")
|
112
|
+
print(f"The Copeland scores are {prof.copeland_scores()}")
|
113
|
+
print(f"The Borda scores are {prof.borda_scores()}")
|
114
|
+
print(f"The Condorcet winner is {prof.condorcet_winner()}")
|
115
|
+
print(f"The weak Condorcet winner is {prof.weak_condorcet_winner()}")
|
116
|
+
print(f"The Condorcet loser is {prof.condorcet_loser()}")
|
117
|
+
print(f"The profile is uniquely weighted: {prof.is_uniquely_weighted()}")
|
118
|
+
|
119
|
+
```
|
120
|
+
|
121
|
+
To use one of the many voting methods, import the function from `pref_voting.voting_methods` and apply it to the profile:
|
122
|
+
|
123
|
+
```python
|
124
|
+
from pref_voting.generate_profiles import generate_profile
|
125
|
+
from pref_voting.voting_methods import *
|
126
|
+
|
127
|
+
prof = generate_profile(3, 4) # create a profile with 3 candidates and 4 voters
|
128
|
+
split_cycle(prof) # returns the sorted list of winning candidates
|
129
|
+
split_cycle.display(prof) # displays the winning candidates
|
130
|
+
|
131
|
+
```
|
132
|
+
|
133
|
+
Additional notebooks that demonstrate how to use the package can be found in the [examples directory](https://github.com/voting-tools/pref_voting/tree/main/examples)
|
134
|
+
|
135
|
+
Some interesting political elections are analyzed using pref_voting in the [election-analysis repository](https://github.com/voting-tools/election-analysis).
|
136
|
+
|
137
|
+
Consult the documentation [https://pref-voting.readthedocs.io](https://pref-voting.readthedocs.io) for a complete overview of the package.
|
138
|
+
|
139
|
+
|
140
|
+
## Testing
|
141
|
+
|
142
|
+
To ensure that the package is working correctly, you can run the test suite using [pytest](https://docs.pytest.org/en/stable/). The test files are located in the `tests` directory. Follow the instructions below based on your setup.
|
143
|
+
|
144
|
+
### Prerequisites
|
145
|
+
|
146
|
+
- **Python 3.9 or higher**: Ensure you have a compatible version of Python installed.
|
147
|
+
- **`pytest`**: Install `pytest` if it's not already installed.
|
148
|
+
|
149
|
+
### Running the tests
|
150
|
+
|
151
|
+
If you are using **Poetry** to manage your dependencies, run the tests with:
|
152
|
+
|
153
|
+
```bash
|
154
|
+
poetry run pytest
|
155
|
+
|
156
|
+
```
|
157
|
+
|
158
|
+
From the command line, run:
|
159
|
+
|
160
|
+
```bash
|
161
|
+
pytest
|
162
|
+
```
|
163
|
+
|
164
|
+
For more detailed output, add the -v or --verbose flag:
|
165
|
+
|
166
|
+
```bash
|
167
|
+
pytest -v
|
168
|
+
```
|
169
|
+
|
170
|
+
## How to cite
|
171
|
+
|
172
|
+
If you would like to acknowledge our work in a scientific paper,
|
173
|
+
please use the following citation:
|
174
|
+
|
175
|
+
Wesley H. Holliday and Eric Pacuit (2025). pref_voting: The Preferential Voting Tools package for Python. Journal of Open Source Software, 10(105), 7020. https://doi.org/10.21105/joss.07020
|
176
|
+
|
177
|
+
### BibTeX:
|
178
|
+
|
179
|
+
```bibtex
|
180
|
+
@article{HollidayPacuit2025,
|
181
|
+
author = {Wesley H. Holliday and Eric Pacuit},
|
182
|
+
title = {pref_voting: The Preferential Voting Tools package for Python},
|
183
|
+
journal = {Journal of Open Source Software},
|
184
|
+
year = {2025},
|
185
|
+
publisher = {The Open Journal},
|
186
|
+
volume = {10},
|
187
|
+
number = {105},
|
188
|
+
pages = {7020},
|
189
|
+
doi = {10.21105/joss.07020}
|
190
|
+
}
|
191
|
+
|
192
|
+
```
|
193
|
+
|
194
|
+
Alternatively, you can cite the archived code repository
|
195
|
+
at [zenodo](https://doi.org/10.5281/zenodo.14675583).
|
196
|
+
|
197
|
+
## Contributing
|
198
|
+
|
199
|
+
If you would like to contribute to the project, please see the [contributing guidelines](CONTRIBUTING.md).
|
200
|
+
|
201
|
+
## Questions?
|
202
|
+
|
203
|
+
Feel free to [send an email](https://pacuit.org/) if you have questions about the project.
|
204
|
+
|
205
|
+
## License
|
206
|
+
|
207
|
+
[MIT](https://github.com/voting-tools/pref_voting/blob/main/LICENSE.txt)
|
208
|
+
|
@@ -0,0 +1,174 @@
|
|
1
|
+
pref_voting
|
2
|
+
==========
|
3
|
+
[](https://doi.org/10.21105/joss.07020) [](https://doi.org/10.5281/zenodo.14675583)
|
4
|
+
|
5
|
+
[](https://github.com/voting-tools/pref_voting/actions/workflows/tests.yml)
|
6
|
+
|
7
|
+
|
8
|
+
> [!NOTE]
|
9
|
+
> - [**Documentation**](https://pref-voting.readthedocs.io/)
|
10
|
+
> - [**Installation**](https://pref-voting.readthedocs.io/en/latest/installation.html)
|
11
|
+
> - [**Example Notebooks**](https://github.com/voting-tools/pref_voting/tree/main/examples)
|
12
|
+
> - [**Example Elections**](https://github.com/voting-tools/election-analysis)
|
13
|
+
> - [**► pref_voting web app**](https://pref.tools/pref_voting/)
|
14
|
+
|
15
|
+
See the [COMSOC community page](https://comsoc-community.org/tools) for an overview of other software tools related to Computational Social Choice.
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
The package can be installed using the ``pip3`` package manager:
|
20
|
+
|
21
|
+
```bash
|
22
|
+
pip3 install pref_voting
|
23
|
+
```
|
24
|
+
**Notes**:
|
25
|
+
* The package requires Python 3.10 or higher and has been tested on Python 3.12.
|
26
|
+
|
27
|
+
* Since the package uses Numba, refer to the [Numba documentation for the latest supported Python version](https://numba.readthedocs.io/en/stable/user/installing.html#version-support-information).
|
28
|
+
* If you have both Python 2 and Python 3 installed on your system, make sure to use ``pip3`` instead of pip to install packages for Python 3. Alternatively, you can use ``python3 -m pip`` to ensure you're using the correct version of pip. If you have modified your system's defaults or soft links, adjust accordingly.
|
29
|
+
|
30
|
+
See the [installation guide](https://pref-voting.readthedocs.io/en/latest/installation.html) for more detailed instructions.
|
31
|
+
|
32
|
+
## Example Usage
|
33
|
+
|
34
|
+
A profile (of linear orders over the candidates) is created by initializing a `Profile` class object. Simply provide a list of rankings (each ranking is a tuple of numbers) and a list giving the number of voters with each ranking:
|
35
|
+
|
36
|
+
```python
|
37
|
+
from pref_voting.profiles import Profile
|
38
|
+
|
39
|
+
rankings = [
|
40
|
+
(0, 1, 2, 3), # candidate 0 is ranked first, candidate 1 is ranked second, candidate 2 is ranked 3rd, and candidate 3 is ranked last.
|
41
|
+
(2, 3, 1, 0),
|
42
|
+
(3, 1, 2, 0),
|
43
|
+
(1, 2, 0, 3),
|
44
|
+
(1, 3, 2, 0)]
|
45
|
+
|
46
|
+
rcounts = [5, 3, 2, 4, 3] # 5 voters submitted the first ranking (0, 1, 2, 3), 3 voters submitted the second ranking, and so on.
|
47
|
+
|
48
|
+
prof = Profile(rankings, rcounts=rcounts)
|
49
|
+
|
50
|
+
prof.display() # display the profile
|
51
|
+
```
|
52
|
+
|
53
|
+
The function `generate_profile` is used to generate a profile for a given number of candidates and voters:
|
54
|
+
|
55
|
+
```python
|
56
|
+
from pref_voting.generate_profiles import generate_profile
|
57
|
+
|
58
|
+
# generate a profile using the Impartial Culture probability model
|
59
|
+
prof = generate_profile(3, 4) # prof is a Profile object with 3 candidates and 4 voters
|
60
|
+
|
61
|
+
# generate a profile using the Impartial Anonymous Culture probability model
|
62
|
+
prof = generate_profile(3, 4, probmod = "IAC") # prof is a Profile object with 3 candidates and 4 voters
|
63
|
+
```
|
64
|
+
|
65
|
+
The `Profile` class has a number of methods that can be used to analyze the profile. For example, to determine the margin of victory between two candidates, the plurality scores, the Copeland scores, the Borda scores, the Condorcet winner, the weak Condorcet winner, and the Condorcet loser, and whether the profile is uniquely weighted, use the following code:
|
66
|
+
|
67
|
+
```python
|
68
|
+
|
69
|
+
prof = Profile([
|
70
|
+
[2, 1, 0, 3],
|
71
|
+
[3, 2, 0, 1],
|
72
|
+
[3, 1, 0, 2]],
|
73
|
+
rcounts=[2, 2, 3])
|
74
|
+
|
75
|
+
prof.display()
|
76
|
+
|
77
|
+
print(f"The margin of 1 over 3 is {prof.margin(1, 3)}")
|
78
|
+
print(f"The Plurality scores are {prof.plurality_scores()}")
|
79
|
+
print(f"The Copeland scores are {prof.copeland_scores()}")
|
80
|
+
print(f"The Borda scores are {prof.borda_scores()}")
|
81
|
+
print(f"The Condorcet winner is {prof.condorcet_winner()}")
|
82
|
+
print(f"The weak Condorcet winner is {prof.weak_condorcet_winner()}")
|
83
|
+
print(f"The Condorcet loser is {prof.condorcet_loser()}")
|
84
|
+
print(f"The profile is uniquely weighted: {prof.is_uniquely_weighted()}")
|
85
|
+
|
86
|
+
```
|
87
|
+
|
88
|
+
To use one of the many voting methods, import the function from `pref_voting.voting_methods` and apply it to the profile:
|
89
|
+
|
90
|
+
```python
|
91
|
+
from pref_voting.generate_profiles import generate_profile
|
92
|
+
from pref_voting.voting_methods import *
|
93
|
+
|
94
|
+
prof = generate_profile(3, 4) # create a profile with 3 candidates and 4 voters
|
95
|
+
split_cycle(prof) # returns the sorted list of winning candidates
|
96
|
+
split_cycle.display(prof) # displays the winning candidates
|
97
|
+
|
98
|
+
```
|
99
|
+
|
100
|
+
Additional notebooks that demonstrate how to use the package can be found in the [examples directory](https://github.com/voting-tools/pref_voting/tree/main/examples)
|
101
|
+
|
102
|
+
Some interesting political elections are analyzed using pref_voting in the [election-analysis repository](https://github.com/voting-tools/election-analysis).
|
103
|
+
|
104
|
+
Consult the documentation [https://pref-voting.readthedocs.io](https://pref-voting.readthedocs.io) for a complete overview of the package.
|
105
|
+
|
106
|
+
|
107
|
+
## Testing
|
108
|
+
|
109
|
+
To ensure that the package is working correctly, you can run the test suite using [pytest](https://docs.pytest.org/en/stable/). The test files are located in the `tests` directory. Follow the instructions below based on your setup.
|
110
|
+
|
111
|
+
### Prerequisites
|
112
|
+
|
113
|
+
- **Python 3.9 or higher**: Ensure you have a compatible version of Python installed.
|
114
|
+
- **`pytest`**: Install `pytest` if it's not already installed.
|
115
|
+
|
116
|
+
### Running the tests
|
117
|
+
|
118
|
+
If you are using **Poetry** to manage your dependencies, run the tests with:
|
119
|
+
|
120
|
+
```bash
|
121
|
+
poetry run pytest
|
122
|
+
|
123
|
+
```
|
124
|
+
|
125
|
+
From the command line, run:
|
126
|
+
|
127
|
+
```bash
|
128
|
+
pytest
|
129
|
+
```
|
130
|
+
|
131
|
+
For more detailed output, add the -v or --verbose flag:
|
132
|
+
|
133
|
+
```bash
|
134
|
+
pytest -v
|
135
|
+
```
|
136
|
+
|
137
|
+
## How to cite
|
138
|
+
|
139
|
+
If you would like to acknowledge our work in a scientific paper,
|
140
|
+
please use the following citation:
|
141
|
+
|
142
|
+
Wesley H. Holliday and Eric Pacuit (2025). pref_voting: The Preferential Voting Tools package for Python. Journal of Open Source Software, 10(105), 7020. https://doi.org/10.21105/joss.07020
|
143
|
+
|
144
|
+
### BibTeX:
|
145
|
+
|
146
|
+
```bibtex
|
147
|
+
@article{HollidayPacuit2025,
|
148
|
+
author = {Wesley H. Holliday and Eric Pacuit},
|
149
|
+
title = {pref_voting: The Preferential Voting Tools package for Python},
|
150
|
+
journal = {Journal of Open Source Software},
|
151
|
+
year = {2025},
|
152
|
+
publisher = {The Open Journal},
|
153
|
+
volume = {10},
|
154
|
+
number = {105},
|
155
|
+
pages = {7020},
|
156
|
+
doi = {10.21105/joss.07020}
|
157
|
+
}
|
158
|
+
|
159
|
+
```
|
160
|
+
|
161
|
+
Alternatively, you can cite the archived code repository
|
162
|
+
at [zenodo](https://doi.org/10.5281/zenodo.14675583).
|
163
|
+
|
164
|
+
## Contributing
|
165
|
+
|
166
|
+
If you would like to contribute to the project, please see the [contributing guidelines](CONTRIBUTING.md).
|
167
|
+
|
168
|
+
## Questions?
|
169
|
+
|
170
|
+
Feel free to [send an email](https://pacuit.org/) if you have questions about the project.
|
171
|
+
|
172
|
+
## License
|
173
|
+
|
174
|
+
[MIT](https://github.com/voting-tools/pref_voting/blob/main/LICENSE.txt)
|
@@ -0,0 +1 @@
|
|
1
|
+
__version__ = '1.16.31'
|