The actual routine scripts are provided and shown below, however links to ".txt "files are more useful and given first:
2) Shapley-Shubik index calculator
3) Randomized Shapley-Shubik index calculator
4) Johnston index calculator
The Banzhaf power
index routine
//
Start of the Banzhaf index calculator function
function
banzhaf_index = banzhaf_pir(weights,quota)
totalwinning
=0;
for
i = 1:length(weights)
tally(i)=0,
end
for
i=1:(2^(length(weights))-1)
total(i)=0;
for
k=1:length(weights)
total(i)=total(i)+bitget(i,k)*weights(k);
end
if
(total(i)>=quota) then
totalwinning=totalwinning+1,
for
k=1:length(weights)
if
(((total(i)-bitget(i,k)*weights(k))
tally(k)=tally(k)+bitget(i,k),
end
end
end
end
crit_sum=0;
for
k=1:length(weights)
crit_sum=crit_sum+tally(k);
end
banzhaf_index=tally;
for
k=1:length(weights)
banzhaf_index(k)=banzhaf_index(k)/crit_sum;
end
endfunction
//
End of the Banzhaf index calculator function
//**************************************
//**************************************
//
Enter the weights and quota here
//
Be sure the weights and quota are non-negative and that there are no more than
10 weights
weights
= [4 2 1 ];
quota
=5;
//
You have entered the weights and quota
//**************************************
//**************************************
banzhaf_voting_index="Null--Invalid
input.Check that the quota and weights are non-negative and that there are no
more than 10 candidates";
good_input=((quota>0)&(length(weights)<11 amp="" min="" weights="">0));11>
if
(good_input) then banzhaf_voting_index=banzhaf_pir(weights,quota);
end
banzhaf_voting_index
The Shapley-Shubik
power index routine
//
Start of the Shapley-Shubik index calculator function
function
shapley_index = shapley_pir(weights, quota)
for
i= 1:length(weights)
totals(i)=0,
x(i)=i,
end
y=perms(x);
for
j = 1:size(y,"r")
permutation=y(j,:);
i=1;
total=0;
decider(j)
=0;
while
(decider(j)==0)
total=total+weights(permutation(i)),
if
(total>=quota) then
decider(j)
= i,
else
i=i+1,
end
end
decider(j)
= permutation(i);
end
for
i = 1:length(decider)
totals(decider(i))=totals(decider(i))+1,
end
crit_sum=0;
for
k=1:length(weights)
crit_sum=crit_sum+totals(k);
end
shapley_index=totals;
for
k=1:length(weights)
shapley_index(k)=shapley_index(k)/crit_sum;
end
endfunction
//
End of the Shapley-Shubik index calculator function
//**************************************
//**************************************
//
Enter the weights and quota here
//
Be sure the weights and quota are non-negative and that there are no more than
10 weights
weights
= [4 2 1 ];
quota
=5;
//
You have entered the weights and quota
//**************************************
//**************************************
shapley_voting_index="Null--Invalid
input.Check that the quota and weights are non-negative and that there are no
more than 8 candidates";
good_input=((quota>0)&(length(weights)<9 amp="" min="" weights="">0));9>
if
(good_input) then shapley_voting_index=shapley_pir(weights,quota);
end
shapley_voting_index
The randomized
Shapley-Shubik power index routine
//
Start of the randomized Shapley-Shubik index calculator function
function
r_shapley_index = r_shapley_pir(weights, quota)
for
i= 1:length(weights)
totals(i)=0,
x(i)=i,
end
//**************************************
//**************************************
//
Enter the sample size here
Sample_Size=100000;
//**************************************
//**************************************
for
j = 1:Sample_Size
permutation=grand(1,'prm',x);
i=1;
total=0;
decider(j)
=0;
while
(decider(j)==0)
total=total+weights(permutation(i)),
if
(total>=quota) then
decider(j)
= i,
else
i=i+1,
end
end
decider(j)
= permutation(i);
end
for
i = 1:length(decider)
totals(decider(i))=totals(decider(i))+1,
end
crit_sum=0;
for
k=1:length(weights)
crit_sum=crit_sum+totals(k);
end
r_shapley_index=totals;
for
k=1:length(weights)
r_shapley_index(k)=r_shapley_index(k)/crit_sum;
end
endfunction
//
End of the randomized Shapley-Shubik index calculator function
//**************************************
//**************************************
//
Enter the weights and quota here
//
Be sure the weights and quota are non-negative and that there are between 9 and
20 weights
weights
= [9 9 9 9 8 8 8 6 5 5 4 3 ];
quota
=12;
//
You have entered the weights and quota
//**************************************
//**************************************
r_shapley_voting_index="Null--Invalid
input.Check that the quota and weights are non-negative and that there are
between 9 and 20";
good_input=((quota>0)&(length(weights)<21 amp="" length="" weights="">8)&(min(weights)>0));21>
if
(good_input) then r_shapley_voting_index=r_shapley_pir(weights, quota);
end
r_shapley_voting_index
The Johnston power
index routine
//
Start of the Johnston index calculator function
function
johnston_index = johnston_pir(weights,quota)
totalwinning
=0;
temp=0;
for
i = 1:length(weights)
tally(i)=0,
end
for
i=1:(2^(length(weights))-1)
total(i)=0;
for
k=1:length(weights)
total(i)=total(i)+bitget(i,k)*weights(k);
end
if
(total(i)>=quota) then
totalwinning=totalwinning+1,
for
k=1:length(weights)
if
(((total(i)-bitget(i,k)*weights(k))
temp=0;
for
l=1:length(weights)
temp=temp+bitget(i,l),
end
tally(k)=tally(k)+1/temp,
end
end
end
end
crit_sum=0;
for
k=1:length(weights)
crit_sum=crit_sum+tally(k);
end
johnston_index=tally;
for
k=1:length(weights)
johnston_index(k)=johnston_index(k)/crit_sum;
end
endfunction
//
End of the Johnston index calculator function
//**************************************
//**************************************
//
Enter the weights and quota here
//
Be sure the weights and quota are non-negative and that there are between 9 and
20 weights
weights
= [4 2 1 ];
quota
=5;
//
You have entered the weights and quota
//**************************************
//**************************************
johnston_voting_index="Null--Invalid
input.Check that the quota and weights are non-negative and that there are no
more than 10 candidates";
good_input=((quota>0)&(length(weights)<11 amp="" min="" weights="">0));11>
if
(good_input) then johnston_voting_index=johnston_pir(weights,quota);
end
johnston_voting_index
No comments:
Post a Comment