Allocated Score: Difference between revisions
→Python Implementation
Dr. Edmonds (talk | contribs) No edit summary |
|||
Line 37:
<source lang="python">
def Allocated_Score(
"""Credit to https://electowiki.org/wiki/Allocated_Score
Allocated Score is another name for STAR-PR
Parameters:
#Normalize score matrix▼
ballots = pd.DataFrame(S.values/K, columns=S.columns)▼
"""
▲ # Normalize score matrix
#Find number of voters and quota size▼
V = ballots.shape[0]▼
▲ # Find number of voters and quota size
ballot_weight = pd.Series(np.ones(V),name='weights')▼
# Populate winners in a loop
winner_list = []
while len(winner_list) <
weighted_scores = ballots.multiply(ballot_weight, axis="index")
# Select winner
winner_list.append(w)▼
#
#Create lists for manipulation▼
cand_df = pd.concat([ballot_weight,weighted_scores[w]], axis=1).copy() ▼
cand_df_sort = cand_df.sort_values(by=[w], ascending=False).copy() ▼
#find the score where a quota is filled▼
split_point = cand_df_sort[cand_df_sort['weights'].cumsum() < quota][w].min()▼
#Amount of ballot for voters who voted more than the split point▼
spent_above = cand_df[cand_df[w] > split_point]['weights'].sum()▼
#Allocate all ballots above split point▼
if spent_above>0: ▼
cand_df.loc[cand_df[w] > split_point, 'weights'] = 0.0▼
#Amount of ballot for voters who gave a score on the split point▼
weight_on_split = cand_df[cand_df[w] == split_point]['weights'].sum()▼
#
ballots.drop(winner, axis=1, inplace=True)
if weight_on_split>0: ▼
spent_value = (quota - spent_above)/weight_on_split▼
#Take the spent value from the voters on the threshold evenly▼
cand_df.loc[cand_df[w] == split_point, 'weights'] = cand_df.loc[cand_df[w] == split_point, 'weights'] * (1 - spent_value)▼
ballot_weight = cand_df['weights'].clip(0.0,1.0)▼
▲ # Create lists for manipulation
▲ # find the score where a quota is filled
▲ winner
▲ ].min()
▲ # Amount of ballot for voters who voted more than the split point
▲ # Allocate all ballots above split point
▲ # Amount of ballot for voters who gave a score on the split point
# Fraction of ballot on split needed to be spent
▲ spent_value = (quota - spent_above) / weight_on_split
▲ # Take the spent value from the voters on the threshold evenly
▲ cand_df.loc[cand_df[
cand_df[winner] == split_point, "weights"
] * (1 - spent_value)
</source>
|