Question

In: Computer Science

Write a python or java code which can calculate the Shapely-Shubik and Banzhaf indices to determine...

Write a python or java code which can calculate the Shapely-Shubik and Banzhaf indices to determine which state in America has the most power in the primary election.

Solutions

Expert Solution

%matplotlib inline

import powerindex as px

game=px.Game(quota=51,weights=[51,49])


game.calc_banzhaf()

print(game.banzhaf)  

#output   [1.0, 0.0]


game.calc_shapley_shubik()

print(game.shapley_shubik)

#output [1.0, 0.0]


game=px.Game(51,weights=[50,50])

game.calc()

print(game.banzhaf)

print(game.shapley_shubik)

#output [0.5, 0.5]

        [0.5, 0.5]


game=px.Game(4,[3, 2, 1])

game.calc() # again it calculates all available indices

print("Banzhaf index:")

print(game.banzhaf)

print("Shapley-Shubik index:")

print(game.shapley_shubik)

#Output Banzhaf index:

      # [0.6, 0.2, 0.2]

      # Shapley-Shubik index:

      # [0.6666666666666667, 0.16666666666666669, 0.16666666666666669]


countries={"California":4,"Florida":4,"Georgia":4," Louisiana":2,"Michigan":2,"New Jersey":1}

parties=[px.Party(countries[country],country) for country in countries]

game=px.Game(12,parties=parties)

game.calc()

print("Banzhaf index:")

print(game.banzhaf)

print("Shapley-Shubik index:")

print(game.shapley_shubik)

# run this code and you will get this output

      #Banzhaf index:

      # [0.238, 0.238, 0.238, 0.143, 0.143, 0.00]

      # Shapley-Shubik index:

      # [0.233, 0.233, 0.233, 0.150, 0.150, 0.00]


Related Solutions

can you please convert this python code into java? Python code is as shown below: #...
can you please convert this python code into java? Python code is as shown below: # recursive function def row_puzzle_rec(row, pos, visited):    # if the element at the current position is 0 we have reached our goal    if row[pos] == 0:        possible = True    else:        # make a copy of the visited array        visited = visited[:]        # if the element at the current position has been already visited then...
Parse string java code Write a recursive program that can calculate the value of a given...
Parse string java code Write a recursive program that can calculate the value of a given polynomial in a string, which is not more than the tenth order, for the given x. The polynomial will be given in the following format and should display the value of the polynomial for spaced-out x Using index of for -/+
write a python code to Determine the binary and decimal representations of the following hexadecimal numbers....
write a python code to Determine the binary and decimal representations of the following hexadecimal numbers. Store answers in the appropriate variables in the .py file. (a) 0xfca1 (b) 0xc4d8
PYTHON Write a program for an event organizer. The code can be as short as 7...
PYTHON Write a program for an event organizer. The code can be as short as 7 lines; 9 lines are good enough! a) Request the organizer to enter a participant’s full name in a single input. The input can take two different formats: i. l_name, f_name [1.5 pts] ii. f_name l_name [1.5 pts] b) Print a customized greeting message using the first name, regardless of the formats: “Dear f_name, welcome to the party!” with only the 1 st letter of...
Based on the following Python code, write a Python statement which outputs “December” when the value...
Based on the following Python code, write a Python statement which outputs “December” when the value of the Month is 12 or “November” when the value of the Month is 11 or “Neither” when value of the Month is neither 11 or 12. The user enters the value which is store in the variable namedMonth. Month = int(input("Please enter the value of the month in numerical format such as 11 for November, or 12 for December: " )) sing Python
Apply The knowledge of “Mapping Models to code” Concept to write Chunks of java code which...
Apply The knowledge of “Mapping Models to code” Concept to write Chunks of java code which maps the following relationships: a. Aggregation b. Composition c. Generalization d. Realization e. Dependency
Write a python code to generate a random undirected weighted graph and calculate the shortest distance...
Write a python code to generate a random undirected weighted graph and calculate the shortest distance from node i to node j (shortest path algorithm) where i is not equal to j. Store the results in text file. Thank you!
PLEASE CODE IN JAVA In this assignment you will write a program in that can figure...
PLEASE CODE IN JAVA In this assignment you will write a program in that can figure out a number chosen by a human user. The human user will think of a number between 1 and 100. The program will make guesses and the user will tell the program to guess higher or lower.                                                                   Requirements The purpose of the assignment is to practice writing functions. Although it would be possible to write the entire program in the main function, your...
please write the java code so it can run on jGRASP Thanks! CODE 1 1 /**...
please write the java code so it can run on jGRASP Thanks! CODE 1 1 /** 2 * SameArray2.java 3 * @author Sherri Vaseashta4 * @version1 5 * @see 6 */ 7 import java.util.Scanner;8 public class SameArray29{ 10 public static void main(String[] args) 11 { 12 int[] array1 = {2, 4, 6, 8, 10}; 13 int[] array2 = new int[5]; //initializing array2 14 15 //copies the content of array1 and array2 16 for (int arrayCounter = 0; arrayCounter < 5;...
Write this code in java and don't forget to comment every step. Write a method which...
Write this code in java and don't forget to comment every step. Write a method which asks a baker how hot their water is, and prints out whether it is OK to make bread with the water. If the water is at or above 110F, your method should print "Too Warm." If the water is below 90.5F, print "Too Cold." If it is in between, print "Just right to bake!" For example, if the user inputs the number 100.5, the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT