Allocated Score: Difference between revisions

No edit summary
Line 37:
 
<source lang="python">
import pandas as pd
import numpy as np
 
def Allocated_Score(Kballots: pd.DataFrame, Wseats: int, Smax_score: int):
"""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
ballots = pd.DataFrame(Sballots.values /K max_score, columns=Sballots.columns)
V = ballots.shape[0]
 
quota = V/W
# Find number of voters and quota size
ballot_weight = pd.Series(np.ones(V),name='weights')
Vvoters = ballots.shape[0]
#Populatequota winners= invoters a/ loopseats
ballot_weight = pd.Series(np.ones(Vvoters), name='"weights'")
 
# Populate winners in a loop
winner_list = []
while len(winner_list) < Wseats:
 
weighted_scores = ballots.multiply(ballot_weight, axis="index")
 
# Select winner
wwinner = weighted_scores.sum().idxmax()
#Add winner to list
winner_list.append(w)
 
#remove Add winner fromto ballotlist
ballotswinner_list.dropappend(w, axis=1, inplace=Truewinner)
#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()
 
#Fraction ofremove ballotwinner onfrom split needed to be spentballot
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
return winner_list
cand_df = pd.concat([ballot_weight, weighted_scores[wwinner]], axis=1).copy()
cand_df_sort = cand_df.sort_values(by=[wwinner], ascending=False).copy()
 
# find the score where a quota is filled
split_point = cand_df_sort[cand_df_sort['"weights'"].cumsum() < quota][w].min()
winner
].min()
 
# Amount of ballot for voters who voted more than the split point
spent_above = cand_df[cand_df[wwinner] > split_point]['"weights'"].sum()
 
# Allocate all ballots above split point
if spent_above > 0:
cand_df.loc[cand_df[wwinner] > 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[wwinner] == split_point]['"weights'"].sum()
 
# Fraction of ballot on split needed to be spent
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[wwinner] == split_point, '"weights'"] = cand_df.loc[cand_df[w] == split_point, 'weights'] * (1 - spent_value)
cand_df[winner] == split_point, "weights"
] * (1 - spent_value)
 
ballot_weight = cand_df['"weights'"].clip(0.0, 1.0)
 
return winner_list.append(w)
</source>
 
29

edits