Sequentially Spent Score: Difference between revisions
Add python code for clarity of implementation
Dr. Edmonds (talk | contribs) No edit summary |
Dr. Edmonds (talk | contribs) (Add python code for clarity of implementation) |
||
Line 16:
Specifically, in the second step, the score given to the winner is divided by (the total score given to that winner) * (Total number of Winners)/ (Total number of voters). This is a number greater than 1 and hence reduces the amount spent on that winner.
==Python Implementation==
Given a Pandas dataframe '''S''' with columns representing candidates and rows representing voters the entries would encode the score of all the ballots. For a max score of '''K''' and a desired number of winners '''W'''.
<source lang="python">
#Normalize score matrix
S_wrk = pd.DataFrame(S.values/K, columns=S_in.columns)
#Find number of voters
V = S_wrk.shape[0]
#Populate winners in a loop
winner_list = []
while len(winner_list) < W:
#Select winner
w = S_wrk.sum().idxmax()
#Add winner to list
winner_list.append(w)
#Check for Surplus
surplus_factor = max( S_wrk[w].sum() *W/V , 1.0)
#Score spent on winner by each voter
score_spent = S_wrk[w]/ surplus_factor
#Total score left to be spent by each voter
ballot_weight = np.clip(ballot_weight-score_spent,0.0,1.0)
#Update Ballots as the minimum of original score and score remaining
mins = np.minimum(S_wrk.values,ballot_weight.values[:, np.newaxis])
S_wrk = pd.DataFrame(mins, columns = S_wrk.columns)
</source>
==Variants==
|