Utility functions
Vector Distances
kendall_tau(ordering_1, ordering_2, search_pairs)
Calculate the unnormalized Kendall tau distance between two orderings.
The Kendall tau rank distance is a metric that counts the number of pairwise disagreements between two ranking lists. The larger the distance, the more dissimilar the two lists are. Kendall tau distance is also called bubble-sort distance. An ordering holds the option names in the order of their rank (rank=index).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ordering_1
|
IntArray
|
First (NumPy) array containing ranked options. |
required |
ordering_2
|
IntArray
|
The second ordering array. |
required |
search_pairs
|
Sequence[tuple[int, int]]
|
Index pairs. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
float
|
Normalized kendall tau distance |
Source code in src/utils/distance_functions.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | |
kendall_tau_on_ranks(rank_arr_1, rank_arr_2, search_pairs, color_vec)
Beware: don't use this for orderings!
This function calculates the kendal tau distance between two rank vektors. (The Kendall tau rank distance is a metric that counts the number of pairwise disagreements between two ranking lists. The larger the distance, the more dissimilar the two lists are. Kendall tau distance is also called bubble-sort distance). Rank vectors hold the rank of each option (option = index). Not to be confused with an ordering (or sequence) where the vector holds options and the index is the rank.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rank_arr_1
|
FloatArray
|
First Array containing the ranks of each option. |
required |
rank_arr_2
|
FloatArray
|
The second rank array. |
required |
search_pairs
|
Sequence[tuple[int, int]]
|
The pairs of indices. |
required |
color_vec
|
IntArray
|
(IntArray): The vector of colors (for efficiency). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Kendall tau distance. |
Source code in src/utils/distance_functions.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |
spearman(ordering_1, ordering_2, _search_pairs=None)
This calculates the normalized Spearman distance between two orderings. Spearman's foot rule is a measure of the distance between ranked lists. It is given as the sum of the absolute differences between the ranks of the two orderings (values from 0 to n-1 in any order).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ordering_1
|
IntArray
|
The first (NumPy) array containing the option's ranks. |
required |
ordering_2
|
IntArray
|
The second rank array. |
required |
_search_pairs
|
This parameter is intentionally unused. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Spearman distance |
Source code in src/utils/distance_functions.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
spearman_distance(rank_arr_1, rank_arr_2)
Beware: don't use this for orderings!
This function calculates the Spearman distance between two rank vektors. Spearman's foot rule is a measure of the distance between ranked lists. It is given as the sum of the absolute differences between the ranks of the two lists. This function is meant to work with numeric values as well. Hence, we only assume the rank values to be comparable (e.q. normalized).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rank_arr_1
|
FloatArray
|
Array containing the ranks of each option |
required |
rank_arr_2
|
FloatArray
|
The second rank array. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The Spearman distance |
Source code in src/utils/distance_functions.py
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | |
unnormalized_kendall_tau(ordering_1, ordering_2, search_pairs)
This function calculates the kendal tau distance on two orderings. An ordering holds the option names in the order of their rank (rank=index).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ordering_1
|
IntArray
|
First Array containing ranked options. |
required |
ordering_2
|
IntArray
|
The second ordering array. |
required |
search_pairs
|
Sequence[tuple[int, int]]
|
Index pairs (for efficiency). |
required |
Returns:
| Type | Description |
|---|---|
int
|
The kendall tau distance |
Source code in src/utils/distance_functions.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | |
Simulation Indicators
get_grid_colors(model)
Returns the current grid state as a list of rows. Each row is a list of cell colors. Assumes that the cells were created in row-major order and stored in model.color_cells.
Source code in src/utils/metrics.py
2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
Social Choice
Here we define the social welfare functions that can be used in the simulation. Beware: We assume the preference relation in the following (unconventional) way on purpose. pref_table: numpy matrix with one row per agent, column number is option number and the values (each in [0,1]) are normalized ranking values. The purpose of this is to allow for non-discrete and non-equidistant rankings.
approval_voting(pref_table)
This function implements the approval voting social welfare function. Beware: Input is a preference table (values define a ranking, index=option), but the output is a ranking/an ordering (values represent options).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pref_table
|
ndarray
|
Agent's preferences (disagreement) as matrix. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Resulting preference ranking (beware: not a pref. relation). |
Source code in src/utils/social_welfare_functions.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | |
complete_ranking(ranking, num_options)
This function adds options that are not in the ranking in a random order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ranking
|
ndarray
|
Partial ranking of option indices. |
required |
num_options
|
int
|
The total number of options. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Completed ranking of length |
Source code in src/utils/social_welfare_functions.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |
continuous_score_voting(pref_table)
This function implements a continuous score voting based on disagreement. Beware: Input is a preference table (values define a ranking, index=option), but the output is a ranking/an ordering (values represent options).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pref_table
|
ndarray
|
Agent's preferences (disagreement) as matrix. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Resulting preference ranking (beware: not a pref. relation). |
Source code in src/utils/social_welfare_functions.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | |
imp_prepr_for_approval(pref_table)
This is just like preprocessing_for_approval, but more intelligent. It sets the threshold depending on the variances.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pref_table
|
ndarray
|
Preferences table. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Binary approvals with shape of |
Source code in src/utils/social_welfare_functions.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | |
majority_rule(pref_table)
This function implements the majority rule social welfare function. Beware: Input is a preference table (values define a ranking, index=option), but the output is a ranking/an ordering (values represent options).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pref_table
|
ndarray
|
Preferences (disagreement values) per agent (rows) per option (cols). |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Resulting preference ranking (beware: not a pref. relation) |
Source code in src/utils/social_welfare_functions.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | |
preprocessing_for_approval(pref_table, threshold=None)
Interpret values below threshold as approval.
This function prepares the preference table for approval voting by interpreting every value below a threshold as an approval. Beware: the values are distance/disagreement => smaller = less disagreement The standard threshold is 1/m (m = number of options). The reasoning is that if the preferences are normalized, 1/m ensures the threshold to be proportionate to the number of options. It also ensures that, on average, half of the options will be approved. The actual number of approved options, however, can still vary depending on the specific values in the preference table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pref_table
|
ndarray
|
Preferences table. |
required |
threshold
|
float | None
|
Approval threshold; defaults to 1/m. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Binary approvals with shape of |
Source code in src/utils/social_welfare_functions.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | |
run_tie_breaking_preparation_for_majority(pref_table, noise_factor=100)
This function prepares the preference table for majority rule such that it handles ties in the voters' preferences. Because majority rule cannot usually deal with ties. The tie breaking is randomized to ensure anonymity and neutrality.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pref_table
|
ndarray
|
Preferences per agent (rows) per option (cols). |
required |
noise_factor
|
int
|
Controls noise magnitude. |
100
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Table without ties in first choices. |
Source code in src/utils/social_welfare_functions.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | |