@@ -27,6 +27,32 @@ def permute_voters(
27
27
-------
28
28
np.ndarray
29
29
Ordinal votes.
30
+
31
+ Examples
32
+ --------
33
+
34
+ .. testcode::
35
+
36
+ from prefsampling.ordinal import didi
37
+ from prefsampling.core import permute_voters
38
+
39
+ # Get some votes
40
+ ordinal_votes = didi(2, 3, (0.5, 0.2, 0.1))
41
+
42
+ # Randomly permute the voters
43
+ permute_voters(ordinal_votes)
44
+
45
+ # The syntax is the same with approval votes
46
+
47
+ from prefsampling.approval import resampling
48
+
49
+ approval_votes = resampling(2, 3, 0.5, 0.2)
50
+ permute_voters(approval_votes)
51
+
52
+ # You can set the seed for reproducibility
53
+
54
+ permute_voters(approval_votes, seed=234)
55
+
30
56
"""
31
57
rng = np .random .default_rng (seed )
32
58
rng .shuffle (votes )
@@ -59,6 +85,31 @@ def rename_candidates(
59
85
-------
60
86
list[set[int]] or np.ndarray
61
87
Votes with renamed candidates.
88
+
89
+ Examples
90
+ --------
91
+
92
+ .. testcode::
93
+
94
+ from prefsampling.ordinal import didi
95
+ from prefsampling.core import rename_candidates
96
+
97
+ # Get some votes
98
+ ordinal_votes = didi(2, 3, (0.5, 0.2, 0.1))
99
+
100
+ # Randomly permute the voters
101
+ rename_candidates(ordinal_votes)
102
+
103
+ # With approval votes, you need to give the number of candidates
104
+
105
+ from prefsampling.approval import resampling
106
+
107
+ approval_votes = resampling(2, 3, 0.5, 0.2)
108
+ rename_candidates(approval_votes, num_candidates=3)
109
+
110
+ # You can set the seed for reproducibility
111
+
112
+ permute_voters(approval_votes, num_candidates=3, seed=234)
62
113
"""
63
114
rng = np .random .default_rng (seed )
64
115
@@ -85,8 +136,11 @@ def resample_as_central_vote(
85
136
votes : np .ndarray | list [set [int ]], sampler : Callable , sampler_parameters : dict
86
137
) -> np .ndarray | list [set [int ]]:
87
138
"""
88
- Resamples the votes by using them as the central vote of a given sampler. Only samplers that
89
- accept a :code:`central_vote` argument can be used.
139
+ Resamples the votes by using them as the central vote of a given sampler. The outcome is
140
+ obtained as follows: for each input vote, we pass it to the sampler as central vote; a single
141
+ vote is then resampled and added to the outcome.
142
+
143
+ Only samplers that accept a :code:`central_vote` argument can be used.
90
144
91
145
Votes are copied before being returned to avoid loss of data.
92
146
@@ -104,6 +158,36 @@ def resample_as_central_vote(
104
158
-------
105
159
list[set[int]] or np.ndarray
106
160
Votes resampled.
161
+
162
+ Examples
163
+ --------
164
+
165
+ .. testcode::
166
+
167
+ from prefsampling.ordinal import urn, mallows
168
+ from prefsampling.core import rename_candidates
169
+
170
+ # Get some votes
171
+ ordinal_votes = urn(2, 3, 0.2)
172
+
173
+ # We resample them by passing them as central vote to a Mallows' model
174
+ resample_as_central_vote(ordinal_votes, mallows, {'phi': 0.3})
175
+
176
+ # The syntax is the same with approval votes
177
+
178
+ from prefsampling.approval import urn, resampling
179
+
180
+ approval_votes = urn(2, 3, 0.5, 0.2)
181
+ resample_as_central_vote(approval_votes, resampling, {'phi': 0.4, 'p': 0.8})
182
+
183
+ # To ensure reproducibility, you need to pass the seed everywhere
184
+ seed = 4234
185
+ approval_votes = urn(2, 3, 0.5, 0.2, seed=seed)
186
+ resample_as_central_vote(
187
+ approval_votes,
188
+ resampling,
189
+ {'phi': 0.4, 'p': 0.8, 'seed':seed}
190
+ )
107
191
"""
108
192
res = deepcopy (votes )
109
193
sampler_parameters ["num_voters" ] = 1
0 commit comments